blob: 29cdb19102e6f62bb68260ad3fbf39282b5f5e0e [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);
William M. Brack8eabb052004-06-07 14:15:54 +00007185 /*
7186 * Since we are about to delete cur, if it's nsDef is non-NULL we
7187 * need to preserve it (it contains the ns definitions for the
7188 * children we just moved). We'll just stick it on to the end
7189 * of cur->parent's list, since it's never going to be re-serialized
7190 * (bug 143738).
7191 */
7192 if (cur->nsDef != NULL) {
7193 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7194 while (parDef->next != NULL)
7195 parDef = parDef->next;
7196 parDef->next = cur->nsDef;
7197 cur->nsDef = NULL;
7198 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007199 delete = cur;
7200 goto skip_children;
7201 }
7202 }
7203 }
7204 /*
7205 * Simplification 4.2 whitespaces
7206 */
7207 else if ((cur->type == XML_TEXT_NODE) ||
7208 (cur->type == XML_CDATA_SECTION_NODE)) {
7209 if (IS_BLANK_NODE(cur)) {
7210 if (cur->parent->type == XML_ELEMENT_NODE) {
7211 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7212 &&
7213 (!xmlStrEqual
7214 (cur->parent->name, BAD_CAST "param")))
7215 delete = cur;
7216 } else {
7217 delete = cur;
7218 goto skip_children;
7219 }
7220 }
7221 } else {
7222 delete = cur;
7223 goto skip_children;
7224 }
7225
7226 /*
7227 * Skip to next node
7228 */
7229 if (cur->children != NULL) {
7230 if ((cur->children->type != XML_ENTITY_DECL) &&
7231 (cur->children->type != XML_ENTITY_REF_NODE) &&
7232 (cur->children->type != XML_ENTITY_NODE)) {
7233 cur = cur->children;
7234 continue;
7235 }
7236 }
7237 skip_children:
7238 if (cur->next != NULL) {
7239 cur = cur->next;
7240 continue;
7241 }
7242
7243 do {
7244 cur = cur->parent;
7245 if (cur == NULL)
7246 break;
7247 if (cur == root) {
7248 cur = NULL;
7249 break;
7250 }
7251 if (cur->next != NULL) {
7252 cur = cur->next;
7253 break;
7254 }
7255 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007256 }
7257 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007258 xmlUnlinkNode(delete);
7259 xmlFreeNode(delete);
7260 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007261 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007262}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007263
Daniel Veillardc5312d72003-02-21 17:14:10 +00007264/**
7265 * xmlRelaxNGCleanupDoc:
7266 * @ctxt: a Relax-NG parser context
7267 * @doc: an xmldocPtr document pointer
7268 *
7269 * Cleanup the document from unwanted nodes for parsing, resolve
7270 * Include and externalRef lookups.
7271 *
7272 * Returns the cleaned up document or NULL in case of error
7273 */
7274static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007275xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7276{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007277 xmlNodePtr root;
7278
7279 /*
7280 * Extract the root
7281 */
7282 root = xmlDocGetRootElement(doc);
7283 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007284 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7285 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007286 return (NULL);
7287 }
7288 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007289 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007290}
7291
7292/**
7293 * xmlRelaxNGParse:
7294 * @ctxt: a Relax-NG parser context
7295 *
7296 * parse a schema definition resource and build an internal
7297 * XML Shema struture which can be used to validate instances.
7298 * *WARNING* this interface is highly subject to change
7299 *
7300 * Returns the internal XML RelaxNG structure built from the resource or
7301 * NULL in case of error
7302 */
7303xmlRelaxNGPtr
7304xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7305{
7306 xmlRelaxNGPtr ret = NULL;
7307 xmlDocPtr doc;
7308 xmlNodePtr root;
7309
7310 xmlRelaxNGInitTypes();
7311
7312 if (ctxt == NULL)
7313 return (NULL);
7314
7315 /*
7316 * First step is to parse the input document into an DOM/Infoset
7317 */
7318 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007319 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007320 if (doc == NULL) {
7321 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7322 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7323 NULL);
7324 return (NULL);
7325 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007326 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007327 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007328 if (doc == NULL) {
7329 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7330 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7331 NULL);
7332 return (NULL);
7333 }
7334 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7335 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007336 } else if (ctxt->document != NULL) {
7337 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007338 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007339 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7340 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7341 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007342 }
7343 ctxt->document = doc;
7344
7345 /*
7346 * Some preprocessing of the document content
7347 */
7348 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7349 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007350 xmlFreeDoc(ctxt->document);
7351 ctxt->document = NULL;
7352 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007353 }
7354
Daniel Veillard6eadf632003-01-23 18:29:16 +00007355 /*
7356 * Then do the parsing for good
7357 */
7358 root = xmlDocGetRootElement(doc);
7359 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007360 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7361 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7362 ctxt->URL, NULL);
7363 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007364 return (NULL);
7365 }
7366 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007367 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007368 xmlFreeDoc(doc);
7369 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007370 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007371
7372 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007373 * Check the ref/defines links
7374 */
7375 /*
7376 * try to preprocess interleaves
7377 */
7378 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007379 xmlHashScan(ctxt->interleaves,
7380 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007381 }
7382
7383 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007384 * if there was a parsing error return NULL
7385 */
7386 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007387 xmlRelaxNGFree(ret);
7388 ctxt->document = NULL;
7389 xmlFreeDoc(doc);
7390 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007391 }
7392
7393 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007394 * try to compile (parts of) the schemas
7395 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007396 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7397 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007398 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007399
Daniel Veillard4c004142003-10-07 11:33:24 +00007400 def = xmlRelaxNGNewDefine(ctxt, NULL);
7401 if (def != NULL) {
7402 def->type = XML_RELAXNG_START;
7403 def->content = ret->topgrammar->start;
7404 ret->topgrammar->start = def;
7405 }
7406 }
7407 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007408 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007409
7410 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007411 * Transfer the pointer for cleanup at the schema level.
7412 */
7413 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007414 ctxt->document = NULL;
7415 ret->documents = ctxt->documents;
7416 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007417
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007418 ret->includes = ctxt->includes;
7419 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007420 ret->defNr = ctxt->defNr;
7421 ret->defTab = ctxt->defTab;
7422 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007423 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007424 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007425
7426 return (ret);
7427}
Daniel Veillard4c004142003-10-07 11:33:24 +00007428
Daniel Veillard6eadf632003-01-23 18:29:16 +00007429/**
7430 * xmlRelaxNGSetParserErrors:
7431 * @ctxt: a Relax-NG validation context
7432 * @err: the error callback
7433 * @warn: the warning callback
7434 * @ctx: contextual data for the callbacks
7435 *
7436 * Set the callback functions used to handle errors for a validation context
7437 */
7438void
7439xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007440 xmlRelaxNGValidityErrorFunc err,
7441 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7442{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007443 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007444 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007445 ctxt->error = err;
7446 ctxt->warning = warn;
7447 ctxt->userData = ctx;
7448}
Daniel Veillard409a8142003-07-18 15:16:57 +00007449
7450/**
7451 * xmlRelaxNGGetParserErrors:
7452 * @ctxt: a Relax-NG validation context
7453 * @err: the error callback result
7454 * @warn: the warning callback result
7455 * @ctx: contextual data for the callbacks result
7456 *
7457 * Get the callback information used to handle errors for a validation context
7458 *
7459 * Returns -1 in case of failure, 0 otherwise.
7460 */
7461int
7462xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007463 xmlRelaxNGValidityErrorFunc * err,
7464 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7465{
Daniel Veillard409a8142003-07-18 15:16:57 +00007466 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007467 return (-1);
7468 if (err != NULL)
7469 *err = ctxt->error;
7470 if (warn != NULL)
7471 *warn = ctxt->warning;
7472 if (ctx != NULL)
7473 *ctx = ctxt->userData;
7474 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007475}
7476
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007477#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007478
Daniel Veillard6eadf632003-01-23 18:29:16 +00007479/************************************************************************
7480 * *
7481 * Dump back a compiled form *
7482 * *
7483 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007484static void xmlRelaxNGDumpDefine(FILE * output,
7485 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007486
7487/**
7488 * xmlRelaxNGDumpDefines:
7489 * @output: the file output
7490 * @defines: a list of define structures
7491 *
7492 * Dump a RelaxNG structure back
7493 */
7494static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007495xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7496{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007497 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007498 xmlRelaxNGDumpDefine(output, defines);
7499 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007500 }
7501}
7502
7503/**
7504 * xmlRelaxNGDumpDefine:
7505 * @output: the file output
7506 * @define: a define structure
7507 *
7508 * Dump a RelaxNG structure back
7509 */
7510static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007511xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7512{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007513 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007514 return;
7515 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007516 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007517 fprintf(output, "<empty/>\n");
7518 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007519 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007520 fprintf(output, "<notAllowed/>\n");
7521 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007522 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007523 fprintf(output, "<text/>\n");
7524 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007525 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007526 fprintf(output, "<element>\n");
7527 if (define->name != NULL) {
7528 fprintf(output, "<name");
7529 if (define->ns != NULL)
7530 fprintf(output, " ns=\"%s\"", define->ns);
7531 fprintf(output, ">%s</name>\n", define->name);
7532 }
7533 xmlRelaxNGDumpDefines(output, define->attrs);
7534 xmlRelaxNGDumpDefines(output, define->content);
7535 fprintf(output, "</element>\n");
7536 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007537 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007538 fprintf(output, "<list>\n");
7539 xmlRelaxNGDumpDefines(output, define->content);
7540 fprintf(output, "</list>\n");
7541 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007542 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007543 fprintf(output, "<oneOrMore>\n");
7544 xmlRelaxNGDumpDefines(output, define->content);
7545 fprintf(output, "</oneOrMore>\n");
7546 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007547 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007548 fprintf(output, "<zeroOrMore>\n");
7549 xmlRelaxNGDumpDefines(output, define->content);
7550 fprintf(output, "</zeroOrMore>\n");
7551 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007552 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007553 fprintf(output, "<choice>\n");
7554 xmlRelaxNGDumpDefines(output, define->content);
7555 fprintf(output, "</choice>\n");
7556 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007557 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007558 fprintf(output, "<group>\n");
7559 xmlRelaxNGDumpDefines(output, define->content);
7560 fprintf(output, "</group>\n");
7561 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007562 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007563 fprintf(output, "<interleave>\n");
7564 xmlRelaxNGDumpDefines(output, define->content);
7565 fprintf(output, "</interleave>\n");
7566 break;
7567 case XML_RELAXNG_OPTIONAL:
7568 fprintf(output, "<optional>\n");
7569 xmlRelaxNGDumpDefines(output, define->content);
7570 fprintf(output, "</optional>\n");
7571 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007572 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007573 fprintf(output, "<attribute>\n");
7574 xmlRelaxNGDumpDefines(output, define->content);
7575 fprintf(output, "</attribute>\n");
7576 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007577 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007578 fprintf(output, "<define");
7579 if (define->name != NULL)
7580 fprintf(output, " name=\"%s\"", define->name);
7581 fprintf(output, ">\n");
7582 xmlRelaxNGDumpDefines(output, define->content);
7583 fprintf(output, "</define>\n");
7584 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007585 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007586 fprintf(output, "<ref");
7587 if (define->name != NULL)
7588 fprintf(output, " name=\"%s\"", define->name);
7589 fprintf(output, ">\n");
7590 xmlRelaxNGDumpDefines(output, define->content);
7591 fprintf(output, "</ref>\n");
7592 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007593 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 fprintf(output, "<parentRef");
7595 if (define->name != NULL)
7596 fprintf(output, " name=\"%s\"", define->name);
7597 fprintf(output, ">\n");
7598 xmlRelaxNGDumpDefines(output, define->content);
7599 fprintf(output, "</parentRef>\n");
7600 break;
7601 case XML_RELAXNG_EXTERNALREF:
7602 fprintf(output, "<externalRef>");
7603 xmlRelaxNGDumpDefines(output, define->content);
7604 fprintf(output, "</externalRef>\n");
7605 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007606 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007607 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007608 TODO break;
7609 case XML_RELAXNG_START:
7610 case XML_RELAXNG_EXCEPT:
7611 case XML_RELAXNG_PARAM:
7612 TODO break;
7613 case XML_RELAXNG_NOOP:
7614 xmlRelaxNGDumpDefines(output, define->content);
7615 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007616 }
7617}
Daniel Veillard4c004142003-10-07 11:33:24 +00007618
Daniel Veillard6eadf632003-01-23 18:29:16 +00007619/**
7620 * xmlRelaxNGDumpGrammar:
7621 * @output: the file output
7622 * @grammar: a grammar structure
7623 * @top: is this a top grammar
7624 *
7625 * Dump a RelaxNG structure back
7626 */
7627static void
7628xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7629{
7630 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007631 return;
7632
Daniel Veillard6eadf632003-01-23 18:29:16 +00007633 fprintf(output, "<grammar");
7634 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007635 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7636 switch (grammar->combine) {
7637 case XML_RELAXNG_COMBINE_UNDEFINED:
7638 break;
7639 case XML_RELAXNG_COMBINE_CHOICE:
7640 fprintf(output, " combine=\"choice\"");
7641 break;
7642 case XML_RELAXNG_COMBINE_INTERLEAVE:
7643 fprintf(output, " combine=\"interleave\"");
7644 break;
7645 default:
7646 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007647 }
7648 fprintf(output, ">\n");
7649 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007650 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007651 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007652 fprintf(output, "<start>\n");
7653 xmlRelaxNGDumpDefine(output, grammar->start);
7654 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007655 }
7656 /* TODO ? Dump the defines ? */
7657 fprintf(output, "</grammar>\n");
7658}
7659
7660/**
7661 * xmlRelaxNGDump:
7662 * @output: the file output
7663 * @schema: a schema structure
7664 *
7665 * Dump a RelaxNG structure back
7666 */
7667void
7668xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7669{
7670 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007671 fprintf(output, "RelaxNG empty or failed to compile\n");
7672 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007673 }
7674 fprintf(output, "RelaxNG: ");
7675 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007676 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007677 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007678 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007679 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007680 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007681 }
7682 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007683 fprintf(output, "RelaxNG has no top grammar\n");
7684 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007685 }
7686 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7687}
7688
Daniel Veillardfebcca42003-02-16 15:44:18 +00007689/**
7690 * xmlRelaxNGDumpTree:
7691 * @output: the file output
7692 * @schema: a schema structure
7693 *
7694 * Dump the transformed RelaxNG tree.
7695 */
7696void
7697xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7698{
7699 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007700 fprintf(output, "RelaxNG empty or failed to compile\n");
7701 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007702 }
7703 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007704 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007705 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007706 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007707 }
7708}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007709#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007710
Daniel Veillard6eadf632003-01-23 18:29:16 +00007711/************************************************************************
7712 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007713 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007714 * *
7715 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007716static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7717 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007718
7719/**
7720 * xmlRelaxNGValidateCompiledCallback:
7721 * @exec: the regular expression instance
7722 * @token: the token which matched
7723 * @transdata: callback data, the define for the subelement if available
7724 @ @inputdata: callback data, the Relax NG validation context
7725 *
7726 * Handle the callback and if needed validate the element children.
7727 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007728static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007729xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007730 const xmlChar * token,
7731 void *transdata, void *inputdata)
7732{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007733 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7734 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7735 int ret;
7736
7737#ifdef DEBUG_COMPILE
7738 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007739 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007740#endif
7741 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007742 fprintf(stderr, "callback on %s missing context\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 }
7747 if (define == NULL) {
7748 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007749 return;
7750 fprintf(stderr, "callback on %s missing define\n", token);
7751 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7752 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7753 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007754 }
7755 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007756 fprintf(stderr, "callback on %s missing info\n", token);
7757 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7758 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7759 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007760 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007761 fprintf(stderr, "callback on %s define is not element\n", token);
7762 if (ctxt->errNo == XML_RELAXNG_OK)
7763 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7764 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007765 }
7766 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007767 if (ret != 0)
7768 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007769}
7770
7771/**
7772 * xmlRelaxNGValidateCompiledContent:
7773 * @ctxt: the RelaxNG validation context
7774 * @regexp: the regular expression as compiled
7775 * @content: list of children to test against the regexp
7776 *
7777 * Validate the content model of an element or start using the regexp
7778 *
7779 * Returns 0 in case of success, -1 in case of error.
7780 */
7781static int
7782xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007783 xmlRegexpPtr regexp, xmlNodePtr content)
7784{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007785 xmlRegExecCtxtPtr exec;
7786 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007787 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007788 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007789
7790 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007791 return (-1);
7792 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007793 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007794 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007795 cur = content;
7796 while (cur != NULL) {
7797 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007798 switch (cur->type) {
7799 case XML_TEXT_NODE:
7800 case XML_CDATA_SECTION_NODE:
7801 if (xmlIsBlankNode(cur))
7802 break;
7803 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7804 if (ret < 0) {
7805 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7806 cur->parent->name);
7807 }
7808 break;
7809 case XML_ELEMENT_NODE:
7810 if (cur->ns != NULL) {
7811 ret = xmlRegExecPushString2(exec, cur->name,
7812 cur->ns->href, ctxt);
7813 } else {
7814 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7815 }
7816 if (ret < 0) {
7817 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7818 }
7819 break;
7820 default:
7821 break;
7822 }
7823 if (ret < 0)
7824 break;
7825 /*
7826 * Switch to next element
7827 */
7828 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007829 }
7830 ret = xmlRegExecPushString(exec, NULL, NULL);
7831 if (ret == 1) {
7832 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007833 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007834 } else if (ret == 0) {
7835 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007836 * TODO: get some of the names needed to exit the current state of exec
7837 */
7838 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7839 ret = -1;
7840 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7841 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007842 } else {
7843 ret = -1;
7844 }
7845 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007846 /*
7847 * There might be content model errors outside of the pure
7848 * regexp validation, e.g. for attribute values.
7849 */
7850 if ((ret == 0) && (ctxt->perr != 0)) {
7851 ret = ctxt->perr;
7852 }
7853 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007854 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007855}
7856
7857/************************************************************************
7858 * *
7859 * Progressive validation of when possible *
7860 * *
7861 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007862static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7863 xmlRelaxNGDefinePtr defines);
7864static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00007865 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007866static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007867
7868/**
7869 * xmlRelaxNGElemPush:
7870 * @ctxt: the validation context
7871 * @exec: the regexp runtime for the new content model
7872 *
7873 * Push a new regexp for the current node content model on the stack
7874 *
7875 * Returns 0 in case of success and -1 in case of error.
7876 */
7877static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007878xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7879{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007880 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007881 ctxt->elemMax = 10;
7882 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7883 sizeof
7884 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007885 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007886 xmlRngVErrMemory(ctxt, "validating\n");
7887 return (-1);
7888 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007889 }
7890 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007891 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007892 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007893 ctxt->elemMax *
7894 sizeof
7895 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007896 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007897 xmlRngVErrMemory(ctxt, "validating\n");
7898 return (-1);
7899 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007900 }
7901 ctxt->elemTab[ctxt->elemNr++] = exec;
7902 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007903 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007904}
7905
7906/**
7907 * xmlRelaxNGElemPop:
7908 * @ctxt: the validation context
7909 *
7910 * Pop the regexp of the current node content model from the stack
7911 *
7912 * Returns the exec or NULL if empty
7913 */
7914static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007915xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7916{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007917 xmlRegExecCtxtPtr ret;
7918
Daniel Veillard4c004142003-10-07 11:33:24 +00007919 if (ctxt->elemNr <= 0)
7920 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007921 ctxt->elemNr--;
7922 ret = ctxt->elemTab[ctxt->elemNr];
7923 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007924 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007925 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7926 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007927 ctxt->elem = NULL;
7928 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007929}
7930
7931/**
7932 * xmlRelaxNGValidateProgressiveCallback:
7933 * @exec: the regular expression instance
7934 * @token: the token which matched
7935 * @transdata: callback data, the define for the subelement if available
7936 @ @inputdata: callback data, the Relax NG validation context
7937 *
7938 * Handle the callback and if needed validate the element children.
7939 * some of the in/out informations are passed via the context in @inputdata.
7940 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007941static void
7942xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7943 ATTRIBUTE_UNUSED,
7944 const xmlChar * token,
7945 void *transdata, void *inputdata)
7946{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007947 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7948 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007949 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007950 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007951 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007952
7953#ifdef DEBUG_PROGRESSIVE
7954 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007955 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007956#endif
7957 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007958 fprintf(stderr, "callback on %s missing context\n", token);
7959 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007960 }
7961 ctxt->pstate = 1;
7962 if (define == NULL) {
7963 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007964 return;
7965 fprintf(stderr, "callback on %s missing define\n", token);
7966 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7967 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7968 ctxt->pstate = -1;
7969 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007970 }
7971 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007972 fprintf(stderr, "callback on %s missing info\n", token);
7973 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7974 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7975 ctxt->pstate = -1;
7976 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007977 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007978 fprintf(stderr, "callback on %s define is not element\n", token);
7979 if (ctxt->errNo == XML_RELAXNG_OK)
7980 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7981 ctxt->pstate = -1;
7982 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007983 }
7984 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007985 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7986 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7987 xmlRelaxNGDumpValidError(ctxt);
7988 ctxt->pstate = -1;
7989 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007990 }
7991 if (define->contModel == NULL) {
7992 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007993 * this node cannot be validated in a streamable fashion
7994 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00007995#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00007996 xmlGenericError(xmlGenericErrorContext,
7997 "Element '%s' validation is not streamable\n",
7998 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007999#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008000 ctxt->pstate = 0;
8001 ctxt->pdef = define;
8002 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008003 }
8004 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008005 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008006 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008007 ctxt->pstate = -1;
8008 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008009 }
8010 xmlRelaxNGElemPush(ctxt, exec);
8011
8012 /*
8013 * Validate the attributes part of the content.
8014 */
8015 state = xmlRelaxNGNewValidState(ctxt, node);
8016 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008017 ctxt->pstate = -1;
8018 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008019 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008020 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008021 ctxt->state = state;
8022 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008023 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8024 if (ret != 0) {
8025 ctxt->pstate = -1;
8026 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8027 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008028 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008029 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008030 ctxt->state->seq = NULL;
8031 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8032 if (ret != 0) {
8033 ctxt->pstate = -1;
8034 }
8035 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008036 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008037 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008038
8039 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008040
Daniel Veillard4c004142003-10-07 11:33:24 +00008041 for (i = 0; i < ctxt->states->nbState; i++) {
8042 state = ctxt->states->tabState[i];
8043 ctxt->state = state;
8044 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008045
Daniel Veillard4c004142003-10-07 11:33:24 +00008046 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8047 tmp = 0;
8048 break;
8049 }
8050 }
8051 if (tmp != 0) {
8052 /*
8053 * validation error, log the message for the "best" one
8054 */
8055 ctxt->flags |= FLAGS_IGNORABLE;
8056 xmlRelaxNGLogBestError(ctxt);
8057 }
8058 for (i = 0; i < ctxt->states->nbState; i++) {
8059 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8060 }
8061 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8062 ctxt->states = NULL;
8063 if ((ret == 0) && (tmp == -1))
8064 ctxt->pstate = -1;
8065 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008066 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008067 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008068 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8069 xmlRelaxNGDumpValidError(ctxt);
8070 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008071 }
8072 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008073}
8074
8075/**
8076 * xmlRelaxNGValidatePushElement:
8077 * @ctxt: the validation context
8078 * @doc: a document instance
8079 * @elem: an element instance
8080 *
8081 * Push a new element start on the RelaxNG validation stack.
8082 *
8083 * returns 1 if no validation problem was found or 0 if validating the
8084 * element requires a full node, and -1 in case of error.
8085 */
8086int
Daniel Veillard33300b42003-04-17 09:09:19 +00008087xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8088 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008089 xmlNodePtr elem)
8090{
8091 int ret = 1;
8092
8093 if ((ctxt == NULL) || (elem == NULL))
8094 return (-1);
8095
8096#ifdef DEBUG_PROGRESSIVE
8097 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8098#endif
8099 if (ctxt->elem == 0) {
8100 xmlRelaxNGPtr schema;
8101 xmlRelaxNGGrammarPtr grammar;
8102 xmlRegExecCtxtPtr exec;
8103 xmlRelaxNGDefinePtr define;
8104
8105 schema = ctxt->schema;
8106 if (schema == NULL) {
8107 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8108 return (-1);
8109 }
8110 grammar = schema->topgrammar;
8111 if ((grammar == NULL) || (grammar->start == NULL)) {
8112 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8113 return (-1);
8114 }
8115 define = grammar->start;
8116 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008117 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008118 return (0);
8119 }
8120 exec = xmlRegNewExecCtxt(define->contModel,
8121 xmlRelaxNGValidateProgressiveCallback,
8122 ctxt);
8123 if (exec == NULL) {
8124 return (-1);
8125 }
8126 xmlRelaxNGElemPush(ctxt, exec);
8127 }
8128 ctxt->pnode = elem;
8129 ctxt->pstate = 0;
8130 if (elem->ns != NULL) {
8131 ret =
8132 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8133 ctxt);
8134 } else {
8135 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8136 }
8137 if (ret < 0) {
8138 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8139 } else {
8140 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008141 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008142 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008143 ret = -1;
8144 else
8145 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008146 }
8147#ifdef DEBUG_PROGRESSIVE
8148 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008149 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8150 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008151#endif
8152 return (ret);
8153}
8154
8155/**
8156 * xmlRelaxNGValidatePushCData:
8157 * @ctxt: the RelaxNG validation context
8158 * @data: some character data read
8159 * @len: the lenght of the data
8160 *
8161 * check the CData parsed for validation in the current stack
8162 *
8163 * returns 1 if no validation problem was found or -1 otherwise
8164 */
8165int
8166xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008167 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008168{
8169 int ret = 1;
8170
8171 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8172 return (-1);
8173
8174#ifdef DEBUG_PROGRESSIVE
8175 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8176#endif
8177
8178 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008179 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008180 break;
8181 data++;
8182 }
8183 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008184 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008185
8186 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8187 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008188 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008189#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008190 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008191#endif
8192
Daniel Veillard4c004142003-10-07 11:33:24 +00008193 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008194 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008195 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008196}
8197
8198/**
8199 * xmlRelaxNGValidatePopElement:
8200 * @ctxt: the RelaxNG validation context
8201 * @doc: a document instance
8202 * @elem: an element instance
8203 *
8204 * Pop the element end from the RelaxNG validation stack.
8205 *
8206 * returns 1 if no validation problem was found or 0 otherwise
8207 */
8208int
8209xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8210 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008211 xmlNodePtr elem)
8212{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008213 int ret;
8214 xmlRegExecCtxtPtr exec;
8215
Daniel Veillard4c004142003-10-07 11:33:24 +00008216 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8217 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008218#ifdef DEBUG_PROGRESSIVE
8219 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8220#endif
8221 /*
8222 * verify that we reached a terminal state of the content model.
8223 */
8224 exec = xmlRelaxNGElemPop(ctxt);
8225 ret = xmlRegExecPushString(exec, NULL, NULL);
8226 if (ret == 0) {
8227 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008228 * TODO: get some of the names needed to exit the current state of exec
8229 */
8230 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8231 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008232 } else if (ret < 0) {
8233 ret = -1;
8234 } else {
8235 ret = 1;
8236 }
8237 xmlRegFreeExecCtxt(exec);
8238#ifdef DEBUG_PROGRESSIVE
8239 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008240 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8241 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008242#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008243 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008244}
8245
8246/**
8247 * xmlRelaxNGValidateFullElement:
8248 * @ctxt: the validation context
8249 * @doc: a document instance
8250 * @elem: an element instance
8251 *
8252 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8253 * 0 and the content of the node has been expanded.
8254 *
8255 * returns 1 if no validation problem was found or -1 in case of error.
8256 */
8257int
Daniel Veillard33300b42003-04-17 09:09:19 +00008258xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8259 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008260 xmlNodePtr elem)
8261{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008262 int ret;
8263 xmlRelaxNGValidStatePtr state;
8264
Daniel Veillard4c004142003-10-07 11:33:24 +00008265 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8266 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008267#ifdef DEBUG_PROGRESSIVE
8268 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8269#endif
8270 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8271 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008272 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008273 }
8274 state->seq = elem;
8275 ctxt->state = state;
8276 ctxt->errNo = XML_RELAXNG_OK;
8277 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008278 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8279 ret = -1;
8280 else
8281 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008282 xmlRelaxNGFreeValidState(ctxt, state);
8283 ctxt->state = NULL;
8284#ifdef DEBUG_PROGRESSIVE
8285 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008286 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8287 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008288#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008289 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008290}
8291
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008292/************************************************************************
8293 * *
8294 * Generic interpreted validation implementation *
8295 * *
8296 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008297static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8298 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008299
8300/**
8301 * xmlRelaxNGSkipIgnored:
8302 * @ctxt: a schema validation context
8303 * @node: the top node.
8304 *
8305 * Skip ignorable nodes in that context
8306 *
8307 * Returns the new sibling or NULL in case of error.
8308 */
8309static xmlNodePtr
8310xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008311 xmlNodePtr node)
8312{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008313 /*
8314 * TODO complete and handle entities
8315 */
8316 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008317 ((node->type == XML_COMMENT_NODE) ||
8318 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008319 (node->type == XML_XINCLUDE_START) ||
8320 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008321 (((node->type == XML_TEXT_NODE) ||
8322 (node->type == XML_CDATA_SECTION_NODE)) &&
8323 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8324 (IS_BLANK_NODE(node)))))) {
8325 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008326 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008327 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008328}
8329
8330/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008331 * xmlRelaxNGNormalize:
8332 * @ctxt: a schema validation context
8333 * @str: the string to normalize
8334 *
8335 * Implements the normalizeWhiteSpace( s ) function from
8336 * section 6.2.9 of the spec
8337 *
8338 * Returns the new string or NULL in case of error.
8339 */
8340static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008341xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8342{
Daniel Veillardedc91922003-01-26 00:52:04 +00008343 xmlChar *ret, *p;
8344 const xmlChar *tmp;
8345 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008346
Daniel Veillardedc91922003-01-26 00:52:04 +00008347 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008348 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008349 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008350 while (*tmp != 0)
8351 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008352 len = tmp - str;
8353
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008354 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008355 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008356 xmlRngVErrMemory(ctxt, "validating\n");
8357 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008358 }
8359 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008360 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008361 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008362 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008363 if (IS_BLANK_CH(*str)) {
8364 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008365 str++;
8366 if (*str == 0)
8367 break;
8368 *p++ = ' ';
8369 } else
8370 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008371 }
8372 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008373 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008374}
8375
8376/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008377 * xmlRelaxNGValidateDatatype:
8378 * @ctxt: a Relax-NG validation context
8379 * @value: the string value
8380 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008381 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008382 *
8383 * Validate the given value against the dataype
8384 *
8385 * Returns 0 if the validation succeeded or an error code.
8386 */
8387static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008388xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8389 const xmlChar * value,
8390 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8391{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008392 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008393 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008394 void *result = NULL;
8395 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008396
8397 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008398 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008399 }
8400 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008401 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008402 if ((define->attrs != NULL) &&
8403 (define->attrs->type == XML_RELAXNG_PARAM)) {
8404 ret =
8405 lib->check(lib->data, define->name, value, &result, node);
8406 } else {
8407 ret = lib->check(lib->data, define->name, value, NULL, node);
8408 }
8409 } else
8410 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008411 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008412 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8413 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8414 lib->freef(lib->data, result);
8415 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008416 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008417 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008418 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008419 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008420 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008421 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8422 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008423 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008424 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008425 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008426 if (lib->facet != NULL) {
8427 tmp = lib->facet(lib->data, define->name, cur->name,
8428 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008429 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008430 ret = -1;
8431 }
8432 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008433 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008434 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008435 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008436
Daniel Veillard4c004142003-10-07 11:33:24 +00008437 oldvalue = ctxt->state->value;
8438 oldendvalue = ctxt->state->endvalue;
8439 ctxt->state->value = (xmlChar *) value;
8440 ctxt->state->endvalue = NULL;
8441 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8442 ctxt->state->value = (xmlChar *) oldvalue;
8443 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008444 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008445 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008446 lib->freef(lib->data, result);
8447 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008448}
8449
8450/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008451 * xmlRelaxNGNextValue:
8452 * @ctxt: a Relax-NG validation context
8453 *
8454 * Skip to the next value when validating within a list
8455 *
8456 * Returns 0 if the operation succeeded or an error code.
8457 */
8458static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008459xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8460{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008461 xmlChar *cur;
8462
8463 cur = ctxt->state->value;
8464 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008465 ctxt->state->value = NULL;
8466 ctxt->state->endvalue = NULL;
8467 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008468 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008469 while (*cur != 0)
8470 cur++;
8471 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8472 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008473 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008474 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008475 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008476 ctxt->state->value = cur;
8477 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008478}
8479
8480/**
8481 * xmlRelaxNGValidateValueList:
8482 * @ctxt: a Relax-NG validation context
8483 * @defines: the list of definitions to verify
8484 *
8485 * Validate the given set of definitions for the current value
8486 *
8487 * Returns 0 if the validation succeeded or an error code.
8488 */
8489static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008490xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8491 xmlRelaxNGDefinePtr defines)
8492{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008493 int ret = 0;
8494
8495 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008496 ret = xmlRelaxNGValidateValue(ctxt, defines);
8497 if (ret != 0)
8498 break;
8499 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008500 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008501 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008502}
8503
8504/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008505 * xmlRelaxNGValidateValue:
8506 * @ctxt: a Relax-NG validation context
8507 * @define: the definition to verify
8508 *
8509 * Validate the given definition for the current value
8510 *
8511 * Returns 0 if the validation succeeded or an error code.
8512 */
8513static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008514xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8515 xmlRelaxNGDefinePtr define)
8516{
Daniel Veillardedc91922003-01-26 00:52:04 +00008517 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008518 xmlChar *value;
8519
8520 value = ctxt->state->value;
8521 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008522 case XML_RELAXNG_EMPTY:{
8523 if ((value != NULL) && (value[0] != 0)) {
8524 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008525
William M. Brack76e95df2003-10-18 16:20:14 +00008526 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008527 idx++;
8528 if (value[idx] != 0)
8529 ret = -1;
8530 }
8531 break;
8532 }
8533 case XML_RELAXNG_TEXT:
8534 break;
8535 case XML_RELAXNG_VALUE:{
8536 if (!xmlStrEqual(value, define->value)) {
8537 if (define->name != NULL) {
8538 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008539
Daniel Veillard4c004142003-10-07 11:33:24 +00008540 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8541 if ((lib != NULL) && (lib->comp != NULL)) {
8542 ret = lib->comp(lib->data, define->name,
8543 define->value, define->node,
8544 (void *) define->attrs,
8545 value, ctxt->state->node);
8546 } else
8547 ret = -1;
8548 if (ret < 0) {
8549 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8550 define->name);
8551 return (-1);
8552 } else if (ret == 1) {
8553 ret = 0;
8554 } else {
8555 ret = -1;
8556 }
8557 } else {
8558 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008559
Daniel Veillard4c004142003-10-07 11:33:24 +00008560 /*
8561 * TODO: trivial optimizations are possible by
8562 * computing at compile-time
8563 */
8564 nval = xmlRelaxNGNormalize(ctxt, define->value);
8565 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008566
Daniel Veillard4c004142003-10-07 11:33:24 +00008567 if ((nval == NULL) || (nvalue == NULL) ||
8568 (!xmlStrEqual(nval, nvalue)))
8569 ret = -1;
8570 if (nval != NULL)
8571 xmlFree(nval);
8572 if (nvalue != NULL)
8573 xmlFree(nvalue);
8574 }
8575 }
8576 if (ret == 0)
8577 xmlRelaxNGNextValue(ctxt);
8578 break;
8579 }
8580 case XML_RELAXNG_DATATYPE:{
8581 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8582 ctxt->state->seq);
8583 if (ret == 0)
8584 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008585
Daniel Veillard4c004142003-10-07 11:33:24 +00008586 break;
8587 }
8588 case XML_RELAXNG_CHOICE:{
8589 xmlRelaxNGDefinePtr list = define->content;
8590 xmlChar *oldvalue;
8591
8592 oldflags = ctxt->flags;
8593 ctxt->flags |= FLAGS_IGNORABLE;
8594
8595 oldvalue = ctxt->state->value;
8596 while (list != NULL) {
8597 ret = xmlRelaxNGValidateValue(ctxt, list);
8598 if (ret == 0) {
8599 break;
8600 }
8601 ctxt->state->value = oldvalue;
8602 list = list->next;
8603 }
8604 ctxt->flags = oldflags;
8605 if (ret != 0) {
8606 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8607 xmlRelaxNGDumpValidError(ctxt);
8608 } else {
8609 if (ctxt->errNr > 0)
8610 xmlRelaxNGPopErrors(ctxt, 0);
8611 }
8612 if (ret == 0)
8613 xmlRelaxNGNextValue(ctxt);
8614 break;
8615 }
8616 case XML_RELAXNG_LIST:{
8617 xmlRelaxNGDefinePtr list = define->content;
8618 xmlChar *oldvalue, *oldend, *val, *cur;
8619
Daniel Veillard416589a2003-02-17 17:25:42 +00008620#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008621 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008622#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008623
Daniel Veillard4c004142003-10-07 11:33:24 +00008624 oldvalue = ctxt->state->value;
8625 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008626
Daniel Veillard4c004142003-10-07 11:33:24 +00008627 val = xmlStrdup(oldvalue);
8628 if (val == NULL) {
8629 val = xmlStrdup(BAD_CAST "");
8630 }
8631 if (val == NULL) {
8632 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8633 return (-1);
8634 }
8635 cur = val;
8636 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008637 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008638 *cur = 0;
8639 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008640#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008641 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008642#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008643 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008644 *cur++ = 0;
8645 } else
8646 cur++;
8647 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008648#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008649 xmlGenericError(xmlGenericErrorContext,
8650 "list value: '%s' found %d items\n",
8651 oldvalue, nb_values);
8652 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008653#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008654 ctxt->state->endvalue = cur;
8655 cur = val;
8656 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8657 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008658
Daniel Veillard4c004142003-10-07 11:33:24 +00008659 ctxt->state->value = cur;
8660
8661 while (list != NULL) {
8662 if (ctxt->state->value == ctxt->state->endvalue)
8663 ctxt->state->value = NULL;
8664 ret = xmlRelaxNGValidateValue(ctxt, list);
8665 if (ret != 0) {
8666#ifdef DEBUG_LIST
8667 xmlGenericError(xmlGenericErrorContext,
8668 "Failed to validate value: '%s' with %d rule\n",
8669 ctxt->state->value, nb_values);
8670#endif
8671 break;
8672 }
8673#ifdef DEBUG_LIST
8674 nb_values++;
8675#endif
8676 list = list->next;
8677 }
8678
8679 if ((ret == 0) && (ctxt->state->value != NULL) &&
8680 (ctxt->state->value != ctxt->state->endvalue)) {
8681 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8682 ctxt->state->value);
8683 ret = -1;
8684 }
8685 xmlFree(val);
8686 ctxt->state->value = oldvalue;
8687 ctxt->state->endvalue = oldend;
8688 break;
8689 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008690 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008691 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8692 if (ret != 0) {
8693 break;
8694 }
8695 /* no break on purpose */
8696 case XML_RELAXNG_ZEROORMORE:{
8697 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008698
Daniel Veillard4c004142003-10-07 11:33:24 +00008699 oldflags = ctxt->flags;
8700 ctxt->flags |= FLAGS_IGNORABLE;
8701 cur = ctxt->state->value;
8702 temp = NULL;
8703 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8704 (temp != cur)) {
8705 temp = cur;
8706 ret =
8707 xmlRelaxNGValidateValueList(ctxt, define->content);
8708 if (ret != 0) {
8709 ctxt->state->value = temp;
8710 ret = 0;
8711 break;
8712 }
8713 cur = ctxt->state->value;
8714 }
8715 ctxt->flags = oldflags;
8716 if (ret != 0) {
8717 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8718 xmlRelaxNGDumpValidError(ctxt);
8719 } else {
8720 if (ctxt->errNr > 0)
8721 xmlRelaxNGPopErrors(ctxt, 0);
8722 }
8723 break;
8724 }
8725 case XML_RELAXNG_EXCEPT:{
8726 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008727
Daniel Veillard4c004142003-10-07 11:33:24 +00008728 list = define->content;
8729 while (list != NULL) {
8730 ret = xmlRelaxNGValidateValue(ctxt, list);
8731 if (ret == 0) {
8732 ret = -1;
8733 break;
8734 } else
8735 ret = 0;
8736 list = list->next;
8737 }
8738 break;
8739 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008740 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008741 case XML_RELAXNG_GROUP:{
8742 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008743
Daniel Veillard4c004142003-10-07 11:33:24 +00008744 list = define->content;
8745 while (list != NULL) {
8746 ret = xmlRelaxNGValidateValue(ctxt, list);
8747 if (ret != 0) {
8748 ret = -1;
8749 break;
8750 } else
8751 ret = 0;
8752 list = list->next;
8753 }
8754 break;
8755 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008756 case XML_RELAXNG_REF:
8757 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008758 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8759 break;
8760 default:
8761 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008762 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008763 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008764}
8765
8766/**
8767 * xmlRelaxNGValidateValueContent:
8768 * @ctxt: a Relax-NG validation context
8769 * @defines: the list of definitions to verify
8770 *
8771 * Validate the given definitions for the current value
8772 *
8773 * Returns 0 if the validation succeeded or an error code.
8774 */
8775static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008776xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8777 xmlRelaxNGDefinePtr defines)
8778{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008779 int ret = 0;
8780
8781 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008782 ret = xmlRelaxNGValidateValue(ctxt, defines);
8783 if (ret != 0)
8784 break;
8785 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008786 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008787 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008788}
8789
8790/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008791 * xmlRelaxNGAttributeMatch:
8792 * @ctxt: a Relax-NG validation context
8793 * @define: the definition to check
8794 * @prop: the attribute
8795 *
8796 * Check if the attribute matches the definition nameClass
8797 *
8798 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8799 */
8800static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008801xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8802 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8803{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008804 int ret;
8805
8806 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008807 if (!xmlStrEqual(define->name, prop->name))
8808 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008809 }
8810 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008811 if (define->ns[0] == 0) {
8812 if (prop->ns != NULL)
8813 return (0);
8814 } else {
8815 if ((prop->ns == NULL) ||
8816 (!xmlStrEqual(define->ns, prop->ns->href)))
8817 return (0);
8818 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008819 }
8820 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008821 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008822 define = define->nameClass;
8823 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008824 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008825
Daniel Veillard4c004142003-10-07 11:33:24 +00008826 list = define->content;
8827 while (list != NULL) {
8828 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8829 if (ret == 1)
8830 return (0);
8831 if (ret < 0)
8832 return (ret);
8833 list = list->next;
8834 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008835 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008836 TODO}
8837 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008838}
8839
8840/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008841 * xmlRelaxNGValidateAttribute:
8842 * @ctxt: a Relax-NG validation context
8843 * @define: the definition to verify
8844 *
8845 * Validate the given attribute definition for that node
8846 *
8847 * Returns 0 if the validation succeeded or an error code.
8848 */
8849static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008850xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8851 xmlRelaxNGDefinePtr define)
8852{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008853 int ret = 0, i;
8854 xmlChar *value, *oldvalue;
8855 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008856 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008857
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008858 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008859 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008860 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008861 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8862 tmp = ctxt->state->attrs[i];
8863 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8864 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8865 (tmp->ns == NULL)) ||
8866 ((tmp->ns != NULL) &&
8867 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8868 prop = tmp;
8869 break;
8870 }
8871 }
8872 }
8873 if (prop != NULL) {
8874 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8875 oldvalue = ctxt->state->value;
8876 oldseq = ctxt->state->seq;
8877 ctxt->state->seq = (xmlNodePtr) prop;
8878 ctxt->state->value = value;
8879 ctxt->state->endvalue = NULL;
8880 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8881 if (ctxt->state->value != NULL)
8882 value = ctxt->state->value;
8883 if (value != NULL)
8884 xmlFree(value);
8885 ctxt->state->value = oldvalue;
8886 ctxt->state->seq = oldseq;
8887 if (ret == 0) {
8888 /*
8889 * flag the attribute as processed
8890 */
8891 ctxt->state->attrs[i] = NULL;
8892 ctxt->state->nbAttrLeft--;
8893 }
8894 } else {
8895 ret = -1;
8896 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008897#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008898 xmlGenericError(xmlGenericErrorContext,
8899 "xmlRelaxNGValidateAttribute(%s): %d\n",
8900 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008901#endif
8902 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008903 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8904 tmp = ctxt->state->attrs[i];
8905 if ((tmp != NULL) &&
8906 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8907 prop = tmp;
8908 break;
8909 }
8910 }
8911 if (prop != NULL) {
8912 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8913 oldvalue = ctxt->state->value;
8914 oldseq = ctxt->state->seq;
8915 ctxt->state->seq = (xmlNodePtr) prop;
8916 ctxt->state->value = value;
8917 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8918 if (ctxt->state->value != NULL)
8919 value = ctxt->state->value;
8920 if (value != NULL)
8921 xmlFree(value);
8922 ctxt->state->value = oldvalue;
8923 ctxt->state->seq = oldseq;
8924 if (ret == 0) {
8925 /*
8926 * flag the attribute as processed
8927 */
8928 ctxt->state->attrs[i] = NULL;
8929 ctxt->state->nbAttrLeft--;
8930 }
8931 } else {
8932 ret = -1;
8933 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008934#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008935 if (define->ns != NULL) {
8936 xmlGenericError(xmlGenericErrorContext,
8937 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8938 define->ns, ret);
8939 } else {
8940 xmlGenericError(xmlGenericErrorContext,
8941 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8942 ret);
8943 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008944#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008945 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008946
8947 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008948}
8949
8950/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008951 * xmlRelaxNGValidateAttributeList:
8952 * @ctxt: a Relax-NG validation context
8953 * @define: the list of definition to verify
8954 *
8955 * Validate the given node against the list of attribute definitions
8956 *
8957 * Returns 0 if the validation succeeded or an error code.
8958 */
8959static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008960xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8961 xmlRelaxNGDefinePtr defines)
8962{
Daniel Veillardce192eb2003-04-16 15:58:05 +00008963 int ret = 0, res;
8964 int needmore = 0;
8965 xmlRelaxNGDefinePtr cur;
8966
8967 cur = defines;
8968 while (cur != NULL) {
8969 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008970 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8971 ret = -1;
8972 } else
8973 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008974 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008975 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008976 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00008977 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008978 cur = defines;
8979 while (cur != NULL) {
8980 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008981 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8982 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8983 if (res < 0)
8984 ret = -1;
8985 } else {
8986 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8987 return (-1);
8988 }
8989 if (res == -1) /* continues on -2 */
8990 break;
8991 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008992 cur = cur->next;
8993 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008994
8995 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008996}
8997
8998/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008999 * xmlRelaxNGNodeMatchesList:
9000 * @node: the node
9001 * @list: a NULL terminated array of definitions
9002 *
9003 * Check if a node can be matched by one of the definitions
9004 *
9005 * Returns 1 if matches 0 otherwise
9006 */
9007static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009008xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9009{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009010 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009011 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009012
9013 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009014 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009015
9016 cur = list[i++];
9017 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009018 if ((node->type == XML_ELEMENT_NODE) &&
9019 (cur->type == XML_RELAXNG_ELEMENT)) {
9020 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9021 if (tmp == 1)
9022 return (1);
9023 } else if (((node->type == XML_TEXT_NODE) ||
9024 (node->type == XML_CDATA_SECTION_NODE)) &&
9025 (cur->type == XML_RELAXNG_TEXT)) {
9026 return (1);
9027 }
9028 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009029 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009030 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009031}
9032
9033/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009034 * xmlRelaxNGValidateInterleave:
9035 * @ctxt: a Relax-NG validation context
9036 * @define: the definition to verify
9037 *
9038 * Validate an interleave definition for a node.
9039 *
9040 * Returns 0 if the validation succeeded or an error code.
9041 */
9042static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009043xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9044 xmlRelaxNGDefinePtr define)
9045{
William M. Brack779af002003-08-01 15:55:39 +00009046 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009047 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009048 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009049
9050 xmlRelaxNGValidStatePtr oldstate;
9051 xmlRelaxNGPartitionPtr partitions;
9052 xmlRelaxNGInterleaveGroupPtr group = NULL;
9053 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9054 xmlNodePtr *list = NULL, *lasts = NULL;
9055
9056 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009057 partitions = (xmlRelaxNGPartitionPtr) define->data;
9058 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009059 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009060 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9061 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009062 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009063 /*
9064 * Optimizations for MIXED
9065 */
9066 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009067 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009068 ctxt->flags |= FLAGS_MIXED_CONTENT;
9069 if (nbgroups == 2) {
9070 /*
9071 * this is a pure <mixed> case
9072 */
9073 if (ctxt->state != NULL)
9074 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9075 ctxt->state->seq);
9076 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9077 ret = xmlRelaxNGValidateDefinition(ctxt,
9078 partitions->groups[1]->
9079 rule);
9080 else
9081 ret = xmlRelaxNGValidateDefinition(ctxt,
9082 partitions->groups[0]->
9083 rule);
9084 if (ret == 0) {
9085 if (ctxt->state != NULL)
9086 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9087 ctxt->state->
9088 seq);
9089 }
9090 ctxt->flags = oldflags;
9091 return (ret);
9092 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009093 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009094
9095 /*
9096 * Build arrays to store the first and last node of the chain
9097 * pertaining to each group
9098 */
9099 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9100 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009101 xmlRngVErrMemory(ctxt, "validating\n");
9102 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009103 }
9104 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9105 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9106 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009107 xmlRngVErrMemory(ctxt, "validating\n");
9108 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009109 }
9110 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9111
9112 /*
9113 * Walk the sequence of children finding the right group and
9114 * sorting them in sequences.
9115 */
9116 cur = ctxt->state->seq;
9117 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9118 start = cur;
9119 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009120 ctxt->state->seq = cur;
9121 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009122 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009123 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009124
Daniel Veillard4c004142003-10-07 11:33:24 +00009125 if ((cur->type == XML_TEXT_NODE) ||
9126 (cur->type == XML_CDATA_SECTION_NODE)) {
9127 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9128 NULL);
9129 } else if (cur->type == XML_ELEMENT_NODE) {
9130 if (cur->ns != NULL) {
9131 tmp = xmlHashLookup2(partitions->triage, cur->name,
9132 cur->ns->href);
9133 if (tmp == NULL)
9134 tmp = xmlHashLookup2(partitions->triage,
9135 BAD_CAST "#any",
9136 cur->ns->href);
9137 } else
9138 tmp =
9139 xmlHashLookup2(partitions->triage, cur->name,
9140 NULL);
9141 if (tmp == NULL)
9142 tmp =
9143 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9144 NULL);
9145 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009146
Daniel Veillard4c004142003-10-07 11:33:24 +00009147 if (tmp == NULL) {
9148 i = nbgroups;
9149 } else {
9150 i = ((long) tmp) - 1;
9151 if (partitions->flags & IS_NEEDCHECK) {
9152 group = partitions->groups[i];
9153 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9154 i = nbgroups;
9155 }
9156 }
9157 } else {
9158 for (i = 0; i < nbgroups; i++) {
9159 group = partitions->groups[i];
9160 if (group == NULL)
9161 continue;
9162 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9163 break;
9164 }
9165 }
9166 /*
9167 * We break as soon as an element not matched is found
9168 */
9169 if (i >= nbgroups) {
9170 break;
9171 }
9172 if (lasts[i] != NULL) {
9173 lasts[i]->next = cur;
9174 lasts[i] = cur;
9175 } else {
9176 list[i] = cur;
9177 lasts[i] = cur;
9178 }
9179 if (cur->next != NULL)
9180 lastchg = cur->next;
9181 else
9182 lastchg = cur;
9183 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009184 }
9185 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009186 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9187 ret = -1;
9188 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009189 }
9190 lastelem = cur;
9191 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009192 for (i = 0; i < nbgroups; i++) {
9193 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9194 group = partitions->groups[i];
9195 if (lasts[i] != NULL) {
9196 last = lasts[i]->next;
9197 lasts[i]->next = NULL;
9198 }
9199 ctxt->state->seq = list[i];
9200 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9201 if (ret != 0)
9202 break;
9203 if (ctxt->state != NULL) {
9204 cur = ctxt->state->seq;
9205 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9206 xmlRelaxNGFreeValidState(ctxt, oldstate);
9207 oldstate = ctxt->state;
9208 ctxt->state = NULL;
9209 if (cur != NULL) {
9210 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9211 ret = -1;
9212 ctxt->state = oldstate;
9213 goto done;
9214 }
9215 } else if (ctxt->states != NULL) {
9216 int j;
9217 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009218
Daniel Veillard4c004142003-10-07 11:33:24 +00009219 for (j = 0; j < ctxt->states->nbState; j++) {
9220 cur = ctxt->states->tabState[j]->seq;
9221 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9222 if (cur == NULL) {
9223 found = 1;
9224 break;
9225 }
9226 }
9227 if (ctxt->states->nbState > 0) {
9228 xmlRelaxNGFreeValidState(ctxt, oldstate);
9229 oldstate =
9230 ctxt->states->tabState[ctxt->states->nbState - 1];
9231 }
9232 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9233 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9234 }
9235 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9236 ctxt->states = NULL;
9237 if (found == 0) {
9238 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9239 ret = -1;
9240 ctxt->state = oldstate;
9241 goto done;
9242 }
9243 } else {
9244 ret = -1;
9245 break;
9246 }
9247 if (lasts[i] != NULL) {
9248 lasts[i]->next = last;
9249 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009250 }
9251 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009252 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009253 ctxt->state = oldstate;
9254 ctxt->state->seq = lastelem;
9255 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009256 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9257 ret = -1;
9258 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009259 }
9260
Daniel Veillard4c004142003-10-07 11:33:24 +00009261 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009262 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009263 /*
9264 * builds the next links chain from the prev one
9265 */
9266 cur = lastchg;
9267 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009268 if ((cur == start) || (cur->prev == NULL))
9269 break;
9270 cur->prev->next = cur;
9271 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009272 }
9273 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009274 if (ctxt->errNr > errNr)
9275 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009276 }
9277
9278 xmlFree(list);
9279 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009280 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009281}
9282
9283/**
9284 * xmlRelaxNGValidateDefinitionList:
9285 * @ctxt: a Relax-NG validation context
9286 * @define: the list of definition to verify
9287 *
9288 * Validate the given node content against the (list) of definitions
9289 *
9290 * Returns 0 if the validation succeeded or an error code.
9291 */
9292static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009293xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9294 xmlRelaxNGDefinePtr defines)
9295{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009296 int ret = 0, res;
9297
9298
Daniel Veillard952379b2003-03-17 15:37:12 +00009299 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009300 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9301 BAD_CAST "NULL definition list");
9302 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009303 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009304 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009305 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9306 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9307 if (res < 0)
9308 ret = -1;
9309 } else {
9310 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9311 return (-1);
9312 }
9313 if (res == -1) /* continues on -2 */
9314 break;
9315 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009316 }
9317
Daniel Veillard4c004142003-10-07 11:33:24 +00009318 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009319}
9320
9321/**
9322 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009323 * @ctxt: a Relax-NG validation context
9324 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009325 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009326 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009327 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009328 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009329 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009330 */
9331static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009332xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9333 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9334{
Daniel Veillard580ced82003-03-21 21:22:48 +00009335 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009336
Daniel Veillardfd573f12003-03-16 17:52:32 +00009337 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009338 if (!xmlStrEqual(elem->name, define->name)) {
9339 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9340 return (0);
9341 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009342 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009343 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009344 if (elem->ns == NULL) {
9345 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9346 return (0);
9347 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9348 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9349 elem->name, define->ns);
9350 return (0);
9351 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009352 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009353 (define->name == NULL)) {
9354 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9355 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009356 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009357 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9358 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009359 }
9360
9361 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009362 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009363
9364 define = define->nameClass;
9365 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009366 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009367
Daniel Veillard4c004142003-10-07 11:33:24 +00009368 if (ctxt != NULL) {
9369 oldflags = ctxt->flags;
9370 ctxt->flags |= FLAGS_IGNORABLE;
9371 }
9372
9373 list = define->content;
9374 while (list != NULL) {
9375 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9376 if (ret == 1) {
9377 if (ctxt != NULL)
9378 ctxt->flags = oldflags;
9379 return (0);
9380 }
9381 if (ret < 0) {
9382 if (ctxt != NULL)
9383 ctxt->flags = oldflags;
9384 return (ret);
9385 }
9386 list = list->next;
9387 }
9388 ret = 1;
9389 if (ctxt != NULL) {
9390 ctxt->flags = oldflags;
9391 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009392 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009393 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009394
Daniel Veillard4c004142003-10-07 11:33:24 +00009395 if (ctxt != NULL) {
9396 oldflags = ctxt->flags;
9397 ctxt->flags |= FLAGS_IGNORABLE;
9398 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009399
Daniel Veillard4c004142003-10-07 11:33:24 +00009400 list = define->nameClass;
9401 while (list != NULL) {
9402 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9403 if (ret == 1) {
9404 if (ctxt != NULL)
9405 ctxt->flags = oldflags;
9406 return (1);
9407 }
9408 if (ret < 0) {
9409 if (ctxt != NULL)
9410 ctxt->flags = oldflags;
9411 return (ret);
9412 }
9413 list = list->next;
9414 }
9415 if (ctxt != NULL) {
9416 if (ret != 0) {
9417 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9418 xmlRelaxNGDumpValidError(ctxt);
9419 } else {
9420 if (ctxt->errNr > 0)
9421 xmlRelaxNGPopErrors(ctxt, 0);
9422 }
9423 }
9424 ret = 0;
9425 if (ctxt != NULL) {
9426 ctxt->flags = oldflags;
9427 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009428 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009429 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009430 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009431 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009432}
9433
9434/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009435 * xmlRelaxNGBestState:
9436 * @ctxt: a Relax-NG validation context
9437 *
9438 * Find the "best" state in the ctxt->states list of states to report
9439 * errors about. I.e. a state with no element left in the child list
9440 * or the one with the less attributes left.
9441 * This is called only if a falidation error was detected
9442 *
9443 * Returns the index of the "best" state or -1 in case of error
9444 */
9445static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009446xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9447{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009448 xmlRelaxNGValidStatePtr state;
9449 int i, tmp;
9450 int best = -1;
9451 int value = 1000000;
9452
9453 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9454 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009455 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009456
Daniel Veillard4c004142003-10-07 11:33:24 +00009457 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009458 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009459 if (state == NULL)
9460 continue;
9461 if (state->seq != NULL) {
9462 if ((best == -1) || (value > 100000)) {
9463 value = 100000;
9464 best = i;
9465 }
9466 } else {
9467 tmp = state->nbAttrLeft;
9468 if ((best == -1) || (value > tmp)) {
9469 value = tmp;
9470 best = i;
9471 }
9472 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009473 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009474 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009475}
9476
9477/**
9478 * xmlRelaxNGLogBestError:
9479 * @ctxt: a Relax-NG validation context
9480 *
9481 * Find the "best" state in the ctxt->states list of states to report
9482 * errors about and log it.
9483 */
9484static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009485xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9486{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009487 int best;
9488
9489 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9490 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009491 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009492
9493 best = xmlRelaxNGBestState(ctxt);
9494 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009495 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009496
Daniel Veillard4c004142003-10-07 11:33:24 +00009497 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009498 }
9499}
9500
9501/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009502 * xmlRelaxNGValidateElementEnd:
9503 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009504 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009505 *
9506 * Validate the end of the element, implements check that
9507 * there is nothing left not consumed in the element content
9508 * or in the attribute list.
9509 *
9510 * Returns 0 if the validation succeeded or an error code.
9511 */
9512static int
William M. Brack272693c2003-11-14 16:20:34 +00009513xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009514{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009515 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009516 xmlRelaxNGValidStatePtr state;
9517
9518 state = ctxt->state;
9519 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009520 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9521 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009522 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009523 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9524 state->node->name, state->seq->name);
9525 }
9526 return (-1);
9527 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009528 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009529 for (i = 0; i < state->nbAttrs; i++) {
9530 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009531 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009532 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9533 state->attrs[i]->name, state->node->name);
9534 }
9535 return (-1 - i);
9536 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009537 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009538 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009539}
9540
9541/**
9542 * xmlRelaxNGValidateState:
9543 * @ctxt: a Relax-NG validation context
9544 * @define: the definition to verify
9545 *
9546 * Validate the current state against the definition
9547 *
9548 * Returns 0 if the validation succeeded or an error code.
9549 */
9550static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009551xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9552 xmlRelaxNGDefinePtr define)
9553{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009554 xmlNodePtr node;
9555 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009556 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009557
9558 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009559 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9560 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009561 }
9562
9563 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009564 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009565 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009566 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009567 }
9568#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009569 for (i = 0; i < ctxt->depth; i++)
9570 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009571 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009572 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009573 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009574 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009575 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009576 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009577 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009578 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009579#endif
9580 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009581 switch (define->type) {
9582 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009583 node = xmlRelaxNGSkipIgnored(ctxt, node);
9584 ret = 0;
9585 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009586 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009587 ret = -1;
9588 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009589 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009590 while ((node != NULL) &&
9591 ((node->type == XML_TEXT_NODE) ||
9592 (node->type == XML_COMMENT_NODE) ||
9593 (node->type == XML_PI_NODE) ||
9594 (node->type == XML_CDATA_SECTION_NODE)))
9595 node = node->next;
9596 ctxt->state->seq = node;
9597 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009598 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009599 errNr = ctxt->errNr;
9600 node = xmlRelaxNGSkipIgnored(ctxt, node);
9601 if (node == NULL) {
9602 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9603 ret = -1;
9604 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9605 xmlRelaxNGDumpValidError(ctxt);
9606 break;
9607 }
9608 if (node->type != XML_ELEMENT_NODE) {
9609 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9610 ret = -1;
9611 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9612 xmlRelaxNGDumpValidError(ctxt);
9613 break;
9614 }
9615 /*
9616 * This node was already validated successfully against
9617 * this definition.
9618 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009619 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009620 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9621 if (ctxt->errNr > errNr)
9622 xmlRelaxNGPopErrors(ctxt, errNr);
9623 if (ctxt->errNr != 0) {
9624 while ((ctxt->err != NULL) &&
9625 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9626 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9627 ||
9628 ((ctxt->err->err ==
9629 XML_RELAXNG_ERR_ELEMEXTRANS)
9630 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9631 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9632 || (ctxt->err->err ==
9633 XML_RELAXNG_ERR_NOTELEM)))
9634 xmlRelaxNGValidErrorPop(ctxt);
9635 }
9636 break;
9637 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009638
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009639 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9640 if (ret <= 0) {
9641 ret = -1;
9642 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9643 xmlRelaxNGDumpValidError(ctxt);
9644 break;
9645 }
9646 ret = 0;
9647 if (ctxt->errNr != 0) {
9648 if (ctxt->errNr > errNr)
9649 xmlRelaxNGPopErrors(ctxt, errNr);
9650 while ((ctxt->err != NULL) &&
9651 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9652 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9653 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9654 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9655 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9656 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9657 xmlRelaxNGValidErrorPop(ctxt);
9658 }
9659 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009660
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009661 oldflags = ctxt->flags;
9662 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9663 ctxt->flags -= FLAGS_MIXED_CONTENT;
9664 }
9665 state = xmlRelaxNGNewValidState(ctxt, node);
9666 if (state == NULL) {
9667 ret = -1;
9668 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9669 xmlRelaxNGDumpValidError(ctxt);
9670 break;
9671 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009672
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009673 oldstate = ctxt->state;
9674 ctxt->state = state;
9675 if (define->attrs != NULL) {
9676 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9677 if (tmp != 0) {
9678 ret = -1;
9679 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9680 }
9681 }
9682 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009683 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9684 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9685 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009686
Daniel Veillard4c004142003-10-07 11:33:24 +00009687 nstate = xmlRelaxNGNewValidState(ctxt, node);
9688 ctxt->state = nstate;
9689 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009690
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009691 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9692 define->contModel,
9693 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009694 nseq = ctxt->state->seq;
9695 ctxt->state = tmpstate;
9696 ctxt->states = tmpstates;
9697 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009698
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009699#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009700 xmlGenericError(xmlGenericErrorContext,
9701 "Validating content of '%s' : %d\n",
9702 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009703#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009704 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009705 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009706
9707 if (ctxt->states != NULL) {
9708 tmp = -1;
9709
Daniel Veillardce192eb2003-04-16 15:58:05 +00009710 for (i = 0; i < ctxt->states->nbState; i++) {
9711 state = ctxt->states->tabState[i];
9712 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009713 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009714
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009715 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009716 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009717 break;
9718 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009719 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009720 if (tmp != 0) {
9721 /*
9722 * validation error, log the message for the "best" one
9723 */
9724 ctxt->flags |= FLAGS_IGNORABLE;
9725 xmlRelaxNGLogBestError(ctxt);
9726 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009727 for (i = 0; i < ctxt->states->nbState; i++) {
9728 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009729 ctxt->states->
9730 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009731 }
9732 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9733 ctxt->flags = oldflags;
9734 ctxt->states = NULL;
9735 if ((ret == 0) && (tmp == -1))
9736 ret = -1;
9737 } else {
9738 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009739 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009740 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009741 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009742 xmlRelaxNGFreeValidState(ctxt, state);
9743 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009744 } else {
9745 if (define->content != NULL) {
9746 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009747 define->
9748 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009749 if (tmp != 0) {
9750 ret = -1;
9751 if (ctxt->state == NULL) {
9752 ctxt->state = oldstate;
9753 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9754 node->name);
9755 ctxt->state = NULL;
9756 } else {
9757 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9758 node->name);
9759 }
9760
9761 }
9762 }
9763 if (ctxt->states != NULL) {
9764 tmp = -1;
9765
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009766 for (i = 0; i < ctxt->states->nbState; i++) {
9767 state = ctxt->states->tabState[i];
9768 ctxt->state = state;
9769
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009770 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009771 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009772 break;
9773 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009774 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009775 if (tmp != 0) {
9776 /*
9777 * validation error, log the message for the "best" one
9778 */
9779 ctxt->flags |= FLAGS_IGNORABLE;
9780 xmlRelaxNGLogBestError(ctxt);
9781 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009782 for (i = 0; i < ctxt->states->nbState; i++) {
9783 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009784 ctxt->states->
9785 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009786 }
9787 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9788 ctxt->flags = oldflags;
9789 ctxt->states = NULL;
9790 if ((ret == 0) && (tmp == -1))
9791 ret = -1;
9792 } else {
9793 state = ctxt->state;
9794 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009795 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009796 xmlRelaxNGFreeValidState(ctxt, state);
9797 }
9798 }
9799 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +00009800 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009801 }
9802 ctxt->flags = oldflags;
9803 ctxt->state = oldstate;
9804 if (oldstate != NULL)
9805 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9806 if (ret != 0) {
9807 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9808 xmlRelaxNGDumpValidError(ctxt);
9809 ret = 0;
9810 } else {
9811 ret = -2;
9812 }
9813 } else {
9814 if (ctxt->errNr > errNr)
9815 xmlRelaxNGPopErrors(ctxt, errNr);
9816 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009817
9818#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009819 xmlGenericError(xmlGenericErrorContext,
9820 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9821 node->name, ret);
9822 if (oldstate == NULL)
9823 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9824 else if (oldstate->seq == NULL)
9825 xmlGenericError(xmlGenericErrorContext, ": done\n");
9826 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9827 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9828 oldstate->seq->name);
9829 else
9830 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9831 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009832#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009833 break;
9834 case XML_RELAXNG_OPTIONAL:{
9835 errNr = ctxt->errNr;
9836 oldflags = ctxt->flags;
9837 ctxt->flags |= FLAGS_IGNORABLE;
9838 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9839 ret =
9840 xmlRelaxNGValidateDefinitionList(ctxt,
9841 define->content);
9842 if (ret != 0) {
9843 if (ctxt->state != NULL)
9844 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9845 ctxt->state = oldstate;
9846 ctxt->flags = oldflags;
9847 ret = 0;
9848 if (ctxt->errNr > errNr)
9849 xmlRelaxNGPopErrors(ctxt, errNr);
9850 break;
9851 }
9852 if (ctxt->states != NULL) {
9853 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9854 } else {
9855 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9856 if (ctxt->states == NULL) {
9857 xmlRelaxNGFreeValidState(ctxt, oldstate);
9858 ctxt->flags = oldflags;
9859 ret = -1;
9860 if (ctxt->errNr > errNr)
9861 xmlRelaxNGPopErrors(ctxt, errNr);
9862 break;
9863 }
9864 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9865 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9866 ctxt->state = NULL;
9867 }
9868 ctxt->flags = oldflags;
9869 ret = 0;
9870 if (ctxt->errNr > errNr)
9871 xmlRelaxNGPopErrors(ctxt, errNr);
9872 break;
9873 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009874 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009875 errNr = ctxt->errNr;
9876 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9877 if (ret != 0) {
9878 break;
9879 }
9880 if (ctxt->errNr > errNr)
9881 xmlRelaxNGPopErrors(ctxt, errNr);
9882 /* no break on purpose */
9883 case XML_RELAXNG_ZEROORMORE:{
9884 int progress;
9885 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9886 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009887
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009888 errNr = ctxt->errNr;
9889 res = xmlRelaxNGNewStates(ctxt, 1);
9890 if (res == NULL) {
9891 ret = -1;
9892 break;
9893 }
9894 /*
9895 * All the input states are also exit states
9896 */
9897 if (ctxt->state != NULL) {
9898 xmlRelaxNGAddStates(ctxt, res,
9899 xmlRelaxNGCopyValidState(ctxt,
9900 ctxt->
9901 state));
9902 } else {
9903 for (j = 0; j < ctxt->states->nbState; j++) {
9904 xmlRelaxNGAddStates(ctxt, res,
9905 xmlRelaxNGCopyValidState(ctxt,
9906 ctxt->
9907 states->
9908 tabState
9909 [j]));
9910 }
9911 }
9912 oldflags = ctxt->flags;
9913 ctxt->flags |= FLAGS_IGNORABLE;
9914 do {
9915 progress = 0;
9916 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009917
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009918 if (ctxt->states != NULL) {
9919 states = ctxt->states;
9920 for (i = 0; i < states->nbState; i++) {
9921 ctxt->state = states->tabState[i];
9922 ctxt->states = NULL;
9923 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9924 define->
9925 content);
9926 if (ret == 0) {
9927 if (ctxt->state != NULL) {
9928 tmp = xmlRelaxNGAddStates(ctxt, res,
9929 ctxt->state);
9930 ctxt->state = NULL;
9931 if (tmp == 1)
9932 progress = 1;
9933 } else if (ctxt->states != NULL) {
9934 for (j = 0; j < ctxt->states->nbState;
9935 j++) {
9936 tmp =
9937 xmlRelaxNGAddStates(ctxt, res,
9938 ctxt->
9939 states->
9940 tabState
9941 [j]);
9942 if (tmp == 1)
9943 progress = 1;
9944 }
9945 xmlRelaxNGFreeStates(ctxt,
9946 ctxt->states);
9947 ctxt->states = NULL;
9948 }
9949 } else {
9950 if (ctxt->state != NULL) {
9951 xmlRelaxNGFreeValidState(ctxt,
9952 ctxt->state);
9953 ctxt->state = NULL;
9954 }
9955 }
9956 }
9957 } else {
9958 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9959 define->
9960 content);
9961 if (ret != 0) {
9962 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9963 ctxt->state = NULL;
9964 } else {
9965 base = res->nbState;
9966 if (ctxt->state != NULL) {
9967 tmp = xmlRelaxNGAddStates(ctxt, res,
9968 ctxt->state);
9969 ctxt->state = NULL;
9970 if (tmp == 1)
9971 progress = 1;
9972 } else if (ctxt->states != NULL) {
9973 for (j = 0; j < ctxt->states->nbState; j++) {
9974 tmp = xmlRelaxNGAddStates(ctxt, res,
9975 ctxt->
9976 states->
9977 tabState[j]);
9978 if (tmp == 1)
9979 progress = 1;
9980 }
9981 if (states == NULL) {
9982 states = ctxt->states;
9983 } else {
9984 xmlRelaxNGFreeStates(ctxt,
9985 ctxt->states);
9986 }
9987 ctxt->states = NULL;
9988 }
9989 }
9990 }
9991 if (progress) {
9992 /*
9993 * Collect all the new nodes added at that step
9994 * and make them the new node set
9995 */
9996 if (res->nbState - base == 1) {
9997 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9998 res->
9999 tabState
10000 [base]);
10001 } else {
10002 if (states == NULL) {
10003 xmlRelaxNGNewStates(ctxt,
10004 res->nbState - base);
10005 }
10006 states->nbState = 0;
10007 for (i = base; i < res->nbState; i++)
10008 xmlRelaxNGAddStates(ctxt, states,
10009 xmlRelaxNGCopyValidState
10010 (ctxt,
10011 res->tabState[i]));
10012 ctxt->states = states;
10013 }
10014 }
10015 } while (progress == 1);
10016 if (states != NULL) {
10017 xmlRelaxNGFreeStates(ctxt, states);
10018 }
10019 ctxt->states = res;
10020 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010021#if 0
10022 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010023 * errors may have to be propagated back...
10024 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010025 if (ctxt->errNr > errNr)
10026 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010027#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010028 ret = 0;
10029 break;
10030 }
10031 case XML_RELAXNG_CHOICE:{
10032 xmlRelaxNGDefinePtr list = NULL;
10033 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010034
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010035 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010036
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010037 errNr = ctxt->errNr;
10038 if ((define->dflags & IS_TRIABLE)
10039 && (define->data != NULL)) {
10040 xmlHashTablePtr triage =
10041 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010042
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010043 /*
10044 * Something we can optimize cleanly there is only one
10045 * possble branch out !
10046 */
10047 if (node == NULL) {
10048 ret = -1;
10049 break;
10050 }
10051 if ((node->type == XML_TEXT_NODE) ||
10052 (node->type == XML_CDATA_SECTION_NODE)) {
10053 list =
10054 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10055 } else if (node->type == XML_ELEMENT_NODE) {
10056 if (node->ns != NULL) {
10057 list = xmlHashLookup2(triage, node->name,
10058 node->ns->href);
10059 if (list == NULL)
10060 list =
10061 xmlHashLookup2(triage, BAD_CAST "#any",
10062 node->ns->href);
10063 } else
10064 list =
10065 xmlHashLookup2(triage, node->name, NULL);
10066 if (list == NULL)
10067 list =
10068 xmlHashLookup2(triage, BAD_CAST "#any",
10069 NULL);
10070 }
10071 if (list == NULL) {
10072 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010073 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010074 break;
10075 }
10076 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10077 if (ret == 0) {
10078 }
10079 break;
10080 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010081
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010082 list = define->content;
10083 oldflags = ctxt->flags;
10084 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010085
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010086 while (list != NULL) {
10087 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10088 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10089 if (ret == 0) {
10090 if (states == NULL) {
10091 states = xmlRelaxNGNewStates(ctxt, 1);
10092 }
10093 if (ctxt->state != NULL) {
10094 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10095 } else if (ctxt->states != NULL) {
10096 for (i = 0; i < ctxt->states->nbState; i++) {
10097 xmlRelaxNGAddStates(ctxt, states,
10098 ctxt->states->
10099 tabState[i]);
10100 }
10101 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10102 ctxt->states = NULL;
10103 }
10104 } else {
10105 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10106 }
10107 ctxt->state = oldstate;
10108 list = list->next;
10109 }
10110 if (states != NULL) {
10111 xmlRelaxNGFreeValidState(ctxt, oldstate);
10112 ctxt->states = states;
10113 ctxt->state = NULL;
10114 ret = 0;
10115 } else {
10116 ctxt->states = NULL;
10117 }
10118 ctxt->flags = oldflags;
10119 if (ret != 0) {
10120 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10121 xmlRelaxNGDumpValidError(ctxt);
10122 }
10123 } else {
10124 if (ctxt->errNr > errNr)
10125 xmlRelaxNGPopErrors(ctxt, errNr);
10126 }
10127 break;
10128 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010129 case XML_RELAXNG_DEF:
10130 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010131 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10132 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010133 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010134 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10135 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010136 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010137 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10138 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010139 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010140 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010141 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010142 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010143 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010144 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10145 break;
10146 case XML_RELAXNG_DATATYPE:{
10147 xmlNodePtr child;
10148 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010149
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010150 child = node;
10151 while (child != NULL) {
10152 if (child->type == XML_ELEMENT_NODE) {
10153 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10154 node->parent->name);
10155 ret = -1;
10156 break;
10157 } else if ((child->type == XML_TEXT_NODE) ||
10158 (child->type == XML_CDATA_SECTION_NODE)) {
10159 content = xmlStrcat(content, child->content);
10160 }
10161 /* TODO: handle entities ... */
10162 child = child->next;
10163 }
10164 if (ret == -1) {
10165 if (content != NULL)
10166 xmlFree(content);
10167 break;
10168 }
10169 if (content == NULL) {
10170 content = xmlStrdup(BAD_CAST "");
10171 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010172 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010173 ret = -1;
10174 break;
10175 }
10176 }
10177 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10178 ctxt->state->seq);
10179 if (ret == -1) {
10180 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10181 } else if (ret == 0) {
10182 ctxt->state->seq = NULL;
10183 }
10184 if (content != NULL)
10185 xmlFree(content);
10186 break;
10187 }
10188 case XML_RELAXNG_VALUE:{
10189 xmlChar *content = NULL;
10190 xmlChar *oldvalue;
10191 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010192
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010193 child = node;
10194 while (child != NULL) {
10195 if (child->type == XML_ELEMENT_NODE) {
10196 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10197 node->parent->name);
10198 ret = -1;
10199 break;
10200 } else if ((child->type == XML_TEXT_NODE) ||
10201 (child->type == XML_CDATA_SECTION_NODE)) {
10202 content = xmlStrcat(content, child->content);
10203 }
10204 /* TODO: handle entities ... */
10205 child = child->next;
10206 }
10207 if (ret == -1) {
10208 if (content != NULL)
10209 xmlFree(content);
10210 break;
10211 }
10212 if (content == NULL) {
10213 content = xmlStrdup(BAD_CAST "");
10214 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010215 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010216 ret = -1;
10217 break;
10218 }
10219 }
10220 oldvalue = ctxt->state->value;
10221 ctxt->state->value = content;
10222 ret = xmlRelaxNGValidateValue(ctxt, define);
10223 ctxt->state->value = oldvalue;
10224 if (ret == -1) {
10225 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10226 } else if (ret == 0) {
10227 ctxt->state->seq = NULL;
10228 }
10229 if (content != NULL)
10230 xmlFree(content);
10231 break;
10232 }
10233 case XML_RELAXNG_LIST:{
10234 xmlChar *content;
10235 xmlNodePtr child;
10236 xmlChar *oldvalue, *oldendvalue;
10237 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010238
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010239 /*
10240 * Make sure it's only text nodes
10241 */
10242
10243 content = NULL;
10244 child = node;
10245 while (child != NULL) {
10246 if (child->type == XML_ELEMENT_NODE) {
10247 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10248 node->parent->name);
10249 ret = -1;
10250 break;
10251 } else if ((child->type == XML_TEXT_NODE) ||
10252 (child->type == XML_CDATA_SECTION_NODE)) {
10253 content = xmlStrcat(content, child->content);
10254 }
10255 /* TODO: handle entities ... */
10256 child = child->next;
10257 }
10258 if (ret == -1) {
10259 if (content != NULL)
10260 xmlFree(content);
10261 break;
10262 }
10263 if (content == NULL) {
10264 content = xmlStrdup(BAD_CAST "");
10265 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010266 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010267 ret = -1;
10268 break;
10269 }
10270 }
10271 len = xmlStrlen(content);
10272 oldvalue = ctxt->state->value;
10273 oldendvalue = ctxt->state->endvalue;
10274 ctxt->state->value = content;
10275 ctxt->state->endvalue = content + len;
10276 ret = xmlRelaxNGValidateValue(ctxt, define);
10277 ctxt->state->value = oldvalue;
10278 ctxt->state->endvalue = oldendvalue;
10279 if (ret == -1) {
10280 VALID_ERR(XML_RELAXNG_ERR_LIST);
10281 } else if ((ret == 0) && (node != NULL)) {
10282 ctxt->state->seq = node->next;
10283 }
10284 if (content != NULL)
10285 xmlFree(content);
10286 break;
10287 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010288 case XML_RELAXNG_EXCEPT:
10289 case XML_RELAXNG_PARAM:
10290 TODO ret = -1;
10291 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010292 }
10293 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010294#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010295 for (i = 0; i < ctxt->depth; i++)
10296 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010297 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010298 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010299 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010300 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010301 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010302 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010303 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010304 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010305#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010306 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010307}
10308
10309/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010310 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010311 * @ctxt: a Relax-NG validation context
10312 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010313 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010314 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010315 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010316 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010317 */
10318static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010319xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10320 xmlRelaxNGDefinePtr define)
10321{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010322 xmlRelaxNGStatesPtr states, res;
10323 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010324
Daniel Veillardfd573f12003-03-16 17:52:32 +000010325 /*
10326 * We should NOT have both ctxt->state and ctxt->states
10327 */
10328 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010329 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10330 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010331 }
10332
10333 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010334 if (ctxt->states != NULL) {
10335 ctxt->state = ctxt->states->tabState[0];
10336 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10337 ctxt->states = NULL;
10338 }
10339 ret = xmlRelaxNGValidateState(ctxt, define);
10340 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10341 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10342 ctxt->state = NULL;
10343 }
10344 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10345 ctxt->state = ctxt->states->tabState[0];
10346 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10347 ctxt->states = NULL;
10348 }
10349 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010350 }
10351
10352 states = ctxt->states;
10353 ctxt->states = NULL;
10354 res = NULL;
10355 j = 0;
10356 oldflags = ctxt->flags;
10357 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010358 for (i = 0; i < states->nbState; i++) {
10359 ctxt->state = states->tabState[i];
10360 ctxt->states = NULL;
10361 ret = xmlRelaxNGValidateState(ctxt, define);
10362 /*
10363 * We should NOT have both ctxt->state and ctxt->states
10364 */
10365 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10366 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10367 ctxt->state = NULL;
10368 }
10369 if (ret == 0) {
10370 if (ctxt->states == NULL) {
10371 if (res != NULL) {
10372 /* add the state to the container */
10373 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10374 ctxt->state = NULL;
10375 } else {
10376 /* add the state directly in states */
10377 states->tabState[j++] = ctxt->state;
10378 ctxt->state = NULL;
10379 }
10380 } else {
10381 if (res == NULL) {
10382 /* make it the new container and copy other results */
10383 res = ctxt->states;
10384 ctxt->states = NULL;
10385 for (k = 0; k < j; k++)
10386 xmlRelaxNGAddStates(ctxt, res,
10387 states->tabState[k]);
10388 } else {
10389 /* add all the new results to res and reff the container */
10390 for (k = 0; k < ctxt->states->nbState; k++)
10391 xmlRelaxNGAddStates(ctxt, res,
10392 ctxt->states->tabState[k]);
10393 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10394 ctxt->states = NULL;
10395 }
10396 }
10397 } else {
10398 if (ctxt->state != NULL) {
10399 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10400 ctxt->state = NULL;
10401 } else if (ctxt->states != NULL) {
10402 for (k = 0; k < ctxt->states->nbState; k++)
10403 xmlRelaxNGFreeValidState(ctxt,
10404 ctxt->states->tabState[k]);
10405 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10406 ctxt->states = NULL;
10407 }
10408 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010409 }
10410 ctxt->flags = oldflags;
10411 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010412 xmlRelaxNGFreeStates(ctxt, states);
10413 ctxt->states = res;
10414 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010415 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010416 states->nbState = j;
10417 ctxt->states = states;
10418 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010419 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010420 ctxt->state = states->tabState[0];
10421 xmlRelaxNGFreeStates(ctxt, states);
10422 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010423 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010424 ret = -1;
10425 xmlRelaxNGFreeStates(ctxt, states);
10426 if (ctxt->states != NULL) {
10427 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10428 ctxt->states = NULL;
10429 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010430 }
10431 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010432 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10433 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010434 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010435 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010436}
10437
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010438/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010439 * xmlRelaxNGValidateDocument:
10440 * @ctxt: a Relax-NG validation context
10441 * @doc: the document
10442 *
10443 * Validate the given document
10444 *
10445 * Returns 0 if the validation succeeded or an error code.
10446 */
10447static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010448xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10449{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010450 int ret;
10451 xmlRelaxNGPtr schema;
10452 xmlRelaxNGGrammarPtr grammar;
10453 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010454 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010455
10456 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010457 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010458
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010459 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010460 schema = ctxt->schema;
10461 grammar = schema->topgrammar;
10462 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010463 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10464 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010465 }
10466 state = xmlRelaxNGNewValidState(ctxt, NULL);
10467 ctxt->state = state;
10468 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010469 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010470 state = ctxt->state;
10471 node = state->seq;
10472 node = xmlRelaxNGSkipIgnored(ctxt, node);
10473 if (node != NULL) {
10474 if (ret != -1) {
10475 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10476 ret = -1;
10477 }
10478 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010479 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010480 int i;
10481 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010482
Daniel Veillard4c004142003-10-07 11:33:24 +000010483 for (i = 0; i < ctxt->states->nbState; i++) {
10484 state = ctxt->states->tabState[i];
10485 node = state->seq;
10486 node = xmlRelaxNGSkipIgnored(ctxt, node);
10487 if (node == NULL)
10488 tmp = 0;
10489 xmlRelaxNGFreeValidState(ctxt, state);
10490 }
10491 if (tmp == -1) {
10492 if (ret != -1) {
10493 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10494 ret = -1;
10495 }
10496 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010497 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010498 if (ctxt->state != NULL) {
10499 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010500 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010501 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010502 if (ret != 0)
10503 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010504#ifdef DEBUG
10505 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010506 ctxt->error(ctxt->userData,
10507 "%d Extra error messages left on stack !\n",
10508 ctxt->errNr);
10509 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010510 }
10511#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010512#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010513 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010514 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010515
Daniel Veillard4c004142003-10-07 11:33:24 +000010516 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10517 vctxt.valid = 1;
10518 vctxt.error = ctxt->error;
10519 vctxt.warning = ctxt->warning;
10520 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010521
Daniel Veillard4c004142003-10-07 11:33:24 +000010522 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10523 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010524 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010525#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010526 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010527 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010528
Daniel Veillard4c004142003-10-07 11:33:24 +000010529 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010530}
10531
Daniel Veillardfd573f12003-03-16 17:52:32 +000010532/************************************************************************
10533 * *
10534 * Validation interfaces *
10535 * *
10536 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010537
Daniel Veillard6eadf632003-01-23 18:29:16 +000010538/**
10539 * xmlRelaxNGNewValidCtxt:
10540 * @schema: a precompiled XML RelaxNGs
10541 *
10542 * Create an XML RelaxNGs validation context based on the given schema
10543 *
10544 * Returns the validation context or NULL in case of error
10545 */
10546xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010547xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10548{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010549 xmlRelaxNGValidCtxtPtr ret;
10550
10551 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10552 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010553 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010554 return (NULL);
10555 }
10556 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10557 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010558 ret->error = xmlGenericError;
10559 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010560 ret->errNr = 0;
10561 ret->errMax = 0;
10562 ret->err = NULL;
10563 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010564 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010565 ret->states = NULL;
10566 ret->freeState = NULL;
10567 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010568 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010569 return (ret);
10570}
10571
10572/**
10573 * xmlRelaxNGFreeValidCtxt:
10574 * @ctxt: the schema validation context
10575 *
10576 * Free the resources associated to the schema validation context
10577 */
10578void
Daniel Veillard4c004142003-10-07 11:33:24 +000010579xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10580{
Daniel Veillard798024a2003-03-19 10:36:09 +000010581 int k;
10582
Daniel Veillard6eadf632003-01-23 18:29:16 +000010583 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010584 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010585 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010586 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010587 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010588 for (k = 0; k < ctxt->freeState->nbState; k++) {
10589 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10590 }
10591 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010592 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010593 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010594 for (k = 0; k < ctxt->freeStatesNr; k++) {
10595 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10596 }
10597 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010598 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010599 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010600 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010601 if (ctxt->elemTab != NULL) {
10602 xmlRegExecCtxtPtr exec;
10603
Daniel Veillard4c004142003-10-07 11:33:24 +000010604 exec = xmlRelaxNGElemPop(ctxt);
10605 while (exec != NULL) {
10606 xmlRegFreeExecCtxt(exec);
10607 exec = xmlRelaxNGElemPop(ctxt);
10608 }
10609 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010610 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010611 xmlFree(ctxt);
10612}
10613
10614/**
10615 * xmlRelaxNGSetValidErrors:
10616 * @ctxt: a Relax-NG validation context
10617 * @err: the error function
10618 * @warn: the warning function
10619 * @ctx: the functions context
10620 *
10621 * Set the error and warning callback informations
10622 */
10623void
10624xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010625 xmlRelaxNGValidityErrorFunc err,
10626 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10627{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010628 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010629 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010630 ctxt->error = err;
10631 ctxt->warning = warn;
10632 ctxt->userData = ctx;
10633}
10634
10635/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010636 * xmlRelaxNGGetValidErrors:
10637 * @ctxt: a Relax-NG validation context
10638 * @err: the error function result
10639 * @warn: the warning function result
10640 * @ctx: the functions context result
10641 *
10642 * Get the error and warning callback informations
10643 *
10644 * Returns -1 in case of error and 0 otherwise
10645 */
10646int
10647xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010648 xmlRelaxNGValidityErrorFunc * err,
10649 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10650{
Daniel Veillard409a8142003-07-18 15:16:57 +000010651 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010652 return (-1);
10653 if (err != NULL)
10654 *err = ctxt->error;
10655 if (warn != NULL)
10656 *warn = ctxt->warning;
10657 if (ctx != NULL)
10658 *ctx = ctxt->userData;
10659 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010660}
10661
10662/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010663 * xmlRelaxNGValidateDoc:
10664 * @ctxt: a Relax-NG validation context
10665 * @doc: a parsed document tree
10666 *
10667 * Validate a document tree in memory.
10668 *
10669 * Returns 0 if the document is valid, a positive error code
10670 * number otherwise and -1 in case of internal or API error.
10671 */
10672int
Daniel Veillard4c004142003-10-07 11:33:24 +000010673xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10674{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010675 int ret;
10676
10677 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010678 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010679
10680 ctxt->doc = doc;
10681
10682 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010683 /*
10684 * TODO: build error codes
10685 */
10686 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010687 return (1);
10688 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010689}
10690
10691#endif /* LIBXML_SCHEMAS_ENABLED */