blob: 6bf62ee31eb03a7554e38fe84ac446a773104a7d [file] [log] [blame]
Daniel Veillard6eadf632003-01-23 18:29:16 +00001/*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
Daniel Veillardd41f4f42003-01-29 21:07:52 +00009/**
10 * TODO:
Daniel Veillardf4b4f982003-02-13 11:02:08 +000011 * - add support for DTD compatibility spec
12 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
Daniel Veillardac297932003-04-17 12:55:35 +000013 * - report better mem allocations pbms at runtime and abort immediately.
Daniel Veillardd41f4f42003-01-29 21:07:52 +000014 */
15
Daniel Veillard6eadf632003-01-23 18:29:16 +000016#define IN_LIBXML
17#include "libxml.h"
18
19#ifdef LIBXML_SCHEMAS_ENABLED
20
21#include <string.h>
22#include <stdio.h>
23#include <libxml/xmlmemory.h>
24#include <libxml/parser.h>
25#include <libxml/parserInternals.h>
26#include <libxml/hash.h>
27#include <libxml/uri.h>
28
29#include <libxml/relaxng.h>
30
31#include <libxml/xmlschemastypes.h>
32#include <libxml/xmlautomata.h>
33#include <libxml/xmlregexp.h>
Daniel Veillardc6e997c2003-01-27 12:35:42 +000034#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000035
36/*
37 * The Relax-NG namespace
38 */
39static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
40 "http://relaxng.org/ns/structure/1.0";
41
42#define IS_RELAXNG(node, type) \
43 ((node != NULL) && (node->ns != NULL) && \
44 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
45 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
46
47
Daniel Veillard952379b2003-03-17 15:37:12 +000048/* #define DEBUG 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000049
Daniel Veillardc482e262003-02-26 14:48:48 +000050/* #define DEBUG_GRAMMAR 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000051
Daniel Veillard71531f32003-02-05 13:19:53 +000052/* #define DEBUG_CONTENT 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000053
Daniel Veillard71531f32003-02-05 13:19:53 +000054/* #define DEBUG_TYPE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000055
Daniel Veillard71531f32003-02-05 13:19:53 +000056/* #define DEBUG_VALID 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000057
Daniel Veillarde5b110b2003-02-04 14:43:39 +000058/* #define DEBUG_INTERLEAVE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000059
Daniel Veillard416589a2003-02-17 17:25:42 +000060/* #define DEBUG_LIST 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000061
Daniel Veillard5add8682003-03-10 13:13:58 +000062/* #define DEBUG_INCLUDE */
Daniel Veillard4c004142003-10-07 11:33:24 +000063
Daniel Veillarda507fbf2003-03-31 16:09:37 +000064/* #define DEBUG_ERROR 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000065
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000066/* #define DEBUG_COMPILE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000067
Daniel Veillardf4e55762003-04-15 23:32:22 +000068/* #define DEBUG_PROGRESSIVE 1 */
Daniel Veillard6eadf632003-01-23 18:29:16 +000069
Daniel Veillard5f1946a2003-03-31 16:38:16 +000070#define MAX_ERROR 5
71
Daniel Veillard6eadf632003-01-23 18:29:16 +000072#define TODO \
73 xmlGenericError(xmlGenericErrorContext, \
74 "Unimplemented block at %s:%d\n", \
75 __FILE__, __LINE__);
76
77typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
78typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
79
80typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
81typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
82
Daniel Veillardd41f4f42003-01-29 21:07:52 +000083typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
84typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
85
Daniel Veillarda9d912d2003-02-01 17:43:10 +000086typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
87typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
88
Daniel Veillard6eadf632003-01-23 18:29:16 +000089typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000090 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
91 XML_RELAXNG_COMBINE_CHOICE, /* choice */
92 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000093} xmlRelaxNGCombine;
94
Daniel Veillard4c5cf702003-02-21 15:40:34 +000095typedef enum {
96 XML_RELAXNG_CONTENT_ERROR = -1,
97 XML_RELAXNG_CONTENT_EMPTY = 0,
98 XML_RELAXNG_CONTENT_SIMPLE,
99 XML_RELAXNG_CONTENT_COMPLEX
100} xmlRelaxNGContentType;
101
Daniel Veillard6eadf632003-01-23 18:29:16 +0000102typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
103typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
104
105struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000106 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
107 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
108 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
109 xmlRelaxNGDefinePtr start; /* <start> content */
110 xmlRelaxNGCombine combine; /* the default combine value */
111 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
112 xmlHashTablePtr defs; /* define* */
113 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000114};
115
116
Daniel Veillard6eadf632003-01-23 18:29:16 +0000117typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000118 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
119 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000120 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000121 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
122 XML_RELAXNG_TEXT, /* textual content */
123 XML_RELAXNG_ELEMENT, /* an element */
124 XML_RELAXNG_DATATYPE, /* extenal data type definition */
125 XML_RELAXNG_PARAM, /* extenal data type parameter */
126 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
127 XML_RELAXNG_LIST, /* a list of patterns */
128 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
129 XML_RELAXNG_DEF, /* a definition */
130 XML_RELAXNG_REF, /* reference to a definition */
131 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
132 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
133 XML_RELAXNG_OPTIONAL, /* optional patterns */
134 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
135 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
136 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
137 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
138 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
139 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000140} xmlRelaxNGType;
141
Daniel Veillard52b48c72003-04-13 19:53:42 +0000142#define IS_NULLABLE (1 << 0)
143#define IS_NOT_NULLABLE (1 << 1)
144#define IS_INDETERMINIST (1 << 2)
145#define IS_MIXED (1 << 3)
146#define IS_TRIABLE (1 << 4)
147#define IS_PROCESSED (1 << 5)
148#define IS_COMPILABLE (1 << 6)
149#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000150
Daniel Veillard6eadf632003-01-23 18:29:16 +0000151struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000152 xmlRelaxNGType type; /* the type of definition */
153 xmlNodePtr node; /* the node in the source */
154 xmlChar *name; /* the element local name if present */
155 xmlChar *ns; /* the namespace local name if present */
156 xmlChar *value; /* value when available */
157 void *data; /* data lib or specific pointer */
158 xmlRelaxNGDefinePtr content; /* the expected content */
159 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
160 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
161 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
162 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
163 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
164 short depth; /* used for the cycle detection */
165 short dflags; /* define related flags */
166 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000167};
168
169/**
170 * _xmlRelaxNG:
171 *
172 * A RelaxNGs definition
173 */
174struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000175 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000176 xmlRelaxNGGrammarPtr topgrammar;
177 xmlDocPtr doc;
178
Daniel Veillard4c004142003-10-07 11:33:24 +0000179 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000180
Daniel Veillard4c004142003-10-07 11:33:24 +0000181 xmlHashTablePtr defs; /* define */
182 xmlHashTablePtr refs; /* references */
183 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
184 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
185 int defNr; /* number of defines used */
186 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000187
Daniel Veillard6eadf632003-01-23 18:29:16 +0000188};
189
Daniel Veillard77648bb2003-02-20 15:03:22 +0000190#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
191#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
192#define XML_RELAXNG_IN_LIST (1 << 2)
193#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
194#define XML_RELAXNG_IN_START (1 << 4)
195#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
196#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
197#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000198#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
199#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000200
201struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000202 void *userData; /* user specific data block */
203 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
204 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000205 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000206 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000207
Daniel Veillard4c004142003-10-07 11:33:24 +0000208 xmlRelaxNGPtr schema; /* The schema in use */
209 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
210 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
211 int flags; /* parser flags */
212 int nbErrors; /* number of errors at parse time */
213 int nbWarnings; /* number of warnings at parse time */
214 const xmlChar *define; /* the current define scope */
215 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000216
Daniel Veillard4c004142003-10-07 11:33:24 +0000217 int nbInterleaves;
218 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000219
Daniel Veillard4c004142003-10-07 11:33:24 +0000220 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
221 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
222 xmlChar *URL;
223 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000224
Daniel Veillard4c004142003-10-07 11:33:24 +0000225 int defNr; /* number of defines used */
226 int defMax; /* number of defines aloocated */
227 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000228
Daniel Veillard4c004142003-10-07 11:33:24 +0000229 const char *buffer;
230 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000231
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000232 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000233 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
234 int docNr; /* Depth of the parsing stack */
235 int docMax; /* Max depth of the parsing stack */
236 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000237
238 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000239 xmlRelaxNGIncludePtr inc; /* Current parsed include */
240 int incNr; /* Depth of the include parsing stack */
241 int incMax; /* Max depth of the parsing stack */
242 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000243
Daniel Veillard4c004142003-10-07 11:33:24 +0000244 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000245
246 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000247 xmlAutomataPtr am; /* the automata */
248 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000249
250 int crng; /* compact syntax and other flags */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000251};
252
253#define FLAGS_IGNORABLE 1
254#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000255#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000256
257/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000258 * xmlRelaxNGInterleaveGroup:
259 *
260 * A RelaxNGs partition set associated to lists of definitions
261 */
262typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
263typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
264struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000265 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
266 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
267 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000268};
269
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000270#define IS_DETERMINIST 1
271#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000272
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000273/**
274 * xmlRelaxNGPartitions:
275 *
276 * A RelaxNGs partition associated to an interleave group
277 */
278typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
279typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
280struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000281 int nbgroups; /* number of groups in the partitions */
282 xmlHashTablePtr triage; /* hash table used to direct nodes to the
283 * right group when possible */
284 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000285 xmlRelaxNGInterleaveGroupPtr *groups;
286};
287
288/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000289 * xmlRelaxNGValidState:
290 *
291 * A RelaxNGs validation state
292 */
293#define MAX_ATTR 20
294typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
295typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
296struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000297 xmlNodePtr node; /* the current node */
298 xmlNodePtr seq; /* the sequence of children left to validate */
299 int nbAttrs; /* the number of attributes */
300 int maxAttrs; /* the size of attrs */
301 int nbAttrLeft; /* the number of attributes left to validate */
302 xmlChar *value; /* the value when operating on string */
303 xmlChar *endvalue; /* the end value when operating on string */
304 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000305};
306
307/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000308 * xmlRelaxNGStates:
309 *
310 * A RelaxNGs container for validation state
311 */
312typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
313typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
314struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000315 int nbState; /* the number of states */
316 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000317 xmlRelaxNGValidStatePtr *tabState;
318};
319
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000320#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000321
Daniel Veillardfd573f12003-03-16 17:52:32 +0000322/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000323 * xmlRelaxNGValidError:
324 *
325 * A RelaxNGs validation error
326 */
327typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
328typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
329struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000330 xmlRelaxNGValidErr err; /* the error number */
331 int flags; /* flags */
332 xmlNodePtr node; /* the current node */
333 xmlNodePtr seq; /* the current child */
334 const xmlChar *arg1; /* first arg */
335 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000336};
337
338/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000339 * xmlRelaxNGValidCtxt:
340 *
341 * A RelaxNGs validation context
342 */
343
344struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000345 void *userData; /* user specific data block */
346 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
347 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000348 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000349 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000350
Daniel Veillard4c004142003-10-07 11:33:24 +0000351 xmlRelaxNGPtr schema; /* The schema in use */
352 xmlDocPtr doc; /* the document being validated */
353 int flags; /* validation flags */
354 int depth; /* validation depth */
355 int idref; /* requires idref checking */
356 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000357
358 /*
359 * Errors accumulated in branches may have to be stacked to be
360 * provided back when it's sure they affect validation.
361 */
362 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000363 int errNr; /* Depth of the error stack */
364 int errMax; /* Max depth of the error stack */
365 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000366
Daniel Veillard4c004142003-10-07 11:33:24 +0000367 xmlRelaxNGValidStatePtr state; /* the current validation state */
368 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000369
Daniel Veillard4c004142003-10-07 11:33:24 +0000370 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
371 int freeStatesNr;
372 int freeStatesMax;
373 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000374
375 /*
376 * This is used for "progressive" validation
377 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000378 xmlRegExecCtxtPtr elem; /* the current element regexp */
379 int elemNr; /* the number of element validated */
380 int elemMax; /* the max depth of elements */
381 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
382 int pstate; /* progressive state */
383 xmlNodePtr pnode; /* the current node */
384 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
385 int perr; /* signal error in content model
386 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000387};
388
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000389/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000390 * xmlRelaxNGInclude:
391 *
392 * Structure associated to a RelaxNGs document element
393 */
394struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000395 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
396 xmlChar *href; /* the normalized href value */
397 xmlDocPtr doc; /* the associated XML document */
398 xmlRelaxNGDefinePtr content; /* the definitions */
399 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000400};
401
402/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000403 * xmlRelaxNGDocument:
404 *
405 * Structure associated to a RelaxNGs document element
406 */
407struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000408 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000409 xmlChar *href; /* the normalized href value */
410 xmlDocPtr doc; /* the associated XML document */
411 xmlRelaxNGDefinePtr content; /* the definitions */
412 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000413};
414
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000415
Daniel Veillard6eadf632003-01-23 18:29:16 +0000416/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000417 * *
418 * Some factorized error routines *
419 * *
420 ************************************************************************/
421
422/**
423 * xmlRngPErrMemory:
424 * @ctxt: an Relax-NG parser context
425 * @extra: extra informations
426 *
427 * Handle a redefinition of attribute error
428 */
429static void
430xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
431{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000432 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000433 xmlGenericErrorFunc channel = NULL;
434 void *data = NULL;
435
436 if (ctxt != NULL) {
437 channel = ctxt->error;
438 data = ctxt->userData;
439 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000440 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000441 }
442 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000443 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000444 NULL, NULL, XML_FROM_RELAXNGP,
445 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
446 NULL, NULL, 0, 0,
447 "Memory allocation failed : %s\n", extra);
448 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000449 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000450 NULL, NULL, XML_FROM_RELAXNGP,
451 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
452 NULL, NULL, 0, 0, "Memory allocation failed\n");
453}
454
455/**
456 * xmlRngVErrMemory:
457 * @ctxt: a Relax-NG validation context
458 * @extra: extra informations
459 *
460 * Handle a redefinition of attribute error
461 */
462static void
463xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
464{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000465 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000466 xmlGenericErrorFunc channel = NULL;
467 void *data = NULL;
468
469 if (ctxt != NULL) {
470 channel = ctxt->error;
471 data = ctxt->userData;
472 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000473 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000474 }
475 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000476 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000477 NULL, NULL, XML_FROM_RELAXNGV,
478 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
479 NULL, NULL, 0, 0,
480 "Memory allocation failed : %s\n", extra);
481 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000482 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000483 NULL, NULL, XML_FROM_RELAXNGV,
484 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
485 NULL, NULL, 0, 0, "Memory allocation failed\n");
486}
487
488/**
489 * xmlRngPErr:
490 * @ctxt: a Relax-NG parser context
491 * @node: the node raising the error
492 * @error: the error code
493 * @msg: message
494 * @str1: extra info
495 * @str2: extra info
496 *
497 * Handle a Relax NG Parsing error
498 */
499static void
500xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
501 const char *msg, const xmlChar * str1, const xmlChar * str2)
502{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000503 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000504 xmlGenericErrorFunc channel = NULL;
505 void *data = NULL;
506
507 if (ctxt != NULL) {
508 channel = ctxt->error;
509 data = ctxt->userData;
510 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000511 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000512 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000513 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000514 NULL, node, XML_FROM_RELAXNGP,
515 error, XML_ERR_ERROR, NULL, 0,
516 (const char *) str1, (const char *) str2, NULL, 0, 0,
517 msg, str1, str2);
518}
519
520/**
521 * xmlRngVErr:
522 * @ctxt: a Relax-NG validation context
523 * @node: the node raising the error
524 * @error: the error code
525 * @msg: message
526 * @str1: extra info
527 * @str2: extra info
528 *
529 * Handle a Relax NG Validation error
530 */
531static void
532xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
533 const char *msg, const xmlChar * str1, const xmlChar * str2)
534{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000535 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000536 xmlGenericErrorFunc channel = NULL;
537 void *data = NULL;
538
539 if (ctxt != NULL) {
540 channel = ctxt->error;
541 data = ctxt->userData;
542 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000543 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000544 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000545 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000546 NULL, node, XML_FROM_RELAXNGV,
547 error, XML_ERR_ERROR, NULL, 0,
548 (const char *) str1, (const char *) str2, NULL, 0, 0,
549 msg, str1, str2);
550}
551
552/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000553 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000554 * Preliminary type checking interfaces *
555 * *
556 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000557
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000558/**
559 * xmlRelaxNGTypeHave:
560 * @data: data needed for the library
561 * @type: the type name
562 * @value: the value to check
563 *
564 * Function provided by a type library to check if a type is exported
565 *
566 * Returns 1 if yes, 0 if no and -1 in case of error.
567 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000568typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000569
570/**
571 * xmlRelaxNGTypeCheck:
572 * @data: data needed for the library
573 * @type: the type name
574 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000575 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000576 *
577 * Function provided by a type library to check if a value match a type
578 *
579 * Returns 1 if yes, 0 if no and -1 in case of error.
580 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000581typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
582 const xmlChar * value, void **result,
583 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000584
585/**
586 * xmlRelaxNGFacetCheck:
587 * @data: data needed for the library
588 * @type: the type name
589 * @facet: the facet name
590 * @val: the facet value
591 * @strval: the string value
592 * @value: the value to check
593 *
594 * Function provided by a type library to check a value facet
595 *
596 * Returns 1 if yes, 0 if no and -1 in case of error.
597 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000598typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
599 const xmlChar * facet,
600 const xmlChar * val,
601 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000602
603/**
604 * xmlRelaxNGTypeFree:
605 * @data: data needed for the library
606 * @result: the value to free
607 *
608 * Function provided by a type library to free a returned result
609 */
610typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000611
612/**
613 * xmlRelaxNGTypeCompare:
614 * @data: data needed for the library
615 * @type: the type name
616 * @value1: the first value
617 * @value2: the second value
618 *
619 * Function provided by a type library to compare two values accordingly
620 * to a type.
621 *
622 * Returns 1 if yes, 0 if no and -1 in case of error.
623 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000624typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
625 const xmlChar * value1,
626 xmlNodePtr ctxt1,
627 void *comp1,
628 const xmlChar * value2,
629 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000630typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
631typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
632struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000633 const xmlChar *namespace; /* the datatypeLibrary value */
634 void *data; /* data needed for the library */
635 xmlRelaxNGTypeHave have; /* the export function */
636 xmlRelaxNGTypeCheck check; /* the checking function */
637 xmlRelaxNGTypeCompare comp; /* the compare function */
638 xmlRelaxNGFacetCheck facet; /* the facet check function */
639 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000640};
641
642/************************************************************************
643 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000644 * Allocation functions *
645 * *
646 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000647static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
648static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000649static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000650static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000651static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
652 ATTRIBUTE_UNUSED,
653 xmlRelaxNGValidStatePtr state1,
654 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000655static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000656 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000657
658/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000659 * xmlRelaxNGFreeDocument:
660 * @docu: a document structure
661 *
662 * Deallocate a RelaxNG document structure.
663 */
664static void
665xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
666{
667 if (docu == NULL)
668 return;
669
670 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000671 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000672 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000673 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000674 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000675 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000676 xmlFree(docu);
677}
678
679/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000680 * xmlRelaxNGFreeDocumentList:
681 * @docu: a list of document structure
682 *
683 * Deallocate a RelaxNG document structures.
684 */
685static void
686xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
687{
688 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000689
Daniel Veillardc482e262003-02-26 14:48:48 +0000690 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000691 next = docu->next;
692 xmlRelaxNGFreeDocument(docu);
693 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000694 }
695}
696
697/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000698 * xmlRelaxNGFreeInclude:
699 * @incl: a include structure
700 *
701 * Deallocate a RelaxNG include structure.
702 */
703static void
704xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
705{
706 if (incl == NULL)
707 return;
708
709 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000710 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000711 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000712 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000713 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000714 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000715 xmlFree(incl);
716}
717
718/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000719 * xmlRelaxNGFreeIncludeList:
720 * @incl: a include structure list
721 *
722 * Deallocate a RelaxNG include structure.
723 */
724static void
725xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
726{
727 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000728
Daniel Veillardc482e262003-02-26 14:48:48 +0000729 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000730 next = incl->next;
731 xmlRelaxNGFreeInclude(incl);
732 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000733 }
734}
735
736/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000737 * xmlRelaxNGNewRelaxNG:
738 * @ctxt: a Relax-NG validation context (optional)
739 *
740 * Allocate a new RelaxNG structure.
741 *
742 * Returns the newly allocated structure or NULL in case or error
743 */
744static xmlRelaxNGPtr
745xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
746{
747 xmlRelaxNGPtr ret;
748
749 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
750 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000751 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000752 return (NULL);
753 }
754 memset(ret, 0, sizeof(xmlRelaxNG));
755
756 return (ret);
757}
758
759/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000760 * xmlRelaxNGFreeInnerSchema:
761 * @schema: a schema structure
762 *
763 * Deallocate a RelaxNG schema structure.
764 */
765static void
766xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
767{
768 if (schema == NULL)
769 return;
770
771 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000772 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000773 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000774 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000775
Daniel Veillard4c004142003-10-07 11:33:24 +0000776 for (i = 0; i < schema->defNr; i++)
777 xmlRelaxNGFreeDefine(schema->defTab[i]);
778 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000779 }
780
781 xmlFree(schema);
782}
783
784/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000785 * xmlRelaxNGFree:
786 * @schema: a schema structure
787 *
788 * Deallocate a RelaxNG structure.
789 */
790void
791xmlRelaxNGFree(xmlRelaxNGPtr schema)
792{
793 if (schema == NULL)
794 return;
795
Daniel Veillard6eadf632003-01-23 18:29:16 +0000796 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000797 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000798 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000799 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000800 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000801 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000802 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000803 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000804 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000805 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000806
Daniel Veillard4c004142003-10-07 11:33:24 +0000807 for (i = 0; i < schema->defNr; i++)
808 xmlRelaxNGFreeDefine(schema->defTab[i]);
809 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000810 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000811
812 xmlFree(schema);
813}
814
815/**
816 * xmlRelaxNGNewGrammar:
817 * @ctxt: a Relax-NG validation context (optional)
818 *
819 * Allocate a new RelaxNG grammar.
820 *
821 * Returns the newly allocated structure or NULL in case or error
822 */
823static xmlRelaxNGGrammarPtr
824xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
825{
826 xmlRelaxNGGrammarPtr ret;
827
828 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
829 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000830 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000831 return (NULL);
832 }
833 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
834
835 return (ret);
836}
837
838/**
839 * xmlRelaxNGFreeGrammar:
840 * @grammar: a grammar structure
841 *
842 * Deallocate a RelaxNG grammar structure.
843 */
844static void
845xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
846{
847 if (grammar == NULL)
848 return;
849
Daniel Veillardc482e262003-02-26 14:48:48 +0000850 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000851 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000852 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000853 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000854 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000855 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000856 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000857 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000858 }
859 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000860 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000861 }
862
863 xmlFree(grammar);
864}
865
866/**
867 * xmlRelaxNGNewDefine:
868 * @ctxt: a Relax-NG validation context
869 * @node: the node in the input document.
870 *
871 * Allocate a new RelaxNG define.
872 *
873 * Returns the newly allocated structure or NULL in case or error
874 */
875static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000876xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000877{
878 xmlRelaxNGDefinePtr ret;
879
Daniel Veillard419a7682003-02-03 23:22:49 +0000880 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000881 ctxt->defMax = 16;
882 ctxt->defNr = 0;
883 ctxt->defTab = (xmlRelaxNGDefinePtr *)
884 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
885 if (ctxt->defTab == NULL) {
886 xmlRngPErrMemory(ctxt, "allocating define\n");
887 return (NULL);
888 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000889 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000890 xmlRelaxNGDefinePtr *tmp;
891
892 ctxt->defMax *= 2;
893 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
894 ctxt->defMax *
895 sizeof
896 (xmlRelaxNGDefinePtr));
897 if (tmp == NULL) {
898 xmlRngPErrMemory(ctxt, "allocating define\n");
899 return (NULL);
900 }
901 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000902 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000903 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
904 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000905 xmlRngPErrMemory(ctxt, "allocating define\n");
906 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000907 }
908 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000909 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000910 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000911 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000912 return (ret);
913}
914
915/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000916 * xmlRelaxNGFreePartition:
917 * @partitions: a partition set structure
918 *
919 * Deallocate RelaxNG partition set structures.
920 */
921static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000922xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
923{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000924 xmlRelaxNGInterleaveGroupPtr group;
925 int j;
926
927 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000928 if (partitions->groups != NULL) {
929 for (j = 0; j < partitions->nbgroups; j++) {
930 group = partitions->groups[j];
931 if (group != NULL) {
932 if (group->defs != NULL)
933 xmlFree(group->defs);
934 if (group->attrs != NULL)
935 xmlFree(group->attrs);
936 xmlFree(group);
937 }
938 }
939 xmlFree(partitions->groups);
940 }
941 if (partitions->triage != NULL) {
942 xmlHashFree(partitions->triage, NULL);
943 }
944 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000945 }
946}
Daniel Veillard4c004142003-10-07 11:33:24 +0000947
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000948/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000949 * xmlRelaxNGFreeDefine:
950 * @define: a define structure
951 *
952 * Deallocate a RelaxNG define structure.
953 */
954static void
955xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
956{
957 if (define == NULL)
958 return;
959
Daniel Veillard4c004142003-10-07 11:33:24 +0000960 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
961 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000962
Daniel Veillard4c004142003-10-07 11:33:24 +0000963 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
964 if ((lib != NULL) && (lib->freef != NULL))
965 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000966 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000967 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
968 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
969 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
970 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000971 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000972 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000973 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000974 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000975 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000976 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000977 if (define->contModel != NULL)
978 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000979 xmlFree(define);
980}
981
982/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000983 * xmlRelaxNGNewStates:
984 * @ctxt: a Relax-NG validation context
985 * @size: the default size for the container
986 *
987 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000988 *
989 * Returns the newly allocated structure or NULL in case or error
990 */
991static xmlRelaxNGStatesPtr
992xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
993{
994 xmlRelaxNGStatesPtr ret;
995
Daniel Veillard798024a2003-03-19 10:36:09 +0000996 if ((ctxt != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +0000997 (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) {
998 ctxt->freeStatesNr--;
999 ret = ctxt->freeStates[ctxt->freeStatesNr];
1000 ret->nbState = 0;
1001 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001002 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001003 if (size < 16)
1004 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001005
1006 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001007 (size -
1008 1) *
1009 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001010 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001011 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001012 return (NULL);
1013 }
1014 ret->nbState = 0;
1015 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001016 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1017 sizeof
1018 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001019 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001020 xmlRngVErrMemory(ctxt, "allocating states\n");
1021 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001022 return (NULL);
1023 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001024 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001025}
1026
1027/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001028 * xmlRelaxNGAddStateUniq:
1029 * @ctxt: a Relax-NG validation context
1030 * @states: the states container
1031 * @state: the validation state
1032 *
1033 * Add a RelaxNG validation state to the container without checking
1034 * for unicity.
1035 *
1036 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1037 */
1038static int
1039xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001040 xmlRelaxNGStatesPtr states,
1041 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001042{
1043 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001044 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001045 }
1046 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001047 xmlRelaxNGValidStatePtr *tmp;
1048 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001049
Daniel Veillard4c004142003-10-07 11:33:24 +00001050 size = states->maxState * 2;
1051 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1052 (size) *
1053 sizeof
1054 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001055 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001056 xmlRngVErrMemory(ctxt, "adding states\n");
1057 return (-1);
1058 }
1059 states->tabState = tmp;
1060 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001061 }
1062 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001063 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001064}
1065
1066/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001067 * xmlRelaxNGAddState:
1068 * @ctxt: a Relax-NG validation context
1069 * @states: the states container
1070 * @state: the validation state
1071 *
1072 * Add a RelaxNG validation state to the container
1073 *
1074 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1075 */
1076static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001077xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1078 xmlRelaxNGStatesPtr states,
1079 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001080{
1081 int i;
1082
1083 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001084 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001085 }
1086 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001087 xmlRelaxNGValidStatePtr *tmp;
1088 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001089
Daniel Veillard4c004142003-10-07 11:33:24 +00001090 size = states->maxState * 2;
1091 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1092 (size) *
1093 sizeof
1094 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001095 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001096 xmlRngVErrMemory(ctxt, "adding states\n");
1097 return (-1);
1098 }
1099 states->tabState = tmp;
1100 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001101 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001102 for (i = 0; i < states->nbState; i++) {
1103 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1104 xmlRelaxNGFreeValidState(ctxt, state);
1105 return (0);
1106 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001107 }
1108 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001109 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001110}
1111
1112/**
1113 * xmlRelaxNGFreeStates:
1114 * @ctxt: a Relax-NG validation context
1115 * @states: teh container
1116 *
1117 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001118 */
1119static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001120xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001121 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001122{
Daniel Veillard798024a2003-03-19 10:36:09 +00001123 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001124 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001125 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001126 ctxt->freeStatesMax = 40;
1127 ctxt->freeStatesNr = 0;
1128 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1129 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1130 if (ctxt->freeStates == NULL) {
1131 xmlRngVErrMemory(ctxt, "storing states\n");
1132 }
1133 } else if ((ctxt != NULL)
1134 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1135 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001136
Daniel Veillard4c004142003-10-07 11:33:24 +00001137 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1138 2 * ctxt->freeStatesMax *
1139 sizeof
1140 (xmlRelaxNGStatesPtr));
1141 if (tmp == NULL) {
1142 xmlRngVErrMemory(ctxt, "storing states\n");
1143 xmlFree(states->tabState);
1144 xmlFree(states);
1145 return;
1146 }
1147 ctxt->freeStates = tmp;
1148 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001149 }
1150 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001151 xmlFree(states->tabState);
1152 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001153 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001154 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001155 }
1156}
1157
1158/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001159 * xmlRelaxNGNewValidState:
1160 * @ctxt: a Relax-NG validation context
1161 * @node: the current node or NULL for the document
1162 *
1163 * Allocate a new RelaxNG validation state
1164 *
1165 * Returns the newly allocated structure or NULL in case or error
1166 */
1167static xmlRelaxNGValidStatePtr
1168xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1169{
1170 xmlRelaxNGValidStatePtr ret;
1171 xmlAttrPtr attr;
1172 xmlAttrPtr attrs[MAX_ATTR];
1173 int nbAttrs = 0;
1174 xmlNodePtr root = NULL;
1175
1176 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001177 root = xmlDocGetRootElement(ctxt->doc);
1178 if (root == NULL)
1179 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001180 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001181 attr = node->properties;
1182 while (attr != NULL) {
1183 if (nbAttrs < MAX_ATTR)
1184 attrs[nbAttrs++] = attr;
1185 else
1186 nbAttrs++;
1187 attr = attr->next;
1188 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001189 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001190 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1191 ctxt->freeState->nbState--;
1192 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001193 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001194 ret =
1195 (xmlRelaxNGValidStatePtr)
1196 xmlMalloc(sizeof(xmlRelaxNGValidState));
1197 if (ret == NULL) {
1198 xmlRngVErrMemory(ctxt, "allocating states\n");
1199 return (NULL);
1200 }
1201 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001202 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001203 ret->value = NULL;
1204 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001205 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001206 ret->node = (xmlNodePtr) ctxt->doc;
1207 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001208 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001209 ret->node = node;
1210 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001211 }
1212 ret->nbAttrs = 0;
1213 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001214 if (ret->attrs == NULL) {
1215 if (nbAttrs < 4)
1216 ret->maxAttrs = 4;
1217 else
1218 ret->maxAttrs = nbAttrs;
1219 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1220 sizeof(xmlAttrPtr));
1221 if (ret->attrs == NULL) {
1222 xmlRngVErrMemory(ctxt, "allocating states\n");
1223 return (ret);
1224 }
1225 } else if (ret->maxAttrs < nbAttrs) {
1226 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001227
Daniel Veillard4c004142003-10-07 11:33:24 +00001228 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1229 sizeof(xmlAttrPtr));
1230 if (tmp == NULL) {
1231 xmlRngVErrMemory(ctxt, "allocating states\n");
1232 return (ret);
1233 }
1234 ret->attrs = tmp;
1235 ret->maxAttrs = nbAttrs;
1236 }
1237 ret->nbAttrs = nbAttrs;
1238 if (nbAttrs < MAX_ATTR) {
1239 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1240 } else {
1241 attr = node->properties;
1242 nbAttrs = 0;
1243 while (attr != NULL) {
1244 ret->attrs[nbAttrs++] = attr;
1245 attr = attr->next;
1246 }
1247 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001248 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001249 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001250 return (ret);
1251}
1252
1253/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001254 * xmlRelaxNGCopyValidState:
1255 * @ctxt: a Relax-NG validation context
1256 * @state: a validation state
1257 *
1258 * Copy the validation state
1259 *
1260 * Returns the newly allocated structure or NULL in case or error
1261 */
1262static xmlRelaxNGValidStatePtr
1263xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001264 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001265{
1266 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001267 unsigned int maxAttrs;
1268 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001269
1270 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001271 return (NULL);
1272 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1273 ctxt->freeState->nbState--;
1274 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001275 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001276 ret =
1277 (xmlRelaxNGValidStatePtr)
1278 xmlMalloc(sizeof(xmlRelaxNGValidState));
1279 if (ret == NULL) {
1280 xmlRngVErrMemory(ctxt, "allocating states\n");
1281 return (NULL);
1282 }
1283 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001284 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001285 attrs = ret->attrs;
1286 maxAttrs = ret->maxAttrs;
1287 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1288 ret->attrs = attrs;
1289 ret->maxAttrs = maxAttrs;
1290 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001291 if (ret->attrs == NULL) {
1292 ret->maxAttrs = state->maxAttrs;
1293 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1294 sizeof(xmlAttrPtr));
1295 if (ret->attrs == NULL) {
1296 xmlRngVErrMemory(ctxt, "allocating states\n");
1297 ret->nbAttrs = 0;
1298 return (ret);
1299 }
1300 } else if (ret->maxAttrs < state->nbAttrs) {
1301 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001302
Daniel Veillard4c004142003-10-07 11:33:24 +00001303 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1304 sizeof(xmlAttrPtr));
1305 if (tmp == NULL) {
1306 xmlRngVErrMemory(ctxt, "allocating states\n");
1307 ret->nbAttrs = 0;
1308 return (ret);
1309 }
1310 ret->maxAttrs = state->maxAttrs;
1311 ret->attrs = tmp;
1312 }
1313 memcpy(ret->attrs, state->attrs,
1314 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001315 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001316 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001317}
1318
1319/**
1320 * xmlRelaxNGEqualValidState:
1321 * @ctxt: a Relax-NG validation context
1322 * @state1: a validation state
1323 * @state2: a validation state
1324 *
1325 * Compare the validation states for equality
1326 *
1327 * Returns 1 if equald, 0 otherwise
1328 */
1329static int
1330xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001331 xmlRelaxNGValidStatePtr state1,
1332 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001333{
1334 int i;
1335
1336 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001337 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001338 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001339 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001340 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001341 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001342 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001343 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001344 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001345 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001346 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001347 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001348 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001349 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001350 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001351 (!xmlStrEqual(state1->value, state2->value)))
1352 return (0);
1353 for (i = 0; i < state1->nbAttrs; i++) {
1354 if (state1->attrs[i] != state2->attrs[i])
1355 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001356 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001357 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001358}
1359
1360/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001361 * xmlRelaxNGFreeValidState:
1362 * @state: a validation state structure
1363 *
1364 * Deallocate a RelaxNG validation state structure.
1365 */
1366static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001367xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001368 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001369{
1370 if (state == NULL)
1371 return;
1372
Daniel Veillard798024a2003-03-19 10:36:09 +00001373 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001374 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001375 }
1376 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001377 if (state->attrs != NULL)
1378 xmlFree(state->attrs);
1379 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001380 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001381 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001382 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001383}
1384
1385/************************************************************************
1386 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001387 * Semi internal functions *
1388 * *
1389 ************************************************************************/
1390
1391/**
1392 * xmlRelaxParserSetFlag:
1393 * @ctxt: a RelaxNG parser context
1394 * @flags: a set of flags values
1395 *
1396 * Semi private function used to pass informations to a parser context
1397 * which are a combination of xmlRelaxNGParserFlag .
1398 *
1399 * Returns 0 if success and -1 in case of error
1400 */
1401int
1402xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1403{
1404 if (ctxt == NULL) return(-1);
1405 if (flags & XML_RELAXNGP_FREE_DOC) {
1406 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1407 flags -= XML_RELAXNGP_FREE_DOC;
1408 }
1409 if (flags & XML_RELAXNGP_CRNG) {
1410 ctxt->crng |= XML_RELAXNGP_CRNG;
1411 flags -= XML_RELAXNGP_CRNG;
1412 }
1413 if (flags != 0) return(-1);
1414 return(0);
1415}
1416
1417/************************************************************************
1418 * *
1419 * Document functions *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001420 * *
1421 ************************************************************************/
1422static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001423 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001424
1425/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001426 * xmlRelaxNGIncludePush:
1427 * @ctxt: the parser context
1428 * @value: the element doc
1429 *
1430 * Pushes a new include on top of the include stack
1431 *
1432 * Returns 0 in case of error, the index in the stack otherwise
1433 */
1434static int
1435xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001436 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001437{
1438 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001439 ctxt->incMax = 4;
1440 ctxt->incNr = 0;
1441 ctxt->incTab =
1442 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1443 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001444 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001445 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001446 return (0);
1447 }
1448 }
1449 if (ctxt->incNr >= ctxt->incMax) {
1450 ctxt->incMax *= 2;
1451 ctxt->incTab =
1452 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001453 ctxt->incMax *
1454 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001455 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001456 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001457 return (0);
1458 }
1459 }
1460 ctxt->incTab[ctxt->incNr] = value;
1461 ctxt->inc = value;
1462 return (ctxt->incNr++);
1463}
1464
1465/**
1466 * xmlRelaxNGIncludePop:
1467 * @ctxt: the parser context
1468 *
1469 * Pops the top include from the include stack
1470 *
1471 * Returns the include just removed
1472 */
1473static xmlRelaxNGIncludePtr
1474xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1475{
1476 xmlRelaxNGIncludePtr ret;
1477
1478 if (ctxt->incNr <= 0)
1479 return (0);
1480 ctxt->incNr--;
1481 if (ctxt->incNr > 0)
1482 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1483 else
1484 ctxt->inc = NULL;
1485 ret = ctxt->incTab[ctxt->incNr];
1486 ctxt->incTab[ctxt->incNr] = 0;
1487 return (ret);
1488}
1489
1490/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001491 * xmlRelaxNGRemoveRedefine:
1492 * @ctxt: the parser context
1493 * @URL: the normalized URL
1494 * @target: the included target
1495 * @name: the define name to eliminate
1496 *
1497 * Applies the elimination algorithm of 4.7
1498 *
1499 * Returns 0 in case of error, 1 in case of success.
1500 */
1501static int
1502xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001503 const xmlChar * URL ATTRIBUTE_UNUSED,
1504 xmlNodePtr target, const xmlChar * name)
1505{
Daniel Veillard5add8682003-03-10 13:13:58 +00001506 int found = 0;
1507 xmlNodePtr tmp, tmp2;
1508 xmlChar *name2;
1509
1510#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001511 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001512 xmlGenericError(xmlGenericErrorContext,
1513 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001514 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001515 xmlGenericError(xmlGenericErrorContext,
1516 "Elimination of <include> define %s from %s\n",
1517 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001518#endif
1519 tmp = target;
1520 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001521 tmp2 = tmp->next;
1522 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1523 found = 1;
1524 xmlUnlinkNode(tmp);
1525 xmlFreeNode(tmp);
1526 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1527 name2 = xmlGetProp(tmp, BAD_CAST "name");
1528 xmlRelaxNGNormExtSpace(name2);
1529 if (name2 != NULL) {
1530 if (xmlStrEqual(name, name2)) {
1531 found = 1;
1532 xmlUnlinkNode(tmp);
1533 xmlFreeNode(tmp);
1534 }
1535 xmlFree(name2);
1536 }
1537 } else if (IS_RELAXNG(tmp, "include")) {
1538 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001539 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001540
Daniel Veillard4c004142003-10-07 11:33:24 +00001541 if ((inc != NULL) && (inc->doc != NULL) &&
1542 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001543
Daniel Veillard4c004142003-10-07 11:33:24 +00001544 if (xmlStrEqual
1545 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001546#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001547 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001548#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001549 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1550 inc->doc->children->
1551 children, name) == 1) {
1552 found = 1;
1553 }
1554 if (href != NULL)
1555 xmlFree(href);
1556 }
1557 }
1558 }
1559 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001560 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001561 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001562}
1563
1564/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001565 * xmlRelaxNGLoadInclude:
1566 * @ctxt: the parser context
1567 * @URL: the normalized URL
1568 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001569 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001570 *
1571 * First lookup if the document is already loaded into the parser context,
1572 * check against recursion. If not found the resource is loaded and
1573 * the content is preprocessed before being returned back to the caller.
1574 *
1575 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1576 */
1577static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001578xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1579 xmlNodePtr node, const xmlChar * ns)
1580{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001581 xmlRelaxNGIncludePtr ret = NULL;
1582 xmlDocPtr doc;
1583 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001584 xmlNodePtr root, cur;
1585
1586#ifdef DEBUG_INCLUDE
1587 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001588 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001589#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001590
1591 /*
1592 * check against recursion in the stack
1593 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001594 for (i = 0; i < ctxt->incNr; i++) {
1595 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1596 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1597 "Detected an Include recursion for %s\n", URL,
1598 NULL);
1599 return (NULL);
1600 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001601 }
1602
1603 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001604 * load the document
1605 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001606 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001607 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001608 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1609 "xmlRelaxNG: could not load %s\n", URL, NULL);
1610 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001611 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001612#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001613 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001614#endif
1615
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001616 /*
1617 * Allocate the document structures and register it first.
1618 */
1619 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1620 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001621 xmlRngPErrMemory(ctxt, "allocating include\n");
1622 xmlFreeDoc(doc);
1623 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001624 }
1625 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1626 ret->doc = doc;
1627 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001628 ret->next = ctxt->includes;
1629 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001630
1631 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001632 * transmit the ns if needed
1633 */
1634 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001635 root = xmlDocGetRootElement(doc);
1636 if (root != NULL) {
1637 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1638 xmlSetProp(root, BAD_CAST "ns", ns);
1639 }
1640 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001641 }
1642
1643 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001644 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001645 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001646 xmlRelaxNGIncludePush(ctxt, ret);
1647
1648 /*
1649 * Some preprocessing of the document content, this include recursing
1650 * in the include stack.
1651 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001652#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001653 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001654#endif
1655
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001656 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1657 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001658 ctxt->inc = NULL;
1659 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001660 }
1661
1662 /*
1663 * Pop up the include from the stack
1664 */
1665 xmlRelaxNGIncludePop(ctxt);
1666
Daniel Veillard5add8682003-03-10 13:13:58 +00001667#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001668 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001669#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001670 /*
1671 * Check that the top element is a grammar
1672 */
1673 root = xmlDocGetRootElement(doc);
1674 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001675 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1676 "xmlRelaxNG: included document is empty %s\n", URL,
1677 NULL);
1678 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001679 }
1680 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001681 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1682 "xmlRelaxNG: included document %s root is not a grammar\n",
1683 URL, NULL);
1684 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001685 }
1686
1687 /*
1688 * Elimination of redefined rules in the include.
1689 */
1690 cur = node->children;
1691 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001692 if (IS_RELAXNG(cur, "start")) {
1693 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001694
Daniel Veillard4c004142003-10-07 11:33:24 +00001695 found =
1696 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1697 if (!found) {
1698 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1699 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1700 URL, NULL);
1701 }
1702 } else if (IS_RELAXNG(cur, "define")) {
1703 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001704
Daniel Veillard4c004142003-10-07 11:33:24 +00001705 name = xmlGetProp(cur, BAD_CAST "name");
1706 if (name == NULL) {
1707 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1708 "xmlRelaxNG: include %s has define without name\n",
1709 URL, NULL);
1710 } else {
1711 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001712
Daniel Veillard4c004142003-10-07 11:33:24 +00001713 xmlRelaxNGNormExtSpace(name);
1714 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1715 root->children, name);
1716 if (!found) {
1717 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1718 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1719 URL, name);
1720 }
1721 xmlFree(name);
1722 }
1723 }
1724 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001725 }
1726
1727
Daniel Veillard4c004142003-10-07 11:33:24 +00001728 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001729}
1730
1731/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001732 * xmlRelaxNGValidErrorPush:
1733 * @ctxt: the validation context
1734 * @err: the error code
1735 * @arg1: the first string argument
1736 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001737 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001738 *
1739 * Pushes a new error on top of the error stack
1740 *
1741 * Returns 0 in case of error, the index in the stack otherwise
1742 */
1743static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001744xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1745 xmlRelaxNGValidErr err, const xmlChar * arg1,
1746 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001747{
1748 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001749
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001750#ifdef DEBUG_ERROR
1751 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001752 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001753#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001754 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001755 ctxt->errMax = 8;
1756 ctxt->errNr = 0;
1757 ctxt->errTab =
1758 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1759 sizeof
1760 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001761 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001762 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001763 return (0);
1764 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001765 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001766 }
1767 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001768 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001769 ctxt->errTab =
1770 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001771 ctxt->errMax *
1772 sizeof
1773 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001774 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001775 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001776 return (0);
1777 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001778 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001779 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001780 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001781 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1782 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001783 cur = &ctxt->errTab[ctxt->errNr];
1784 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001785 if (dup) {
1786 cur->arg1 = xmlStrdup(arg1);
1787 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001788 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001789 } else {
1790 cur->arg1 = arg1;
1791 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001792 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001793 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001794 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001795 cur->node = ctxt->state->node;
1796 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001797 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001798 cur->node = NULL;
1799 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001800 }
1801 ctxt->err = cur;
1802 return (ctxt->errNr++);
1803}
1804
1805/**
1806 * xmlRelaxNGValidErrorPop:
1807 * @ctxt: the validation context
1808 *
1809 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001810 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001811static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001812xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1813{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001814 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001815
Daniel Veillard580ced82003-03-21 21:22:48 +00001816 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001817 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001818 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001819 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001820 ctxt->errNr--;
1821 if (ctxt->errNr > 0)
1822 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1823 else
1824 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001825 cur = &ctxt->errTab[ctxt->errNr];
1826 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001827 if (cur->arg1 != NULL)
1828 xmlFree((xmlChar *) cur->arg1);
1829 cur->arg1 = NULL;
1830 if (cur->arg2 != NULL)
1831 xmlFree((xmlChar *) cur->arg2);
1832 cur->arg2 = NULL;
1833 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001834 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001835}
1836
Daniel Veillard42f12e92003-03-07 18:32:59 +00001837/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001838 * xmlRelaxNGDocumentPush:
1839 * @ctxt: the parser context
1840 * @value: the element doc
1841 *
1842 * Pushes a new doc on top of the doc stack
1843 *
1844 * Returns 0 in case of error, the index in the stack otherwise
1845 */
1846static int
1847xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001848 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001849{
1850 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001851 ctxt->docMax = 4;
1852 ctxt->docNr = 0;
1853 ctxt->docTab =
1854 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1855 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001856 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001857 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001858 return (0);
1859 }
1860 }
1861 if (ctxt->docNr >= ctxt->docMax) {
1862 ctxt->docMax *= 2;
1863 ctxt->docTab =
1864 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001865 ctxt->docMax *
1866 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001867 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001868 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001869 return (0);
1870 }
1871 }
1872 ctxt->docTab[ctxt->docNr] = value;
1873 ctxt->doc = value;
1874 return (ctxt->docNr++);
1875}
1876
1877/**
1878 * xmlRelaxNGDocumentPop:
1879 * @ctxt: the parser context
1880 *
1881 * Pops the top doc from the doc stack
1882 *
1883 * Returns the doc just removed
1884 */
1885static xmlRelaxNGDocumentPtr
1886xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1887{
1888 xmlRelaxNGDocumentPtr ret;
1889
1890 if (ctxt->docNr <= 0)
1891 return (0);
1892 ctxt->docNr--;
1893 if (ctxt->docNr > 0)
1894 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1895 else
1896 ctxt->doc = NULL;
1897 ret = ctxt->docTab[ctxt->docNr];
1898 ctxt->docTab[ctxt->docNr] = 0;
1899 return (ret);
1900}
1901
1902/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001903 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001904 * @ctxt: the parser context
1905 * @URL: the normalized URL
1906 * @ns: the inherited ns if any
1907 *
1908 * First lookup if the document is already loaded into the parser context,
1909 * check against recursion. If not found the resource is loaded and
1910 * the content is preprocessed before being returned back to the caller.
1911 *
1912 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1913 */
1914static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001915xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1916 const xmlChar * URL, const xmlChar * ns)
1917{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001918 xmlRelaxNGDocumentPtr ret = NULL;
1919 xmlDocPtr doc;
1920 xmlNodePtr root;
1921 int i;
1922
1923 /*
1924 * check against recursion in the stack
1925 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001926 for (i = 0; i < ctxt->docNr; i++) {
1927 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1928 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1929 "Detected an externalRef recursion for %s\n", URL,
1930 NULL);
1931 return (NULL);
1932 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001933 }
1934
1935 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001936 * load the document
1937 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001938 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001939 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001940 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1941 "xmlRelaxNG: could not load %s\n", URL, NULL);
1942 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001943 }
1944
1945 /*
1946 * Allocate the document structures and register it first.
1947 */
1948 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1949 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001950 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1951 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1952 xmlFreeDoc(doc);
1953 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001954 }
1955 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1956 ret->doc = doc;
1957 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001958 ret->next = ctxt->documents;
1959 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001960
1961 /*
1962 * transmit the ns if needed
1963 */
1964 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001965 root = xmlDocGetRootElement(doc);
1966 if (root != NULL) {
1967 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1968 xmlSetProp(root, BAD_CAST "ns", ns);
1969 }
1970 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001971 }
1972
1973 /*
1974 * push it on the stack and register it in the hash table
1975 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001976 xmlRelaxNGDocumentPush(ctxt, ret);
1977
1978 /*
1979 * Some preprocessing of the document content
1980 */
1981 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1982 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001983 ctxt->doc = NULL;
1984 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001985 }
1986
1987 xmlRelaxNGDocumentPop(ctxt);
1988
Daniel Veillard4c004142003-10-07 11:33:24 +00001989 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001990}
1991
1992/************************************************************************
1993 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001994 * Error functions *
1995 * *
1996 ************************************************************************/
1997
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001998#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1999#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2000#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2001#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2002#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002003
Daniel Veillard231d7912003-02-09 14:22:17 +00002004static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002005xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2006{
Daniel Veillard231d7912003-02-09 14:22:17 +00002007 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002008 return ("none");
2009 switch (def->type) {
2010 case XML_RELAXNG_EMPTY:
2011 return ("empty");
2012 case XML_RELAXNG_NOT_ALLOWED:
2013 return ("notAllowed");
2014 case XML_RELAXNG_EXCEPT:
2015 return ("except");
2016 case XML_RELAXNG_TEXT:
2017 return ("text");
2018 case XML_RELAXNG_ELEMENT:
2019 return ("element");
2020 case XML_RELAXNG_DATATYPE:
2021 return ("datatype");
2022 case XML_RELAXNG_VALUE:
2023 return ("value");
2024 case XML_RELAXNG_LIST:
2025 return ("list");
2026 case XML_RELAXNG_ATTRIBUTE:
2027 return ("attribute");
2028 case XML_RELAXNG_DEF:
2029 return ("def");
2030 case XML_RELAXNG_REF:
2031 return ("ref");
2032 case XML_RELAXNG_EXTERNALREF:
2033 return ("externalRef");
2034 case XML_RELAXNG_PARENTREF:
2035 return ("parentRef");
2036 case XML_RELAXNG_OPTIONAL:
2037 return ("optional");
2038 case XML_RELAXNG_ZEROORMORE:
2039 return ("zeroOrMore");
2040 case XML_RELAXNG_ONEORMORE:
2041 return ("oneOrMore");
2042 case XML_RELAXNG_CHOICE:
2043 return ("choice");
2044 case XML_RELAXNG_GROUP:
2045 return ("group");
2046 case XML_RELAXNG_INTERLEAVE:
2047 return ("interleave");
2048 case XML_RELAXNG_START:
2049 return ("start");
2050 case XML_RELAXNG_NOOP:
2051 return ("noop");
2052 case XML_RELAXNG_PARAM:
2053 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002054 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002055 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002056}
Daniel Veillardd2298792003-02-14 16:54:11 +00002057
Daniel Veillard6eadf632003-01-23 18:29:16 +00002058/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002059 * xmlRelaxNGGetErrorString:
2060 * @err: the error code
2061 * @arg1: the first string argument
2062 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002063 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002064 * computes a formatted error string for the given error code and args
2065 *
2066 * Returns the error string, it must be deallocated by the caller
2067 */
2068static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002069xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2070 const xmlChar * arg2)
2071{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002072 char msg[1000];
2073
2074 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002075 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002076 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002077 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002078
2079 msg[0] = 0;
2080 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002081 case XML_RELAXNG_OK:
2082 return (NULL);
2083 case XML_RELAXNG_ERR_MEMORY:
2084 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002085 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002086 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2087 break;
2088 case XML_RELAXNG_ERR_TYPEVAL:
2089 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2090 arg2);
2091 break;
2092 case XML_RELAXNG_ERR_DUPID:
2093 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2094 break;
2095 case XML_RELAXNG_ERR_TYPECMP:
2096 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2097 break;
2098 case XML_RELAXNG_ERR_NOSTATE:
2099 return (xmlCharStrdup("Internal error: no state\n"));
2100 case XML_RELAXNG_ERR_NODEFINE:
2101 return (xmlCharStrdup("Internal error: no define\n"));
2102 case XML_RELAXNG_ERR_INTERNAL:
2103 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2104 break;
2105 case XML_RELAXNG_ERR_LISTEXTRA:
2106 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2107 break;
2108 case XML_RELAXNG_ERR_INTERNODATA:
2109 return (xmlCharStrdup
2110 ("Internal: interleave block has no data\n"));
2111 case XML_RELAXNG_ERR_INTERSEQ:
2112 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2113 case XML_RELAXNG_ERR_INTEREXTRA:
2114 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2115 break;
2116 case XML_RELAXNG_ERR_ELEMNAME:
2117 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2118 arg2);
2119 break;
2120 case XML_RELAXNG_ERR_ELEMNONS:
2121 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2122 arg1);
2123 break;
2124 case XML_RELAXNG_ERR_ELEMWRONGNS:
2125 snprintf(msg, 1000,
2126 "Element %s has wrong namespace: expecting %s\n", arg1,
2127 arg2);
2128 break;
2129 case XML_RELAXNG_ERR_ELEMWRONG:
2130 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2131 break;
2132 case XML_RELAXNG_ERR_TEXTWRONG:
2133 snprintf(msg, 1000,
2134 "Did not expect text in element %s content\n", arg1);
2135 break;
2136 case XML_RELAXNG_ERR_ELEMEXTRANS:
2137 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2138 arg1);
2139 break;
2140 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2141 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2142 break;
2143 case XML_RELAXNG_ERR_NOELEM:
2144 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2145 arg1);
2146 break;
2147 case XML_RELAXNG_ERR_NOTELEM:
2148 return (xmlCharStrdup("Expecting an element got text\n"));
2149 case XML_RELAXNG_ERR_ATTRVALID:
2150 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2151 arg1);
2152 break;
2153 case XML_RELAXNG_ERR_CONTENTVALID:
2154 snprintf(msg, 1000, "Element %s failed to validate content\n",
2155 arg1);
2156 break;
2157 case XML_RELAXNG_ERR_EXTRACONTENT:
2158 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2159 arg1, arg2);
2160 break;
2161 case XML_RELAXNG_ERR_INVALIDATTR:
2162 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2163 arg1, arg2);
2164 break;
2165 case XML_RELAXNG_ERR_LACKDATA:
2166 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2167 arg1);
2168 break;
2169 case XML_RELAXNG_ERR_DATAELEM:
2170 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2171 arg1);
2172 break;
2173 case XML_RELAXNG_ERR_VALELEM:
2174 snprintf(msg, 1000, "Value element %s has child elements\n",
2175 arg1);
2176 break;
2177 case XML_RELAXNG_ERR_LISTELEM:
2178 snprintf(msg, 1000, "List element %s has child elements\n",
2179 arg1);
2180 break;
2181 case XML_RELAXNG_ERR_DATATYPE:
2182 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2183 break;
2184 case XML_RELAXNG_ERR_VALUE:
2185 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2186 break;
2187 case XML_RELAXNG_ERR_LIST:
2188 return (xmlCharStrdup("Error validating list\n"));
2189 case XML_RELAXNG_ERR_NOGRAMMAR:
2190 return (xmlCharStrdup("No top grammar defined\n"));
2191 case XML_RELAXNG_ERR_EXTRADATA:
2192 return (xmlCharStrdup("Extra data in the document\n"));
2193 default:
2194 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002195 }
2196 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002197 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002198 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002199 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002200 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002201}
2202
2203/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002204 * xmlRelaxNGShowValidError:
2205 * @ctxt: the validation context
2206 * @err: the error number
2207 * @node: the node
2208 * @child: the node child generating the problem.
2209 * @arg1: the first argument
2210 * @arg2: the second argument
2211 *
2212 * Show a validation error.
2213 */
2214static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002215xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2216 xmlRelaxNGValidErr err, xmlNodePtr node,
2217 xmlNodePtr child, const xmlChar * arg1,
2218 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002219{
2220 xmlChar *msg;
2221
2222 if (ctxt->error == NULL)
2223 return;
2224
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002225#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002226 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002227#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002228 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2229 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002230 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002231
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002232 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002233 ctxt->errNo = err;
2234 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2235 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002236 xmlFree(msg);
2237}
2238
2239/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002240 * xmlRelaxNGPopErrors:
2241 * @ctxt: the validation context
2242 * @level: the error level in the stack
2243 *
2244 * pop and discard all errors until the given level is reached
2245 */
2246static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002247xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2248{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002249 int i;
2250 xmlRelaxNGValidErrorPtr err;
2251
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002252#ifdef DEBUG_ERROR
2253 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002254 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002255#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002256 for (i = level; i < ctxt->errNr; i++) {
2257 err = &ctxt->errTab[i];
2258 if (err->flags & ERROR_IS_DUP) {
2259 if (err->arg1 != NULL)
2260 xmlFree((xmlChar *) err->arg1);
2261 err->arg1 = NULL;
2262 if (err->arg2 != NULL)
2263 xmlFree((xmlChar *) err->arg2);
2264 err->arg2 = NULL;
2265 err->flags = 0;
2266 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002267 }
2268 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002269 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002270 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002271}
Daniel Veillard4c004142003-10-07 11:33:24 +00002272
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002273/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002274 * xmlRelaxNGDumpValidError:
2275 * @ctxt: the validation context
2276 *
2277 * Show all validation error over a given index.
2278 */
2279static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002280xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2281{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002282 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002283 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002284
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002285#ifdef DEBUG_ERROR
2286 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002287 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002288#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002289 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2290 err = &ctxt->errTab[i];
2291 if (k < MAX_ERROR) {
2292 for (j = 0; j < i; j++) {
2293 dup = &ctxt->errTab[j];
2294 if ((err->err == dup->err) && (err->node == dup->node) &&
2295 (xmlStrEqual(err->arg1, dup->arg1)) &&
2296 (xmlStrEqual(err->arg2, dup->arg2))) {
2297 goto skip;
2298 }
2299 }
2300 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2301 err->arg1, err->arg2);
2302 k++;
2303 }
2304 skip:
2305 if (err->flags & ERROR_IS_DUP) {
2306 if (err->arg1 != NULL)
2307 xmlFree((xmlChar *) err->arg1);
2308 err->arg1 = NULL;
2309 if (err->arg2 != NULL)
2310 xmlFree((xmlChar *) err->arg2);
2311 err->arg2 = NULL;
2312 err->flags = 0;
2313 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002314 }
2315 ctxt->errNr = 0;
2316}
Daniel Veillard4c004142003-10-07 11:33:24 +00002317
Daniel Veillard42f12e92003-03-07 18:32:59 +00002318/**
2319 * xmlRelaxNGAddValidError:
2320 * @ctxt: the validation context
2321 * @err: the error number
2322 * @arg1: the first argument
2323 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002324 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002325 *
2326 * Register a validation error, either generating it if it's sure
2327 * or stacking it for later handling if unsure.
2328 */
2329static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002330xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2331 xmlRelaxNGValidErr err, const xmlChar * arg1,
2332 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002333{
2334 if ((ctxt == NULL) || (ctxt->error == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002335 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002336
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002337#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002338 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002339#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002340 /*
2341 * generate the error directly
2342 */
William M. Brack60929622004-03-27 17:54:18 +00002343 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2344 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002345 xmlNodePtr node, seq;
2346
2347 /*
2348 * Flush first any stacked error which might be the
2349 * real cause of the problem.
2350 */
2351 if (ctxt->errNr != 0)
2352 xmlRelaxNGDumpValidError(ctxt);
2353 if (ctxt->state != NULL) {
2354 node = ctxt->state->node;
2355 seq = ctxt->state->seq;
2356 } else {
2357 node = seq = NULL;
2358 }
2359 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002360 }
2361 /*
2362 * Stack the error for later processing if needed
2363 */
2364 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002365 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002366 }
2367}
2368
Daniel Veillard6eadf632003-01-23 18:29:16 +00002369
2370/************************************************************************
2371 * *
2372 * Type library hooks *
2373 * *
2374 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002375static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002376 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002377
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002378/**
2379 * xmlRelaxNGSchemaTypeHave:
2380 * @data: data needed for the library
2381 * @type: the type name
2382 *
2383 * Check if the given type is provided by
2384 * the W3C XMLSchema Datatype library.
2385 *
2386 * Returns 1 if yes, 0 if no and -1 in case of error.
2387 */
2388static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002389xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2390{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002391 xmlSchemaTypePtr typ;
2392
2393 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002394 return (-1);
2395 typ = xmlSchemaGetPredefinedType(type,
2396 BAD_CAST
2397 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002398 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002399 return (0);
2400 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002401}
2402
2403/**
2404 * xmlRelaxNGSchemaTypeCheck:
2405 * @data: data needed for the library
2406 * @type: the type name
2407 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002408 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002409 *
2410 * Check if the given type and value are validated by
2411 * the W3C XMLSchema Datatype library.
2412 *
2413 * Returns 1 if yes, 0 if no and -1 in case of error.
2414 */
2415static int
2416xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002417 const xmlChar * type,
2418 const xmlChar * value,
2419 void **result, xmlNodePtr node)
2420{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002421 xmlSchemaTypePtr typ;
2422 int ret;
2423
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002424 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002425 return (-1);
2426 typ = xmlSchemaGetPredefinedType(type,
2427 BAD_CAST
2428 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002429 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002430 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002431 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002432 (xmlSchemaValPtr *) result, node);
2433 if (ret == 2) /* special ID error code */
2434 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002435 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002436 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002437 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002438 return (0);
2439 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002440}
2441
2442/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002443 * xmlRelaxNGSchemaFacetCheck:
2444 * @data: data needed for the library
2445 * @type: the type name
2446 * @facet: the facet name
2447 * @val: the facet value
2448 * @strval: the string value
2449 * @value: the value to check
2450 *
2451 * Function provided by a type library to check a value facet
2452 *
2453 * Returns 1 if yes, 0 if no and -1 in case of error.
2454 */
2455static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002456xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2457 const xmlChar * type, const xmlChar * facetname,
2458 const xmlChar * val, const xmlChar * strval,
2459 void *value)
2460{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002461 xmlSchemaFacetPtr facet;
2462 xmlSchemaTypePtr typ;
2463 int ret;
2464
2465 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002466 return (-1);
2467 typ = xmlSchemaGetPredefinedType(type,
2468 BAD_CAST
2469 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002470 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002471 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002472
2473 facet = xmlSchemaNewFacet();
2474 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002475 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002476
Daniel Veillard4c004142003-10-07 11:33:24 +00002477 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002478 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002479 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002480 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002481 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002482 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002483 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002484 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002485 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002486 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002487 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002488 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002489 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002490 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002491 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002492 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002493 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002494 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002495 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002496 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002497 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002498 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2499 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2500 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2501 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002502 xmlSchemaFreeFacet(facet);
2503 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002504 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002505 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002506 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2507 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002508 xmlSchemaFreeFacet(facet);
2509 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002510 }
2511 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2512 xmlSchemaFreeFacet(facet);
2513 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002514 return (-1);
2515 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002516}
2517
2518/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002519 * xmlRelaxNGSchemaFreeValue:
2520 * @data: data needed for the library
2521 * @value: the value to free
2522 *
2523 * Function provided by a type library to free a Schemas value
2524 *
2525 * Returns 1 if yes, 0 if no and -1 in case of error.
2526 */
2527static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002528xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2529{
Daniel Veillard80b19092003-03-28 13:29:53 +00002530 xmlSchemaFreeValue(value);
2531}
2532
2533/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002534 * xmlRelaxNGSchemaTypeCompare:
2535 * @data: data needed for the library
2536 * @type: the type name
2537 * @value1: the first value
2538 * @value2: the second value
2539 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002540 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002541 * Datatype library.
2542 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002543 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002544 */
2545static int
2546xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002547 const xmlChar * type,
2548 const xmlChar * value1,
2549 xmlNodePtr ctxt1,
2550 void *comp1,
2551 const xmlChar * value2, xmlNodePtr ctxt2)
2552{
Daniel Veillard80b19092003-03-28 13:29:53 +00002553 int ret;
2554 xmlSchemaTypePtr typ;
2555 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2556
2557 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002558 return (-1);
2559 typ = xmlSchemaGetPredefinedType(type,
2560 BAD_CAST
2561 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002562 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002563 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002564 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002565 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2566 if (ret != 0)
2567 return (-1);
2568 if (res1 == NULL)
2569 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002570 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002571 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002572 }
2573 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002574 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002575 xmlSchemaFreeValue(res1);
2576 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002577 }
2578 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002579 xmlSchemaFreeValue(res1);
2580 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002581 }
2582 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002583 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002584 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002585 xmlSchemaFreeValue(res2);
2586 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002587 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002588 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002589 return (1);
2590 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002591}
Daniel Veillard4c004142003-10-07 11:33:24 +00002592
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002593/**
2594 * xmlRelaxNGDefaultTypeHave:
2595 * @data: data needed for the library
2596 * @type: the type name
2597 *
2598 * Check if the given type is provided by
2599 * the default datatype library.
2600 *
2601 * Returns 1 if yes, 0 if no and -1 in case of error.
2602 */
2603static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002604xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2605 const xmlChar * type)
2606{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002607 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002608 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002609 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002610 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002611 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002612 return (1);
2613 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002614}
2615
2616/**
2617 * xmlRelaxNGDefaultTypeCheck:
2618 * @data: data needed for the library
2619 * @type: the type name
2620 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002621 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002622 *
2623 * Check if the given type and value are validated by
2624 * the default datatype library.
2625 *
2626 * Returns 1 if yes, 0 if no and -1 in case of error.
2627 */
2628static int
2629xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002630 const xmlChar * type ATTRIBUTE_UNUSED,
2631 const xmlChar * value ATTRIBUTE_UNUSED,
2632 void **result ATTRIBUTE_UNUSED,
2633 xmlNodePtr node ATTRIBUTE_UNUSED)
2634{
Daniel Veillardd4310742003-02-18 21:12:46 +00002635 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002636 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002637 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002638 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002639 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002640 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002641 }
2642
Daniel Veillard4c004142003-10-07 11:33:24 +00002643 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002644}
2645
2646/**
2647 * xmlRelaxNGDefaultTypeCompare:
2648 * @data: data needed for the library
2649 * @type: the type name
2650 * @value1: the first value
2651 * @value2: the second value
2652 *
2653 * Compare two values accordingly a type from the default
2654 * datatype library.
2655 *
2656 * Returns 1 if yes, 0 if no and -1 in case of error.
2657 */
2658static int
2659xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002660 const xmlChar * type,
2661 const xmlChar * value1,
2662 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2663 void *comp1 ATTRIBUTE_UNUSED,
2664 const xmlChar * value2,
2665 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2666{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002667 int ret = -1;
2668
2669 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002670 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002671 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002672 if (!xmlStrEqual(value1, value2)) {
2673 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002674
Daniel Veillard4c004142003-10-07 11:33:24 +00002675 /*
2676 * TODO: trivial optimizations are possible by
2677 * computing at compile-time
2678 */
2679 nval = xmlRelaxNGNormalize(NULL, value1);
2680 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002681
Daniel Veillard4c004142003-10-07 11:33:24 +00002682 if ((nval == NULL) || (nvalue == NULL))
2683 ret = -1;
2684 else if (xmlStrEqual(nval, nvalue))
2685 ret = 1;
2686 else
2687 ret = 0;
2688 if (nval != NULL)
2689 xmlFree(nval);
2690 if (nvalue != NULL)
2691 xmlFree(nvalue);
2692 } else
2693 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002694 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002695 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002696}
Daniel Veillard4c004142003-10-07 11:33:24 +00002697
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002698static int xmlRelaxNGTypeInitialized = 0;
2699static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2700
2701/**
2702 * xmlRelaxNGFreeTypeLibrary:
2703 * @lib: the type library structure
2704 * @namespace: the URI bound to the library
2705 *
2706 * Free the structure associated to the type library
2707 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002708static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002709xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002710 const xmlChar * namespace ATTRIBUTE_UNUSED)
2711{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002712 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002713 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002714 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002715 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002716 xmlFree(lib);
2717}
2718
2719/**
2720 * xmlRelaxNGRegisterTypeLibrary:
2721 * @namespace: the URI bound to the library
2722 * @data: data associated to the library
2723 * @have: the provide function
2724 * @check: the checking function
2725 * @comp: the comparison function
2726 *
2727 * Register a new type library
2728 *
2729 * Returns 0 in case of success and -1 in case of error.
2730 */
2731static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002732xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2733 xmlRelaxNGTypeHave have,
2734 xmlRelaxNGTypeCheck check,
2735 xmlRelaxNGTypeCompare comp,
2736 xmlRelaxNGFacetCheck facet,
2737 xmlRelaxNGTypeFree freef)
2738{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002739 xmlRelaxNGTypeLibraryPtr lib;
2740 int ret;
2741
2742 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002743 (check == NULL) || (comp == NULL))
2744 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002745 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002746 xmlGenericError(xmlGenericErrorContext,
2747 "Relax-NG types library '%s' already registered\n",
2748 namespace);
2749 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002750 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002751 lib =
2752 (xmlRelaxNGTypeLibraryPtr)
2753 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002754 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002755 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002756 return (-1);
2757 }
2758 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2759 lib->namespace = xmlStrdup(namespace);
2760 lib->data = data;
2761 lib->have = have;
2762 lib->comp = comp;
2763 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002764 lib->facet = facet;
2765 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002766 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2767 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002768 xmlGenericError(xmlGenericErrorContext,
2769 "Relax-NG types library failed to register '%s'\n",
2770 namespace);
2771 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2772 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002773 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002774 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002775}
2776
2777/**
2778 * xmlRelaxNGInitTypes:
2779 *
2780 * Initilize the default type libraries.
2781 *
2782 * Returns 0 in case of success and -1 in case of error.
2783 */
2784static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002785xmlRelaxNGInitTypes(void)
2786{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002787 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002788 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002789 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2790 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002791 xmlGenericError(xmlGenericErrorContext,
2792 "Failed to allocate sh table for Relax-NG types\n");
2793 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002794 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002795 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2796 "http://www.w3.org/2001/XMLSchema-datatypes",
2797 NULL, xmlRelaxNGSchemaTypeHave,
2798 xmlRelaxNGSchemaTypeCheck,
2799 xmlRelaxNGSchemaTypeCompare,
2800 xmlRelaxNGSchemaFacetCheck,
2801 xmlRelaxNGSchemaFreeValue);
2802 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2803 xmlRelaxNGDefaultTypeHave,
2804 xmlRelaxNGDefaultTypeCheck,
2805 xmlRelaxNGDefaultTypeCompare, NULL,
2806 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002807 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002808 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002809}
2810
2811/**
2812 * xmlRelaxNGCleanupTypes:
2813 *
2814 * Cleanup the default Schemas type library associated to RelaxNG
2815 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002816void
2817xmlRelaxNGCleanupTypes(void)
2818{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002819 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002820 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002821 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002822 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002823 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002824 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002825}
2826
2827/************************************************************************
2828 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002829 * Compiling element content into regexp *
2830 * *
2831 * Sometime the element content can be compiled into a pure regexp, *
2832 * This allows a faster execution and streamability at that level *
2833 * *
2834 ************************************************************************/
2835
Daniel Veillard52b48c72003-04-13 19:53:42 +00002836static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2837 xmlRelaxNGDefinePtr def);
2838
Daniel Veillard952379b2003-03-17 15:37:12 +00002839/**
2840 * xmlRelaxNGIsCompileable:
2841 * @define: the definition to check
2842 *
2843 * Check if a definition is nullable.
2844 *
2845 * Returns 1 if yes, 0 if no and -1 in case of error
2846 */
2847static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002848xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2849{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002850 int ret = -1;
2851
Daniel Veillard952379b2003-03-17 15:37:12 +00002852 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002853 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002854 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002855 if ((def->type != XML_RELAXNG_ELEMENT) &&
2856 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002857 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002858 if ((def->type != XML_RELAXNG_ELEMENT) &&
2859 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002860 return (0);
2861 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002862 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002863 ret = xmlRelaxNGIsCompileable(def->content);
2864 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002865 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002866 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002867 ret = 1;
2868 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002869 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002870 /*
2871 * Check if the element content is compileable
2872 */
2873 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2874 ((def->dflags & IS_COMPILABLE) == 0)) {
2875 xmlRelaxNGDefinePtr list;
2876
2877 list = def->content;
2878 while (list != NULL) {
2879 ret = xmlRelaxNGIsCompileable(list);
2880 if (ret != 1)
2881 break;
2882 list = list->next;
2883 }
William M. Brack60929622004-03-27 17:54:18 +00002884 /*
2885 * Because the routine is recursive, we must guard against
2886 * discovering both COMPILABLE and NOT_COMPILABLE
2887 */
2888 if (ret == 0) {
2889 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002890 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002891 }
2892 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002893 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002894#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002895 if (ret == 1) {
2896 xmlGenericError(xmlGenericErrorContext,
2897 "element content for %s is compilable\n",
2898 def->name);
2899 } else if (ret == 0) {
2900 xmlGenericError(xmlGenericErrorContext,
2901 "element content for %s is not compilable\n",
2902 def->name);
2903 } else {
2904 xmlGenericError(xmlGenericErrorContext,
2905 "Problem in RelaxNGIsCompileable for element %s\n",
2906 def->name);
2907 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002908#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002909 }
2910 /*
2911 * All elements return a compileable status unless they
2912 * are generic like anyName
2913 */
2914 if ((def->nameClass != NULL) || (def->name == NULL))
2915 ret = 0;
2916 else
2917 ret = 1;
2918 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002919 case XML_RELAXNG_REF:
2920 case XML_RELAXNG_EXTERNALREF:
2921 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002922 if (def->depth == -20) {
2923 return (1);
2924 } else {
2925 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002926
Daniel Veillard4c004142003-10-07 11:33:24 +00002927 def->depth = -20;
2928 list = def->content;
2929 while (list != NULL) {
2930 ret = xmlRelaxNGIsCompileable(list);
2931 if (ret != 1)
2932 break;
2933 list = list->next;
2934 }
2935 }
2936 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002937 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002938 case XML_RELAXNG_OPTIONAL:
2939 case XML_RELAXNG_ZEROORMORE:
2940 case XML_RELAXNG_ONEORMORE:
2941 case XML_RELAXNG_CHOICE:
2942 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002943 case XML_RELAXNG_DEF:{
2944 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002945
Daniel Veillard4c004142003-10-07 11:33:24 +00002946 list = def->content;
2947 while (list != NULL) {
2948 ret = xmlRelaxNGIsCompileable(list);
2949 if (ret != 1)
2950 break;
2951 list = list->next;
2952 }
2953 break;
2954 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002955 case XML_RELAXNG_EXCEPT:
2956 case XML_RELAXNG_ATTRIBUTE:
2957 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002958 case XML_RELAXNG_DATATYPE:
2959 case XML_RELAXNG_LIST:
2960 case XML_RELAXNG_PARAM:
2961 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002962 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002963 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002964 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002965 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002966 if (ret == 0)
2967 def->dflags |= IS_NOT_COMPILABLE;
2968 if (ret == 1)
2969 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002970#ifdef DEBUG_COMPILE
2971 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002972 xmlGenericError(xmlGenericErrorContext,
2973 "RelaxNGIsCompileable %s : true\n",
2974 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002975 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002976 xmlGenericError(xmlGenericErrorContext,
2977 "RelaxNGIsCompileable %s : false\n",
2978 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002979 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002980 xmlGenericError(xmlGenericErrorContext,
2981 "Problem in RelaxNGIsCompileable %s\n",
2982 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002983 }
2984#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002985 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002986}
2987
2988/**
2989 * xmlRelaxNGCompile:
2990 * ctxt: the RelaxNG parser context
2991 * @define: the definition tree to compile
2992 *
2993 * Compile the set of definitions, it works recursively, till the
2994 * element boundaries, where it tries to compile the content if possible
2995 *
2996 * Returns 0 if success and -1 in case of error
2997 */
2998static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002999xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3000{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003001 int ret = 0;
3002 xmlRelaxNGDefinePtr list;
3003
Daniel Veillard4c004142003-10-07 11:33:24 +00003004 if ((ctxt == NULL) || (def == NULL))
3005 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003006
Daniel Veillard4c004142003-10-07 11:33:24 +00003007 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003008 case XML_RELAXNG_START:
3009 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003010 xmlAutomataPtr oldam = ctxt->am;
3011 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003012
3013 def->depth = -25;
3014
Daniel Veillard4c004142003-10-07 11:33:24 +00003015 list = def->content;
3016 ctxt->am = xmlNewAutomata();
3017 if (ctxt->am == NULL)
3018 return (-1);
3019 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3020 while (list != NULL) {
3021 xmlRelaxNGCompile(ctxt, list);
3022 list = list->next;
3023 }
3024 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3025 def->contModel = xmlAutomataCompile(ctxt->am);
3026 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003027
Daniel Veillard4c004142003-10-07 11:33:24 +00003028 xmlFreeAutomata(ctxt->am);
3029 ctxt->state = oldstate;
3030 ctxt->am = oldam;
3031 }
3032 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003033 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003034 if ((ctxt->am != NULL) && (def->name != NULL)) {
3035 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3036 ctxt->state, NULL,
3037 def->name, def->ns,
3038 def);
3039 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003040 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003041 xmlAutomataPtr oldam = ctxt->am;
3042 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003043
3044 def->depth = -25;
3045
Daniel Veillard4c004142003-10-07 11:33:24 +00003046 list = def->content;
3047 ctxt->am = xmlNewAutomata();
3048 if (ctxt->am == NULL)
3049 return (-1);
3050 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3051 while (list != NULL) {
3052 xmlRelaxNGCompile(ctxt, list);
3053 list = list->next;
3054 }
3055 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3056 def->contModel = xmlAutomataCompile(ctxt->am);
3057 if (!xmlRegexpIsDeterminist(def->contModel)) {
3058 /*
3059 * we can only use the automata if it is determinist
3060 */
3061 xmlRegFreeRegexp(def->contModel);
3062 def->contModel = NULL;
3063 }
3064 xmlFreeAutomata(ctxt->am);
3065 ctxt->state = oldstate;
3066 ctxt->am = oldam;
3067 } else {
3068 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003069
Daniel Veillard4c004142003-10-07 11:33:24 +00003070 /*
3071 * we can't build the content model for this element content
3072 * but it still might be possible to build it for some of its
3073 * children, recurse.
3074 */
3075 ret = xmlRelaxNGTryCompile(ctxt, def);
3076 ctxt->am = oldam;
3077 }
3078 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003079 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003080 ret = xmlRelaxNGCompile(ctxt, def->content);
3081 break;
3082 case XML_RELAXNG_OPTIONAL:{
3083 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003084
Daniel Veillard4c004142003-10-07 11:33:24 +00003085 xmlRelaxNGCompile(ctxt, def->content);
3086 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3087 break;
3088 }
3089 case XML_RELAXNG_ZEROORMORE:{
3090 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003091
Daniel Veillard4c004142003-10-07 11:33:24 +00003092 ctxt->state =
3093 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3094 oldstate = ctxt->state;
3095 list = def->content;
3096 while (list != NULL) {
3097 xmlRelaxNGCompile(ctxt, list);
3098 list = list->next;
3099 }
3100 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3101 ctxt->state =
3102 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3103 break;
3104 }
3105 case XML_RELAXNG_ONEORMORE:{
3106 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003107
Daniel Veillard4c004142003-10-07 11:33:24 +00003108 list = def->content;
3109 while (list != NULL) {
3110 xmlRelaxNGCompile(ctxt, list);
3111 list = list->next;
3112 }
3113 oldstate = ctxt->state;
3114 list = def->content;
3115 while (list != NULL) {
3116 xmlRelaxNGCompile(ctxt, list);
3117 list = list->next;
3118 }
3119 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3120 ctxt->state =
3121 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3122 break;
3123 }
3124 case XML_RELAXNG_CHOICE:{
3125 xmlAutomataStatePtr target = NULL;
3126 xmlAutomataStatePtr oldstate = ctxt->state;
3127
3128 list = def->content;
3129 while (list != NULL) {
3130 ctxt->state = oldstate;
3131 ret = xmlRelaxNGCompile(ctxt, list);
3132 if (ret != 0)
3133 break;
3134 if (target == NULL)
3135 target = ctxt->state;
3136 else {
3137 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3138 target);
3139 }
3140 list = list->next;
3141 }
3142 ctxt->state = target;
3143
3144 break;
3145 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003146 case XML_RELAXNG_REF:
3147 case XML_RELAXNG_EXTERNALREF:
3148 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003149 case XML_RELAXNG_GROUP:
3150 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003151 list = def->content;
3152 while (list != NULL) {
3153 ret = xmlRelaxNGCompile(ctxt, list);
3154 if (ret != 0)
3155 break;
3156 list = list->next;
3157 }
3158 break;
3159 case XML_RELAXNG_TEXT:{
3160 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003161
Daniel Veillard4c004142003-10-07 11:33:24 +00003162 ctxt->state =
3163 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3164 oldstate = ctxt->state;
3165 xmlRelaxNGCompile(ctxt, def->content);
3166 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3167 ctxt->state, BAD_CAST "#text",
3168 NULL);
3169 ctxt->state =
3170 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3171 break;
3172 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003173 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003174 ctxt->state =
3175 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3176 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003177 case XML_RELAXNG_EXCEPT:
3178 case XML_RELAXNG_ATTRIBUTE:
3179 case XML_RELAXNG_INTERLEAVE:
3180 case XML_RELAXNG_NOT_ALLOWED:
3181 case XML_RELAXNG_DATATYPE:
3182 case XML_RELAXNG_LIST:
3183 case XML_RELAXNG_PARAM:
3184 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003185 /* This should not happen and generate an internal error */
3186 fprintf(stderr, "RNG internal error trying to compile %s\n",
3187 xmlRelaxNGDefName(def));
3188 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003189 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003190 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003191}
3192
3193/**
3194 * xmlRelaxNGTryCompile:
3195 * ctxt: the RelaxNG parser context
3196 * @define: the definition tree to compile
3197 *
3198 * Try to compile the set of definitions, it works recursively,
3199 * possibly ignoring parts which cannot be compiled.
3200 *
3201 * Returns 0 if success and -1 in case of error
3202 */
3203static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003204xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3205{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003206 int ret = 0;
3207 xmlRelaxNGDefinePtr list;
3208
Daniel Veillard4c004142003-10-07 11:33:24 +00003209 if ((ctxt == NULL) || (def == NULL))
3210 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003211
3212 if ((def->type == XML_RELAXNG_START) ||
3213 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003214 ret = xmlRelaxNGIsCompileable(def);
3215 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3216 ctxt->am = NULL;
3217 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003218#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003219 if (ret == 0) {
3220 if (def->type == XML_RELAXNG_START)
3221 xmlGenericError(xmlGenericErrorContext,
3222 "compiled the start\n");
3223 else
3224 xmlGenericError(xmlGenericErrorContext,
3225 "compiled element %s\n", def->name);
3226 } else {
3227 if (def->type == XML_RELAXNG_START)
3228 xmlGenericError(xmlGenericErrorContext,
3229 "failed to compile the start\n");
3230 else
3231 xmlGenericError(xmlGenericErrorContext,
3232 "failed to compile element %s\n",
3233 def->name);
3234 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003235#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003236 return (ret);
3237 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003238 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003239 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003240 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003241 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3242 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003243 case XML_RELAXNG_TEXT:
3244 case XML_RELAXNG_DATATYPE:
3245 case XML_RELAXNG_LIST:
3246 case XML_RELAXNG_PARAM:
3247 case XML_RELAXNG_VALUE:
3248 case XML_RELAXNG_EMPTY:
3249 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003250 ret = 0;
3251 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003252 case XML_RELAXNG_OPTIONAL:
3253 case XML_RELAXNG_ZEROORMORE:
3254 case XML_RELAXNG_ONEORMORE:
3255 case XML_RELAXNG_CHOICE:
3256 case XML_RELAXNG_GROUP:
3257 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003258 case XML_RELAXNG_START:
3259 case XML_RELAXNG_REF:
3260 case XML_RELAXNG_EXTERNALREF:
3261 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003262 list = def->content;
3263 while (list != NULL) {
3264 ret = xmlRelaxNGTryCompile(ctxt, list);
3265 if (ret != 0)
3266 break;
3267 list = list->next;
3268 }
3269 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003270 case XML_RELAXNG_EXCEPT:
3271 case XML_RELAXNG_ATTRIBUTE:
3272 case XML_RELAXNG_INTERLEAVE:
3273 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003274 ret = 0;
3275 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003276 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003277 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003278}
3279
3280/************************************************************************
3281 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003282 * Parsing functions *
3283 * *
3284 ************************************************************************/
3285
Daniel Veillard4c004142003-10-07 11:33:24 +00003286static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3287 ctxt, xmlNodePtr node);
3288static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3289 ctxt, xmlNodePtr node);
3290static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3291 ctxt, xmlNodePtr nodes,
3292 int group);
3293static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3294 ctxt, xmlNodePtr node);
3295static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3296 xmlNodePtr node);
3297static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3298 xmlNodePtr nodes);
3299static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3300 ctxt, xmlNodePtr node,
3301 xmlRelaxNGDefinePtr
3302 def);
3303static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3304 ctxt, xmlNodePtr nodes);
3305static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3306 xmlRelaxNGDefinePtr define,
3307 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003308
3309
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003310#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003311
3312/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003313 * xmlRelaxNGIsNullable:
3314 * @define: the definition to verify
3315 *
3316 * Check if a definition is nullable.
3317 *
3318 * Returns 1 if yes, 0 if no and -1 in case of error
3319 */
3320static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003321xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3322{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003323 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003324
Daniel Veillardfd573f12003-03-16 17:52:32 +00003325 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003326 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003327
Daniel Veillarde063f482003-03-21 16:53:17 +00003328 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003329 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003330 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003331 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003332 switch (define->type) {
3333 case XML_RELAXNG_EMPTY:
3334 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003335 ret = 1;
3336 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003337 case XML_RELAXNG_NOOP:
3338 case XML_RELAXNG_DEF:
3339 case XML_RELAXNG_REF:
3340 case XML_RELAXNG_EXTERNALREF:
3341 case XML_RELAXNG_PARENTREF:
3342 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003343 ret = xmlRelaxNGIsNullable(define->content);
3344 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003345 case XML_RELAXNG_EXCEPT:
3346 case XML_RELAXNG_NOT_ALLOWED:
3347 case XML_RELAXNG_ELEMENT:
3348 case XML_RELAXNG_DATATYPE:
3349 case XML_RELAXNG_PARAM:
3350 case XML_RELAXNG_VALUE:
3351 case XML_RELAXNG_LIST:
3352 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003353 ret = 0;
3354 break;
3355 case XML_RELAXNG_CHOICE:{
3356 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003357
Daniel Veillard4c004142003-10-07 11:33:24 +00003358 while (list != NULL) {
3359 ret = xmlRelaxNGIsNullable(list);
3360 if (ret != 0)
3361 goto done;
3362 list = list->next;
3363 }
3364 ret = 0;
3365 break;
3366 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003367 case XML_RELAXNG_START:
3368 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003369 case XML_RELAXNG_GROUP:{
3370 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003371
Daniel Veillard4c004142003-10-07 11:33:24 +00003372 while (list != NULL) {
3373 ret = xmlRelaxNGIsNullable(list);
3374 if (ret != 1)
3375 goto done;
3376 list = list->next;
3377 }
3378 return (1);
3379 }
3380 default:
3381 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003382 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003383 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003384 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003385 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003386 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003387 define->dflags |= IS_NULLABLE;
3388 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003389}
3390
3391/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003392 * xmlRelaxNGIsBlank:
3393 * @str: a string
3394 *
3395 * Check if a string is ignorable c.f. 4.2. Whitespace
3396 *
3397 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3398 */
3399static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003400xmlRelaxNGIsBlank(xmlChar * str)
3401{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003402 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003403 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003404 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003405 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003406 return (0);
3407 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003408 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003409 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003410}
3411
Daniel Veillard6eadf632003-01-23 18:29:16 +00003412/**
3413 * xmlRelaxNGGetDataTypeLibrary:
3414 * @ctxt: a Relax-NG parser context
3415 * @node: the current data or value element
3416 *
3417 * Applies algorithm from 4.3. datatypeLibrary attribute
3418 *
3419 * Returns the datatypeLibary value or NULL if not found
3420 */
3421static xmlChar *
3422xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003423 xmlNodePtr node)
3424{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003425 xmlChar *ret, *escape;
3426
Daniel Veillard6eadf632003-01-23 18:29:16 +00003427 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003428 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3429 if (ret != NULL) {
3430 if (ret[0] == 0) {
3431 xmlFree(ret);
3432 return (NULL);
3433 }
3434 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3435 if (escape == NULL) {
3436 return (ret);
3437 }
3438 xmlFree(ret);
3439 return (escape);
3440 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003441 }
3442 node = node->parent;
3443 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003444 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3445 if (ret != NULL) {
3446 if (ret[0] == 0) {
3447 xmlFree(ret);
3448 return (NULL);
3449 }
3450 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3451 if (escape == NULL) {
3452 return (ret);
3453 }
3454 xmlFree(ret);
3455 return (escape);
3456 }
3457 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003458 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003459 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003460}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003461
3462/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003463 * xmlRelaxNGParseValue:
3464 * @ctxt: a Relax-NG parser context
3465 * @node: the data node.
3466 *
3467 * parse the content of a RelaxNG value node.
3468 *
3469 * Returns the definition pointer or NULL in case of error
3470 */
3471static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003472xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3473{
Daniel Veillardedc91922003-01-26 00:52:04 +00003474 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003475 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003476 xmlChar *type;
3477 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003478 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003479
Daniel Veillardfd573f12003-03-16 17:52:32 +00003480 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003481 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003482 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003483 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003484
3485 type = xmlGetProp(node, BAD_CAST "type");
3486 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003487 xmlRelaxNGNormExtSpace(type);
3488 if (xmlValidateNCName(type, 0)) {
3489 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3490 "value type '%s' is not an NCName\n", type, NULL);
3491 }
3492 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3493 if (library == NULL)
3494 library =
3495 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003496
Daniel Veillard4c004142003-10-07 11:33:24 +00003497 def->name = type;
3498 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003499
Daniel Veillard4c004142003-10-07 11:33:24 +00003500 lib = (xmlRelaxNGTypeLibraryPtr)
3501 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3502 if (lib == NULL) {
3503 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3504 "Use of unregistered type library '%s'\n", library,
3505 NULL);
3506 def->data = NULL;
3507 } else {
3508 def->data = lib;
3509 if (lib->have == NULL) {
3510 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3511 "Internal error with type library '%s': no 'have'\n",
3512 library, NULL);
3513 } else {
3514 success = lib->have(lib->data, def->name);
3515 if (success != 1) {
3516 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3517 "Error type '%s' is not exported by type library '%s'\n",
3518 def->name, library);
3519 }
3520 }
3521 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003522 }
3523 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003524 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003525 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003526 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3527 (node->children->next != NULL)) {
3528 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3529 "Expecting a single text value for <value>content\n",
3530 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003531 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003532 def->value = xmlNodeGetContent(node);
3533 if (def->value == NULL) {
3534 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3535 "Element <value> has no content\n", NULL, NULL);
3536 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3537 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003538
Daniel Veillard4c004142003-10-07 11:33:24 +00003539 success =
3540 lib->check(lib->data, def->name, def->value, &val, node);
3541 if (success != 1) {
3542 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3543 "Value '%s' is not acceptable for type '%s'\n",
3544 def->value, def->name);
3545 } else {
3546 if (val != NULL)
3547 def->attrs = val;
3548 }
3549 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003550 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003551 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003552}
3553
3554/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003555 * xmlRelaxNGParseData:
3556 * @ctxt: a Relax-NG parser context
3557 * @node: the data node.
3558 *
3559 * parse the content of a RelaxNG data node.
3560 *
3561 * Returns the definition pointer or NULL in case of error
3562 */
3563static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003564xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3565{
Daniel Veillard416589a2003-02-17 17:25:42 +00003566 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003567 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003568 xmlRelaxNGTypeLibraryPtr lib;
3569 xmlChar *type;
3570 xmlChar *library;
3571 xmlNodePtr content;
3572 int tmp;
3573
3574 type = xmlGetProp(node, BAD_CAST "type");
3575 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003576 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3577 NULL);
3578 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003579 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003580 xmlRelaxNGNormExtSpace(type);
3581 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003582 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3583 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003584 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003585 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3586 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003587 library =
3588 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003589
Daniel Veillardfd573f12003-03-16 17:52:32 +00003590 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003591 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003592 xmlFree(type);
3593 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003594 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003595 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003596 def->name = type;
3597 def->ns = library;
3598
3599 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003600 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003601 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003602 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3603 "Use of unregistered type library '%s'\n", library,
3604 NULL);
3605 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003606 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003607 def->data = lib;
3608 if (lib->have == NULL) {
3609 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3610 "Internal error with type library '%s': no 'have'\n",
3611 library, NULL);
3612 } else {
3613 tmp = lib->have(lib->data, def->name);
3614 if (tmp != 1) {
3615 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3616 "Error type '%s' is not exported by type library '%s'\n",
3617 def->name, library);
3618 } else
3619 if ((xmlStrEqual
3620 (library,
3621 BAD_CAST
3622 "http://www.w3.org/2001/XMLSchema-datatypes"))
3623 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3624 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3625 ctxt->idref = 1;
3626 }
3627 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003628 }
3629 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003630
3631 /*
3632 * Handle optional params
3633 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003634 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003635 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3636 break;
3637 if (xmlStrEqual(library,
3638 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3639 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3640 "Type library '%s' does not allow type parameters\n",
3641 library, NULL);
3642 content = content->next;
3643 while ((content != NULL) &&
3644 (xmlStrEqual(content->name, BAD_CAST "param")))
3645 content = content->next;
3646 } else {
3647 param = xmlRelaxNGNewDefine(ctxt, node);
3648 if (param != NULL) {
3649 param->type = XML_RELAXNG_PARAM;
3650 param->name = xmlGetProp(content, BAD_CAST "name");
3651 if (param->name == NULL) {
3652 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3653 "param has no name\n", NULL, NULL);
3654 }
3655 param->value = xmlNodeGetContent(content);
3656 if (lastparam == NULL) {
3657 def->attrs = lastparam = param;
3658 } else {
3659 lastparam->next = param;
3660 lastparam = param;
3661 }
3662 if (lib != NULL) {
3663 }
3664 }
3665 content = content->next;
3666 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003667 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003668 /*
3669 * Handle optional except
3670 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003671 if ((content != NULL)
3672 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3673 xmlNodePtr child;
3674 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003675
Daniel Veillard4c004142003-10-07 11:33:24 +00003676 except = xmlRelaxNGNewDefine(ctxt, node);
3677 if (except == NULL) {
3678 return (def);
3679 }
3680 except->type = XML_RELAXNG_EXCEPT;
3681 child = content->children;
3682 if (last == NULL) {
3683 def->content = except;
3684 } else {
3685 last->next = except;
3686 }
3687 if (child == NULL) {
3688 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3689 "except has no content\n", NULL, NULL);
3690 }
3691 while (child != NULL) {
3692 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3693 if (tmp2 != NULL) {
3694 if (last2 == NULL) {
3695 except->content = last2 = tmp2;
3696 } else {
3697 last2->next = tmp2;
3698 last2 = tmp2;
3699 }
3700 }
3701 child = child->next;
3702 }
3703 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003704 }
3705 /*
3706 * Check there is no unhandled data
3707 */
3708 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003709 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3710 "Element data has unexpected content %s\n",
3711 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003712 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003713
Daniel Veillard4c004142003-10-07 11:33:24 +00003714 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003715}
3716
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003717static const xmlChar *invalidName = BAD_CAST "\1";
3718
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003719/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003720 * xmlRelaxNGCompareNameClasses:
3721 * @defs1: the first element/attribute defs
3722 * @defs2: the second element/attribute defs
3723 * @name: the restriction on the name
3724 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003725 *
3726 * Compare the 2 lists of element definitions. The comparison is
3727 * that if both lists do not accept the same QNames, it returns 1
3728 * If the 2 lists can accept the same QName the comparison returns 0
3729 *
3730 * Returns 1 disttinct, 0 if equal
3731 */
3732static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003733xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003734 xmlRelaxNGDefinePtr def2)
3735{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003736 int ret = 1;
3737 xmlNode node;
3738 xmlNs ns;
3739 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003740
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003741 ctxt.flags = FLAGS_IGNORABLE;
3742
Daniel Veillard42f12e92003-03-07 18:32:59 +00003743 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3744
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003745 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003746 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3747 if (def2->type == XML_RELAXNG_TEXT)
3748 return (1);
3749 if (def1->name != NULL) {
3750 node.name = def1->name;
3751 } else {
3752 node.name = invalidName;
3753 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003754 if (def1->ns != NULL) {
3755 if (def1->ns[0] == 0) {
3756 node.ns = NULL;
3757 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003758 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003759 ns.href = def1->ns;
3760 }
3761 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003762 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003763 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003764 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003765 if (def1->nameClass != NULL) {
3766 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3767 } else {
3768 ret = 0;
3769 }
3770 } else {
3771 ret = 1;
3772 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003773 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003774 if (def2->type == XML_RELAXNG_TEXT)
3775 return (0);
3776 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003777 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003778 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003779 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003780 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003781 }
3782 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003783 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003784 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003785 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3786 if (def2->name != NULL) {
3787 node.name = def2->name;
3788 } else {
3789 node.name = invalidName;
3790 }
3791 node.ns = &ns;
3792 if (def2->ns != NULL) {
3793 if (def2->ns[0] == 0) {
3794 node.ns = NULL;
3795 } else {
3796 ns.href = def2->ns;
3797 }
3798 } else {
3799 ns.href = invalidName;
3800 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003801 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003802 if (def2->nameClass != NULL) {
3803 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3804 } else {
3805 ret = 0;
3806 }
3807 } else {
3808 ret = 1;
3809 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003810 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003811 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003812 }
3813
Daniel Veillard4c004142003-10-07 11:33:24 +00003814 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003815}
3816
3817/**
3818 * xmlRelaxNGCompareElemDefLists:
3819 * @ctxt: a Relax-NG parser context
3820 * @defs1: the first list of element/attribute defs
3821 * @defs2: the second list of element/attribute defs
3822 *
3823 * Compare the 2 lists of element or attribute definitions. The comparison
3824 * is that if both lists do not accept the same QNames, it returns 1
3825 * If the 2 lists can accept the same QName the comparison returns 0
3826 *
3827 * Returns 1 disttinct, 0 if equal
3828 */
3829static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003830xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3831 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3832 xmlRelaxNGDefinePtr * def2)
3833{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003834 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003835
Daniel Veillard154877e2003-01-30 12:17:05 +00003836 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003837 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003838 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003839 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003840 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003841 while ((*def2) != NULL) {
3842 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3843 return (0);
3844 def2++;
3845 }
3846 def2 = basedef2;
3847 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003848 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003849 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003850}
3851
3852/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003853 * xmlRelaxNGGenerateAttributes:
3854 * @ctxt: a Relax-NG parser context
3855 * @def: the definition definition
3856 *
3857 * Check if the definition can only generate attributes
3858 *
3859 * Returns 1 if yes, 0 if no and -1 in case of error.
3860 */
3861static int
3862xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003863 xmlRelaxNGDefinePtr def)
3864{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003865 xmlRelaxNGDefinePtr parent, cur, tmp;
3866
3867 /*
3868 * Don't run that check in case of error. Infinite recursion
3869 * becomes possible.
3870 */
3871 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003872 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003873
3874 parent = NULL;
3875 cur = def;
3876 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003877 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3878 (cur->type == XML_RELAXNG_TEXT) ||
3879 (cur->type == XML_RELAXNG_DATATYPE) ||
3880 (cur->type == XML_RELAXNG_PARAM) ||
3881 (cur->type == XML_RELAXNG_LIST) ||
3882 (cur->type == XML_RELAXNG_VALUE) ||
3883 (cur->type == XML_RELAXNG_EMPTY))
3884 return (0);
3885 if ((cur->type == XML_RELAXNG_CHOICE) ||
3886 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3887 (cur->type == XML_RELAXNG_GROUP) ||
3888 (cur->type == XML_RELAXNG_ONEORMORE) ||
3889 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3890 (cur->type == XML_RELAXNG_OPTIONAL) ||
3891 (cur->type == XML_RELAXNG_PARENTREF) ||
3892 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3893 (cur->type == XML_RELAXNG_REF) ||
3894 (cur->type == XML_RELAXNG_DEF)) {
3895 if (cur->content != NULL) {
3896 parent = cur;
3897 cur = cur->content;
3898 tmp = cur;
3899 while (tmp != NULL) {
3900 tmp->parent = parent;
3901 tmp = tmp->next;
3902 }
3903 continue;
3904 }
3905 }
3906 if (cur == def)
3907 break;
3908 if (cur->next != NULL) {
3909 cur = cur->next;
3910 continue;
3911 }
3912 do {
3913 cur = cur->parent;
3914 if (cur == NULL)
3915 break;
3916 if (cur == def)
3917 return (1);
3918 if (cur->next != NULL) {
3919 cur = cur->next;
3920 break;
3921 }
3922 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003923 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003924 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003925}
Daniel Veillard4c004142003-10-07 11:33:24 +00003926
Daniel Veillardce192eb2003-04-16 15:58:05 +00003927/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003928 * xmlRelaxNGGetElements:
3929 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003930 * @def: the definition definition
3931 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003932 *
3933 * Compute the list of top elements a definition can generate
3934 *
3935 * Returns a list of elements or NULL if none was found.
3936 */
3937static xmlRelaxNGDefinePtr *
3938xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003939 xmlRelaxNGDefinePtr def, int eora)
3940{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003941 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003942 int len = 0;
3943 int max = 0;
3944
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003945 /*
3946 * Don't run that check in case of error. Infinite recursion
3947 * becomes possible.
3948 */
3949 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003950 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003951
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003952 parent = NULL;
3953 cur = def;
3954 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003955 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3956 (cur->type == XML_RELAXNG_TEXT))) ||
3957 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3958 if (ret == NULL) {
3959 max = 10;
3960 ret = (xmlRelaxNGDefinePtr *)
3961 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3962 if (ret == NULL) {
3963 xmlRngPErrMemory(ctxt, "getting element list\n");
3964 return (NULL);
3965 }
3966 } else if (max <= len) {
3967 max *= 2;
3968 ret =
3969 xmlRealloc(ret,
3970 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3971 if (ret == NULL) {
3972 xmlRngPErrMemory(ctxt, "getting element list\n");
3973 return (NULL);
3974 }
3975 }
3976 ret[len++] = cur;
3977 ret[len] = NULL;
3978 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3979 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3980 (cur->type == XML_RELAXNG_GROUP) ||
3981 (cur->type == XML_RELAXNG_ONEORMORE) ||
3982 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3983 (cur->type == XML_RELAXNG_OPTIONAL) ||
3984 (cur->type == XML_RELAXNG_PARENTREF) ||
3985 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00003986 (cur->type == XML_RELAXNG_DEF) ||
3987 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003988 /*
3989 * Don't go within elements or attributes or string values.
3990 * Just gather the element top list
3991 */
3992 if (cur->content != NULL) {
3993 parent = cur;
3994 cur = cur->content;
3995 tmp = cur;
3996 while (tmp != NULL) {
3997 tmp->parent = parent;
3998 tmp = tmp->next;
3999 }
4000 continue;
4001 }
4002 }
4003 if (cur == def)
4004 break;
4005 if (cur->next != NULL) {
4006 cur = cur->next;
4007 continue;
4008 }
4009 do {
4010 cur = cur->parent;
4011 if (cur == NULL)
4012 break;
4013 if (cur == def)
4014 return (ret);
4015 if (cur->next != NULL) {
4016 cur = cur->next;
4017 break;
4018 }
4019 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004020 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004021 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004022}
Daniel Veillard4c004142003-10-07 11:33:24 +00004023
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004024/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004025 * xmlRelaxNGCheckChoiceDeterminism:
4026 * @ctxt: a Relax-NG parser context
4027 * @def: the choice definition
4028 *
4029 * Also used to find indeterministic pattern in choice
4030 */
4031static void
4032xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004033 xmlRelaxNGDefinePtr def)
4034{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004035 xmlRelaxNGDefinePtr **list;
4036 xmlRelaxNGDefinePtr cur;
4037 int nbchild = 0, i, j, ret;
4038 int is_nullable = 0;
4039 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004040 xmlHashTablePtr triage = NULL;
4041 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004042
Daniel Veillard4c004142003-10-07 11:33:24 +00004043 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4044 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004045
Daniel Veillarde063f482003-03-21 16:53:17 +00004046 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004047 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004048
Daniel Veillardfd573f12003-03-16 17:52:32 +00004049 /*
4050 * Don't run that check in case of error. Infinite recursion
4051 * becomes possible.
4052 */
4053 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004054 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004055
4056 is_nullable = xmlRelaxNGIsNullable(def);
4057
4058 cur = def->content;
4059 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004060 nbchild++;
4061 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004062 }
4063
4064 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004065 sizeof(xmlRelaxNGDefinePtr
4066 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004067 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004068 xmlRngPErrMemory(ctxt, "building choice\n");
4069 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004070 }
4071 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004072 /*
4073 * a bit strong but safe
4074 */
4075 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004076 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004077 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004078 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004079 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004080 cur = def->content;
4081 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004082 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4083 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4084 is_triable = 0;
4085 } else if (is_triable == 1) {
4086 xmlRelaxNGDefinePtr *tmp;
4087 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004088
Daniel Veillard4c004142003-10-07 11:33:24 +00004089 tmp = list[i];
4090 while ((*tmp != NULL) && (is_triable == 1)) {
4091 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4092 res = xmlHashAddEntry2(triage,
4093 BAD_CAST "#text", NULL,
4094 (void *) cur);
4095 if (res != 0)
4096 is_triable = -1;
4097 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4098 ((*tmp)->name != NULL)) {
4099 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4100 res = xmlHashAddEntry2(triage,
4101 (*tmp)->name, NULL,
4102 (void *) cur);
4103 else
4104 res = xmlHashAddEntry2(triage,
4105 (*tmp)->name, (*tmp)->ns,
4106 (void *) cur);
4107 if (res != 0)
4108 is_triable = -1;
4109 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4110 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4111 res = xmlHashAddEntry2(triage,
4112 BAD_CAST "#any", NULL,
4113 (void *) cur);
4114 else
4115 res = xmlHashAddEntry2(triage,
4116 BAD_CAST "#any", (*tmp)->ns,
4117 (void *) cur);
4118 if (res != 0)
4119 is_triable = -1;
4120 } else {
4121 is_triable = -1;
4122 }
4123 tmp++;
4124 }
4125 }
4126 i++;
4127 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004128 }
4129
Daniel Veillard4c004142003-10-07 11:33:24 +00004130 for (i = 0; i < nbchild; i++) {
4131 if (list[i] == NULL)
4132 continue;
4133 for (j = 0; j < i; j++) {
4134 if (list[j] == NULL)
4135 continue;
4136 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4137 if (ret == 0) {
4138 is_indeterminist = 1;
4139 }
4140 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004141 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004142 for (i = 0; i < nbchild; i++) {
4143 if (list[i] != NULL)
4144 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004145 }
4146
4147 xmlFree(list);
4148 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004149 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004150 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004151 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004152 def->dflags |= IS_TRIABLE;
4153 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004154 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004155 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004156 }
4157 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004158}
4159
4160/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004161 * xmlRelaxNGCheckGroupAttrs:
4162 * @ctxt: a Relax-NG parser context
4163 * @def: the group definition
4164 *
4165 * Detects violations of rule 7.3
4166 */
4167static void
4168xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004169 xmlRelaxNGDefinePtr def)
4170{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004171 xmlRelaxNGDefinePtr **list;
4172 xmlRelaxNGDefinePtr cur;
4173 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004174
4175 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004176 ((def->type != XML_RELAXNG_GROUP) &&
4177 (def->type != XML_RELAXNG_ELEMENT)))
4178 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004179
Daniel Veillarde063f482003-03-21 16:53:17 +00004180 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004181 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004182
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004183 /*
4184 * Don't run that check in case of error. Infinite recursion
4185 * becomes possible.
4186 */
4187 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004188 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004189
Daniel Veillardfd573f12003-03-16 17:52:32 +00004190 cur = def->attrs;
4191 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004192 nbchild++;
4193 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004194 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004195 cur = def->content;
4196 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004197 nbchild++;
4198 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004199 }
4200
4201 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004202 sizeof(xmlRelaxNGDefinePtr
4203 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004204 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004205 xmlRngPErrMemory(ctxt, "building group\n");
4206 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004207 }
4208 i = 0;
4209 cur = def->attrs;
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 cur = def->content;
4216 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004217 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4218 i++;
4219 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004220 }
4221
Daniel Veillard4c004142003-10-07 11:33:24 +00004222 for (i = 0; i < nbchild; i++) {
4223 if (list[i] == NULL)
4224 continue;
4225 for (j = 0; j < i; j++) {
4226 if (list[j] == NULL)
4227 continue;
4228 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4229 if (ret == 0) {
4230 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4231 "Attributes conflicts in group\n", NULL, NULL);
4232 }
4233 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004234 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004235 for (i = 0; i < nbchild; i++) {
4236 if (list[i] != NULL)
4237 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004238 }
4239
4240 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004241 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004242}
4243
4244/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004245 * xmlRelaxNGComputeInterleaves:
4246 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004247 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004248 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004249 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004250 * A lot of work for preprocessing interleave definitions
4251 * is potentially needed to get a decent execution speed at runtime
4252 * - trying to get a total order on the element nodes generated
4253 * by the interleaves, order the list of interleave definitions
4254 * following that order.
4255 * - if <text/> is used to handle mixed content, it is better to
4256 * flag this in the define and simplify the runtime checking
4257 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004258 */
4259static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004260xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004261 xmlRelaxNGParserCtxtPtr ctxt,
4262 xmlChar * name ATTRIBUTE_UNUSED)
4263{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004264 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004265
Daniel Veillardfd573f12003-03-16 17:52:32 +00004266 xmlRelaxNGPartitionPtr partitions = NULL;
4267 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4268 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004269 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004270 int nbgroups = 0;
4271 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004272 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004273 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004274
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004275 /*
4276 * Don't run that check in case of error. Infinite recursion
4277 * becomes possible.
4278 */
4279 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004280 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004281
Daniel Veillardfd573f12003-03-16 17:52:32 +00004282#ifdef DEBUG_INTERLEAVE
4283 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004284 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004285#endif
4286 cur = def->content;
4287 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004288 nbchild++;
4289 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004290 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004291
Daniel Veillardfd573f12003-03-16 17:52:32 +00004292#ifdef DEBUG_INTERLEAVE
4293 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4294#endif
4295 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004296 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004297 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004298 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004299 cur = def->content;
4300 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004301 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4302 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4303 if (groups[nbgroups] == NULL)
4304 goto error;
4305 if (cur->type == XML_RELAXNG_TEXT)
4306 is_mixed++;
4307 groups[nbgroups]->rule = cur;
4308 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4309 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4310 nbgroups++;
4311 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004312 }
4313#ifdef DEBUG_INTERLEAVE
4314 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4315#endif
4316
4317 /*
4318 * Let's check that all rules makes a partitions according to 7.4
4319 */
4320 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004321 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004322 if (partitions == NULL)
4323 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004324 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004325 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004326 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004327 for (i = 0; i < nbgroups; i++) {
4328 group = groups[i];
4329 for (j = i + 1; j < nbgroups; j++) {
4330 if (groups[j] == NULL)
4331 continue;
4332
4333 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4334 groups[j]->defs);
4335 if (ret == 0) {
4336 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4337 "Element or text conflicts in interleave\n",
4338 NULL, NULL);
4339 }
4340 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4341 groups[j]->attrs);
4342 if (ret == 0) {
4343 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4344 "Attributes conflicts in interleave\n", NULL,
4345 NULL);
4346 }
4347 }
4348 tmp = group->defs;
4349 if ((tmp != NULL) && (*tmp != NULL)) {
4350 while (*tmp != NULL) {
4351 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4352 res = xmlHashAddEntry2(partitions->triage,
4353 BAD_CAST "#text", NULL,
4354 (void *) (long) (i + 1));
4355 if (res != 0)
4356 is_determinist = -1;
4357 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4358 ((*tmp)->name != NULL)) {
4359 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4360 res = xmlHashAddEntry2(partitions->triage,
4361 (*tmp)->name, NULL,
4362 (void *) (long) (i + 1));
4363 else
4364 res = xmlHashAddEntry2(partitions->triage,
4365 (*tmp)->name, (*tmp)->ns,
4366 (void *) (long) (i + 1));
4367 if (res != 0)
4368 is_determinist = -1;
4369 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4370 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4371 res = xmlHashAddEntry2(partitions->triage,
4372 BAD_CAST "#any", NULL,
4373 (void *) (long) (i + 1));
4374 else
4375 res = xmlHashAddEntry2(partitions->triage,
4376 BAD_CAST "#any", (*tmp)->ns,
4377 (void *) (long) (i + 1));
4378 if ((*tmp)->nameClass != NULL)
4379 is_determinist = 2;
4380 if (res != 0)
4381 is_determinist = -1;
4382 } else {
4383 is_determinist = -1;
4384 }
4385 tmp++;
4386 }
4387 } else {
4388 is_determinist = 0;
4389 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004390 }
4391 partitions->groups = groups;
4392
4393 /*
4394 * and save the partition list back in the def
4395 */
4396 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004397 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004398 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004399 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004400 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004401 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004402 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004403 return;
4404
Daniel Veillard4c004142003-10-07 11:33:24 +00004405 error:
4406 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004407 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004408 for (i = 0; i < nbgroups; i++)
4409 if (groups[i] != NULL) {
4410 if (groups[i]->defs != NULL)
4411 xmlFree(groups[i]->defs);
4412 xmlFree(groups[i]);
4413 }
4414 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004415 }
4416 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004417}
4418
4419/**
4420 * xmlRelaxNGParseInterleave:
4421 * @ctxt: a Relax-NG parser context
4422 * @node: the data node.
4423 *
4424 * parse the content of a RelaxNG interleave node.
4425 *
4426 * Returns the definition pointer or NULL in case of error
4427 */
4428static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004429xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4430{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004431 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004432 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004433 xmlNodePtr child;
4434
Daniel Veillardfd573f12003-03-16 17:52:32 +00004435 def = xmlRelaxNGNewDefine(ctxt, node);
4436 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004437 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004438 }
4439 def->type = XML_RELAXNG_INTERLEAVE;
4440
4441 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004442 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004443 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004444 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004445 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004446 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004447
Daniel Veillard4c004142003-10-07 11:33:24 +00004448 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4449 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4450 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4451 "Failed to add %s to hash table\n",
4452 (const xmlChar *) name, NULL);
4453 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004454 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004455 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004456 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004457 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4458 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004459 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004460 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004461 if (IS_RELAXNG(child, "element")) {
4462 cur = xmlRelaxNGParseElement(ctxt, child);
4463 } else {
4464 cur = xmlRelaxNGParsePattern(ctxt, child);
4465 }
4466 if (cur != NULL) {
4467 cur->parent = def;
4468 if (last == NULL) {
4469 def->content = last = cur;
4470 } else {
4471 last->next = cur;
4472 last = cur;
4473 }
4474 }
4475 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004476 }
4477
Daniel Veillard4c004142003-10-07 11:33:24 +00004478 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004479}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004480
4481/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004482 * xmlRelaxNGParseInclude:
4483 * @ctxt: a Relax-NG parser context
4484 * @node: the include node
4485 *
4486 * Integrate the content of an include node in the current grammar
4487 *
4488 * Returns 0 in case of success or -1 in case of error
4489 */
4490static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004491xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4492{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004493 xmlRelaxNGIncludePtr incl;
4494 xmlNodePtr root;
4495 int ret = 0, tmp;
4496
Daniel Veillard807daf82004-02-22 22:13:27 +00004497 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004498 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004499 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4500 "Include node has no data\n", NULL, NULL);
4501 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004502 }
4503 root = xmlDocGetRootElement(incl->doc);
4504 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004505 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4506 NULL, NULL);
4507 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004508 }
4509 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004510 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4511 "Include document root is not a grammar\n", NULL, NULL);
4512 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004513 }
4514
4515 /*
4516 * Merge the definition from both the include and the internal list
4517 */
4518 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004519 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4520 if (tmp != 0)
4521 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004522 }
4523 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004524 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4525 if (tmp != 0)
4526 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004527 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004528 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004529}
4530
4531/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004532 * xmlRelaxNGParseDefine:
4533 * @ctxt: a Relax-NG parser context
4534 * @node: the define node
4535 *
4536 * parse the content of a RelaxNG define element node.
4537 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004538 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004539 */
4540static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004541xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4542{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004543 xmlChar *name;
4544 int ret = 0, tmp;
4545 xmlRelaxNGDefinePtr def;
4546 const xmlChar *olddefine;
4547
4548 name = xmlGetProp(node, BAD_CAST "name");
4549 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004550 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4551 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004552 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004553 xmlRelaxNGNormExtSpace(name);
4554 if (xmlValidateNCName(name, 0)) {
4555 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4556 "define name '%s' is not an NCName\n", name, NULL);
4557 }
4558 def = xmlRelaxNGNewDefine(ctxt, node);
4559 if (def == NULL) {
4560 xmlFree(name);
4561 return (-1);
4562 }
4563 def->type = XML_RELAXNG_DEF;
4564 def->name = name;
4565 if (node->children == NULL) {
4566 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4567 "define has no children\n", NULL, NULL);
4568 } else {
4569 olddefine = ctxt->define;
4570 ctxt->define = name;
4571 def->content =
4572 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4573 ctxt->define = olddefine;
4574 }
4575 if (ctxt->grammar->defs == NULL)
4576 ctxt->grammar->defs = xmlHashCreate(10);
4577 if (ctxt->grammar->defs == NULL) {
4578 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4579 "Could not create definition hash\n", NULL, NULL);
4580 ret = -1;
4581 } else {
4582 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4583 if (tmp < 0) {
4584 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004585
Daniel Veillard4c004142003-10-07 11:33:24 +00004586 prev = xmlHashLookup(ctxt->grammar->defs, name);
4587 if (prev == NULL) {
4588 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4589 "Internal error on define aggregation of %s\n",
4590 name, NULL);
4591 ret = -1;
4592 } else {
4593 while (prev->nextHash != NULL)
4594 prev = prev->nextHash;
4595 prev->nextHash = def;
4596 }
4597 }
4598 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004599 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004600 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004601}
4602
4603/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004604 * xmlRelaxNGProcessExternalRef:
4605 * @ctxt: the parser context
4606 * @node: the externlRef node
4607 *
4608 * Process and compile an externlRef node
4609 *
4610 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4611 */
4612static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004613xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4614{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004615 xmlRelaxNGDocumentPtr docu;
4616 xmlNodePtr root, tmp;
4617 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004618 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004619 xmlRelaxNGDefinePtr def;
4620
Daniel Veillard807daf82004-02-22 22:13:27 +00004621 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004622 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004623 def = xmlRelaxNGNewDefine(ctxt, node);
4624 if (def == NULL)
4625 return (NULL);
4626 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004627
Daniel Veillard4c004142003-10-07 11:33:24 +00004628 if (docu->content == NULL) {
4629 /*
4630 * Then do the parsing for good
4631 */
4632 root = xmlDocGetRootElement(docu->doc);
4633 if (root == NULL) {
4634 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4635 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4636 NULL);
4637 return (NULL);
4638 }
4639 /*
4640 * ns transmission rules
4641 */
4642 ns = xmlGetProp(root, BAD_CAST "ns");
4643 if (ns == NULL) {
4644 tmp = node;
4645 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4646 ns = xmlGetProp(tmp, BAD_CAST "ns");
4647 if (ns != NULL) {
4648 break;
4649 }
4650 tmp = tmp->parent;
4651 }
4652 if (ns != NULL) {
4653 xmlSetProp(root, BAD_CAST "ns", ns);
4654 newNs = 1;
4655 xmlFree(ns);
4656 }
4657 } else {
4658 xmlFree(ns);
4659 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004660
Daniel Veillard4c004142003-10-07 11:33:24 +00004661 /*
4662 * Parsing to get a precompiled schemas.
4663 */
4664 oldflags = ctxt->flags;
4665 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4666 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4667 ctxt->flags = oldflags;
4668 if ((docu->schema != NULL) &&
4669 (docu->schema->topgrammar != NULL)) {
4670 docu->content = docu->schema->topgrammar->start;
4671 }
4672
4673 /*
4674 * the externalRef may be reused in a different ns context
4675 */
4676 if (newNs == 1) {
4677 xmlUnsetProp(root, BAD_CAST "ns");
4678 }
4679 }
4680 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004681 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004682 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004683 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004684 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004685}
4686
4687/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004688 * xmlRelaxNGParsePattern:
4689 * @ctxt: a Relax-NG parser context
4690 * @node: the pattern node.
4691 *
4692 * parse the content of a RelaxNG pattern node.
4693 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004694 * Returns the definition pointer or NULL in case of error or if no
4695 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004696 */
4697static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004698xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4699{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004700 xmlRelaxNGDefinePtr def = NULL;
4701
Daniel Veillardd2298792003-02-14 16:54:11 +00004702 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004703 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004704 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004705 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004706 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004707 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004708 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004709 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004710 def = xmlRelaxNGNewDefine(ctxt, node);
4711 if (def == NULL)
4712 return (NULL);
4713 def->type = XML_RELAXNG_EMPTY;
4714 if (node->children != NULL) {
4715 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4716 "empty: had a child node\n", NULL, NULL);
4717 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004718 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004719 def = xmlRelaxNGNewDefine(ctxt, node);
4720 if (def == NULL)
4721 return (NULL);
4722 def->type = XML_RELAXNG_TEXT;
4723 if (node->children != NULL) {
4724 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4725 "text: had a child node\n", NULL, NULL);
4726 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004727 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004728 def = xmlRelaxNGNewDefine(ctxt, node);
4729 if (def == NULL)
4730 return (NULL);
4731 def->type = XML_RELAXNG_ZEROORMORE;
4732 if (node->children == NULL) {
4733 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4734 "Element %s is empty\n", node->name, NULL);
4735 } else {
4736 def->content =
4737 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4738 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004739 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004740 def = xmlRelaxNGNewDefine(ctxt, node);
4741 if (def == NULL)
4742 return (NULL);
4743 def->type = XML_RELAXNG_ONEORMORE;
4744 if (node->children == NULL) {
4745 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4746 "Element %s is empty\n", node->name, NULL);
4747 } else {
4748 def->content =
4749 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4750 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004751 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004752 def = xmlRelaxNGNewDefine(ctxt, node);
4753 if (def == NULL)
4754 return (NULL);
4755 def->type = XML_RELAXNG_OPTIONAL;
4756 if (node->children == NULL) {
4757 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4758 "Element %s is empty\n", node->name, NULL);
4759 } else {
4760 def->content =
4761 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4762 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004763 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004764 def = xmlRelaxNGNewDefine(ctxt, node);
4765 if (def == NULL)
4766 return (NULL);
4767 def->type = XML_RELAXNG_CHOICE;
4768 if (node->children == NULL) {
4769 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4770 "Element %s is empty\n", node->name, NULL);
4771 } else {
4772 def->content =
4773 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4774 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004775 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004776 def = xmlRelaxNGNewDefine(ctxt, node);
4777 if (def == NULL)
4778 return (NULL);
4779 def->type = XML_RELAXNG_GROUP;
4780 if (node->children == NULL) {
4781 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4782 "Element %s is empty\n", node->name, NULL);
4783 } else {
4784 def->content =
4785 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4786 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004787 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004788 def = xmlRelaxNGNewDefine(ctxt, node);
4789 if (def == NULL)
4790 return (NULL);
4791 def->type = XML_RELAXNG_REF;
4792 def->name = xmlGetProp(node, BAD_CAST "name");
4793 if (def->name == NULL) {
4794 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4795 NULL, NULL);
4796 } else {
4797 xmlRelaxNGNormExtSpace(def->name);
4798 if (xmlValidateNCName(def->name, 0)) {
4799 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4800 "ref name '%s' is not an NCName\n", def->name,
4801 NULL);
4802 }
4803 }
4804 if (node->children != NULL) {
4805 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4806 NULL, NULL);
4807 }
4808 if (ctxt->grammar->refs == NULL)
4809 ctxt->grammar->refs = xmlHashCreate(10);
4810 if (ctxt->grammar->refs == NULL) {
4811 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4812 "Could not create references hash\n", NULL, NULL);
4813 def = NULL;
4814 } else {
4815 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004816
Daniel Veillard4c004142003-10-07 11:33:24 +00004817 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4818 if (tmp < 0) {
4819 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004820
Daniel Veillard4c004142003-10-07 11:33:24 +00004821 prev = (xmlRelaxNGDefinePtr)
4822 xmlHashLookup(ctxt->grammar->refs, def->name);
4823 if (prev == NULL) {
4824 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004825 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4826 "Error refs definitions '%s'\n",
4827 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004828 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004829 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4830 "Error refs definitions\n",
4831 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004832 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004833 def = NULL;
4834 } else {
4835 def->nextHash = prev->nextHash;
4836 prev->nextHash = def;
4837 }
4838 }
4839 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004840 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004841 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004842 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004843 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004844 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004845 def = xmlRelaxNGNewDefine(ctxt, node);
4846 if (def == NULL)
4847 return (NULL);
4848 def->type = XML_RELAXNG_LIST;
4849 if (node->children == NULL) {
4850 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4851 "Element %s is empty\n", node->name, NULL);
4852 } else {
4853 def->content =
4854 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4855 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004856 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004857 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004858 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004859 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004860 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004861 def = xmlRelaxNGNewDefine(ctxt, node);
4862 if (def == NULL)
4863 return (NULL);
4864 def->type = XML_RELAXNG_NOT_ALLOWED;
4865 if (node->children != NULL) {
4866 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4867 "xmlRelaxNGParse: notAllowed element is not empty\n",
4868 NULL, NULL);
4869 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004870 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004871 xmlRelaxNGGrammarPtr grammar, old;
4872 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004873
Daniel Veillardc482e262003-02-26 14:48:48 +00004874#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004875 xmlGenericError(xmlGenericErrorContext,
4876 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004877#endif
4878
Daniel Veillard4c004142003-10-07 11:33:24 +00004879 oldparent = ctxt->parentgrammar;
4880 old = ctxt->grammar;
4881 ctxt->parentgrammar = old;
4882 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4883 if (old != NULL) {
4884 ctxt->grammar = old;
4885 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004886#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004887 if (grammar != NULL) {
4888 grammar->next = old->next;
4889 old->next = grammar;
4890 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004891#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004892 }
4893 if (grammar != NULL)
4894 def = grammar->start;
4895 else
4896 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004897 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004898 if (ctxt->parentgrammar == NULL) {
4899 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4900 "Use of parentRef without a parent grammar\n", NULL,
4901 NULL);
4902 return (NULL);
4903 }
4904 def = xmlRelaxNGNewDefine(ctxt, node);
4905 if (def == NULL)
4906 return (NULL);
4907 def->type = XML_RELAXNG_PARENTREF;
4908 def->name = xmlGetProp(node, BAD_CAST "name");
4909 if (def->name == NULL) {
4910 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4911 "parentRef has no name\n", NULL, NULL);
4912 } else {
4913 xmlRelaxNGNormExtSpace(def->name);
4914 if (xmlValidateNCName(def->name, 0)) {
4915 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4916 "parentRef name '%s' is not an NCName\n",
4917 def->name, NULL);
4918 }
4919 }
4920 if (node->children != NULL) {
4921 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4922 "parentRef is not empty\n", NULL, NULL);
4923 }
4924 if (ctxt->parentgrammar->refs == NULL)
4925 ctxt->parentgrammar->refs = xmlHashCreate(10);
4926 if (ctxt->parentgrammar->refs == NULL) {
4927 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4928 "Could not create references hash\n", NULL, NULL);
4929 def = NULL;
4930 } else if (def->name != NULL) {
4931 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00004932
Daniel Veillard4c004142003-10-07 11:33:24 +00004933 tmp =
4934 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4935 if (tmp < 0) {
4936 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00004937
Daniel Veillard4c004142003-10-07 11:33:24 +00004938 prev = (xmlRelaxNGDefinePtr)
4939 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4940 if (prev == NULL) {
4941 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4942 "Internal error parentRef definitions '%s'\n",
4943 def->name, NULL);
4944 def = NULL;
4945 } else {
4946 def->nextHash = prev->nextHash;
4947 prev->nextHash = def;
4948 }
4949 }
4950 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004951 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004952 if (node->children == NULL) {
4953 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4954 NULL, NULL);
4955 def = NULL;
4956 } else {
4957 def = xmlRelaxNGParseInterleave(ctxt, node);
4958 if (def != NULL) {
4959 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004960
Daniel Veillard4c004142003-10-07 11:33:24 +00004961 if ((def->content != NULL) && (def->content->next != NULL)) {
4962 tmp = xmlRelaxNGNewDefine(ctxt, node);
4963 if (tmp != NULL) {
4964 tmp->type = XML_RELAXNG_GROUP;
4965 tmp->content = def->content;
4966 def->content = tmp;
4967 }
4968 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004969
Daniel Veillard4c004142003-10-07 11:33:24 +00004970 tmp = xmlRelaxNGNewDefine(ctxt, node);
4971 if (tmp == NULL)
4972 return (def);
4973 tmp->type = XML_RELAXNG_TEXT;
4974 tmp->next = def->content;
4975 def->content = tmp;
4976 }
4977 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004978 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004979 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4980 "Unexpected node %s is not a pattern\n", node->name,
4981 NULL);
4982 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004983 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004984 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004985}
4986
4987/**
4988 * xmlRelaxNGParseAttribute:
4989 * @ctxt: a Relax-NG parser context
4990 * @node: the element node
4991 *
4992 * parse the content of a RelaxNG attribute node.
4993 *
4994 * Returns the definition pointer or NULL in case of error.
4995 */
4996static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004997xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4998{
Daniel Veillardd2298792003-02-14 16:54:11 +00004999 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005000 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005001 int old_flags;
5002
Daniel Veillardfd573f12003-03-16 17:52:32 +00005003 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005004 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005005 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005006 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005007 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005008 child = node->children;
5009 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005010 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5011 "xmlRelaxNGParseattribute: attribute has no children\n",
5012 NULL, NULL);
5013 return (ret);
5014 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005015 old_flags = ctxt->flags;
5016 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005017 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5018 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005019 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005020
Daniel Veillardd2298792003-02-14 16:54:11 +00005021 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005022 cur = xmlRelaxNGParsePattern(ctxt, child);
5023 if (cur != NULL) {
5024 switch (cur->type) {
5025 case XML_RELAXNG_EMPTY:
5026 case XML_RELAXNG_NOT_ALLOWED:
5027 case XML_RELAXNG_TEXT:
5028 case XML_RELAXNG_ELEMENT:
5029 case XML_RELAXNG_DATATYPE:
5030 case XML_RELAXNG_VALUE:
5031 case XML_RELAXNG_LIST:
5032 case XML_RELAXNG_REF:
5033 case XML_RELAXNG_PARENTREF:
5034 case XML_RELAXNG_EXTERNALREF:
5035 case XML_RELAXNG_DEF:
5036 case XML_RELAXNG_ONEORMORE:
5037 case XML_RELAXNG_ZEROORMORE:
5038 case XML_RELAXNG_OPTIONAL:
5039 case XML_RELAXNG_CHOICE:
5040 case XML_RELAXNG_GROUP:
5041 case XML_RELAXNG_INTERLEAVE:
5042 case XML_RELAXNG_ATTRIBUTE:
5043 ret->content = cur;
5044 cur->parent = ret;
5045 break;
5046 case XML_RELAXNG_START:
5047 case XML_RELAXNG_PARAM:
5048 case XML_RELAXNG_EXCEPT:
5049 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5050 "attribute has invalid content\n", NULL,
5051 NULL);
5052 break;
5053 case XML_RELAXNG_NOOP:
5054 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5055 "RNG Internal error, noop found in attribute\n",
5056 NULL, NULL);
5057 break;
5058 }
5059 }
5060 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005061 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005062 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005063 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5064 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005065 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005066 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005067 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005068}
5069
5070/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005071 * xmlRelaxNGParseExceptNameClass:
5072 * @ctxt: a Relax-NG parser context
5073 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005074 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005075 *
5076 * parse the content of a RelaxNG nameClass node.
5077 *
5078 * Returns the definition pointer or NULL in case of error.
5079 */
5080static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005081xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005082 xmlNodePtr node, int attr)
5083{
Daniel Veillard144fae12003-02-03 13:17:57 +00005084 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5085 xmlNodePtr child;
5086
Daniel Veillardd2298792003-02-14 16:54:11 +00005087 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005088 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5089 "Expecting an except node\n", NULL, NULL);
5090 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005091 }
5092 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005093 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5094 "exceptNameClass allows only a single except node\n",
5095 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005096 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005097 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005098 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5099 NULL, NULL);
5100 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005101 }
5102
Daniel Veillardfd573f12003-03-16 17:52:32 +00005103 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005104 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005105 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005106 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005107 child = node->children;
5108 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005109 cur = xmlRelaxNGNewDefine(ctxt, child);
5110 if (cur == NULL)
5111 break;
5112 if (attr)
5113 cur->type = XML_RELAXNG_ATTRIBUTE;
5114 else
5115 cur->type = XML_RELAXNG_ELEMENT;
5116
Daniel Veillard419a7682003-02-03 23:22:49 +00005117 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005118 if (last == NULL) {
5119 ret->content = cur;
5120 } else {
5121 last->next = cur;
5122 }
5123 last = cur;
5124 }
5125 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005126 }
5127
Daniel Veillard4c004142003-10-07 11:33:24 +00005128 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005129}
5130
5131/**
5132 * xmlRelaxNGParseNameClass:
5133 * @ctxt: a Relax-NG parser context
5134 * @node: the nameClass node
5135 * @def: the current definition
5136 *
5137 * parse the content of a RelaxNG nameClass node.
5138 *
5139 * Returns the definition pointer or NULL in case of error.
5140 */
5141static xmlRelaxNGDefinePtr
5142xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005143 xmlRelaxNGDefinePtr def)
5144{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005145 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005146 xmlChar *val;
5147
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005148 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005149 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005150 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005151 if ((def->type != XML_RELAXNG_ELEMENT) &&
5152 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5153 ret = xmlRelaxNGNewDefine(ctxt, node);
5154 if (ret == NULL)
5155 return (NULL);
5156 ret->parent = def;
5157 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5158 ret->type = XML_RELAXNG_ATTRIBUTE;
5159 else
5160 ret->type = XML_RELAXNG_ELEMENT;
5161 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005162 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005163 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005164 val = xmlNodeGetContent(node);
5165 xmlRelaxNGNormExtSpace(val);
5166 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005167 if (node->parent != NULL)
5168 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5169 "Element %s name '%s' is not an NCName\n",
5170 node->parent->name, val);
5171 else
5172 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5173 "name '%s' is not an NCName\n",
5174 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005175 }
5176 ret->name = val;
5177 val = xmlGetProp(node, BAD_CAST "ns");
5178 ret->ns = val;
5179 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5180 (val != NULL) &&
5181 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005182 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005183 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005184 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005185 }
5186 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5187 (val != NULL) &&
5188 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005189 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5190 "Attribute with QName 'xmlns' is not allowed\n",
5191 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005192 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005193 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005194 ret->name = NULL;
5195 ret->ns = NULL;
5196 if (node->children != NULL) {
5197 ret->nameClass =
5198 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5199 (def->type ==
5200 XML_RELAXNG_ATTRIBUTE));
5201 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005202 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005203 ret->name = NULL;
5204 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5205 if (ret->ns == NULL) {
5206 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5207 "nsName has no ns attribute\n", NULL, NULL);
5208 }
5209 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5210 (ret->ns != NULL) &&
5211 (xmlStrEqual
5212 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5213 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5214 "Attribute with namespace '%s' is not allowed\n",
5215 ret->ns, NULL);
5216 }
5217 if (node->children != NULL) {
5218 ret->nameClass =
5219 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5220 (def->type ==
5221 XML_RELAXNG_ATTRIBUTE));
5222 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005223 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005224 xmlNodePtr child;
5225 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005226
Daniel Veillard4c004142003-10-07 11:33:24 +00005227 ret = xmlRelaxNGNewDefine(ctxt, node);
5228 if (ret == NULL)
5229 return (NULL);
5230 ret->parent = def;
5231 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005232
Daniel Veillard4c004142003-10-07 11:33:24 +00005233 if (node->children == NULL) {
5234 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5235 "Element choice is empty\n", NULL, NULL);
5236 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005237
Daniel Veillard4c004142003-10-07 11:33:24 +00005238 child = node->children;
5239 while (child != NULL) {
5240 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5241 if (tmp != NULL) {
5242 if (last == NULL) {
5243 last = ret->nameClass = tmp;
5244 } else {
5245 last->next = tmp;
5246 last = tmp;
5247 }
5248 }
5249 child = child->next;
5250 }
5251 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005252 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005253 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5254 "expecting name, anyName, nsName or choice : got %s\n",
5255 node->name, NULL);
5256 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005257 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005258 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005259 if (def->nameClass == NULL) {
5260 def->nameClass = ret;
5261 } else {
5262 tmp = def->nameClass;
5263 while (tmp->next != NULL) {
5264 tmp = tmp->next;
5265 }
5266 tmp->next = ret;
5267 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005268 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005269 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005270}
5271
5272/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005273 * xmlRelaxNGParseElement:
5274 * @ctxt: a Relax-NG parser context
5275 * @node: the element node
5276 *
5277 * parse the content of a RelaxNG element node.
5278 *
5279 * Returns the definition pointer or NULL in case of error.
5280 */
5281static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005282xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5283{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005284 xmlRelaxNGDefinePtr ret, cur, last;
5285 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005286 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005287
Daniel Veillardfd573f12003-03-16 17:52:32 +00005288 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005289 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005290 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005291 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005292 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005293 child = node->children;
5294 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005295 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5296 "xmlRelaxNGParseElement: element has no children\n",
5297 NULL, NULL);
5298 return (ret);
5299 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005300 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5301 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005302 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005303
Daniel Veillard6eadf632003-01-23 18:29:16 +00005304 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005305 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5306 "xmlRelaxNGParseElement: element has no content\n",
5307 NULL, NULL);
5308 return (ret);
5309 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005310 olddefine = ctxt->define;
5311 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005312 last = NULL;
5313 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005314 cur = xmlRelaxNGParsePattern(ctxt, child);
5315 if (cur != NULL) {
5316 cur->parent = ret;
5317 switch (cur->type) {
5318 case XML_RELAXNG_EMPTY:
5319 case XML_RELAXNG_NOT_ALLOWED:
5320 case XML_RELAXNG_TEXT:
5321 case XML_RELAXNG_ELEMENT:
5322 case XML_RELAXNG_DATATYPE:
5323 case XML_RELAXNG_VALUE:
5324 case XML_RELAXNG_LIST:
5325 case XML_RELAXNG_REF:
5326 case XML_RELAXNG_PARENTREF:
5327 case XML_RELAXNG_EXTERNALREF:
5328 case XML_RELAXNG_DEF:
5329 case XML_RELAXNG_ZEROORMORE:
5330 case XML_RELAXNG_ONEORMORE:
5331 case XML_RELAXNG_OPTIONAL:
5332 case XML_RELAXNG_CHOICE:
5333 case XML_RELAXNG_GROUP:
5334 case XML_RELAXNG_INTERLEAVE:
5335 if (last == NULL) {
5336 ret->content = last = cur;
5337 } else {
5338 if ((last->type == XML_RELAXNG_ELEMENT) &&
5339 (ret->content == last)) {
5340 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5341 if (ret->content != NULL) {
5342 ret->content->type = XML_RELAXNG_GROUP;
5343 ret->content->content = last;
5344 } else {
5345 ret->content = last;
5346 }
5347 }
5348 last->next = cur;
5349 last = cur;
5350 }
5351 break;
5352 case XML_RELAXNG_ATTRIBUTE:
5353 cur->next = ret->attrs;
5354 ret->attrs = cur;
5355 break;
5356 case XML_RELAXNG_START:
5357 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5358 "RNG Internal error, start found in element\n",
5359 NULL, NULL);
5360 break;
5361 case XML_RELAXNG_PARAM:
5362 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5363 "RNG Internal error, param found in element\n",
5364 NULL, NULL);
5365 break;
5366 case XML_RELAXNG_EXCEPT:
5367 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5368 "RNG Internal error, except found in element\n",
5369 NULL, NULL);
5370 break;
5371 case XML_RELAXNG_NOOP:
5372 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5373 "RNG Internal error, noop found in element\n",
5374 NULL, NULL);
5375 break;
5376 }
5377 }
5378 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005379 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005380 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005381 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005382}
5383
5384/**
5385 * xmlRelaxNGParsePatterns:
5386 * @ctxt: a Relax-NG parser context
5387 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005388 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005389 *
5390 * parse the content of a RelaxNG start node.
5391 *
5392 * Returns the definition pointer or NULL in case of error.
5393 */
5394static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005395xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005396 int group)
5397{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005398 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005399
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005400 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005401 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005402 if (IS_RELAXNG(nodes, "element")) {
5403 cur = xmlRelaxNGParseElement(ctxt, nodes);
5404 if (def == NULL) {
5405 def = last = cur;
5406 } else {
5407 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5408 (def == last)) {
5409 def = xmlRelaxNGNewDefine(ctxt, nodes);
5410 def->type = XML_RELAXNG_GROUP;
5411 def->content = last;
5412 }
5413 last->next = cur;
5414 last = cur;
5415 }
5416 cur->parent = parent;
5417 } else {
5418 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5419 if (cur != NULL) {
5420 if (def == NULL) {
5421 def = last = cur;
5422 } else {
5423 last->next = cur;
5424 last = cur;
5425 }
5426 }
5427 }
5428 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005429 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005430 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005431}
5432
5433/**
5434 * xmlRelaxNGParseStart:
5435 * @ctxt: a Relax-NG parser context
5436 * @nodes: start children nodes
5437 *
5438 * parse the content of a RelaxNG start node.
5439 *
5440 * Returns 0 in case of success, -1 in case of error
5441 */
5442static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005443xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5444{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005445 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005446 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005447
Daniel Veillardd2298792003-02-14 16:54:11 +00005448 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005449 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5450 NULL, NULL);
5451 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005452 }
5453 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005454 def = xmlRelaxNGNewDefine(ctxt, nodes);
5455 if (def == NULL)
5456 return (-1);
5457 def->type = XML_RELAXNG_EMPTY;
5458 if (nodes->children != NULL) {
5459 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5460 "element empty is not empty\n", NULL, NULL);
5461 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005462 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005463 def = xmlRelaxNGNewDefine(ctxt, nodes);
5464 if (def == NULL)
5465 return (-1);
5466 def->type = XML_RELAXNG_NOT_ALLOWED;
5467 if (nodes->children != NULL) {
5468 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5469 "element notAllowed is not empty\n", NULL, NULL);
5470 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005471 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005472 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005473 }
5474 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005475 last = ctxt->grammar->start;
5476 while (last->next != NULL)
5477 last = last->next;
5478 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005479 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005480 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005481 }
5482 nodes = nodes->next;
5483 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005484 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5485 "start more than one children\n", NULL, NULL);
5486 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005487 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005488 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005489}
5490
5491/**
5492 * xmlRelaxNGParseGrammarContent:
5493 * @ctxt: a Relax-NG parser context
5494 * @nodes: grammar children nodes
5495 *
5496 * parse the content of a RelaxNG grammar node.
5497 *
5498 * Returns 0 in case of success, -1 in case of error
5499 */
5500static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005501xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5502 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005503{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005504 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005505
5506 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005507 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5508 "grammar has no children\n", NULL, NULL);
5509 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005510 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005511 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005512 if (IS_RELAXNG(nodes, "start")) {
5513 if (nodes->children == NULL) {
5514 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5515 "start has no children\n", NULL, NULL);
5516 } else {
5517 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5518 if (tmp != 0)
5519 ret = -1;
5520 }
5521 } else if (IS_RELAXNG(nodes, "define")) {
5522 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5523 if (tmp != 0)
5524 ret = -1;
5525 } else if (IS_RELAXNG(nodes, "include")) {
5526 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5527 if (tmp != 0)
5528 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005529 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005530 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5531 "grammar has unexpected child %s\n", nodes->name,
5532 NULL);
5533 ret = -1;
5534 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005535 nodes = nodes->next;
5536 }
5537 return (ret);
5538}
5539
5540/**
5541 * xmlRelaxNGCheckReference:
5542 * @ref: the ref
5543 * @ctxt: a Relax-NG parser context
5544 * @name: the name associated to the defines
5545 *
5546 * Applies the 4.17. combine attribute rule for all the define
5547 * element of a given grammar using the same name.
5548 */
5549static void
5550xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005551 xmlRelaxNGParserCtxtPtr ctxt,
5552 const xmlChar * name)
5553{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005554 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005555 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005556
5557 grammar = ctxt->grammar;
5558 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005559 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5560 "Internal error: no grammar in CheckReference %s\n",
5561 name, NULL);
5562 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005563 }
5564 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005565 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5566 "Internal error: reference has content in CheckReference %s\n",
5567 name, NULL);
5568 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005569 }
5570 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005571 def = xmlHashLookup(grammar->defs, name);
5572 if (def != NULL) {
5573 cur = ref;
5574 while (cur != NULL) {
5575 cur->content = def;
5576 cur = cur->nextHash;
5577 }
5578 } else {
5579 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5580 "Reference %s has no matching definition\n", name,
5581 NULL);
5582 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005583 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005584 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5585 "Reference %s has no matching definition\n", name,
5586 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005587 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005588}
5589
5590/**
5591 * xmlRelaxNGCheckCombine:
5592 * @define: the define(s) list
5593 * @ctxt: a Relax-NG parser context
5594 * @name: the name associated to the defines
5595 *
5596 * Applies the 4.17. combine attribute rule for all the define
5597 * element of a given grammar using the same name.
5598 */
5599static void
5600xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005601 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5602{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005603 xmlChar *combine;
5604 int choiceOrInterleave = -1;
5605 int missing = 0;
5606 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5607
5608 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005609 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005610 cur = define;
5611 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005612 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5613 if (combine != NULL) {
5614 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5615 if (choiceOrInterleave == -1)
5616 choiceOrInterleave = 1;
5617 else if (choiceOrInterleave == 0) {
5618 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5619 "Defines for %s use both 'choice' and 'interleave'\n",
5620 name, NULL);
5621 }
5622 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5623 if (choiceOrInterleave == -1)
5624 choiceOrInterleave = 0;
5625 else if (choiceOrInterleave == 1) {
5626 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5627 "Defines for %s use both 'choice' and 'interleave'\n",
5628 name, NULL);
5629 }
5630 } else {
5631 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5632 "Defines for %s use unknown combine value '%s''\n",
5633 name, combine);
5634 }
5635 xmlFree(combine);
5636 } else {
5637 if (missing == 0)
5638 missing = 1;
5639 else {
5640 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5641 "Some defines for %s needs the combine attribute\n",
5642 name, NULL);
5643 }
5644 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005645
Daniel Veillard4c004142003-10-07 11:33:24 +00005646 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005647 }
5648#ifdef DEBUG
5649 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005650 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5651 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005652#endif
5653 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005654 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005655 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005656 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005657 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005658 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005659 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005660 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005661 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005662 tmp = define;
5663 last = NULL;
5664 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005665 if (tmp->content != NULL) {
5666 if (tmp->content->next != NULL) {
5667 /*
5668 * we need first to create a wrapper.
5669 */
5670 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5671 if (tmp2 == NULL)
5672 break;
5673 tmp2->type = XML_RELAXNG_GROUP;
5674 tmp2->content = tmp->content;
5675 } else {
5676 tmp2 = tmp->content;
5677 }
5678 if (last == NULL) {
5679 cur->content = tmp2;
5680 } else {
5681 last->next = tmp2;
5682 }
5683 last = tmp2;
5684 }
5685 tmp->content = cur;
5686 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005687 }
5688 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005689 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005690 if (ctxt->interleaves == NULL)
5691 ctxt->interleaves = xmlHashCreate(10);
5692 if (ctxt->interleaves == NULL) {
5693 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5694 "Failed to create interleaves hash table\n", NULL,
5695 NULL);
5696 } else {
5697 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005698
Daniel Veillard4c004142003-10-07 11:33:24 +00005699 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5700 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5701 0) {
5702 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5703 "Failed to add %s to hash table\n",
5704 (const xmlChar *) tmpname, NULL);
5705 }
5706 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005707 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005708}
5709
5710/**
5711 * xmlRelaxNGCombineStart:
5712 * @ctxt: a Relax-NG parser context
5713 * @grammar: the grammar
5714 *
5715 * Applies the 4.17. combine rule for all the start
5716 * element of a given grammar.
5717 */
5718static void
5719xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005720 xmlRelaxNGGrammarPtr grammar)
5721{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005722 xmlRelaxNGDefinePtr starts;
5723 xmlChar *combine;
5724 int choiceOrInterleave = -1;
5725 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005726 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005727
Daniel Veillard2df2de22003-02-17 23:34:33 +00005728 starts = grammar->start;
5729 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005730 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005731 cur = starts;
5732 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005733 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5734 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5735 combine = NULL;
5736 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5737 "Internal error: start element not found\n", NULL,
5738 NULL);
5739 } else {
5740 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5741 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005742
Daniel Veillard4c004142003-10-07 11:33:24 +00005743 if (combine != NULL) {
5744 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5745 if (choiceOrInterleave == -1)
5746 choiceOrInterleave = 1;
5747 else if (choiceOrInterleave == 0) {
5748 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5749 "<start> use both 'choice' and 'interleave'\n",
5750 NULL, NULL);
5751 }
5752 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5753 if (choiceOrInterleave == -1)
5754 choiceOrInterleave = 0;
5755 else if (choiceOrInterleave == 1) {
5756 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5757 "<start> use both 'choice' and 'interleave'\n",
5758 NULL, NULL);
5759 }
5760 } else {
5761 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5762 "<start> uses unknown combine value '%s''\n",
5763 combine, NULL);
5764 }
5765 xmlFree(combine);
5766 } else {
5767 if (missing == 0)
5768 missing = 1;
5769 else {
5770 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5771 "Some <start> element miss the combine attribute\n",
5772 NULL, NULL);
5773 }
5774 }
5775
5776 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005777 }
5778#ifdef DEBUG
5779 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005780 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5781 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005782#endif
5783 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005784 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005785 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005786 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005787 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005788 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005789 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005790 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005791 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005792 cur->content = grammar->start;
5793 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005794 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005795 if (ctxt->interleaves == NULL)
5796 ctxt->interleaves = xmlHashCreate(10);
5797 if (ctxt->interleaves == NULL) {
5798 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5799 "Failed to create interleaves hash table\n", NULL,
5800 NULL);
5801 } else {
5802 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005803
Daniel Veillard4c004142003-10-07 11:33:24 +00005804 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5805 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5806 0) {
5807 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5808 "Failed to add %s to hash table\n",
5809 (const xmlChar *) tmpname, NULL);
5810 }
5811 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005812 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005813}
5814
5815/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005816 * xmlRelaxNGCheckCycles:
5817 * @ctxt: a Relax-NG parser context
5818 * @nodes: grammar children nodes
5819 * @depth: the counter
5820 *
5821 * Check for cycles.
5822 *
5823 * Returns 0 if check passed, and -1 in case of error
5824 */
5825static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005826xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5827 xmlRelaxNGDefinePtr cur, int depth)
5828{
Daniel Veillardd4310742003-02-18 21:12:46 +00005829 int ret = 0;
5830
5831 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005832 if ((cur->type == XML_RELAXNG_REF) ||
5833 (cur->type == XML_RELAXNG_PARENTREF)) {
5834 if (cur->depth == -1) {
5835 cur->depth = depth;
5836 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5837 cur->depth = -2;
5838 } else if (depth == cur->depth) {
5839 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5840 "Detected a cycle in %s references\n",
5841 cur->name, NULL);
5842 return (-1);
5843 }
5844 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5845 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5846 } else {
5847 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5848 }
5849 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005850 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005851 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005852}
5853
5854/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005855 * xmlRelaxNGTryUnlink:
5856 * @ctxt: a Relax-NG parser context
5857 * @cur: the definition to unlink
5858 * @parent: the parent definition
5859 * @prev: the previous sibling definition
5860 *
5861 * Try to unlink a definition. If not possble make it a NOOP
5862 *
5863 * Returns the new prev definition
5864 */
5865static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005866xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5867 xmlRelaxNGDefinePtr cur,
5868 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5869{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005870 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005871 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005872 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005873 if (parent != NULL) {
5874 if (parent->content == cur)
5875 parent->content = cur->next;
5876 else if (parent->attrs == cur)
5877 parent->attrs = cur->next;
5878 else if (parent->nameClass == cur)
5879 parent->nameClass = cur->next;
5880 } else {
5881 cur->type = XML_RELAXNG_NOOP;
5882 prev = cur;
5883 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005884 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005885 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005886}
5887
5888/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005889 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005890 * @ctxt: a Relax-NG parser context
5891 * @nodes: grammar children nodes
5892 *
5893 * Check for simplification of empty and notAllowed
5894 */
5895static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005896xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5897 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5898{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005899 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005900
Daniel Veillardfd573f12003-03-16 17:52:32 +00005901 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005902 if ((cur->type == XML_RELAXNG_REF) ||
5903 (cur->type == XML_RELAXNG_PARENTREF)) {
5904 if (cur->depth != -3) {
5905 cur->depth = -3;
5906 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5907 }
5908 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5909 cur->parent = parent;
5910 if ((parent != NULL) &&
5911 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5912 (parent->type == XML_RELAXNG_LIST) ||
5913 (parent->type == XML_RELAXNG_GROUP) ||
5914 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5915 (parent->type == XML_RELAXNG_ONEORMORE) ||
5916 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5917 parent->type = XML_RELAXNG_NOT_ALLOWED;
5918 break;
5919 }
5920 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5921 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5922 } else
5923 prev = cur;
5924 } else if (cur->type == XML_RELAXNG_EMPTY) {
5925 cur->parent = parent;
5926 if ((parent != NULL) &&
5927 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5928 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5929 parent->type = XML_RELAXNG_EMPTY;
5930 break;
5931 }
5932 if ((parent != NULL) &&
5933 ((parent->type == XML_RELAXNG_GROUP) ||
5934 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5935 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5936 } else
5937 prev = cur;
5938 } else {
5939 cur->parent = parent;
5940 if (cur->content != NULL)
5941 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5942 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5943 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5944 if (cur->nameClass != NULL)
5945 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5946 /*
5947 * On Elements, try to move attribute only generating rules on
5948 * the attrs rules.
5949 */
5950 if (cur->type == XML_RELAXNG_ELEMENT) {
5951 int attronly;
5952 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00005953
Daniel Veillard4c004142003-10-07 11:33:24 +00005954 while (cur->content != NULL) {
5955 attronly =
5956 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5957 if (attronly == 1) {
5958 /*
5959 * migrate cur->content to attrs
5960 */
5961 tmp = cur->content;
5962 cur->content = tmp->next;
5963 tmp->next = cur->attrs;
5964 cur->attrs = tmp;
5965 } else {
5966 /*
5967 * cur->content can generate elements or text
5968 */
5969 break;
5970 }
5971 }
5972 pre = cur->content;
5973 while ((pre != NULL) && (pre->next != NULL)) {
5974 tmp = pre->next;
5975 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5976 if (attronly == 1) {
5977 /*
5978 * migrate tmp to attrs
5979 */
5980 pre->next = tmp->next;
5981 tmp->next = cur->attrs;
5982 cur->attrs = tmp;
5983 } else {
5984 pre = tmp;
5985 }
5986 }
5987 }
5988 /*
5989 * This may result in a simplification
5990 */
5991 if ((cur->type == XML_RELAXNG_GROUP) ||
5992 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5993 if (cur->content == NULL)
5994 cur->type = XML_RELAXNG_EMPTY;
5995 else if (cur->content->next == NULL) {
5996 if ((parent == NULL) && (prev == NULL)) {
5997 cur->type = XML_RELAXNG_NOOP;
5998 } else if (prev == NULL) {
5999 parent->content = cur->content;
6000 cur->content->next = cur->next;
6001 cur = cur->content;
6002 } else {
6003 cur->content->next = cur->next;
6004 prev->next = cur->content;
6005 cur = cur->content;
6006 }
6007 }
6008 }
6009 /*
6010 * the current node may have been transformed back
6011 */
6012 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6013 (cur->content != NULL) &&
6014 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6015 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6016 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6017 if ((parent != NULL) &&
6018 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6019 (parent->type == XML_RELAXNG_LIST) ||
6020 (parent->type == XML_RELAXNG_GROUP) ||
6021 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6022 (parent->type == XML_RELAXNG_ONEORMORE) ||
6023 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6024 parent->type = XML_RELAXNG_NOT_ALLOWED;
6025 break;
6026 }
6027 if ((parent != NULL) &&
6028 (parent->type == XML_RELAXNG_CHOICE)) {
6029 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6030 } else
6031 prev = cur;
6032 } else if (cur->type == XML_RELAXNG_EMPTY) {
6033 if ((parent != NULL) &&
6034 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6035 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6036 parent->type = XML_RELAXNG_EMPTY;
6037 break;
6038 }
6039 if ((parent != NULL) &&
6040 ((parent->type == XML_RELAXNG_GROUP) ||
6041 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6042 (parent->type == XML_RELAXNG_CHOICE))) {
6043 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6044 } else
6045 prev = cur;
6046 } else {
6047 prev = cur;
6048 }
6049 }
6050 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006051 }
6052}
6053
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006054/**
6055 * xmlRelaxNGGroupContentType:
6056 * @ct1: the first content type
6057 * @ct2: the second content type
6058 *
6059 * Try to group 2 content types
6060 *
6061 * Returns the content type
6062 */
6063static xmlRelaxNGContentType
6064xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006065 xmlRelaxNGContentType ct2)
6066{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006067 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006068 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6069 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006070 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006071 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006072 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006073 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006074 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006075 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6076 return (XML_RELAXNG_CONTENT_COMPLEX);
6077 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006078}
6079
6080/**
6081 * xmlRelaxNGMaxContentType:
6082 * @ct1: the first content type
6083 * @ct2: the second content type
6084 *
6085 * Compute the max content-type
6086 *
6087 * Returns the content type
6088 */
6089static xmlRelaxNGContentType
6090xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006091 xmlRelaxNGContentType ct2)
6092{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006093 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006094 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6095 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006096 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006097 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6098 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006099 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006100 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6101 return (XML_RELAXNG_CONTENT_COMPLEX);
6102 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006103}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006104
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006105/**
6106 * xmlRelaxNGCheckRules:
6107 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006108 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006109 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006110 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006111 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006112 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006113 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006114 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006115 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006116static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006117xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6118 xmlRelaxNGDefinePtr cur, int flags,
6119 xmlRelaxNGType ptype)
6120{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006121 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006122 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006123
Daniel Veillardfd573f12003-03-16 17:52:32 +00006124 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006125 ret = XML_RELAXNG_CONTENT_EMPTY;
6126 if ((cur->type == XML_RELAXNG_REF) ||
6127 (cur->type == XML_RELAXNG_PARENTREF)) {
6128 if (flags & XML_RELAXNG_IN_LIST) {
6129 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6130 "Found forbidden pattern list//ref\n", NULL,
6131 NULL);
6132 }
6133 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6134 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6135 "Found forbidden pattern data/except//ref\n",
6136 NULL, NULL);
6137 }
6138 if (cur->depth > -4) {
6139 cur->depth = -4;
6140 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6141 flags, cur->type);
6142 cur->depth = ret - 15;
6143 } else if (cur->depth == -4) {
6144 ret = XML_RELAXNG_CONTENT_COMPLEX;
6145 } else {
6146 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6147 }
6148 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6149 /*
6150 * The 7.3 Attribute derivation rule for groups is plugged there
6151 */
6152 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6153 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6154 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6155 "Found forbidden pattern data/except//element(ref)\n",
6156 NULL, NULL);
6157 }
6158 if (flags & XML_RELAXNG_IN_LIST) {
6159 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6160 "Found forbidden pattern list//element(ref)\n",
6161 NULL, NULL);
6162 }
6163 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6164 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6165 "Found forbidden pattern attribute//element(ref)\n",
6166 NULL, NULL);
6167 }
6168 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6169 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6170 "Found forbidden pattern attribute//element(ref)\n",
6171 NULL, NULL);
6172 }
6173 /*
6174 * reset since in the simple form elements are only child
6175 * of grammar/define
6176 */
6177 nflags = 0;
6178 ret =
6179 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6180 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6181 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6182 "Element %s attributes have a content type error\n",
6183 cur->name, NULL);
6184 }
6185 ret =
6186 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6187 cur->type);
6188 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6189 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6190 "Element %s has a content type error\n",
6191 cur->name, NULL);
6192 } else {
6193 ret = XML_RELAXNG_CONTENT_COMPLEX;
6194 }
6195 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6196 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6197 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6198 "Found forbidden pattern attribute//attribute\n",
6199 NULL, NULL);
6200 }
6201 if (flags & XML_RELAXNG_IN_LIST) {
6202 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6203 "Found forbidden pattern list//attribute\n",
6204 NULL, NULL);
6205 }
6206 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6207 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6208 "Found forbidden pattern oneOrMore//group//attribute\n",
6209 NULL, NULL);
6210 }
6211 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6212 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6213 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6214 NULL, NULL);
6215 }
6216 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6217 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6218 "Found forbidden pattern data/except//attribute\n",
6219 NULL, NULL);
6220 }
6221 if (flags & XML_RELAXNG_IN_START) {
6222 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6223 "Found forbidden pattern start//attribute\n",
6224 NULL, NULL);
6225 }
6226 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6227 && (cur->name == NULL)) {
6228 if (cur->ns == NULL) {
6229 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6230 "Found anyName attribute without oneOrMore ancestor\n",
6231 NULL, NULL);
6232 } else {
6233 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6234 "Found nsName attribute without oneOrMore ancestor\n",
6235 NULL, NULL);
6236 }
6237 }
6238 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6239 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6240 ret = XML_RELAXNG_CONTENT_EMPTY;
6241 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6242 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6243 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6244 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6245 "Found forbidden pattern data/except//oneOrMore\n",
6246 NULL, NULL);
6247 }
6248 if (flags & XML_RELAXNG_IN_START) {
6249 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6250 "Found forbidden pattern start//oneOrMore\n",
6251 NULL, NULL);
6252 }
6253 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6254 ret =
6255 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6256 cur->type);
6257 ret = xmlRelaxNGGroupContentType(ret, ret);
6258 } else if (cur->type == XML_RELAXNG_LIST) {
6259 if (flags & XML_RELAXNG_IN_LIST) {
6260 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6261 "Found forbidden pattern list//list\n", NULL,
6262 NULL);
6263 }
6264 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6265 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6266 "Found forbidden pattern data/except//list\n",
6267 NULL, NULL);
6268 }
6269 if (flags & XML_RELAXNG_IN_START) {
6270 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6271 "Found forbidden pattern start//list\n", NULL,
6272 NULL);
6273 }
6274 nflags = flags | XML_RELAXNG_IN_LIST;
6275 ret =
6276 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6277 cur->type);
6278 } else if (cur->type == XML_RELAXNG_GROUP) {
6279 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6280 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6281 "Found forbidden pattern data/except//group\n",
6282 NULL, NULL);
6283 }
6284 if (flags & XML_RELAXNG_IN_START) {
6285 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6286 "Found forbidden pattern start//group\n", NULL,
6287 NULL);
6288 }
6289 if (flags & XML_RELAXNG_IN_ONEORMORE)
6290 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6291 else
6292 nflags = flags;
6293 ret =
6294 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6295 cur->type);
6296 /*
6297 * The 7.3 Attribute derivation rule for groups is plugged there
6298 */
6299 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6300 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6301 if (flags & XML_RELAXNG_IN_LIST) {
6302 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6303 "Found forbidden pattern list//interleave\n",
6304 NULL, NULL);
6305 }
6306 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6307 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6308 "Found forbidden pattern data/except//interleave\n",
6309 NULL, NULL);
6310 }
6311 if (flags & XML_RELAXNG_IN_START) {
6312 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6313 "Found forbidden pattern start//interleave\n",
6314 NULL, NULL);
6315 }
6316 if (flags & XML_RELAXNG_IN_ONEORMORE)
6317 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6318 else
6319 nflags = flags;
6320 ret =
6321 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6322 cur->type);
6323 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6324 if ((cur->parent != NULL) &&
6325 (cur->parent->type == XML_RELAXNG_DATATYPE))
6326 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6327 else
6328 nflags = flags;
6329 ret =
6330 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6331 cur->type);
6332 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6333 if (flags & XML_RELAXNG_IN_START) {
6334 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6335 "Found forbidden pattern start//data\n", NULL,
6336 NULL);
6337 }
6338 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6339 ret = XML_RELAXNG_CONTENT_SIMPLE;
6340 } else if (cur->type == XML_RELAXNG_VALUE) {
6341 if (flags & XML_RELAXNG_IN_START) {
6342 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6343 "Found forbidden pattern start//value\n", NULL,
6344 NULL);
6345 }
6346 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6347 ret = XML_RELAXNG_CONTENT_SIMPLE;
6348 } else if (cur->type == XML_RELAXNG_TEXT) {
6349 if (flags & XML_RELAXNG_IN_LIST) {
6350 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6351 "Found forbidden pattern list//text\n", NULL,
6352 NULL);
6353 }
6354 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6355 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6356 "Found forbidden pattern data/except//text\n",
6357 NULL, NULL);
6358 }
6359 if (flags & XML_RELAXNG_IN_START) {
6360 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6361 "Found forbidden pattern start//text\n", NULL,
6362 NULL);
6363 }
6364 ret = XML_RELAXNG_CONTENT_COMPLEX;
6365 } else if (cur->type == XML_RELAXNG_EMPTY) {
6366 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6367 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6368 "Found forbidden pattern data/except//empty\n",
6369 NULL, NULL);
6370 }
6371 if (flags & XML_RELAXNG_IN_START) {
6372 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6373 "Found forbidden pattern start//empty\n", NULL,
6374 NULL);
6375 }
6376 ret = XML_RELAXNG_CONTENT_EMPTY;
6377 } else if (cur->type == XML_RELAXNG_CHOICE) {
6378 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6379 ret =
6380 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6381 } else {
6382 ret =
6383 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6384 }
6385 cur = cur->next;
6386 if (ptype == XML_RELAXNG_GROUP) {
6387 val = xmlRelaxNGGroupContentType(val, ret);
6388 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6389 tmp = xmlRelaxNGGroupContentType(val, ret);
6390 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6391 tmp = xmlRelaxNGMaxContentType(val, ret);
6392 } else if (ptype == XML_RELAXNG_CHOICE) {
6393 val = xmlRelaxNGMaxContentType(val, ret);
6394 } else if (ptype == XML_RELAXNG_LIST) {
6395 val = XML_RELAXNG_CONTENT_SIMPLE;
6396 } else if (ptype == XML_RELAXNG_EXCEPT) {
6397 if (ret == XML_RELAXNG_CONTENT_ERROR)
6398 val = XML_RELAXNG_CONTENT_ERROR;
6399 else
6400 val = XML_RELAXNG_CONTENT_SIMPLE;
6401 } else {
6402 val = xmlRelaxNGGroupContentType(val, ret);
6403 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006404
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006405 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006406 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006407}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006408
6409/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006410 * xmlRelaxNGParseGrammar:
6411 * @ctxt: a Relax-NG parser context
6412 * @nodes: grammar children nodes
6413 *
6414 * parse a Relax-NG <grammar> node
6415 *
6416 * Returns the internal xmlRelaxNGGrammarPtr built or
6417 * NULL in case of error
6418 */
6419static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006420xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6421{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006422 xmlRelaxNGGrammarPtr ret, tmp, old;
6423
Daniel Veillardc482e262003-02-26 14:48:48 +00006424#ifdef DEBUG_GRAMMAR
6425 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6426#endif
6427
Daniel Veillard6eadf632003-01-23 18:29:16 +00006428 ret = xmlRelaxNGNewGrammar(ctxt);
6429 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006430 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006431
6432 /*
6433 * Link the new grammar in the tree
6434 */
6435 ret->parent = ctxt->grammar;
6436 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006437 tmp = ctxt->grammar->children;
6438 if (tmp == NULL) {
6439 ctxt->grammar->children = ret;
6440 } else {
6441 while (tmp->next != NULL)
6442 tmp = tmp->next;
6443 tmp->next = ret;
6444 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006445 }
6446
6447 old = ctxt->grammar;
6448 ctxt->grammar = ret;
6449 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6450 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006451 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006452 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6453 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006454 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006455 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6456 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006457 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006458
6459 /*
6460 * Apply 4.17 mergingd rules to defines and starts
6461 */
6462 xmlRelaxNGCombineStart(ctxt, ret);
6463 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006464 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6465 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006466 }
6467
6468 /*
6469 * link together defines and refs in this grammar
6470 */
6471 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006472 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6473 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006474 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006475
Daniel Veillard6eadf632003-01-23 18:29:16 +00006476 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006477 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006478}
6479
6480/**
6481 * xmlRelaxNGParseDocument:
6482 * @ctxt: a Relax-NG parser context
6483 * @node: the root node of the RelaxNG schema
6484 *
6485 * parse a Relax-NG definition resource and build an internal
6486 * xmlRelaxNG struture which can be used to validate instances.
6487 *
6488 * Returns the internal XML RelaxNG structure built or
6489 * NULL in case of error
6490 */
6491static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006492xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6493{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006494 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006495 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006496 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006497
6498 if ((ctxt == NULL) || (node == NULL))
6499 return (NULL);
6500
6501 schema = xmlRelaxNGNewRelaxNG(ctxt);
6502 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006503 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006504
Daniel Veillard276be4a2003-01-24 01:03:34 +00006505 olddefine = ctxt->define;
6506 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006507 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006508 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006509 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006510 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006511
Daniel Veillard4c004142003-10-07 11:33:24 +00006512 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6513 if (schema->topgrammar == NULL) {
6514 return (schema);
6515 }
6516 /*
6517 * Link the new grammar in the tree
6518 */
6519 ret->parent = ctxt->grammar;
6520 if (ctxt->grammar != NULL) {
6521 tmp = ctxt->grammar->children;
6522 if (tmp == NULL) {
6523 ctxt->grammar->children = ret;
6524 } else {
6525 while (tmp->next != NULL)
6526 tmp = tmp->next;
6527 tmp->next = ret;
6528 }
6529 }
6530 old = ctxt->grammar;
6531 ctxt->grammar = ret;
6532 xmlRelaxNGParseStart(ctxt, node);
6533 if (old != NULL)
6534 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006535 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006536 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006537 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006538 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6539 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6540 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6541 while ((schema->topgrammar->start != NULL) &&
6542 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6543 (schema->topgrammar->start->next != NULL))
6544 schema->topgrammar->start =
6545 schema->topgrammar->start->content;
6546 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6547 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6548 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006549 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006550#ifdef DEBUG
6551 if (schema == NULL)
6552 xmlGenericError(xmlGenericErrorContext,
6553 "xmlRelaxNGParseDocument() failed\n");
6554#endif
6555
6556 return (schema);
6557}
6558
6559/************************************************************************
6560 * *
6561 * Reading RelaxNGs *
6562 * *
6563 ************************************************************************/
6564
6565/**
6566 * xmlRelaxNGNewParserCtxt:
6567 * @URL: the location of the schema
6568 *
6569 * Create an XML RelaxNGs parse context for that file/resource expected
6570 * to contain an XML RelaxNGs file.
6571 *
6572 * Returns the parser context or NULL in case of error
6573 */
6574xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006575xmlRelaxNGNewParserCtxt(const char *URL)
6576{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006577 xmlRelaxNGParserCtxtPtr ret;
6578
6579 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006580 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006581
Daniel Veillard4c004142003-10-07 11:33:24 +00006582 ret =
6583 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006584 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006585 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006586 return (NULL);
6587 }
6588 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006589 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006590 ret->error = xmlGenericError;
6591 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006592 return (ret);
6593}
6594
6595/**
6596 * xmlRelaxNGNewMemParserCtxt:
6597 * @buffer: a pointer to a char array containing the schemas
6598 * @size: the size of the array
6599 *
6600 * Create an XML RelaxNGs parse context for that memory buffer expected
6601 * to contain an XML RelaxNGs file.
6602 *
6603 * Returns the parser context or NULL in case of error
6604 */
6605xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006606xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6607{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006608 xmlRelaxNGParserCtxtPtr ret;
6609
6610 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006611 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006612
Daniel Veillard4c004142003-10-07 11:33:24 +00006613 ret =
6614 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006615 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006616 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006617 return (NULL);
6618 }
6619 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6620 ret->buffer = buffer;
6621 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006622 ret->error = xmlGenericError;
6623 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006624 return (ret);
6625}
6626
6627/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006628 * xmlRelaxNGNewDocParserCtxt:
6629 * @doc: a preparsed document tree
6630 *
6631 * Create an XML RelaxNGs parser context for that document.
6632 * Note: since the process of compiling a RelaxNG schemas modifies the
6633 * document, the @doc parameter is duplicated internally.
6634 *
6635 * Returns the parser context or NULL in case of error
6636 */
6637xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006638xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6639{
Daniel Veillard33300b42003-04-17 09:09:19 +00006640 xmlRelaxNGParserCtxtPtr ret;
6641 xmlDocPtr copy;
6642
6643 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006644 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006645 copy = xmlCopyDoc(doc, 1);
6646 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006647 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006648
Daniel Veillard4c004142003-10-07 11:33:24 +00006649 ret =
6650 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006651 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006652 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006653 return (NULL);
6654 }
6655 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6656 ret->document = copy;
6657 ret->userData = xmlGenericErrorContext;
6658 return (ret);
6659}
6660
6661/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006662 * xmlRelaxNGFreeParserCtxt:
6663 * @ctxt: the schema parser context
6664 *
6665 * Free the resources associated to the schema parser context
6666 */
6667void
Daniel Veillard4c004142003-10-07 11:33:24 +00006668xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6669{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006670 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006671 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006672 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006673 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006674 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006675 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006676 if (ctxt->interleaves != NULL)
6677 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006678 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006679 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006680 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006681 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006682 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006683 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006684 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006685 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006686 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006687 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006688
Daniel Veillard4c004142003-10-07 11:33:24 +00006689 for (i = 0; i < ctxt->defNr; i++)
6690 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6691 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006692 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006693 xmlFree(ctxt);
6694}
6695
Daniel Veillard6eadf632003-01-23 18:29:16 +00006696/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006697 * xmlRelaxNGNormExtSpace:
6698 * @value: a value
6699 *
6700 * Removes the leading and ending spaces of the value
6701 * The string is modified "in situ"
6702 */
6703static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006704xmlRelaxNGNormExtSpace(xmlChar * value)
6705{
Daniel Veillardd2298792003-02-14 16:54:11 +00006706 xmlChar *start = value;
6707 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006708
Daniel Veillard4c004142003-10-07 11:33:24 +00006709 if (value == NULL)
6710 return;
6711
William M. Brack76e95df2003-10-18 16:20:14 +00006712 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006713 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006714 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006715 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006716 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006717 cur++;
6718 if (*cur == 0)
6719 return;
6720 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006721 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006722 cur++;
6723 if (*cur == 0) {
6724 *start = 0;
6725 return;
6726 }
6727 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006728 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006729 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006730 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006731 *start++ = *cur++;
6732 if (*cur == 0) {
6733 *start = 0;
6734 return;
6735 }
6736 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006737 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006738 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006739 if (*cur == 0) {
6740 *start = 0;
6741 return;
6742 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006743 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006744 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006745 }
6746}
6747
6748/**
6749 * xmlRelaxNGCheckAttributes:
6750 * @ctxt: a Relax-NG parser context
6751 * @node: a Relax-NG node
6752 *
6753 * Check all the attributes on the given node
6754 */
6755static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006756xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6757{
Daniel Veillardd2298792003-02-14 16:54:11 +00006758 xmlAttrPtr cur, next;
6759
6760 cur = node->properties;
6761 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006762 next = cur->next;
6763 if ((cur->ns == NULL) ||
6764 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6765 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6766 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6767 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6768 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6769 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6770 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6771 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6772 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6773 "Attribute %s is not allowed on %s\n",
6774 cur->name, node->name);
6775 }
6776 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6777 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6778 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6779 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6780 "Attribute %s is not allowed on %s\n",
6781 cur->name, node->name);
6782 }
6783 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6784 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6785 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6786 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6787 "Attribute %s is not allowed on %s\n",
6788 cur->name, node->name);
6789 }
6790 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6791 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6792 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6793 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6794 "Attribute %s is not allowed on %s\n",
6795 cur->name, node->name);
6796 }
6797 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6798 xmlChar *val;
6799 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006800
Daniel Veillard4c004142003-10-07 11:33:24 +00006801 val = xmlNodeListGetString(node->doc, cur->children, 1);
6802 if (val != NULL) {
6803 if (val[0] != 0) {
6804 uri = xmlParseURI((const char *) val);
6805 if (uri == NULL) {
6806 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6807 "Attribute %s contains invalid URI %s\n",
6808 cur->name, val);
6809 } else {
6810 if (uri->scheme == NULL) {
6811 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6812 "Attribute %s URI %s is not absolute\n",
6813 cur->name, val);
6814 }
6815 if (uri->fragment != NULL) {
6816 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6817 "Attribute %s URI %s has a fragment ID\n",
6818 cur->name, val);
6819 }
6820 xmlFreeURI(uri);
6821 }
6822 }
6823 xmlFree(val);
6824 }
6825 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6826 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6827 "Unknown attribute %s on %s\n", cur->name,
6828 node->name);
6829 }
6830 }
6831 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006832 }
6833}
6834
6835/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006836 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006837 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006838 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006839 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006840 * Cleanup the subtree from unwanted nodes for parsing, resolve
6841 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006842 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006843static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006844xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6845{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006846 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006847
Daniel Veillard6eadf632003-01-23 18:29:16 +00006848 delete = NULL;
6849 cur = root;
6850 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006851 if (delete != NULL) {
6852 xmlUnlinkNode(delete);
6853 xmlFreeNode(delete);
6854 delete = NULL;
6855 }
6856 if (cur->type == XML_ELEMENT_NODE) {
6857 /*
6858 * Simplification 4.1. Annotations
6859 */
6860 if ((cur->ns == NULL) ||
6861 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6862 if ((cur->parent != NULL) &&
6863 (cur->parent->type == XML_ELEMENT_NODE) &&
6864 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6865 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6866 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6867 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6868 "element %s doesn't allow foreign elements\n",
6869 cur->parent->name, NULL);
6870 }
6871 delete = cur;
6872 goto skip_children;
6873 } else {
6874 xmlRelaxNGCleanupAttributes(ctxt, cur);
6875 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6876 xmlChar *href, *ns, *base, *URL;
6877 xmlRelaxNGDocumentPtr docu;
6878 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00006879 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006880
Daniel Veillard4c004142003-10-07 11:33:24 +00006881 ns = xmlGetProp(cur, BAD_CAST "ns");
6882 if (ns == NULL) {
6883 tmp = cur->parent;
6884 while ((tmp != NULL) &&
6885 (tmp->type == XML_ELEMENT_NODE)) {
6886 ns = xmlGetProp(tmp, BAD_CAST "ns");
6887 if (ns != NULL)
6888 break;
6889 tmp = tmp->parent;
6890 }
6891 }
6892 href = xmlGetProp(cur, BAD_CAST "href");
6893 if (href == NULL) {
6894 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6895 "xmlRelaxNGParse: externalRef has no href attribute\n",
6896 NULL, NULL);
6897 delete = cur;
6898 goto skip_children;
6899 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00006900 uri = xmlParseURI((const char *) href);
6901 if (uri == NULL) {
6902 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6903 "Incorrect URI for externalRef %s\n",
6904 href, NULL);
6905 if (href != NULL)
6906 xmlFree(href);
6907 delete = cur;
6908 goto skip_children;
6909 }
6910 if (uri->fragment != NULL) {
6911 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6912 "Fragment forbidden in URI for externalRef %s\n",
6913 href, NULL);
6914 xmlFreeURI(uri);
6915 if (href != NULL)
6916 xmlFree(href);
6917 delete = cur;
6918 goto skip_children;
6919 }
6920 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00006921 base = xmlNodeGetBase(cur->doc, cur);
6922 URL = xmlBuildURI(href, base);
6923 if (URL == NULL) {
6924 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6925 "Failed to compute URL for externalRef %s\n",
6926 href, NULL);
6927 if (href != NULL)
6928 xmlFree(href);
6929 if (base != NULL)
6930 xmlFree(base);
6931 delete = cur;
6932 goto skip_children;
6933 }
6934 if (href != NULL)
6935 xmlFree(href);
6936 if (base != NULL)
6937 xmlFree(base);
6938 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6939 if (docu == NULL) {
6940 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6941 "Failed to load externalRef %s\n", URL,
6942 NULL);
6943 xmlFree(URL);
6944 delete = cur;
6945 goto skip_children;
6946 }
6947 if (ns != NULL)
6948 xmlFree(ns);
6949 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00006950 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00006951 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6952 xmlChar *href, *ns, *base, *URL;
6953 xmlRelaxNGIncludePtr incl;
6954 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006955
Daniel Veillard4c004142003-10-07 11:33:24 +00006956 href = xmlGetProp(cur, BAD_CAST "href");
6957 if (href == NULL) {
6958 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6959 "xmlRelaxNGParse: include has no href attribute\n",
6960 NULL, NULL);
6961 delete = cur;
6962 goto skip_children;
6963 }
6964 base = xmlNodeGetBase(cur->doc, cur);
6965 URL = xmlBuildURI(href, base);
6966 if (URL == NULL) {
6967 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6968 "Failed to compute URL for include %s\n",
6969 href, NULL);
6970 if (href != NULL)
6971 xmlFree(href);
6972 if (base != NULL)
6973 xmlFree(base);
6974 delete = cur;
6975 goto skip_children;
6976 }
6977 if (href != NULL)
6978 xmlFree(href);
6979 if (base != NULL)
6980 xmlFree(base);
6981 ns = xmlGetProp(cur, BAD_CAST "ns");
6982 if (ns == NULL) {
6983 tmp = cur->parent;
6984 while ((tmp != NULL) &&
6985 (tmp->type == XML_ELEMENT_NODE)) {
6986 ns = xmlGetProp(tmp, BAD_CAST "ns");
6987 if (ns != NULL)
6988 break;
6989 tmp = tmp->parent;
6990 }
6991 }
6992 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6993 if (ns != NULL)
6994 xmlFree(ns);
6995 if (incl == NULL) {
6996 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
6997 "Failed to load include %s\n", URL,
6998 NULL);
6999 xmlFree(URL);
7000 delete = cur;
7001 goto skip_children;
7002 }
7003 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007004 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007005 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7006 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7007 {
7008 xmlChar *name, *ns;
7009 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007010
Daniel Veillard4c004142003-10-07 11:33:24 +00007011 /*
7012 * Simplification 4.8. name attribute of element
7013 * and attribute elements
7014 */
7015 name = xmlGetProp(cur, BAD_CAST "name");
7016 if (name != NULL) {
7017 if (cur->children == NULL) {
7018 text =
7019 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7020 name);
7021 } else {
7022 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007023
Daniel Veillard4c004142003-10-07 11:33:24 +00007024 node = xmlNewNode(cur->ns, BAD_CAST "name");
7025 if (node != NULL) {
7026 xmlAddPrevSibling(cur->children, node);
7027 text = xmlNewText(name);
7028 xmlAddChild(node, text);
7029 text = node;
7030 }
7031 }
7032 if (text == NULL) {
7033 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7034 "Failed to create a name %s element\n",
7035 name, NULL);
7036 }
7037 xmlUnsetProp(cur, BAD_CAST "name");
7038 xmlFree(name);
7039 ns = xmlGetProp(cur, BAD_CAST "ns");
7040 if (ns != NULL) {
7041 if (text != NULL) {
7042 xmlSetProp(text, BAD_CAST "ns", ns);
7043 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7044 }
7045 xmlFree(ns);
7046 } else if (xmlStrEqual(cur->name,
7047 BAD_CAST "attribute")) {
7048 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7049 }
7050 }
7051 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7052 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7053 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7054 /*
7055 * Simplification 4.8. name attribute of element
7056 * and attribute elements
7057 */
7058 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7059 xmlNodePtr node;
7060 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007061
Daniel Veillard4c004142003-10-07 11:33:24 +00007062 node = cur->parent;
7063 while ((node != NULL) &&
7064 (node->type == XML_ELEMENT_NODE)) {
7065 ns = xmlGetProp(node, BAD_CAST "ns");
7066 if (ns != NULL) {
7067 break;
7068 }
7069 node = node->parent;
7070 }
7071 if (ns == NULL) {
7072 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7073 } else {
7074 xmlSetProp(cur, BAD_CAST "ns", ns);
7075 xmlFree(ns);
7076 }
7077 }
7078 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7079 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007080
Daniel Veillard4c004142003-10-07 11:33:24 +00007081 /*
7082 * Simplification: 4.10. QNames
7083 */
7084 name = xmlNodeGetContent(cur);
7085 if (name != NULL) {
7086 local = xmlSplitQName2(name, &prefix);
7087 if (local != NULL) {
7088 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007089
Daniel Veillard4c004142003-10-07 11:33:24 +00007090 ns = xmlSearchNs(cur->doc, cur, prefix);
7091 if (ns == NULL) {
7092 xmlRngPErr(ctxt, cur,
7093 XML_RNGP_PREFIX_UNDEFINED,
7094 "xmlRelaxNGParse: no namespace for prefix %s\n",
7095 prefix, NULL);
7096 } else {
7097 xmlSetProp(cur, BAD_CAST "ns",
7098 ns->href);
7099 xmlNodeSetContent(cur, local);
7100 }
7101 xmlFree(local);
7102 xmlFree(prefix);
7103 }
7104 xmlFree(name);
7105 }
7106 }
7107 /*
7108 * 4.16
7109 */
7110 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7111 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7112 xmlRngPErr(ctxt, cur,
7113 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7114 "Found nsName/except//nsName forbidden construct\n",
7115 NULL, NULL);
7116 }
7117 }
7118 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7119 (cur != root)) {
7120 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007121
Daniel Veillard4c004142003-10-07 11:33:24 +00007122 /*
7123 * 4.16
7124 */
7125 if ((cur->parent != NULL) &&
7126 (xmlStrEqual
7127 (cur->parent->name, BAD_CAST "anyName"))) {
7128 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7129 xmlRelaxNGCleanupTree(ctxt, cur);
7130 ctxt->flags = oldflags;
7131 goto skip_children;
7132 } else if ((cur->parent != NULL) &&
7133 (xmlStrEqual
7134 (cur->parent->name, BAD_CAST "nsName"))) {
7135 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7136 xmlRelaxNGCleanupTree(ctxt, cur);
7137 ctxt->flags = oldflags;
7138 goto skip_children;
7139 }
7140 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7141 /*
7142 * 4.16
7143 */
7144 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7145 xmlRngPErr(ctxt, cur,
7146 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7147 "Found anyName/except//anyName forbidden construct\n",
7148 NULL, NULL);
7149 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7150 xmlRngPErr(ctxt, cur,
7151 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7152 "Found nsName/except//anyName forbidden construct\n",
7153 NULL, NULL);
7154 }
7155 }
7156 /*
7157 * Thisd is not an else since "include" is transformed
7158 * into a div
7159 */
7160 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7161 xmlChar *ns;
7162 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007163
Daniel Veillard4c004142003-10-07 11:33:24 +00007164 /*
7165 * implements rule 4.11
7166 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007167
Daniel Veillard4c004142003-10-07 11:33:24 +00007168 ns = xmlGetProp(cur, BAD_CAST "ns");
7169
7170 child = cur->children;
7171 ins = cur;
7172 while (child != NULL) {
7173 if (ns != NULL) {
7174 if (!xmlHasProp(child, BAD_CAST "ns")) {
7175 xmlSetProp(child, BAD_CAST "ns", ns);
7176 }
7177 }
7178 tmp = child->next;
7179 xmlUnlinkNode(child);
7180 ins = xmlAddNextSibling(ins, child);
7181 child = tmp;
7182 }
7183 if (ns != NULL)
7184 xmlFree(ns);
7185 delete = cur;
7186 goto skip_children;
7187 }
7188 }
7189 }
7190 /*
7191 * Simplification 4.2 whitespaces
7192 */
7193 else if ((cur->type == XML_TEXT_NODE) ||
7194 (cur->type == XML_CDATA_SECTION_NODE)) {
7195 if (IS_BLANK_NODE(cur)) {
7196 if (cur->parent->type == XML_ELEMENT_NODE) {
7197 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7198 &&
7199 (!xmlStrEqual
7200 (cur->parent->name, BAD_CAST "param")))
7201 delete = cur;
7202 } else {
7203 delete = cur;
7204 goto skip_children;
7205 }
7206 }
7207 } else {
7208 delete = cur;
7209 goto skip_children;
7210 }
7211
7212 /*
7213 * Skip to next node
7214 */
7215 if (cur->children != NULL) {
7216 if ((cur->children->type != XML_ENTITY_DECL) &&
7217 (cur->children->type != XML_ENTITY_REF_NODE) &&
7218 (cur->children->type != XML_ENTITY_NODE)) {
7219 cur = cur->children;
7220 continue;
7221 }
7222 }
7223 skip_children:
7224 if (cur->next != NULL) {
7225 cur = cur->next;
7226 continue;
7227 }
7228
7229 do {
7230 cur = cur->parent;
7231 if (cur == NULL)
7232 break;
7233 if (cur == root) {
7234 cur = NULL;
7235 break;
7236 }
7237 if (cur->next != NULL) {
7238 cur = cur->next;
7239 break;
7240 }
7241 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007242 }
7243 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007244 xmlUnlinkNode(delete);
7245 xmlFreeNode(delete);
7246 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007247 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007248}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007249
Daniel Veillardc5312d72003-02-21 17:14:10 +00007250/**
7251 * xmlRelaxNGCleanupDoc:
7252 * @ctxt: a Relax-NG parser context
7253 * @doc: an xmldocPtr document pointer
7254 *
7255 * Cleanup the document from unwanted nodes for parsing, resolve
7256 * Include and externalRef lookups.
7257 *
7258 * Returns the cleaned up document or NULL in case of error
7259 */
7260static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007261xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7262{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007263 xmlNodePtr root;
7264
7265 /*
7266 * Extract the root
7267 */
7268 root = xmlDocGetRootElement(doc);
7269 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007270 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7271 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007272 return (NULL);
7273 }
7274 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007275 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007276}
7277
7278/**
7279 * xmlRelaxNGParse:
7280 * @ctxt: a Relax-NG parser context
7281 *
7282 * parse a schema definition resource and build an internal
7283 * XML Shema struture which can be used to validate instances.
7284 * *WARNING* this interface is highly subject to change
7285 *
7286 * Returns the internal XML RelaxNG structure built from the resource or
7287 * NULL in case of error
7288 */
7289xmlRelaxNGPtr
7290xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7291{
7292 xmlRelaxNGPtr ret = NULL;
7293 xmlDocPtr doc;
7294 xmlNodePtr root;
7295
7296 xmlRelaxNGInitTypes();
7297
7298 if (ctxt == NULL)
7299 return (NULL);
7300
7301 /*
7302 * First step is to parse the input document into an DOM/Infoset
7303 */
7304 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007305 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007306 if (doc == NULL) {
7307 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7308 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7309 NULL);
7310 return (NULL);
7311 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007312 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007313 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007314 if (doc == NULL) {
7315 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7316 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7317 NULL);
7318 return (NULL);
7319 }
7320 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7321 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007322 } else if (ctxt->document != NULL) {
7323 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007324 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007325 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7326 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7327 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007328 }
7329 ctxt->document = doc;
7330
7331 /*
7332 * Some preprocessing of the document content
7333 */
7334 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7335 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007336 xmlFreeDoc(ctxt->document);
7337 ctxt->document = NULL;
7338 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007339 }
7340
Daniel Veillard6eadf632003-01-23 18:29:16 +00007341 /*
7342 * Then do the parsing for good
7343 */
7344 root = xmlDocGetRootElement(doc);
7345 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007346 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7347 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7348 ctxt->URL, NULL);
7349 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007350 return (NULL);
7351 }
7352 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007353 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007354 xmlFreeDoc(doc);
7355 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007356 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007357
7358 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007359 * Check the ref/defines links
7360 */
7361 /*
7362 * try to preprocess interleaves
7363 */
7364 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007365 xmlHashScan(ctxt->interleaves,
7366 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007367 }
7368
7369 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007370 * if there was a parsing error return NULL
7371 */
7372 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007373 xmlRelaxNGFree(ret);
7374 ctxt->document = NULL;
7375 xmlFreeDoc(doc);
7376 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007377 }
7378
7379 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007380 * try to compile (parts of) the schemas
7381 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007382 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7383 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007384 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007385
Daniel Veillard4c004142003-10-07 11:33:24 +00007386 def = xmlRelaxNGNewDefine(ctxt, NULL);
7387 if (def != NULL) {
7388 def->type = XML_RELAXNG_START;
7389 def->content = ret->topgrammar->start;
7390 ret->topgrammar->start = def;
7391 }
7392 }
7393 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007394 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007395
7396 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007397 * Transfer the pointer for cleanup at the schema level.
7398 */
7399 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007400 ctxt->document = NULL;
7401 ret->documents = ctxt->documents;
7402 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007403
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007404 ret->includes = ctxt->includes;
7405 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007406 ret->defNr = ctxt->defNr;
7407 ret->defTab = ctxt->defTab;
7408 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007409 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007410 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007411
7412 return (ret);
7413}
Daniel Veillard4c004142003-10-07 11:33:24 +00007414
Daniel Veillard6eadf632003-01-23 18:29:16 +00007415/**
7416 * xmlRelaxNGSetParserErrors:
7417 * @ctxt: a Relax-NG validation context
7418 * @err: the error callback
7419 * @warn: the warning callback
7420 * @ctx: contextual data for the callbacks
7421 *
7422 * Set the callback functions used to handle errors for a validation context
7423 */
7424void
7425xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007426 xmlRelaxNGValidityErrorFunc err,
7427 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7428{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007429 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007430 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007431 ctxt->error = err;
7432 ctxt->warning = warn;
7433 ctxt->userData = ctx;
7434}
Daniel Veillard409a8142003-07-18 15:16:57 +00007435
7436/**
7437 * xmlRelaxNGGetParserErrors:
7438 * @ctxt: a Relax-NG validation context
7439 * @err: the error callback result
7440 * @warn: the warning callback result
7441 * @ctx: contextual data for the callbacks result
7442 *
7443 * Get the callback information used to handle errors for a validation context
7444 *
7445 * Returns -1 in case of failure, 0 otherwise.
7446 */
7447int
7448xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007449 xmlRelaxNGValidityErrorFunc * err,
7450 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7451{
Daniel Veillard409a8142003-07-18 15:16:57 +00007452 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007453 return (-1);
7454 if (err != NULL)
7455 *err = ctxt->error;
7456 if (warn != NULL)
7457 *warn = ctxt->warning;
7458 if (ctx != NULL)
7459 *ctx = ctxt->userData;
7460 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007461}
7462
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007463#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007464
Daniel Veillard6eadf632003-01-23 18:29:16 +00007465/************************************************************************
7466 * *
7467 * Dump back a compiled form *
7468 * *
7469 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007470static void xmlRelaxNGDumpDefine(FILE * output,
7471 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007472
7473/**
7474 * xmlRelaxNGDumpDefines:
7475 * @output: the file output
7476 * @defines: a list of define structures
7477 *
7478 * Dump a RelaxNG structure back
7479 */
7480static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007481xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7482{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007483 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007484 xmlRelaxNGDumpDefine(output, defines);
7485 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007486 }
7487}
7488
7489/**
7490 * xmlRelaxNGDumpDefine:
7491 * @output: the file output
7492 * @define: a define structure
7493 *
7494 * Dump a RelaxNG structure back
7495 */
7496static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007497xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7498{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007499 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007500 return;
7501 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007502 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007503 fprintf(output, "<empty/>\n");
7504 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007505 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007506 fprintf(output, "<notAllowed/>\n");
7507 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007508 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007509 fprintf(output, "<text/>\n");
7510 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007511 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007512 fprintf(output, "<element>\n");
7513 if (define->name != NULL) {
7514 fprintf(output, "<name");
7515 if (define->ns != NULL)
7516 fprintf(output, " ns=\"%s\"", define->ns);
7517 fprintf(output, ">%s</name>\n", define->name);
7518 }
7519 xmlRelaxNGDumpDefines(output, define->attrs);
7520 xmlRelaxNGDumpDefines(output, define->content);
7521 fprintf(output, "</element>\n");
7522 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007523 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007524 fprintf(output, "<list>\n");
7525 xmlRelaxNGDumpDefines(output, define->content);
7526 fprintf(output, "</list>\n");
7527 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007528 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007529 fprintf(output, "<oneOrMore>\n");
7530 xmlRelaxNGDumpDefines(output, define->content);
7531 fprintf(output, "</oneOrMore>\n");
7532 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007533 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007534 fprintf(output, "<zeroOrMore>\n");
7535 xmlRelaxNGDumpDefines(output, define->content);
7536 fprintf(output, "</zeroOrMore>\n");
7537 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007538 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007539 fprintf(output, "<choice>\n");
7540 xmlRelaxNGDumpDefines(output, define->content);
7541 fprintf(output, "</choice>\n");
7542 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007543 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007544 fprintf(output, "<group>\n");
7545 xmlRelaxNGDumpDefines(output, define->content);
7546 fprintf(output, "</group>\n");
7547 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007548 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007549 fprintf(output, "<interleave>\n");
7550 xmlRelaxNGDumpDefines(output, define->content);
7551 fprintf(output, "</interleave>\n");
7552 break;
7553 case XML_RELAXNG_OPTIONAL:
7554 fprintf(output, "<optional>\n");
7555 xmlRelaxNGDumpDefines(output, define->content);
7556 fprintf(output, "</optional>\n");
7557 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007558 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007559 fprintf(output, "<attribute>\n");
7560 xmlRelaxNGDumpDefines(output, define->content);
7561 fprintf(output, "</attribute>\n");
7562 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007563 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007564 fprintf(output, "<define");
7565 if (define->name != NULL)
7566 fprintf(output, " name=\"%s\"", define->name);
7567 fprintf(output, ">\n");
7568 xmlRelaxNGDumpDefines(output, define->content);
7569 fprintf(output, "</define>\n");
7570 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007571 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007572 fprintf(output, "<ref");
7573 if (define->name != NULL)
7574 fprintf(output, " name=\"%s\"", define->name);
7575 fprintf(output, ">\n");
7576 xmlRelaxNGDumpDefines(output, define->content);
7577 fprintf(output, "</ref>\n");
7578 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007579 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007580 fprintf(output, "<parentRef");
7581 if (define->name != NULL)
7582 fprintf(output, " name=\"%s\"", define->name);
7583 fprintf(output, ">\n");
7584 xmlRelaxNGDumpDefines(output, define->content);
7585 fprintf(output, "</parentRef>\n");
7586 break;
7587 case XML_RELAXNG_EXTERNALREF:
7588 fprintf(output, "<externalRef>");
7589 xmlRelaxNGDumpDefines(output, define->content);
7590 fprintf(output, "</externalRef>\n");
7591 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007592 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007593 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 TODO break;
7595 case XML_RELAXNG_START:
7596 case XML_RELAXNG_EXCEPT:
7597 case XML_RELAXNG_PARAM:
7598 TODO break;
7599 case XML_RELAXNG_NOOP:
7600 xmlRelaxNGDumpDefines(output, define->content);
7601 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007602 }
7603}
Daniel Veillard4c004142003-10-07 11:33:24 +00007604
Daniel Veillard6eadf632003-01-23 18:29:16 +00007605/**
7606 * xmlRelaxNGDumpGrammar:
7607 * @output: the file output
7608 * @grammar: a grammar structure
7609 * @top: is this a top grammar
7610 *
7611 * Dump a RelaxNG structure back
7612 */
7613static void
7614xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7615{
7616 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007617 return;
7618
Daniel Veillard6eadf632003-01-23 18:29:16 +00007619 fprintf(output, "<grammar");
7620 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007621 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7622 switch (grammar->combine) {
7623 case XML_RELAXNG_COMBINE_UNDEFINED:
7624 break;
7625 case XML_RELAXNG_COMBINE_CHOICE:
7626 fprintf(output, " combine=\"choice\"");
7627 break;
7628 case XML_RELAXNG_COMBINE_INTERLEAVE:
7629 fprintf(output, " combine=\"interleave\"");
7630 break;
7631 default:
7632 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007633 }
7634 fprintf(output, ">\n");
7635 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007636 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007637 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007638 fprintf(output, "<start>\n");
7639 xmlRelaxNGDumpDefine(output, grammar->start);
7640 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007641 }
7642 /* TODO ? Dump the defines ? */
7643 fprintf(output, "</grammar>\n");
7644}
7645
7646/**
7647 * xmlRelaxNGDump:
7648 * @output: the file output
7649 * @schema: a schema structure
7650 *
7651 * Dump a RelaxNG structure back
7652 */
7653void
7654xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7655{
7656 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007657 fprintf(output, "RelaxNG empty or failed to compile\n");
7658 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007659 }
7660 fprintf(output, "RelaxNG: ");
7661 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007662 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007663 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007664 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007665 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007666 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007667 }
7668 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007669 fprintf(output, "RelaxNG has no top grammar\n");
7670 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007671 }
7672 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7673}
7674
Daniel Veillardfebcca42003-02-16 15:44:18 +00007675/**
7676 * xmlRelaxNGDumpTree:
7677 * @output: the file output
7678 * @schema: a schema structure
7679 *
7680 * Dump the transformed RelaxNG tree.
7681 */
7682void
7683xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7684{
7685 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007686 fprintf(output, "RelaxNG empty or failed to compile\n");
7687 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007688 }
7689 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007690 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007691 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007692 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007693 }
7694}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007695#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007696
Daniel Veillard6eadf632003-01-23 18:29:16 +00007697/************************************************************************
7698 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007699 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007700 * *
7701 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007702static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7703 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007704
7705/**
7706 * xmlRelaxNGValidateCompiledCallback:
7707 * @exec: the regular expression instance
7708 * @token: the token which matched
7709 * @transdata: callback data, the define for the subelement if available
7710 @ @inputdata: callback data, the Relax NG validation context
7711 *
7712 * Handle the callback and if needed validate the element children.
7713 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007714static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007715xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007716 const xmlChar * token,
7717 void *transdata, void *inputdata)
7718{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007719 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7720 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7721 int ret;
7722
7723#ifdef DEBUG_COMPILE
7724 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007725 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007726#endif
7727 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007728 fprintf(stderr, "callback on %s missing context\n", token);
7729 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7730 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7731 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007732 }
7733 if (define == NULL) {
7734 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007735 return;
7736 fprintf(stderr, "callback on %s missing define\n", token);
7737 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7738 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7739 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007740 }
7741 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007742 fprintf(stderr, "callback on %s missing info\n", token);
7743 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7744 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7745 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007746 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007747 fprintf(stderr, "callback on %s define is not element\n", token);
7748 if (ctxt->errNo == XML_RELAXNG_OK)
7749 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7750 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007751 }
7752 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007753 if (ret != 0)
7754 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007755}
7756
7757/**
7758 * xmlRelaxNGValidateCompiledContent:
7759 * @ctxt: the RelaxNG validation context
7760 * @regexp: the regular expression as compiled
7761 * @content: list of children to test against the regexp
7762 *
7763 * Validate the content model of an element or start using the regexp
7764 *
7765 * Returns 0 in case of success, -1 in case of error.
7766 */
7767static int
7768xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007769 xmlRegexpPtr regexp, xmlNodePtr content)
7770{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007771 xmlRegExecCtxtPtr exec;
7772 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007773 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007774 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007775
7776 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007777 return (-1);
7778 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007779 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007780 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007781 cur = content;
7782 while (cur != NULL) {
7783 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007784 switch (cur->type) {
7785 case XML_TEXT_NODE:
7786 case XML_CDATA_SECTION_NODE:
7787 if (xmlIsBlankNode(cur))
7788 break;
7789 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7790 if (ret < 0) {
7791 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7792 cur->parent->name);
7793 }
7794 break;
7795 case XML_ELEMENT_NODE:
7796 if (cur->ns != NULL) {
7797 ret = xmlRegExecPushString2(exec, cur->name,
7798 cur->ns->href, ctxt);
7799 } else {
7800 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7801 }
7802 if (ret < 0) {
7803 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7804 }
7805 break;
7806 default:
7807 break;
7808 }
7809 if (ret < 0)
7810 break;
7811 /*
7812 * Switch to next element
7813 */
7814 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007815 }
7816 ret = xmlRegExecPushString(exec, NULL, NULL);
7817 if (ret == 1) {
7818 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007819 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007820 } else if (ret == 0) {
7821 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007822 * TODO: get some of the names needed to exit the current state of exec
7823 */
7824 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7825 ret = -1;
7826 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7827 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007828 } else {
7829 ret = -1;
7830 }
7831 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007832 /*
7833 * There might be content model errors outside of the pure
7834 * regexp validation, e.g. for attribute values.
7835 */
7836 if ((ret == 0) && (ctxt->perr != 0)) {
7837 ret = ctxt->perr;
7838 }
7839 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007840 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007841}
7842
7843/************************************************************************
7844 * *
7845 * Progressive validation of when possible *
7846 * *
7847 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007848static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7849 xmlRelaxNGDefinePtr defines);
7850static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00007851 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007852static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007853
7854/**
7855 * xmlRelaxNGElemPush:
7856 * @ctxt: the validation context
7857 * @exec: the regexp runtime for the new content model
7858 *
7859 * Push a new regexp for the current node content model on the stack
7860 *
7861 * Returns 0 in case of success and -1 in case of error.
7862 */
7863static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007864xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7865{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007866 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007867 ctxt->elemMax = 10;
7868 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7869 sizeof
7870 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007871 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007872 xmlRngVErrMemory(ctxt, "validating\n");
7873 return (-1);
7874 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007875 }
7876 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007877 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007878 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007879 ctxt->elemMax *
7880 sizeof
7881 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007882 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007883 xmlRngVErrMemory(ctxt, "validating\n");
7884 return (-1);
7885 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007886 }
7887 ctxt->elemTab[ctxt->elemNr++] = exec;
7888 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007889 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007890}
7891
7892/**
7893 * xmlRelaxNGElemPop:
7894 * @ctxt: the validation context
7895 *
7896 * Pop the regexp of the current node content model from the stack
7897 *
7898 * Returns the exec or NULL if empty
7899 */
7900static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007901xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7902{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007903 xmlRegExecCtxtPtr ret;
7904
Daniel Veillard4c004142003-10-07 11:33:24 +00007905 if (ctxt->elemNr <= 0)
7906 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007907 ctxt->elemNr--;
7908 ret = ctxt->elemTab[ctxt->elemNr];
7909 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007910 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007911 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7912 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007913 ctxt->elem = NULL;
7914 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007915}
7916
7917/**
7918 * xmlRelaxNGValidateProgressiveCallback:
7919 * @exec: the regular expression instance
7920 * @token: the token which matched
7921 * @transdata: callback data, the define for the subelement if available
7922 @ @inputdata: callback data, the Relax NG validation context
7923 *
7924 * Handle the callback and if needed validate the element children.
7925 * some of the in/out informations are passed via the context in @inputdata.
7926 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007927static void
7928xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7929 ATTRIBUTE_UNUSED,
7930 const xmlChar * token,
7931 void *transdata, void *inputdata)
7932{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007933 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7934 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007935 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007936 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007937 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007938
7939#ifdef DEBUG_PROGRESSIVE
7940 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007941 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007942#endif
7943 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007944 fprintf(stderr, "callback on %s missing context\n", token);
7945 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007946 }
7947 ctxt->pstate = 1;
7948 if (define == NULL) {
7949 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007950 return;
7951 fprintf(stderr, "callback on %s missing define\n", token);
7952 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7953 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7954 ctxt->pstate = -1;
7955 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007956 }
7957 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007958 fprintf(stderr, "callback on %s missing info\n", token);
7959 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7960 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7961 ctxt->pstate = -1;
7962 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007963 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007964 fprintf(stderr, "callback on %s define is not element\n", token);
7965 if (ctxt->errNo == XML_RELAXNG_OK)
7966 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7967 ctxt->pstate = -1;
7968 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007969 }
7970 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007971 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7972 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7973 xmlRelaxNGDumpValidError(ctxt);
7974 ctxt->pstate = -1;
7975 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007976 }
7977 if (define->contModel == NULL) {
7978 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007979 * this node cannot be validated in a streamable fashion
7980 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00007981#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00007982 xmlGenericError(xmlGenericErrorContext,
7983 "Element '%s' validation is not streamable\n",
7984 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007985#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00007986 ctxt->pstate = 0;
7987 ctxt->pdef = define;
7988 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007989 }
7990 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00007991 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007992 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007993 ctxt->pstate = -1;
7994 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007995 }
7996 xmlRelaxNGElemPush(ctxt, exec);
7997
7998 /*
7999 * Validate the attributes part of the content.
8000 */
8001 state = xmlRelaxNGNewValidState(ctxt, node);
8002 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008003 ctxt->pstate = -1;
8004 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008005 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008006 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008007 ctxt->state = state;
8008 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008009 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8010 if (ret != 0) {
8011 ctxt->pstate = -1;
8012 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8013 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008014 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008015 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008016 ctxt->state->seq = NULL;
8017 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8018 if (ret != 0) {
8019 ctxt->pstate = -1;
8020 }
8021 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008022 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008023 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008024
8025 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008026
Daniel Veillard4c004142003-10-07 11:33:24 +00008027 for (i = 0; i < ctxt->states->nbState; i++) {
8028 state = ctxt->states->tabState[i];
8029 ctxt->state = state;
8030 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008031
Daniel Veillard4c004142003-10-07 11:33:24 +00008032 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8033 tmp = 0;
8034 break;
8035 }
8036 }
8037 if (tmp != 0) {
8038 /*
8039 * validation error, log the message for the "best" one
8040 */
8041 ctxt->flags |= FLAGS_IGNORABLE;
8042 xmlRelaxNGLogBestError(ctxt);
8043 }
8044 for (i = 0; i < ctxt->states->nbState; i++) {
8045 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8046 }
8047 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8048 ctxt->states = NULL;
8049 if ((ret == 0) && (tmp == -1))
8050 ctxt->pstate = -1;
8051 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008052 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008053 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008054 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8055 xmlRelaxNGDumpValidError(ctxt);
8056 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008057 }
8058 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008059}
8060
8061/**
8062 * xmlRelaxNGValidatePushElement:
8063 * @ctxt: the validation context
8064 * @doc: a document instance
8065 * @elem: an element instance
8066 *
8067 * Push a new element start on the RelaxNG validation stack.
8068 *
8069 * returns 1 if no validation problem was found or 0 if validating the
8070 * element requires a full node, and -1 in case of error.
8071 */
8072int
Daniel Veillard33300b42003-04-17 09:09:19 +00008073xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8074 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008075 xmlNodePtr elem)
8076{
8077 int ret = 1;
8078
8079 if ((ctxt == NULL) || (elem == NULL))
8080 return (-1);
8081
8082#ifdef DEBUG_PROGRESSIVE
8083 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8084#endif
8085 if (ctxt->elem == 0) {
8086 xmlRelaxNGPtr schema;
8087 xmlRelaxNGGrammarPtr grammar;
8088 xmlRegExecCtxtPtr exec;
8089 xmlRelaxNGDefinePtr define;
8090
8091 schema = ctxt->schema;
8092 if (schema == NULL) {
8093 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8094 return (-1);
8095 }
8096 grammar = schema->topgrammar;
8097 if ((grammar == NULL) || (grammar->start == NULL)) {
8098 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8099 return (-1);
8100 }
8101 define = grammar->start;
8102 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008103 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008104 return (0);
8105 }
8106 exec = xmlRegNewExecCtxt(define->contModel,
8107 xmlRelaxNGValidateProgressiveCallback,
8108 ctxt);
8109 if (exec == NULL) {
8110 return (-1);
8111 }
8112 xmlRelaxNGElemPush(ctxt, exec);
8113 }
8114 ctxt->pnode = elem;
8115 ctxt->pstate = 0;
8116 if (elem->ns != NULL) {
8117 ret =
8118 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8119 ctxt);
8120 } else {
8121 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8122 }
8123 if (ret < 0) {
8124 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8125 } else {
8126 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008127 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008128 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008129 ret = -1;
8130 else
8131 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008132 }
8133#ifdef DEBUG_PROGRESSIVE
8134 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008135 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8136 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008137#endif
8138 return (ret);
8139}
8140
8141/**
8142 * xmlRelaxNGValidatePushCData:
8143 * @ctxt: the RelaxNG validation context
8144 * @data: some character data read
8145 * @len: the lenght of the data
8146 *
8147 * check the CData parsed for validation in the current stack
8148 *
8149 * returns 1 if no validation problem was found or -1 otherwise
8150 */
8151int
8152xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008153 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008154{
8155 int ret = 1;
8156
8157 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8158 return (-1);
8159
8160#ifdef DEBUG_PROGRESSIVE
8161 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8162#endif
8163
8164 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008165 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008166 break;
8167 data++;
8168 }
8169 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008170 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008171
8172 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8173 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008174 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008175#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008176 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008177#endif
8178
Daniel Veillard4c004142003-10-07 11:33:24 +00008179 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008180 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008181 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008182}
8183
8184/**
8185 * xmlRelaxNGValidatePopElement:
8186 * @ctxt: the RelaxNG validation context
8187 * @doc: a document instance
8188 * @elem: an element instance
8189 *
8190 * Pop the element end from the RelaxNG validation stack.
8191 *
8192 * returns 1 if no validation problem was found or 0 otherwise
8193 */
8194int
8195xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8196 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008197 xmlNodePtr elem)
8198{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008199 int ret;
8200 xmlRegExecCtxtPtr exec;
8201
Daniel Veillard4c004142003-10-07 11:33:24 +00008202 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8203 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008204#ifdef DEBUG_PROGRESSIVE
8205 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8206#endif
8207 /*
8208 * verify that we reached a terminal state of the content model.
8209 */
8210 exec = xmlRelaxNGElemPop(ctxt);
8211 ret = xmlRegExecPushString(exec, NULL, NULL);
8212 if (ret == 0) {
8213 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008214 * TODO: get some of the names needed to exit the current state of exec
8215 */
8216 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8217 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008218 } else if (ret < 0) {
8219 ret = -1;
8220 } else {
8221 ret = 1;
8222 }
8223 xmlRegFreeExecCtxt(exec);
8224#ifdef DEBUG_PROGRESSIVE
8225 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008226 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8227 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008228#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008229 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008230}
8231
8232/**
8233 * xmlRelaxNGValidateFullElement:
8234 * @ctxt: the validation context
8235 * @doc: a document instance
8236 * @elem: an element instance
8237 *
8238 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8239 * 0 and the content of the node has been expanded.
8240 *
8241 * returns 1 if no validation problem was found or -1 in case of error.
8242 */
8243int
Daniel Veillard33300b42003-04-17 09:09:19 +00008244xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8245 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008246 xmlNodePtr elem)
8247{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008248 int ret;
8249 xmlRelaxNGValidStatePtr state;
8250
Daniel Veillard4c004142003-10-07 11:33:24 +00008251 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8252 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008253#ifdef DEBUG_PROGRESSIVE
8254 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8255#endif
8256 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8257 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008258 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008259 }
8260 state->seq = elem;
8261 ctxt->state = state;
8262 ctxt->errNo = XML_RELAXNG_OK;
8263 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008264 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8265 ret = -1;
8266 else
8267 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008268 xmlRelaxNGFreeValidState(ctxt, state);
8269 ctxt->state = NULL;
8270#ifdef DEBUG_PROGRESSIVE
8271 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008272 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8273 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008274#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008275 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008276}
8277
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008278/************************************************************************
8279 * *
8280 * Generic interpreted validation implementation *
8281 * *
8282 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008283static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8284 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008285
8286/**
8287 * xmlRelaxNGSkipIgnored:
8288 * @ctxt: a schema validation context
8289 * @node: the top node.
8290 *
8291 * Skip ignorable nodes in that context
8292 *
8293 * Returns the new sibling or NULL in case of error.
8294 */
8295static xmlNodePtr
8296xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008297 xmlNodePtr node)
8298{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008299 /*
8300 * TODO complete and handle entities
8301 */
8302 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008303 ((node->type == XML_COMMENT_NODE) ||
8304 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008305 (node->type == XML_XINCLUDE_START) ||
8306 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008307 (((node->type == XML_TEXT_NODE) ||
8308 (node->type == XML_CDATA_SECTION_NODE)) &&
8309 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8310 (IS_BLANK_NODE(node)))))) {
8311 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008312 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008313 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008314}
8315
8316/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008317 * xmlRelaxNGNormalize:
8318 * @ctxt: a schema validation context
8319 * @str: the string to normalize
8320 *
8321 * Implements the normalizeWhiteSpace( s ) function from
8322 * section 6.2.9 of the spec
8323 *
8324 * Returns the new string or NULL in case of error.
8325 */
8326static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008327xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8328{
Daniel Veillardedc91922003-01-26 00:52:04 +00008329 xmlChar *ret, *p;
8330 const xmlChar *tmp;
8331 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008332
Daniel Veillardedc91922003-01-26 00:52:04 +00008333 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008334 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008335 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008336 while (*tmp != 0)
8337 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008338 len = tmp - str;
8339
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008340 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008341 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008342 xmlRngVErrMemory(ctxt, "validating\n");
8343 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008344 }
8345 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008346 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008347 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008348 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008349 if (IS_BLANK_CH(*str)) {
8350 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008351 str++;
8352 if (*str == 0)
8353 break;
8354 *p++ = ' ';
8355 } else
8356 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008357 }
8358 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008359 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008360}
8361
8362/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008363 * xmlRelaxNGValidateDatatype:
8364 * @ctxt: a Relax-NG validation context
8365 * @value: the string value
8366 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008367 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008368 *
8369 * Validate the given value against the dataype
8370 *
8371 * Returns 0 if the validation succeeded or an error code.
8372 */
8373static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008374xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8375 const xmlChar * value,
8376 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8377{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008378 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008379 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008380 void *result = NULL;
8381 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008382
8383 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008384 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008385 }
8386 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008387 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008388 if ((define->attrs != NULL) &&
8389 (define->attrs->type == XML_RELAXNG_PARAM)) {
8390 ret =
8391 lib->check(lib->data, define->name, value, &result, node);
8392 } else {
8393 ret = lib->check(lib->data, define->name, value, NULL, node);
8394 }
8395 } else
8396 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008397 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008398 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8399 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8400 lib->freef(lib->data, result);
8401 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008402 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008403 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008404 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008405 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008406 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008407 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8408 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008409 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008410 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008411 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008412 if (lib->facet != NULL) {
8413 tmp = lib->facet(lib->data, define->name, cur->name,
8414 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008415 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008416 ret = -1;
8417 }
8418 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008419 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008420 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008421 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008422
Daniel Veillard4c004142003-10-07 11:33:24 +00008423 oldvalue = ctxt->state->value;
8424 oldendvalue = ctxt->state->endvalue;
8425 ctxt->state->value = (xmlChar *) value;
8426 ctxt->state->endvalue = NULL;
8427 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8428 ctxt->state->value = (xmlChar *) oldvalue;
8429 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008430 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008431 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008432 lib->freef(lib->data, result);
8433 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008434}
8435
8436/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008437 * xmlRelaxNGNextValue:
8438 * @ctxt: a Relax-NG validation context
8439 *
8440 * Skip to the next value when validating within a list
8441 *
8442 * Returns 0 if the operation succeeded or an error code.
8443 */
8444static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008445xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8446{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008447 xmlChar *cur;
8448
8449 cur = ctxt->state->value;
8450 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008451 ctxt->state->value = NULL;
8452 ctxt->state->endvalue = NULL;
8453 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008454 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008455 while (*cur != 0)
8456 cur++;
8457 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8458 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008459 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008460 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008461 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008462 ctxt->state->value = cur;
8463 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008464}
8465
8466/**
8467 * xmlRelaxNGValidateValueList:
8468 * @ctxt: a Relax-NG validation context
8469 * @defines: the list of definitions to verify
8470 *
8471 * Validate the given set of definitions for the current value
8472 *
8473 * Returns 0 if the validation succeeded or an error code.
8474 */
8475static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008476xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8477 xmlRelaxNGDefinePtr defines)
8478{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008479 int ret = 0;
8480
8481 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008482 ret = xmlRelaxNGValidateValue(ctxt, defines);
8483 if (ret != 0)
8484 break;
8485 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008486 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008487 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008488}
8489
8490/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008491 * xmlRelaxNGValidateValue:
8492 * @ctxt: a Relax-NG validation context
8493 * @define: the definition to verify
8494 *
8495 * Validate the given definition for the current value
8496 *
8497 * Returns 0 if the validation succeeded or an error code.
8498 */
8499static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008500xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8501 xmlRelaxNGDefinePtr define)
8502{
Daniel Veillardedc91922003-01-26 00:52:04 +00008503 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008504 xmlChar *value;
8505
8506 value = ctxt->state->value;
8507 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008508 case XML_RELAXNG_EMPTY:{
8509 if ((value != NULL) && (value[0] != 0)) {
8510 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008511
William M. Brack76e95df2003-10-18 16:20:14 +00008512 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008513 idx++;
8514 if (value[idx] != 0)
8515 ret = -1;
8516 }
8517 break;
8518 }
8519 case XML_RELAXNG_TEXT:
8520 break;
8521 case XML_RELAXNG_VALUE:{
8522 if (!xmlStrEqual(value, define->value)) {
8523 if (define->name != NULL) {
8524 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008525
Daniel Veillard4c004142003-10-07 11:33:24 +00008526 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8527 if ((lib != NULL) && (lib->comp != NULL)) {
8528 ret = lib->comp(lib->data, define->name,
8529 define->value, define->node,
8530 (void *) define->attrs,
8531 value, ctxt->state->node);
8532 } else
8533 ret = -1;
8534 if (ret < 0) {
8535 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8536 define->name);
8537 return (-1);
8538 } else if (ret == 1) {
8539 ret = 0;
8540 } else {
8541 ret = -1;
8542 }
8543 } else {
8544 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008545
Daniel Veillard4c004142003-10-07 11:33:24 +00008546 /*
8547 * TODO: trivial optimizations are possible by
8548 * computing at compile-time
8549 */
8550 nval = xmlRelaxNGNormalize(ctxt, define->value);
8551 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008552
Daniel Veillard4c004142003-10-07 11:33:24 +00008553 if ((nval == NULL) || (nvalue == NULL) ||
8554 (!xmlStrEqual(nval, nvalue)))
8555 ret = -1;
8556 if (nval != NULL)
8557 xmlFree(nval);
8558 if (nvalue != NULL)
8559 xmlFree(nvalue);
8560 }
8561 }
8562 if (ret == 0)
8563 xmlRelaxNGNextValue(ctxt);
8564 break;
8565 }
8566 case XML_RELAXNG_DATATYPE:{
8567 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8568 ctxt->state->seq);
8569 if (ret == 0)
8570 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008571
Daniel Veillard4c004142003-10-07 11:33:24 +00008572 break;
8573 }
8574 case XML_RELAXNG_CHOICE:{
8575 xmlRelaxNGDefinePtr list = define->content;
8576 xmlChar *oldvalue;
8577
8578 oldflags = ctxt->flags;
8579 ctxt->flags |= FLAGS_IGNORABLE;
8580
8581 oldvalue = ctxt->state->value;
8582 while (list != NULL) {
8583 ret = xmlRelaxNGValidateValue(ctxt, list);
8584 if (ret == 0) {
8585 break;
8586 }
8587 ctxt->state->value = oldvalue;
8588 list = list->next;
8589 }
8590 ctxt->flags = oldflags;
8591 if (ret != 0) {
8592 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8593 xmlRelaxNGDumpValidError(ctxt);
8594 } else {
8595 if (ctxt->errNr > 0)
8596 xmlRelaxNGPopErrors(ctxt, 0);
8597 }
8598 if (ret == 0)
8599 xmlRelaxNGNextValue(ctxt);
8600 break;
8601 }
8602 case XML_RELAXNG_LIST:{
8603 xmlRelaxNGDefinePtr list = define->content;
8604 xmlChar *oldvalue, *oldend, *val, *cur;
8605
Daniel Veillard416589a2003-02-17 17:25:42 +00008606#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008607 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008608#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008609
Daniel Veillard4c004142003-10-07 11:33:24 +00008610 oldvalue = ctxt->state->value;
8611 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008612
Daniel Veillard4c004142003-10-07 11:33:24 +00008613 val = xmlStrdup(oldvalue);
8614 if (val == NULL) {
8615 val = xmlStrdup(BAD_CAST "");
8616 }
8617 if (val == NULL) {
8618 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8619 return (-1);
8620 }
8621 cur = val;
8622 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008623 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008624 *cur = 0;
8625 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008626#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008627 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008628#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008629 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008630 *cur++ = 0;
8631 } else
8632 cur++;
8633 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008634#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008635 xmlGenericError(xmlGenericErrorContext,
8636 "list value: '%s' found %d items\n",
8637 oldvalue, nb_values);
8638 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008639#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008640 ctxt->state->endvalue = cur;
8641 cur = val;
8642 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8643 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008644
Daniel Veillard4c004142003-10-07 11:33:24 +00008645 ctxt->state->value = cur;
8646
8647 while (list != NULL) {
8648 if (ctxt->state->value == ctxt->state->endvalue)
8649 ctxt->state->value = NULL;
8650 ret = xmlRelaxNGValidateValue(ctxt, list);
8651 if (ret != 0) {
8652#ifdef DEBUG_LIST
8653 xmlGenericError(xmlGenericErrorContext,
8654 "Failed to validate value: '%s' with %d rule\n",
8655 ctxt->state->value, nb_values);
8656#endif
8657 break;
8658 }
8659#ifdef DEBUG_LIST
8660 nb_values++;
8661#endif
8662 list = list->next;
8663 }
8664
8665 if ((ret == 0) && (ctxt->state->value != NULL) &&
8666 (ctxt->state->value != ctxt->state->endvalue)) {
8667 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8668 ctxt->state->value);
8669 ret = -1;
8670 }
8671 xmlFree(val);
8672 ctxt->state->value = oldvalue;
8673 ctxt->state->endvalue = oldend;
8674 break;
8675 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008676 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008677 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8678 if (ret != 0) {
8679 break;
8680 }
8681 /* no break on purpose */
8682 case XML_RELAXNG_ZEROORMORE:{
8683 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008684
Daniel Veillard4c004142003-10-07 11:33:24 +00008685 oldflags = ctxt->flags;
8686 ctxt->flags |= FLAGS_IGNORABLE;
8687 cur = ctxt->state->value;
8688 temp = NULL;
8689 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8690 (temp != cur)) {
8691 temp = cur;
8692 ret =
8693 xmlRelaxNGValidateValueList(ctxt, define->content);
8694 if (ret != 0) {
8695 ctxt->state->value = temp;
8696 ret = 0;
8697 break;
8698 }
8699 cur = ctxt->state->value;
8700 }
8701 ctxt->flags = oldflags;
8702 if (ret != 0) {
8703 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8704 xmlRelaxNGDumpValidError(ctxt);
8705 } else {
8706 if (ctxt->errNr > 0)
8707 xmlRelaxNGPopErrors(ctxt, 0);
8708 }
8709 break;
8710 }
8711 case XML_RELAXNG_EXCEPT:{
8712 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008713
Daniel Veillard4c004142003-10-07 11:33:24 +00008714 list = define->content;
8715 while (list != NULL) {
8716 ret = xmlRelaxNGValidateValue(ctxt, list);
8717 if (ret == 0) {
8718 ret = -1;
8719 break;
8720 } else
8721 ret = 0;
8722 list = list->next;
8723 }
8724 break;
8725 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008726 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008727 case XML_RELAXNG_GROUP:{
8728 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008729
Daniel Veillard4c004142003-10-07 11:33:24 +00008730 list = define->content;
8731 while (list != NULL) {
8732 ret = xmlRelaxNGValidateValue(ctxt, list);
8733 if (ret != 0) {
8734 ret = -1;
8735 break;
8736 } else
8737 ret = 0;
8738 list = list->next;
8739 }
8740 break;
8741 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008742 case XML_RELAXNG_REF:
8743 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008744 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8745 break;
8746 default:
8747 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008748 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008749 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008750}
8751
8752/**
8753 * xmlRelaxNGValidateValueContent:
8754 * @ctxt: a Relax-NG validation context
8755 * @defines: the list of definitions to verify
8756 *
8757 * Validate the given definitions for the current value
8758 *
8759 * Returns 0 if the validation succeeded or an error code.
8760 */
8761static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008762xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8763 xmlRelaxNGDefinePtr defines)
8764{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008765 int ret = 0;
8766
8767 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008768 ret = xmlRelaxNGValidateValue(ctxt, defines);
8769 if (ret != 0)
8770 break;
8771 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008772 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008773 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008774}
8775
8776/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008777 * xmlRelaxNGAttributeMatch:
8778 * @ctxt: a Relax-NG validation context
8779 * @define: the definition to check
8780 * @prop: the attribute
8781 *
8782 * Check if the attribute matches the definition nameClass
8783 *
8784 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8785 */
8786static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008787xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8788 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8789{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008790 int ret;
8791
8792 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008793 if (!xmlStrEqual(define->name, prop->name))
8794 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008795 }
8796 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008797 if (define->ns[0] == 0) {
8798 if (prop->ns != NULL)
8799 return (0);
8800 } else {
8801 if ((prop->ns == NULL) ||
8802 (!xmlStrEqual(define->ns, prop->ns->href)))
8803 return (0);
8804 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008805 }
8806 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008807 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008808 define = define->nameClass;
8809 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008810 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008811
Daniel Veillard4c004142003-10-07 11:33:24 +00008812 list = define->content;
8813 while (list != NULL) {
8814 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8815 if (ret == 1)
8816 return (0);
8817 if (ret < 0)
8818 return (ret);
8819 list = list->next;
8820 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008821 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008822 TODO}
8823 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008824}
8825
8826/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008827 * xmlRelaxNGValidateAttribute:
8828 * @ctxt: a Relax-NG validation context
8829 * @define: the definition to verify
8830 *
8831 * Validate the given attribute definition for that node
8832 *
8833 * Returns 0 if the validation succeeded or an error code.
8834 */
8835static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008836xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8837 xmlRelaxNGDefinePtr define)
8838{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008839 int ret = 0, i;
8840 xmlChar *value, *oldvalue;
8841 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008842 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008843
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008844 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008845 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008846 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008847 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8848 tmp = ctxt->state->attrs[i];
8849 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8850 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8851 (tmp->ns == NULL)) ||
8852 ((tmp->ns != NULL) &&
8853 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8854 prop = tmp;
8855 break;
8856 }
8857 }
8858 }
8859 if (prop != NULL) {
8860 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8861 oldvalue = ctxt->state->value;
8862 oldseq = ctxt->state->seq;
8863 ctxt->state->seq = (xmlNodePtr) prop;
8864 ctxt->state->value = value;
8865 ctxt->state->endvalue = NULL;
8866 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8867 if (ctxt->state->value != NULL)
8868 value = ctxt->state->value;
8869 if (value != NULL)
8870 xmlFree(value);
8871 ctxt->state->value = oldvalue;
8872 ctxt->state->seq = oldseq;
8873 if (ret == 0) {
8874 /*
8875 * flag the attribute as processed
8876 */
8877 ctxt->state->attrs[i] = NULL;
8878 ctxt->state->nbAttrLeft--;
8879 }
8880 } else {
8881 ret = -1;
8882 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008883#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008884 xmlGenericError(xmlGenericErrorContext,
8885 "xmlRelaxNGValidateAttribute(%s): %d\n",
8886 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008887#endif
8888 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008889 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8890 tmp = ctxt->state->attrs[i];
8891 if ((tmp != NULL) &&
8892 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8893 prop = tmp;
8894 break;
8895 }
8896 }
8897 if (prop != NULL) {
8898 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8899 oldvalue = ctxt->state->value;
8900 oldseq = ctxt->state->seq;
8901 ctxt->state->seq = (xmlNodePtr) prop;
8902 ctxt->state->value = value;
8903 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8904 if (ctxt->state->value != NULL)
8905 value = ctxt->state->value;
8906 if (value != NULL)
8907 xmlFree(value);
8908 ctxt->state->value = oldvalue;
8909 ctxt->state->seq = oldseq;
8910 if (ret == 0) {
8911 /*
8912 * flag the attribute as processed
8913 */
8914 ctxt->state->attrs[i] = NULL;
8915 ctxt->state->nbAttrLeft--;
8916 }
8917 } else {
8918 ret = -1;
8919 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008920#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008921 if (define->ns != NULL) {
8922 xmlGenericError(xmlGenericErrorContext,
8923 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8924 define->ns, ret);
8925 } else {
8926 xmlGenericError(xmlGenericErrorContext,
8927 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8928 ret);
8929 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008930#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008931 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008932
8933 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008934}
8935
8936/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008937 * xmlRelaxNGValidateAttributeList:
8938 * @ctxt: a Relax-NG validation context
8939 * @define: the list of definition to verify
8940 *
8941 * Validate the given node against the list of attribute definitions
8942 *
8943 * Returns 0 if the validation succeeded or an error code.
8944 */
8945static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008946xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8947 xmlRelaxNGDefinePtr defines)
8948{
Daniel Veillardce192eb2003-04-16 15:58:05 +00008949 int ret = 0, res;
8950 int needmore = 0;
8951 xmlRelaxNGDefinePtr cur;
8952
8953 cur = defines;
8954 while (cur != NULL) {
8955 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008956 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8957 ret = -1;
8958 } else
8959 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008960 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008961 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008962 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00008963 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008964 cur = defines;
8965 while (cur != NULL) {
8966 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008967 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8968 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8969 if (res < 0)
8970 ret = -1;
8971 } else {
8972 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8973 return (-1);
8974 }
8975 if (res == -1) /* continues on -2 */
8976 break;
8977 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008978 cur = cur->next;
8979 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008980
8981 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008982}
8983
8984/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008985 * xmlRelaxNGNodeMatchesList:
8986 * @node: the node
8987 * @list: a NULL terminated array of definitions
8988 *
8989 * Check if a node can be matched by one of the definitions
8990 *
8991 * Returns 1 if matches 0 otherwise
8992 */
8993static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008994xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
8995{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008996 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008997 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008998
8999 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009000 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009001
9002 cur = list[i++];
9003 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009004 if ((node->type == XML_ELEMENT_NODE) &&
9005 (cur->type == XML_RELAXNG_ELEMENT)) {
9006 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9007 if (tmp == 1)
9008 return (1);
9009 } else if (((node->type == XML_TEXT_NODE) ||
9010 (node->type == XML_CDATA_SECTION_NODE)) &&
9011 (cur->type == XML_RELAXNG_TEXT)) {
9012 return (1);
9013 }
9014 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009015 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009016 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009017}
9018
9019/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009020 * xmlRelaxNGValidateInterleave:
9021 * @ctxt: a Relax-NG validation context
9022 * @define: the definition to verify
9023 *
9024 * Validate an interleave definition for a node.
9025 *
9026 * Returns 0 if the validation succeeded or an error code.
9027 */
9028static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009029xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9030 xmlRelaxNGDefinePtr define)
9031{
William M. Brack779af002003-08-01 15:55:39 +00009032 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009033 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009034 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009035
9036 xmlRelaxNGValidStatePtr oldstate;
9037 xmlRelaxNGPartitionPtr partitions;
9038 xmlRelaxNGInterleaveGroupPtr group = NULL;
9039 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9040 xmlNodePtr *list = NULL, *lasts = NULL;
9041
9042 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009043 partitions = (xmlRelaxNGPartitionPtr) define->data;
9044 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009045 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009046 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9047 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009048 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009049 /*
9050 * Optimizations for MIXED
9051 */
9052 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009053 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009054 ctxt->flags |= FLAGS_MIXED_CONTENT;
9055 if (nbgroups == 2) {
9056 /*
9057 * this is a pure <mixed> case
9058 */
9059 if (ctxt->state != NULL)
9060 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9061 ctxt->state->seq);
9062 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9063 ret = xmlRelaxNGValidateDefinition(ctxt,
9064 partitions->groups[1]->
9065 rule);
9066 else
9067 ret = xmlRelaxNGValidateDefinition(ctxt,
9068 partitions->groups[0]->
9069 rule);
9070 if (ret == 0) {
9071 if (ctxt->state != NULL)
9072 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9073 ctxt->state->
9074 seq);
9075 }
9076 ctxt->flags = oldflags;
9077 return (ret);
9078 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009079 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009080
9081 /*
9082 * Build arrays to store the first and last node of the chain
9083 * pertaining to each group
9084 */
9085 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9086 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009087 xmlRngVErrMemory(ctxt, "validating\n");
9088 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009089 }
9090 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9091 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9092 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009093 xmlRngVErrMemory(ctxt, "validating\n");
9094 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009095 }
9096 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9097
9098 /*
9099 * Walk the sequence of children finding the right group and
9100 * sorting them in sequences.
9101 */
9102 cur = ctxt->state->seq;
9103 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9104 start = cur;
9105 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009106 ctxt->state->seq = cur;
9107 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009108 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009109 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009110
Daniel Veillard4c004142003-10-07 11:33:24 +00009111 if ((cur->type == XML_TEXT_NODE) ||
9112 (cur->type == XML_CDATA_SECTION_NODE)) {
9113 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9114 NULL);
9115 } else if (cur->type == XML_ELEMENT_NODE) {
9116 if (cur->ns != NULL) {
9117 tmp = xmlHashLookup2(partitions->triage, cur->name,
9118 cur->ns->href);
9119 if (tmp == NULL)
9120 tmp = xmlHashLookup2(partitions->triage,
9121 BAD_CAST "#any",
9122 cur->ns->href);
9123 } else
9124 tmp =
9125 xmlHashLookup2(partitions->triage, cur->name,
9126 NULL);
9127 if (tmp == NULL)
9128 tmp =
9129 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9130 NULL);
9131 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009132
Daniel Veillard4c004142003-10-07 11:33:24 +00009133 if (tmp == NULL) {
9134 i = nbgroups;
9135 } else {
9136 i = ((long) tmp) - 1;
9137 if (partitions->flags & IS_NEEDCHECK) {
9138 group = partitions->groups[i];
9139 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9140 i = nbgroups;
9141 }
9142 }
9143 } else {
9144 for (i = 0; i < nbgroups; i++) {
9145 group = partitions->groups[i];
9146 if (group == NULL)
9147 continue;
9148 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9149 break;
9150 }
9151 }
9152 /*
9153 * We break as soon as an element not matched is found
9154 */
9155 if (i >= nbgroups) {
9156 break;
9157 }
9158 if (lasts[i] != NULL) {
9159 lasts[i]->next = cur;
9160 lasts[i] = cur;
9161 } else {
9162 list[i] = cur;
9163 lasts[i] = cur;
9164 }
9165 if (cur->next != NULL)
9166 lastchg = cur->next;
9167 else
9168 lastchg = cur;
9169 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009170 }
9171 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009172 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9173 ret = -1;
9174 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009175 }
9176 lastelem = cur;
9177 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009178 for (i = 0; i < nbgroups; i++) {
9179 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9180 group = partitions->groups[i];
9181 if (lasts[i] != NULL) {
9182 last = lasts[i]->next;
9183 lasts[i]->next = NULL;
9184 }
9185 ctxt->state->seq = list[i];
9186 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9187 if (ret != 0)
9188 break;
9189 if (ctxt->state != NULL) {
9190 cur = ctxt->state->seq;
9191 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9192 xmlRelaxNGFreeValidState(ctxt, oldstate);
9193 oldstate = ctxt->state;
9194 ctxt->state = NULL;
9195 if (cur != NULL) {
9196 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9197 ret = -1;
9198 ctxt->state = oldstate;
9199 goto done;
9200 }
9201 } else if (ctxt->states != NULL) {
9202 int j;
9203 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009204
Daniel Veillard4c004142003-10-07 11:33:24 +00009205 for (j = 0; j < ctxt->states->nbState; j++) {
9206 cur = ctxt->states->tabState[j]->seq;
9207 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9208 if (cur == NULL) {
9209 found = 1;
9210 break;
9211 }
9212 }
9213 if (ctxt->states->nbState > 0) {
9214 xmlRelaxNGFreeValidState(ctxt, oldstate);
9215 oldstate =
9216 ctxt->states->tabState[ctxt->states->nbState - 1];
9217 }
9218 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9219 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9220 }
9221 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9222 ctxt->states = NULL;
9223 if (found == 0) {
9224 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9225 ret = -1;
9226 ctxt->state = oldstate;
9227 goto done;
9228 }
9229 } else {
9230 ret = -1;
9231 break;
9232 }
9233 if (lasts[i] != NULL) {
9234 lasts[i]->next = last;
9235 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009236 }
9237 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009238 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009239 ctxt->state = oldstate;
9240 ctxt->state->seq = lastelem;
9241 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009242 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9243 ret = -1;
9244 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009245 }
9246
Daniel Veillard4c004142003-10-07 11:33:24 +00009247 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009248 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009249 /*
9250 * builds the next links chain from the prev one
9251 */
9252 cur = lastchg;
9253 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009254 if ((cur == start) || (cur->prev == NULL))
9255 break;
9256 cur->prev->next = cur;
9257 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009258 }
9259 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009260 if (ctxt->errNr > errNr)
9261 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009262 }
9263
9264 xmlFree(list);
9265 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009266 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009267}
9268
9269/**
9270 * xmlRelaxNGValidateDefinitionList:
9271 * @ctxt: a Relax-NG validation context
9272 * @define: the list of definition to verify
9273 *
9274 * Validate the given node content against the (list) of definitions
9275 *
9276 * Returns 0 if the validation succeeded or an error code.
9277 */
9278static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009279xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9280 xmlRelaxNGDefinePtr defines)
9281{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009282 int ret = 0, res;
9283
9284
Daniel Veillard952379b2003-03-17 15:37:12 +00009285 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009286 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9287 BAD_CAST "NULL definition list");
9288 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009289 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009290 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009291 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9292 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9293 if (res < 0)
9294 ret = -1;
9295 } else {
9296 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9297 return (-1);
9298 }
9299 if (res == -1) /* continues on -2 */
9300 break;
9301 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009302 }
9303
Daniel Veillard4c004142003-10-07 11:33:24 +00009304 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009305}
9306
9307/**
9308 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009309 * @ctxt: a Relax-NG validation context
9310 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009311 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009312 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009313 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009314 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009315 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009316 */
9317static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009318xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9319 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9320{
Daniel Veillard580ced82003-03-21 21:22:48 +00009321 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009322
Daniel Veillardfd573f12003-03-16 17:52:32 +00009323 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009324 if (!xmlStrEqual(elem->name, define->name)) {
9325 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9326 return (0);
9327 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009328 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009329 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009330 if (elem->ns == NULL) {
9331 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9332 return (0);
9333 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9334 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9335 elem->name, define->ns);
9336 return (0);
9337 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009338 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009339 (define->name == NULL)) {
9340 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9341 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009342 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009343 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9344 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009345 }
9346
9347 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009348 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009349
9350 define = define->nameClass;
9351 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009352 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009353
Daniel Veillard4c004142003-10-07 11:33:24 +00009354 if (ctxt != NULL) {
9355 oldflags = ctxt->flags;
9356 ctxt->flags |= FLAGS_IGNORABLE;
9357 }
9358
9359 list = define->content;
9360 while (list != NULL) {
9361 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9362 if (ret == 1) {
9363 if (ctxt != NULL)
9364 ctxt->flags = oldflags;
9365 return (0);
9366 }
9367 if (ret < 0) {
9368 if (ctxt != NULL)
9369 ctxt->flags = oldflags;
9370 return (ret);
9371 }
9372 list = list->next;
9373 }
9374 ret = 1;
9375 if (ctxt != NULL) {
9376 ctxt->flags = oldflags;
9377 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009378 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009379 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009380
Daniel Veillard4c004142003-10-07 11:33:24 +00009381 if (ctxt != NULL) {
9382 oldflags = ctxt->flags;
9383 ctxt->flags |= FLAGS_IGNORABLE;
9384 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009385
Daniel Veillard4c004142003-10-07 11:33:24 +00009386 list = define->nameClass;
9387 while (list != NULL) {
9388 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9389 if (ret == 1) {
9390 if (ctxt != NULL)
9391 ctxt->flags = oldflags;
9392 return (1);
9393 }
9394 if (ret < 0) {
9395 if (ctxt != NULL)
9396 ctxt->flags = oldflags;
9397 return (ret);
9398 }
9399 list = list->next;
9400 }
9401 if (ctxt != NULL) {
9402 if (ret != 0) {
9403 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9404 xmlRelaxNGDumpValidError(ctxt);
9405 } else {
9406 if (ctxt->errNr > 0)
9407 xmlRelaxNGPopErrors(ctxt, 0);
9408 }
9409 }
9410 ret = 0;
9411 if (ctxt != NULL) {
9412 ctxt->flags = oldflags;
9413 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009414 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009415 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009416 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009417 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009418}
9419
9420/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009421 * xmlRelaxNGBestState:
9422 * @ctxt: a Relax-NG validation context
9423 *
9424 * Find the "best" state in the ctxt->states list of states to report
9425 * errors about. I.e. a state with no element left in the child list
9426 * or the one with the less attributes left.
9427 * This is called only if a falidation error was detected
9428 *
9429 * Returns the index of the "best" state or -1 in case of error
9430 */
9431static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009432xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9433{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009434 xmlRelaxNGValidStatePtr state;
9435 int i, tmp;
9436 int best = -1;
9437 int value = 1000000;
9438
9439 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9440 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009441 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009442
Daniel Veillard4c004142003-10-07 11:33:24 +00009443 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009444 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009445 if (state == NULL)
9446 continue;
9447 if (state->seq != NULL) {
9448 if ((best == -1) || (value > 100000)) {
9449 value = 100000;
9450 best = i;
9451 }
9452 } else {
9453 tmp = state->nbAttrLeft;
9454 if ((best == -1) || (value > tmp)) {
9455 value = tmp;
9456 best = i;
9457 }
9458 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009459 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009460 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009461}
9462
9463/**
9464 * xmlRelaxNGLogBestError:
9465 * @ctxt: a Relax-NG validation context
9466 *
9467 * Find the "best" state in the ctxt->states list of states to report
9468 * errors about and log it.
9469 */
9470static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009471xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9472{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009473 int best;
9474
9475 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9476 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009477 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009478
9479 best = xmlRelaxNGBestState(ctxt);
9480 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009481 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009482
Daniel Veillard4c004142003-10-07 11:33:24 +00009483 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009484 }
9485}
9486
9487/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009488 * xmlRelaxNGValidateElementEnd:
9489 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009490 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009491 *
9492 * Validate the end of the element, implements check that
9493 * there is nothing left not consumed in the element content
9494 * or in the attribute list.
9495 *
9496 * Returns 0 if the validation succeeded or an error code.
9497 */
9498static int
William M. Brack272693c2003-11-14 16:20:34 +00009499xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009500{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009501 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009502 xmlRelaxNGValidStatePtr state;
9503
9504 state = ctxt->state;
9505 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009506 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9507 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009508 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009509 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9510 state->node->name, state->seq->name);
9511 }
9512 return (-1);
9513 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009514 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009515 for (i = 0; i < state->nbAttrs; i++) {
9516 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009517 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009518 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9519 state->attrs[i]->name, state->node->name);
9520 }
9521 return (-1 - i);
9522 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009523 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009524 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009525}
9526
9527/**
9528 * xmlRelaxNGValidateState:
9529 * @ctxt: a Relax-NG validation context
9530 * @define: the definition to verify
9531 *
9532 * Validate the current state against the definition
9533 *
9534 * Returns 0 if the validation succeeded or an error code.
9535 */
9536static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009537xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9538 xmlRelaxNGDefinePtr define)
9539{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009540 xmlNodePtr node;
9541 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009542 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009543
9544 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009545 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9546 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009547 }
9548
9549 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009550 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009551 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009552 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009553 }
9554#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009555 for (i = 0; i < ctxt->depth; i++)
9556 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009557 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009558 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009559 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009560 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009561 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009562 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009563 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009564 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009565#endif
9566 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009567 switch (define->type) {
9568 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009569 node = xmlRelaxNGSkipIgnored(ctxt, node);
9570 ret = 0;
9571 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009572 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009573 ret = -1;
9574 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009575 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009576 while ((node != NULL) &&
9577 ((node->type == XML_TEXT_NODE) ||
9578 (node->type == XML_COMMENT_NODE) ||
9579 (node->type == XML_PI_NODE) ||
9580 (node->type == XML_CDATA_SECTION_NODE)))
9581 node = node->next;
9582 ctxt->state->seq = node;
9583 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009584 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009585 errNr = ctxt->errNr;
9586 node = xmlRelaxNGSkipIgnored(ctxt, node);
9587 if (node == NULL) {
9588 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9589 ret = -1;
9590 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9591 xmlRelaxNGDumpValidError(ctxt);
9592 break;
9593 }
9594 if (node->type != XML_ELEMENT_NODE) {
9595 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9596 ret = -1;
9597 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9598 xmlRelaxNGDumpValidError(ctxt);
9599 break;
9600 }
9601 /*
9602 * This node was already validated successfully against
9603 * this definition.
9604 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009605 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009606 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9607 if (ctxt->errNr > errNr)
9608 xmlRelaxNGPopErrors(ctxt, errNr);
9609 if (ctxt->errNr != 0) {
9610 while ((ctxt->err != NULL) &&
9611 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9612 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9613 ||
9614 ((ctxt->err->err ==
9615 XML_RELAXNG_ERR_ELEMEXTRANS)
9616 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9617 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9618 || (ctxt->err->err ==
9619 XML_RELAXNG_ERR_NOTELEM)))
9620 xmlRelaxNGValidErrorPop(ctxt);
9621 }
9622 break;
9623 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009624
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009625 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9626 if (ret <= 0) {
9627 ret = -1;
9628 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9629 xmlRelaxNGDumpValidError(ctxt);
9630 break;
9631 }
9632 ret = 0;
9633 if (ctxt->errNr != 0) {
9634 if (ctxt->errNr > errNr)
9635 xmlRelaxNGPopErrors(ctxt, errNr);
9636 while ((ctxt->err != NULL) &&
9637 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9638 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9639 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9640 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9641 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9642 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9643 xmlRelaxNGValidErrorPop(ctxt);
9644 }
9645 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009646
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009647 oldflags = ctxt->flags;
9648 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9649 ctxt->flags -= FLAGS_MIXED_CONTENT;
9650 }
9651 state = xmlRelaxNGNewValidState(ctxt, node);
9652 if (state == NULL) {
9653 ret = -1;
9654 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9655 xmlRelaxNGDumpValidError(ctxt);
9656 break;
9657 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009658
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009659 oldstate = ctxt->state;
9660 ctxt->state = state;
9661 if (define->attrs != NULL) {
9662 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9663 if (tmp != 0) {
9664 ret = -1;
9665 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9666 }
9667 }
9668 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009669 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9670 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9671 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009672
Daniel Veillard4c004142003-10-07 11:33:24 +00009673 nstate = xmlRelaxNGNewValidState(ctxt, node);
9674 ctxt->state = nstate;
9675 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009676
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009677 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9678 define->contModel,
9679 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009680 nseq = ctxt->state->seq;
9681 ctxt->state = tmpstate;
9682 ctxt->states = tmpstates;
9683 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009684
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009685#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009686 xmlGenericError(xmlGenericErrorContext,
9687 "Validating content of '%s' : %d\n",
9688 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009689#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009690 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009691 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009692
9693 if (ctxt->states != NULL) {
9694 tmp = -1;
9695
Daniel Veillardce192eb2003-04-16 15:58:05 +00009696 for (i = 0; i < ctxt->states->nbState; i++) {
9697 state = ctxt->states->tabState[i];
9698 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009699 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009700
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009701 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009702 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009703 break;
9704 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009705 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009706 if (tmp != 0) {
9707 /*
9708 * validation error, log the message for the "best" one
9709 */
9710 ctxt->flags |= FLAGS_IGNORABLE;
9711 xmlRelaxNGLogBestError(ctxt);
9712 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009713 for (i = 0; i < ctxt->states->nbState; i++) {
9714 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009715 ctxt->states->
9716 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009717 }
9718 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9719 ctxt->flags = oldflags;
9720 ctxt->states = NULL;
9721 if ((ret == 0) && (tmp == -1))
9722 ret = -1;
9723 } else {
9724 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009725 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009726 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009727 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009728 xmlRelaxNGFreeValidState(ctxt, state);
9729 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009730 } else {
9731 if (define->content != NULL) {
9732 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009733 define->
9734 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009735 if (tmp != 0) {
9736 ret = -1;
9737 if (ctxt->state == NULL) {
9738 ctxt->state = oldstate;
9739 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9740 node->name);
9741 ctxt->state = NULL;
9742 } else {
9743 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9744 node->name);
9745 }
9746
9747 }
9748 }
9749 if (ctxt->states != NULL) {
9750 tmp = -1;
9751
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009752 for (i = 0; i < ctxt->states->nbState; i++) {
9753 state = ctxt->states->tabState[i];
9754 ctxt->state = state;
9755
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009756 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009757 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009758 break;
9759 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009760 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009761 if (tmp != 0) {
9762 /*
9763 * validation error, log the message for the "best" one
9764 */
9765 ctxt->flags |= FLAGS_IGNORABLE;
9766 xmlRelaxNGLogBestError(ctxt);
9767 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009768 for (i = 0; i < ctxt->states->nbState; i++) {
9769 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009770 ctxt->states->
9771 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009772 }
9773 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9774 ctxt->flags = oldflags;
9775 ctxt->states = NULL;
9776 if ((ret == 0) && (tmp == -1))
9777 ret = -1;
9778 } else {
9779 state = ctxt->state;
9780 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009781 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009782 xmlRelaxNGFreeValidState(ctxt, state);
9783 }
9784 }
9785 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +00009786 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009787 }
9788 ctxt->flags = oldflags;
9789 ctxt->state = oldstate;
9790 if (oldstate != NULL)
9791 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9792 if (ret != 0) {
9793 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9794 xmlRelaxNGDumpValidError(ctxt);
9795 ret = 0;
9796 } else {
9797 ret = -2;
9798 }
9799 } else {
9800 if (ctxt->errNr > errNr)
9801 xmlRelaxNGPopErrors(ctxt, errNr);
9802 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009803
9804#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009805 xmlGenericError(xmlGenericErrorContext,
9806 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9807 node->name, ret);
9808 if (oldstate == NULL)
9809 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9810 else if (oldstate->seq == NULL)
9811 xmlGenericError(xmlGenericErrorContext, ": done\n");
9812 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9813 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9814 oldstate->seq->name);
9815 else
9816 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9817 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009818#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009819 break;
9820 case XML_RELAXNG_OPTIONAL:{
9821 errNr = ctxt->errNr;
9822 oldflags = ctxt->flags;
9823 ctxt->flags |= FLAGS_IGNORABLE;
9824 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9825 ret =
9826 xmlRelaxNGValidateDefinitionList(ctxt,
9827 define->content);
9828 if (ret != 0) {
9829 if (ctxt->state != NULL)
9830 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9831 ctxt->state = oldstate;
9832 ctxt->flags = oldflags;
9833 ret = 0;
9834 if (ctxt->errNr > errNr)
9835 xmlRelaxNGPopErrors(ctxt, errNr);
9836 break;
9837 }
9838 if (ctxt->states != NULL) {
9839 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9840 } else {
9841 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9842 if (ctxt->states == NULL) {
9843 xmlRelaxNGFreeValidState(ctxt, oldstate);
9844 ctxt->flags = oldflags;
9845 ret = -1;
9846 if (ctxt->errNr > errNr)
9847 xmlRelaxNGPopErrors(ctxt, errNr);
9848 break;
9849 }
9850 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9851 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9852 ctxt->state = NULL;
9853 }
9854 ctxt->flags = oldflags;
9855 ret = 0;
9856 if (ctxt->errNr > errNr)
9857 xmlRelaxNGPopErrors(ctxt, errNr);
9858 break;
9859 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009860 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009861 errNr = ctxt->errNr;
9862 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9863 if (ret != 0) {
9864 break;
9865 }
9866 if (ctxt->errNr > errNr)
9867 xmlRelaxNGPopErrors(ctxt, errNr);
9868 /* no break on purpose */
9869 case XML_RELAXNG_ZEROORMORE:{
9870 int progress;
9871 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9872 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009873
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009874 errNr = ctxt->errNr;
9875 res = xmlRelaxNGNewStates(ctxt, 1);
9876 if (res == NULL) {
9877 ret = -1;
9878 break;
9879 }
9880 /*
9881 * All the input states are also exit states
9882 */
9883 if (ctxt->state != NULL) {
9884 xmlRelaxNGAddStates(ctxt, res,
9885 xmlRelaxNGCopyValidState(ctxt,
9886 ctxt->
9887 state));
9888 } else {
9889 for (j = 0; j < ctxt->states->nbState; j++) {
9890 xmlRelaxNGAddStates(ctxt, res,
9891 xmlRelaxNGCopyValidState(ctxt,
9892 ctxt->
9893 states->
9894 tabState
9895 [j]));
9896 }
9897 }
9898 oldflags = ctxt->flags;
9899 ctxt->flags |= FLAGS_IGNORABLE;
9900 do {
9901 progress = 0;
9902 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009903
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009904 if (ctxt->states != NULL) {
9905 states = ctxt->states;
9906 for (i = 0; i < states->nbState; i++) {
9907 ctxt->state = states->tabState[i];
9908 ctxt->states = NULL;
9909 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9910 define->
9911 content);
9912 if (ret == 0) {
9913 if (ctxt->state != NULL) {
9914 tmp = xmlRelaxNGAddStates(ctxt, res,
9915 ctxt->state);
9916 ctxt->state = NULL;
9917 if (tmp == 1)
9918 progress = 1;
9919 } else if (ctxt->states != NULL) {
9920 for (j = 0; j < ctxt->states->nbState;
9921 j++) {
9922 tmp =
9923 xmlRelaxNGAddStates(ctxt, res,
9924 ctxt->
9925 states->
9926 tabState
9927 [j]);
9928 if (tmp == 1)
9929 progress = 1;
9930 }
9931 xmlRelaxNGFreeStates(ctxt,
9932 ctxt->states);
9933 ctxt->states = NULL;
9934 }
9935 } else {
9936 if (ctxt->state != NULL) {
9937 xmlRelaxNGFreeValidState(ctxt,
9938 ctxt->state);
9939 ctxt->state = NULL;
9940 }
9941 }
9942 }
9943 } else {
9944 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9945 define->
9946 content);
9947 if (ret != 0) {
9948 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9949 ctxt->state = NULL;
9950 } else {
9951 base = res->nbState;
9952 if (ctxt->state != NULL) {
9953 tmp = xmlRelaxNGAddStates(ctxt, res,
9954 ctxt->state);
9955 ctxt->state = NULL;
9956 if (tmp == 1)
9957 progress = 1;
9958 } else if (ctxt->states != NULL) {
9959 for (j = 0; j < ctxt->states->nbState; j++) {
9960 tmp = xmlRelaxNGAddStates(ctxt, res,
9961 ctxt->
9962 states->
9963 tabState[j]);
9964 if (tmp == 1)
9965 progress = 1;
9966 }
9967 if (states == NULL) {
9968 states = ctxt->states;
9969 } else {
9970 xmlRelaxNGFreeStates(ctxt,
9971 ctxt->states);
9972 }
9973 ctxt->states = NULL;
9974 }
9975 }
9976 }
9977 if (progress) {
9978 /*
9979 * Collect all the new nodes added at that step
9980 * and make them the new node set
9981 */
9982 if (res->nbState - base == 1) {
9983 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9984 res->
9985 tabState
9986 [base]);
9987 } else {
9988 if (states == NULL) {
9989 xmlRelaxNGNewStates(ctxt,
9990 res->nbState - base);
9991 }
9992 states->nbState = 0;
9993 for (i = base; i < res->nbState; i++)
9994 xmlRelaxNGAddStates(ctxt, states,
9995 xmlRelaxNGCopyValidState
9996 (ctxt,
9997 res->tabState[i]));
9998 ctxt->states = states;
9999 }
10000 }
10001 } while (progress == 1);
10002 if (states != NULL) {
10003 xmlRelaxNGFreeStates(ctxt, states);
10004 }
10005 ctxt->states = res;
10006 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010007#if 0
10008 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010009 * errors may have to be propagated back...
10010 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010011 if (ctxt->errNr > errNr)
10012 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010013#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010014 ret = 0;
10015 break;
10016 }
10017 case XML_RELAXNG_CHOICE:{
10018 xmlRelaxNGDefinePtr list = NULL;
10019 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010020
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010021 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010022
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010023 errNr = ctxt->errNr;
10024 if ((define->dflags & IS_TRIABLE)
10025 && (define->data != NULL)) {
10026 xmlHashTablePtr triage =
10027 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010028
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010029 /*
10030 * Something we can optimize cleanly there is only one
10031 * possble branch out !
10032 */
10033 if (node == NULL) {
10034 ret = -1;
10035 break;
10036 }
10037 if ((node->type == XML_TEXT_NODE) ||
10038 (node->type == XML_CDATA_SECTION_NODE)) {
10039 list =
10040 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10041 } else if (node->type == XML_ELEMENT_NODE) {
10042 if (node->ns != NULL) {
10043 list = xmlHashLookup2(triage, node->name,
10044 node->ns->href);
10045 if (list == NULL)
10046 list =
10047 xmlHashLookup2(triage, BAD_CAST "#any",
10048 node->ns->href);
10049 } else
10050 list =
10051 xmlHashLookup2(triage, node->name, NULL);
10052 if (list == NULL)
10053 list =
10054 xmlHashLookup2(triage, BAD_CAST "#any",
10055 NULL);
10056 }
10057 if (list == NULL) {
10058 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010059 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010060 break;
10061 }
10062 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10063 if (ret == 0) {
10064 }
10065 break;
10066 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010067
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010068 list = define->content;
10069 oldflags = ctxt->flags;
10070 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010071
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010072 while (list != NULL) {
10073 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10074 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10075 if (ret == 0) {
10076 if (states == NULL) {
10077 states = xmlRelaxNGNewStates(ctxt, 1);
10078 }
10079 if (ctxt->state != NULL) {
10080 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10081 } else if (ctxt->states != NULL) {
10082 for (i = 0; i < ctxt->states->nbState; i++) {
10083 xmlRelaxNGAddStates(ctxt, states,
10084 ctxt->states->
10085 tabState[i]);
10086 }
10087 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10088 ctxt->states = NULL;
10089 }
10090 } else {
10091 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10092 }
10093 ctxt->state = oldstate;
10094 list = list->next;
10095 }
10096 if (states != NULL) {
10097 xmlRelaxNGFreeValidState(ctxt, oldstate);
10098 ctxt->states = states;
10099 ctxt->state = NULL;
10100 ret = 0;
10101 } else {
10102 ctxt->states = NULL;
10103 }
10104 ctxt->flags = oldflags;
10105 if (ret != 0) {
10106 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10107 xmlRelaxNGDumpValidError(ctxt);
10108 }
10109 } else {
10110 if (ctxt->errNr > errNr)
10111 xmlRelaxNGPopErrors(ctxt, errNr);
10112 }
10113 break;
10114 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010115 case XML_RELAXNG_DEF:
10116 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010117 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10118 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010119 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010120 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10121 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010122 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010123 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10124 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010125 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010126 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010127 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010128 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010129 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010130 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10131 break;
10132 case XML_RELAXNG_DATATYPE:{
10133 xmlNodePtr child;
10134 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010135
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010136 child = node;
10137 while (child != NULL) {
10138 if (child->type == XML_ELEMENT_NODE) {
10139 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10140 node->parent->name);
10141 ret = -1;
10142 break;
10143 } else if ((child->type == XML_TEXT_NODE) ||
10144 (child->type == XML_CDATA_SECTION_NODE)) {
10145 content = xmlStrcat(content, child->content);
10146 }
10147 /* TODO: handle entities ... */
10148 child = child->next;
10149 }
10150 if (ret == -1) {
10151 if (content != NULL)
10152 xmlFree(content);
10153 break;
10154 }
10155 if (content == NULL) {
10156 content = xmlStrdup(BAD_CAST "");
10157 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010158 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010159 ret = -1;
10160 break;
10161 }
10162 }
10163 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10164 ctxt->state->seq);
10165 if (ret == -1) {
10166 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10167 } else if (ret == 0) {
10168 ctxt->state->seq = NULL;
10169 }
10170 if (content != NULL)
10171 xmlFree(content);
10172 break;
10173 }
10174 case XML_RELAXNG_VALUE:{
10175 xmlChar *content = NULL;
10176 xmlChar *oldvalue;
10177 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010178
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010179 child = node;
10180 while (child != NULL) {
10181 if (child->type == XML_ELEMENT_NODE) {
10182 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10183 node->parent->name);
10184 ret = -1;
10185 break;
10186 } else if ((child->type == XML_TEXT_NODE) ||
10187 (child->type == XML_CDATA_SECTION_NODE)) {
10188 content = xmlStrcat(content, child->content);
10189 }
10190 /* TODO: handle entities ... */
10191 child = child->next;
10192 }
10193 if (ret == -1) {
10194 if (content != NULL)
10195 xmlFree(content);
10196 break;
10197 }
10198 if (content == NULL) {
10199 content = xmlStrdup(BAD_CAST "");
10200 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010201 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010202 ret = -1;
10203 break;
10204 }
10205 }
10206 oldvalue = ctxt->state->value;
10207 ctxt->state->value = content;
10208 ret = xmlRelaxNGValidateValue(ctxt, define);
10209 ctxt->state->value = oldvalue;
10210 if (ret == -1) {
10211 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10212 } else if (ret == 0) {
10213 ctxt->state->seq = NULL;
10214 }
10215 if (content != NULL)
10216 xmlFree(content);
10217 break;
10218 }
10219 case XML_RELAXNG_LIST:{
10220 xmlChar *content;
10221 xmlNodePtr child;
10222 xmlChar *oldvalue, *oldendvalue;
10223 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010224
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010225 /*
10226 * Make sure it's only text nodes
10227 */
10228
10229 content = NULL;
10230 child = node;
10231 while (child != NULL) {
10232 if (child->type == XML_ELEMENT_NODE) {
10233 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10234 node->parent->name);
10235 ret = -1;
10236 break;
10237 } else if ((child->type == XML_TEXT_NODE) ||
10238 (child->type == XML_CDATA_SECTION_NODE)) {
10239 content = xmlStrcat(content, child->content);
10240 }
10241 /* TODO: handle entities ... */
10242 child = child->next;
10243 }
10244 if (ret == -1) {
10245 if (content != NULL)
10246 xmlFree(content);
10247 break;
10248 }
10249 if (content == NULL) {
10250 content = xmlStrdup(BAD_CAST "");
10251 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010252 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010253 ret = -1;
10254 break;
10255 }
10256 }
10257 len = xmlStrlen(content);
10258 oldvalue = ctxt->state->value;
10259 oldendvalue = ctxt->state->endvalue;
10260 ctxt->state->value = content;
10261 ctxt->state->endvalue = content + len;
10262 ret = xmlRelaxNGValidateValue(ctxt, define);
10263 ctxt->state->value = oldvalue;
10264 ctxt->state->endvalue = oldendvalue;
10265 if (ret == -1) {
10266 VALID_ERR(XML_RELAXNG_ERR_LIST);
10267 } else if ((ret == 0) && (node != NULL)) {
10268 ctxt->state->seq = node->next;
10269 }
10270 if (content != NULL)
10271 xmlFree(content);
10272 break;
10273 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010274 case XML_RELAXNG_EXCEPT:
10275 case XML_RELAXNG_PARAM:
10276 TODO ret = -1;
10277 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010278 }
10279 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010280#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010281 for (i = 0; i < ctxt->depth; i++)
10282 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010283 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010284 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010285 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010286 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010287 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010288 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010289 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010290 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010291#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010292 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010293}
10294
10295/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010296 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010297 * @ctxt: a Relax-NG validation context
10298 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010299 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010300 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010301 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010302 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010303 */
10304static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010305xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10306 xmlRelaxNGDefinePtr define)
10307{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010308 xmlRelaxNGStatesPtr states, res;
10309 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010310
Daniel Veillardfd573f12003-03-16 17:52:32 +000010311 /*
10312 * We should NOT have both ctxt->state and ctxt->states
10313 */
10314 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010315 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10316 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010317 }
10318
10319 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010320 if (ctxt->states != NULL) {
10321 ctxt->state = ctxt->states->tabState[0];
10322 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10323 ctxt->states = NULL;
10324 }
10325 ret = xmlRelaxNGValidateState(ctxt, define);
10326 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10327 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10328 ctxt->state = NULL;
10329 }
10330 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10331 ctxt->state = ctxt->states->tabState[0];
10332 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10333 ctxt->states = NULL;
10334 }
10335 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010336 }
10337
10338 states = ctxt->states;
10339 ctxt->states = NULL;
10340 res = NULL;
10341 j = 0;
10342 oldflags = ctxt->flags;
10343 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010344 for (i = 0; i < states->nbState; i++) {
10345 ctxt->state = states->tabState[i];
10346 ctxt->states = NULL;
10347 ret = xmlRelaxNGValidateState(ctxt, define);
10348 /*
10349 * We should NOT have both ctxt->state and ctxt->states
10350 */
10351 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10352 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10353 ctxt->state = NULL;
10354 }
10355 if (ret == 0) {
10356 if (ctxt->states == NULL) {
10357 if (res != NULL) {
10358 /* add the state to the container */
10359 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10360 ctxt->state = NULL;
10361 } else {
10362 /* add the state directly in states */
10363 states->tabState[j++] = ctxt->state;
10364 ctxt->state = NULL;
10365 }
10366 } else {
10367 if (res == NULL) {
10368 /* make it the new container and copy other results */
10369 res = ctxt->states;
10370 ctxt->states = NULL;
10371 for (k = 0; k < j; k++)
10372 xmlRelaxNGAddStates(ctxt, res,
10373 states->tabState[k]);
10374 } else {
10375 /* add all the new results to res and reff the container */
10376 for (k = 0; k < ctxt->states->nbState; k++)
10377 xmlRelaxNGAddStates(ctxt, res,
10378 ctxt->states->tabState[k]);
10379 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10380 ctxt->states = NULL;
10381 }
10382 }
10383 } else {
10384 if (ctxt->state != NULL) {
10385 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10386 ctxt->state = NULL;
10387 } else if (ctxt->states != NULL) {
10388 for (k = 0; k < ctxt->states->nbState; k++)
10389 xmlRelaxNGFreeValidState(ctxt,
10390 ctxt->states->tabState[k]);
10391 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10392 ctxt->states = NULL;
10393 }
10394 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010395 }
10396 ctxt->flags = oldflags;
10397 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010398 xmlRelaxNGFreeStates(ctxt, states);
10399 ctxt->states = res;
10400 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010401 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010402 states->nbState = j;
10403 ctxt->states = states;
10404 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010405 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010406 ctxt->state = states->tabState[0];
10407 xmlRelaxNGFreeStates(ctxt, states);
10408 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010409 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010410 ret = -1;
10411 xmlRelaxNGFreeStates(ctxt, states);
10412 if (ctxt->states != NULL) {
10413 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10414 ctxt->states = NULL;
10415 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010416 }
10417 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010418 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10419 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010420 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010421 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010422}
10423
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010424/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010425 * xmlRelaxNGValidateDocument:
10426 * @ctxt: a Relax-NG validation context
10427 * @doc: the document
10428 *
10429 * Validate the given document
10430 *
10431 * Returns 0 if the validation succeeded or an error code.
10432 */
10433static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010434xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10435{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010436 int ret;
10437 xmlRelaxNGPtr schema;
10438 xmlRelaxNGGrammarPtr grammar;
10439 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010440 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010441
10442 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010443 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010444
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010445 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010446 schema = ctxt->schema;
10447 grammar = schema->topgrammar;
10448 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010449 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10450 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010451 }
10452 state = xmlRelaxNGNewValidState(ctxt, NULL);
10453 ctxt->state = state;
10454 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010455 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010456 state = ctxt->state;
10457 node = state->seq;
10458 node = xmlRelaxNGSkipIgnored(ctxt, node);
10459 if (node != NULL) {
10460 if (ret != -1) {
10461 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10462 ret = -1;
10463 }
10464 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010465 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010466 int i;
10467 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010468
Daniel Veillard4c004142003-10-07 11:33:24 +000010469 for (i = 0; i < ctxt->states->nbState; i++) {
10470 state = ctxt->states->tabState[i];
10471 node = state->seq;
10472 node = xmlRelaxNGSkipIgnored(ctxt, node);
10473 if (node == NULL)
10474 tmp = 0;
10475 xmlRelaxNGFreeValidState(ctxt, state);
10476 }
10477 if (tmp == -1) {
10478 if (ret != -1) {
10479 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10480 ret = -1;
10481 }
10482 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010483 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010484 if (ctxt->state != NULL) {
10485 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010486 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010487 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010488 if (ret != 0)
10489 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010490#ifdef DEBUG
10491 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010492 ctxt->error(ctxt->userData,
10493 "%d Extra error messages left on stack !\n",
10494 ctxt->errNr);
10495 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010496 }
10497#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010498#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010499 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010500 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010501
Daniel Veillard4c004142003-10-07 11:33:24 +000010502 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10503 vctxt.valid = 1;
10504 vctxt.error = ctxt->error;
10505 vctxt.warning = ctxt->warning;
10506 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010507
Daniel Veillard4c004142003-10-07 11:33:24 +000010508 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10509 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010510 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010511#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010512 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010513 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010514
Daniel Veillard4c004142003-10-07 11:33:24 +000010515 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010516}
10517
Daniel Veillardfd573f12003-03-16 17:52:32 +000010518/************************************************************************
10519 * *
10520 * Validation interfaces *
10521 * *
10522 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010523
Daniel Veillard6eadf632003-01-23 18:29:16 +000010524/**
10525 * xmlRelaxNGNewValidCtxt:
10526 * @schema: a precompiled XML RelaxNGs
10527 *
10528 * Create an XML RelaxNGs validation context based on the given schema
10529 *
10530 * Returns the validation context or NULL in case of error
10531 */
10532xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010533xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10534{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010535 xmlRelaxNGValidCtxtPtr ret;
10536
10537 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10538 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010539 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010540 return (NULL);
10541 }
10542 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10543 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010544 ret->error = xmlGenericError;
10545 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010546 ret->errNr = 0;
10547 ret->errMax = 0;
10548 ret->err = NULL;
10549 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010550 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010551 ret->states = NULL;
10552 ret->freeState = NULL;
10553 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010554 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010555 return (ret);
10556}
10557
10558/**
10559 * xmlRelaxNGFreeValidCtxt:
10560 * @ctxt: the schema validation context
10561 *
10562 * Free the resources associated to the schema validation context
10563 */
10564void
Daniel Veillard4c004142003-10-07 11:33:24 +000010565xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10566{
Daniel Veillard798024a2003-03-19 10:36:09 +000010567 int k;
10568
Daniel Veillard6eadf632003-01-23 18:29:16 +000010569 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010570 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010571 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010572 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010573 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010574 for (k = 0; k < ctxt->freeState->nbState; k++) {
10575 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10576 }
10577 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010578 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010579 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010580 for (k = 0; k < ctxt->freeStatesNr; k++) {
10581 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10582 }
10583 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010584 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010585 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010586 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010587 if (ctxt->elemTab != NULL) {
10588 xmlRegExecCtxtPtr exec;
10589
Daniel Veillard4c004142003-10-07 11:33:24 +000010590 exec = xmlRelaxNGElemPop(ctxt);
10591 while (exec != NULL) {
10592 xmlRegFreeExecCtxt(exec);
10593 exec = xmlRelaxNGElemPop(ctxt);
10594 }
10595 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010596 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010597 xmlFree(ctxt);
10598}
10599
10600/**
10601 * xmlRelaxNGSetValidErrors:
10602 * @ctxt: a Relax-NG validation context
10603 * @err: the error function
10604 * @warn: the warning function
10605 * @ctx: the functions context
10606 *
10607 * Set the error and warning callback informations
10608 */
10609void
10610xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010611 xmlRelaxNGValidityErrorFunc err,
10612 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10613{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010614 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010615 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010616 ctxt->error = err;
10617 ctxt->warning = warn;
10618 ctxt->userData = ctx;
10619}
10620
10621/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010622 * xmlRelaxNGGetValidErrors:
10623 * @ctxt: a Relax-NG validation context
10624 * @err: the error function result
10625 * @warn: the warning function result
10626 * @ctx: the functions context result
10627 *
10628 * Get the error and warning callback informations
10629 *
10630 * Returns -1 in case of error and 0 otherwise
10631 */
10632int
10633xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010634 xmlRelaxNGValidityErrorFunc * err,
10635 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10636{
Daniel Veillard409a8142003-07-18 15:16:57 +000010637 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010638 return (-1);
10639 if (err != NULL)
10640 *err = ctxt->error;
10641 if (warn != NULL)
10642 *warn = ctxt->warning;
10643 if (ctx != NULL)
10644 *ctx = ctxt->userData;
10645 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010646}
10647
10648/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010649 * xmlRelaxNGValidateDoc:
10650 * @ctxt: a Relax-NG validation context
10651 * @doc: a parsed document tree
10652 *
10653 * Validate a document tree in memory.
10654 *
10655 * Returns 0 if the document is valid, a positive error code
10656 * number otherwise and -1 in case of internal or API error.
10657 */
10658int
Daniel Veillard4c004142003-10-07 11:33:24 +000010659xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10660{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010661 int ret;
10662
10663 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010664 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010665
10666 ctxt->doc = doc;
10667
10668 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010669 /*
10670 * TODO: build error codes
10671 */
10672 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010673 return (1);
10674 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010675}
10676
10677#endif /* LIBXML_SCHEMAS_ENABLED */