blob: dcaf8802a281420cb4a8a11bb6ba807baa06dc04 [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;
1539 xmlRelaxNGDocumentPtr inc = tmp->_private;
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) ||
3981 (cur->type == XML_RELAXNG_DEF)) {
3982 /*
3983 * Don't go within elements or attributes or string values.
3984 * Just gather the element top list
3985 */
3986 if (cur->content != NULL) {
3987 parent = cur;
3988 cur = cur->content;
3989 tmp = cur;
3990 while (tmp != NULL) {
3991 tmp->parent = parent;
3992 tmp = tmp->next;
3993 }
3994 continue;
3995 }
3996 }
3997 if (cur == def)
3998 break;
3999 if (cur->next != NULL) {
4000 cur = cur->next;
4001 continue;
4002 }
4003 do {
4004 cur = cur->parent;
4005 if (cur == NULL)
4006 break;
4007 if (cur == def)
4008 return (ret);
4009 if (cur->next != NULL) {
4010 cur = cur->next;
4011 break;
4012 }
4013 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004014 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004015 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004016}
Daniel Veillard4c004142003-10-07 11:33:24 +00004017
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004018/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004019 * xmlRelaxNGCheckChoiceDeterminism:
4020 * @ctxt: a Relax-NG parser context
4021 * @def: the choice definition
4022 *
4023 * Also used to find indeterministic pattern in choice
4024 */
4025static void
4026xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004027 xmlRelaxNGDefinePtr def)
4028{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004029 xmlRelaxNGDefinePtr **list;
4030 xmlRelaxNGDefinePtr cur;
4031 int nbchild = 0, i, j, ret;
4032 int is_nullable = 0;
4033 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004034 xmlHashTablePtr triage = NULL;
4035 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004036
Daniel Veillard4c004142003-10-07 11:33:24 +00004037 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4038 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004039
Daniel Veillarde063f482003-03-21 16:53:17 +00004040 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004041 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004042
Daniel Veillardfd573f12003-03-16 17:52:32 +00004043 /*
4044 * Don't run that check in case of error. Infinite recursion
4045 * becomes possible.
4046 */
4047 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004048 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004049
4050 is_nullable = xmlRelaxNGIsNullable(def);
4051
4052 cur = def->content;
4053 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004054 nbchild++;
4055 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004056 }
4057
4058 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004059 sizeof(xmlRelaxNGDefinePtr
4060 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004061 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004062 xmlRngPErrMemory(ctxt, "building choice\n");
4063 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004064 }
4065 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004066 /*
4067 * a bit strong but safe
4068 */
4069 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004070 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004071 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004072 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004073 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004074 cur = def->content;
4075 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004076 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4077 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4078 is_triable = 0;
4079 } else if (is_triable == 1) {
4080 xmlRelaxNGDefinePtr *tmp;
4081 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004082
Daniel Veillard4c004142003-10-07 11:33:24 +00004083 tmp = list[i];
4084 while ((*tmp != NULL) && (is_triable == 1)) {
4085 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4086 res = xmlHashAddEntry2(triage,
4087 BAD_CAST "#text", NULL,
4088 (void *) cur);
4089 if (res != 0)
4090 is_triable = -1;
4091 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4092 ((*tmp)->name != NULL)) {
4093 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4094 res = xmlHashAddEntry2(triage,
4095 (*tmp)->name, NULL,
4096 (void *) cur);
4097 else
4098 res = xmlHashAddEntry2(triage,
4099 (*tmp)->name, (*tmp)->ns,
4100 (void *) cur);
4101 if (res != 0)
4102 is_triable = -1;
4103 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4104 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4105 res = xmlHashAddEntry2(triage,
4106 BAD_CAST "#any", NULL,
4107 (void *) cur);
4108 else
4109 res = xmlHashAddEntry2(triage,
4110 BAD_CAST "#any", (*tmp)->ns,
4111 (void *) cur);
4112 if (res != 0)
4113 is_triable = -1;
4114 } else {
4115 is_triable = -1;
4116 }
4117 tmp++;
4118 }
4119 }
4120 i++;
4121 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004122 }
4123
Daniel Veillard4c004142003-10-07 11:33:24 +00004124 for (i = 0; i < nbchild; i++) {
4125 if (list[i] == NULL)
4126 continue;
4127 for (j = 0; j < i; j++) {
4128 if (list[j] == NULL)
4129 continue;
4130 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4131 if (ret == 0) {
4132 is_indeterminist = 1;
4133 }
4134 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004135 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004136 for (i = 0; i < nbchild; i++) {
4137 if (list[i] != NULL)
4138 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004139 }
4140
4141 xmlFree(list);
4142 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004143 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004144 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004145 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004146 def->dflags |= IS_TRIABLE;
4147 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004148 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004149 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004150 }
4151 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004152}
4153
4154/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004155 * xmlRelaxNGCheckGroupAttrs:
4156 * @ctxt: a Relax-NG parser context
4157 * @def: the group definition
4158 *
4159 * Detects violations of rule 7.3
4160 */
4161static void
4162xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004163 xmlRelaxNGDefinePtr def)
4164{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004165 xmlRelaxNGDefinePtr **list;
4166 xmlRelaxNGDefinePtr cur;
4167 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004168
4169 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004170 ((def->type != XML_RELAXNG_GROUP) &&
4171 (def->type != XML_RELAXNG_ELEMENT)))
4172 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004173
Daniel Veillarde063f482003-03-21 16:53:17 +00004174 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004175 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004176
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004177 /*
4178 * Don't run that check in case of error. Infinite recursion
4179 * becomes possible.
4180 */
4181 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004182 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004183
Daniel Veillardfd573f12003-03-16 17:52:32 +00004184 cur = def->attrs;
4185 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004186 nbchild++;
4187 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004188 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004189 cur = def->content;
4190 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004191 nbchild++;
4192 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004193 }
4194
4195 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004196 sizeof(xmlRelaxNGDefinePtr
4197 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004198 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004199 xmlRngPErrMemory(ctxt, "building group\n");
4200 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004201 }
4202 i = 0;
4203 cur = def->attrs;
4204 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004205 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4206 i++;
4207 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004208 }
4209 cur = def->content;
4210 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004211 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4212 i++;
4213 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004214 }
4215
Daniel Veillard4c004142003-10-07 11:33:24 +00004216 for (i = 0; i < nbchild; i++) {
4217 if (list[i] == NULL)
4218 continue;
4219 for (j = 0; j < i; j++) {
4220 if (list[j] == NULL)
4221 continue;
4222 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4223 if (ret == 0) {
4224 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4225 "Attributes conflicts in group\n", NULL, NULL);
4226 }
4227 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004228 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004229 for (i = 0; i < nbchild; i++) {
4230 if (list[i] != NULL)
4231 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004232 }
4233
4234 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004235 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004236}
4237
4238/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004239 * xmlRelaxNGComputeInterleaves:
4240 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004241 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004242 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004243 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004244 * A lot of work for preprocessing interleave definitions
4245 * is potentially needed to get a decent execution speed at runtime
4246 * - trying to get a total order on the element nodes generated
4247 * by the interleaves, order the list of interleave definitions
4248 * following that order.
4249 * - if <text/> is used to handle mixed content, it is better to
4250 * flag this in the define and simplify the runtime checking
4251 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004252 */
4253static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004254xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004255 xmlRelaxNGParserCtxtPtr ctxt,
4256 xmlChar * name ATTRIBUTE_UNUSED)
4257{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004258 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004259
Daniel Veillardfd573f12003-03-16 17:52:32 +00004260 xmlRelaxNGPartitionPtr partitions = NULL;
4261 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4262 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004263 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004264 int nbgroups = 0;
4265 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004266 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004267 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004268
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004269 /*
4270 * Don't run that check in case of error. Infinite recursion
4271 * becomes possible.
4272 */
4273 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004274 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004275
Daniel Veillardfd573f12003-03-16 17:52:32 +00004276#ifdef DEBUG_INTERLEAVE
4277 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004278 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004279#endif
4280 cur = def->content;
4281 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004282 nbchild++;
4283 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004284 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004285
Daniel Veillardfd573f12003-03-16 17:52:32 +00004286#ifdef DEBUG_INTERLEAVE
4287 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4288#endif
4289 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004290 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004291 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004292 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004293 cur = def->content;
4294 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004295 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4296 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4297 if (groups[nbgroups] == NULL)
4298 goto error;
4299 if (cur->type == XML_RELAXNG_TEXT)
4300 is_mixed++;
4301 groups[nbgroups]->rule = cur;
4302 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4303 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4304 nbgroups++;
4305 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004306 }
4307#ifdef DEBUG_INTERLEAVE
4308 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4309#endif
4310
4311 /*
4312 * Let's check that all rules makes a partitions according to 7.4
4313 */
4314 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004315 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004316 if (partitions == NULL)
4317 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004318 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004319 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004320 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004321 for (i = 0; i < nbgroups; i++) {
4322 group = groups[i];
4323 for (j = i + 1; j < nbgroups; j++) {
4324 if (groups[j] == NULL)
4325 continue;
4326
4327 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4328 groups[j]->defs);
4329 if (ret == 0) {
4330 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4331 "Element or text conflicts in interleave\n",
4332 NULL, NULL);
4333 }
4334 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4335 groups[j]->attrs);
4336 if (ret == 0) {
4337 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4338 "Attributes conflicts in interleave\n", NULL,
4339 NULL);
4340 }
4341 }
4342 tmp = group->defs;
4343 if ((tmp != NULL) && (*tmp != NULL)) {
4344 while (*tmp != NULL) {
4345 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4346 res = xmlHashAddEntry2(partitions->triage,
4347 BAD_CAST "#text", NULL,
4348 (void *) (long) (i + 1));
4349 if (res != 0)
4350 is_determinist = -1;
4351 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4352 ((*tmp)->name != NULL)) {
4353 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4354 res = xmlHashAddEntry2(partitions->triage,
4355 (*tmp)->name, NULL,
4356 (void *) (long) (i + 1));
4357 else
4358 res = xmlHashAddEntry2(partitions->triage,
4359 (*tmp)->name, (*tmp)->ns,
4360 (void *) (long) (i + 1));
4361 if (res != 0)
4362 is_determinist = -1;
4363 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4364 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4365 res = xmlHashAddEntry2(partitions->triage,
4366 BAD_CAST "#any", NULL,
4367 (void *) (long) (i + 1));
4368 else
4369 res = xmlHashAddEntry2(partitions->triage,
4370 BAD_CAST "#any", (*tmp)->ns,
4371 (void *) (long) (i + 1));
4372 if ((*tmp)->nameClass != NULL)
4373 is_determinist = 2;
4374 if (res != 0)
4375 is_determinist = -1;
4376 } else {
4377 is_determinist = -1;
4378 }
4379 tmp++;
4380 }
4381 } else {
4382 is_determinist = 0;
4383 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004384 }
4385 partitions->groups = groups;
4386
4387 /*
4388 * and save the partition list back in the def
4389 */
4390 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004391 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004392 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004393 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004394 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004395 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004396 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004397 return;
4398
Daniel Veillard4c004142003-10-07 11:33:24 +00004399 error:
4400 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004401 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004402 for (i = 0; i < nbgroups; i++)
4403 if (groups[i] != NULL) {
4404 if (groups[i]->defs != NULL)
4405 xmlFree(groups[i]->defs);
4406 xmlFree(groups[i]);
4407 }
4408 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004409 }
4410 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004411}
4412
4413/**
4414 * xmlRelaxNGParseInterleave:
4415 * @ctxt: a Relax-NG parser context
4416 * @node: the data node.
4417 *
4418 * parse the content of a RelaxNG interleave node.
4419 *
4420 * Returns the definition pointer or NULL in case of error
4421 */
4422static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004423xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4424{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004425 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004426 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004427 xmlNodePtr child;
4428
Daniel Veillardfd573f12003-03-16 17:52:32 +00004429 def = xmlRelaxNGNewDefine(ctxt, node);
4430 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004431 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004432 }
4433 def->type = XML_RELAXNG_INTERLEAVE;
4434
4435 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004436 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004437 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004438 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004439 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004440 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004441
Daniel Veillard4c004142003-10-07 11:33:24 +00004442 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4443 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4444 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4445 "Failed to add %s to hash table\n",
4446 (const xmlChar *) name, NULL);
4447 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004448 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004449 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004450 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004451 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4452 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004453 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004454 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004455 if (IS_RELAXNG(child, "element")) {
4456 cur = xmlRelaxNGParseElement(ctxt, child);
4457 } else {
4458 cur = xmlRelaxNGParsePattern(ctxt, child);
4459 }
4460 if (cur != NULL) {
4461 cur->parent = def;
4462 if (last == NULL) {
4463 def->content = last = cur;
4464 } else {
4465 last->next = cur;
4466 last = cur;
4467 }
4468 }
4469 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004470 }
4471
Daniel Veillard4c004142003-10-07 11:33:24 +00004472 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004473}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004474
4475/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004476 * xmlRelaxNGParseInclude:
4477 * @ctxt: a Relax-NG parser context
4478 * @node: the include node
4479 *
4480 * Integrate the content of an include node in the current grammar
4481 *
4482 * Returns 0 in case of success or -1 in case of error
4483 */
4484static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004485xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4486{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004487 xmlRelaxNGIncludePtr incl;
4488 xmlNodePtr root;
4489 int ret = 0, tmp;
4490
4491 incl = node->_private;
4492 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004493 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4494 "Include node has no data\n", NULL, NULL);
4495 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004496 }
4497 root = xmlDocGetRootElement(incl->doc);
4498 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004499 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4500 NULL, NULL);
4501 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004502 }
4503 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004504 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4505 "Include document root is not a grammar\n", NULL, NULL);
4506 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004507 }
4508
4509 /*
4510 * Merge the definition from both the include and the internal list
4511 */
4512 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004513 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4514 if (tmp != 0)
4515 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004516 }
4517 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004518 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4519 if (tmp != 0)
4520 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004521 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004522 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004523}
4524
4525/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004526 * xmlRelaxNGParseDefine:
4527 * @ctxt: a Relax-NG parser context
4528 * @node: the define node
4529 *
4530 * parse the content of a RelaxNG define element node.
4531 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004532 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004533 */
4534static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004535xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4536{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004537 xmlChar *name;
4538 int ret = 0, tmp;
4539 xmlRelaxNGDefinePtr def;
4540 const xmlChar *olddefine;
4541
4542 name = xmlGetProp(node, BAD_CAST "name");
4543 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004544 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4545 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004546 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004547 xmlRelaxNGNormExtSpace(name);
4548 if (xmlValidateNCName(name, 0)) {
4549 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4550 "define name '%s' is not an NCName\n", name, NULL);
4551 }
4552 def = xmlRelaxNGNewDefine(ctxt, node);
4553 if (def == NULL) {
4554 xmlFree(name);
4555 return (-1);
4556 }
4557 def->type = XML_RELAXNG_DEF;
4558 def->name = name;
4559 if (node->children == NULL) {
4560 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4561 "define has no children\n", NULL, NULL);
4562 } else {
4563 olddefine = ctxt->define;
4564 ctxt->define = name;
4565 def->content =
4566 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4567 ctxt->define = olddefine;
4568 }
4569 if (ctxt->grammar->defs == NULL)
4570 ctxt->grammar->defs = xmlHashCreate(10);
4571 if (ctxt->grammar->defs == NULL) {
4572 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4573 "Could not create definition hash\n", NULL, NULL);
4574 ret = -1;
4575 } else {
4576 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4577 if (tmp < 0) {
4578 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004579
Daniel Veillard4c004142003-10-07 11:33:24 +00004580 prev = xmlHashLookup(ctxt->grammar->defs, name);
4581 if (prev == NULL) {
4582 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4583 "Internal error on define aggregation of %s\n",
4584 name, NULL);
4585 ret = -1;
4586 } else {
4587 while (prev->nextHash != NULL)
4588 prev = prev->nextHash;
4589 prev->nextHash = def;
4590 }
4591 }
4592 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004593 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004594 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004595}
4596
4597/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004598 * xmlRelaxNGProcessExternalRef:
4599 * @ctxt: the parser context
4600 * @node: the externlRef node
4601 *
4602 * Process and compile an externlRef node
4603 *
4604 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4605 */
4606static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004607xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4608{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004609 xmlRelaxNGDocumentPtr docu;
4610 xmlNodePtr root, tmp;
4611 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004612 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004613 xmlRelaxNGDefinePtr def;
4614
4615 docu = node->_private;
4616 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004617 def = xmlRelaxNGNewDefine(ctxt, node);
4618 if (def == NULL)
4619 return (NULL);
4620 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004621
Daniel Veillard4c004142003-10-07 11:33:24 +00004622 if (docu->content == NULL) {
4623 /*
4624 * Then do the parsing for good
4625 */
4626 root = xmlDocGetRootElement(docu->doc);
4627 if (root == NULL) {
4628 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4629 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4630 NULL);
4631 return (NULL);
4632 }
4633 /*
4634 * ns transmission rules
4635 */
4636 ns = xmlGetProp(root, BAD_CAST "ns");
4637 if (ns == NULL) {
4638 tmp = node;
4639 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4640 ns = xmlGetProp(tmp, BAD_CAST "ns");
4641 if (ns != NULL) {
4642 break;
4643 }
4644 tmp = tmp->parent;
4645 }
4646 if (ns != NULL) {
4647 xmlSetProp(root, BAD_CAST "ns", ns);
4648 newNs = 1;
4649 xmlFree(ns);
4650 }
4651 } else {
4652 xmlFree(ns);
4653 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004654
Daniel Veillard4c004142003-10-07 11:33:24 +00004655 /*
4656 * Parsing to get a precompiled schemas.
4657 */
4658 oldflags = ctxt->flags;
4659 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4660 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4661 ctxt->flags = oldflags;
4662 if ((docu->schema != NULL) &&
4663 (docu->schema->topgrammar != NULL)) {
4664 docu->content = docu->schema->topgrammar->start;
4665 }
4666
4667 /*
4668 * the externalRef may be reused in a different ns context
4669 */
4670 if (newNs == 1) {
4671 xmlUnsetProp(root, BAD_CAST "ns");
4672 }
4673 }
4674 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004675 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004676 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004677 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004678 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004679}
4680
4681/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004682 * xmlRelaxNGParsePattern:
4683 * @ctxt: a Relax-NG parser context
4684 * @node: the pattern node.
4685 *
4686 * parse the content of a RelaxNG pattern node.
4687 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004688 * Returns the definition pointer or NULL in case of error or if no
4689 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004690 */
4691static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004692xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4693{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004694 xmlRelaxNGDefinePtr def = NULL;
4695
Daniel Veillardd2298792003-02-14 16:54:11 +00004696 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004697 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004698 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004699 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004700 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004701 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004702 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004703 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004704 def = xmlRelaxNGNewDefine(ctxt, node);
4705 if (def == NULL)
4706 return (NULL);
4707 def->type = XML_RELAXNG_EMPTY;
4708 if (node->children != NULL) {
4709 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4710 "empty: had a child node\n", NULL, NULL);
4711 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004712 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004713 def = xmlRelaxNGNewDefine(ctxt, node);
4714 if (def == NULL)
4715 return (NULL);
4716 def->type = XML_RELAXNG_TEXT;
4717 if (node->children != NULL) {
4718 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4719 "text: had a child node\n", NULL, NULL);
4720 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004721 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004722 def = xmlRelaxNGNewDefine(ctxt, node);
4723 if (def == NULL)
4724 return (NULL);
4725 def->type = XML_RELAXNG_ZEROORMORE;
4726 if (node->children == NULL) {
4727 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4728 "Element %s is empty\n", node->name, NULL);
4729 } else {
4730 def->content =
4731 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4732 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004733 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004734 def = xmlRelaxNGNewDefine(ctxt, node);
4735 if (def == NULL)
4736 return (NULL);
4737 def->type = XML_RELAXNG_ONEORMORE;
4738 if (node->children == NULL) {
4739 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4740 "Element %s is empty\n", node->name, NULL);
4741 } else {
4742 def->content =
4743 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4744 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004745 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004746 def = xmlRelaxNGNewDefine(ctxt, node);
4747 if (def == NULL)
4748 return (NULL);
4749 def->type = XML_RELAXNG_OPTIONAL;
4750 if (node->children == NULL) {
4751 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4752 "Element %s is empty\n", node->name, NULL);
4753 } else {
4754 def->content =
4755 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4756 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004757 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004758 def = xmlRelaxNGNewDefine(ctxt, node);
4759 if (def == NULL)
4760 return (NULL);
4761 def->type = XML_RELAXNG_CHOICE;
4762 if (node->children == NULL) {
4763 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4764 "Element %s is empty\n", node->name, NULL);
4765 } else {
4766 def->content =
4767 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4768 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004769 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004770 def = xmlRelaxNGNewDefine(ctxt, node);
4771 if (def == NULL)
4772 return (NULL);
4773 def->type = XML_RELAXNG_GROUP;
4774 if (node->children == NULL) {
4775 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4776 "Element %s is empty\n", node->name, NULL);
4777 } else {
4778 def->content =
4779 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4780 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004781 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004782 def = xmlRelaxNGNewDefine(ctxt, node);
4783 if (def == NULL)
4784 return (NULL);
4785 def->type = XML_RELAXNG_REF;
4786 def->name = xmlGetProp(node, BAD_CAST "name");
4787 if (def->name == NULL) {
4788 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4789 NULL, NULL);
4790 } else {
4791 xmlRelaxNGNormExtSpace(def->name);
4792 if (xmlValidateNCName(def->name, 0)) {
4793 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4794 "ref name '%s' is not an NCName\n", def->name,
4795 NULL);
4796 }
4797 }
4798 if (node->children != NULL) {
4799 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4800 NULL, NULL);
4801 }
4802 if (ctxt->grammar->refs == NULL)
4803 ctxt->grammar->refs = xmlHashCreate(10);
4804 if (ctxt->grammar->refs == NULL) {
4805 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4806 "Could not create references hash\n", NULL, NULL);
4807 def = NULL;
4808 } else {
4809 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004810
Daniel Veillard4c004142003-10-07 11:33:24 +00004811 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4812 if (tmp < 0) {
4813 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004814
Daniel Veillard4c004142003-10-07 11:33:24 +00004815 prev = (xmlRelaxNGDefinePtr)
4816 xmlHashLookup(ctxt->grammar->refs, def->name);
4817 if (prev == NULL) {
4818 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004819 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4820 "Error refs definitions '%s'\n",
4821 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004822 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004823 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4824 "Error refs definitions\n",
4825 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004826 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004827 def = NULL;
4828 } else {
4829 def->nextHash = prev->nextHash;
4830 prev->nextHash = def;
4831 }
4832 }
4833 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004834 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004835 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004836 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004837 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004838 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004839 def = xmlRelaxNGNewDefine(ctxt, node);
4840 if (def == NULL)
4841 return (NULL);
4842 def->type = XML_RELAXNG_LIST;
4843 if (node->children == NULL) {
4844 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4845 "Element %s is empty\n", node->name, NULL);
4846 } else {
4847 def->content =
4848 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4849 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004850 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004851 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004852 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004853 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004854 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004855 def = xmlRelaxNGNewDefine(ctxt, node);
4856 if (def == NULL)
4857 return (NULL);
4858 def->type = XML_RELAXNG_NOT_ALLOWED;
4859 if (node->children != NULL) {
4860 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4861 "xmlRelaxNGParse: notAllowed element is not empty\n",
4862 NULL, NULL);
4863 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004864 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004865 xmlRelaxNGGrammarPtr grammar, old;
4866 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004867
Daniel Veillardc482e262003-02-26 14:48:48 +00004868#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004869 xmlGenericError(xmlGenericErrorContext,
4870 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004871#endif
4872
Daniel Veillard4c004142003-10-07 11:33:24 +00004873 oldparent = ctxt->parentgrammar;
4874 old = ctxt->grammar;
4875 ctxt->parentgrammar = old;
4876 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4877 if (old != NULL) {
4878 ctxt->grammar = old;
4879 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004880#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004881 if (grammar != NULL) {
4882 grammar->next = old->next;
4883 old->next = grammar;
4884 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004885#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004886 }
4887 if (grammar != NULL)
4888 def = grammar->start;
4889 else
4890 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004891 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004892 if (ctxt->parentgrammar == NULL) {
4893 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4894 "Use of parentRef without a parent grammar\n", NULL,
4895 NULL);
4896 return (NULL);
4897 }
4898 def = xmlRelaxNGNewDefine(ctxt, node);
4899 if (def == NULL)
4900 return (NULL);
4901 def->type = XML_RELAXNG_PARENTREF;
4902 def->name = xmlGetProp(node, BAD_CAST "name");
4903 if (def->name == NULL) {
4904 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4905 "parentRef has no name\n", NULL, NULL);
4906 } else {
4907 xmlRelaxNGNormExtSpace(def->name);
4908 if (xmlValidateNCName(def->name, 0)) {
4909 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4910 "parentRef name '%s' is not an NCName\n",
4911 def->name, NULL);
4912 }
4913 }
4914 if (node->children != NULL) {
4915 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4916 "parentRef is not empty\n", NULL, NULL);
4917 }
4918 if (ctxt->parentgrammar->refs == NULL)
4919 ctxt->parentgrammar->refs = xmlHashCreate(10);
4920 if (ctxt->parentgrammar->refs == NULL) {
4921 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4922 "Could not create references hash\n", NULL, NULL);
4923 def = NULL;
4924 } else if (def->name != NULL) {
4925 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00004926
Daniel Veillard4c004142003-10-07 11:33:24 +00004927 tmp =
4928 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4929 if (tmp < 0) {
4930 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00004931
Daniel Veillard4c004142003-10-07 11:33:24 +00004932 prev = (xmlRelaxNGDefinePtr)
4933 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4934 if (prev == NULL) {
4935 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4936 "Internal error parentRef definitions '%s'\n",
4937 def->name, NULL);
4938 def = NULL;
4939 } else {
4940 def->nextHash = prev->nextHash;
4941 prev->nextHash = def;
4942 }
4943 }
4944 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004945 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004946 if (node->children == NULL) {
4947 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4948 NULL, NULL);
4949 def = NULL;
4950 } else {
4951 def = xmlRelaxNGParseInterleave(ctxt, node);
4952 if (def != NULL) {
4953 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004954
Daniel Veillard4c004142003-10-07 11:33:24 +00004955 if ((def->content != NULL) && (def->content->next != NULL)) {
4956 tmp = xmlRelaxNGNewDefine(ctxt, node);
4957 if (tmp != NULL) {
4958 tmp->type = XML_RELAXNG_GROUP;
4959 tmp->content = def->content;
4960 def->content = tmp;
4961 }
4962 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004963
Daniel Veillard4c004142003-10-07 11:33:24 +00004964 tmp = xmlRelaxNGNewDefine(ctxt, node);
4965 if (tmp == NULL)
4966 return (def);
4967 tmp->type = XML_RELAXNG_TEXT;
4968 tmp->next = def->content;
4969 def->content = tmp;
4970 }
4971 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004972 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004973 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4974 "Unexpected node %s is not a pattern\n", node->name,
4975 NULL);
4976 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004977 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004978 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004979}
4980
4981/**
4982 * xmlRelaxNGParseAttribute:
4983 * @ctxt: a Relax-NG parser context
4984 * @node: the element node
4985 *
4986 * parse the content of a RelaxNG attribute node.
4987 *
4988 * Returns the definition pointer or NULL in case of error.
4989 */
4990static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004991xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4992{
Daniel Veillardd2298792003-02-14 16:54:11 +00004993 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004994 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004995 int old_flags;
4996
Daniel Veillardfd573f12003-03-16 17:52:32 +00004997 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004998 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004999 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005000 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005001 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005002 child = node->children;
5003 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005004 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5005 "xmlRelaxNGParseattribute: attribute has no children\n",
5006 NULL, NULL);
5007 return (ret);
5008 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005009 old_flags = ctxt->flags;
5010 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005011 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5012 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005013 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005014
Daniel Veillardd2298792003-02-14 16:54:11 +00005015 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005016 cur = xmlRelaxNGParsePattern(ctxt, child);
5017 if (cur != NULL) {
5018 switch (cur->type) {
5019 case XML_RELAXNG_EMPTY:
5020 case XML_RELAXNG_NOT_ALLOWED:
5021 case XML_RELAXNG_TEXT:
5022 case XML_RELAXNG_ELEMENT:
5023 case XML_RELAXNG_DATATYPE:
5024 case XML_RELAXNG_VALUE:
5025 case XML_RELAXNG_LIST:
5026 case XML_RELAXNG_REF:
5027 case XML_RELAXNG_PARENTREF:
5028 case XML_RELAXNG_EXTERNALREF:
5029 case XML_RELAXNG_DEF:
5030 case XML_RELAXNG_ONEORMORE:
5031 case XML_RELAXNG_ZEROORMORE:
5032 case XML_RELAXNG_OPTIONAL:
5033 case XML_RELAXNG_CHOICE:
5034 case XML_RELAXNG_GROUP:
5035 case XML_RELAXNG_INTERLEAVE:
5036 case XML_RELAXNG_ATTRIBUTE:
5037 ret->content = cur;
5038 cur->parent = ret;
5039 break;
5040 case XML_RELAXNG_START:
5041 case XML_RELAXNG_PARAM:
5042 case XML_RELAXNG_EXCEPT:
5043 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5044 "attribute has invalid content\n", NULL,
5045 NULL);
5046 break;
5047 case XML_RELAXNG_NOOP:
5048 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5049 "RNG Internal error, noop found in attribute\n",
5050 NULL, NULL);
5051 break;
5052 }
5053 }
5054 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005055 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005056 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005057 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5058 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005059 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005060 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005061 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005062}
5063
5064/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005065 * xmlRelaxNGParseExceptNameClass:
5066 * @ctxt: a Relax-NG parser context
5067 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005068 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005069 *
5070 * parse the content of a RelaxNG nameClass node.
5071 *
5072 * Returns the definition pointer or NULL in case of error.
5073 */
5074static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005075xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005076 xmlNodePtr node, int attr)
5077{
Daniel Veillard144fae12003-02-03 13:17:57 +00005078 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5079 xmlNodePtr child;
5080
Daniel Veillardd2298792003-02-14 16:54:11 +00005081 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005082 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5083 "Expecting an except node\n", NULL, NULL);
5084 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005085 }
5086 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005087 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5088 "exceptNameClass allows only a single except node\n",
5089 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005090 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005091 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005092 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5093 NULL, NULL);
5094 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005095 }
5096
Daniel Veillardfd573f12003-03-16 17:52:32 +00005097 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005098 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005099 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005100 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005101 child = node->children;
5102 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005103 cur = xmlRelaxNGNewDefine(ctxt, child);
5104 if (cur == NULL)
5105 break;
5106 if (attr)
5107 cur->type = XML_RELAXNG_ATTRIBUTE;
5108 else
5109 cur->type = XML_RELAXNG_ELEMENT;
5110
Daniel Veillard419a7682003-02-03 23:22:49 +00005111 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005112 if (last == NULL) {
5113 ret->content = cur;
5114 } else {
5115 last->next = cur;
5116 }
5117 last = cur;
5118 }
5119 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005120 }
5121
Daniel Veillard4c004142003-10-07 11:33:24 +00005122 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005123}
5124
5125/**
5126 * xmlRelaxNGParseNameClass:
5127 * @ctxt: a Relax-NG parser context
5128 * @node: the nameClass node
5129 * @def: the current definition
5130 *
5131 * parse the content of a RelaxNG nameClass node.
5132 *
5133 * Returns the definition pointer or NULL in case of error.
5134 */
5135static xmlRelaxNGDefinePtr
5136xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005137 xmlRelaxNGDefinePtr def)
5138{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005139 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005140 xmlChar *val;
5141
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005142 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005143 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005144 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005145 if ((def->type != XML_RELAXNG_ELEMENT) &&
5146 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5147 ret = xmlRelaxNGNewDefine(ctxt, node);
5148 if (ret == NULL)
5149 return (NULL);
5150 ret->parent = def;
5151 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5152 ret->type = XML_RELAXNG_ATTRIBUTE;
5153 else
5154 ret->type = XML_RELAXNG_ELEMENT;
5155 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005156 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005157 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005158 val = xmlNodeGetContent(node);
5159 xmlRelaxNGNormExtSpace(val);
5160 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005161 if (node->parent != NULL)
5162 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5163 "Element %s name '%s' is not an NCName\n",
5164 node->parent->name, val);
5165 else
5166 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5167 "name '%s' is not an NCName\n",
5168 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005169 }
5170 ret->name = val;
5171 val = xmlGetProp(node, BAD_CAST "ns");
5172 ret->ns = val;
5173 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5174 (val != NULL) &&
5175 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005176 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005177 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005178 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005179 }
5180 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5181 (val != NULL) &&
5182 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005183 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5184 "Attribute with QName 'xmlns' is not allowed\n",
5185 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005186 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005187 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005188 ret->name = NULL;
5189 ret->ns = NULL;
5190 if (node->children != NULL) {
5191 ret->nameClass =
5192 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5193 (def->type ==
5194 XML_RELAXNG_ATTRIBUTE));
5195 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005196 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005197 ret->name = NULL;
5198 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5199 if (ret->ns == NULL) {
5200 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5201 "nsName has no ns attribute\n", NULL, NULL);
5202 }
5203 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5204 (ret->ns != NULL) &&
5205 (xmlStrEqual
5206 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5207 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5208 "Attribute with namespace '%s' is not allowed\n",
5209 ret->ns, NULL);
5210 }
5211 if (node->children != NULL) {
5212 ret->nameClass =
5213 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5214 (def->type ==
5215 XML_RELAXNG_ATTRIBUTE));
5216 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005217 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005218 xmlNodePtr child;
5219 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005220
Daniel Veillard4c004142003-10-07 11:33:24 +00005221 ret = xmlRelaxNGNewDefine(ctxt, node);
5222 if (ret == NULL)
5223 return (NULL);
5224 ret->parent = def;
5225 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005226
Daniel Veillard4c004142003-10-07 11:33:24 +00005227 if (node->children == NULL) {
5228 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5229 "Element choice is empty\n", NULL, NULL);
5230 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005231
Daniel Veillard4c004142003-10-07 11:33:24 +00005232 child = node->children;
5233 while (child != NULL) {
5234 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5235 if (tmp != NULL) {
5236 if (last == NULL) {
5237 last = ret->nameClass = tmp;
5238 } else {
5239 last->next = tmp;
5240 last = tmp;
5241 }
5242 }
5243 child = child->next;
5244 }
5245 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005246 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005247 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5248 "expecting name, anyName, nsName or choice : got %s\n",
5249 node->name, NULL);
5250 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005251 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005252 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005253 if (def->nameClass == NULL) {
5254 def->nameClass = ret;
5255 } else {
5256 tmp = def->nameClass;
5257 while (tmp->next != NULL) {
5258 tmp = tmp->next;
5259 }
5260 tmp->next = ret;
5261 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005262 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005263 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005264}
5265
5266/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005267 * xmlRelaxNGParseElement:
5268 * @ctxt: a Relax-NG parser context
5269 * @node: the element node
5270 *
5271 * parse the content of a RelaxNG element node.
5272 *
5273 * Returns the definition pointer or NULL in case of error.
5274 */
5275static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005276xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5277{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005278 xmlRelaxNGDefinePtr ret, cur, last;
5279 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005280 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005281
Daniel Veillardfd573f12003-03-16 17:52:32 +00005282 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005283 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005284 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005285 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005286 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005287 child = node->children;
5288 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005289 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5290 "xmlRelaxNGParseElement: element has no children\n",
5291 NULL, NULL);
5292 return (ret);
5293 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005294 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5295 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005296 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005297
Daniel Veillard6eadf632003-01-23 18:29:16 +00005298 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005299 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5300 "xmlRelaxNGParseElement: element has no content\n",
5301 NULL, NULL);
5302 return (ret);
5303 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005304 olddefine = ctxt->define;
5305 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005306 last = NULL;
5307 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005308 cur = xmlRelaxNGParsePattern(ctxt, child);
5309 if (cur != NULL) {
5310 cur->parent = ret;
5311 switch (cur->type) {
5312 case XML_RELAXNG_EMPTY:
5313 case XML_RELAXNG_NOT_ALLOWED:
5314 case XML_RELAXNG_TEXT:
5315 case XML_RELAXNG_ELEMENT:
5316 case XML_RELAXNG_DATATYPE:
5317 case XML_RELAXNG_VALUE:
5318 case XML_RELAXNG_LIST:
5319 case XML_RELAXNG_REF:
5320 case XML_RELAXNG_PARENTREF:
5321 case XML_RELAXNG_EXTERNALREF:
5322 case XML_RELAXNG_DEF:
5323 case XML_RELAXNG_ZEROORMORE:
5324 case XML_RELAXNG_ONEORMORE:
5325 case XML_RELAXNG_OPTIONAL:
5326 case XML_RELAXNG_CHOICE:
5327 case XML_RELAXNG_GROUP:
5328 case XML_RELAXNG_INTERLEAVE:
5329 if (last == NULL) {
5330 ret->content = last = cur;
5331 } else {
5332 if ((last->type == XML_RELAXNG_ELEMENT) &&
5333 (ret->content == last)) {
5334 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5335 if (ret->content != NULL) {
5336 ret->content->type = XML_RELAXNG_GROUP;
5337 ret->content->content = last;
5338 } else {
5339 ret->content = last;
5340 }
5341 }
5342 last->next = cur;
5343 last = cur;
5344 }
5345 break;
5346 case XML_RELAXNG_ATTRIBUTE:
5347 cur->next = ret->attrs;
5348 ret->attrs = cur;
5349 break;
5350 case XML_RELAXNG_START:
5351 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5352 "RNG Internal error, start found in element\n",
5353 NULL, NULL);
5354 break;
5355 case XML_RELAXNG_PARAM:
5356 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5357 "RNG Internal error, param found in element\n",
5358 NULL, NULL);
5359 break;
5360 case XML_RELAXNG_EXCEPT:
5361 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5362 "RNG Internal error, except found in element\n",
5363 NULL, NULL);
5364 break;
5365 case XML_RELAXNG_NOOP:
5366 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5367 "RNG Internal error, noop found in element\n",
5368 NULL, NULL);
5369 break;
5370 }
5371 }
5372 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005373 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005374 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005375 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005376}
5377
5378/**
5379 * xmlRelaxNGParsePatterns:
5380 * @ctxt: a Relax-NG parser context
5381 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005382 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005383 *
5384 * parse the content of a RelaxNG start node.
5385 *
5386 * Returns the definition pointer or NULL in case of error.
5387 */
5388static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005389xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005390 int group)
5391{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005392 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005393
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005394 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005395 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005396 if (IS_RELAXNG(nodes, "element")) {
5397 cur = xmlRelaxNGParseElement(ctxt, nodes);
5398 if (def == NULL) {
5399 def = last = cur;
5400 } else {
5401 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5402 (def == last)) {
5403 def = xmlRelaxNGNewDefine(ctxt, nodes);
5404 def->type = XML_RELAXNG_GROUP;
5405 def->content = last;
5406 }
5407 last->next = cur;
5408 last = cur;
5409 }
5410 cur->parent = parent;
5411 } else {
5412 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5413 if (cur != NULL) {
5414 if (def == NULL) {
5415 def = last = cur;
5416 } else {
5417 last->next = cur;
5418 last = cur;
5419 }
5420 }
5421 }
5422 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005423 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005424 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005425}
5426
5427/**
5428 * xmlRelaxNGParseStart:
5429 * @ctxt: a Relax-NG parser context
5430 * @nodes: start children nodes
5431 *
5432 * parse the content of a RelaxNG start node.
5433 *
5434 * Returns 0 in case of success, -1 in case of error
5435 */
5436static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005437xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5438{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005439 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005440 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005441
Daniel Veillardd2298792003-02-14 16:54:11 +00005442 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005443 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5444 NULL, NULL);
5445 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005446 }
5447 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005448 def = xmlRelaxNGNewDefine(ctxt, nodes);
5449 if (def == NULL)
5450 return (-1);
5451 def->type = XML_RELAXNG_EMPTY;
5452 if (nodes->children != NULL) {
5453 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5454 "element empty is not empty\n", NULL, NULL);
5455 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005456 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005457 def = xmlRelaxNGNewDefine(ctxt, nodes);
5458 if (def == NULL)
5459 return (-1);
5460 def->type = XML_RELAXNG_NOT_ALLOWED;
5461 if (nodes->children != NULL) {
5462 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5463 "element notAllowed is not empty\n", NULL, NULL);
5464 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005465 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005466 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005467 }
5468 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005469 last = ctxt->grammar->start;
5470 while (last->next != NULL)
5471 last = last->next;
5472 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005473 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005474 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005475 }
5476 nodes = nodes->next;
5477 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005478 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5479 "start more than one children\n", NULL, NULL);
5480 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005481 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005482 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005483}
5484
5485/**
5486 * xmlRelaxNGParseGrammarContent:
5487 * @ctxt: a Relax-NG parser context
5488 * @nodes: grammar children nodes
5489 *
5490 * parse the content of a RelaxNG grammar node.
5491 *
5492 * Returns 0 in case of success, -1 in case of error
5493 */
5494static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005495xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5496 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005497{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005498 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005499
5500 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005501 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5502 "grammar has no children\n", NULL, NULL);
5503 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005504 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005505 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005506 if (IS_RELAXNG(nodes, "start")) {
5507 if (nodes->children == NULL) {
5508 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5509 "start has no children\n", NULL, NULL);
5510 } else {
5511 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5512 if (tmp != 0)
5513 ret = -1;
5514 }
5515 } else if (IS_RELAXNG(nodes, "define")) {
5516 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5517 if (tmp != 0)
5518 ret = -1;
5519 } else if (IS_RELAXNG(nodes, "include")) {
5520 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5521 if (tmp != 0)
5522 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005523 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005524 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5525 "grammar has unexpected child %s\n", nodes->name,
5526 NULL);
5527 ret = -1;
5528 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005529 nodes = nodes->next;
5530 }
5531 return (ret);
5532}
5533
5534/**
5535 * xmlRelaxNGCheckReference:
5536 * @ref: the ref
5537 * @ctxt: a Relax-NG parser context
5538 * @name: the name associated to the defines
5539 *
5540 * Applies the 4.17. combine attribute rule for all the define
5541 * element of a given grammar using the same name.
5542 */
5543static void
5544xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005545 xmlRelaxNGParserCtxtPtr ctxt,
5546 const xmlChar * name)
5547{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005548 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005549 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005550
5551 grammar = ctxt->grammar;
5552 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005553 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5554 "Internal error: no grammar in CheckReference %s\n",
5555 name, NULL);
5556 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005557 }
5558 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005559 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5560 "Internal error: reference has content in CheckReference %s\n",
5561 name, NULL);
5562 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005563 }
5564 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005565 def = xmlHashLookup(grammar->defs, name);
5566 if (def != NULL) {
5567 cur = ref;
5568 while (cur != NULL) {
5569 cur->content = def;
5570 cur = cur->nextHash;
5571 }
5572 } else {
5573 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5574 "Reference %s has no matching definition\n", name,
5575 NULL);
5576 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005577 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005578 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5579 "Reference %s has no matching definition\n", name,
5580 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005581 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005582}
5583
5584/**
5585 * xmlRelaxNGCheckCombine:
5586 * @define: the define(s) list
5587 * @ctxt: a Relax-NG parser context
5588 * @name: the name associated to the defines
5589 *
5590 * Applies the 4.17. combine attribute rule for all the define
5591 * element of a given grammar using the same name.
5592 */
5593static void
5594xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005595 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5596{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005597 xmlChar *combine;
5598 int choiceOrInterleave = -1;
5599 int missing = 0;
5600 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5601
5602 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005603 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005604 cur = define;
5605 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005606 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5607 if (combine != NULL) {
5608 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5609 if (choiceOrInterleave == -1)
5610 choiceOrInterleave = 1;
5611 else if (choiceOrInterleave == 0) {
5612 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5613 "Defines for %s use both 'choice' and 'interleave'\n",
5614 name, NULL);
5615 }
5616 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5617 if (choiceOrInterleave == -1)
5618 choiceOrInterleave = 0;
5619 else if (choiceOrInterleave == 1) {
5620 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5621 "Defines for %s use both 'choice' and 'interleave'\n",
5622 name, NULL);
5623 }
5624 } else {
5625 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5626 "Defines for %s use unknown combine value '%s''\n",
5627 name, combine);
5628 }
5629 xmlFree(combine);
5630 } else {
5631 if (missing == 0)
5632 missing = 1;
5633 else {
5634 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5635 "Some defines for %s needs the combine attribute\n",
5636 name, NULL);
5637 }
5638 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005639
Daniel Veillard4c004142003-10-07 11:33:24 +00005640 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005641 }
5642#ifdef DEBUG
5643 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005644 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5645 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005646#endif
5647 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005648 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005649 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005650 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005651 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005652 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005653 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005654 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005655 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005656 tmp = define;
5657 last = NULL;
5658 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005659 if (tmp->content != NULL) {
5660 if (tmp->content->next != NULL) {
5661 /*
5662 * we need first to create a wrapper.
5663 */
5664 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5665 if (tmp2 == NULL)
5666 break;
5667 tmp2->type = XML_RELAXNG_GROUP;
5668 tmp2->content = tmp->content;
5669 } else {
5670 tmp2 = tmp->content;
5671 }
5672 if (last == NULL) {
5673 cur->content = tmp2;
5674 } else {
5675 last->next = tmp2;
5676 }
5677 last = tmp2;
5678 }
5679 tmp->content = cur;
5680 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005681 }
5682 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005683 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005684 if (ctxt->interleaves == NULL)
5685 ctxt->interleaves = xmlHashCreate(10);
5686 if (ctxt->interleaves == NULL) {
5687 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5688 "Failed to create interleaves hash table\n", NULL,
5689 NULL);
5690 } else {
5691 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005692
Daniel Veillard4c004142003-10-07 11:33:24 +00005693 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5694 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5695 0) {
5696 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5697 "Failed to add %s to hash table\n",
5698 (const xmlChar *) tmpname, NULL);
5699 }
5700 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005701 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005702}
5703
5704/**
5705 * xmlRelaxNGCombineStart:
5706 * @ctxt: a Relax-NG parser context
5707 * @grammar: the grammar
5708 *
5709 * Applies the 4.17. combine rule for all the start
5710 * element of a given grammar.
5711 */
5712static void
5713xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005714 xmlRelaxNGGrammarPtr grammar)
5715{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005716 xmlRelaxNGDefinePtr starts;
5717 xmlChar *combine;
5718 int choiceOrInterleave = -1;
5719 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005720 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005721
Daniel Veillard2df2de22003-02-17 23:34:33 +00005722 starts = grammar->start;
5723 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005724 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005725 cur = starts;
5726 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005727 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5728 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5729 combine = NULL;
5730 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5731 "Internal error: start element not found\n", NULL,
5732 NULL);
5733 } else {
5734 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5735 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005736
Daniel Veillard4c004142003-10-07 11:33:24 +00005737 if (combine != NULL) {
5738 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5739 if (choiceOrInterleave == -1)
5740 choiceOrInterleave = 1;
5741 else if (choiceOrInterleave == 0) {
5742 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5743 "<start> use both 'choice' and 'interleave'\n",
5744 NULL, NULL);
5745 }
5746 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5747 if (choiceOrInterleave == -1)
5748 choiceOrInterleave = 0;
5749 else if (choiceOrInterleave == 1) {
5750 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5751 "<start> use both 'choice' and 'interleave'\n",
5752 NULL, NULL);
5753 }
5754 } else {
5755 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5756 "<start> uses unknown combine value '%s''\n",
5757 combine, NULL);
5758 }
5759 xmlFree(combine);
5760 } else {
5761 if (missing == 0)
5762 missing = 1;
5763 else {
5764 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5765 "Some <start> element miss the combine attribute\n",
5766 NULL, NULL);
5767 }
5768 }
5769
5770 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005771 }
5772#ifdef DEBUG
5773 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005774 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5775 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005776#endif
5777 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005778 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005779 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005780 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005781 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005782 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005783 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005784 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005785 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005786 cur->content = grammar->start;
5787 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005788 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005789 if (ctxt->interleaves == NULL)
5790 ctxt->interleaves = xmlHashCreate(10);
5791 if (ctxt->interleaves == NULL) {
5792 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5793 "Failed to create interleaves hash table\n", NULL,
5794 NULL);
5795 } else {
5796 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005797
Daniel Veillard4c004142003-10-07 11:33:24 +00005798 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5799 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5800 0) {
5801 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5802 "Failed to add %s to hash table\n",
5803 (const xmlChar *) tmpname, NULL);
5804 }
5805 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005806 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005807}
5808
5809/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005810 * xmlRelaxNGCheckCycles:
5811 * @ctxt: a Relax-NG parser context
5812 * @nodes: grammar children nodes
5813 * @depth: the counter
5814 *
5815 * Check for cycles.
5816 *
5817 * Returns 0 if check passed, and -1 in case of error
5818 */
5819static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005820xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5821 xmlRelaxNGDefinePtr cur, int depth)
5822{
Daniel Veillardd4310742003-02-18 21:12:46 +00005823 int ret = 0;
5824
5825 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005826 if ((cur->type == XML_RELAXNG_REF) ||
5827 (cur->type == XML_RELAXNG_PARENTREF)) {
5828 if (cur->depth == -1) {
5829 cur->depth = depth;
5830 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5831 cur->depth = -2;
5832 } else if (depth == cur->depth) {
5833 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5834 "Detected a cycle in %s references\n",
5835 cur->name, NULL);
5836 return (-1);
5837 }
5838 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5839 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5840 } else {
5841 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5842 }
5843 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005844 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005845 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005846}
5847
5848/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005849 * xmlRelaxNGTryUnlink:
5850 * @ctxt: a Relax-NG parser context
5851 * @cur: the definition to unlink
5852 * @parent: the parent definition
5853 * @prev: the previous sibling definition
5854 *
5855 * Try to unlink a definition. If not possble make it a NOOP
5856 *
5857 * Returns the new prev definition
5858 */
5859static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005860xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5861 xmlRelaxNGDefinePtr cur,
5862 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5863{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005864 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005865 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005866 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005867 if (parent != NULL) {
5868 if (parent->content == cur)
5869 parent->content = cur->next;
5870 else if (parent->attrs == cur)
5871 parent->attrs = cur->next;
5872 else if (parent->nameClass == cur)
5873 parent->nameClass = cur->next;
5874 } else {
5875 cur->type = XML_RELAXNG_NOOP;
5876 prev = cur;
5877 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005878 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005879 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005880}
5881
5882/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005883 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005884 * @ctxt: a Relax-NG parser context
5885 * @nodes: grammar children nodes
5886 *
5887 * Check for simplification of empty and notAllowed
5888 */
5889static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005890xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5891 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5892{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005893 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005894
Daniel Veillardfd573f12003-03-16 17:52:32 +00005895 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005896 if ((cur->type == XML_RELAXNG_REF) ||
5897 (cur->type == XML_RELAXNG_PARENTREF)) {
5898 if (cur->depth != -3) {
5899 cur->depth = -3;
5900 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5901 }
5902 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5903 cur->parent = parent;
5904 if ((parent != NULL) &&
5905 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5906 (parent->type == XML_RELAXNG_LIST) ||
5907 (parent->type == XML_RELAXNG_GROUP) ||
5908 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5909 (parent->type == XML_RELAXNG_ONEORMORE) ||
5910 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5911 parent->type = XML_RELAXNG_NOT_ALLOWED;
5912 break;
5913 }
5914 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5915 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5916 } else
5917 prev = cur;
5918 } else if (cur->type == XML_RELAXNG_EMPTY) {
5919 cur->parent = parent;
5920 if ((parent != NULL) &&
5921 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5922 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5923 parent->type = XML_RELAXNG_EMPTY;
5924 break;
5925 }
5926 if ((parent != NULL) &&
5927 ((parent->type == XML_RELAXNG_GROUP) ||
5928 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5929 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5930 } else
5931 prev = cur;
5932 } else {
5933 cur->parent = parent;
5934 if (cur->content != NULL)
5935 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5936 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5937 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5938 if (cur->nameClass != NULL)
5939 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5940 /*
5941 * On Elements, try to move attribute only generating rules on
5942 * the attrs rules.
5943 */
5944 if (cur->type == XML_RELAXNG_ELEMENT) {
5945 int attronly;
5946 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00005947
Daniel Veillard4c004142003-10-07 11:33:24 +00005948 while (cur->content != NULL) {
5949 attronly =
5950 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5951 if (attronly == 1) {
5952 /*
5953 * migrate cur->content to attrs
5954 */
5955 tmp = cur->content;
5956 cur->content = tmp->next;
5957 tmp->next = cur->attrs;
5958 cur->attrs = tmp;
5959 } else {
5960 /*
5961 * cur->content can generate elements or text
5962 */
5963 break;
5964 }
5965 }
5966 pre = cur->content;
5967 while ((pre != NULL) && (pre->next != NULL)) {
5968 tmp = pre->next;
5969 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5970 if (attronly == 1) {
5971 /*
5972 * migrate tmp to attrs
5973 */
5974 pre->next = tmp->next;
5975 tmp->next = cur->attrs;
5976 cur->attrs = tmp;
5977 } else {
5978 pre = tmp;
5979 }
5980 }
5981 }
5982 /*
5983 * This may result in a simplification
5984 */
5985 if ((cur->type == XML_RELAXNG_GROUP) ||
5986 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5987 if (cur->content == NULL)
5988 cur->type = XML_RELAXNG_EMPTY;
5989 else if (cur->content->next == NULL) {
5990 if ((parent == NULL) && (prev == NULL)) {
5991 cur->type = XML_RELAXNG_NOOP;
5992 } else if (prev == NULL) {
5993 parent->content = cur->content;
5994 cur->content->next = cur->next;
5995 cur = cur->content;
5996 } else {
5997 cur->content->next = cur->next;
5998 prev->next = cur->content;
5999 cur = cur->content;
6000 }
6001 }
6002 }
6003 /*
6004 * the current node may have been transformed back
6005 */
6006 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6007 (cur->content != NULL) &&
6008 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6009 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6010 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6011 if ((parent != NULL) &&
6012 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6013 (parent->type == XML_RELAXNG_LIST) ||
6014 (parent->type == XML_RELAXNG_GROUP) ||
6015 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6016 (parent->type == XML_RELAXNG_ONEORMORE) ||
6017 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6018 parent->type = XML_RELAXNG_NOT_ALLOWED;
6019 break;
6020 }
6021 if ((parent != NULL) &&
6022 (parent->type == XML_RELAXNG_CHOICE)) {
6023 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6024 } else
6025 prev = cur;
6026 } else if (cur->type == XML_RELAXNG_EMPTY) {
6027 if ((parent != NULL) &&
6028 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6029 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6030 parent->type = XML_RELAXNG_EMPTY;
6031 break;
6032 }
6033 if ((parent != NULL) &&
6034 ((parent->type == XML_RELAXNG_GROUP) ||
6035 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6036 (parent->type == XML_RELAXNG_CHOICE))) {
6037 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6038 } else
6039 prev = cur;
6040 } else {
6041 prev = cur;
6042 }
6043 }
6044 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006045 }
6046}
6047
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006048/**
6049 * xmlRelaxNGGroupContentType:
6050 * @ct1: the first content type
6051 * @ct2: the second content type
6052 *
6053 * Try to group 2 content types
6054 *
6055 * Returns the content type
6056 */
6057static xmlRelaxNGContentType
6058xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006059 xmlRelaxNGContentType ct2)
6060{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006061 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006062 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6063 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006064 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006065 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006066 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006067 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006068 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006069 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6070 return (XML_RELAXNG_CONTENT_COMPLEX);
6071 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006072}
6073
6074/**
6075 * xmlRelaxNGMaxContentType:
6076 * @ct1: the first content type
6077 * @ct2: the second content type
6078 *
6079 * Compute the max content-type
6080 *
6081 * Returns the content type
6082 */
6083static xmlRelaxNGContentType
6084xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006085 xmlRelaxNGContentType ct2)
6086{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006087 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006088 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6089 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006090 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006091 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6092 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006093 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006094 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6095 return (XML_RELAXNG_CONTENT_COMPLEX);
6096 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006097}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006098
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006099/**
6100 * xmlRelaxNGCheckRules:
6101 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006102 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006103 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006104 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006105 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006106 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006107 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006108 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006109 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006110static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006111xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6112 xmlRelaxNGDefinePtr cur, int flags,
6113 xmlRelaxNGType ptype)
6114{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006115 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006116 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006117
Daniel Veillardfd573f12003-03-16 17:52:32 +00006118 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006119 ret = XML_RELAXNG_CONTENT_EMPTY;
6120 if ((cur->type == XML_RELAXNG_REF) ||
6121 (cur->type == XML_RELAXNG_PARENTREF)) {
6122 if (flags & XML_RELAXNG_IN_LIST) {
6123 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6124 "Found forbidden pattern list//ref\n", NULL,
6125 NULL);
6126 }
6127 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6128 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6129 "Found forbidden pattern data/except//ref\n",
6130 NULL, NULL);
6131 }
6132 if (cur->depth > -4) {
6133 cur->depth = -4;
6134 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6135 flags, cur->type);
6136 cur->depth = ret - 15;
6137 } else if (cur->depth == -4) {
6138 ret = XML_RELAXNG_CONTENT_COMPLEX;
6139 } else {
6140 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6141 }
6142 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6143 /*
6144 * The 7.3 Attribute derivation rule for groups is plugged there
6145 */
6146 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6147 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6148 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6149 "Found forbidden pattern data/except//element(ref)\n",
6150 NULL, NULL);
6151 }
6152 if (flags & XML_RELAXNG_IN_LIST) {
6153 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6154 "Found forbidden pattern list//element(ref)\n",
6155 NULL, NULL);
6156 }
6157 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6158 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6159 "Found forbidden pattern attribute//element(ref)\n",
6160 NULL, NULL);
6161 }
6162 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6163 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6164 "Found forbidden pattern attribute//element(ref)\n",
6165 NULL, NULL);
6166 }
6167 /*
6168 * reset since in the simple form elements are only child
6169 * of grammar/define
6170 */
6171 nflags = 0;
6172 ret =
6173 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6174 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6175 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6176 "Element %s attributes have a content type error\n",
6177 cur->name, NULL);
6178 }
6179 ret =
6180 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6181 cur->type);
6182 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6183 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6184 "Element %s has a content type error\n",
6185 cur->name, NULL);
6186 } else {
6187 ret = XML_RELAXNG_CONTENT_COMPLEX;
6188 }
6189 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6190 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6191 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6192 "Found forbidden pattern attribute//attribute\n",
6193 NULL, NULL);
6194 }
6195 if (flags & XML_RELAXNG_IN_LIST) {
6196 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6197 "Found forbidden pattern list//attribute\n",
6198 NULL, NULL);
6199 }
6200 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6201 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6202 "Found forbidden pattern oneOrMore//group//attribute\n",
6203 NULL, NULL);
6204 }
6205 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6206 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6207 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6208 NULL, NULL);
6209 }
6210 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6211 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6212 "Found forbidden pattern data/except//attribute\n",
6213 NULL, NULL);
6214 }
6215 if (flags & XML_RELAXNG_IN_START) {
6216 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6217 "Found forbidden pattern start//attribute\n",
6218 NULL, NULL);
6219 }
6220 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6221 && (cur->name == NULL)) {
6222 if (cur->ns == NULL) {
6223 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6224 "Found anyName attribute without oneOrMore ancestor\n",
6225 NULL, NULL);
6226 } else {
6227 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6228 "Found nsName attribute without oneOrMore ancestor\n",
6229 NULL, NULL);
6230 }
6231 }
6232 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6233 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6234 ret = XML_RELAXNG_CONTENT_EMPTY;
6235 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6236 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6237 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6238 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6239 "Found forbidden pattern data/except//oneOrMore\n",
6240 NULL, NULL);
6241 }
6242 if (flags & XML_RELAXNG_IN_START) {
6243 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6244 "Found forbidden pattern start//oneOrMore\n",
6245 NULL, NULL);
6246 }
6247 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6248 ret =
6249 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6250 cur->type);
6251 ret = xmlRelaxNGGroupContentType(ret, ret);
6252 } else if (cur->type == XML_RELAXNG_LIST) {
6253 if (flags & XML_RELAXNG_IN_LIST) {
6254 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6255 "Found forbidden pattern list//list\n", NULL,
6256 NULL);
6257 }
6258 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6259 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6260 "Found forbidden pattern data/except//list\n",
6261 NULL, NULL);
6262 }
6263 if (flags & XML_RELAXNG_IN_START) {
6264 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6265 "Found forbidden pattern start//list\n", NULL,
6266 NULL);
6267 }
6268 nflags = flags | XML_RELAXNG_IN_LIST;
6269 ret =
6270 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6271 cur->type);
6272 } else if (cur->type == XML_RELAXNG_GROUP) {
6273 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6274 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6275 "Found forbidden pattern data/except//group\n",
6276 NULL, NULL);
6277 }
6278 if (flags & XML_RELAXNG_IN_START) {
6279 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6280 "Found forbidden pattern start//group\n", NULL,
6281 NULL);
6282 }
6283 if (flags & XML_RELAXNG_IN_ONEORMORE)
6284 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6285 else
6286 nflags = flags;
6287 ret =
6288 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6289 cur->type);
6290 /*
6291 * The 7.3 Attribute derivation rule for groups is plugged there
6292 */
6293 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6294 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6295 if (flags & XML_RELAXNG_IN_LIST) {
6296 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6297 "Found forbidden pattern list//interleave\n",
6298 NULL, NULL);
6299 }
6300 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6301 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6302 "Found forbidden pattern data/except//interleave\n",
6303 NULL, NULL);
6304 }
6305 if (flags & XML_RELAXNG_IN_START) {
6306 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6307 "Found forbidden pattern start//interleave\n",
6308 NULL, NULL);
6309 }
6310 if (flags & XML_RELAXNG_IN_ONEORMORE)
6311 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6312 else
6313 nflags = flags;
6314 ret =
6315 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6316 cur->type);
6317 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6318 if ((cur->parent != NULL) &&
6319 (cur->parent->type == XML_RELAXNG_DATATYPE))
6320 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6321 else
6322 nflags = flags;
6323 ret =
6324 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6325 cur->type);
6326 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6327 if (flags & XML_RELAXNG_IN_START) {
6328 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6329 "Found forbidden pattern start//data\n", NULL,
6330 NULL);
6331 }
6332 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6333 ret = XML_RELAXNG_CONTENT_SIMPLE;
6334 } else if (cur->type == XML_RELAXNG_VALUE) {
6335 if (flags & XML_RELAXNG_IN_START) {
6336 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6337 "Found forbidden pattern start//value\n", NULL,
6338 NULL);
6339 }
6340 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6341 ret = XML_RELAXNG_CONTENT_SIMPLE;
6342 } else if (cur->type == XML_RELAXNG_TEXT) {
6343 if (flags & XML_RELAXNG_IN_LIST) {
6344 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6345 "Found forbidden pattern list//text\n", NULL,
6346 NULL);
6347 }
6348 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6349 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6350 "Found forbidden pattern data/except//text\n",
6351 NULL, NULL);
6352 }
6353 if (flags & XML_RELAXNG_IN_START) {
6354 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6355 "Found forbidden pattern start//text\n", NULL,
6356 NULL);
6357 }
6358 ret = XML_RELAXNG_CONTENT_COMPLEX;
6359 } else if (cur->type == XML_RELAXNG_EMPTY) {
6360 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6361 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6362 "Found forbidden pattern data/except//empty\n",
6363 NULL, NULL);
6364 }
6365 if (flags & XML_RELAXNG_IN_START) {
6366 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6367 "Found forbidden pattern start//empty\n", NULL,
6368 NULL);
6369 }
6370 ret = XML_RELAXNG_CONTENT_EMPTY;
6371 } else if (cur->type == XML_RELAXNG_CHOICE) {
6372 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6373 ret =
6374 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6375 } else {
6376 ret =
6377 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6378 }
6379 cur = cur->next;
6380 if (ptype == XML_RELAXNG_GROUP) {
6381 val = xmlRelaxNGGroupContentType(val, ret);
6382 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6383 tmp = xmlRelaxNGGroupContentType(val, ret);
6384 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6385 tmp = xmlRelaxNGMaxContentType(val, ret);
6386 } else if (ptype == XML_RELAXNG_CHOICE) {
6387 val = xmlRelaxNGMaxContentType(val, ret);
6388 } else if (ptype == XML_RELAXNG_LIST) {
6389 val = XML_RELAXNG_CONTENT_SIMPLE;
6390 } else if (ptype == XML_RELAXNG_EXCEPT) {
6391 if (ret == XML_RELAXNG_CONTENT_ERROR)
6392 val = XML_RELAXNG_CONTENT_ERROR;
6393 else
6394 val = XML_RELAXNG_CONTENT_SIMPLE;
6395 } else {
6396 val = xmlRelaxNGGroupContentType(val, ret);
6397 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006398
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006399 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006400 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006401}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006402
6403/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006404 * xmlRelaxNGParseGrammar:
6405 * @ctxt: a Relax-NG parser context
6406 * @nodes: grammar children nodes
6407 *
6408 * parse a Relax-NG <grammar> node
6409 *
6410 * Returns the internal xmlRelaxNGGrammarPtr built or
6411 * NULL in case of error
6412 */
6413static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006414xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6415{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006416 xmlRelaxNGGrammarPtr ret, tmp, old;
6417
Daniel Veillardc482e262003-02-26 14:48:48 +00006418#ifdef DEBUG_GRAMMAR
6419 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6420#endif
6421
Daniel Veillard6eadf632003-01-23 18:29:16 +00006422 ret = xmlRelaxNGNewGrammar(ctxt);
6423 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006424 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006425
6426 /*
6427 * Link the new grammar in the tree
6428 */
6429 ret->parent = ctxt->grammar;
6430 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006431 tmp = ctxt->grammar->children;
6432 if (tmp == NULL) {
6433 ctxt->grammar->children = ret;
6434 } else {
6435 while (tmp->next != NULL)
6436 tmp = tmp->next;
6437 tmp->next = ret;
6438 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006439 }
6440
6441 old = ctxt->grammar;
6442 ctxt->grammar = ret;
6443 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6444 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006445 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006446 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6447 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006448 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006449 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6450 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006451 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006452
6453 /*
6454 * Apply 4.17 mergingd rules to defines and starts
6455 */
6456 xmlRelaxNGCombineStart(ctxt, ret);
6457 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006458 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6459 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006460 }
6461
6462 /*
6463 * link together defines and refs in this grammar
6464 */
6465 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006466 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6467 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006468 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006469
Daniel Veillard6eadf632003-01-23 18:29:16 +00006470 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006471 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006472}
6473
6474/**
6475 * xmlRelaxNGParseDocument:
6476 * @ctxt: a Relax-NG parser context
6477 * @node: the root node of the RelaxNG schema
6478 *
6479 * parse a Relax-NG definition resource and build an internal
6480 * xmlRelaxNG struture which can be used to validate instances.
6481 *
6482 * Returns the internal XML RelaxNG structure built or
6483 * NULL in case of error
6484 */
6485static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006486xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6487{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006488 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006489 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006490 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006491
6492 if ((ctxt == NULL) || (node == NULL))
6493 return (NULL);
6494
6495 schema = xmlRelaxNGNewRelaxNG(ctxt);
6496 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006497 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006498
Daniel Veillard276be4a2003-01-24 01:03:34 +00006499 olddefine = ctxt->define;
6500 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006501 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006502 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006503 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006504 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006505
Daniel Veillard4c004142003-10-07 11:33:24 +00006506 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6507 if (schema->topgrammar == NULL) {
6508 return (schema);
6509 }
6510 /*
6511 * Link the new grammar in the tree
6512 */
6513 ret->parent = ctxt->grammar;
6514 if (ctxt->grammar != NULL) {
6515 tmp = ctxt->grammar->children;
6516 if (tmp == NULL) {
6517 ctxt->grammar->children = ret;
6518 } else {
6519 while (tmp->next != NULL)
6520 tmp = tmp->next;
6521 tmp->next = ret;
6522 }
6523 }
6524 old = ctxt->grammar;
6525 ctxt->grammar = ret;
6526 xmlRelaxNGParseStart(ctxt, node);
6527 if (old != NULL)
6528 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006529 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006530 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006531 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006532 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6533 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6534 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6535 while ((schema->topgrammar->start != NULL) &&
6536 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6537 (schema->topgrammar->start->next != NULL))
6538 schema->topgrammar->start =
6539 schema->topgrammar->start->content;
6540 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6541 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6542 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006543 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006544#ifdef DEBUG
6545 if (schema == NULL)
6546 xmlGenericError(xmlGenericErrorContext,
6547 "xmlRelaxNGParseDocument() failed\n");
6548#endif
6549
6550 return (schema);
6551}
6552
6553/************************************************************************
6554 * *
6555 * Reading RelaxNGs *
6556 * *
6557 ************************************************************************/
6558
6559/**
6560 * xmlRelaxNGNewParserCtxt:
6561 * @URL: the location of the schema
6562 *
6563 * Create an XML RelaxNGs parse context for that file/resource expected
6564 * to contain an XML RelaxNGs file.
6565 *
6566 * Returns the parser context or NULL in case of error
6567 */
6568xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006569xmlRelaxNGNewParserCtxt(const char *URL)
6570{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006571 xmlRelaxNGParserCtxtPtr ret;
6572
6573 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006574 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006575
Daniel Veillard4c004142003-10-07 11:33:24 +00006576 ret =
6577 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006578 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006579 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006580 return (NULL);
6581 }
6582 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006583 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006584 ret->error = xmlGenericError;
6585 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006586 return (ret);
6587}
6588
6589/**
6590 * xmlRelaxNGNewMemParserCtxt:
6591 * @buffer: a pointer to a char array containing the schemas
6592 * @size: the size of the array
6593 *
6594 * Create an XML RelaxNGs parse context for that memory buffer expected
6595 * to contain an XML RelaxNGs file.
6596 *
6597 * Returns the parser context or NULL in case of error
6598 */
6599xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006600xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6601{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006602 xmlRelaxNGParserCtxtPtr ret;
6603
6604 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006605 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006606
Daniel Veillard4c004142003-10-07 11:33:24 +00006607 ret =
6608 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006609 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006610 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006611 return (NULL);
6612 }
6613 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6614 ret->buffer = buffer;
6615 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006616 ret->error = xmlGenericError;
6617 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006618 return (ret);
6619}
6620
6621/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006622 * xmlRelaxNGNewDocParserCtxt:
6623 * @doc: a preparsed document tree
6624 *
6625 * Create an XML RelaxNGs parser context for that document.
6626 * Note: since the process of compiling a RelaxNG schemas modifies the
6627 * document, the @doc parameter is duplicated internally.
6628 *
6629 * Returns the parser context or NULL in case of error
6630 */
6631xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006632xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6633{
Daniel Veillard33300b42003-04-17 09:09:19 +00006634 xmlRelaxNGParserCtxtPtr ret;
6635 xmlDocPtr copy;
6636
6637 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006638 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006639 copy = xmlCopyDoc(doc, 1);
6640 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006641 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006642
Daniel Veillard4c004142003-10-07 11:33:24 +00006643 ret =
6644 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006645 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006646 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006647 return (NULL);
6648 }
6649 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6650 ret->document = copy;
6651 ret->userData = xmlGenericErrorContext;
6652 return (ret);
6653}
6654
6655/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006656 * xmlRelaxNGFreeParserCtxt:
6657 * @ctxt: the schema parser context
6658 *
6659 * Free the resources associated to the schema parser context
6660 */
6661void
Daniel Veillard4c004142003-10-07 11:33:24 +00006662xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6663{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006664 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006665 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006666 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006667 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006668 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006669 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006670 if (ctxt->interleaves != NULL)
6671 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006672 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006673 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006674 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006675 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006676 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006677 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006678 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006679 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006680 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006681 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006682
Daniel Veillard4c004142003-10-07 11:33:24 +00006683 for (i = 0; i < ctxt->defNr; i++)
6684 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6685 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006686 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006687 xmlFree(ctxt);
6688}
6689
Daniel Veillard6eadf632003-01-23 18:29:16 +00006690/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006691 * xmlRelaxNGNormExtSpace:
6692 * @value: a value
6693 *
6694 * Removes the leading and ending spaces of the value
6695 * The string is modified "in situ"
6696 */
6697static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006698xmlRelaxNGNormExtSpace(xmlChar * value)
6699{
Daniel Veillardd2298792003-02-14 16:54:11 +00006700 xmlChar *start = value;
6701 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006702
Daniel Veillard4c004142003-10-07 11:33:24 +00006703 if (value == NULL)
6704 return;
6705
William M. Brack76e95df2003-10-18 16:20:14 +00006706 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006707 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006708 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006709 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006710 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006711 cur++;
6712 if (*cur == 0)
6713 return;
6714 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006715 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006716 cur++;
6717 if (*cur == 0) {
6718 *start = 0;
6719 return;
6720 }
6721 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006722 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006723 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006724 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006725 *start++ = *cur++;
6726 if (*cur == 0) {
6727 *start = 0;
6728 return;
6729 }
6730 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006731 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006732 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006733 if (*cur == 0) {
6734 *start = 0;
6735 return;
6736 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006737 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006738 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006739 }
6740}
6741
6742/**
6743 * xmlRelaxNGCheckAttributes:
6744 * @ctxt: a Relax-NG parser context
6745 * @node: a Relax-NG node
6746 *
6747 * Check all the attributes on the given node
6748 */
6749static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006750xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6751{
Daniel Veillardd2298792003-02-14 16:54:11 +00006752 xmlAttrPtr cur, next;
6753
6754 cur = node->properties;
6755 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006756 next = cur->next;
6757 if ((cur->ns == NULL) ||
6758 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6759 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6760 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6761 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6762 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6763 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6764 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6765 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6766 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6767 "Attribute %s is not allowed on %s\n",
6768 cur->name, node->name);
6769 }
6770 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6771 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6772 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6773 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6774 "Attribute %s is not allowed on %s\n",
6775 cur->name, node->name);
6776 }
6777 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6778 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6779 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6780 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6781 "Attribute %s is not allowed on %s\n",
6782 cur->name, node->name);
6783 }
6784 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6785 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6786 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6787 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6788 "Attribute %s is not allowed on %s\n",
6789 cur->name, node->name);
6790 }
6791 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6792 xmlChar *val;
6793 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006794
Daniel Veillard4c004142003-10-07 11:33:24 +00006795 val = xmlNodeListGetString(node->doc, cur->children, 1);
6796 if (val != NULL) {
6797 if (val[0] != 0) {
6798 uri = xmlParseURI((const char *) val);
6799 if (uri == NULL) {
6800 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6801 "Attribute %s contains invalid URI %s\n",
6802 cur->name, val);
6803 } else {
6804 if (uri->scheme == NULL) {
6805 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6806 "Attribute %s URI %s is not absolute\n",
6807 cur->name, val);
6808 }
6809 if (uri->fragment != NULL) {
6810 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6811 "Attribute %s URI %s has a fragment ID\n",
6812 cur->name, val);
6813 }
6814 xmlFreeURI(uri);
6815 }
6816 }
6817 xmlFree(val);
6818 }
6819 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6820 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6821 "Unknown attribute %s on %s\n", cur->name,
6822 node->name);
6823 }
6824 }
6825 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006826 }
6827}
6828
6829/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006830 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006831 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006832 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006833 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006834 * Cleanup the subtree from unwanted nodes for parsing, resolve
6835 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006836 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006837static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006838xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6839{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006840 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006841
Daniel Veillard6eadf632003-01-23 18:29:16 +00006842 delete = NULL;
6843 cur = root;
6844 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006845 if (delete != NULL) {
6846 xmlUnlinkNode(delete);
6847 xmlFreeNode(delete);
6848 delete = NULL;
6849 }
6850 if (cur->type == XML_ELEMENT_NODE) {
6851 /*
6852 * Simplification 4.1. Annotations
6853 */
6854 if ((cur->ns == NULL) ||
6855 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6856 if ((cur->parent != NULL) &&
6857 (cur->parent->type == XML_ELEMENT_NODE) &&
6858 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6859 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6860 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6861 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6862 "element %s doesn't allow foreign elements\n",
6863 cur->parent->name, NULL);
6864 }
6865 delete = cur;
6866 goto skip_children;
6867 } else {
6868 xmlRelaxNGCleanupAttributes(ctxt, cur);
6869 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6870 xmlChar *href, *ns, *base, *URL;
6871 xmlRelaxNGDocumentPtr docu;
6872 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006873
Daniel Veillard4c004142003-10-07 11:33:24 +00006874 ns = xmlGetProp(cur, BAD_CAST "ns");
6875 if (ns == NULL) {
6876 tmp = cur->parent;
6877 while ((tmp != NULL) &&
6878 (tmp->type == XML_ELEMENT_NODE)) {
6879 ns = xmlGetProp(tmp, BAD_CAST "ns");
6880 if (ns != NULL)
6881 break;
6882 tmp = tmp->parent;
6883 }
6884 }
6885 href = xmlGetProp(cur, BAD_CAST "href");
6886 if (href == NULL) {
6887 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6888 "xmlRelaxNGParse: externalRef has no href attribute\n",
6889 NULL, NULL);
6890 delete = cur;
6891 goto skip_children;
6892 }
6893 base = xmlNodeGetBase(cur->doc, cur);
6894 URL = xmlBuildURI(href, base);
6895 if (URL == NULL) {
6896 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6897 "Failed to compute URL for externalRef %s\n",
6898 href, NULL);
6899 if (href != NULL)
6900 xmlFree(href);
6901 if (base != NULL)
6902 xmlFree(base);
6903 delete = cur;
6904 goto skip_children;
6905 }
6906 if (href != NULL)
6907 xmlFree(href);
6908 if (base != NULL)
6909 xmlFree(base);
6910 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6911 if (docu == NULL) {
6912 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6913 "Failed to load externalRef %s\n", URL,
6914 NULL);
6915 xmlFree(URL);
6916 delete = cur;
6917 goto skip_children;
6918 }
6919 if (ns != NULL)
6920 xmlFree(ns);
6921 xmlFree(URL);
6922 cur->_private = docu;
6923 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6924 xmlChar *href, *ns, *base, *URL;
6925 xmlRelaxNGIncludePtr incl;
6926 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006927
Daniel Veillard4c004142003-10-07 11:33:24 +00006928 href = xmlGetProp(cur, BAD_CAST "href");
6929 if (href == NULL) {
6930 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6931 "xmlRelaxNGParse: include has no href attribute\n",
6932 NULL, NULL);
6933 delete = cur;
6934 goto skip_children;
6935 }
6936 base = xmlNodeGetBase(cur->doc, cur);
6937 URL = xmlBuildURI(href, base);
6938 if (URL == NULL) {
6939 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6940 "Failed to compute URL for include %s\n",
6941 href, NULL);
6942 if (href != NULL)
6943 xmlFree(href);
6944 if (base != NULL)
6945 xmlFree(base);
6946 delete = cur;
6947 goto skip_children;
6948 }
6949 if (href != NULL)
6950 xmlFree(href);
6951 if (base != NULL)
6952 xmlFree(base);
6953 ns = xmlGetProp(cur, BAD_CAST "ns");
6954 if (ns == NULL) {
6955 tmp = cur->parent;
6956 while ((tmp != NULL) &&
6957 (tmp->type == XML_ELEMENT_NODE)) {
6958 ns = xmlGetProp(tmp, BAD_CAST "ns");
6959 if (ns != NULL)
6960 break;
6961 tmp = tmp->parent;
6962 }
6963 }
6964 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6965 if (ns != NULL)
6966 xmlFree(ns);
6967 if (incl == NULL) {
6968 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
6969 "Failed to load include %s\n", URL,
6970 NULL);
6971 xmlFree(URL);
6972 delete = cur;
6973 goto skip_children;
6974 }
6975 xmlFree(URL);
6976 cur->_private = incl;
6977 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6978 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
6979 {
6980 xmlChar *name, *ns;
6981 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006982
Daniel Veillard4c004142003-10-07 11:33:24 +00006983 /*
6984 * Simplification 4.8. name attribute of element
6985 * and attribute elements
6986 */
6987 name = xmlGetProp(cur, BAD_CAST "name");
6988 if (name != NULL) {
6989 if (cur->children == NULL) {
6990 text =
6991 xmlNewChild(cur, cur->ns, BAD_CAST "name",
6992 name);
6993 } else {
6994 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006995
Daniel Veillard4c004142003-10-07 11:33:24 +00006996 node = xmlNewNode(cur->ns, BAD_CAST "name");
6997 if (node != NULL) {
6998 xmlAddPrevSibling(cur->children, node);
6999 text = xmlNewText(name);
7000 xmlAddChild(node, text);
7001 text = node;
7002 }
7003 }
7004 if (text == NULL) {
7005 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7006 "Failed to create a name %s element\n",
7007 name, NULL);
7008 }
7009 xmlUnsetProp(cur, BAD_CAST "name");
7010 xmlFree(name);
7011 ns = xmlGetProp(cur, BAD_CAST "ns");
7012 if (ns != NULL) {
7013 if (text != NULL) {
7014 xmlSetProp(text, BAD_CAST "ns", ns);
7015 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7016 }
7017 xmlFree(ns);
7018 } else if (xmlStrEqual(cur->name,
7019 BAD_CAST "attribute")) {
7020 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7021 }
7022 }
7023 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7024 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7025 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7026 /*
7027 * Simplification 4.8. name attribute of element
7028 * and attribute elements
7029 */
7030 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7031 xmlNodePtr node;
7032 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007033
Daniel Veillard4c004142003-10-07 11:33:24 +00007034 node = cur->parent;
7035 while ((node != NULL) &&
7036 (node->type == XML_ELEMENT_NODE)) {
7037 ns = xmlGetProp(node, BAD_CAST "ns");
7038 if (ns != NULL) {
7039 break;
7040 }
7041 node = node->parent;
7042 }
7043 if (ns == NULL) {
7044 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7045 } else {
7046 xmlSetProp(cur, BAD_CAST "ns", ns);
7047 xmlFree(ns);
7048 }
7049 }
7050 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7051 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007052
Daniel Veillard4c004142003-10-07 11:33:24 +00007053 /*
7054 * Simplification: 4.10. QNames
7055 */
7056 name = xmlNodeGetContent(cur);
7057 if (name != NULL) {
7058 local = xmlSplitQName2(name, &prefix);
7059 if (local != NULL) {
7060 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007061
Daniel Veillard4c004142003-10-07 11:33:24 +00007062 ns = xmlSearchNs(cur->doc, cur, prefix);
7063 if (ns == NULL) {
7064 xmlRngPErr(ctxt, cur,
7065 XML_RNGP_PREFIX_UNDEFINED,
7066 "xmlRelaxNGParse: no namespace for prefix %s\n",
7067 prefix, NULL);
7068 } else {
7069 xmlSetProp(cur, BAD_CAST "ns",
7070 ns->href);
7071 xmlNodeSetContent(cur, local);
7072 }
7073 xmlFree(local);
7074 xmlFree(prefix);
7075 }
7076 xmlFree(name);
7077 }
7078 }
7079 /*
7080 * 4.16
7081 */
7082 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7083 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7084 xmlRngPErr(ctxt, cur,
7085 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7086 "Found nsName/except//nsName forbidden construct\n",
7087 NULL, NULL);
7088 }
7089 }
7090 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7091 (cur != root)) {
7092 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007093
Daniel Veillard4c004142003-10-07 11:33:24 +00007094 /*
7095 * 4.16
7096 */
7097 if ((cur->parent != NULL) &&
7098 (xmlStrEqual
7099 (cur->parent->name, BAD_CAST "anyName"))) {
7100 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7101 xmlRelaxNGCleanupTree(ctxt, cur);
7102 ctxt->flags = oldflags;
7103 goto skip_children;
7104 } else if ((cur->parent != NULL) &&
7105 (xmlStrEqual
7106 (cur->parent->name, BAD_CAST "nsName"))) {
7107 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7108 xmlRelaxNGCleanupTree(ctxt, cur);
7109 ctxt->flags = oldflags;
7110 goto skip_children;
7111 }
7112 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7113 /*
7114 * 4.16
7115 */
7116 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7117 xmlRngPErr(ctxt, cur,
7118 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7119 "Found anyName/except//anyName forbidden construct\n",
7120 NULL, NULL);
7121 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7122 xmlRngPErr(ctxt, cur,
7123 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7124 "Found nsName/except//anyName forbidden construct\n",
7125 NULL, NULL);
7126 }
7127 }
7128 /*
7129 * Thisd is not an else since "include" is transformed
7130 * into a div
7131 */
7132 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7133 xmlChar *ns;
7134 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007135
Daniel Veillard4c004142003-10-07 11:33:24 +00007136 /*
7137 * implements rule 4.11
7138 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007139
Daniel Veillard4c004142003-10-07 11:33:24 +00007140 ns = xmlGetProp(cur, BAD_CAST "ns");
7141
7142 child = cur->children;
7143 ins = cur;
7144 while (child != NULL) {
7145 if (ns != NULL) {
7146 if (!xmlHasProp(child, BAD_CAST "ns")) {
7147 xmlSetProp(child, BAD_CAST "ns", ns);
7148 }
7149 }
7150 tmp = child->next;
7151 xmlUnlinkNode(child);
7152 ins = xmlAddNextSibling(ins, child);
7153 child = tmp;
7154 }
7155 if (ns != NULL)
7156 xmlFree(ns);
7157 delete = cur;
7158 goto skip_children;
7159 }
7160 }
7161 }
7162 /*
7163 * Simplification 4.2 whitespaces
7164 */
7165 else if ((cur->type == XML_TEXT_NODE) ||
7166 (cur->type == XML_CDATA_SECTION_NODE)) {
7167 if (IS_BLANK_NODE(cur)) {
7168 if (cur->parent->type == XML_ELEMENT_NODE) {
7169 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7170 &&
7171 (!xmlStrEqual
7172 (cur->parent->name, BAD_CAST "param")))
7173 delete = cur;
7174 } else {
7175 delete = cur;
7176 goto skip_children;
7177 }
7178 }
7179 } else {
7180 delete = cur;
7181 goto skip_children;
7182 }
7183
7184 /*
7185 * Skip to next node
7186 */
7187 if (cur->children != NULL) {
7188 if ((cur->children->type != XML_ENTITY_DECL) &&
7189 (cur->children->type != XML_ENTITY_REF_NODE) &&
7190 (cur->children->type != XML_ENTITY_NODE)) {
7191 cur = cur->children;
7192 continue;
7193 }
7194 }
7195 skip_children:
7196 if (cur->next != NULL) {
7197 cur = cur->next;
7198 continue;
7199 }
7200
7201 do {
7202 cur = cur->parent;
7203 if (cur == NULL)
7204 break;
7205 if (cur == root) {
7206 cur = NULL;
7207 break;
7208 }
7209 if (cur->next != NULL) {
7210 cur = cur->next;
7211 break;
7212 }
7213 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007214 }
7215 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007216 xmlUnlinkNode(delete);
7217 xmlFreeNode(delete);
7218 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007219 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007220}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007221
Daniel Veillardc5312d72003-02-21 17:14:10 +00007222/**
7223 * xmlRelaxNGCleanupDoc:
7224 * @ctxt: a Relax-NG parser context
7225 * @doc: an xmldocPtr document pointer
7226 *
7227 * Cleanup the document from unwanted nodes for parsing, resolve
7228 * Include and externalRef lookups.
7229 *
7230 * Returns the cleaned up document or NULL in case of error
7231 */
7232static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007233xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7234{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007235 xmlNodePtr root;
7236
7237 /*
7238 * Extract the root
7239 */
7240 root = xmlDocGetRootElement(doc);
7241 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007242 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7243 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007244 return (NULL);
7245 }
7246 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007247 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007248}
7249
7250/**
7251 * xmlRelaxNGParse:
7252 * @ctxt: a Relax-NG parser context
7253 *
7254 * parse a schema definition resource and build an internal
7255 * XML Shema struture which can be used to validate instances.
7256 * *WARNING* this interface is highly subject to change
7257 *
7258 * Returns the internal XML RelaxNG structure built from the resource or
7259 * NULL in case of error
7260 */
7261xmlRelaxNGPtr
7262xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7263{
7264 xmlRelaxNGPtr ret = NULL;
7265 xmlDocPtr doc;
7266 xmlNodePtr root;
7267
7268 xmlRelaxNGInitTypes();
7269
7270 if (ctxt == NULL)
7271 return (NULL);
7272
7273 /*
7274 * First step is to parse the input document into an DOM/Infoset
7275 */
7276 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007277 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007278 if (doc == NULL) {
7279 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7280 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7281 NULL);
7282 return (NULL);
7283 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007284 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007285 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007286 if (doc == NULL) {
7287 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7288 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7289 NULL);
7290 return (NULL);
7291 }
7292 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7293 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007294 } else if (ctxt->document != NULL) {
7295 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007296 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007297 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7298 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7299 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007300 }
7301 ctxt->document = doc;
7302
7303 /*
7304 * Some preprocessing of the document content
7305 */
7306 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7307 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007308 xmlFreeDoc(ctxt->document);
7309 ctxt->document = NULL;
7310 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007311 }
7312
Daniel Veillard6eadf632003-01-23 18:29:16 +00007313 /*
7314 * Then do the parsing for good
7315 */
7316 root = xmlDocGetRootElement(doc);
7317 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007318 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7319 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7320 ctxt->URL, NULL);
7321 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007322 return (NULL);
7323 }
7324 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007325 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007326 xmlFreeDoc(doc);
7327 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007328 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007329
7330 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007331 * Check the ref/defines links
7332 */
7333 /*
7334 * try to preprocess interleaves
7335 */
7336 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007337 xmlHashScan(ctxt->interleaves,
7338 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007339 }
7340
7341 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007342 * if there was a parsing error return NULL
7343 */
7344 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007345 xmlRelaxNGFree(ret);
7346 ctxt->document = NULL;
7347 xmlFreeDoc(doc);
7348 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007349 }
7350
7351 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007352 * try to compile (parts of) the schemas
7353 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007354 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7355 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007356 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007357
Daniel Veillard4c004142003-10-07 11:33:24 +00007358 def = xmlRelaxNGNewDefine(ctxt, NULL);
7359 if (def != NULL) {
7360 def->type = XML_RELAXNG_START;
7361 def->content = ret->topgrammar->start;
7362 ret->topgrammar->start = def;
7363 }
7364 }
7365 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007366 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007367
7368 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007369 * Transfer the pointer for cleanup at the schema level.
7370 */
7371 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007372 ctxt->document = NULL;
7373 ret->documents = ctxt->documents;
7374 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007375
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007376 ret->includes = ctxt->includes;
7377 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007378 ret->defNr = ctxt->defNr;
7379 ret->defTab = ctxt->defTab;
7380 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007381 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007382 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007383
7384 return (ret);
7385}
Daniel Veillard4c004142003-10-07 11:33:24 +00007386
Daniel Veillard6eadf632003-01-23 18:29:16 +00007387/**
7388 * xmlRelaxNGSetParserErrors:
7389 * @ctxt: a Relax-NG validation context
7390 * @err: the error callback
7391 * @warn: the warning callback
7392 * @ctx: contextual data for the callbacks
7393 *
7394 * Set the callback functions used to handle errors for a validation context
7395 */
7396void
7397xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007398 xmlRelaxNGValidityErrorFunc err,
7399 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7400{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007401 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007402 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007403 ctxt->error = err;
7404 ctxt->warning = warn;
7405 ctxt->userData = ctx;
7406}
Daniel Veillard409a8142003-07-18 15:16:57 +00007407
7408/**
7409 * xmlRelaxNGGetParserErrors:
7410 * @ctxt: a Relax-NG validation context
7411 * @err: the error callback result
7412 * @warn: the warning callback result
7413 * @ctx: contextual data for the callbacks result
7414 *
7415 * Get the callback information used to handle errors for a validation context
7416 *
7417 * Returns -1 in case of failure, 0 otherwise.
7418 */
7419int
7420xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007421 xmlRelaxNGValidityErrorFunc * err,
7422 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7423{
Daniel Veillard409a8142003-07-18 15:16:57 +00007424 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007425 return (-1);
7426 if (err != NULL)
7427 *err = ctxt->error;
7428 if (warn != NULL)
7429 *warn = ctxt->warning;
7430 if (ctx != NULL)
7431 *ctx = ctxt->userData;
7432 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007433}
7434
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007435#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007436
Daniel Veillard6eadf632003-01-23 18:29:16 +00007437/************************************************************************
7438 * *
7439 * Dump back a compiled form *
7440 * *
7441 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007442static void xmlRelaxNGDumpDefine(FILE * output,
7443 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007444
7445/**
7446 * xmlRelaxNGDumpDefines:
7447 * @output: the file output
7448 * @defines: a list of define structures
7449 *
7450 * Dump a RelaxNG structure back
7451 */
7452static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007453xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7454{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007455 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007456 xmlRelaxNGDumpDefine(output, defines);
7457 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007458 }
7459}
7460
7461/**
7462 * xmlRelaxNGDumpDefine:
7463 * @output: the file output
7464 * @define: a define structure
7465 *
7466 * Dump a RelaxNG structure back
7467 */
7468static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007469xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7470{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007471 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007472 return;
7473 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007474 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007475 fprintf(output, "<empty/>\n");
7476 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007477 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007478 fprintf(output, "<notAllowed/>\n");
7479 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007480 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007481 fprintf(output, "<text/>\n");
7482 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007483 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007484 fprintf(output, "<element>\n");
7485 if (define->name != NULL) {
7486 fprintf(output, "<name");
7487 if (define->ns != NULL)
7488 fprintf(output, " ns=\"%s\"", define->ns);
7489 fprintf(output, ">%s</name>\n", define->name);
7490 }
7491 xmlRelaxNGDumpDefines(output, define->attrs);
7492 xmlRelaxNGDumpDefines(output, define->content);
7493 fprintf(output, "</element>\n");
7494 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007495 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007496 fprintf(output, "<list>\n");
7497 xmlRelaxNGDumpDefines(output, define->content);
7498 fprintf(output, "</list>\n");
7499 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007500 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007501 fprintf(output, "<oneOrMore>\n");
7502 xmlRelaxNGDumpDefines(output, define->content);
7503 fprintf(output, "</oneOrMore>\n");
7504 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007505 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007506 fprintf(output, "<zeroOrMore>\n");
7507 xmlRelaxNGDumpDefines(output, define->content);
7508 fprintf(output, "</zeroOrMore>\n");
7509 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007510 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007511 fprintf(output, "<choice>\n");
7512 xmlRelaxNGDumpDefines(output, define->content);
7513 fprintf(output, "</choice>\n");
7514 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007515 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007516 fprintf(output, "<group>\n");
7517 xmlRelaxNGDumpDefines(output, define->content);
7518 fprintf(output, "</group>\n");
7519 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007520 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007521 fprintf(output, "<interleave>\n");
7522 xmlRelaxNGDumpDefines(output, define->content);
7523 fprintf(output, "</interleave>\n");
7524 break;
7525 case XML_RELAXNG_OPTIONAL:
7526 fprintf(output, "<optional>\n");
7527 xmlRelaxNGDumpDefines(output, define->content);
7528 fprintf(output, "</optional>\n");
7529 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007530 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007531 fprintf(output, "<attribute>\n");
7532 xmlRelaxNGDumpDefines(output, define->content);
7533 fprintf(output, "</attribute>\n");
7534 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007535 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007536 fprintf(output, "<define");
7537 if (define->name != NULL)
7538 fprintf(output, " name=\"%s\"", define->name);
7539 fprintf(output, ">\n");
7540 xmlRelaxNGDumpDefines(output, define->content);
7541 fprintf(output, "</define>\n");
7542 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007543 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007544 fprintf(output, "<ref");
7545 if (define->name != NULL)
7546 fprintf(output, " name=\"%s\"", define->name);
7547 fprintf(output, ">\n");
7548 xmlRelaxNGDumpDefines(output, define->content);
7549 fprintf(output, "</ref>\n");
7550 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007551 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007552 fprintf(output, "<parentRef");
7553 if (define->name != NULL)
7554 fprintf(output, " name=\"%s\"", define->name);
7555 fprintf(output, ">\n");
7556 xmlRelaxNGDumpDefines(output, define->content);
7557 fprintf(output, "</parentRef>\n");
7558 break;
7559 case XML_RELAXNG_EXTERNALREF:
7560 fprintf(output, "<externalRef>");
7561 xmlRelaxNGDumpDefines(output, define->content);
7562 fprintf(output, "</externalRef>\n");
7563 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007564 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007565 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007566 TODO break;
7567 case XML_RELAXNG_START:
7568 case XML_RELAXNG_EXCEPT:
7569 case XML_RELAXNG_PARAM:
7570 TODO break;
7571 case XML_RELAXNG_NOOP:
7572 xmlRelaxNGDumpDefines(output, define->content);
7573 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007574 }
7575}
Daniel Veillard4c004142003-10-07 11:33:24 +00007576
Daniel Veillard6eadf632003-01-23 18:29:16 +00007577/**
7578 * xmlRelaxNGDumpGrammar:
7579 * @output: the file output
7580 * @grammar: a grammar structure
7581 * @top: is this a top grammar
7582 *
7583 * Dump a RelaxNG structure back
7584 */
7585static void
7586xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7587{
7588 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007589 return;
7590
Daniel Veillard6eadf632003-01-23 18:29:16 +00007591 fprintf(output, "<grammar");
7592 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007593 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7594 switch (grammar->combine) {
7595 case XML_RELAXNG_COMBINE_UNDEFINED:
7596 break;
7597 case XML_RELAXNG_COMBINE_CHOICE:
7598 fprintf(output, " combine=\"choice\"");
7599 break;
7600 case XML_RELAXNG_COMBINE_INTERLEAVE:
7601 fprintf(output, " combine=\"interleave\"");
7602 break;
7603 default:
7604 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007605 }
7606 fprintf(output, ">\n");
7607 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007608 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007609 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007610 fprintf(output, "<start>\n");
7611 xmlRelaxNGDumpDefine(output, grammar->start);
7612 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007613 }
7614 /* TODO ? Dump the defines ? */
7615 fprintf(output, "</grammar>\n");
7616}
7617
7618/**
7619 * xmlRelaxNGDump:
7620 * @output: the file output
7621 * @schema: a schema structure
7622 *
7623 * Dump a RelaxNG structure back
7624 */
7625void
7626xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7627{
7628 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007629 fprintf(output, "RelaxNG empty or failed to compile\n");
7630 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007631 }
7632 fprintf(output, "RelaxNG: ");
7633 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007634 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007635 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007636 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007637 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007638 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007639 }
7640 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007641 fprintf(output, "RelaxNG has no top grammar\n");
7642 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007643 }
7644 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7645}
7646
Daniel Veillardfebcca42003-02-16 15:44:18 +00007647/**
7648 * xmlRelaxNGDumpTree:
7649 * @output: the file output
7650 * @schema: a schema structure
7651 *
7652 * Dump the transformed RelaxNG tree.
7653 */
7654void
7655xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7656{
7657 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007658 fprintf(output, "RelaxNG empty or failed to compile\n");
7659 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007660 }
7661 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007662 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007663 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007664 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007665 }
7666}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007667#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007668
Daniel Veillard6eadf632003-01-23 18:29:16 +00007669/************************************************************************
7670 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007671 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007672 * *
7673 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007674static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7675 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007676
7677/**
7678 * xmlRelaxNGValidateCompiledCallback:
7679 * @exec: the regular expression instance
7680 * @token: the token which matched
7681 * @transdata: callback data, the define for the subelement if available
7682 @ @inputdata: callback data, the Relax NG validation context
7683 *
7684 * Handle the callback and if needed validate the element children.
7685 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007686static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007687xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007688 const xmlChar * token,
7689 void *transdata, void *inputdata)
7690{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007691 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7692 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7693 int ret;
7694
7695#ifdef DEBUG_COMPILE
7696 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007697 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007698#endif
7699 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007700 fprintf(stderr, "callback on %s missing context\n", token);
7701 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7702 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7703 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007704 }
7705 if (define == NULL) {
7706 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007707 return;
7708 fprintf(stderr, "callback on %s missing define\n", token);
7709 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7710 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7711 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007712 }
7713 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007714 fprintf(stderr, "callback on %s missing info\n", token);
7715 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7716 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7717 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007718 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007719 fprintf(stderr, "callback on %s define is not element\n", token);
7720 if (ctxt->errNo == XML_RELAXNG_OK)
7721 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7722 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007723 }
7724 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007725 if (ret != 0)
7726 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007727}
7728
7729/**
7730 * xmlRelaxNGValidateCompiledContent:
7731 * @ctxt: the RelaxNG validation context
7732 * @regexp: the regular expression as compiled
7733 * @content: list of children to test against the regexp
7734 *
7735 * Validate the content model of an element or start using the regexp
7736 *
7737 * Returns 0 in case of success, -1 in case of error.
7738 */
7739static int
7740xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007741 xmlRegexpPtr regexp, xmlNodePtr content)
7742{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007743 xmlRegExecCtxtPtr exec;
7744 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007745 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007746 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007747
7748 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007749 return (-1);
7750 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007751 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007752 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007753 cur = content;
7754 while (cur != NULL) {
7755 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007756 switch (cur->type) {
7757 case XML_TEXT_NODE:
7758 case XML_CDATA_SECTION_NODE:
7759 if (xmlIsBlankNode(cur))
7760 break;
7761 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7762 if (ret < 0) {
7763 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7764 cur->parent->name);
7765 }
7766 break;
7767 case XML_ELEMENT_NODE:
7768 if (cur->ns != NULL) {
7769 ret = xmlRegExecPushString2(exec, cur->name,
7770 cur->ns->href, ctxt);
7771 } else {
7772 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7773 }
7774 if (ret < 0) {
7775 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7776 }
7777 break;
7778 default:
7779 break;
7780 }
7781 if (ret < 0)
7782 break;
7783 /*
7784 * Switch to next element
7785 */
7786 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007787 }
7788 ret = xmlRegExecPushString(exec, NULL, NULL);
7789 if (ret == 1) {
7790 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007791 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007792 } else if (ret == 0) {
7793 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007794 * TODO: get some of the names needed to exit the current state of exec
7795 */
7796 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7797 ret = -1;
7798 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7799 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007800 } else {
7801 ret = -1;
7802 }
7803 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007804 /*
7805 * There might be content model errors outside of the pure
7806 * regexp validation, e.g. for attribute values.
7807 */
7808 if ((ret == 0) && (ctxt->perr != 0)) {
7809 ret = ctxt->perr;
7810 }
7811 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007812 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007813}
7814
7815/************************************************************************
7816 * *
7817 * Progressive validation of when possible *
7818 * *
7819 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007820static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7821 xmlRelaxNGDefinePtr defines);
7822static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00007823 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007824static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007825
7826/**
7827 * xmlRelaxNGElemPush:
7828 * @ctxt: the validation context
7829 * @exec: the regexp runtime for the new content model
7830 *
7831 * Push a new regexp for the current node content model on the stack
7832 *
7833 * Returns 0 in case of success and -1 in case of error.
7834 */
7835static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007836xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7837{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007838 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007839 ctxt->elemMax = 10;
7840 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7841 sizeof
7842 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007843 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007844 xmlRngVErrMemory(ctxt, "validating\n");
7845 return (-1);
7846 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007847 }
7848 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007849 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007850 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007851 ctxt->elemMax *
7852 sizeof
7853 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007854 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007855 xmlRngVErrMemory(ctxt, "validating\n");
7856 return (-1);
7857 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007858 }
7859 ctxt->elemTab[ctxt->elemNr++] = exec;
7860 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007861 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007862}
7863
7864/**
7865 * xmlRelaxNGElemPop:
7866 * @ctxt: the validation context
7867 *
7868 * Pop the regexp of the current node content model from the stack
7869 *
7870 * Returns the exec or NULL if empty
7871 */
7872static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007873xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7874{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007875 xmlRegExecCtxtPtr ret;
7876
Daniel Veillard4c004142003-10-07 11:33:24 +00007877 if (ctxt->elemNr <= 0)
7878 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007879 ctxt->elemNr--;
7880 ret = ctxt->elemTab[ctxt->elemNr];
7881 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007882 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007883 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7884 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007885 ctxt->elem = NULL;
7886 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007887}
7888
7889/**
7890 * xmlRelaxNGValidateProgressiveCallback:
7891 * @exec: the regular expression instance
7892 * @token: the token which matched
7893 * @transdata: callback data, the define for the subelement if available
7894 @ @inputdata: callback data, the Relax NG validation context
7895 *
7896 * Handle the callback and if needed validate the element children.
7897 * some of the in/out informations are passed via the context in @inputdata.
7898 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007899static void
7900xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7901 ATTRIBUTE_UNUSED,
7902 const xmlChar * token,
7903 void *transdata, void *inputdata)
7904{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007905 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7906 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007907 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007908 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007909 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007910
7911#ifdef DEBUG_PROGRESSIVE
7912 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007913 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007914#endif
7915 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007916 fprintf(stderr, "callback on %s missing context\n", token);
7917 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007918 }
7919 ctxt->pstate = 1;
7920 if (define == NULL) {
7921 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007922 return;
7923 fprintf(stderr, "callback on %s missing define\n", token);
7924 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7925 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7926 ctxt->pstate = -1;
7927 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007928 }
7929 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007930 fprintf(stderr, "callback on %s missing info\n", token);
7931 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7932 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7933 ctxt->pstate = -1;
7934 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007935 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007936 fprintf(stderr, "callback on %s define is not element\n", token);
7937 if (ctxt->errNo == XML_RELAXNG_OK)
7938 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7939 ctxt->pstate = -1;
7940 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007941 }
7942 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007943 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7944 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7945 xmlRelaxNGDumpValidError(ctxt);
7946 ctxt->pstate = -1;
7947 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007948 }
7949 if (define->contModel == NULL) {
7950 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007951 * this node cannot be validated in a streamable fashion
7952 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00007953#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00007954 xmlGenericError(xmlGenericErrorContext,
7955 "Element '%s' validation is not streamable\n",
7956 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007957#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00007958 ctxt->pstate = 0;
7959 ctxt->pdef = define;
7960 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007961 }
7962 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00007963 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007964 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007965 ctxt->pstate = -1;
7966 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007967 }
7968 xmlRelaxNGElemPush(ctxt, exec);
7969
7970 /*
7971 * Validate the attributes part of the content.
7972 */
7973 state = xmlRelaxNGNewValidState(ctxt, node);
7974 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007975 ctxt->pstate = -1;
7976 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007977 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007978 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007979 ctxt->state = state;
7980 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007981 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
7982 if (ret != 0) {
7983 ctxt->pstate = -1;
7984 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
7985 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007986 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007987 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007988 ctxt->state->seq = NULL;
7989 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
7990 if (ret != 0) {
7991 ctxt->pstate = -1;
7992 }
7993 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00007994 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007995 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007996
7997 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007998
Daniel Veillard4c004142003-10-07 11:33:24 +00007999 for (i = 0; i < ctxt->states->nbState; i++) {
8000 state = ctxt->states->tabState[i];
8001 ctxt->state = state;
8002 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008003
Daniel Veillard4c004142003-10-07 11:33:24 +00008004 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8005 tmp = 0;
8006 break;
8007 }
8008 }
8009 if (tmp != 0) {
8010 /*
8011 * validation error, log the message for the "best" one
8012 */
8013 ctxt->flags |= FLAGS_IGNORABLE;
8014 xmlRelaxNGLogBestError(ctxt);
8015 }
8016 for (i = 0; i < ctxt->states->nbState; i++) {
8017 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8018 }
8019 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8020 ctxt->states = NULL;
8021 if ((ret == 0) && (tmp == -1))
8022 ctxt->pstate = -1;
8023 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008024 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008025 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008026 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8027 xmlRelaxNGDumpValidError(ctxt);
8028 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008029 }
8030 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008031}
8032
8033/**
8034 * xmlRelaxNGValidatePushElement:
8035 * @ctxt: the validation context
8036 * @doc: a document instance
8037 * @elem: an element instance
8038 *
8039 * Push a new element start on the RelaxNG validation stack.
8040 *
8041 * returns 1 if no validation problem was found or 0 if validating the
8042 * element requires a full node, and -1 in case of error.
8043 */
8044int
Daniel Veillard33300b42003-04-17 09:09:19 +00008045xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8046 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008047 xmlNodePtr elem)
8048{
8049 int ret = 1;
8050
8051 if ((ctxt == NULL) || (elem == NULL))
8052 return (-1);
8053
8054#ifdef DEBUG_PROGRESSIVE
8055 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8056#endif
8057 if (ctxt->elem == 0) {
8058 xmlRelaxNGPtr schema;
8059 xmlRelaxNGGrammarPtr grammar;
8060 xmlRegExecCtxtPtr exec;
8061 xmlRelaxNGDefinePtr define;
8062
8063 schema = ctxt->schema;
8064 if (schema == NULL) {
8065 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8066 return (-1);
8067 }
8068 grammar = schema->topgrammar;
8069 if ((grammar == NULL) || (grammar->start == NULL)) {
8070 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8071 return (-1);
8072 }
8073 define = grammar->start;
8074 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008075 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008076 return (0);
8077 }
8078 exec = xmlRegNewExecCtxt(define->contModel,
8079 xmlRelaxNGValidateProgressiveCallback,
8080 ctxt);
8081 if (exec == NULL) {
8082 return (-1);
8083 }
8084 xmlRelaxNGElemPush(ctxt, exec);
8085 }
8086 ctxt->pnode = elem;
8087 ctxt->pstate = 0;
8088 if (elem->ns != NULL) {
8089 ret =
8090 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8091 ctxt);
8092 } else {
8093 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8094 }
8095 if (ret < 0) {
8096 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8097 } else {
8098 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008099 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008100 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008101 ret = -1;
8102 else
8103 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008104 }
8105#ifdef DEBUG_PROGRESSIVE
8106 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008107 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8108 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008109#endif
8110 return (ret);
8111}
8112
8113/**
8114 * xmlRelaxNGValidatePushCData:
8115 * @ctxt: the RelaxNG validation context
8116 * @data: some character data read
8117 * @len: the lenght of the data
8118 *
8119 * check the CData parsed for validation in the current stack
8120 *
8121 * returns 1 if no validation problem was found or -1 otherwise
8122 */
8123int
8124xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008125 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008126{
8127 int ret = 1;
8128
8129 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8130 return (-1);
8131
8132#ifdef DEBUG_PROGRESSIVE
8133 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8134#endif
8135
8136 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008137 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008138 break;
8139 data++;
8140 }
8141 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008142 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008143
8144 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8145 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008146 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008147#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008148 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008149#endif
8150
Daniel Veillard4c004142003-10-07 11:33:24 +00008151 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008152 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008153 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008154}
8155
8156/**
8157 * xmlRelaxNGValidatePopElement:
8158 * @ctxt: the RelaxNG validation context
8159 * @doc: a document instance
8160 * @elem: an element instance
8161 *
8162 * Pop the element end from the RelaxNG validation stack.
8163 *
8164 * returns 1 if no validation problem was found or 0 otherwise
8165 */
8166int
8167xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8168 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008169 xmlNodePtr elem)
8170{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008171 int ret;
8172 xmlRegExecCtxtPtr exec;
8173
Daniel Veillard4c004142003-10-07 11:33:24 +00008174 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8175 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008176#ifdef DEBUG_PROGRESSIVE
8177 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8178#endif
8179 /*
8180 * verify that we reached a terminal state of the content model.
8181 */
8182 exec = xmlRelaxNGElemPop(ctxt);
8183 ret = xmlRegExecPushString(exec, NULL, NULL);
8184 if (ret == 0) {
8185 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008186 * TODO: get some of the names needed to exit the current state of exec
8187 */
8188 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8189 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008190 } else if (ret < 0) {
8191 ret = -1;
8192 } else {
8193 ret = 1;
8194 }
8195 xmlRegFreeExecCtxt(exec);
8196#ifdef DEBUG_PROGRESSIVE
8197 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008198 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8199 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008200#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008201 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008202}
8203
8204/**
8205 * xmlRelaxNGValidateFullElement:
8206 * @ctxt: the validation context
8207 * @doc: a document instance
8208 * @elem: an element instance
8209 *
8210 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8211 * 0 and the content of the node has been expanded.
8212 *
8213 * returns 1 if no validation problem was found or -1 in case of error.
8214 */
8215int
Daniel Veillard33300b42003-04-17 09:09:19 +00008216xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8217 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008218 xmlNodePtr elem)
8219{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008220 int ret;
8221 xmlRelaxNGValidStatePtr state;
8222
Daniel Veillard4c004142003-10-07 11:33:24 +00008223 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8224 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008225#ifdef DEBUG_PROGRESSIVE
8226 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8227#endif
8228 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8229 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008230 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008231 }
8232 state->seq = elem;
8233 ctxt->state = state;
8234 ctxt->errNo = XML_RELAXNG_OK;
8235 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008236 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8237 ret = -1;
8238 else
8239 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008240 xmlRelaxNGFreeValidState(ctxt, state);
8241 ctxt->state = NULL;
8242#ifdef DEBUG_PROGRESSIVE
8243 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008244 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8245 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008246#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008247 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008248}
8249
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008250/************************************************************************
8251 * *
8252 * Generic interpreted validation implementation *
8253 * *
8254 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008255static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8256 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008257
8258/**
8259 * xmlRelaxNGSkipIgnored:
8260 * @ctxt: a schema validation context
8261 * @node: the top node.
8262 *
8263 * Skip ignorable nodes in that context
8264 *
8265 * Returns the new sibling or NULL in case of error.
8266 */
8267static xmlNodePtr
8268xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008269 xmlNodePtr node)
8270{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008271 /*
8272 * TODO complete and handle entities
8273 */
8274 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008275 ((node->type == XML_COMMENT_NODE) ||
8276 (node->type == XML_PI_NODE) ||
8277 (((node->type == XML_TEXT_NODE) ||
8278 (node->type == XML_CDATA_SECTION_NODE)) &&
8279 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8280 (IS_BLANK_NODE(node)))))) {
8281 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008282 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008283 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008284}
8285
8286/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008287 * xmlRelaxNGNormalize:
8288 * @ctxt: a schema validation context
8289 * @str: the string to normalize
8290 *
8291 * Implements the normalizeWhiteSpace( s ) function from
8292 * section 6.2.9 of the spec
8293 *
8294 * Returns the new string or NULL in case of error.
8295 */
8296static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008297xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8298{
Daniel Veillardedc91922003-01-26 00:52:04 +00008299 xmlChar *ret, *p;
8300 const xmlChar *tmp;
8301 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008302
Daniel Veillardedc91922003-01-26 00:52:04 +00008303 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008304 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008305 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008306 while (*tmp != 0)
8307 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008308 len = tmp - str;
8309
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008310 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008311 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008312 xmlRngVErrMemory(ctxt, "validating\n");
8313 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008314 }
8315 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008316 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008317 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008318 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008319 if (IS_BLANK_CH(*str)) {
8320 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008321 str++;
8322 if (*str == 0)
8323 break;
8324 *p++ = ' ';
8325 } else
8326 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008327 }
8328 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008329 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008330}
8331
8332/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008333 * xmlRelaxNGValidateDatatype:
8334 * @ctxt: a Relax-NG validation context
8335 * @value: the string value
8336 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008337 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008338 *
8339 * Validate the given value against the dataype
8340 *
8341 * Returns 0 if the validation succeeded or an error code.
8342 */
8343static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008344xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8345 const xmlChar * value,
8346 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8347{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008348 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008349 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008350 void *result = NULL;
8351 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008352
8353 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008354 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008355 }
8356 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008357 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008358 if ((define->attrs != NULL) &&
8359 (define->attrs->type == XML_RELAXNG_PARAM)) {
8360 ret =
8361 lib->check(lib->data, define->name, value, &result, node);
8362 } else {
8363 ret = lib->check(lib->data, define->name, value, NULL, node);
8364 }
8365 } else
8366 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008367 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008368 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8369 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8370 lib->freef(lib->data, result);
8371 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008372 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008373 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008374 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008375 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008376 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008377 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8378 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008379 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008380 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008381 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008382 if (lib->facet != NULL) {
8383 tmp = lib->facet(lib->data, define->name, cur->name,
8384 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008385 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008386 ret = -1;
8387 }
8388 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008389 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008390 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008391 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008392
Daniel Veillard4c004142003-10-07 11:33:24 +00008393 oldvalue = ctxt->state->value;
8394 oldendvalue = ctxt->state->endvalue;
8395 ctxt->state->value = (xmlChar *) value;
8396 ctxt->state->endvalue = NULL;
8397 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8398 ctxt->state->value = (xmlChar *) oldvalue;
8399 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008400 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008401 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008402 lib->freef(lib->data, result);
8403 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008404}
8405
8406/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008407 * xmlRelaxNGNextValue:
8408 * @ctxt: a Relax-NG validation context
8409 *
8410 * Skip to the next value when validating within a list
8411 *
8412 * Returns 0 if the operation succeeded or an error code.
8413 */
8414static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008415xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8416{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008417 xmlChar *cur;
8418
8419 cur = ctxt->state->value;
8420 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008421 ctxt->state->value = NULL;
8422 ctxt->state->endvalue = NULL;
8423 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008424 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008425 while (*cur != 0)
8426 cur++;
8427 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8428 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008429 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008430 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008431 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008432 ctxt->state->value = cur;
8433 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008434}
8435
8436/**
8437 * xmlRelaxNGValidateValueList:
8438 * @ctxt: a Relax-NG validation context
8439 * @defines: the list of definitions to verify
8440 *
8441 * Validate the given set of definitions for the current value
8442 *
8443 * Returns 0 if the validation succeeded or an error code.
8444 */
8445static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008446xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8447 xmlRelaxNGDefinePtr defines)
8448{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008449 int ret = 0;
8450
8451 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008452 ret = xmlRelaxNGValidateValue(ctxt, defines);
8453 if (ret != 0)
8454 break;
8455 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008456 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008457 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008458}
8459
8460/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008461 * xmlRelaxNGValidateValue:
8462 * @ctxt: a Relax-NG validation context
8463 * @define: the definition to verify
8464 *
8465 * Validate the given definition for the current value
8466 *
8467 * Returns 0 if the validation succeeded or an error code.
8468 */
8469static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008470xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8471 xmlRelaxNGDefinePtr define)
8472{
Daniel Veillardedc91922003-01-26 00:52:04 +00008473 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008474 xmlChar *value;
8475
8476 value = ctxt->state->value;
8477 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008478 case XML_RELAXNG_EMPTY:{
8479 if ((value != NULL) && (value[0] != 0)) {
8480 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008481
William M. Brack76e95df2003-10-18 16:20:14 +00008482 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008483 idx++;
8484 if (value[idx] != 0)
8485 ret = -1;
8486 }
8487 break;
8488 }
8489 case XML_RELAXNG_TEXT:
8490 break;
8491 case XML_RELAXNG_VALUE:{
8492 if (!xmlStrEqual(value, define->value)) {
8493 if (define->name != NULL) {
8494 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008495
Daniel Veillard4c004142003-10-07 11:33:24 +00008496 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8497 if ((lib != NULL) && (lib->comp != NULL)) {
8498 ret = lib->comp(lib->data, define->name,
8499 define->value, define->node,
8500 (void *) define->attrs,
8501 value, ctxt->state->node);
8502 } else
8503 ret = -1;
8504 if (ret < 0) {
8505 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8506 define->name);
8507 return (-1);
8508 } else if (ret == 1) {
8509 ret = 0;
8510 } else {
8511 ret = -1;
8512 }
8513 } else {
8514 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008515
Daniel Veillard4c004142003-10-07 11:33:24 +00008516 /*
8517 * TODO: trivial optimizations are possible by
8518 * computing at compile-time
8519 */
8520 nval = xmlRelaxNGNormalize(ctxt, define->value);
8521 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008522
Daniel Veillard4c004142003-10-07 11:33:24 +00008523 if ((nval == NULL) || (nvalue == NULL) ||
8524 (!xmlStrEqual(nval, nvalue)))
8525 ret = -1;
8526 if (nval != NULL)
8527 xmlFree(nval);
8528 if (nvalue != NULL)
8529 xmlFree(nvalue);
8530 }
8531 }
8532 if (ret == 0)
8533 xmlRelaxNGNextValue(ctxt);
8534 break;
8535 }
8536 case XML_RELAXNG_DATATYPE:{
8537 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8538 ctxt->state->seq);
8539 if (ret == 0)
8540 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008541
Daniel Veillard4c004142003-10-07 11:33:24 +00008542 break;
8543 }
8544 case XML_RELAXNG_CHOICE:{
8545 xmlRelaxNGDefinePtr list = define->content;
8546 xmlChar *oldvalue;
8547
8548 oldflags = ctxt->flags;
8549 ctxt->flags |= FLAGS_IGNORABLE;
8550
8551 oldvalue = ctxt->state->value;
8552 while (list != NULL) {
8553 ret = xmlRelaxNGValidateValue(ctxt, list);
8554 if (ret == 0) {
8555 break;
8556 }
8557 ctxt->state->value = oldvalue;
8558 list = list->next;
8559 }
8560 ctxt->flags = oldflags;
8561 if (ret != 0) {
8562 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8563 xmlRelaxNGDumpValidError(ctxt);
8564 } else {
8565 if (ctxt->errNr > 0)
8566 xmlRelaxNGPopErrors(ctxt, 0);
8567 }
8568 if (ret == 0)
8569 xmlRelaxNGNextValue(ctxt);
8570 break;
8571 }
8572 case XML_RELAXNG_LIST:{
8573 xmlRelaxNGDefinePtr list = define->content;
8574 xmlChar *oldvalue, *oldend, *val, *cur;
8575
Daniel Veillard416589a2003-02-17 17:25:42 +00008576#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008577 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008578#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008579
Daniel Veillard4c004142003-10-07 11:33:24 +00008580 oldvalue = ctxt->state->value;
8581 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008582
Daniel Veillard4c004142003-10-07 11:33:24 +00008583 val = xmlStrdup(oldvalue);
8584 if (val == NULL) {
8585 val = xmlStrdup(BAD_CAST "");
8586 }
8587 if (val == NULL) {
8588 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8589 return (-1);
8590 }
8591 cur = val;
8592 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008593 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008594 *cur = 0;
8595 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008596#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008597 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008598#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008599 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008600 *cur++ = 0;
8601 } else
8602 cur++;
8603 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008604#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008605 xmlGenericError(xmlGenericErrorContext,
8606 "list value: '%s' found %d items\n",
8607 oldvalue, nb_values);
8608 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008609#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008610 ctxt->state->endvalue = cur;
8611 cur = val;
8612 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8613 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008614
Daniel Veillard4c004142003-10-07 11:33:24 +00008615 ctxt->state->value = cur;
8616
8617 while (list != NULL) {
8618 if (ctxt->state->value == ctxt->state->endvalue)
8619 ctxt->state->value = NULL;
8620 ret = xmlRelaxNGValidateValue(ctxt, list);
8621 if (ret != 0) {
8622#ifdef DEBUG_LIST
8623 xmlGenericError(xmlGenericErrorContext,
8624 "Failed to validate value: '%s' with %d rule\n",
8625 ctxt->state->value, nb_values);
8626#endif
8627 break;
8628 }
8629#ifdef DEBUG_LIST
8630 nb_values++;
8631#endif
8632 list = list->next;
8633 }
8634
8635 if ((ret == 0) && (ctxt->state->value != NULL) &&
8636 (ctxt->state->value != ctxt->state->endvalue)) {
8637 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8638 ctxt->state->value);
8639 ret = -1;
8640 }
8641 xmlFree(val);
8642 ctxt->state->value = oldvalue;
8643 ctxt->state->endvalue = oldend;
8644 break;
8645 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008646 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008647 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8648 if (ret != 0) {
8649 break;
8650 }
8651 /* no break on purpose */
8652 case XML_RELAXNG_ZEROORMORE:{
8653 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008654
Daniel Veillard4c004142003-10-07 11:33:24 +00008655 oldflags = ctxt->flags;
8656 ctxt->flags |= FLAGS_IGNORABLE;
8657 cur = ctxt->state->value;
8658 temp = NULL;
8659 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8660 (temp != cur)) {
8661 temp = cur;
8662 ret =
8663 xmlRelaxNGValidateValueList(ctxt, define->content);
8664 if (ret != 0) {
8665 ctxt->state->value = temp;
8666 ret = 0;
8667 break;
8668 }
8669 cur = ctxt->state->value;
8670 }
8671 ctxt->flags = oldflags;
8672 if (ret != 0) {
8673 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8674 xmlRelaxNGDumpValidError(ctxt);
8675 } else {
8676 if (ctxt->errNr > 0)
8677 xmlRelaxNGPopErrors(ctxt, 0);
8678 }
8679 break;
8680 }
8681 case XML_RELAXNG_EXCEPT:{
8682 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008683
Daniel Veillard4c004142003-10-07 11:33:24 +00008684 list = define->content;
8685 while (list != NULL) {
8686 ret = xmlRelaxNGValidateValue(ctxt, list);
8687 if (ret == 0) {
8688 ret = -1;
8689 break;
8690 } else
8691 ret = 0;
8692 list = list->next;
8693 }
8694 break;
8695 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008696 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008697 case XML_RELAXNG_GROUP:{
8698 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008699
Daniel Veillard4c004142003-10-07 11:33:24 +00008700 list = define->content;
8701 while (list != NULL) {
8702 ret = xmlRelaxNGValidateValue(ctxt, list);
8703 if (ret != 0) {
8704 ret = -1;
8705 break;
8706 } else
8707 ret = 0;
8708 list = list->next;
8709 }
8710 break;
8711 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008712 case XML_RELAXNG_REF:
8713 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008714 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8715 break;
8716 default:
8717 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008718 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008719 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008720}
8721
8722/**
8723 * xmlRelaxNGValidateValueContent:
8724 * @ctxt: a Relax-NG validation context
8725 * @defines: the list of definitions to verify
8726 *
8727 * Validate the given definitions for the current value
8728 *
8729 * Returns 0 if the validation succeeded or an error code.
8730 */
8731static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008732xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8733 xmlRelaxNGDefinePtr defines)
8734{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008735 int ret = 0;
8736
8737 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008738 ret = xmlRelaxNGValidateValue(ctxt, defines);
8739 if (ret != 0)
8740 break;
8741 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008742 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008743 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008744}
8745
8746/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008747 * xmlRelaxNGAttributeMatch:
8748 * @ctxt: a Relax-NG validation context
8749 * @define: the definition to check
8750 * @prop: the attribute
8751 *
8752 * Check if the attribute matches the definition nameClass
8753 *
8754 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8755 */
8756static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008757xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8758 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8759{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008760 int ret;
8761
8762 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008763 if (!xmlStrEqual(define->name, prop->name))
8764 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008765 }
8766 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008767 if (define->ns[0] == 0) {
8768 if (prop->ns != NULL)
8769 return (0);
8770 } else {
8771 if ((prop->ns == NULL) ||
8772 (!xmlStrEqual(define->ns, prop->ns->href)))
8773 return (0);
8774 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008775 }
8776 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008777 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008778 define = define->nameClass;
8779 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008780 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008781
Daniel Veillard4c004142003-10-07 11:33:24 +00008782 list = define->content;
8783 while (list != NULL) {
8784 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8785 if (ret == 1)
8786 return (0);
8787 if (ret < 0)
8788 return (ret);
8789 list = list->next;
8790 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008791 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008792 TODO}
8793 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008794}
8795
8796/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008797 * xmlRelaxNGValidateAttribute:
8798 * @ctxt: a Relax-NG validation context
8799 * @define: the definition to verify
8800 *
8801 * Validate the given attribute definition for that node
8802 *
8803 * Returns 0 if the validation succeeded or an error code.
8804 */
8805static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008806xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8807 xmlRelaxNGDefinePtr define)
8808{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008809 int ret = 0, i;
8810 xmlChar *value, *oldvalue;
8811 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008812 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008813
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008814 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008815 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008816 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008817 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8818 tmp = ctxt->state->attrs[i];
8819 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8820 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8821 (tmp->ns == NULL)) ||
8822 ((tmp->ns != NULL) &&
8823 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8824 prop = tmp;
8825 break;
8826 }
8827 }
8828 }
8829 if (prop != NULL) {
8830 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8831 oldvalue = ctxt->state->value;
8832 oldseq = ctxt->state->seq;
8833 ctxt->state->seq = (xmlNodePtr) prop;
8834 ctxt->state->value = value;
8835 ctxt->state->endvalue = NULL;
8836 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8837 if (ctxt->state->value != NULL)
8838 value = ctxt->state->value;
8839 if (value != NULL)
8840 xmlFree(value);
8841 ctxt->state->value = oldvalue;
8842 ctxt->state->seq = oldseq;
8843 if (ret == 0) {
8844 /*
8845 * flag the attribute as processed
8846 */
8847 ctxt->state->attrs[i] = NULL;
8848 ctxt->state->nbAttrLeft--;
8849 }
8850 } else {
8851 ret = -1;
8852 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008853#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008854 xmlGenericError(xmlGenericErrorContext,
8855 "xmlRelaxNGValidateAttribute(%s): %d\n",
8856 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008857#endif
8858 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008859 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8860 tmp = ctxt->state->attrs[i];
8861 if ((tmp != NULL) &&
8862 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8863 prop = tmp;
8864 break;
8865 }
8866 }
8867 if (prop != NULL) {
8868 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8869 oldvalue = ctxt->state->value;
8870 oldseq = ctxt->state->seq;
8871 ctxt->state->seq = (xmlNodePtr) prop;
8872 ctxt->state->value = value;
8873 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8874 if (ctxt->state->value != NULL)
8875 value = ctxt->state->value;
8876 if (value != NULL)
8877 xmlFree(value);
8878 ctxt->state->value = oldvalue;
8879 ctxt->state->seq = oldseq;
8880 if (ret == 0) {
8881 /*
8882 * flag the attribute as processed
8883 */
8884 ctxt->state->attrs[i] = NULL;
8885 ctxt->state->nbAttrLeft--;
8886 }
8887 } else {
8888 ret = -1;
8889 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008890#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008891 if (define->ns != NULL) {
8892 xmlGenericError(xmlGenericErrorContext,
8893 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8894 define->ns, ret);
8895 } else {
8896 xmlGenericError(xmlGenericErrorContext,
8897 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8898 ret);
8899 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008900#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008901 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008902
8903 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008904}
8905
8906/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008907 * xmlRelaxNGValidateAttributeList:
8908 * @ctxt: a Relax-NG validation context
8909 * @define: the list of definition to verify
8910 *
8911 * Validate the given node against the list of attribute definitions
8912 *
8913 * Returns 0 if the validation succeeded or an error code.
8914 */
8915static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008916xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8917 xmlRelaxNGDefinePtr defines)
8918{
Daniel Veillardce192eb2003-04-16 15:58:05 +00008919 int ret = 0, res;
8920 int needmore = 0;
8921 xmlRelaxNGDefinePtr cur;
8922
8923 cur = defines;
8924 while (cur != NULL) {
8925 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008926 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8927 ret = -1;
8928 } else
8929 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008930 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008931 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008932 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00008933 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008934 cur = defines;
8935 while (cur != NULL) {
8936 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008937 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8938 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8939 if (res < 0)
8940 ret = -1;
8941 } else {
8942 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8943 return (-1);
8944 }
8945 if (res == -1) /* continues on -2 */
8946 break;
8947 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008948 cur = cur->next;
8949 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008950
8951 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008952}
8953
8954/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008955 * xmlRelaxNGNodeMatchesList:
8956 * @node: the node
8957 * @list: a NULL terminated array of definitions
8958 *
8959 * Check if a node can be matched by one of the definitions
8960 *
8961 * Returns 1 if matches 0 otherwise
8962 */
8963static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008964xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
8965{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008966 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008967 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008968
8969 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008970 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008971
8972 cur = list[i++];
8973 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008974 if ((node->type == XML_ELEMENT_NODE) &&
8975 (cur->type == XML_RELAXNG_ELEMENT)) {
8976 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8977 if (tmp == 1)
8978 return (1);
8979 } else if (((node->type == XML_TEXT_NODE) ||
8980 (node->type == XML_CDATA_SECTION_NODE)) &&
8981 (cur->type == XML_RELAXNG_TEXT)) {
8982 return (1);
8983 }
8984 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008985 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008986 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008987}
8988
8989/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008990 * xmlRelaxNGValidateInterleave:
8991 * @ctxt: a Relax-NG validation context
8992 * @define: the definition to verify
8993 *
8994 * Validate an interleave definition for a node.
8995 *
8996 * Returns 0 if the validation succeeded or an error code.
8997 */
8998static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008999xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9000 xmlRelaxNGDefinePtr define)
9001{
William M. Brack779af002003-08-01 15:55:39 +00009002 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009003 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009004 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009005
9006 xmlRelaxNGValidStatePtr oldstate;
9007 xmlRelaxNGPartitionPtr partitions;
9008 xmlRelaxNGInterleaveGroupPtr group = NULL;
9009 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9010 xmlNodePtr *list = NULL, *lasts = NULL;
9011
9012 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009013 partitions = (xmlRelaxNGPartitionPtr) define->data;
9014 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009015 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009016 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9017 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009018 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009019 /*
9020 * Optimizations for MIXED
9021 */
9022 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009023 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009024 ctxt->flags |= FLAGS_MIXED_CONTENT;
9025 if (nbgroups == 2) {
9026 /*
9027 * this is a pure <mixed> case
9028 */
9029 if (ctxt->state != NULL)
9030 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9031 ctxt->state->seq);
9032 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9033 ret = xmlRelaxNGValidateDefinition(ctxt,
9034 partitions->groups[1]->
9035 rule);
9036 else
9037 ret = xmlRelaxNGValidateDefinition(ctxt,
9038 partitions->groups[0]->
9039 rule);
9040 if (ret == 0) {
9041 if (ctxt->state != NULL)
9042 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9043 ctxt->state->
9044 seq);
9045 }
9046 ctxt->flags = oldflags;
9047 return (ret);
9048 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009049 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009050
9051 /*
9052 * Build arrays to store the first and last node of the chain
9053 * pertaining to each group
9054 */
9055 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9056 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009057 xmlRngVErrMemory(ctxt, "validating\n");
9058 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009059 }
9060 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9061 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9062 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009063 xmlRngVErrMemory(ctxt, "validating\n");
9064 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009065 }
9066 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9067
9068 /*
9069 * Walk the sequence of children finding the right group and
9070 * sorting them in sequences.
9071 */
9072 cur = ctxt->state->seq;
9073 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9074 start = cur;
9075 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009076 ctxt->state->seq = cur;
9077 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009078 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009079 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009080
Daniel Veillard4c004142003-10-07 11:33:24 +00009081 if ((cur->type == XML_TEXT_NODE) ||
9082 (cur->type == XML_CDATA_SECTION_NODE)) {
9083 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9084 NULL);
9085 } else if (cur->type == XML_ELEMENT_NODE) {
9086 if (cur->ns != NULL) {
9087 tmp = xmlHashLookup2(partitions->triage, cur->name,
9088 cur->ns->href);
9089 if (tmp == NULL)
9090 tmp = xmlHashLookup2(partitions->triage,
9091 BAD_CAST "#any",
9092 cur->ns->href);
9093 } else
9094 tmp =
9095 xmlHashLookup2(partitions->triage, cur->name,
9096 NULL);
9097 if (tmp == NULL)
9098 tmp =
9099 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9100 NULL);
9101 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009102
Daniel Veillard4c004142003-10-07 11:33:24 +00009103 if (tmp == NULL) {
9104 i = nbgroups;
9105 } else {
9106 i = ((long) tmp) - 1;
9107 if (partitions->flags & IS_NEEDCHECK) {
9108 group = partitions->groups[i];
9109 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9110 i = nbgroups;
9111 }
9112 }
9113 } else {
9114 for (i = 0; i < nbgroups; i++) {
9115 group = partitions->groups[i];
9116 if (group == NULL)
9117 continue;
9118 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9119 break;
9120 }
9121 }
9122 /*
9123 * We break as soon as an element not matched is found
9124 */
9125 if (i >= nbgroups) {
9126 break;
9127 }
9128 if (lasts[i] != NULL) {
9129 lasts[i]->next = cur;
9130 lasts[i] = cur;
9131 } else {
9132 list[i] = cur;
9133 lasts[i] = cur;
9134 }
9135 if (cur->next != NULL)
9136 lastchg = cur->next;
9137 else
9138 lastchg = cur;
9139 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009140 }
9141 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009142 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9143 ret = -1;
9144 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009145 }
9146 lastelem = cur;
9147 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009148 for (i = 0; i < nbgroups; i++) {
9149 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9150 group = partitions->groups[i];
9151 if (lasts[i] != NULL) {
9152 last = lasts[i]->next;
9153 lasts[i]->next = NULL;
9154 }
9155 ctxt->state->seq = list[i];
9156 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9157 if (ret != 0)
9158 break;
9159 if (ctxt->state != NULL) {
9160 cur = ctxt->state->seq;
9161 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9162 xmlRelaxNGFreeValidState(ctxt, oldstate);
9163 oldstate = ctxt->state;
9164 ctxt->state = NULL;
9165 if (cur != NULL) {
9166 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9167 ret = -1;
9168 ctxt->state = oldstate;
9169 goto done;
9170 }
9171 } else if (ctxt->states != NULL) {
9172 int j;
9173 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009174
Daniel Veillard4c004142003-10-07 11:33:24 +00009175 for (j = 0; j < ctxt->states->nbState; j++) {
9176 cur = ctxt->states->tabState[j]->seq;
9177 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9178 if (cur == NULL) {
9179 found = 1;
9180 break;
9181 }
9182 }
9183 if (ctxt->states->nbState > 0) {
9184 xmlRelaxNGFreeValidState(ctxt, oldstate);
9185 oldstate =
9186 ctxt->states->tabState[ctxt->states->nbState - 1];
9187 }
9188 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9189 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9190 }
9191 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9192 ctxt->states = NULL;
9193 if (found == 0) {
9194 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9195 ret = -1;
9196 ctxt->state = oldstate;
9197 goto done;
9198 }
9199 } else {
9200 ret = -1;
9201 break;
9202 }
9203 if (lasts[i] != NULL) {
9204 lasts[i]->next = last;
9205 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009206 }
9207 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009208 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009209 ctxt->state = oldstate;
9210 ctxt->state->seq = lastelem;
9211 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009212 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9213 ret = -1;
9214 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009215 }
9216
Daniel Veillard4c004142003-10-07 11:33:24 +00009217 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009218 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009219 /*
9220 * builds the next links chain from the prev one
9221 */
9222 cur = lastchg;
9223 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009224 if ((cur == start) || (cur->prev == NULL))
9225 break;
9226 cur->prev->next = cur;
9227 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009228 }
9229 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009230 if (ctxt->errNr > errNr)
9231 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009232 }
9233
9234 xmlFree(list);
9235 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009236 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009237}
9238
9239/**
9240 * xmlRelaxNGValidateDefinitionList:
9241 * @ctxt: a Relax-NG validation context
9242 * @define: the list of definition to verify
9243 *
9244 * Validate the given node content against the (list) of definitions
9245 *
9246 * Returns 0 if the validation succeeded or an error code.
9247 */
9248static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009249xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9250 xmlRelaxNGDefinePtr defines)
9251{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009252 int ret = 0, res;
9253
9254
Daniel Veillard952379b2003-03-17 15:37:12 +00009255 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009256 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9257 BAD_CAST "NULL definition list");
9258 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009259 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009260 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009261 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9262 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9263 if (res < 0)
9264 ret = -1;
9265 } else {
9266 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9267 return (-1);
9268 }
9269 if (res == -1) /* continues on -2 */
9270 break;
9271 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009272 }
9273
Daniel Veillard4c004142003-10-07 11:33:24 +00009274 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009275}
9276
9277/**
9278 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009279 * @ctxt: a Relax-NG validation context
9280 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009281 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009282 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009283 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009284 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009285 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009286 */
9287static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009288xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9289 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9290{
Daniel Veillard580ced82003-03-21 21:22:48 +00009291 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009292
Daniel Veillardfd573f12003-03-16 17:52:32 +00009293 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009294 if (!xmlStrEqual(elem->name, define->name)) {
9295 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9296 return (0);
9297 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009298 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009299 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009300 if (elem->ns == NULL) {
9301 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9302 return (0);
9303 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9304 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9305 elem->name, define->ns);
9306 return (0);
9307 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009308 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009309 (define->name == NULL)) {
9310 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9311 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009312 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009313 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9314 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009315 }
9316
9317 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009318 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009319
9320 define = define->nameClass;
9321 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009322 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009323
Daniel Veillard4c004142003-10-07 11:33:24 +00009324 if (ctxt != NULL) {
9325 oldflags = ctxt->flags;
9326 ctxt->flags |= FLAGS_IGNORABLE;
9327 }
9328
9329 list = define->content;
9330 while (list != NULL) {
9331 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9332 if (ret == 1) {
9333 if (ctxt != NULL)
9334 ctxt->flags = oldflags;
9335 return (0);
9336 }
9337 if (ret < 0) {
9338 if (ctxt != NULL)
9339 ctxt->flags = oldflags;
9340 return (ret);
9341 }
9342 list = list->next;
9343 }
9344 ret = 1;
9345 if (ctxt != NULL) {
9346 ctxt->flags = oldflags;
9347 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009348 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009349 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009350
Daniel Veillard4c004142003-10-07 11:33:24 +00009351 if (ctxt != NULL) {
9352 oldflags = ctxt->flags;
9353 ctxt->flags |= FLAGS_IGNORABLE;
9354 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009355
Daniel Veillard4c004142003-10-07 11:33:24 +00009356 list = define->nameClass;
9357 while (list != NULL) {
9358 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9359 if (ret == 1) {
9360 if (ctxt != NULL)
9361 ctxt->flags = oldflags;
9362 return (1);
9363 }
9364 if (ret < 0) {
9365 if (ctxt != NULL)
9366 ctxt->flags = oldflags;
9367 return (ret);
9368 }
9369 list = list->next;
9370 }
9371 if (ctxt != NULL) {
9372 if (ret != 0) {
9373 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9374 xmlRelaxNGDumpValidError(ctxt);
9375 } else {
9376 if (ctxt->errNr > 0)
9377 xmlRelaxNGPopErrors(ctxt, 0);
9378 }
9379 }
9380 ret = 0;
9381 if (ctxt != NULL) {
9382 ctxt->flags = oldflags;
9383 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009384 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009385 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009386 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009387 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009388}
9389
9390/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009391 * xmlRelaxNGBestState:
9392 * @ctxt: a Relax-NG validation context
9393 *
9394 * Find the "best" state in the ctxt->states list of states to report
9395 * errors about. I.e. a state with no element left in the child list
9396 * or the one with the less attributes left.
9397 * This is called only if a falidation error was detected
9398 *
9399 * Returns the index of the "best" state or -1 in case of error
9400 */
9401static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009402xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9403{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009404 xmlRelaxNGValidStatePtr state;
9405 int i, tmp;
9406 int best = -1;
9407 int value = 1000000;
9408
9409 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9410 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009411 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009412
Daniel Veillard4c004142003-10-07 11:33:24 +00009413 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009414 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009415 if (state == NULL)
9416 continue;
9417 if (state->seq != NULL) {
9418 if ((best == -1) || (value > 100000)) {
9419 value = 100000;
9420 best = i;
9421 }
9422 } else {
9423 tmp = state->nbAttrLeft;
9424 if ((best == -1) || (value > tmp)) {
9425 value = tmp;
9426 best = i;
9427 }
9428 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009429 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009430 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009431}
9432
9433/**
9434 * xmlRelaxNGLogBestError:
9435 * @ctxt: a Relax-NG validation context
9436 *
9437 * Find the "best" state in the ctxt->states list of states to report
9438 * errors about and log it.
9439 */
9440static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009441xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9442{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009443 int best;
9444
9445 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9446 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009447 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009448
9449 best = xmlRelaxNGBestState(ctxt);
9450 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009451 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009452
Daniel Veillard4c004142003-10-07 11:33:24 +00009453 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009454 }
9455}
9456
9457/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009458 * xmlRelaxNGValidateElementEnd:
9459 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009460 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009461 *
9462 * Validate the end of the element, implements check that
9463 * there is nothing left not consumed in the element content
9464 * or in the attribute list.
9465 *
9466 * Returns 0 if the validation succeeded or an error code.
9467 */
9468static int
William M. Brack272693c2003-11-14 16:20:34 +00009469xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009470{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009471 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009472 xmlRelaxNGValidStatePtr state;
9473
9474 state = ctxt->state;
9475 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009476 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9477 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009478 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009479 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9480 state->node->name, state->seq->name);
9481 }
9482 return (-1);
9483 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009484 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009485 for (i = 0; i < state->nbAttrs; i++) {
9486 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009487 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009488 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9489 state->attrs[i]->name, state->node->name);
9490 }
9491 return (-1 - i);
9492 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009493 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009494 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009495}
9496
9497/**
9498 * xmlRelaxNGValidateState:
9499 * @ctxt: a Relax-NG validation context
9500 * @define: the definition to verify
9501 *
9502 * Validate the current state against the definition
9503 *
9504 * Returns 0 if the validation succeeded or an error code.
9505 */
9506static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009507xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9508 xmlRelaxNGDefinePtr define)
9509{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009510 xmlNodePtr node;
9511 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009512 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009513
9514 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009515 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9516 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009517 }
9518
9519 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009520 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009521 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009522 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009523 }
9524#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009525 for (i = 0; i < ctxt->depth; i++)
9526 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009527 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009528 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009529 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009530 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009531 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009532 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009533 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009534 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009535#endif
9536 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009537 switch (define->type) {
9538 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009539 node = xmlRelaxNGSkipIgnored(ctxt, node);
9540 ret = 0;
9541 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009542 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009543 ret = -1;
9544 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009545 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009546 while ((node != NULL) &&
9547 ((node->type == XML_TEXT_NODE) ||
9548 (node->type == XML_COMMENT_NODE) ||
9549 (node->type == XML_PI_NODE) ||
9550 (node->type == XML_CDATA_SECTION_NODE)))
9551 node = node->next;
9552 ctxt->state->seq = node;
9553 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009554 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009555 errNr = ctxt->errNr;
9556 node = xmlRelaxNGSkipIgnored(ctxt, node);
9557 if (node == NULL) {
9558 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9559 ret = -1;
9560 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9561 xmlRelaxNGDumpValidError(ctxt);
9562 break;
9563 }
9564 if (node->type != XML_ELEMENT_NODE) {
9565 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9566 ret = -1;
9567 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9568 xmlRelaxNGDumpValidError(ctxt);
9569 break;
9570 }
9571 /*
9572 * This node was already validated successfully against
9573 * this definition.
9574 */
9575 if (node->_private == define) {
9576 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9577 if (ctxt->errNr > errNr)
9578 xmlRelaxNGPopErrors(ctxt, errNr);
9579 if (ctxt->errNr != 0) {
9580 while ((ctxt->err != NULL) &&
9581 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9582 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9583 ||
9584 ((ctxt->err->err ==
9585 XML_RELAXNG_ERR_ELEMEXTRANS)
9586 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9587 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9588 || (ctxt->err->err ==
9589 XML_RELAXNG_ERR_NOTELEM)))
9590 xmlRelaxNGValidErrorPop(ctxt);
9591 }
9592 break;
9593 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009594
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009595 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9596 if (ret <= 0) {
9597 ret = -1;
9598 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9599 xmlRelaxNGDumpValidError(ctxt);
9600 break;
9601 }
9602 ret = 0;
9603 if (ctxt->errNr != 0) {
9604 if (ctxt->errNr > errNr)
9605 xmlRelaxNGPopErrors(ctxt, errNr);
9606 while ((ctxt->err != NULL) &&
9607 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9608 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9609 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9610 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9611 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9612 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9613 xmlRelaxNGValidErrorPop(ctxt);
9614 }
9615 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009616
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009617 oldflags = ctxt->flags;
9618 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9619 ctxt->flags -= FLAGS_MIXED_CONTENT;
9620 }
9621 state = xmlRelaxNGNewValidState(ctxt, node);
9622 if (state == NULL) {
9623 ret = -1;
9624 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9625 xmlRelaxNGDumpValidError(ctxt);
9626 break;
9627 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009628
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009629 oldstate = ctxt->state;
9630 ctxt->state = state;
9631 if (define->attrs != NULL) {
9632 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9633 if (tmp != 0) {
9634 ret = -1;
9635 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9636 }
9637 }
9638 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009639 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9640 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9641 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009642
Daniel Veillard4c004142003-10-07 11:33:24 +00009643 nstate = xmlRelaxNGNewValidState(ctxt, node);
9644 ctxt->state = nstate;
9645 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009646
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009647 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9648 define->contModel,
9649 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009650 nseq = ctxt->state->seq;
9651 ctxt->state = tmpstate;
9652 ctxt->states = tmpstates;
9653 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009654
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009655#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009656 xmlGenericError(xmlGenericErrorContext,
9657 "Validating content of '%s' : %d\n",
9658 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009659#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009660 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009661 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009662
9663 if (ctxt->states != NULL) {
9664 tmp = -1;
9665
Daniel Veillardce192eb2003-04-16 15:58:05 +00009666 for (i = 0; i < ctxt->states->nbState; i++) {
9667 state = ctxt->states->tabState[i];
9668 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009669 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009670
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009671 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009672 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009673 break;
9674 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009675 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009676 if (tmp != 0) {
9677 /*
9678 * validation error, log the message for the "best" one
9679 */
9680 ctxt->flags |= FLAGS_IGNORABLE;
9681 xmlRelaxNGLogBestError(ctxt);
9682 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009683 for (i = 0; i < ctxt->states->nbState; i++) {
9684 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009685 ctxt->states->
9686 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009687 }
9688 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9689 ctxt->flags = oldflags;
9690 ctxt->states = NULL;
9691 if ((ret == 0) && (tmp == -1))
9692 ret = -1;
9693 } else {
9694 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009695 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009696 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009697 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009698 xmlRelaxNGFreeValidState(ctxt, state);
9699 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009700 } else {
9701 if (define->content != NULL) {
9702 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009703 define->
9704 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009705 if (tmp != 0) {
9706 ret = -1;
9707 if (ctxt->state == NULL) {
9708 ctxt->state = oldstate;
9709 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9710 node->name);
9711 ctxt->state = NULL;
9712 } else {
9713 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9714 node->name);
9715 }
9716
9717 }
9718 }
9719 if (ctxt->states != NULL) {
9720 tmp = -1;
9721
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009722 for (i = 0; i < ctxt->states->nbState; i++) {
9723 state = ctxt->states->tabState[i];
9724 ctxt->state = state;
9725
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009726 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009727 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009728 break;
9729 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009730 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009731 if (tmp != 0) {
9732 /*
9733 * validation error, log the message for the "best" one
9734 */
9735 ctxt->flags |= FLAGS_IGNORABLE;
9736 xmlRelaxNGLogBestError(ctxt);
9737 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009738 for (i = 0; i < ctxt->states->nbState; i++) {
9739 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009740 ctxt->states->
9741 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009742 }
9743 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9744 ctxt->flags = oldflags;
9745 ctxt->states = NULL;
9746 if ((ret == 0) && (tmp == -1))
9747 ret = -1;
9748 } else {
9749 state = ctxt->state;
9750 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009751 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009752 xmlRelaxNGFreeValidState(ctxt, state);
9753 }
9754 }
9755 if (ret == 0) {
9756 node->_private = define;
9757 }
9758 ctxt->flags = oldflags;
9759 ctxt->state = oldstate;
9760 if (oldstate != NULL)
9761 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9762 if (ret != 0) {
9763 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9764 xmlRelaxNGDumpValidError(ctxt);
9765 ret = 0;
9766 } else {
9767 ret = -2;
9768 }
9769 } else {
9770 if (ctxt->errNr > errNr)
9771 xmlRelaxNGPopErrors(ctxt, errNr);
9772 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009773
9774#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009775 xmlGenericError(xmlGenericErrorContext,
9776 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9777 node->name, ret);
9778 if (oldstate == NULL)
9779 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9780 else if (oldstate->seq == NULL)
9781 xmlGenericError(xmlGenericErrorContext, ": done\n");
9782 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9783 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9784 oldstate->seq->name);
9785 else
9786 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9787 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009788#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009789 break;
9790 case XML_RELAXNG_OPTIONAL:{
9791 errNr = ctxt->errNr;
9792 oldflags = ctxt->flags;
9793 ctxt->flags |= FLAGS_IGNORABLE;
9794 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9795 ret =
9796 xmlRelaxNGValidateDefinitionList(ctxt,
9797 define->content);
9798 if (ret != 0) {
9799 if (ctxt->state != NULL)
9800 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9801 ctxt->state = oldstate;
9802 ctxt->flags = oldflags;
9803 ret = 0;
9804 if (ctxt->errNr > errNr)
9805 xmlRelaxNGPopErrors(ctxt, errNr);
9806 break;
9807 }
9808 if (ctxt->states != NULL) {
9809 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9810 } else {
9811 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9812 if (ctxt->states == NULL) {
9813 xmlRelaxNGFreeValidState(ctxt, oldstate);
9814 ctxt->flags = oldflags;
9815 ret = -1;
9816 if (ctxt->errNr > errNr)
9817 xmlRelaxNGPopErrors(ctxt, errNr);
9818 break;
9819 }
9820 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9821 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9822 ctxt->state = NULL;
9823 }
9824 ctxt->flags = oldflags;
9825 ret = 0;
9826 if (ctxt->errNr > errNr)
9827 xmlRelaxNGPopErrors(ctxt, errNr);
9828 break;
9829 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009830 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009831 errNr = ctxt->errNr;
9832 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9833 if (ret != 0) {
9834 break;
9835 }
9836 if (ctxt->errNr > errNr)
9837 xmlRelaxNGPopErrors(ctxt, errNr);
9838 /* no break on purpose */
9839 case XML_RELAXNG_ZEROORMORE:{
9840 int progress;
9841 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9842 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009843
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009844 errNr = ctxt->errNr;
9845 res = xmlRelaxNGNewStates(ctxt, 1);
9846 if (res == NULL) {
9847 ret = -1;
9848 break;
9849 }
9850 /*
9851 * All the input states are also exit states
9852 */
9853 if (ctxt->state != NULL) {
9854 xmlRelaxNGAddStates(ctxt, res,
9855 xmlRelaxNGCopyValidState(ctxt,
9856 ctxt->
9857 state));
9858 } else {
9859 for (j = 0; j < ctxt->states->nbState; j++) {
9860 xmlRelaxNGAddStates(ctxt, res,
9861 xmlRelaxNGCopyValidState(ctxt,
9862 ctxt->
9863 states->
9864 tabState
9865 [j]));
9866 }
9867 }
9868 oldflags = ctxt->flags;
9869 ctxt->flags |= FLAGS_IGNORABLE;
9870 do {
9871 progress = 0;
9872 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009873
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009874 if (ctxt->states != NULL) {
9875 states = ctxt->states;
9876 for (i = 0; i < states->nbState; i++) {
9877 ctxt->state = states->tabState[i];
9878 ctxt->states = NULL;
9879 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9880 define->
9881 content);
9882 if (ret == 0) {
9883 if (ctxt->state != NULL) {
9884 tmp = xmlRelaxNGAddStates(ctxt, res,
9885 ctxt->state);
9886 ctxt->state = NULL;
9887 if (tmp == 1)
9888 progress = 1;
9889 } else if (ctxt->states != NULL) {
9890 for (j = 0; j < ctxt->states->nbState;
9891 j++) {
9892 tmp =
9893 xmlRelaxNGAddStates(ctxt, res,
9894 ctxt->
9895 states->
9896 tabState
9897 [j]);
9898 if (tmp == 1)
9899 progress = 1;
9900 }
9901 xmlRelaxNGFreeStates(ctxt,
9902 ctxt->states);
9903 ctxt->states = NULL;
9904 }
9905 } else {
9906 if (ctxt->state != NULL) {
9907 xmlRelaxNGFreeValidState(ctxt,
9908 ctxt->state);
9909 ctxt->state = NULL;
9910 }
9911 }
9912 }
9913 } else {
9914 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9915 define->
9916 content);
9917 if (ret != 0) {
9918 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9919 ctxt->state = NULL;
9920 } else {
9921 base = res->nbState;
9922 if (ctxt->state != NULL) {
9923 tmp = xmlRelaxNGAddStates(ctxt, res,
9924 ctxt->state);
9925 ctxt->state = NULL;
9926 if (tmp == 1)
9927 progress = 1;
9928 } else if (ctxt->states != NULL) {
9929 for (j = 0; j < ctxt->states->nbState; j++) {
9930 tmp = xmlRelaxNGAddStates(ctxt, res,
9931 ctxt->
9932 states->
9933 tabState[j]);
9934 if (tmp == 1)
9935 progress = 1;
9936 }
9937 if (states == NULL) {
9938 states = ctxt->states;
9939 } else {
9940 xmlRelaxNGFreeStates(ctxt,
9941 ctxt->states);
9942 }
9943 ctxt->states = NULL;
9944 }
9945 }
9946 }
9947 if (progress) {
9948 /*
9949 * Collect all the new nodes added at that step
9950 * and make them the new node set
9951 */
9952 if (res->nbState - base == 1) {
9953 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9954 res->
9955 tabState
9956 [base]);
9957 } else {
9958 if (states == NULL) {
9959 xmlRelaxNGNewStates(ctxt,
9960 res->nbState - base);
9961 }
9962 states->nbState = 0;
9963 for (i = base; i < res->nbState; i++)
9964 xmlRelaxNGAddStates(ctxt, states,
9965 xmlRelaxNGCopyValidState
9966 (ctxt,
9967 res->tabState[i]));
9968 ctxt->states = states;
9969 }
9970 }
9971 } while (progress == 1);
9972 if (states != NULL) {
9973 xmlRelaxNGFreeStates(ctxt, states);
9974 }
9975 ctxt->states = res;
9976 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009977#if 0
9978 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00009979 * errors may have to be propagated back...
9980 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009981 if (ctxt->errNr > errNr)
9982 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009983#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009984 ret = 0;
9985 break;
9986 }
9987 case XML_RELAXNG_CHOICE:{
9988 xmlRelaxNGDefinePtr list = NULL;
9989 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009990
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009991 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009992
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009993 errNr = ctxt->errNr;
9994 if ((define->dflags & IS_TRIABLE)
9995 && (define->data != NULL)) {
9996 xmlHashTablePtr triage =
9997 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +00009998
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009999 /*
10000 * Something we can optimize cleanly there is only one
10001 * possble branch out !
10002 */
10003 if (node == NULL) {
10004 ret = -1;
10005 break;
10006 }
10007 if ((node->type == XML_TEXT_NODE) ||
10008 (node->type == XML_CDATA_SECTION_NODE)) {
10009 list =
10010 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10011 } else if (node->type == XML_ELEMENT_NODE) {
10012 if (node->ns != NULL) {
10013 list = xmlHashLookup2(triage, node->name,
10014 node->ns->href);
10015 if (list == NULL)
10016 list =
10017 xmlHashLookup2(triage, BAD_CAST "#any",
10018 node->ns->href);
10019 } else
10020 list =
10021 xmlHashLookup2(triage, node->name, NULL);
10022 if (list == NULL)
10023 list =
10024 xmlHashLookup2(triage, BAD_CAST "#any",
10025 NULL);
10026 }
10027 if (list == NULL) {
10028 ret = -1;
10029 break;
10030 }
10031 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10032 if (ret == 0) {
10033 }
10034 break;
10035 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010036
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010037 list = define->content;
10038 oldflags = ctxt->flags;
10039 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010040
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010041 while (list != NULL) {
10042 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10043 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10044 if (ret == 0) {
10045 if (states == NULL) {
10046 states = xmlRelaxNGNewStates(ctxt, 1);
10047 }
10048 if (ctxt->state != NULL) {
10049 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10050 } else if (ctxt->states != NULL) {
10051 for (i = 0; i < ctxt->states->nbState; i++) {
10052 xmlRelaxNGAddStates(ctxt, states,
10053 ctxt->states->
10054 tabState[i]);
10055 }
10056 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10057 ctxt->states = NULL;
10058 }
10059 } else {
10060 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10061 }
10062 ctxt->state = oldstate;
10063 list = list->next;
10064 }
10065 if (states != NULL) {
10066 xmlRelaxNGFreeValidState(ctxt, oldstate);
10067 ctxt->states = states;
10068 ctxt->state = NULL;
10069 ret = 0;
10070 } else {
10071 ctxt->states = NULL;
10072 }
10073 ctxt->flags = oldflags;
10074 if (ret != 0) {
10075 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10076 xmlRelaxNGDumpValidError(ctxt);
10077 }
10078 } else {
10079 if (ctxt->errNr > errNr)
10080 xmlRelaxNGPopErrors(ctxt, errNr);
10081 }
10082 break;
10083 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010084 case XML_RELAXNG_DEF:
10085 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010086 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10087 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010088 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010089 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10090 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010091 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010092 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10093 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010094 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010095 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010096 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010097 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010098 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010099 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10100 break;
10101 case XML_RELAXNG_DATATYPE:{
10102 xmlNodePtr child;
10103 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010104
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010105 child = node;
10106 while (child != NULL) {
10107 if (child->type == XML_ELEMENT_NODE) {
10108 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10109 node->parent->name);
10110 ret = -1;
10111 break;
10112 } else if ((child->type == XML_TEXT_NODE) ||
10113 (child->type == XML_CDATA_SECTION_NODE)) {
10114 content = xmlStrcat(content, child->content);
10115 }
10116 /* TODO: handle entities ... */
10117 child = child->next;
10118 }
10119 if (ret == -1) {
10120 if (content != NULL)
10121 xmlFree(content);
10122 break;
10123 }
10124 if (content == NULL) {
10125 content = xmlStrdup(BAD_CAST "");
10126 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010127 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010128 ret = -1;
10129 break;
10130 }
10131 }
10132 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10133 ctxt->state->seq);
10134 if (ret == -1) {
10135 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10136 } else if (ret == 0) {
10137 ctxt->state->seq = NULL;
10138 }
10139 if (content != NULL)
10140 xmlFree(content);
10141 break;
10142 }
10143 case XML_RELAXNG_VALUE:{
10144 xmlChar *content = NULL;
10145 xmlChar *oldvalue;
10146 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010147
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010148 child = node;
10149 while (child != NULL) {
10150 if (child->type == XML_ELEMENT_NODE) {
10151 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10152 node->parent->name);
10153 ret = -1;
10154 break;
10155 } else if ((child->type == XML_TEXT_NODE) ||
10156 (child->type == XML_CDATA_SECTION_NODE)) {
10157 content = xmlStrcat(content, child->content);
10158 }
10159 /* TODO: handle entities ... */
10160 child = child->next;
10161 }
10162 if (ret == -1) {
10163 if (content != NULL)
10164 xmlFree(content);
10165 break;
10166 }
10167 if (content == NULL) {
10168 content = xmlStrdup(BAD_CAST "");
10169 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010170 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010171 ret = -1;
10172 break;
10173 }
10174 }
10175 oldvalue = ctxt->state->value;
10176 ctxt->state->value = content;
10177 ret = xmlRelaxNGValidateValue(ctxt, define);
10178 ctxt->state->value = oldvalue;
10179 if (ret == -1) {
10180 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10181 } else if (ret == 0) {
10182 ctxt->state->seq = NULL;
10183 }
10184 if (content != NULL)
10185 xmlFree(content);
10186 break;
10187 }
10188 case XML_RELAXNG_LIST:{
10189 xmlChar *content;
10190 xmlNodePtr child;
10191 xmlChar *oldvalue, *oldendvalue;
10192 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010193
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010194 /*
10195 * Make sure it's only text nodes
10196 */
10197
10198 content = NULL;
10199 child = node;
10200 while (child != NULL) {
10201 if (child->type == XML_ELEMENT_NODE) {
10202 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10203 node->parent->name);
10204 ret = -1;
10205 break;
10206 } else if ((child->type == XML_TEXT_NODE) ||
10207 (child->type == XML_CDATA_SECTION_NODE)) {
10208 content = xmlStrcat(content, child->content);
10209 }
10210 /* TODO: handle entities ... */
10211 child = child->next;
10212 }
10213 if (ret == -1) {
10214 if (content != NULL)
10215 xmlFree(content);
10216 break;
10217 }
10218 if (content == NULL) {
10219 content = xmlStrdup(BAD_CAST "");
10220 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010221 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010222 ret = -1;
10223 break;
10224 }
10225 }
10226 len = xmlStrlen(content);
10227 oldvalue = ctxt->state->value;
10228 oldendvalue = ctxt->state->endvalue;
10229 ctxt->state->value = content;
10230 ctxt->state->endvalue = content + len;
10231 ret = xmlRelaxNGValidateValue(ctxt, define);
10232 ctxt->state->value = oldvalue;
10233 ctxt->state->endvalue = oldendvalue;
10234 if (ret == -1) {
10235 VALID_ERR(XML_RELAXNG_ERR_LIST);
10236 } else if ((ret == 0) && (node != NULL)) {
10237 ctxt->state->seq = node->next;
10238 }
10239 if (content != NULL)
10240 xmlFree(content);
10241 break;
10242 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010243 case XML_RELAXNG_EXCEPT:
10244 case XML_RELAXNG_PARAM:
10245 TODO ret = -1;
10246 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010247 }
10248 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010249#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010250 for (i = 0; i < ctxt->depth; i++)
10251 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010252 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010253 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010254 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010255 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010256 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010257 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010258 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010259 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010260#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010261 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010262}
10263
10264/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010265 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010266 * @ctxt: a Relax-NG validation context
10267 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010268 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010269 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010270 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010271 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010272 */
10273static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010274xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10275 xmlRelaxNGDefinePtr define)
10276{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010277 xmlRelaxNGStatesPtr states, res;
10278 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010279
Daniel Veillardfd573f12003-03-16 17:52:32 +000010280 /*
10281 * We should NOT have both ctxt->state and ctxt->states
10282 */
10283 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010284 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10285 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010286 }
10287
10288 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010289 if (ctxt->states != NULL) {
10290 ctxt->state = ctxt->states->tabState[0];
10291 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10292 ctxt->states = NULL;
10293 }
10294 ret = xmlRelaxNGValidateState(ctxt, define);
10295 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10296 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10297 ctxt->state = NULL;
10298 }
10299 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10300 ctxt->state = ctxt->states->tabState[0];
10301 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10302 ctxt->states = NULL;
10303 }
10304 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010305 }
10306
10307 states = ctxt->states;
10308 ctxt->states = NULL;
10309 res = NULL;
10310 j = 0;
10311 oldflags = ctxt->flags;
10312 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010313 for (i = 0; i < states->nbState; i++) {
10314 ctxt->state = states->tabState[i];
10315 ctxt->states = NULL;
10316 ret = xmlRelaxNGValidateState(ctxt, define);
10317 /*
10318 * We should NOT have both ctxt->state and ctxt->states
10319 */
10320 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10321 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10322 ctxt->state = NULL;
10323 }
10324 if (ret == 0) {
10325 if (ctxt->states == NULL) {
10326 if (res != NULL) {
10327 /* add the state to the container */
10328 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10329 ctxt->state = NULL;
10330 } else {
10331 /* add the state directly in states */
10332 states->tabState[j++] = ctxt->state;
10333 ctxt->state = NULL;
10334 }
10335 } else {
10336 if (res == NULL) {
10337 /* make it the new container and copy other results */
10338 res = ctxt->states;
10339 ctxt->states = NULL;
10340 for (k = 0; k < j; k++)
10341 xmlRelaxNGAddStates(ctxt, res,
10342 states->tabState[k]);
10343 } else {
10344 /* add all the new results to res and reff the container */
10345 for (k = 0; k < ctxt->states->nbState; k++)
10346 xmlRelaxNGAddStates(ctxt, res,
10347 ctxt->states->tabState[k]);
10348 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10349 ctxt->states = NULL;
10350 }
10351 }
10352 } else {
10353 if (ctxt->state != NULL) {
10354 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10355 ctxt->state = NULL;
10356 } else if (ctxt->states != NULL) {
10357 for (k = 0; k < ctxt->states->nbState; k++)
10358 xmlRelaxNGFreeValidState(ctxt,
10359 ctxt->states->tabState[k]);
10360 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10361 ctxt->states = NULL;
10362 }
10363 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010364 }
10365 ctxt->flags = oldflags;
10366 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010367 xmlRelaxNGFreeStates(ctxt, states);
10368 ctxt->states = res;
10369 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010370 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010371 states->nbState = j;
10372 ctxt->states = states;
10373 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010374 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010375 ctxt->state = states->tabState[0];
10376 xmlRelaxNGFreeStates(ctxt, states);
10377 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010378 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010379 ret = -1;
10380 xmlRelaxNGFreeStates(ctxt, states);
10381 if (ctxt->states != NULL) {
10382 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10383 ctxt->states = NULL;
10384 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010385 }
10386 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010387 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10388 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010389 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010390 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010391}
10392
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010393/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010394 * xmlRelaxNGValidateDocument:
10395 * @ctxt: a Relax-NG validation context
10396 * @doc: the document
10397 *
10398 * Validate the given document
10399 *
10400 * Returns 0 if the validation succeeded or an error code.
10401 */
10402static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010403xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10404{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010405 int ret;
10406 xmlRelaxNGPtr schema;
10407 xmlRelaxNGGrammarPtr grammar;
10408 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010409 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010410
10411 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010412 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010413
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010414 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010415 schema = ctxt->schema;
10416 grammar = schema->topgrammar;
10417 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010418 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10419 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010420 }
10421 state = xmlRelaxNGNewValidState(ctxt, NULL);
10422 ctxt->state = state;
10423 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010424 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010425 state = ctxt->state;
10426 node = state->seq;
10427 node = xmlRelaxNGSkipIgnored(ctxt, node);
10428 if (node != NULL) {
10429 if (ret != -1) {
10430 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10431 ret = -1;
10432 }
10433 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010434 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010435 int i;
10436 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010437
Daniel Veillard4c004142003-10-07 11:33:24 +000010438 for (i = 0; i < ctxt->states->nbState; i++) {
10439 state = ctxt->states->tabState[i];
10440 node = state->seq;
10441 node = xmlRelaxNGSkipIgnored(ctxt, node);
10442 if (node == NULL)
10443 tmp = 0;
10444 xmlRelaxNGFreeValidState(ctxt, state);
10445 }
10446 if (tmp == -1) {
10447 if (ret != -1) {
10448 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10449 ret = -1;
10450 }
10451 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010452 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010453 if (ctxt->state != NULL) {
10454 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010455 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010456 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010457 if (ret != 0)
10458 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010459#ifdef DEBUG
10460 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010461 ctxt->error(ctxt->userData,
10462 "%d Extra error messages left on stack !\n",
10463 ctxt->errNr);
10464 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010465 }
10466#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010467 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010468 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010469
Daniel Veillard4c004142003-10-07 11:33:24 +000010470 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10471 vctxt.valid = 1;
10472 vctxt.error = ctxt->error;
10473 vctxt.warning = ctxt->warning;
10474 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010475
Daniel Veillard4c004142003-10-07 11:33:24 +000010476 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10477 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010478 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010479 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010480 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010481
Daniel Veillard4c004142003-10-07 11:33:24 +000010482 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010483}
10484
Daniel Veillardfd573f12003-03-16 17:52:32 +000010485/************************************************************************
10486 * *
10487 * Validation interfaces *
10488 * *
10489 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010490
Daniel Veillard6eadf632003-01-23 18:29:16 +000010491/**
10492 * xmlRelaxNGNewValidCtxt:
10493 * @schema: a precompiled XML RelaxNGs
10494 *
10495 * Create an XML RelaxNGs validation context based on the given schema
10496 *
10497 * Returns the validation context or NULL in case of error
10498 */
10499xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010500xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10501{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010502 xmlRelaxNGValidCtxtPtr ret;
10503
10504 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10505 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010506 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010507 return (NULL);
10508 }
10509 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10510 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010511 ret->error = xmlGenericError;
10512 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010513 ret->errNr = 0;
10514 ret->errMax = 0;
10515 ret->err = NULL;
10516 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010517 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010518 ret->states = NULL;
10519 ret->freeState = NULL;
10520 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010521 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010522 return (ret);
10523}
10524
10525/**
10526 * xmlRelaxNGFreeValidCtxt:
10527 * @ctxt: the schema validation context
10528 *
10529 * Free the resources associated to the schema validation context
10530 */
10531void
Daniel Veillard4c004142003-10-07 11:33:24 +000010532xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10533{
Daniel Veillard798024a2003-03-19 10:36:09 +000010534 int k;
10535
Daniel Veillard6eadf632003-01-23 18:29:16 +000010536 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010537 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010538 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010539 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010540 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010541 for (k = 0; k < ctxt->freeState->nbState; k++) {
10542 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10543 }
10544 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010545 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010546 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010547 for (k = 0; k < ctxt->freeStatesNr; k++) {
10548 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10549 }
10550 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010551 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010552 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010553 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010554 if (ctxt->elemTab != NULL) {
10555 xmlRegExecCtxtPtr exec;
10556
Daniel Veillard4c004142003-10-07 11:33:24 +000010557 exec = xmlRelaxNGElemPop(ctxt);
10558 while (exec != NULL) {
10559 xmlRegFreeExecCtxt(exec);
10560 exec = xmlRelaxNGElemPop(ctxt);
10561 }
10562 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010563 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010564 xmlFree(ctxt);
10565}
10566
10567/**
10568 * xmlRelaxNGSetValidErrors:
10569 * @ctxt: a Relax-NG validation context
10570 * @err: the error function
10571 * @warn: the warning function
10572 * @ctx: the functions context
10573 *
10574 * Set the error and warning callback informations
10575 */
10576void
10577xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010578 xmlRelaxNGValidityErrorFunc err,
10579 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10580{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010581 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010582 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010583 ctxt->error = err;
10584 ctxt->warning = warn;
10585 ctxt->userData = ctx;
10586}
10587
10588/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010589 * xmlRelaxNGGetValidErrors:
10590 * @ctxt: a Relax-NG validation context
10591 * @err: the error function result
10592 * @warn: the warning function result
10593 * @ctx: the functions context result
10594 *
10595 * Get the error and warning callback informations
10596 *
10597 * Returns -1 in case of error and 0 otherwise
10598 */
10599int
10600xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010601 xmlRelaxNGValidityErrorFunc * err,
10602 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10603{
Daniel Veillard409a8142003-07-18 15:16:57 +000010604 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010605 return (-1);
10606 if (err != NULL)
10607 *err = ctxt->error;
10608 if (warn != NULL)
10609 *warn = ctxt->warning;
10610 if (ctx != NULL)
10611 *ctx = ctxt->userData;
10612 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010613}
10614
10615/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010616 * xmlRelaxNGValidateDoc:
10617 * @ctxt: a Relax-NG validation context
10618 * @doc: a parsed document tree
10619 *
10620 * Validate a document tree in memory.
10621 *
10622 * Returns 0 if the document is valid, a positive error code
10623 * number otherwise and -1 in case of internal or API error.
10624 */
10625int
Daniel Veillard4c004142003-10-07 11:33:24 +000010626xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10627{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010628 int ret;
10629
10630 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010631 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010632
10633 ctxt->doc = doc;
10634
10635 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010636 /*
10637 * TODO: build error codes
10638 */
10639 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010640 return (1);
10641 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010642}
10643
10644#endif /* LIBXML_SCHEMAS_ENABLED */