blob: 2715aec47ee9e0d2ca14c1e4518bd8326c079907 [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 Veillard4c004142003-10-07 11:33:24 +00002962 ret = 0;
2963 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002964 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00002965 ret = -1;
2966 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002967 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002968 if (ret == 0)
2969 def->dflags |= IS_NOT_COMPILABLE;
2970 if (ret == 1)
2971 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002972#ifdef DEBUG_COMPILE
2973 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002974 xmlGenericError(xmlGenericErrorContext,
2975 "RelaxNGIsCompileable %s : true\n",
2976 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002977 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002978 xmlGenericError(xmlGenericErrorContext,
2979 "RelaxNGIsCompileable %s : false\n",
2980 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002981 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002982 xmlGenericError(xmlGenericErrorContext,
2983 "Problem in RelaxNGIsCompileable %s\n",
2984 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002985 }
2986#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002987 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002988}
2989
2990/**
2991 * xmlRelaxNGCompile:
2992 * ctxt: the RelaxNG parser context
2993 * @define: the definition tree to compile
2994 *
2995 * Compile the set of definitions, it works recursively, till the
2996 * element boundaries, where it tries to compile the content if possible
2997 *
2998 * Returns 0 if success and -1 in case of error
2999 */
3000static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003001xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3002{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003003 int ret = 0;
3004 xmlRelaxNGDefinePtr list;
3005
Daniel Veillard4c004142003-10-07 11:33:24 +00003006 if ((ctxt == NULL) || (def == NULL))
3007 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003008
Daniel Veillard4c004142003-10-07 11:33:24 +00003009 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003010 case XML_RELAXNG_START:
3011 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003012 xmlAutomataPtr oldam = ctxt->am;
3013 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003014
3015 def->depth = -25;
3016
Daniel Veillard4c004142003-10-07 11:33:24 +00003017 list = def->content;
3018 ctxt->am = xmlNewAutomata();
3019 if (ctxt->am == NULL)
3020 return (-1);
3021 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3022 while (list != NULL) {
3023 xmlRelaxNGCompile(ctxt, list);
3024 list = list->next;
3025 }
3026 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3027 def->contModel = xmlAutomataCompile(ctxt->am);
3028 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003029
Daniel Veillard4c004142003-10-07 11:33:24 +00003030 xmlFreeAutomata(ctxt->am);
3031 ctxt->state = oldstate;
3032 ctxt->am = oldam;
3033 }
3034 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003035 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003036 if ((ctxt->am != NULL) && (def->name != NULL)) {
3037 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3038 ctxt->state, NULL,
3039 def->name, def->ns,
3040 def);
3041 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003042 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003043 xmlAutomataPtr oldam = ctxt->am;
3044 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003045
3046 def->depth = -25;
3047
Daniel Veillard4c004142003-10-07 11:33:24 +00003048 list = def->content;
3049 ctxt->am = xmlNewAutomata();
3050 if (ctxt->am == NULL)
3051 return (-1);
3052 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3053 while (list != NULL) {
3054 xmlRelaxNGCompile(ctxt, list);
3055 list = list->next;
3056 }
3057 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3058 def->contModel = xmlAutomataCompile(ctxt->am);
3059 if (!xmlRegexpIsDeterminist(def->contModel)) {
3060 /*
3061 * we can only use the automata if it is determinist
3062 */
3063 xmlRegFreeRegexp(def->contModel);
3064 def->contModel = NULL;
3065 }
3066 xmlFreeAutomata(ctxt->am);
3067 ctxt->state = oldstate;
3068 ctxt->am = oldam;
3069 } else {
3070 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003071
Daniel Veillard4c004142003-10-07 11:33:24 +00003072 /*
3073 * we can't build the content model for this element content
3074 * but it still might be possible to build it for some of its
3075 * children, recurse.
3076 */
3077 ret = xmlRelaxNGTryCompile(ctxt, def);
3078 ctxt->am = oldam;
3079 }
3080 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003081 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003082 ret = xmlRelaxNGCompile(ctxt, def->content);
3083 break;
3084 case XML_RELAXNG_OPTIONAL:{
3085 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003086
Daniel Veillard4c004142003-10-07 11:33:24 +00003087 xmlRelaxNGCompile(ctxt, def->content);
3088 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3089 break;
3090 }
3091 case XML_RELAXNG_ZEROORMORE:{
3092 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003093
Daniel Veillard4c004142003-10-07 11:33:24 +00003094 ctxt->state =
3095 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3096 oldstate = ctxt->state;
3097 list = def->content;
3098 while (list != NULL) {
3099 xmlRelaxNGCompile(ctxt, list);
3100 list = list->next;
3101 }
3102 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3103 ctxt->state =
3104 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3105 break;
3106 }
3107 case XML_RELAXNG_ONEORMORE:{
3108 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003109
Daniel Veillard4c004142003-10-07 11:33:24 +00003110 list = def->content;
3111 while (list != NULL) {
3112 xmlRelaxNGCompile(ctxt, list);
3113 list = list->next;
3114 }
3115 oldstate = ctxt->state;
3116 list = def->content;
3117 while (list != NULL) {
3118 xmlRelaxNGCompile(ctxt, list);
3119 list = list->next;
3120 }
3121 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3122 ctxt->state =
3123 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3124 break;
3125 }
3126 case XML_RELAXNG_CHOICE:{
3127 xmlAutomataStatePtr target = NULL;
3128 xmlAutomataStatePtr oldstate = ctxt->state;
3129
3130 list = def->content;
3131 while (list != NULL) {
3132 ctxt->state = oldstate;
3133 ret = xmlRelaxNGCompile(ctxt, list);
3134 if (ret != 0)
3135 break;
3136 if (target == NULL)
3137 target = ctxt->state;
3138 else {
3139 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3140 target);
3141 }
3142 list = list->next;
3143 }
3144 ctxt->state = target;
3145
3146 break;
3147 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003148 case XML_RELAXNG_REF:
3149 case XML_RELAXNG_EXTERNALREF:
3150 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003151 case XML_RELAXNG_GROUP:
3152 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003153 list = def->content;
3154 while (list != NULL) {
3155 ret = xmlRelaxNGCompile(ctxt, list);
3156 if (ret != 0)
3157 break;
3158 list = list->next;
3159 }
3160 break;
3161 case XML_RELAXNG_TEXT:{
3162 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003163
Daniel Veillard4c004142003-10-07 11:33:24 +00003164 ctxt->state =
3165 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3166 oldstate = ctxt->state;
3167 xmlRelaxNGCompile(ctxt, def->content);
3168 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3169 ctxt->state, BAD_CAST "#text",
3170 NULL);
3171 ctxt->state =
3172 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3173 break;
3174 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003175 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003176 ctxt->state =
3177 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3178 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003179 case XML_RELAXNG_EXCEPT:
3180 case XML_RELAXNG_ATTRIBUTE:
3181 case XML_RELAXNG_INTERLEAVE:
3182 case XML_RELAXNG_NOT_ALLOWED:
3183 case XML_RELAXNG_DATATYPE:
3184 case XML_RELAXNG_LIST:
3185 case XML_RELAXNG_PARAM:
3186 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003187 /* This should not happen and generate an internal error */
3188 fprintf(stderr, "RNG internal error trying to compile %s\n",
3189 xmlRelaxNGDefName(def));
3190 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003191 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003192 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003193}
3194
3195/**
3196 * xmlRelaxNGTryCompile:
3197 * ctxt: the RelaxNG parser context
3198 * @define: the definition tree to compile
3199 *
3200 * Try to compile the set of definitions, it works recursively,
3201 * possibly ignoring parts which cannot be compiled.
3202 *
3203 * Returns 0 if success and -1 in case of error
3204 */
3205static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003206xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3207{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003208 int ret = 0;
3209 xmlRelaxNGDefinePtr list;
3210
Daniel Veillard4c004142003-10-07 11:33:24 +00003211 if ((ctxt == NULL) || (def == NULL))
3212 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003213
3214 if ((def->type == XML_RELAXNG_START) ||
3215 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003216 ret = xmlRelaxNGIsCompileable(def);
3217 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3218 ctxt->am = NULL;
3219 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003220#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003221 if (ret == 0) {
3222 if (def->type == XML_RELAXNG_START)
3223 xmlGenericError(xmlGenericErrorContext,
3224 "compiled the start\n");
3225 else
3226 xmlGenericError(xmlGenericErrorContext,
3227 "compiled element %s\n", def->name);
3228 } else {
3229 if (def->type == XML_RELAXNG_START)
3230 xmlGenericError(xmlGenericErrorContext,
3231 "failed to compile the start\n");
3232 else
3233 xmlGenericError(xmlGenericErrorContext,
3234 "failed to compile element %s\n",
3235 def->name);
3236 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003237#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003238 return (ret);
3239 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003240 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003241 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003242 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003243 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3244 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003245 case XML_RELAXNG_TEXT:
3246 case XML_RELAXNG_DATATYPE:
3247 case XML_RELAXNG_LIST:
3248 case XML_RELAXNG_PARAM:
3249 case XML_RELAXNG_VALUE:
3250 case XML_RELAXNG_EMPTY:
3251 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003252 ret = 0;
3253 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003254 case XML_RELAXNG_OPTIONAL:
3255 case XML_RELAXNG_ZEROORMORE:
3256 case XML_RELAXNG_ONEORMORE:
3257 case XML_RELAXNG_CHOICE:
3258 case XML_RELAXNG_GROUP:
3259 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003260 case XML_RELAXNG_START:
3261 case XML_RELAXNG_REF:
3262 case XML_RELAXNG_EXTERNALREF:
3263 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003264 list = def->content;
3265 while (list != NULL) {
3266 ret = xmlRelaxNGTryCompile(ctxt, list);
3267 if (ret != 0)
3268 break;
3269 list = list->next;
3270 }
3271 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003272 case XML_RELAXNG_EXCEPT:
3273 case XML_RELAXNG_ATTRIBUTE:
3274 case XML_RELAXNG_INTERLEAVE:
3275 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003276 ret = 0;
3277 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003278 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003279 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003280}
3281
3282/************************************************************************
3283 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003284 * Parsing functions *
3285 * *
3286 ************************************************************************/
3287
Daniel Veillard4c004142003-10-07 11:33:24 +00003288static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3289 ctxt, xmlNodePtr node);
3290static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3291 ctxt, xmlNodePtr node);
3292static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3293 ctxt, xmlNodePtr nodes,
3294 int group);
3295static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3296 ctxt, xmlNodePtr node);
3297static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3298 xmlNodePtr node);
3299static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3300 xmlNodePtr nodes);
3301static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3302 ctxt, xmlNodePtr node,
3303 xmlRelaxNGDefinePtr
3304 def);
3305static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3306 ctxt, xmlNodePtr nodes);
3307static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3308 xmlRelaxNGDefinePtr define,
3309 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003310
3311
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003312#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003313
3314/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003315 * xmlRelaxNGIsNullable:
3316 * @define: the definition to verify
3317 *
3318 * Check if a definition is nullable.
3319 *
3320 * Returns 1 if yes, 0 if no and -1 in case of error
3321 */
3322static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003323xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3324{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003325 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003326
Daniel Veillardfd573f12003-03-16 17:52:32 +00003327 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003328 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003329
Daniel Veillarde063f482003-03-21 16:53:17 +00003330 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003331 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003332 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003333 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003334 switch (define->type) {
3335 case XML_RELAXNG_EMPTY:
3336 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003337 ret = 1;
3338 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003339 case XML_RELAXNG_NOOP:
3340 case XML_RELAXNG_DEF:
3341 case XML_RELAXNG_REF:
3342 case XML_RELAXNG_EXTERNALREF:
3343 case XML_RELAXNG_PARENTREF:
3344 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003345 ret = xmlRelaxNGIsNullable(define->content);
3346 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003347 case XML_RELAXNG_EXCEPT:
3348 case XML_RELAXNG_NOT_ALLOWED:
3349 case XML_RELAXNG_ELEMENT:
3350 case XML_RELAXNG_DATATYPE:
3351 case XML_RELAXNG_PARAM:
3352 case XML_RELAXNG_VALUE:
3353 case XML_RELAXNG_LIST:
3354 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003355 ret = 0;
3356 break;
3357 case XML_RELAXNG_CHOICE:{
3358 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003359
Daniel Veillard4c004142003-10-07 11:33:24 +00003360 while (list != NULL) {
3361 ret = xmlRelaxNGIsNullable(list);
3362 if (ret != 0)
3363 goto done;
3364 list = list->next;
3365 }
3366 ret = 0;
3367 break;
3368 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003369 case XML_RELAXNG_START:
3370 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003371 case XML_RELAXNG_GROUP:{
3372 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003373
Daniel Veillard4c004142003-10-07 11:33:24 +00003374 while (list != NULL) {
3375 ret = xmlRelaxNGIsNullable(list);
3376 if (ret != 1)
3377 goto done;
3378 list = list->next;
3379 }
3380 return (1);
3381 }
3382 default:
3383 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003384 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003385 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003386 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003387 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003388 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003389 define->dflags |= IS_NULLABLE;
3390 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003391}
3392
3393/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003394 * xmlRelaxNGIsBlank:
3395 * @str: a string
3396 *
3397 * Check if a string is ignorable c.f. 4.2. Whitespace
3398 *
3399 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3400 */
3401static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003402xmlRelaxNGIsBlank(xmlChar * str)
3403{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003404 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003405 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003406 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003407 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003408 return (0);
3409 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003410 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003411 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003412}
3413
Daniel Veillard6eadf632003-01-23 18:29:16 +00003414/**
3415 * xmlRelaxNGGetDataTypeLibrary:
3416 * @ctxt: a Relax-NG parser context
3417 * @node: the current data or value element
3418 *
3419 * Applies algorithm from 4.3. datatypeLibrary attribute
3420 *
3421 * Returns the datatypeLibary value or NULL if not found
3422 */
3423static xmlChar *
3424xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003425 xmlNodePtr node)
3426{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003427 xmlChar *ret, *escape;
3428
Daniel Veillard6eadf632003-01-23 18:29:16 +00003429 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003430 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3431 if (ret != NULL) {
3432 if (ret[0] == 0) {
3433 xmlFree(ret);
3434 return (NULL);
3435 }
3436 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3437 if (escape == NULL) {
3438 return (ret);
3439 }
3440 xmlFree(ret);
3441 return (escape);
3442 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003443 }
3444 node = node->parent;
3445 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003446 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3447 if (ret != NULL) {
3448 if (ret[0] == 0) {
3449 xmlFree(ret);
3450 return (NULL);
3451 }
3452 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3453 if (escape == NULL) {
3454 return (ret);
3455 }
3456 xmlFree(ret);
3457 return (escape);
3458 }
3459 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003460 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003461 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003462}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003463
3464/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003465 * xmlRelaxNGParseValue:
3466 * @ctxt: a Relax-NG parser context
3467 * @node: the data node.
3468 *
3469 * parse the content of a RelaxNG value node.
3470 *
3471 * Returns the definition pointer or NULL in case of error
3472 */
3473static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003474xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3475{
Daniel Veillardedc91922003-01-26 00:52:04 +00003476 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003477 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003478 xmlChar *type;
3479 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003480 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003481
Daniel Veillardfd573f12003-03-16 17:52:32 +00003482 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003483 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003484 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003485 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003486
3487 type = xmlGetProp(node, BAD_CAST "type");
3488 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003489 xmlRelaxNGNormExtSpace(type);
3490 if (xmlValidateNCName(type, 0)) {
3491 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3492 "value type '%s' is not an NCName\n", type, NULL);
3493 }
3494 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3495 if (library == NULL)
3496 library =
3497 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003498
Daniel Veillard4c004142003-10-07 11:33:24 +00003499 def->name = type;
3500 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003501
Daniel Veillard4c004142003-10-07 11:33:24 +00003502 lib = (xmlRelaxNGTypeLibraryPtr)
3503 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3504 if (lib == NULL) {
3505 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3506 "Use of unregistered type library '%s'\n", library,
3507 NULL);
3508 def->data = NULL;
3509 } else {
3510 def->data = lib;
3511 if (lib->have == NULL) {
3512 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3513 "Internal error with type library '%s': no 'have'\n",
3514 library, NULL);
3515 } else {
3516 success = lib->have(lib->data, def->name);
3517 if (success != 1) {
3518 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3519 "Error type '%s' is not exported by type library '%s'\n",
3520 def->name, library);
3521 }
3522 }
3523 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003524 }
3525 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003526 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003527 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003528 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3529 (node->children->next != NULL)) {
3530 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3531 "Expecting a single text value for <value>content\n",
3532 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003533 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003534 def->value = xmlNodeGetContent(node);
3535 if (def->value == NULL) {
3536 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3537 "Element <value> has no content\n", NULL, NULL);
3538 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3539 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003540
Daniel Veillard4c004142003-10-07 11:33:24 +00003541 success =
3542 lib->check(lib->data, def->name, def->value, &val, node);
3543 if (success != 1) {
3544 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3545 "Value '%s' is not acceptable for type '%s'\n",
3546 def->value, def->name);
3547 } else {
3548 if (val != NULL)
3549 def->attrs = val;
3550 }
3551 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003552 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003553 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003554}
3555
3556/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003557 * xmlRelaxNGParseData:
3558 * @ctxt: a Relax-NG parser context
3559 * @node: the data node.
3560 *
3561 * parse the content of a RelaxNG data node.
3562 *
3563 * Returns the definition pointer or NULL in case of error
3564 */
3565static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003566xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3567{
Daniel Veillard416589a2003-02-17 17:25:42 +00003568 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003569 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003570 xmlRelaxNGTypeLibraryPtr lib;
3571 xmlChar *type;
3572 xmlChar *library;
3573 xmlNodePtr content;
3574 int tmp;
3575
3576 type = xmlGetProp(node, BAD_CAST "type");
3577 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003578 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3579 NULL);
3580 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003581 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003582 xmlRelaxNGNormExtSpace(type);
3583 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003584 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3585 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003586 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003587 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3588 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003589 library =
3590 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003591
Daniel Veillardfd573f12003-03-16 17:52:32 +00003592 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003593 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003594 xmlFree(type);
3595 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003596 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003597 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003598 def->name = type;
3599 def->ns = library;
3600
3601 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003602 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003603 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003604 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3605 "Use of unregistered type library '%s'\n", library,
3606 NULL);
3607 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003608 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003609 def->data = lib;
3610 if (lib->have == NULL) {
3611 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3612 "Internal error with type library '%s': no 'have'\n",
3613 library, NULL);
3614 } else {
3615 tmp = lib->have(lib->data, def->name);
3616 if (tmp != 1) {
3617 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3618 "Error type '%s' is not exported by type library '%s'\n",
3619 def->name, library);
3620 } else
3621 if ((xmlStrEqual
3622 (library,
3623 BAD_CAST
3624 "http://www.w3.org/2001/XMLSchema-datatypes"))
3625 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3626 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3627 ctxt->idref = 1;
3628 }
3629 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003630 }
3631 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003632
3633 /*
3634 * Handle optional params
3635 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003636 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003637 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3638 break;
3639 if (xmlStrEqual(library,
3640 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3641 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3642 "Type library '%s' does not allow type parameters\n",
3643 library, NULL);
3644 content = content->next;
3645 while ((content != NULL) &&
3646 (xmlStrEqual(content->name, BAD_CAST "param")))
3647 content = content->next;
3648 } else {
3649 param = xmlRelaxNGNewDefine(ctxt, node);
3650 if (param != NULL) {
3651 param->type = XML_RELAXNG_PARAM;
3652 param->name = xmlGetProp(content, BAD_CAST "name");
3653 if (param->name == NULL) {
3654 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3655 "param has no name\n", NULL, NULL);
3656 }
3657 param->value = xmlNodeGetContent(content);
3658 if (lastparam == NULL) {
3659 def->attrs = lastparam = param;
3660 } else {
3661 lastparam->next = param;
3662 lastparam = param;
3663 }
3664 if (lib != NULL) {
3665 }
3666 }
3667 content = content->next;
3668 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003669 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003670 /*
3671 * Handle optional except
3672 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003673 if ((content != NULL)
3674 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3675 xmlNodePtr child;
3676 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003677
Daniel Veillard4c004142003-10-07 11:33:24 +00003678 except = xmlRelaxNGNewDefine(ctxt, node);
3679 if (except == NULL) {
3680 return (def);
3681 }
3682 except->type = XML_RELAXNG_EXCEPT;
3683 child = content->children;
3684 if (last == NULL) {
3685 def->content = except;
3686 } else {
3687 last->next = except;
3688 }
3689 if (child == NULL) {
3690 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3691 "except has no content\n", NULL, NULL);
3692 }
3693 while (child != NULL) {
3694 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3695 if (tmp2 != NULL) {
3696 if (last2 == NULL) {
3697 except->content = last2 = tmp2;
3698 } else {
3699 last2->next = tmp2;
3700 last2 = tmp2;
3701 }
3702 }
3703 child = child->next;
3704 }
3705 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003706 }
3707 /*
3708 * Check there is no unhandled data
3709 */
3710 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003711 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3712 "Element data has unexpected content %s\n",
3713 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003714 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003715
Daniel Veillard4c004142003-10-07 11:33:24 +00003716 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003717}
3718
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003719static const xmlChar *invalidName = BAD_CAST "\1";
3720
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003721/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003722 * xmlRelaxNGCompareNameClasses:
3723 * @defs1: the first element/attribute defs
3724 * @defs2: the second element/attribute defs
3725 * @name: the restriction on the name
3726 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003727 *
3728 * Compare the 2 lists of element definitions. The comparison is
3729 * that if both lists do not accept the same QNames, it returns 1
3730 * If the 2 lists can accept the same QName the comparison returns 0
3731 *
3732 * Returns 1 disttinct, 0 if equal
3733 */
3734static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003735xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003736 xmlRelaxNGDefinePtr def2)
3737{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003738 int ret = 1;
3739 xmlNode node;
3740 xmlNs ns;
3741 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003742
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003743 ctxt.flags = FLAGS_IGNORABLE;
3744
Daniel Veillard42f12e92003-03-07 18:32:59 +00003745 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3746
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003747 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003748 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3749 if (def2->type == XML_RELAXNG_TEXT)
3750 return (1);
3751 if (def1->name != NULL) {
3752 node.name = def1->name;
3753 } else {
3754 node.name = invalidName;
3755 }
3756 node.ns = &ns;
3757 if (def1->ns != NULL) {
3758 if (def1->ns[0] == 0) {
3759 node.ns = NULL;
3760 } else {
3761 ns.href = def1->ns;
3762 }
3763 } else {
3764 ns.href = invalidName;
3765 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003766 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003767 if (def1->nameClass != NULL) {
3768 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3769 } else {
3770 ret = 0;
3771 }
3772 } else {
3773 ret = 1;
3774 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003775 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003776 if (def2->type == XML_RELAXNG_TEXT)
3777 return (0);
3778 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003779 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003780 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003781 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003782 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003783 }
3784 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003785 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003786 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003787 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3788 if (def2->name != NULL) {
3789 node.name = def2->name;
3790 } else {
3791 node.name = invalidName;
3792 }
3793 node.ns = &ns;
3794 if (def2->ns != NULL) {
3795 if (def2->ns[0] == 0) {
3796 node.ns = NULL;
3797 } else {
3798 ns.href = def2->ns;
3799 }
3800 } else {
3801 ns.href = invalidName;
3802 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003803 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003804 if (def2->nameClass != NULL) {
3805 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3806 } else {
3807 ret = 0;
3808 }
3809 } else {
3810 ret = 1;
3811 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003812 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003813 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003814 }
3815
Daniel Veillard4c004142003-10-07 11:33:24 +00003816 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003817}
3818
3819/**
3820 * xmlRelaxNGCompareElemDefLists:
3821 * @ctxt: a Relax-NG parser context
3822 * @defs1: the first list of element/attribute defs
3823 * @defs2: the second list of element/attribute defs
3824 *
3825 * Compare the 2 lists of element or attribute definitions. The comparison
3826 * is that if both lists do not accept the same QNames, it returns 1
3827 * If the 2 lists can accept the same QName the comparison returns 0
3828 *
3829 * Returns 1 disttinct, 0 if equal
3830 */
3831static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003832xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3833 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3834 xmlRelaxNGDefinePtr * def2)
3835{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003836 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003837
Daniel Veillard154877e2003-01-30 12:17:05 +00003838 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003839 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003840 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003841 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003842 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003843 while ((*def2) != NULL) {
3844 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3845 return (0);
3846 def2++;
3847 }
3848 def2 = basedef2;
3849 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003850 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003851 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003852}
3853
3854/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003855 * xmlRelaxNGGenerateAttributes:
3856 * @ctxt: a Relax-NG parser context
3857 * @def: the definition definition
3858 *
3859 * Check if the definition can only generate attributes
3860 *
3861 * Returns 1 if yes, 0 if no and -1 in case of error.
3862 */
3863static int
3864xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003865 xmlRelaxNGDefinePtr def)
3866{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003867 xmlRelaxNGDefinePtr parent, cur, tmp;
3868
3869 /*
3870 * Don't run that check in case of error. Infinite recursion
3871 * becomes possible.
3872 */
3873 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003874 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003875
3876 parent = NULL;
3877 cur = def;
3878 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003879 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3880 (cur->type == XML_RELAXNG_TEXT) ||
3881 (cur->type == XML_RELAXNG_DATATYPE) ||
3882 (cur->type == XML_RELAXNG_PARAM) ||
3883 (cur->type == XML_RELAXNG_LIST) ||
3884 (cur->type == XML_RELAXNG_VALUE) ||
3885 (cur->type == XML_RELAXNG_EMPTY))
3886 return (0);
3887 if ((cur->type == XML_RELAXNG_CHOICE) ||
3888 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3889 (cur->type == XML_RELAXNG_GROUP) ||
3890 (cur->type == XML_RELAXNG_ONEORMORE) ||
3891 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3892 (cur->type == XML_RELAXNG_OPTIONAL) ||
3893 (cur->type == XML_RELAXNG_PARENTREF) ||
3894 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3895 (cur->type == XML_RELAXNG_REF) ||
3896 (cur->type == XML_RELAXNG_DEF)) {
3897 if (cur->content != NULL) {
3898 parent = cur;
3899 cur = cur->content;
3900 tmp = cur;
3901 while (tmp != NULL) {
3902 tmp->parent = parent;
3903 tmp = tmp->next;
3904 }
3905 continue;
3906 }
3907 }
3908 if (cur == def)
3909 break;
3910 if (cur->next != NULL) {
3911 cur = cur->next;
3912 continue;
3913 }
3914 do {
3915 cur = cur->parent;
3916 if (cur == NULL)
3917 break;
3918 if (cur == def)
3919 return (1);
3920 if (cur->next != NULL) {
3921 cur = cur->next;
3922 break;
3923 }
3924 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003925 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003926 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003927}
Daniel Veillard4c004142003-10-07 11:33:24 +00003928
Daniel Veillardce192eb2003-04-16 15:58:05 +00003929/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003930 * xmlRelaxNGGetElements:
3931 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003932 * @def: the definition definition
3933 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003934 *
3935 * Compute the list of top elements a definition can generate
3936 *
3937 * Returns a list of elements or NULL if none was found.
3938 */
3939static xmlRelaxNGDefinePtr *
3940xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003941 xmlRelaxNGDefinePtr def, int eora)
3942{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003943 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003944 int len = 0;
3945 int max = 0;
3946
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003947 /*
3948 * Don't run that check in case of error. Infinite recursion
3949 * becomes possible.
3950 */
3951 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003952 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003953
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003954 parent = NULL;
3955 cur = def;
3956 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003957 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3958 (cur->type == XML_RELAXNG_TEXT))) ||
3959 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3960 if (ret == NULL) {
3961 max = 10;
3962 ret = (xmlRelaxNGDefinePtr *)
3963 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3964 if (ret == NULL) {
3965 xmlRngPErrMemory(ctxt, "getting element list\n");
3966 return (NULL);
3967 }
3968 } else if (max <= len) {
3969 max *= 2;
3970 ret =
3971 xmlRealloc(ret,
3972 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3973 if (ret == NULL) {
3974 xmlRngPErrMemory(ctxt, "getting element list\n");
3975 return (NULL);
3976 }
3977 }
3978 ret[len++] = cur;
3979 ret[len] = NULL;
3980 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3981 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3982 (cur->type == XML_RELAXNG_GROUP) ||
3983 (cur->type == XML_RELAXNG_ONEORMORE) ||
3984 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3985 (cur->type == XML_RELAXNG_OPTIONAL) ||
3986 (cur->type == XML_RELAXNG_PARENTREF) ||
3987 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00003988 (cur->type == XML_RELAXNG_DEF) ||
3989 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003990 /*
3991 * Don't go within elements or attributes or string values.
3992 * Just gather the element top list
3993 */
3994 if (cur->content != NULL) {
3995 parent = cur;
3996 cur = cur->content;
3997 tmp = cur;
3998 while (tmp != NULL) {
3999 tmp->parent = parent;
4000 tmp = tmp->next;
4001 }
4002 continue;
4003 }
4004 }
4005 if (cur == def)
4006 break;
4007 if (cur->next != NULL) {
4008 cur = cur->next;
4009 continue;
4010 }
4011 do {
4012 cur = cur->parent;
4013 if (cur == NULL)
4014 break;
4015 if (cur == def)
4016 return (ret);
4017 if (cur->next != NULL) {
4018 cur = cur->next;
4019 break;
4020 }
4021 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004022 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004023 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004024}
Daniel Veillard4c004142003-10-07 11:33:24 +00004025
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004026/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004027 * xmlRelaxNGCheckChoiceDeterminism:
4028 * @ctxt: a Relax-NG parser context
4029 * @def: the choice definition
4030 *
4031 * Also used to find indeterministic pattern in choice
4032 */
4033static void
4034xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004035 xmlRelaxNGDefinePtr def)
4036{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004037 xmlRelaxNGDefinePtr **list;
4038 xmlRelaxNGDefinePtr cur;
4039 int nbchild = 0, i, j, ret;
4040 int is_nullable = 0;
4041 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004042 xmlHashTablePtr triage = NULL;
4043 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004044
Daniel Veillard4c004142003-10-07 11:33:24 +00004045 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4046 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004047
Daniel Veillarde063f482003-03-21 16:53:17 +00004048 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004049 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004050
Daniel Veillardfd573f12003-03-16 17:52:32 +00004051 /*
4052 * Don't run that check in case of error. Infinite recursion
4053 * becomes possible.
4054 */
4055 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004056 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004057
4058 is_nullable = xmlRelaxNGIsNullable(def);
4059
4060 cur = def->content;
4061 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004062 nbchild++;
4063 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004064 }
4065
4066 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004067 sizeof(xmlRelaxNGDefinePtr
4068 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004069 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004070 xmlRngPErrMemory(ctxt, "building choice\n");
4071 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004072 }
4073 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004074 /*
4075 * a bit strong but safe
4076 */
4077 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004078 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004079 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004080 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004081 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004082 cur = def->content;
4083 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004084 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4085 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4086 is_triable = 0;
4087 } else if (is_triable == 1) {
4088 xmlRelaxNGDefinePtr *tmp;
4089 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004090
Daniel Veillard4c004142003-10-07 11:33:24 +00004091 tmp = list[i];
4092 while ((*tmp != NULL) && (is_triable == 1)) {
4093 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4094 res = xmlHashAddEntry2(triage,
4095 BAD_CAST "#text", NULL,
4096 (void *) cur);
4097 if (res != 0)
4098 is_triable = -1;
4099 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4100 ((*tmp)->name != NULL)) {
4101 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4102 res = xmlHashAddEntry2(triage,
4103 (*tmp)->name, NULL,
4104 (void *) cur);
4105 else
4106 res = xmlHashAddEntry2(triage,
4107 (*tmp)->name, (*tmp)->ns,
4108 (void *) cur);
4109 if (res != 0)
4110 is_triable = -1;
4111 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4112 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4113 res = xmlHashAddEntry2(triage,
4114 BAD_CAST "#any", NULL,
4115 (void *) cur);
4116 else
4117 res = xmlHashAddEntry2(triage,
4118 BAD_CAST "#any", (*tmp)->ns,
4119 (void *) cur);
4120 if (res != 0)
4121 is_triable = -1;
4122 } else {
4123 is_triable = -1;
4124 }
4125 tmp++;
4126 }
4127 }
4128 i++;
4129 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004130 }
4131
Daniel Veillard4c004142003-10-07 11:33:24 +00004132 for (i = 0; i < nbchild; i++) {
4133 if (list[i] == NULL)
4134 continue;
4135 for (j = 0; j < i; j++) {
4136 if (list[j] == NULL)
4137 continue;
4138 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4139 if (ret == 0) {
4140 is_indeterminist = 1;
4141 }
4142 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004143 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004144 for (i = 0; i < nbchild; i++) {
4145 if (list[i] != NULL)
4146 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004147 }
4148
4149 xmlFree(list);
4150 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004151 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004152 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004153 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004154 def->dflags |= IS_TRIABLE;
4155 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004156 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004157 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004158 }
4159 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004160}
4161
4162/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004163 * xmlRelaxNGCheckGroupAttrs:
4164 * @ctxt: a Relax-NG parser context
4165 * @def: the group definition
4166 *
4167 * Detects violations of rule 7.3
4168 */
4169static void
4170xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004171 xmlRelaxNGDefinePtr def)
4172{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004173 xmlRelaxNGDefinePtr **list;
4174 xmlRelaxNGDefinePtr cur;
4175 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004176
4177 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004178 ((def->type != XML_RELAXNG_GROUP) &&
4179 (def->type != XML_RELAXNG_ELEMENT)))
4180 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004181
Daniel Veillarde063f482003-03-21 16:53:17 +00004182 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004183 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004184
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004185 /*
4186 * Don't run that check in case of error. Infinite recursion
4187 * becomes possible.
4188 */
4189 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004190 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004191
Daniel Veillardfd573f12003-03-16 17:52:32 +00004192 cur = def->attrs;
4193 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004194 nbchild++;
4195 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004196 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004197 cur = def->content;
4198 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004199 nbchild++;
4200 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004201 }
4202
4203 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004204 sizeof(xmlRelaxNGDefinePtr
4205 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004206 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004207 xmlRngPErrMemory(ctxt, "building group\n");
4208 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004209 }
4210 i = 0;
4211 cur = def->attrs;
4212 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004213 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4214 i++;
4215 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004216 }
4217 cur = def->content;
4218 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004219 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4220 i++;
4221 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004222 }
4223
Daniel Veillard4c004142003-10-07 11:33:24 +00004224 for (i = 0; i < nbchild; i++) {
4225 if (list[i] == NULL)
4226 continue;
4227 for (j = 0; j < i; j++) {
4228 if (list[j] == NULL)
4229 continue;
4230 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4231 if (ret == 0) {
4232 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4233 "Attributes conflicts in group\n", NULL, NULL);
4234 }
4235 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004236 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004237 for (i = 0; i < nbchild; i++) {
4238 if (list[i] != NULL)
4239 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004240 }
4241
4242 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004243 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004244}
4245
4246/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004247 * xmlRelaxNGComputeInterleaves:
4248 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004249 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004250 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004251 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004252 * A lot of work for preprocessing interleave definitions
4253 * is potentially needed to get a decent execution speed at runtime
4254 * - trying to get a total order on the element nodes generated
4255 * by the interleaves, order the list of interleave definitions
4256 * following that order.
4257 * - if <text/> is used to handle mixed content, it is better to
4258 * flag this in the define and simplify the runtime checking
4259 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004260 */
4261static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004262xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004263 xmlRelaxNGParserCtxtPtr ctxt,
4264 xmlChar * name ATTRIBUTE_UNUSED)
4265{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004266 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004267
Daniel Veillardfd573f12003-03-16 17:52:32 +00004268 xmlRelaxNGPartitionPtr partitions = NULL;
4269 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4270 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004271 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004272 int nbgroups = 0;
4273 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004274 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004275 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004276
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004277 /*
4278 * Don't run that check in case of error. Infinite recursion
4279 * becomes possible.
4280 */
4281 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004282 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004283
Daniel Veillardfd573f12003-03-16 17:52:32 +00004284#ifdef DEBUG_INTERLEAVE
4285 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004286 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004287#endif
4288 cur = def->content;
4289 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004290 nbchild++;
4291 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004292 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004293
Daniel Veillardfd573f12003-03-16 17:52:32 +00004294#ifdef DEBUG_INTERLEAVE
4295 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4296#endif
4297 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004298 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004299 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004300 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004301 cur = def->content;
4302 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004303 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4304 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4305 if (groups[nbgroups] == NULL)
4306 goto error;
4307 if (cur->type == XML_RELAXNG_TEXT)
4308 is_mixed++;
4309 groups[nbgroups]->rule = cur;
4310 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4311 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4312 nbgroups++;
4313 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004314 }
4315#ifdef DEBUG_INTERLEAVE
4316 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4317#endif
4318
4319 /*
4320 * Let's check that all rules makes a partitions according to 7.4
4321 */
4322 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004323 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004324 if (partitions == NULL)
4325 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004326 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004327 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004328 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004329 for (i = 0; i < nbgroups; i++) {
4330 group = groups[i];
4331 for (j = i + 1; j < nbgroups; j++) {
4332 if (groups[j] == NULL)
4333 continue;
4334
4335 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4336 groups[j]->defs);
4337 if (ret == 0) {
4338 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4339 "Element or text conflicts in interleave\n",
4340 NULL, NULL);
4341 }
4342 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4343 groups[j]->attrs);
4344 if (ret == 0) {
4345 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4346 "Attributes conflicts in interleave\n", NULL,
4347 NULL);
4348 }
4349 }
4350 tmp = group->defs;
4351 if ((tmp != NULL) && (*tmp != NULL)) {
4352 while (*tmp != NULL) {
4353 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4354 res = xmlHashAddEntry2(partitions->triage,
4355 BAD_CAST "#text", NULL,
4356 (void *) (long) (i + 1));
4357 if (res != 0)
4358 is_determinist = -1;
4359 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4360 ((*tmp)->name != NULL)) {
4361 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4362 res = xmlHashAddEntry2(partitions->triage,
4363 (*tmp)->name, NULL,
4364 (void *) (long) (i + 1));
4365 else
4366 res = xmlHashAddEntry2(partitions->triage,
4367 (*tmp)->name, (*tmp)->ns,
4368 (void *) (long) (i + 1));
4369 if (res != 0)
4370 is_determinist = -1;
4371 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4372 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4373 res = xmlHashAddEntry2(partitions->triage,
4374 BAD_CAST "#any", NULL,
4375 (void *) (long) (i + 1));
4376 else
4377 res = xmlHashAddEntry2(partitions->triage,
4378 BAD_CAST "#any", (*tmp)->ns,
4379 (void *) (long) (i + 1));
4380 if ((*tmp)->nameClass != NULL)
4381 is_determinist = 2;
4382 if (res != 0)
4383 is_determinist = -1;
4384 } else {
4385 is_determinist = -1;
4386 }
4387 tmp++;
4388 }
4389 } else {
4390 is_determinist = 0;
4391 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004392 }
4393 partitions->groups = groups;
4394
4395 /*
4396 * and save the partition list back in the def
4397 */
4398 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004399 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004400 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004401 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004402 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004403 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004404 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004405 return;
4406
Daniel Veillard4c004142003-10-07 11:33:24 +00004407 error:
4408 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004409 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004410 for (i = 0; i < nbgroups; i++)
4411 if (groups[i] != NULL) {
4412 if (groups[i]->defs != NULL)
4413 xmlFree(groups[i]->defs);
4414 xmlFree(groups[i]);
4415 }
4416 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004417 }
4418 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004419}
4420
4421/**
4422 * xmlRelaxNGParseInterleave:
4423 * @ctxt: a Relax-NG parser context
4424 * @node: the data node.
4425 *
4426 * parse the content of a RelaxNG interleave node.
4427 *
4428 * Returns the definition pointer or NULL in case of error
4429 */
4430static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004431xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4432{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004433 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004434 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004435 xmlNodePtr child;
4436
Daniel Veillardfd573f12003-03-16 17:52:32 +00004437 def = xmlRelaxNGNewDefine(ctxt, node);
4438 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004439 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004440 }
4441 def->type = XML_RELAXNG_INTERLEAVE;
4442
4443 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004444 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004445 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004446 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004447 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004448 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004449
Daniel Veillard4c004142003-10-07 11:33:24 +00004450 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4451 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4452 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4453 "Failed to add %s to hash table\n",
4454 (const xmlChar *) name, NULL);
4455 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004456 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004457 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004458 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004459 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4460 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004461 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004462 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004463 if (IS_RELAXNG(child, "element")) {
4464 cur = xmlRelaxNGParseElement(ctxt, child);
4465 } else {
4466 cur = xmlRelaxNGParsePattern(ctxt, child);
4467 }
4468 if (cur != NULL) {
4469 cur->parent = def;
4470 if (last == NULL) {
4471 def->content = last = cur;
4472 } else {
4473 last->next = cur;
4474 last = cur;
4475 }
4476 }
4477 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004478 }
4479
Daniel Veillard4c004142003-10-07 11:33:24 +00004480 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004481}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004482
4483/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004484 * xmlRelaxNGParseInclude:
4485 * @ctxt: a Relax-NG parser context
4486 * @node: the include node
4487 *
4488 * Integrate the content of an include node in the current grammar
4489 *
4490 * Returns 0 in case of success or -1 in case of error
4491 */
4492static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004493xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4494{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004495 xmlRelaxNGIncludePtr incl;
4496 xmlNodePtr root;
4497 int ret = 0, tmp;
4498
Daniel Veillard807daf82004-02-22 22:13:27 +00004499 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004500 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004501 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4502 "Include node has no data\n", NULL, NULL);
4503 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004504 }
4505 root = xmlDocGetRootElement(incl->doc);
4506 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004507 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4508 NULL, NULL);
4509 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004510 }
4511 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004512 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4513 "Include document root is not a grammar\n", NULL, NULL);
4514 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004515 }
4516
4517 /*
4518 * Merge the definition from both the include and the internal list
4519 */
4520 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004521 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4522 if (tmp != 0)
4523 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004524 }
4525 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004526 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4527 if (tmp != 0)
4528 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004529 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004530 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004531}
4532
4533/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004534 * xmlRelaxNGParseDefine:
4535 * @ctxt: a Relax-NG parser context
4536 * @node: the define node
4537 *
4538 * parse the content of a RelaxNG define element node.
4539 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004540 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004541 */
4542static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004543xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4544{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004545 xmlChar *name;
4546 int ret = 0, tmp;
4547 xmlRelaxNGDefinePtr def;
4548 const xmlChar *olddefine;
4549
4550 name = xmlGetProp(node, BAD_CAST "name");
4551 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004552 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4553 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004554 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004555 xmlRelaxNGNormExtSpace(name);
4556 if (xmlValidateNCName(name, 0)) {
4557 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4558 "define name '%s' is not an NCName\n", name, NULL);
4559 }
4560 def = xmlRelaxNGNewDefine(ctxt, node);
4561 if (def == NULL) {
4562 xmlFree(name);
4563 return (-1);
4564 }
4565 def->type = XML_RELAXNG_DEF;
4566 def->name = name;
4567 if (node->children == NULL) {
4568 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4569 "define has no children\n", NULL, NULL);
4570 } else {
4571 olddefine = ctxt->define;
4572 ctxt->define = name;
4573 def->content =
4574 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4575 ctxt->define = olddefine;
4576 }
4577 if (ctxt->grammar->defs == NULL)
4578 ctxt->grammar->defs = xmlHashCreate(10);
4579 if (ctxt->grammar->defs == NULL) {
4580 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4581 "Could not create definition hash\n", NULL, NULL);
4582 ret = -1;
4583 } else {
4584 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4585 if (tmp < 0) {
4586 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004587
Daniel Veillard4c004142003-10-07 11:33:24 +00004588 prev = xmlHashLookup(ctxt->grammar->defs, name);
4589 if (prev == NULL) {
4590 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4591 "Internal error on define aggregation of %s\n",
4592 name, NULL);
4593 ret = -1;
4594 } else {
4595 while (prev->nextHash != NULL)
4596 prev = prev->nextHash;
4597 prev->nextHash = def;
4598 }
4599 }
4600 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004601 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004602 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004603}
4604
4605/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004606 * xmlRelaxNGProcessExternalRef:
4607 * @ctxt: the parser context
4608 * @node: the externlRef node
4609 *
4610 * Process and compile an externlRef node
4611 *
4612 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4613 */
4614static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004615xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4616{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004617 xmlRelaxNGDocumentPtr docu;
4618 xmlNodePtr root, tmp;
4619 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004620 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004621 xmlRelaxNGDefinePtr def;
4622
Daniel Veillard807daf82004-02-22 22:13:27 +00004623 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004624 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004625 def = xmlRelaxNGNewDefine(ctxt, node);
4626 if (def == NULL)
4627 return (NULL);
4628 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004629
Daniel Veillard4c004142003-10-07 11:33:24 +00004630 if (docu->content == NULL) {
4631 /*
4632 * Then do the parsing for good
4633 */
4634 root = xmlDocGetRootElement(docu->doc);
4635 if (root == NULL) {
4636 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4637 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4638 NULL);
4639 return (NULL);
4640 }
4641 /*
4642 * ns transmission rules
4643 */
4644 ns = xmlGetProp(root, BAD_CAST "ns");
4645 if (ns == NULL) {
4646 tmp = node;
4647 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4648 ns = xmlGetProp(tmp, BAD_CAST "ns");
4649 if (ns != NULL) {
4650 break;
4651 }
4652 tmp = tmp->parent;
4653 }
4654 if (ns != NULL) {
4655 xmlSetProp(root, BAD_CAST "ns", ns);
4656 newNs = 1;
4657 xmlFree(ns);
4658 }
4659 } else {
4660 xmlFree(ns);
4661 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004662
Daniel Veillard4c004142003-10-07 11:33:24 +00004663 /*
4664 * Parsing to get a precompiled schemas.
4665 */
4666 oldflags = ctxt->flags;
4667 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4668 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4669 ctxt->flags = oldflags;
4670 if ((docu->schema != NULL) &&
4671 (docu->schema->topgrammar != NULL)) {
4672 docu->content = docu->schema->topgrammar->start;
4673 }
4674
4675 /*
4676 * the externalRef may be reused in a different ns context
4677 */
4678 if (newNs == 1) {
4679 xmlUnsetProp(root, BAD_CAST "ns");
4680 }
4681 }
4682 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004683 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004684 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004685 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004686 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004687}
4688
4689/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004690 * xmlRelaxNGParsePattern:
4691 * @ctxt: a Relax-NG parser context
4692 * @node: the pattern node.
4693 *
4694 * parse the content of a RelaxNG pattern node.
4695 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004696 * Returns the definition pointer or NULL in case of error or if no
4697 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004698 */
4699static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004700xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4701{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004702 xmlRelaxNGDefinePtr def = NULL;
4703
Daniel Veillardd2298792003-02-14 16:54:11 +00004704 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004705 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004706 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004707 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004708 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004709 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004710 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004711 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004712 def = xmlRelaxNGNewDefine(ctxt, node);
4713 if (def == NULL)
4714 return (NULL);
4715 def->type = XML_RELAXNG_EMPTY;
4716 if (node->children != NULL) {
4717 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4718 "empty: had a child node\n", NULL, NULL);
4719 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004720 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004721 def = xmlRelaxNGNewDefine(ctxt, node);
4722 if (def == NULL)
4723 return (NULL);
4724 def->type = XML_RELAXNG_TEXT;
4725 if (node->children != NULL) {
4726 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4727 "text: had a child node\n", NULL, NULL);
4728 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004729 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004730 def = xmlRelaxNGNewDefine(ctxt, node);
4731 if (def == NULL)
4732 return (NULL);
4733 def->type = XML_RELAXNG_ZEROORMORE;
4734 if (node->children == NULL) {
4735 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4736 "Element %s is empty\n", node->name, NULL);
4737 } else {
4738 def->content =
4739 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4740 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004741 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004742 def = xmlRelaxNGNewDefine(ctxt, node);
4743 if (def == NULL)
4744 return (NULL);
4745 def->type = XML_RELAXNG_ONEORMORE;
4746 if (node->children == NULL) {
4747 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4748 "Element %s is empty\n", node->name, NULL);
4749 } else {
4750 def->content =
4751 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4752 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004753 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004754 def = xmlRelaxNGNewDefine(ctxt, node);
4755 if (def == NULL)
4756 return (NULL);
4757 def->type = XML_RELAXNG_OPTIONAL;
4758 if (node->children == NULL) {
4759 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4760 "Element %s is empty\n", node->name, NULL);
4761 } else {
4762 def->content =
4763 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4764 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004765 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004766 def = xmlRelaxNGNewDefine(ctxt, node);
4767 if (def == NULL)
4768 return (NULL);
4769 def->type = XML_RELAXNG_CHOICE;
4770 if (node->children == NULL) {
4771 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4772 "Element %s is empty\n", node->name, NULL);
4773 } else {
4774 def->content =
4775 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4776 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004777 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004778 def = xmlRelaxNGNewDefine(ctxt, node);
4779 if (def == NULL)
4780 return (NULL);
4781 def->type = XML_RELAXNG_GROUP;
4782 if (node->children == NULL) {
4783 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4784 "Element %s is empty\n", node->name, NULL);
4785 } else {
4786 def->content =
4787 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4788 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004789 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004790 def = xmlRelaxNGNewDefine(ctxt, node);
4791 if (def == NULL)
4792 return (NULL);
4793 def->type = XML_RELAXNG_REF;
4794 def->name = xmlGetProp(node, BAD_CAST "name");
4795 if (def->name == NULL) {
4796 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4797 NULL, NULL);
4798 } else {
4799 xmlRelaxNGNormExtSpace(def->name);
4800 if (xmlValidateNCName(def->name, 0)) {
4801 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4802 "ref name '%s' is not an NCName\n", def->name,
4803 NULL);
4804 }
4805 }
4806 if (node->children != NULL) {
4807 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4808 NULL, NULL);
4809 }
4810 if (ctxt->grammar->refs == NULL)
4811 ctxt->grammar->refs = xmlHashCreate(10);
4812 if (ctxt->grammar->refs == NULL) {
4813 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4814 "Could not create references hash\n", NULL, NULL);
4815 def = NULL;
4816 } else {
4817 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004818
Daniel Veillard4c004142003-10-07 11:33:24 +00004819 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4820 if (tmp < 0) {
4821 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004822
Daniel Veillard4c004142003-10-07 11:33:24 +00004823 prev = (xmlRelaxNGDefinePtr)
4824 xmlHashLookup(ctxt->grammar->refs, def->name);
4825 if (prev == NULL) {
4826 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004827 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4828 "Error refs definitions '%s'\n",
4829 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004830 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004831 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4832 "Error refs definitions\n",
4833 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004834 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004835 def = NULL;
4836 } else {
4837 def->nextHash = prev->nextHash;
4838 prev->nextHash = def;
4839 }
4840 }
4841 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004842 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004843 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004844 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004845 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004846 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004847 def = xmlRelaxNGNewDefine(ctxt, node);
4848 if (def == NULL)
4849 return (NULL);
4850 def->type = XML_RELAXNG_LIST;
4851 if (node->children == NULL) {
4852 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4853 "Element %s is empty\n", node->name, NULL);
4854 } else {
4855 def->content =
4856 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4857 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004858 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004859 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004860 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004861 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004862 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004863 def = xmlRelaxNGNewDefine(ctxt, node);
4864 if (def == NULL)
4865 return (NULL);
4866 def->type = XML_RELAXNG_NOT_ALLOWED;
4867 if (node->children != NULL) {
4868 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4869 "xmlRelaxNGParse: notAllowed element is not empty\n",
4870 NULL, NULL);
4871 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004872 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004873 xmlRelaxNGGrammarPtr grammar, old;
4874 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004875
Daniel Veillardc482e262003-02-26 14:48:48 +00004876#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004877 xmlGenericError(xmlGenericErrorContext,
4878 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004879#endif
4880
Daniel Veillard4c004142003-10-07 11:33:24 +00004881 oldparent = ctxt->parentgrammar;
4882 old = ctxt->grammar;
4883 ctxt->parentgrammar = old;
4884 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4885 if (old != NULL) {
4886 ctxt->grammar = old;
4887 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004888#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004889 if (grammar != NULL) {
4890 grammar->next = old->next;
4891 old->next = grammar;
4892 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004893#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004894 }
4895 if (grammar != NULL)
4896 def = grammar->start;
4897 else
4898 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004899 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004900 if (ctxt->parentgrammar == NULL) {
4901 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4902 "Use of parentRef without a parent grammar\n", NULL,
4903 NULL);
4904 return (NULL);
4905 }
4906 def = xmlRelaxNGNewDefine(ctxt, node);
4907 if (def == NULL)
4908 return (NULL);
4909 def->type = XML_RELAXNG_PARENTREF;
4910 def->name = xmlGetProp(node, BAD_CAST "name");
4911 if (def->name == NULL) {
4912 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4913 "parentRef has no name\n", NULL, NULL);
4914 } else {
4915 xmlRelaxNGNormExtSpace(def->name);
4916 if (xmlValidateNCName(def->name, 0)) {
4917 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4918 "parentRef name '%s' is not an NCName\n",
4919 def->name, NULL);
4920 }
4921 }
4922 if (node->children != NULL) {
4923 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4924 "parentRef is not empty\n", NULL, NULL);
4925 }
4926 if (ctxt->parentgrammar->refs == NULL)
4927 ctxt->parentgrammar->refs = xmlHashCreate(10);
4928 if (ctxt->parentgrammar->refs == NULL) {
4929 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4930 "Could not create references hash\n", NULL, NULL);
4931 def = NULL;
4932 } else if (def->name != NULL) {
4933 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00004934
Daniel Veillard4c004142003-10-07 11:33:24 +00004935 tmp =
4936 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4937 if (tmp < 0) {
4938 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00004939
Daniel Veillard4c004142003-10-07 11:33:24 +00004940 prev = (xmlRelaxNGDefinePtr)
4941 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4942 if (prev == NULL) {
4943 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4944 "Internal error parentRef definitions '%s'\n",
4945 def->name, NULL);
4946 def = NULL;
4947 } else {
4948 def->nextHash = prev->nextHash;
4949 prev->nextHash = def;
4950 }
4951 }
4952 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004953 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004954 if (node->children == NULL) {
4955 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4956 NULL, NULL);
4957 def = NULL;
4958 } else {
4959 def = xmlRelaxNGParseInterleave(ctxt, node);
4960 if (def != NULL) {
4961 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004962
Daniel Veillard4c004142003-10-07 11:33:24 +00004963 if ((def->content != NULL) && (def->content->next != NULL)) {
4964 tmp = xmlRelaxNGNewDefine(ctxt, node);
4965 if (tmp != NULL) {
4966 tmp->type = XML_RELAXNG_GROUP;
4967 tmp->content = def->content;
4968 def->content = tmp;
4969 }
4970 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004971
Daniel Veillard4c004142003-10-07 11:33:24 +00004972 tmp = xmlRelaxNGNewDefine(ctxt, node);
4973 if (tmp == NULL)
4974 return (def);
4975 tmp->type = XML_RELAXNG_TEXT;
4976 tmp->next = def->content;
4977 def->content = tmp;
4978 }
4979 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004980 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004981 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4982 "Unexpected node %s is not a pattern\n", node->name,
4983 NULL);
4984 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004985 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004986 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004987}
4988
4989/**
4990 * xmlRelaxNGParseAttribute:
4991 * @ctxt: a Relax-NG parser context
4992 * @node: the element node
4993 *
4994 * parse the content of a RelaxNG attribute node.
4995 *
4996 * Returns the definition pointer or NULL in case of error.
4997 */
4998static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004999xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5000{
Daniel Veillardd2298792003-02-14 16:54:11 +00005001 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005002 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005003 int old_flags;
5004
Daniel Veillardfd573f12003-03-16 17:52:32 +00005005 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005006 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005007 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005008 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005009 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005010 child = node->children;
5011 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005012 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5013 "xmlRelaxNGParseattribute: attribute has no children\n",
5014 NULL, NULL);
5015 return (ret);
5016 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005017 old_flags = ctxt->flags;
5018 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005019 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5020 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005021 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005022
Daniel Veillardd2298792003-02-14 16:54:11 +00005023 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005024 cur = xmlRelaxNGParsePattern(ctxt, child);
5025 if (cur != NULL) {
5026 switch (cur->type) {
5027 case XML_RELAXNG_EMPTY:
5028 case XML_RELAXNG_NOT_ALLOWED:
5029 case XML_RELAXNG_TEXT:
5030 case XML_RELAXNG_ELEMENT:
5031 case XML_RELAXNG_DATATYPE:
5032 case XML_RELAXNG_VALUE:
5033 case XML_RELAXNG_LIST:
5034 case XML_RELAXNG_REF:
5035 case XML_RELAXNG_PARENTREF:
5036 case XML_RELAXNG_EXTERNALREF:
5037 case XML_RELAXNG_DEF:
5038 case XML_RELAXNG_ONEORMORE:
5039 case XML_RELAXNG_ZEROORMORE:
5040 case XML_RELAXNG_OPTIONAL:
5041 case XML_RELAXNG_CHOICE:
5042 case XML_RELAXNG_GROUP:
5043 case XML_RELAXNG_INTERLEAVE:
5044 case XML_RELAXNG_ATTRIBUTE:
5045 ret->content = cur;
5046 cur->parent = ret;
5047 break;
5048 case XML_RELAXNG_START:
5049 case XML_RELAXNG_PARAM:
5050 case XML_RELAXNG_EXCEPT:
5051 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5052 "attribute has invalid content\n", NULL,
5053 NULL);
5054 break;
5055 case XML_RELAXNG_NOOP:
5056 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5057 "RNG Internal error, noop found in attribute\n",
5058 NULL, NULL);
5059 break;
5060 }
5061 }
5062 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005063 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005064 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005065 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5066 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005067 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005068 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005069 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005070}
5071
5072/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005073 * xmlRelaxNGParseExceptNameClass:
5074 * @ctxt: a Relax-NG parser context
5075 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005076 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005077 *
5078 * parse the content of a RelaxNG nameClass node.
5079 *
5080 * Returns the definition pointer or NULL in case of error.
5081 */
5082static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005083xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005084 xmlNodePtr node, int attr)
5085{
Daniel Veillard144fae12003-02-03 13:17:57 +00005086 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5087 xmlNodePtr child;
5088
Daniel Veillardd2298792003-02-14 16:54:11 +00005089 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005090 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5091 "Expecting an except node\n", NULL, NULL);
5092 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005093 }
5094 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005095 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5096 "exceptNameClass allows only a single except node\n",
5097 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005098 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005099 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005100 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5101 NULL, NULL);
5102 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005103 }
5104
Daniel Veillardfd573f12003-03-16 17:52:32 +00005105 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005106 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005107 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005108 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005109 child = node->children;
5110 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005111 cur = xmlRelaxNGNewDefine(ctxt, child);
5112 if (cur == NULL)
5113 break;
5114 if (attr)
5115 cur->type = XML_RELAXNG_ATTRIBUTE;
5116 else
5117 cur->type = XML_RELAXNG_ELEMENT;
5118
Daniel Veillard419a7682003-02-03 23:22:49 +00005119 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005120 if (last == NULL) {
5121 ret->content = cur;
5122 } else {
5123 last->next = cur;
5124 }
5125 last = cur;
5126 }
5127 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005128 }
5129
Daniel Veillard4c004142003-10-07 11:33:24 +00005130 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005131}
5132
5133/**
5134 * xmlRelaxNGParseNameClass:
5135 * @ctxt: a Relax-NG parser context
5136 * @node: the nameClass node
5137 * @def: the current definition
5138 *
5139 * parse the content of a RelaxNG nameClass node.
5140 *
5141 * Returns the definition pointer or NULL in case of error.
5142 */
5143static xmlRelaxNGDefinePtr
5144xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005145 xmlRelaxNGDefinePtr def)
5146{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005147 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005148 xmlChar *val;
5149
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005150 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005151 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005152 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005153 if ((def->type != XML_RELAXNG_ELEMENT) &&
5154 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5155 ret = xmlRelaxNGNewDefine(ctxt, node);
5156 if (ret == NULL)
5157 return (NULL);
5158 ret->parent = def;
5159 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5160 ret->type = XML_RELAXNG_ATTRIBUTE;
5161 else
5162 ret->type = XML_RELAXNG_ELEMENT;
5163 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005164 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005165 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005166 val = xmlNodeGetContent(node);
5167 xmlRelaxNGNormExtSpace(val);
5168 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005169 if (node->parent != NULL)
5170 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5171 "Element %s name '%s' is not an NCName\n",
5172 node->parent->name, val);
5173 else
5174 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5175 "name '%s' is not an NCName\n",
5176 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005177 }
5178 ret->name = val;
5179 val = xmlGetProp(node, BAD_CAST "ns");
5180 ret->ns = val;
5181 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5182 (val != NULL) &&
5183 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005184 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005185 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005186 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005187 }
5188 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5189 (val != NULL) &&
5190 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005191 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5192 "Attribute with QName 'xmlns' is not allowed\n",
5193 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005194 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005195 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005196 ret->name = NULL;
5197 ret->ns = NULL;
5198 if (node->children != NULL) {
5199 ret->nameClass =
5200 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5201 (def->type ==
5202 XML_RELAXNG_ATTRIBUTE));
5203 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005204 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005205 ret->name = NULL;
5206 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5207 if (ret->ns == NULL) {
5208 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5209 "nsName has no ns attribute\n", NULL, NULL);
5210 }
5211 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5212 (ret->ns != NULL) &&
5213 (xmlStrEqual
5214 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5215 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5216 "Attribute with namespace '%s' is not allowed\n",
5217 ret->ns, NULL);
5218 }
5219 if (node->children != NULL) {
5220 ret->nameClass =
5221 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5222 (def->type ==
5223 XML_RELAXNG_ATTRIBUTE));
5224 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005225 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005226 xmlNodePtr child;
5227 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005228
Daniel Veillard4c004142003-10-07 11:33:24 +00005229 ret = xmlRelaxNGNewDefine(ctxt, node);
5230 if (ret == NULL)
5231 return (NULL);
5232 ret->parent = def;
5233 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005234
Daniel Veillard4c004142003-10-07 11:33:24 +00005235 if (node->children == NULL) {
5236 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5237 "Element choice is empty\n", NULL, NULL);
5238 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005239
Daniel Veillard4c004142003-10-07 11:33:24 +00005240 child = node->children;
5241 while (child != NULL) {
5242 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5243 if (tmp != NULL) {
5244 if (last == NULL) {
5245 last = ret->nameClass = tmp;
5246 } else {
5247 last->next = tmp;
5248 last = tmp;
5249 }
5250 }
5251 child = child->next;
5252 }
5253 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005254 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005255 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5256 "expecting name, anyName, nsName or choice : got %s\n",
5257 node->name, NULL);
5258 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005259 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005260 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005261 if (def->nameClass == NULL) {
5262 def->nameClass = ret;
5263 } else {
5264 tmp = def->nameClass;
5265 while (tmp->next != NULL) {
5266 tmp = tmp->next;
5267 }
5268 tmp->next = ret;
5269 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005270 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005271 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005272}
5273
5274/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005275 * xmlRelaxNGParseElement:
5276 * @ctxt: a Relax-NG parser context
5277 * @node: the element node
5278 *
5279 * parse the content of a RelaxNG element node.
5280 *
5281 * Returns the definition pointer or NULL in case of error.
5282 */
5283static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005284xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5285{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005286 xmlRelaxNGDefinePtr ret, cur, last;
5287 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005288 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005289
Daniel Veillardfd573f12003-03-16 17:52:32 +00005290 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005291 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005292 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005293 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005294 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005295 child = node->children;
5296 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005297 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5298 "xmlRelaxNGParseElement: element has no children\n",
5299 NULL, NULL);
5300 return (ret);
5301 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005302 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5303 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005304 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005305
Daniel Veillard6eadf632003-01-23 18:29:16 +00005306 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005307 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5308 "xmlRelaxNGParseElement: element has no content\n",
5309 NULL, NULL);
5310 return (ret);
5311 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005312 olddefine = ctxt->define;
5313 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005314 last = NULL;
5315 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005316 cur = xmlRelaxNGParsePattern(ctxt, child);
5317 if (cur != NULL) {
5318 cur->parent = ret;
5319 switch (cur->type) {
5320 case XML_RELAXNG_EMPTY:
5321 case XML_RELAXNG_NOT_ALLOWED:
5322 case XML_RELAXNG_TEXT:
5323 case XML_RELAXNG_ELEMENT:
5324 case XML_RELAXNG_DATATYPE:
5325 case XML_RELAXNG_VALUE:
5326 case XML_RELAXNG_LIST:
5327 case XML_RELAXNG_REF:
5328 case XML_RELAXNG_PARENTREF:
5329 case XML_RELAXNG_EXTERNALREF:
5330 case XML_RELAXNG_DEF:
5331 case XML_RELAXNG_ZEROORMORE:
5332 case XML_RELAXNG_ONEORMORE:
5333 case XML_RELAXNG_OPTIONAL:
5334 case XML_RELAXNG_CHOICE:
5335 case XML_RELAXNG_GROUP:
5336 case XML_RELAXNG_INTERLEAVE:
5337 if (last == NULL) {
5338 ret->content = last = cur;
5339 } else {
5340 if ((last->type == XML_RELAXNG_ELEMENT) &&
5341 (ret->content == last)) {
5342 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5343 if (ret->content != NULL) {
5344 ret->content->type = XML_RELAXNG_GROUP;
5345 ret->content->content = last;
5346 } else {
5347 ret->content = last;
5348 }
5349 }
5350 last->next = cur;
5351 last = cur;
5352 }
5353 break;
5354 case XML_RELAXNG_ATTRIBUTE:
5355 cur->next = ret->attrs;
5356 ret->attrs = cur;
5357 break;
5358 case XML_RELAXNG_START:
5359 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5360 "RNG Internal error, start found in element\n",
5361 NULL, NULL);
5362 break;
5363 case XML_RELAXNG_PARAM:
5364 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5365 "RNG Internal error, param found in element\n",
5366 NULL, NULL);
5367 break;
5368 case XML_RELAXNG_EXCEPT:
5369 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5370 "RNG Internal error, except found in element\n",
5371 NULL, NULL);
5372 break;
5373 case XML_RELAXNG_NOOP:
5374 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5375 "RNG Internal error, noop found in element\n",
5376 NULL, NULL);
5377 break;
5378 }
5379 }
5380 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005381 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005382 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005383 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005384}
5385
5386/**
5387 * xmlRelaxNGParsePatterns:
5388 * @ctxt: a Relax-NG parser context
5389 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005390 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005391 *
5392 * parse the content of a RelaxNG start node.
5393 *
5394 * Returns the definition pointer or NULL in case of error.
5395 */
5396static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005397xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005398 int group)
5399{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005400 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005401
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005402 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005403 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005404 if (IS_RELAXNG(nodes, "element")) {
5405 cur = xmlRelaxNGParseElement(ctxt, nodes);
5406 if (def == NULL) {
5407 def = last = cur;
5408 } else {
5409 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5410 (def == last)) {
5411 def = xmlRelaxNGNewDefine(ctxt, nodes);
5412 def->type = XML_RELAXNG_GROUP;
5413 def->content = last;
5414 }
5415 last->next = cur;
5416 last = cur;
5417 }
5418 cur->parent = parent;
5419 } else {
5420 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5421 if (cur != NULL) {
5422 if (def == NULL) {
5423 def = last = cur;
5424 } else {
5425 last->next = cur;
5426 last = cur;
5427 }
5428 }
5429 }
5430 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005431 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005432 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005433}
5434
5435/**
5436 * xmlRelaxNGParseStart:
5437 * @ctxt: a Relax-NG parser context
5438 * @nodes: start children nodes
5439 *
5440 * parse the content of a RelaxNG start node.
5441 *
5442 * Returns 0 in case of success, -1 in case of error
5443 */
5444static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005445xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5446{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005447 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005448 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005449
Daniel Veillardd2298792003-02-14 16:54:11 +00005450 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005451 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5452 NULL, NULL);
5453 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005454 }
5455 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005456 def = xmlRelaxNGNewDefine(ctxt, nodes);
5457 if (def == NULL)
5458 return (-1);
5459 def->type = XML_RELAXNG_EMPTY;
5460 if (nodes->children != NULL) {
5461 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5462 "element empty is not empty\n", NULL, NULL);
5463 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005464 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005465 def = xmlRelaxNGNewDefine(ctxt, nodes);
5466 if (def == NULL)
5467 return (-1);
5468 def->type = XML_RELAXNG_NOT_ALLOWED;
5469 if (nodes->children != NULL) {
5470 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5471 "element notAllowed is not empty\n", NULL, NULL);
5472 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005473 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005474 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005475 }
5476 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005477 last = ctxt->grammar->start;
5478 while (last->next != NULL)
5479 last = last->next;
5480 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005481 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005482 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005483 }
5484 nodes = nodes->next;
5485 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005486 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5487 "start more than one children\n", NULL, NULL);
5488 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005489 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005490 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005491}
5492
5493/**
5494 * xmlRelaxNGParseGrammarContent:
5495 * @ctxt: a Relax-NG parser context
5496 * @nodes: grammar children nodes
5497 *
5498 * parse the content of a RelaxNG grammar node.
5499 *
5500 * Returns 0 in case of success, -1 in case of error
5501 */
5502static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005503xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5504 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005505{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005506 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005507
5508 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005509 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5510 "grammar has no children\n", NULL, NULL);
5511 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005512 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005513 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005514 if (IS_RELAXNG(nodes, "start")) {
5515 if (nodes->children == NULL) {
5516 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5517 "start has no children\n", NULL, NULL);
5518 } else {
5519 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5520 if (tmp != 0)
5521 ret = -1;
5522 }
5523 } else if (IS_RELAXNG(nodes, "define")) {
5524 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5525 if (tmp != 0)
5526 ret = -1;
5527 } else if (IS_RELAXNG(nodes, "include")) {
5528 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5529 if (tmp != 0)
5530 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005531 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005532 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5533 "grammar has unexpected child %s\n", nodes->name,
5534 NULL);
5535 ret = -1;
5536 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005537 nodes = nodes->next;
5538 }
5539 return (ret);
5540}
5541
5542/**
5543 * xmlRelaxNGCheckReference:
5544 * @ref: the ref
5545 * @ctxt: a Relax-NG parser context
5546 * @name: the name associated to the defines
5547 *
5548 * Applies the 4.17. combine attribute rule for all the define
5549 * element of a given grammar using the same name.
5550 */
5551static void
5552xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005553 xmlRelaxNGParserCtxtPtr ctxt,
5554 const xmlChar * name)
5555{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005556 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005557 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005558
5559 grammar = ctxt->grammar;
5560 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005561 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5562 "Internal error: no grammar in CheckReference %s\n",
5563 name, NULL);
5564 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005565 }
5566 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005567 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5568 "Internal error: reference has content in CheckReference %s\n",
5569 name, NULL);
5570 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005571 }
5572 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005573 def = xmlHashLookup(grammar->defs, name);
5574 if (def != NULL) {
5575 cur = ref;
5576 while (cur != NULL) {
5577 cur->content = def;
5578 cur = cur->nextHash;
5579 }
5580 } else {
5581 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5582 "Reference %s has no matching definition\n", name,
5583 NULL);
5584 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005585 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005586 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5587 "Reference %s has no matching definition\n", name,
5588 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005589 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005590}
5591
5592/**
5593 * xmlRelaxNGCheckCombine:
5594 * @define: the define(s) list
5595 * @ctxt: a Relax-NG parser context
5596 * @name: the name associated to the defines
5597 *
5598 * Applies the 4.17. combine attribute rule for all the define
5599 * element of a given grammar using the same name.
5600 */
5601static void
5602xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005603 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5604{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005605 xmlChar *combine;
5606 int choiceOrInterleave = -1;
5607 int missing = 0;
5608 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5609
5610 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005611 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005612 cur = define;
5613 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005614 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5615 if (combine != NULL) {
5616 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5617 if (choiceOrInterleave == -1)
5618 choiceOrInterleave = 1;
5619 else if (choiceOrInterleave == 0) {
5620 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5621 "Defines for %s use both 'choice' and 'interleave'\n",
5622 name, NULL);
5623 }
5624 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5625 if (choiceOrInterleave == -1)
5626 choiceOrInterleave = 0;
5627 else if (choiceOrInterleave == 1) {
5628 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5629 "Defines for %s use both 'choice' and 'interleave'\n",
5630 name, NULL);
5631 }
5632 } else {
5633 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5634 "Defines for %s use unknown combine value '%s''\n",
5635 name, combine);
5636 }
5637 xmlFree(combine);
5638 } else {
5639 if (missing == 0)
5640 missing = 1;
5641 else {
5642 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5643 "Some defines for %s needs the combine attribute\n",
5644 name, NULL);
5645 }
5646 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005647
Daniel Veillard4c004142003-10-07 11:33:24 +00005648 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005649 }
5650#ifdef DEBUG
5651 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005652 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5653 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005654#endif
5655 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005656 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005657 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005658 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005659 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005660 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005661 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005662 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005663 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005664 tmp = define;
5665 last = NULL;
5666 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005667 if (tmp->content != NULL) {
5668 if (tmp->content->next != NULL) {
5669 /*
5670 * we need first to create a wrapper.
5671 */
5672 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5673 if (tmp2 == NULL)
5674 break;
5675 tmp2->type = XML_RELAXNG_GROUP;
5676 tmp2->content = tmp->content;
5677 } else {
5678 tmp2 = tmp->content;
5679 }
5680 if (last == NULL) {
5681 cur->content = tmp2;
5682 } else {
5683 last->next = tmp2;
5684 }
5685 last = tmp2;
5686 }
5687 tmp->content = cur;
5688 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005689 }
5690 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005691 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005692 if (ctxt->interleaves == NULL)
5693 ctxt->interleaves = xmlHashCreate(10);
5694 if (ctxt->interleaves == NULL) {
5695 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5696 "Failed to create interleaves hash table\n", NULL,
5697 NULL);
5698 } else {
5699 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005700
Daniel Veillard4c004142003-10-07 11:33:24 +00005701 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5702 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5703 0) {
5704 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5705 "Failed to add %s to hash table\n",
5706 (const xmlChar *) tmpname, NULL);
5707 }
5708 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005709 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005710}
5711
5712/**
5713 * xmlRelaxNGCombineStart:
5714 * @ctxt: a Relax-NG parser context
5715 * @grammar: the grammar
5716 *
5717 * Applies the 4.17. combine rule for all the start
5718 * element of a given grammar.
5719 */
5720static void
5721xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005722 xmlRelaxNGGrammarPtr grammar)
5723{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005724 xmlRelaxNGDefinePtr starts;
5725 xmlChar *combine;
5726 int choiceOrInterleave = -1;
5727 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005728 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005729
Daniel Veillard2df2de22003-02-17 23:34:33 +00005730 starts = grammar->start;
5731 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005732 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005733 cur = starts;
5734 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005735 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5736 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5737 combine = NULL;
5738 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5739 "Internal error: start element not found\n", NULL,
5740 NULL);
5741 } else {
5742 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5743 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005744
Daniel Veillard4c004142003-10-07 11:33:24 +00005745 if (combine != NULL) {
5746 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5747 if (choiceOrInterleave == -1)
5748 choiceOrInterleave = 1;
5749 else if (choiceOrInterleave == 0) {
5750 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5751 "<start> use both 'choice' and 'interleave'\n",
5752 NULL, NULL);
5753 }
5754 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5755 if (choiceOrInterleave == -1)
5756 choiceOrInterleave = 0;
5757 else if (choiceOrInterleave == 1) {
5758 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5759 "<start> use both 'choice' and 'interleave'\n",
5760 NULL, NULL);
5761 }
5762 } else {
5763 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5764 "<start> uses unknown combine value '%s''\n",
5765 combine, NULL);
5766 }
5767 xmlFree(combine);
5768 } else {
5769 if (missing == 0)
5770 missing = 1;
5771 else {
5772 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5773 "Some <start> element miss the combine attribute\n",
5774 NULL, NULL);
5775 }
5776 }
5777
5778 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005779 }
5780#ifdef DEBUG
5781 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005782 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5783 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005784#endif
5785 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005786 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005787 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005788 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005789 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005790 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005791 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005792 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005793 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005794 cur->content = grammar->start;
5795 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005796 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005797 if (ctxt->interleaves == NULL)
5798 ctxt->interleaves = xmlHashCreate(10);
5799 if (ctxt->interleaves == NULL) {
5800 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5801 "Failed to create interleaves hash table\n", NULL,
5802 NULL);
5803 } else {
5804 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005805
Daniel Veillard4c004142003-10-07 11:33:24 +00005806 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5807 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5808 0) {
5809 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5810 "Failed to add %s to hash table\n",
5811 (const xmlChar *) tmpname, NULL);
5812 }
5813 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005814 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005815}
5816
5817/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005818 * xmlRelaxNGCheckCycles:
5819 * @ctxt: a Relax-NG parser context
5820 * @nodes: grammar children nodes
5821 * @depth: the counter
5822 *
5823 * Check for cycles.
5824 *
5825 * Returns 0 if check passed, and -1 in case of error
5826 */
5827static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005828xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5829 xmlRelaxNGDefinePtr cur, int depth)
5830{
Daniel Veillardd4310742003-02-18 21:12:46 +00005831 int ret = 0;
5832
5833 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005834 if ((cur->type == XML_RELAXNG_REF) ||
5835 (cur->type == XML_RELAXNG_PARENTREF)) {
5836 if (cur->depth == -1) {
5837 cur->depth = depth;
5838 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5839 cur->depth = -2;
5840 } else if (depth == cur->depth) {
5841 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5842 "Detected a cycle in %s references\n",
5843 cur->name, NULL);
5844 return (-1);
5845 }
5846 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5847 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5848 } else {
5849 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5850 }
5851 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005852 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005853 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005854}
5855
5856/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005857 * xmlRelaxNGTryUnlink:
5858 * @ctxt: a Relax-NG parser context
5859 * @cur: the definition to unlink
5860 * @parent: the parent definition
5861 * @prev: the previous sibling definition
5862 *
5863 * Try to unlink a definition. If not possble make it a NOOP
5864 *
5865 * Returns the new prev definition
5866 */
5867static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005868xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5869 xmlRelaxNGDefinePtr cur,
5870 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5871{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005872 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005873 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005874 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005875 if (parent != NULL) {
5876 if (parent->content == cur)
5877 parent->content = cur->next;
5878 else if (parent->attrs == cur)
5879 parent->attrs = cur->next;
5880 else if (parent->nameClass == cur)
5881 parent->nameClass = cur->next;
5882 } else {
5883 cur->type = XML_RELAXNG_NOOP;
5884 prev = cur;
5885 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005886 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005887 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005888}
5889
5890/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005891 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005892 * @ctxt: a Relax-NG parser context
5893 * @nodes: grammar children nodes
5894 *
5895 * Check for simplification of empty and notAllowed
5896 */
5897static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005898xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5899 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5900{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005901 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005902
Daniel Veillardfd573f12003-03-16 17:52:32 +00005903 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005904 if ((cur->type == XML_RELAXNG_REF) ||
5905 (cur->type == XML_RELAXNG_PARENTREF)) {
5906 if (cur->depth != -3) {
5907 cur->depth = -3;
5908 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5909 }
5910 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5911 cur->parent = parent;
5912 if ((parent != NULL) &&
5913 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5914 (parent->type == XML_RELAXNG_LIST) ||
5915 (parent->type == XML_RELAXNG_GROUP) ||
5916 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5917 (parent->type == XML_RELAXNG_ONEORMORE) ||
5918 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5919 parent->type = XML_RELAXNG_NOT_ALLOWED;
5920 break;
5921 }
5922 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5923 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5924 } else
5925 prev = cur;
5926 } else if (cur->type == XML_RELAXNG_EMPTY) {
5927 cur->parent = parent;
5928 if ((parent != NULL) &&
5929 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5930 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5931 parent->type = XML_RELAXNG_EMPTY;
5932 break;
5933 }
5934 if ((parent != NULL) &&
5935 ((parent->type == XML_RELAXNG_GROUP) ||
5936 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5937 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5938 } else
5939 prev = cur;
5940 } else {
5941 cur->parent = parent;
5942 if (cur->content != NULL)
5943 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5944 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5945 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5946 if (cur->nameClass != NULL)
5947 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5948 /*
5949 * On Elements, try to move attribute only generating rules on
5950 * the attrs rules.
5951 */
5952 if (cur->type == XML_RELAXNG_ELEMENT) {
5953 int attronly;
5954 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00005955
Daniel Veillard4c004142003-10-07 11:33:24 +00005956 while (cur->content != NULL) {
5957 attronly =
5958 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5959 if (attronly == 1) {
5960 /*
5961 * migrate cur->content to attrs
5962 */
5963 tmp = cur->content;
5964 cur->content = tmp->next;
5965 tmp->next = cur->attrs;
5966 cur->attrs = tmp;
5967 } else {
5968 /*
5969 * cur->content can generate elements or text
5970 */
5971 break;
5972 }
5973 }
5974 pre = cur->content;
5975 while ((pre != NULL) && (pre->next != NULL)) {
5976 tmp = pre->next;
5977 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5978 if (attronly == 1) {
5979 /*
5980 * migrate tmp to attrs
5981 */
5982 pre->next = tmp->next;
5983 tmp->next = cur->attrs;
5984 cur->attrs = tmp;
5985 } else {
5986 pre = tmp;
5987 }
5988 }
5989 }
5990 /*
5991 * This may result in a simplification
5992 */
5993 if ((cur->type == XML_RELAXNG_GROUP) ||
5994 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5995 if (cur->content == NULL)
5996 cur->type = XML_RELAXNG_EMPTY;
5997 else if (cur->content->next == NULL) {
5998 if ((parent == NULL) && (prev == NULL)) {
5999 cur->type = XML_RELAXNG_NOOP;
6000 } else if (prev == NULL) {
6001 parent->content = cur->content;
6002 cur->content->next = cur->next;
6003 cur = cur->content;
6004 } else {
6005 cur->content->next = cur->next;
6006 prev->next = cur->content;
6007 cur = cur->content;
6008 }
6009 }
6010 }
6011 /*
6012 * the current node may have been transformed back
6013 */
6014 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6015 (cur->content != NULL) &&
6016 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6017 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6018 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6019 if ((parent != NULL) &&
6020 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6021 (parent->type == XML_RELAXNG_LIST) ||
6022 (parent->type == XML_RELAXNG_GROUP) ||
6023 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6024 (parent->type == XML_RELAXNG_ONEORMORE) ||
6025 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6026 parent->type = XML_RELAXNG_NOT_ALLOWED;
6027 break;
6028 }
6029 if ((parent != NULL) &&
6030 (parent->type == XML_RELAXNG_CHOICE)) {
6031 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6032 } else
6033 prev = cur;
6034 } else if (cur->type == XML_RELAXNG_EMPTY) {
6035 if ((parent != NULL) &&
6036 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6037 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6038 parent->type = XML_RELAXNG_EMPTY;
6039 break;
6040 }
6041 if ((parent != NULL) &&
6042 ((parent->type == XML_RELAXNG_GROUP) ||
6043 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6044 (parent->type == XML_RELAXNG_CHOICE))) {
6045 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6046 } else
6047 prev = cur;
6048 } else {
6049 prev = cur;
6050 }
6051 }
6052 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006053 }
6054}
6055
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006056/**
6057 * xmlRelaxNGGroupContentType:
6058 * @ct1: the first content type
6059 * @ct2: the second content type
6060 *
6061 * Try to group 2 content types
6062 *
6063 * Returns the content type
6064 */
6065static xmlRelaxNGContentType
6066xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006067 xmlRelaxNGContentType ct2)
6068{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006069 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006070 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6071 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006072 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006073 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006074 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006075 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006076 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006077 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6078 return (XML_RELAXNG_CONTENT_COMPLEX);
6079 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006080}
6081
6082/**
6083 * xmlRelaxNGMaxContentType:
6084 * @ct1: the first content type
6085 * @ct2: the second content type
6086 *
6087 * Compute the max content-type
6088 *
6089 * Returns the content type
6090 */
6091static xmlRelaxNGContentType
6092xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006093 xmlRelaxNGContentType ct2)
6094{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006095 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006096 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6097 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006098 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006099 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6100 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006101 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006102 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6103 return (XML_RELAXNG_CONTENT_COMPLEX);
6104 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006105}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006106
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006107/**
6108 * xmlRelaxNGCheckRules:
6109 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006110 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006111 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006112 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006113 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006114 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006115 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006116 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006117 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006118static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006119xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6120 xmlRelaxNGDefinePtr cur, int flags,
6121 xmlRelaxNGType ptype)
6122{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006123 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006124 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006125
Daniel Veillardfd573f12003-03-16 17:52:32 +00006126 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006127 ret = XML_RELAXNG_CONTENT_EMPTY;
6128 if ((cur->type == XML_RELAXNG_REF) ||
6129 (cur->type == XML_RELAXNG_PARENTREF)) {
6130 if (flags & XML_RELAXNG_IN_LIST) {
6131 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6132 "Found forbidden pattern list//ref\n", NULL,
6133 NULL);
6134 }
6135 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6136 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6137 "Found forbidden pattern data/except//ref\n",
6138 NULL, NULL);
6139 }
6140 if (cur->depth > -4) {
6141 cur->depth = -4;
6142 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6143 flags, cur->type);
6144 cur->depth = ret - 15;
6145 } else if (cur->depth == -4) {
6146 ret = XML_RELAXNG_CONTENT_COMPLEX;
6147 } else {
6148 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6149 }
6150 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6151 /*
6152 * The 7.3 Attribute derivation rule for groups is plugged there
6153 */
6154 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6155 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6156 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6157 "Found forbidden pattern data/except//element(ref)\n",
6158 NULL, NULL);
6159 }
6160 if (flags & XML_RELAXNG_IN_LIST) {
6161 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6162 "Found forbidden pattern list//element(ref)\n",
6163 NULL, NULL);
6164 }
6165 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6166 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6167 "Found forbidden pattern attribute//element(ref)\n",
6168 NULL, NULL);
6169 }
6170 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6171 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6172 "Found forbidden pattern attribute//element(ref)\n",
6173 NULL, NULL);
6174 }
6175 /*
6176 * reset since in the simple form elements are only child
6177 * of grammar/define
6178 */
6179 nflags = 0;
6180 ret =
6181 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6182 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6183 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6184 "Element %s attributes have a content type error\n",
6185 cur->name, NULL);
6186 }
6187 ret =
6188 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6189 cur->type);
6190 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6191 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6192 "Element %s has a content type error\n",
6193 cur->name, NULL);
6194 } else {
6195 ret = XML_RELAXNG_CONTENT_COMPLEX;
6196 }
6197 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6198 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6199 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6200 "Found forbidden pattern attribute//attribute\n",
6201 NULL, NULL);
6202 }
6203 if (flags & XML_RELAXNG_IN_LIST) {
6204 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6205 "Found forbidden pattern list//attribute\n",
6206 NULL, NULL);
6207 }
6208 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6209 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6210 "Found forbidden pattern oneOrMore//group//attribute\n",
6211 NULL, NULL);
6212 }
6213 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6214 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6215 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6216 NULL, NULL);
6217 }
6218 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6219 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6220 "Found forbidden pattern data/except//attribute\n",
6221 NULL, NULL);
6222 }
6223 if (flags & XML_RELAXNG_IN_START) {
6224 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6225 "Found forbidden pattern start//attribute\n",
6226 NULL, NULL);
6227 }
6228 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6229 && (cur->name == NULL)) {
6230 if (cur->ns == NULL) {
6231 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6232 "Found anyName attribute without oneOrMore ancestor\n",
6233 NULL, NULL);
6234 } else {
6235 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6236 "Found nsName attribute without oneOrMore ancestor\n",
6237 NULL, NULL);
6238 }
6239 }
6240 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6241 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6242 ret = XML_RELAXNG_CONTENT_EMPTY;
6243 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6244 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6245 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6246 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6247 "Found forbidden pattern data/except//oneOrMore\n",
6248 NULL, NULL);
6249 }
6250 if (flags & XML_RELAXNG_IN_START) {
6251 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6252 "Found forbidden pattern start//oneOrMore\n",
6253 NULL, NULL);
6254 }
6255 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6256 ret =
6257 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6258 cur->type);
6259 ret = xmlRelaxNGGroupContentType(ret, ret);
6260 } else if (cur->type == XML_RELAXNG_LIST) {
6261 if (flags & XML_RELAXNG_IN_LIST) {
6262 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6263 "Found forbidden pattern list//list\n", NULL,
6264 NULL);
6265 }
6266 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6267 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6268 "Found forbidden pattern data/except//list\n",
6269 NULL, NULL);
6270 }
6271 if (flags & XML_RELAXNG_IN_START) {
6272 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6273 "Found forbidden pattern start//list\n", NULL,
6274 NULL);
6275 }
6276 nflags = flags | XML_RELAXNG_IN_LIST;
6277 ret =
6278 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6279 cur->type);
6280 } else if (cur->type == XML_RELAXNG_GROUP) {
6281 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6282 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6283 "Found forbidden pattern data/except//group\n",
6284 NULL, NULL);
6285 }
6286 if (flags & XML_RELAXNG_IN_START) {
6287 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6288 "Found forbidden pattern start//group\n", NULL,
6289 NULL);
6290 }
6291 if (flags & XML_RELAXNG_IN_ONEORMORE)
6292 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6293 else
6294 nflags = flags;
6295 ret =
6296 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6297 cur->type);
6298 /*
6299 * The 7.3 Attribute derivation rule for groups is plugged there
6300 */
6301 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6302 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6303 if (flags & XML_RELAXNG_IN_LIST) {
6304 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6305 "Found forbidden pattern list//interleave\n",
6306 NULL, NULL);
6307 }
6308 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6309 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6310 "Found forbidden pattern data/except//interleave\n",
6311 NULL, NULL);
6312 }
6313 if (flags & XML_RELAXNG_IN_START) {
6314 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6315 "Found forbidden pattern start//interleave\n",
6316 NULL, NULL);
6317 }
6318 if (flags & XML_RELAXNG_IN_ONEORMORE)
6319 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6320 else
6321 nflags = flags;
6322 ret =
6323 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6324 cur->type);
6325 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6326 if ((cur->parent != NULL) &&
6327 (cur->parent->type == XML_RELAXNG_DATATYPE))
6328 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6329 else
6330 nflags = flags;
6331 ret =
6332 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6333 cur->type);
6334 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6335 if (flags & XML_RELAXNG_IN_START) {
6336 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6337 "Found forbidden pattern start//data\n", NULL,
6338 NULL);
6339 }
6340 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6341 ret = XML_RELAXNG_CONTENT_SIMPLE;
6342 } else if (cur->type == XML_RELAXNG_VALUE) {
6343 if (flags & XML_RELAXNG_IN_START) {
6344 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6345 "Found forbidden pattern start//value\n", NULL,
6346 NULL);
6347 }
6348 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6349 ret = XML_RELAXNG_CONTENT_SIMPLE;
6350 } else if (cur->type == XML_RELAXNG_TEXT) {
6351 if (flags & XML_RELAXNG_IN_LIST) {
6352 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6353 "Found forbidden pattern list//text\n", NULL,
6354 NULL);
6355 }
6356 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6357 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6358 "Found forbidden pattern data/except//text\n",
6359 NULL, NULL);
6360 }
6361 if (flags & XML_RELAXNG_IN_START) {
6362 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6363 "Found forbidden pattern start//text\n", NULL,
6364 NULL);
6365 }
6366 ret = XML_RELAXNG_CONTENT_COMPLEX;
6367 } else if (cur->type == XML_RELAXNG_EMPTY) {
6368 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6369 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6370 "Found forbidden pattern data/except//empty\n",
6371 NULL, NULL);
6372 }
6373 if (flags & XML_RELAXNG_IN_START) {
6374 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6375 "Found forbidden pattern start//empty\n", NULL,
6376 NULL);
6377 }
6378 ret = XML_RELAXNG_CONTENT_EMPTY;
6379 } else if (cur->type == XML_RELAXNG_CHOICE) {
6380 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6381 ret =
6382 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6383 } else {
6384 ret =
6385 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6386 }
6387 cur = cur->next;
6388 if (ptype == XML_RELAXNG_GROUP) {
6389 val = xmlRelaxNGGroupContentType(val, ret);
6390 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6391 tmp = xmlRelaxNGGroupContentType(val, ret);
6392 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6393 tmp = xmlRelaxNGMaxContentType(val, ret);
6394 } else if (ptype == XML_RELAXNG_CHOICE) {
6395 val = xmlRelaxNGMaxContentType(val, ret);
6396 } else if (ptype == XML_RELAXNG_LIST) {
6397 val = XML_RELAXNG_CONTENT_SIMPLE;
6398 } else if (ptype == XML_RELAXNG_EXCEPT) {
6399 if (ret == XML_RELAXNG_CONTENT_ERROR)
6400 val = XML_RELAXNG_CONTENT_ERROR;
6401 else
6402 val = XML_RELAXNG_CONTENT_SIMPLE;
6403 } else {
6404 val = xmlRelaxNGGroupContentType(val, ret);
6405 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006406
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006407 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006408 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006409}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006410
6411/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006412 * xmlRelaxNGParseGrammar:
6413 * @ctxt: a Relax-NG parser context
6414 * @nodes: grammar children nodes
6415 *
6416 * parse a Relax-NG <grammar> node
6417 *
6418 * Returns the internal xmlRelaxNGGrammarPtr built or
6419 * NULL in case of error
6420 */
6421static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006422xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6423{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006424 xmlRelaxNGGrammarPtr ret, tmp, old;
6425
Daniel Veillardc482e262003-02-26 14:48:48 +00006426#ifdef DEBUG_GRAMMAR
6427 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6428#endif
6429
Daniel Veillard6eadf632003-01-23 18:29:16 +00006430 ret = xmlRelaxNGNewGrammar(ctxt);
6431 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006432 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006433
6434 /*
6435 * Link the new grammar in the tree
6436 */
6437 ret->parent = ctxt->grammar;
6438 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006439 tmp = ctxt->grammar->children;
6440 if (tmp == NULL) {
6441 ctxt->grammar->children = ret;
6442 } else {
6443 while (tmp->next != NULL)
6444 tmp = tmp->next;
6445 tmp->next = ret;
6446 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006447 }
6448
6449 old = ctxt->grammar;
6450 ctxt->grammar = ret;
6451 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6452 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006453 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006454 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6455 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006456 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006457 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6458 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006459 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006460
6461 /*
6462 * Apply 4.17 mergingd rules to defines and starts
6463 */
6464 xmlRelaxNGCombineStart(ctxt, ret);
6465 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006466 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6467 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006468 }
6469
6470 /*
6471 * link together defines and refs in this grammar
6472 */
6473 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006474 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6475 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006476 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006477
Daniel Veillard6eadf632003-01-23 18:29:16 +00006478 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006479 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006480}
6481
6482/**
6483 * xmlRelaxNGParseDocument:
6484 * @ctxt: a Relax-NG parser context
6485 * @node: the root node of the RelaxNG schema
6486 *
6487 * parse a Relax-NG definition resource and build an internal
6488 * xmlRelaxNG struture which can be used to validate instances.
6489 *
6490 * Returns the internal XML RelaxNG structure built or
6491 * NULL in case of error
6492 */
6493static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006494xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6495{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006496 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006497 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006498 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006499
6500 if ((ctxt == NULL) || (node == NULL))
6501 return (NULL);
6502
6503 schema = xmlRelaxNGNewRelaxNG(ctxt);
6504 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006505 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006506
Daniel Veillard276be4a2003-01-24 01:03:34 +00006507 olddefine = ctxt->define;
6508 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006509 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006510 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006511 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006512 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006513
Daniel Veillard4c004142003-10-07 11:33:24 +00006514 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6515 if (schema->topgrammar == NULL) {
6516 return (schema);
6517 }
6518 /*
6519 * Link the new grammar in the tree
6520 */
6521 ret->parent = ctxt->grammar;
6522 if (ctxt->grammar != NULL) {
6523 tmp = ctxt->grammar->children;
6524 if (tmp == NULL) {
6525 ctxt->grammar->children = ret;
6526 } else {
6527 while (tmp->next != NULL)
6528 tmp = tmp->next;
6529 tmp->next = ret;
6530 }
6531 }
6532 old = ctxt->grammar;
6533 ctxt->grammar = ret;
6534 xmlRelaxNGParseStart(ctxt, node);
6535 if (old != NULL)
6536 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006537 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006538 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006539 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006540 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6541 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6542 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6543 while ((schema->topgrammar->start != NULL) &&
6544 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6545 (schema->topgrammar->start->next != NULL))
6546 schema->topgrammar->start =
6547 schema->topgrammar->start->content;
6548 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6549 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6550 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006551 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006552#ifdef DEBUG
6553 if (schema == NULL)
6554 xmlGenericError(xmlGenericErrorContext,
6555 "xmlRelaxNGParseDocument() failed\n");
6556#endif
6557
6558 return (schema);
6559}
6560
6561/************************************************************************
6562 * *
6563 * Reading RelaxNGs *
6564 * *
6565 ************************************************************************/
6566
6567/**
6568 * xmlRelaxNGNewParserCtxt:
6569 * @URL: the location of the schema
6570 *
6571 * Create an XML RelaxNGs parse context for that file/resource expected
6572 * to contain an XML RelaxNGs file.
6573 *
6574 * Returns the parser context or NULL in case of error
6575 */
6576xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006577xmlRelaxNGNewParserCtxt(const char *URL)
6578{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006579 xmlRelaxNGParserCtxtPtr ret;
6580
6581 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006582 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006583
Daniel Veillard4c004142003-10-07 11:33:24 +00006584 ret =
6585 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006586 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006587 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006588 return (NULL);
6589 }
6590 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006591 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006592 ret->error = xmlGenericError;
6593 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006594 return (ret);
6595}
6596
6597/**
6598 * xmlRelaxNGNewMemParserCtxt:
6599 * @buffer: a pointer to a char array containing the schemas
6600 * @size: the size of the array
6601 *
6602 * Create an XML RelaxNGs parse context for that memory buffer expected
6603 * to contain an XML RelaxNGs file.
6604 *
6605 * Returns the parser context or NULL in case of error
6606 */
6607xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006608xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6609{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006610 xmlRelaxNGParserCtxtPtr ret;
6611
6612 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006613 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006614
Daniel Veillard4c004142003-10-07 11:33:24 +00006615 ret =
6616 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006617 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006618 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006619 return (NULL);
6620 }
6621 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6622 ret->buffer = buffer;
6623 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006624 ret->error = xmlGenericError;
6625 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006626 return (ret);
6627}
6628
6629/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006630 * xmlRelaxNGNewDocParserCtxt:
6631 * @doc: a preparsed document tree
6632 *
6633 * Create an XML RelaxNGs parser context for that document.
6634 * Note: since the process of compiling a RelaxNG schemas modifies the
6635 * document, the @doc parameter is duplicated internally.
6636 *
6637 * Returns the parser context or NULL in case of error
6638 */
6639xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006640xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6641{
Daniel Veillard33300b42003-04-17 09:09:19 +00006642 xmlRelaxNGParserCtxtPtr ret;
6643 xmlDocPtr copy;
6644
6645 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006646 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006647 copy = xmlCopyDoc(doc, 1);
6648 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006649 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006650
Daniel Veillard4c004142003-10-07 11:33:24 +00006651 ret =
6652 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006653 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006654 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006655 return (NULL);
6656 }
6657 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6658 ret->document = copy;
6659 ret->userData = xmlGenericErrorContext;
6660 return (ret);
6661}
6662
6663/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006664 * xmlRelaxNGFreeParserCtxt:
6665 * @ctxt: the schema parser context
6666 *
6667 * Free the resources associated to the schema parser context
6668 */
6669void
Daniel Veillard4c004142003-10-07 11:33:24 +00006670xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6671{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006672 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006673 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006674 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006675 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006676 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006677 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006678 if (ctxt->interleaves != NULL)
6679 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006680 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006681 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006682 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006683 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006684 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006685 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006686 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006687 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006688 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006689 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006690
Daniel Veillard4c004142003-10-07 11:33:24 +00006691 for (i = 0; i < ctxt->defNr; i++)
6692 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6693 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006694 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006695 xmlFree(ctxt);
6696}
6697
Daniel Veillard6eadf632003-01-23 18:29:16 +00006698/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006699 * xmlRelaxNGNormExtSpace:
6700 * @value: a value
6701 *
6702 * Removes the leading and ending spaces of the value
6703 * The string is modified "in situ"
6704 */
6705static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006706xmlRelaxNGNormExtSpace(xmlChar * value)
6707{
Daniel Veillardd2298792003-02-14 16:54:11 +00006708 xmlChar *start = value;
6709 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006710
Daniel Veillard4c004142003-10-07 11:33:24 +00006711 if (value == NULL)
6712 return;
6713
William M. Brack76e95df2003-10-18 16:20:14 +00006714 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006715 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006716 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006717 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006718 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006719 cur++;
6720 if (*cur == 0)
6721 return;
6722 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006723 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006724 cur++;
6725 if (*cur == 0) {
6726 *start = 0;
6727 return;
6728 }
6729 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006730 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006731 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006732 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006733 *start++ = *cur++;
6734 if (*cur == 0) {
6735 *start = 0;
6736 return;
6737 }
6738 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006739 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006740 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006741 if (*cur == 0) {
6742 *start = 0;
6743 return;
6744 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006745 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006746 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006747 }
6748}
6749
6750/**
6751 * xmlRelaxNGCheckAttributes:
6752 * @ctxt: a Relax-NG parser context
6753 * @node: a Relax-NG node
6754 *
6755 * Check all the attributes on the given node
6756 */
6757static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006758xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6759{
Daniel Veillardd2298792003-02-14 16:54:11 +00006760 xmlAttrPtr cur, next;
6761
6762 cur = node->properties;
6763 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006764 next = cur->next;
6765 if ((cur->ns == NULL) ||
6766 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6767 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6768 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6769 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6770 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6771 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6772 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6773 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6774 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6775 "Attribute %s is not allowed on %s\n",
6776 cur->name, node->name);
6777 }
6778 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6779 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6780 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6781 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6782 "Attribute %s is not allowed on %s\n",
6783 cur->name, node->name);
6784 }
6785 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6786 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6787 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6788 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6789 "Attribute %s is not allowed on %s\n",
6790 cur->name, node->name);
6791 }
6792 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6793 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6794 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6795 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6796 "Attribute %s is not allowed on %s\n",
6797 cur->name, node->name);
6798 }
6799 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6800 xmlChar *val;
6801 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006802
Daniel Veillard4c004142003-10-07 11:33:24 +00006803 val = xmlNodeListGetString(node->doc, cur->children, 1);
6804 if (val != NULL) {
6805 if (val[0] != 0) {
6806 uri = xmlParseURI((const char *) val);
6807 if (uri == NULL) {
6808 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6809 "Attribute %s contains invalid URI %s\n",
6810 cur->name, val);
6811 } else {
6812 if (uri->scheme == NULL) {
6813 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6814 "Attribute %s URI %s is not absolute\n",
6815 cur->name, val);
6816 }
6817 if (uri->fragment != NULL) {
6818 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6819 "Attribute %s URI %s has a fragment ID\n",
6820 cur->name, val);
6821 }
6822 xmlFreeURI(uri);
6823 }
6824 }
6825 xmlFree(val);
6826 }
6827 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6828 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6829 "Unknown attribute %s on %s\n", cur->name,
6830 node->name);
6831 }
6832 }
6833 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006834 }
6835}
6836
6837/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006838 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006839 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006840 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006841 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006842 * Cleanup the subtree from unwanted nodes for parsing, resolve
6843 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006844 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006845static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006846xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6847{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006848 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006849
Daniel Veillard6eadf632003-01-23 18:29:16 +00006850 delete = NULL;
6851 cur = root;
6852 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006853 if (delete != NULL) {
6854 xmlUnlinkNode(delete);
6855 xmlFreeNode(delete);
6856 delete = NULL;
6857 }
6858 if (cur->type == XML_ELEMENT_NODE) {
6859 /*
6860 * Simplification 4.1. Annotations
6861 */
6862 if ((cur->ns == NULL) ||
6863 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6864 if ((cur->parent != NULL) &&
6865 (cur->parent->type == XML_ELEMENT_NODE) &&
6866 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6867 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6868 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6869 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6870 "element %s doesn't allow foreign elements\n",
6871 cur->parent->name, NULL);
6872 }
6873 delete = cur;
6874 goto skip_children;
6875 } else {
6876 xmlRelaxNGCleanupAttributes(ctxt, cur);
6877 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6878 xmlChar *href, *ns, *base, *URL;
6879 xmlRelaxNGDocumentPtr docu;
6880 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00006881 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006882
Daniel Veillard4c004142003-10-07 11:33:24 +00006883 ns = xmlGetProp(cur, BAD_CAST "ns");
6884 if (ns == NULL) {
6885 tmp = cur->parent;
6886 while ((tmp != NULL) &&
6887 (tmp->type == XML_ELEMENT_NODE)) {
6888 ns = xmlGetProp(tmp, BAD_CAST "ns");
6889 if (ns != NULL)
6890 break;
6891 tmp = tmp->parent;
6892 }
6893 }
6894 href = xmlGetProp(cur, BAD_CAST "href");
6895 if (href == NULL) {
6896 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6897 "xmlRelaxNGParse: externalRef has no href attribute\n",
6898 NULL, NULL);
6899 delete = cur;
6900 goto skip_children;
6901 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00006902 uri = xmlParseURI((const char *) href);
6903 if (uri == NULL) {
6904 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6905 "Incorrect URI for externalRef %s\n",
6906 href, NULL);
6907 if (href != NULL)
6908 xmlFree(href);
6909 delete = cur;
6910 goto skip_children;
6911 }
6912 if (uri->fragment != NULL) {
6913 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6914 "Fragment forbidden in URI for externalRef %s\n",
6915 href, NULL);
6916 xmlFreeURI(uri);
6917 if (href != NULL)
6918 xmlFree(href);
6919 delete = cur;
6920 goto skip_children;
6921 }
6922 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00006923 base = xmlNodeGetBase(cur->doc, cur);
6924 URL = xmlBuildURI(href, base);
6925 if (URL == NULL) {
6926 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6927 "Failed to compute URL for externalRef %s\n",
6928 href, NULL);
6929 if (href != NULL)
6930 xmlFree(href);
6931 if (base != NULL)
6932 xmlFree(base);
6933 delete = cur;
6934 goto skip_children;
6935 }
6936 if (href != NULL)
6937 xmlFree(href);
6938 if (base != NULL)
6939 xmlFree(base);
6940 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6941 if (docu == NULL) {
6942 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6943 "Failed to load externalRef %s\n", URL,
6944 NULL);
6945 xmlFree(URL);
6946 delete = cur;
6947 goto skip_children;
6948 }
6949 if (ns != NULL)
6950 xmlFree(ns);
6951 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00006952 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00006953 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6954 xmlChar *href, *ns, *base, *URL;
6955 xmlRelaxNGIncludePtr incl;
6956 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006957
Daniel Veillard4c004142003-10-07 11:33:24 +00006958 href = xmlGetProp(cur, BAD_CAST "href");
6959 if (href == NULL) {
6960 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6961 "xmlRelaxNGParse: include has no href attribute\n",
6962 NULL, NULL);
6963 delete = cur;
6964 goto skip_children;
6965 }
6966 base = xmlNodeGetBase(cur->doc, cur);
6967 URL = xmlBuildURI(href, base);
6968 if (URL == NULL) {
6969 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6970 "Failed to compute URL for include %s\n",
6971 href, NULL);
6972 if (href != NULL)
6973 xmlFree(href);
6974 if (base != NULL)
6975 xmlFree(base);
6976 delete = cur;
6977 goto skip_children;
6978 }
6979 if (href != NULL)
6980 xmlFree(href);
6981 if (base != NULL)
6982 xmlFree(base);
6983 ns = xmlGetProp(cur, BAD_CAST "ns");
6984 if (ns == NULL) {
6985 tmp = cur->parent;
6986 while ((tmp != NULL) &&
6987 (tmp->type == XML_ELEMENT_NODE)) {
6988 ns = xmlGetProp(tmp, BAD_CAST "ns");
6989 if (ns != NULL)
6990 break;
6991 tmp = tmp->parent;
6992 }
6993 }
6994 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6995 if (ns != NULL)
6996 xmlFree(ns);
6997 if (incl == NULL) {
6998 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
6999 "Failed to load include %s\n", URL,
7000 NULL);
7001 xmlFree(URL);
7002 delete = cur;
7003 goto skip_children;
7004 }
7005 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007006 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007007 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7008 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7009 {
7010 xmlChar *name, *ns;
7011 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007012
Daniel Veillard4c004142003-10-07 11:33:24 +00007013 /*
7014 * Simplification 4.8. name attribute of element
7015 * and attribute elements
7016 */
7017 name = xmlGetProp(cur, BAD_CAST "name");
7018 if (name != NULL) {
7019 if (cur->children == NULL) {
7020 text =
7021 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7022 name);
7023 } else {
7024 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007025
Daniel Veillard4c004142003-10-07 11:33:24 +00007026 node = xmlNewNode(cur->ns, BAD_CAST "name");
7027 if (node != NULL) {
7028 xmlAddPrevSibling(cur->children, node);
7029 text = xmlNewText(name);
7030 xmlAddChild(node, text);
7031 text = node;
7032 }
7033 }
7034 if (text == NULL) {
7035 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7036 "Failed to create a name %s element\n",
7037 name, NULL);
7038 }
7039 xmlUnsetProp(cur, BAD_CAST "name");
7040 xmlFree(name);
7041 ns = xmlGetProp(cur, BAD_CAST "ns");
7042 if (ns != NULL) {
7043 if (text != NULL) {
7044 xmlSetProp(text, BAD_CAST "ns", ns);
7045 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7046 }
7047 xmlFree(ns);
7048 } else if (xmlStrEqual(cur->name,
7049 BAD_CAST "attribute")) {
7050 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7051 }
7052 }
7053 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7054 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7055 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7056 /*
7057 * Simplification 4.8. name attribute of element
7058 * and attribute elements
7059 */
7060 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7061 xmlNodePtr node;
7062 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007063
Daniel Veillard4c004142003-10-07 11:33:24 +00007064 node = cur->parent;
7065 while ((node != NULL) &&
7066 (node->type == XML_ELEMENT_NODE)) {
7067 ns = xmlGetProp(node, BAD_CAST "ns");
7068 if (ns != NULL) {
7069 break;
7070 }
7071 node = node->parent;
7072 }
7073 if (ns == NULL) {
7074 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7075 } else {
7076 xmlSetProp(cur, BAD_CAST "ns", ns);
7077 xmlFree(ns);
7078 }
7079 }
7080 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7081 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007082
Daniel Veillard4c004142003-10-07 11:33:24 +00007083 /*
7084 * Simplification: 4.10. QNames
7085 */
7086 name = xmlNodeGetContent(cur);
7087 if (name != NULL) {
7088 local = xmlSplitQName2(name, &prefix);
7089 if (local != NULL) {
7090 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007091
Daniel Veillard4c004142003-10-07 11:33:24 +00007092 ns = xmlSearchNs(cur->doc, cur, prefix);
7093 if (ns == NULL) {
7094 xmlRngPErr(ctxt, cur,
7095 XML_RNGP_PREFIX_UNDEFINED,
7096 "xmlRelaxNGParse: no namespace for prefix %s\n",
7097 prefix, NULL);
7098 } else {
7099 xmlSetProp(cur, BAD_CAST "ns",
7100 ns->href);
7101 xmlNodeSetContent(cur, local);
7102 }
7103 xmlFree(local);
7104 xmlFree(prefix);
7105 }
7106 xmlFree(name);
7107 }
7108 }
7109 /*
7110 * 4.16
7111 */
7112 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7113 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7114 xmlRngPErr(ctxt, cur,
7115 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7116 "Found nsName/except//nsName forbidden construct\n",
7117 NULL, NULL);
7118 }
7119 }
7120 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7121 (cur != root)) {
7122 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007123
Daniel Veillard4c004142003-10-07 11:33:24 +00007124 /*
7125 * 4.16
7126 */
7127 if ((cur->parent != NULL) &&
7128 (xmlStrEqual
7129 (cur->parent->name, BAD_CAST "anyName"))) {
7130 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7131 xmlRelaxNGCleanupTree(ctxt, cur);
7132 ctxt->flags = oldflags;
7133 goto skip_children;
7134 } else if ((cur->parent != NULL) &&
7135 (xmlStrEqual
7136 (cur->parent->name, BAD_CAST "nsName"))) {
7137 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7138 xmlRelaxNGCleanupTree(ctxt, cur);
7139 ctxt->flags = oldflags;
7140 goto skip_children;
7141 }
7142 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7143 /*
7144 * 4.16
7145 */
7146 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7147 xmlRngPErr(ctxt, cur,
7148 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7149 "Found anyName/except//anyName forbidden construct\n",
7150 NULL, NULL);
7151 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7152 xmlRngPErr(ctxt, cur,
7153 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7154 "Found nsName/except//anyName forbidden construct\n",
7155 NULL, NULL);
7156 }
7157 }
7158 /*
7159 * Thisd is not an else since "include" is transformed
7160 * into a div
7161 */
7162 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7163 xmlChar *ns;
7164 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007165
Daniel Veillard4c004142003-10-07 11:33:24 +00007166 /*
7167 * implements rule 4.11
7168 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007169
Daniel Veillard4c004142003-10-07 11:33:24 +00007170 ns = xmlGetProp(cur, BAD_CAST "ns");
7171
7172 child = cur->children;
7173 ins = cur;
7174 while (child != NULL) {
7175 if (ns != NULL) {
7176 if (!xmlHasProp(child, BAD_CAST "ns")) {
7177 xmlSetProp(child, BAD_CAST "ns", ns);
7178 }
7179 }
7180 tmp = child->next;
7181 xmlUnlinkNode(child);
7182 ins = xmlAddNextSibling(ins, child);
7183 child = tmp;
7184 }
7185 if (ns != NULL)
7186 xmlFree(ns);
7187 delete = cur;
7188 goto skip_children;
7189 }
7190 }
7191 }
7192 /*
7193 * Simplification 4.2 whitespaces
7194 */
7195 else if ((cur->type == XML_TEXT_NODE) ||
7196 (cur->type == XML_CDATA_SECTION_NODE)) {
7197 if (IS_BLANK_NODE(cur)) {
7198 if (cur->parent->type == XML_ELEMENT_NODE) {
7199 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7200 &&
7201 (!xmlStrEqual
7202 (cur->parent->name, BAD_CAST "param")))
7203 delete = cur;
7204 } else {
7205 delete = cur;
7206 goto skip_children;
7207 }
7208 }
7209 } else {
7210 delete = cur;
7211 goto skip_children;
7212 }
7213
7214 /*
7215 * Skip to next node
7216 */
7217 if (cur->children != NULL) {
7218 if ((cur->children->type != XML_ENTITY_DECL) &&
7219 (cur->children->type != XML_ENTITY_REF_NODE) &&
7220 (cur->children->type != XML_ENTITY_NODE)) {
7221 cur = cur->children;
7222 continue;
7223 }
7224 }
7225 skip_children:
7226 if (cur->next != NULL) {
7227 cur = cur->next;
7228 continue;
7229 }
7230
7231 do {
7232 cur = cur->parent;
7233 if (cur == NULL)
7234 break;
7235 if (cur == root) {
7236 cur = NULL;
7237 break;
7238 }
7239 if (cur->next != NULL) {
7240 cur = cur->next;
7241 break;
7242 }
7243 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007244 }
7245 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007246 xmlUnlinkNode(delete);
7247 xmlFreeNode(delete);
7248 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007249 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007250}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007251
Daniel Veillardc5312d72003-02-21 17:14:10 +00007252/**
7253 * xmlRelaxNGCleanupDoc:
7254 * @ctxt: a Relax-NG parser context
7255 * @doc: an xmldocPtr document pointer
7256 *
7257 * Cleanup the document from unwanted nodes for parsing, resolve
7258 * Include and externalRef lookups.
7259 *
7260 * Returns the cleaned up document or NULL in case of error
7261 */
7262static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007263xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7264{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007265 xmlNodePtr root;
7266
7267 /*
7268 * Extract the root
7269 */
7270 root = xmlDocGetRootElement(doc);
7271 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007272 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7273 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007274 return (NULL);
7275 }
7276 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007277 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007278}
7279
7280/**
7281 * xmlRelaxNGParse:
7282 * @ctxt: a Relax-NG parser context
7283 *
7284 * parse a schema definition resource and build an internal
7285 * XML Shema struture which can be used to validate instances.
7286 * *WARNING* this interface is highly subject to change
7287 *
7288 * Returns the internal XML RelaxNG structure built from the resource or
7289 * NULL in case of error
7290 */
7291xmlRelaxNGPtr
7292xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7293{
7294 xmlRelaxNGPtr ret = NULL;
7295 xmlDocPtr doc;
7296 xmlNodePtr root;
7297
7298 xmlRelaxNGInitTypes();
7299
7300 if (ctxt == NULL)
7301 return (NULL);
7302
7303 /*
7304 * First step is to parse the input document into an DOM/Infoset
7305 */
7306 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007307 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007308 if (doc == NULL) {
7309 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7310 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7311 NULL);
7312 return (NULL);
7313 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007314 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007315 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007316 if (doc == NULL) {
7317 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7318 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7319 NULL);
7320 return (NULL);
7321 }
7322 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7323 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007324 } else if (ctxt->document != NULL) {
7325 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007326 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007327 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7328 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7329 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007330 }
7331 ctxt->document = doc;
7332
7333 /*
7334 * Some preprocessing of the document content
7335 */
7336 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7337 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007338 xmlFreeDoc(ctxt->document);
7339 ctxt->document = NULL;
7340 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007341 }
7342
Daniel Veillard6eadf632003-01-23 18:29:16 +00007343 /*
7344 * Then do the parsing for good
7345 */
7346 root = xmlDocGetRootElement(doc);
7347 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007348 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7349 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7350 ctxt->URL, NULL);
7351 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007352 return (NULL);
7353 }
7354 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007355 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007356 xmlFreeDoc(doc);
7357 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007358 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007359
7360 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007361 * Check the ref/defines links
7362 */
7363 /*
7364 * try to preprocess interleaves
7365 */
7366 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007367 xmlHashScan(ctxt->interleaves,
7368 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007369 }
7370
7371 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007372 * if there was a parsing error return NULL
7373 */
7374 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007375 xmlRelaxNGFree(ret);
7376 ctxt->document = NULL;
7377 xmlFreeDoc(doc);
7378 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007379 }
7380
7381 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007382 * try to compile (parts of) the schemas
7383 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007384 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7385 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007386 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007387
Daniel Veillard4c004142003-10-07 11:33:24 +00007388 def = xmlRelaxNGNewDefine(ctxt, NULL);
7389 if (def != NULL) {
7390 def->type = XML_RELAXNG_START;
7391 def->content = ret->topgrammar->start;
7392 ret->topgrammar->start = def;
7393 }
7394 }
7395 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007396 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007397
7398 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007399 * Transfer the pointer for cleanup at the schema level.
7400 */
7401 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007402 ctxt->document = NULL;
7403 ret->documents = ctxt->documents;
7404 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007405
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007406 ret->includes = ctxt->includes;
7407 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007408 ret->defNr = ctxt->defNr;
7409 ret->defTab = ctxt->defTab;
7410 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007411 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007412 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007413
7414 return (ret);
7415}
Daniel Veillard4c004142003-10-07 11:33:24 +00007416
Daniel Veillard6eadf632003-01-23 18:29:16 +00007417/**
7418 * xmlRelaxNGSetParserErrors:
7419 * @ctxt: a Relax-NG validation context
7420 * @err: the error callback
7421 * @warn: the warning callback
7422 * @ctx: contextual data for the callbacks
7423 *
7424 * Set the callback functions used to handle errors for a validation context
7425 */
7426void
7427xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007428 xmlRelaxNGValidityErrorFunc err,
7429 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7430{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007431 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007432 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007433 ctxt->error = err;
7434 ctxt->warning = warn;
7435 ctxt->userData = ctx;
7436}
Daniel Veillard409a8142003-07-18 15:16:57 +00007437
7438/**
7439 * xmlRelaxNGGetParserErrors:
7440 * @ctxt: a Relax-NG validation context
7441 * @err: the error callback result
7442 * @warn: the warning callback result
7443 * @ctx: contextual data for the callbacks result
7444 *
7445 * Get the callback information used to handle errors for a validation context
7446 *
7447 * Returns -1 in case of failure, 0 otherwise.
7448 */
7449int
7450xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007451 xmlRelaxNGValidityErrorFunc * err,
7452 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7453{
Daniel Veillard409a8142003-07-18 15:16:57 +00007454 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007455 return (-1);
7456 if (err != NULL)
7457 *err = ctxt->error;
7458 if (warn != NULL)
7459 *warn = ctxt->warning;
7460 if (ctx != NULL)
7461 *ctx = ctxt->userData;
7462 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007463}
7464
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007465#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007466
Daniel Veillard6eadf632003-01-23 18:29:16 +00007467/************************************************************************
7468 * *
7469 * Dump back a compiled form *
7470 * *
7471 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007472static void xmlRelaxNGDumpDefine(FILE * output,
7473 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007474
7475/**
7476 * xmlRelaxNGDumpDefines:
7477 * @output: the file output
7478 * @defines: a list of define structures
7479 *
7480 * Dump a RelaxNG structure back
7481 */
7482static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007483xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7484{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007485 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007486 xmlRelaxNGDumpDefine(output, defines);
7487 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007488 }
7489}
7490
7491/**
7492 * xmlRelaxNGDumpDefine:
7493 * @output: the file output
7494 * @define: a define structure
7495 *
7496 * Dump a RelaxNG structure back
7497 */
7498static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007499xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7500{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007501 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007502 return;
7503 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007504 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007505 fprintf(output, "<empty/>\n");
7506 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007507 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007508 fprintf(output, "<notAllowed/>\n");
7509 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007510 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007511 fprintf(output, "<text/>\n");
7512 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007513 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007514 fprintf(output, "<element>\n");
7515 if (define->name != NULL) {
7516 fprintf(output, "<name");
7517 if (define->ns != NULL)
7518 fprintf(output, " ns=\"%s\"", define->ns);
7519 fprintf(output, ">%s</name>\n", define->name);
7520 }
7521 xmlRelaxNGDumpDefines(output, define->attrs);
7522 xmlRelaxNGDumpDefines(output, define->content);
7523 fprintf(output, "</element>\n");
7524 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007525 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007526 fprintf(output, "<list>\n");
7527 xmlRelaxNGDumpDefines(output, define->content);
7528 fprintf(output, "</list>\n");
7529 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007530 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007531 fprintf(output, "<oneOrMore>\n");
7532 xmlRelaxNGDumpDefines(output, define->content);
7533 fprintf(output, "</oneOrMore>\n");
7534 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007535 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007536 fprintf(output, "<zeroOrMore>\n");
7537 xmlRelaxNGDumpDefines(output, define->content);
7538 fprintf(output, "</zeroOrMore>\n");
7539 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007540 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007541 fprintf(output, "<choice>\n");
7542 xmlRelaxNGDumpDefines(output, define->content);
7543 fprintf(output, "</choice>\n");
7544 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007545 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007546 fprintf(output, "<group>\n");
7547 xmlRelaxNGDumpDefines(output, define->content);
7548 fprintf(output, "</group>\n");
7549 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007550 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007551 fprintf(output, "<interleave>\n");
7552 xmlRelaxNGDumpDefines(output, define->content);
7553 fprintf(output, "</interleave>\n");
7554 break;
7555 case XML_RELAXNG_OPTIONAL:
7556 fprintf(output, "<optional>\n");
7557 xmlRelaxNGDumpDefines(output, define->content);
7558 fprintf(output, "</optional>\n");
7559 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007560 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007561 fprintf(output, "<attribute>\n");
7562 xmlRelaxNGDumpDefines(output, define->content);
7563 fprintf(output, "</attribute>\n");
7564 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007565 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007566 fprintf(output, "<define");
7567 if (define->name != NULL)
7568 fprintf(output, " name=\"%s\"", define->name);
7569 fprintf(output, ">\n");
7570 xmlRelaxNGDumpDefines(output, define->content);
7571 fprintf(output, "</define>\n");
7572 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007573 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007574 fprintf(output, "<ref");
7575 if (define->name != NULL)
7576 fprintf(output, " name=\"%s\"", define->name);
7577 fprintf(output, ">\n");
7578 xmlRelaxNGDumpDefines(output, define->content);
7579 fprintf(output, "</ref>\n");
7580 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007581 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007582 fprintf(output, "<parentRef");
7583 if (define->name != NULL)
7584 fprintf(output, " name=\"%s\"", define->name);
7585 fprintf(output, ">\n");
7586 xmlRelaxNGDumpDefines(output, define->content);
7587 fprintf(output, "</parentRef>\n");
7588 break;
7589 case XML_RELAXNG_EXTERNALREF:
7590 fprintf(output, "<externalRef>");
7591 xmlRelaxNGDumpDefines(output, define->content);
7592 fprintf(output, "</externalRef>\n");
7593 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007594 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007595 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007596 TODO break;
7597 case XML_RELAXNG_START:
7598 case XML_RELAXNG_EXCEPT:
7599 case XML_RELAXNG_PARAM:
7600 TODO break;
7601 case XML_RELAXNG_NOOP:
7602 xmlRelaxNGDumpDefines(output, define->content);
7603 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007604 }
7605}
Daniel Veillard4c004142003-10-07 11:33:24 +00007606
Daniel Veillard6eadf632003-01-23 18:29:16 +00007607/**
7608 * xmlRelaxNGDumpGrammar:
7609 * @output: the file output
7610 * @grammar: a grammar structure
7611 * @top: is this a top grammar
7612 *
7613 * Dump a RelaxNG structure back
7614 */
7615static void
7616xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7617{
7618 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007619 return;
7620
Daniel Veillard6eadf632003-01-23 18:29:16 +00007621 fprintf(output, "<grammar");
7622 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007623 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7624 switch (grammar->combine) {
7625 case XML_RELAXNG_COMBINE_UNDEFINED:
7626 break;
7627 case XML_RELAXNG_COMBINE_CHOICE:
7628 fprintf(output, " combine=\"choice\"");
7629 break;
7630 case XML_RELAXNG_COMBINE_INTERLEAVE:
7631 fprintf(output, " combine=\"interleave\"");
7632 break;
7633 default:
7634 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007635 }
7636 fprintf(output, ">\n");
7637 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007638 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007639 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007640 fprintf(output, "<start>\n");
7641 xmlRelaxNGDumpDefine(output, grammar->start);
7642 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007643 }
7644 /* TODO ? Dump the defines ? */
7645 fprintf(output, "</grammar>\n");
7646}
7647
7648/**
7649 * xmlRelaxNGDump:
7650 * @output: the file output
7651 * @schema: a schema structure
7652 *
7653 * Dump a RelaxNG structure back
7654 */
7655void
7656xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7657{
7658 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007659 fprintf(output, "RelaxNG empty or failed to compile\n");
7660 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007661 }
7662 fprintf(output, "RelaxNG: ");
7663 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007664 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007665 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007666 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007667 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007668 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007669 }
7670 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007671 fprintf(output, "RelaxNG has no top grammar\n");
7672 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007673 }
7674 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7675}
7676
Daniel Veillardfebcca42003-02-16 15:44:18 +00007677/**
7678 * xmlRelaxNGDumpTree:
7679 * @output: the file output
7680 * @schema: a schema structure
7681 *
7682 * Dump the transformed RelaxNG tree.
7683 */
7684void
7685xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7686{
7687 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007688 fprintf(output, "RelaxNG empty or failed to compile\n");
7689 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007690 }
7691 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007692 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007693 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007694 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007695 }
7696}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007697#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007698
Daniel Veillard6eadf632003-01-23 18:29:16 +00007699/************************************************************************
7700 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007701 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007702 * *
7703 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007704static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7705 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007706
7707/**
7708 * xmlRelaxNGValidateCompiledCallback:
7709 * @exec: the regular expression instance
7710 * @token: the token which matched
7711 * @transdata: callback data, the define for the subelement if available
7712 @ @inputdata: callback data, the Relax NG validation context
7713 *
7714 * Handle the callback and if needed validate the element children.
7715 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007716static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007717xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007718 const xmlChar * token,
7719 void *transdata, void *inputdata)
7720{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007721 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7722 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7723 int ret;
7724
7725#ifdef DEBUG_COMPILE
7726 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007727 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007728#endif
7729 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007730 fprintf(stderr, "callback on %s missing context\n", token);
7731 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7732 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7733 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007734 }
7735 if (define == NULL) {
7736 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007737 return;
7738 fprintf(stderr, "callback on %s missing define\n", token);
7739 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7740 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7741 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007742 }
7743 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007744 fprintf(stderr, "callback on %s missing info\n", token);
7745 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7746 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7747 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007748 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007749 fprintf(stderr, "callback on %s define is not element\n", token);
7750 if (ctxt->errNo == XML_RELAXNG_OK)
7751 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7752 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007753 }
7754 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007755 if (ret != 0)
7756 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007757}
7758
7759/**
7760 * xmlRelaxNGValidateCompiledContent:
7761 * @ctxt: the RelaxNG validation context
7762 * @regexp: the regular expression as compiled
7763 * @content: list of children to test against the regexp
7764 *
7765 * Validate the content model of an element or start using the regexp
7766 *
7767 * Returns 0 in case of success, -1 in case of error.
7768 */
7769static int
7770xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007771 xmlRegexpPtr regexp, xmlNodePtr content)
7772{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007773 xmlRegExecCtxtPtr exec;
7774 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007775 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007776 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007777
7778 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007779 return (-1);
7780 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007781 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007782 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007783 cur = content;
7784 while (cur != NULL) {
7785 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007786 switch (cur->type) {
7787 case XML_TEXT_NODE:
7788 case XML_CDATA_SECTION_NODE:
7789 if (xmlIsBlankNode(cur))
7790 break;
7791 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7792 if (ret < 0) {
7793 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7794 cur->parent->name);
7795 }
7796 break;
7797 case XML_ELEMENT_NODE:
7798 if (cur->ns != NULL) {
7799 ret = xmlRegExecPushString2(exec, cur->name,
7800 cur->ns->href, ctxt);
7801 } else {
7802 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7803 }
7804 if (ret < 0) {
7805 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7806 }
7807 break;
7808 default:
7809 break;
7810 }
7811 if (ret < 0)
7812 break;
7813 /*
7814 * Switch to next element
7815 */
7816 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007817 }
7818 ret = xmlRegExecPushString(exec, NULL, NULL);
7819 if (ret == 1) {
7820 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007821 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007822 } else if (ret == 0) {
7823 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007824 * TODO: get some of the names needed to exit the current state of exec
7825 */
7826 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7827 ret = -1;
7828 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7829 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007830 } else {
7831 ret = -1;
7832 }
7833 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007834 /*
7835 * There might be content model errors outside of the pure
7836 * regexp validation, e.g. for attribute values.
7837 */
7838 if ((ret == 0) && (ctxt->perr != 0)) {
7839 ret = ctxt->perr;
7840 }
7841 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007842 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007843}
7844
7845/************************************************************************
7846 * *
7847 * Progressive validation of when possible *
7848 * *
7849 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007850static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7851 xmlRelaxNGDefinePtr defines);
7852static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00007853 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007854static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007855
7856/**
7857 * xmlRelaxNGElemPush:
7858 * @ctxt: the validation context
7859 * @exec: the regexp runtime for the new content model
7860 *
7861 * Push a new regexp for the current node content model on the stack
7862 *
7863 * Returns 0 in case of success and -1 in case of error.
7864 */
7865static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007866xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7867{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007868 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007869 ctxt->elemMax = 10;
7870 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7871 sizeof
7872 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007873 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007874 xmlRngVErrMemory(ctxt, "validating\n");
7875 return (-1);
7876 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007877 }
7878 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007879 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007880 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007881 ctxt->elemMax *
7882 sizeof
7883 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007884 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007885 xmlRngVErrMemory(ctxt, "validating\n");
7886 return (-1);
7887 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007888 }
7889 ctxt->elemTab[ctxt->elemNr++] = exec;
7890 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007891 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007892}
7893
7894/**
7895 * xmlRelaxNGElemPop:
7896 * @ctxt: the validation context
7897 *
7898 * Pop the regexp of the current node content model from the stack
7899 *
7900 * Returns the exec or NULL if empty
7901 */
7902static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007903xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7904{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007905 xmlRegExecCtxtPtr ret;
7906
Daniel Veillard4c004142003-10-07 11:33:24 +00007907 if (ctxt->elemNr <= 0)
7908 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007909 ctxt->elemNr--;
7910 ret = ctxt->elemTab[ctxt->elemNr];
7911 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007912 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007913 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7914 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007915 ctxt->elem = NULL;
7916 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007917}
7918
7919/**
7920 * xmlRelaxNGValidateProgressiveCallback:
7921 * @exec: the regular expression instance
7922 * @token: the token which matched
7923 * @transdata: callback data, the define for the subelement if available
7924 @ @inputdata: callback data, the Relax NG validation context
7925 *
7926 * Handle the callback and if needed validate the element children.
7927 * some of the in/out informations are passed via the context in @inputdata.
7928 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007929static void
7930xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7931 ATTRIBUTE_UNUSED,
7932 const xmlChar * token,
7933 void *transdata, void *inputdata)
7934{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007935 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7936 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007937 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007938 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007939 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007940
7941#ifdef DEBUG_PROGRESSIVE
7942 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007943 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007944#endif
7945 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007946 fprintf(stderr, "callback on %s missing context\n", token);
7947 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007948 }
7949 ctxt->pstate = 1;
7950 if (define == NULL) {
7951 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007952 return;
7953 fprintf(stderr, "callback on %s missing define\n", token);
7954 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7955 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7956 ctxt->pstate = -1;
7957 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007958 }
7959 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007960 fprintf(stderr, "callback on %s missing info\n", token);
7961 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7962 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7963 ctxt->pstate = -1;
7964 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007965 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007966 fprintf(stderr, "callback on %s define is not element\n", token);
7967 if (ctxt->errNo == XML_RELAXNG_OK)
7968 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7969 ctxt->pstate = -1;
7970 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007971 }
7972 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007973 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7974 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7975 xmlRelaxNGDumpValidError(ctxt);
7976 ctxt->pstate = -1;
7977 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007978 }
7979 if (define->contModel == NULL) {
7980 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007981 * this node cannot be validated in a streamable fashion
7982 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00007983#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00007984 xmlGenericError(xmlGenericErrorContext,
7985 "Element '%s' validation is not streamable\n",
7986 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007987#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00007988 ctxt->pstate = 0;
7989 ctxt->pdef = define;
7990 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007991 }
7992 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00007993 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007994 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007995 ctxt->pstate = -1;
7996 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007997 }
7998 xmlRelaxNGElemPush(ctxt, exec);
7999
8000 /*
8001 * Validate the attributes part of the content.
8002 */
8003 state = xmlRelaxNGNewValidState(ctxt, node);
8004 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008005 ctxt->pstate = -1;
8006 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008007 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008008 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008009 ctxt->state = state;
8010 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008011 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8012 if (ret != 0) {
8013 ctxt->pstate = -1;
8014 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8015 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008016 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008017 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008018 ctxt->state->seq = NULL;
8019 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8020 if (ret != 0) {
8021 ctxt->pstate = -1;
8022 }
8023 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008024 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008025 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008026
8027 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008028
Daniel Veillard4c004142003-10-07 11:33:24 +00008029 for (i = 0; i < ctxt->states->nbState; i++) {
8030 state = ctxt->states->tabState[i];
8031 ctxt->state = state;
8032 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008033
Daniel Veillard4c004142003-10-07 11:33:24 +00008034 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8035 tmp = 0;
8036 break;
8037 }
8038 }
8039 if (tmp != 0) {
8040 /*
8041 * validation error, log the message for the "best" one
8042 */
8043 ctxt->flags |= FLAGS_IGNORABLE;
8044 xmlRelaxNGLogBestError(ctxt);
8045 }
8046 for (i = 0; i < ctxt->states->nbState; i++) {
8047 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8048 }
8049 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8050 ctxt->states = NULL;
8051 if ((ret == 0) && (tmp == -1))
8052 ctxt->pstate = -1;
8053 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008054 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008055 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008056 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8057 xmlRelaxNGDumpValidError(ctxt);
8058 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008059 }
8060 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008061}
8062
8063/**
8064 * xmlRelaxNGValidatePushElement:
8065 * @ctxt: the validation context
8066 * @doc: a document instance
8067 * @elem: an element instance
8068 *
8069 * Push a new element start on the RelaxNG validation stack.
8070 *
8071 * returns 1 if no validation problem was found or 0 if validating the
8072 * element requires a full node, and -1 in case of error.
8073 */
8074int
Daniel Veillard33300b42003-04-17 09:09:19 +00008075xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8076 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008077 xmlNodePtr elem)
8078{
8079 int ret = 1;
8080
8081 if ((ctxt == NULL) || (elem == NULL))
8082 return (-1);
8083
8084#ifdef DEBUG_PROGRESSIVE
8085 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8086#endif
8087 if (ctxt->elem == 0) {
8088 xmlRelaxNGPtr schema;
8089 xmlRelaxNGGrammarPtr grammar;
8090 xmlRegExecCtxtPtr exec;
8091 xmlRelaxNGDefinePtr define;
8092
8093 schema = ctxt->schema;
8094 if (schema == NULL) {
8095 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8096 return (-1);
8097 }
8098 grammar = schema->topgrammar;
8099 if ((grammar == NULL) || (grammar->start == NULL)) {
8100 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8101 return (-1);
8102 }
8103 define = grammar->start;
8104 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008105 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008106 return (0);
8107 }
8108 exec = xmlRegNewExecCtxt(define->contModel,
8109 xmlRelaxNGValidateProgressiveCallback,
8110 ctxt);
8111 if (exec == NULL) {
8112 return (-1);
8113 }
8114 xmlRelaxNGElemPush(ctxt, exec);
8115 }
8116 ctxt->pnode = elem;
8117 ctxt->pstate = 0;
8118 if (elem->ns != NULL) {
8119 ret =
8120 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8121 ctxt);
8122 } else {
8123 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8124 }
8125 if (ret < 0) {
8126 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8127 } else {
8128 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008129 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008130 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008131 ret = -1;
8132 else
8133 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008134 }
8135#ifdef DEBUG_PROGRESSIVE
8136 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008137 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8138 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008139#endif
8140 return (ret);
8141}
8142
8143/**
8144 * xmlRelaxNGValidatePushCData:
8145 * @ctxt: the RelaxNG validation context
8146 * @data: some character data read
8147 * @len: the lenght of the data
8148 *
8149 * check the CData parsed for validation in the current stack
8150 *
8151 * returns 1 if no validation problem was found or -1 otherwise
8152 */
8153int
8154xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008155 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008156{
8157 int ret = 1;
8158
8159 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8160 return (-1);
8161
8162#ifdef DEBUG_PROGRESSIVE
8163 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8164#endif
8165
8166 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008167 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008168 break;
8169 data++;
8170 }
8171 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008172 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008173
8174 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8175 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008176 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008177#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008178 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008179#endif
8180
Daniel Veillard4c004142003-10-07 11:33:24 +00008181 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008182 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008183 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008184}
8185
8186/**
8187 * xmlRelaxNGValidatePopElement:
8188 * @ctxt: the RelaxNG validation context
8189 * @doc: a document instance
8190 * @elem: an element instance
8191 *
8192 * Pop the element end from the RelaxNG validation stack.
8193 *
8194 * returns 1 if no validation problem was found or 0 otherwise
8195 */
8196int
8197xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8198 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008199 xmlNodePtr elem)
8200{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008201 int ret;
8202 xmlRegExecCtxtPtr exec;
8203
Daniel Veillard4c004142003-10-07 11:33:24 +00008204 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8205 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008206#ifdef DEBUG_PROGRESSIVE
8207 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8208#endif
8209 /*
8210 * verify that we reached a terminal state of the content model.
8211 */
8212 exec = xmlRelaxNGElemPop(ctxt);
8213 ret = xmlRegExecPushString(exec, NULL, NULL);
8214 if (ret == 0) {
8215 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008216 * TODO: get some of the names needed to exit the current state of exec
8217 */
8218 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8219 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008220 } else if (ret < 0) {
8221 ret = -1;
8222 } else {
8223 ret = 1;
8224 }
8225 xmlRegFreeExecCtxt(exec);
8226#ifdef DEBUG_PROGRESSIVE
8227 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008228 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8229 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008230#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008231 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008232}
8233
8234/**
8235 * xmlRelaxNGValidateFullElement:
8236 * @ctxt: the validation context
8237 * @doc: a document instance
8238 * @elem: an element instance
8239 *
8240 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8241 * 0 and the content of the node has been expanded.
8242 *
8243 * returns 1 if no validation problem was found or -1 in case of error.
8244 */
8245int
Daniel Veillard33300b42003-04-17 09:09:19 +00008246xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8247 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008248 xmlNodePtr elem)
8249{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008250 int ret;
8251 xmlRelaxNGValidStatePtr state;
8252
Daniel Veillard4c004142003-10-07 11:33:24 +00008253 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8254 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008255#ifdef DEBUG_PROGRESSIVE
8256 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8257#endif
8258 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8259 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008260 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008261 }
8262 state->seq = elem;
8263 ctxt->state = state;
8264 ctxt->errNo = XML_RELAXNG_OK;
8265 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008266 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8267 ret = -1;
8268 else
8269 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008270 xmlRelaxNGFreeValidState(ctxt, state);
8271 ctxt->state = NULL;
8272#ifdef DEBUG_PROGRESSIVE
8273 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008274 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8275 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008276#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008277 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008278}
8279
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008280/************************************************************************
8281 * *
8282 * Generic interpreted validation implementation *
8283 * *
8284 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008285static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8286 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008287
8288/**
8289 * xmlRelaxNGSkipIgnored:
8290 * @ctxt: a schema validation context
8291 * @node: the top node.
8292 *
8293 * Skip ignorable nodes in that context
8294 *
8295 * Returns the new sibling or NULL in case of error.
8296 */
8297static xmlNodePtr
8298xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008299 xmlNodePtr node)
8300{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008301 /*
8302 * TODO complete and handle entities
8303 */
8304 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008305 ((node->type == XML_COMMENT_NODE) ||
8306 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008307 (node->type == XML_XINCLUDE_START) ||
8308 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008309 (((node->type == XML_TEXT_NODE) ||
8310 (node->type == XML_CDATA_SECTION_NODE)) &&
8311 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8312 (IS_BLANK_NODE(node)))))) {
8313 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008314 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008315 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008316}
8317
8318/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008319 * xmlRelaxNGNormalize:
8320 * @ctxt: a schema validation context
8321 * @str: the string to normalize
8322 *
8323 * Implements the normalizeWhiteSpace( s ) function from
8324 * section 6.2.9 of the spec
8325 *
8326 * Returns the new string or NULL in case of error.
8327 */
8328static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008329xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8330{
Daniel Veillardedc91922003-01-26 00:52:04 +00008331 xmlChar *ret, *p;
8332 const xmlChar *tmp;
8333 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008334
Daniel Veillardedc91922003-01-26 00:52:04 +00008335 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008336 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008337 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008338 while (*tmp != 0)
8339 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008340 len = tmp - str;
8341
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008342 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008343 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008344 xmlRngVErrMemory(ctxt, "validating\n");
8345 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008346 }
8347 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008348 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008349 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008350 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008351 if (IS_BLANK_CH(*str)) {
8352 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008353 str++;
8354 if (*str == 0)
8355 break;
8356 *p++ = ' ';
8357 } else
8358 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008359 }
8360 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008361 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008362}
8363
8364/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008365 * xmlRelaxNGValidateDatatype:
8366 * @ctxt: a Relax-NG validation context
8367 * @value: the string value
8368 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008369 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008370 *
8371 * Validate the given value against the dataype
8372 *
8373 * Returns 0 if the validation succeeded or an error code.
8374 */
8375static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008376xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8377 const xmlChar * value,
8378 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8379{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008380 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008381 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008382 void *result = NULL;
8383 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008384
8385 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008386 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008387 }
8388 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008389 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008390 if ((define->attrs != NULL) &&
8391 (define->attrs->type == XML_RELAXNG_PARAM)) {
8392 ret =
8393 lib->check(lib->data, define->name, value, &result, node);
8394 } else {
8395 ret = lib->check(lib->data, define->name, value, NULL, node);
8396 }
8397 } else
8398 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008399 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008400 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8401 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8402 lib->freef(lib->data, result);
8403 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008404 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008405 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008406 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008407 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008408 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008409 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8410 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008411 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008412 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008413 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008414 if (lib->facet != NULL) {
8415 tmp = lib->facet(lib->data, define->name, cur->name,
8416 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008417 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008418 ret = -1;
8419 }
8420 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008421 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008422 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008423 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008424
Daniel Veillard4c004142003-10-07 11:33:24 +00008425 oldvalue = ctxt->state->value;
8426 oldendvalue = ctxt->state->endvalue;
8427 ctxt->state->value = (xmlChar *) value;
8428 ctxt->state->endvalue = NULL;
8429 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8430 ctxt->state->value = (xmlChar *) oldvalue;
8431 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008432 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008433 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008434 lib->freef(lib->data, result);
8435 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008436}
8437
8438/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008439 * xmlRelaxNGNextValue:
8440 * @ctxt: a Relax-NG validation context
8441 *
8442 * Skip to the next value when validating within a list
8443 *
8444 * Returns 0 if the operation succeeded or an error code.
8445 */
8446static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008447xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8448{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008449 xmlChar *cur;
8450
8451 cur = ctxt->state->value;
8452 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008453 ctxt->state->value = NULL;
8454 ctxt->state->endvalue = NULL;
8455 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008456 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008457 while (*cur != 0)
8458 cur++;
8459 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8460 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008461 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008462 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008463 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008464 ctxt->state->value = cur;
8465 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008466}
8467
8468/**
8469 * xmlRelaxNGValidateValueList:
8470 * @ctxt: a Relax-NG validation context
8471 * @defines: the list of definitions to verify
8472 *
8473 * Validate the given set of definitions for the current value
8474 *
8475 * Returns 0 if the validation succeeded or an error code.
8476 */
8477static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008478xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8479 xmlRelaxNGDefinePtr defines)
8480{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008481 int ret = 0;
8482
8483 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008484 ret = xmlRelaxNGValidateValue(ctxt, defines);
8485 if (ret != 0)
8486 break;
8487 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008488 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008489 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008490}
8491
8492/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008493 * xmlRelaxNGValidateValue:
8494 * @ctxt: a Relax-NG validation context
8495 * @define: the definition to verify
8496 *
8497 * Validate the given definition for the current value
8498 *
8499 * Returns 0 if the validation succeeded or an error code.
8500 */
8501static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008502xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8503 xmlRelaxNGDefinePtr define)
8504{
Daniel Veillardedc91922003-01-26 00:52:04 +00008505 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008506 xmlChar *value;
8507
8508 value = ctxt->state->value;
8509 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008510 case XML_RELAXNG_EMPTY:{
8511 if ((value != NULL) && (value[0] != 0)) {
8512 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008513
William M. Brack76e95df2003-10-18 16:20:14 +00008514 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008515 idx++;
8516 if (value[idx] != 0)
8517 ret = -1;
8518 }
8519 break;
8520 }
8521 case XML_RELAXNG_TEXT:
8522 break;
8523 case XML_RELAXNG_VALUE:{
8524 if (!xmlStrEqual(value, define->value)) {
8525 if (define->name != NULL) {
8526 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008527
Daniel Veillard4c004142003-10-07 11:33:24 +00008528 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8529 if ((lib != NULL) && (lib->comp != NULL)) {
8530 ret = lib->comp(lib->data, define->name,
8531 define->value, define->node,
8532 (void *) define->attrs,
8533 value, ctxt->state->node);
8534 } else
8535 ret = -1;
8536 if (ret < 0) {
8537 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8538 define->name);
8539 return (-1);
8540 } else if (ret == 1) {
8541 ret = 0;
8542 } else {
8543 ret = -1;
8544 }
8545 } else {
8546 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008547
Daniel Veillard4c004142003-10-07 11:33:24 +00008548 /*
8549 * TODO: trivial optimizations are possible by
8550 * computing at compile-time
8551 */
8552 nval = xmlRelaxNGNormalize(ctxt, define->value);
8553 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008554
Daniel Veillard4c004142003-10-07 11:33:24 +00008555 if ((nval == NULL) || (nvalue == NULL) ||
8556 (!xmlStrEqual(nval, nvalue)))
8557 ret = -1;
8558 if (nval != NULL)
8559 xmlFree(nval);
8560 if (nvalue != NULL)
8561 xmlFree(nvalue);
8562 }
8563 }
8564 if (ret == 0)
8565 xmlRelaxNGNextValue(ctxt);
8566 break;
8567 }
8568 case XML_RELAXNG_DATATYPE:{
8569 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8570 ctxt->state->seq);
8571 if (ret == 0)
8572 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008573
Daniel Veillard4c004142003-10-07 11:33:24 +00008574 break;
8575 }
8576 case XML_RELAXNG_CHOICE:{
8577 xmlRelaxNGDefinePtr list = define->content;
8578 xmlChar *oldvalue;
8579
8580 oldflags = ctxt->flags;
8581 ctxt->flags |= FLAGS_IGNORABLE;
8582
8583 oldvalue = ctxt->state->value;
8584 while (list != NULL) {
8585 ret = xmlRelaxNGValidateValue(ctxt, list);
8586 if (ret == 0) {
8587 break;
8588 }
8589 ctxt->state->value = oldvalue;
8590 list = list->next;
8591 }
8592 ctxt->flags = oldflags;
8593 if (ret != 0) {
8594 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8595 xmlRelaxNGDumpValidError(ctxt);
8596 } else {
8597 if (ctxt->errNr > 0)
8598 xmlRelaxNGPopErrors(ctxt, 0);
8599 }
8600 if (ret == 0)
8601 xmlRelaxNGNextValue(ctxt);
8602 break;
8603 }
8604 case XML_RELAXNG_LIST:{
8605 xmlRelaxNGDefinePtr list = define->content;
8606 xmlChar *oldvalue, *oldend, *val, *cur;
8607
Daniel Veillard416589a2003-02-17 17:25:42 +00008608#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008609 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008610#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008611
Daniel Veillard4c004142003-10-07 11:33:24 +00008612 oldvalue = ctxt->state->value;
8613 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008614
Daniel Veillard4c004142003-10-07 11:33:24 +00008615 val = xmlStrdup(oldvalue);
8616 if (val == NULL) {
8617 val = xmlStrdup(BAD_CAST "");
8618 }
8619 if (val == NULL) {
8620 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8621 return (-1);
8622 }
8623 cur = val;
8624 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008625 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008626 *cur = 0;
8627 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008628#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008629 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008630#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008631 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008632 *cur++ = 0;
8633 } else
8634 cur++;
8635 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008636#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008637 xmlGenericError(xmlGenericErrorContext,
8638 "list value: '%s' found %d items\n",
8639 oldvalue, nb_values);
8640 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008641#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008642 ctxt->state->endvalue = cur;
8643 cur = val;
8644 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8645 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008646
Daniel Veillard4c004142003-10-07 11:33:24 +00008647 ctxt->state->value = cur;
8648
8649 while (list != NULL) {
8650 if (ctxt->state->value == ctxt->state->endvalue)
8651 ctxt->state->value = NULL;
8652 ret = xmlRelaxNGValidateValue(ctxt, list);
8653 if (ret != 0) {
8654#ifdef DEBUG_LIST
8655 xmlGenericError(xmlGenericErrorContext,
8656 "Failed to validate value: '%s' with %d rule\n",
8657 ctxt->state->value, nb_values);
8658#endif
8659 break;
8660 }
8661#ifdef DEBUG_LIST
8662 nb_values++;
8663#endif
8664 list = list->next;
8665 }
8666
8667 if ((ret == 0) && (ctxt->state->value != NULL) &&
8668 (ctxt->state->value != ctxt->state->endvalue)) {
8669 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8670 ctxt->state->value);
8671 ret = -1;
8672 }
8673 xmlFree(val);
8674 ctxt->state->value = oldvalue;
8675 ctxt->state->endvalue = oldend;
8676 break;
8677 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008678 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008679 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8680 if (ret != 0) {
8681 break;
8682 }
8683 /* no break on purpose */
8684 case XML_RELAXNG_ZEROORMORE:{
8685 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008686
Daniel Veillard4c004142003-10-07 11:33:24 +00008687 oldflags = ctxt->flags;
8688 ctxt->flags |= FLAGS_IGNORABLE;
8689 cur = ctxt->state->value;
8690 temp = NULL;
8691 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8692 (temp != cur)) {
8693 temp = cur;
8694 ret =
8695 xmlRelaxNGValidateValueList(ctxt, define->content);
8696 if (ret != 0) {
8697 ctxt->state->value = temp;
8698 ret = 0;
8699 break;
8700 }
8701 cur = ctxt->state->value;
8702 }
8703 ctxt->flags = oldflags;
8704 if (ret != 0) {
8705 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8706 xmlRelaxNGDumpValidError(ctxt);
8707 } else {
8708 if (ctxt->errNr > 0)
8709 xmlRelaxNGPopErrors(ctxt, 0);
8710 }
8711 break;
8712 }
8713 case XML_RELAXNG_EXCEPT:{
8714 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008715
Daniel Veillard4c004142003-10-07 11:33:24 +00008716 list = define->content;
8717 while (list != NULL) {
8718 ret = xmlRelaxNGValidateValue(ctxt, list);
8719 if (ret == 0) {
8720 ret = -1;
8721 break;
8722 } else
8723 ret = 0;
8724 list = list->next;
8725 }
8726 break;
8727 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008728 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008729 case XML_RELAXNG_GROUP:{
8730 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008731
Daniel Veillard4c004142003-10-07 11:33:24 +00008732 list = define->content;
8733 while (list != NULL) {
8734 ret = xmlRelaxNGValidateValue(ctxt, list);
8735 if (ret != 0) {
8736 ret = -1;
8737 break;
8738 } else
8739 ret = 0;
8740 list = list->next;
8741 }
8742 break;
8743 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008744 case XML_RELAXNG_REF:
8745 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008746 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8747 break;
8748 default:
8749 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008750 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008751 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008752}
8753
8754/**
8755 * xmlRelaxNGValidateValueContent:
8756 * @ctxt: a Relax-NG validation context
8757 * @defines: the list of definitions to verify
8758 *
8759 * Validate the given definitions for the current value
8760 *
8761 * Returns 0 if the validation succeeded or an error code.
8762 */
8763static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008764xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8765 xmlRelaxNGDefinePtr defines)
8766{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008767 int ret = 0;
8768
8769 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008770 ret = xmlRelaxNGValidateValue(ctxt, defines);
8771 if (ret != 0)
8772 break;
8773 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008774 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008775 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008776}
8777
8778/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008779 * xmlRelaxNGAttributeMatch:
8780 * @ctxt: a Relax-NG validation context
8781 * @define: the definition to check
8782 * @prop: the attribute
8783 *
8784 * Check if the attribute matches the definition nameClass
8785 *
8786 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8787 */
8788static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008789xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8790 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8791{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008792 int ret;
8793
8794 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008795 if (!xmlStrEqual(define->name, prop->name))
8796 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008797 }
8798 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008799 if (define->ns[0] == 0) {
8800 if (prop->ns != NULL)
8801 return (0);
8802 } else {
8803 if ((prop->ns == NULL) ||
8804 (!xmlStrEqual(define->ns, prop->ns->href)))
8805 return (0);
8806 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008807 }
8808 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008809 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008810 define = define->nameClass;
8811 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008812 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008813
Daniel Veillard4c004142003-10-07 11:33:24 +00008814 list = define->content;
8815 while (list != NULL) {
8816 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8817 if (ret == 1)
8818 return (0);
8819 if (ret < 0)
8820 return (ret);
8821 list = list->next;
8822 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008823 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008824 TODO}
8825 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008826}
8827
8828/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008829 * xmlRelaxNGValidateAttribute:
8830 * @ctxt: a Relax-NG validation context
8831 * @define: the definition to verify
8832 *
8833 * Validate the given attribute definition for that node
8834 *
8835 * Returns 0 if the validation succeeded or an error code.
8836 */
8837static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008838xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8839 xmlRelaxNGDefinePtr define)
8840{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008841 int ret = 0, i;
8842 xmlChar *value, *oldvalue;
8843 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008844 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008845
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008846 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008847 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008848 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008849 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8850 tmp = ctxt->state->attrs[i];
8851 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8852 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8853 (tmp->ns == NULL)) ||
8854 ((tmp->ns != NULL) &&
8855 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8856 prop = tmp;
8857 break;
8858 }
8859 }
8860 }
8861 if (prop != NULL) {
8862 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8863 oldvalue = ctxt->state->value;
8864 oldseq = ctxt->state->seq;
8865 ctxt->state->seq = (xmlNodePtr) prop;
8866 ctxt->state->value = value;
8867 ctxt->state->endvalue = NULL;
8868 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8869 if (ctxt->state->value != NULL)
8870 value = ctxt->state->value;
8871 if (value != NULL)
8872 xmlFree(value);
8873 ctxt->state->value = oldvalue;
8874 ctxt->state->seq = oldseq;
8875 if (ret == 0) {
8876 /*
8877 * flag the attribute as processed
8878 */
8879 ctxt->state->attrs[i] = NULL;
8880 ctxt->state->nbAttrLeft--;
8881 }
8882 } else {
8883 ret = -1;
8884 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008885#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008886 xmlGenericError(xmlGenericErrorContext,
8887 "xmlRelaxNGValidateAttribute(%s): %d\n",
8888 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008889#endif
8890 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008891 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8892 tmp = ctxt->state->attrs[i];
8893 if ((tmp != NULL) &&
8894 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8895 prop = tmp;
8896 break;
8897 }
8898 }
8899 if (prop != NULL) {
8900 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8901 oldvalue = ctxt->state->value;
8902 oldseq = ctxt->state->seq;
8903 ctxt->state->seq = (xmlNodePtr) prop;
8904 ctxt->state->value = value;
8905 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8906 if (ctxt->state->value != NULL)
8907 value = ctxt->state->value;
8908 if (value != NULL)
8909 xmlFree(value);
8910 ctxt->state->value = oldvalue;
8911 ctxt->state->seq = oldseq;
8912 if (ret == 0) {
8913 /*
8914 * flag the attribute as processed
8915 */
8916 ctxt->state->attrs[i] = NULL;
8917 ctxt->state->nbAttrLeft--;
8918 }
8919 } else {
8920 ret = -1;
8921 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008922#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008923 if (define->ns != NULL) {
8924 xmlGenericError(xmlGenericErrorContext,
8925 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8926 define->ns, ret);
8927 } else {
8928 xmlGenericError(xmlGenericErrorContext,
8929 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8930 ret);
8931 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008932#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008933 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008934
8935 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008936}
8937
8938/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008939 * xmlRelaxNGValidateAttributeList:
8940 * @ctxt: a Relax-NG validation context
8941 * @define: the list of definition to verify
8942 *
8943 * Validate the given node against the list of attribute definitions
8944 *
8945 * Returns 0 if the validation succeeded or an error code.
8946 */
8947static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008948xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8949 xmlRelaxNGDefinePtr defines)
8950{
Daniel Veillardce192eb2003-04-16 15:58:05 +00008951 int ret = 0, res;
8952 int needmore = 0;
8953 xmlRelaxNGDefinePtr cur;
8954
8955 cur = defines;
8956 while (cur != NULL) {
8957 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008958 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8959 ret = -1;
8960 } else
8961 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008962 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008963 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008964 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00008965 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008966 cur = defines;
8967 while (cur != NULL) {
8968 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008969 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8970 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8971 if (res < 0)
8972 ret = -1;
8973 } else {
8974 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8975 return (-1);
8976 }
8977 if (res == -1) /* continues on -2 */
8978 break;
8979 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008980 cur = cur->next;
8981 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008982
8983 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008984}
8985
8986/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008987 * xmlRelaxNGNodeMatchesList:
8988 * @node: the node
8989 * @list: a NULL terminated array of definitions
8990 *
8991 * Check if a node can be matched by one of the definitions
8992 *
8993 * Returns 1 if matches 0 otherwise
8994 */
8995static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008996xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
8997{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008998 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008999 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009000
9001 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009002 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009003
9004 cur = list[i++];
9005 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009006 if ((node->type == XML_ELEMENT_NODE) &&
9007 (cur->type == XML_RELAXNG_ELEMENT)) {
9008 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9009 if (tmp == 1)
9010 return (1);
9011 } else if (((node->type == XML_TEXT_NODE) ||
9012 (node->type == XML_CDATA_SECTION_NODE)) &&
9013 (cur->type == XML_RELAXNG_TEXT)) {
9014 return (1);
9015 }
9016 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009017 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009018 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009019}
9020
9021/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009022 * xmlRelaxNGValidateInterleave:
9023 * @ctxt: a Relax-NG validation context
9024 * @define: the definition to verify
9025 *
9026 * Validate an interleave definition for a node.
9027 *
9028 * Returns 0 if the validation succeeded or an error code.
9029 */
9030static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009031xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9032 xmlRelaxNGDefinePtr define)
9033{
William M. Brack779af002003-08-01 15:55:39 +00009034 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009035 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009036 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009037
9038 xmlRelaxNGValidStatePtr oldstate;
9039 xmlRelaxNGPartitionPtr partitions;
9040 xmlRelaxNGInterleaveGroupPtr group = NULL;
9041 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9042 xmlNodePtr *list = NULL, *lasts = NULL;
9043
9044 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009045 partitions = (xmlRelaxNGPartitionPtr) define->data;
9046 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009047 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009048 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9049 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009050 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009051 /*
9052 * Optimizations for MIXED
9053 */
9054 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009055 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009056 ctxt->flags |= FLAGS_MIXED_CONTENT;
9057 if (nbgroups == 2) {
9058 /*
9059 * this is a pure <mixed> case
9060 */
9061 if (ctxt->state != NULL)
9062 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9063 ctxt->state->seq);
9064 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9065 ret = xmlRelaxNGValidateDefinition(ctxt,
9066 partitions->groups[1]->
9067 rule);
9068 else
9069 ret = xmlRelaxNGValidateDefinition(ctxt,
9070 partitions->groups[0]->
9071 rule);
9072 if (ret == 0) {
9073 if (ctxt->state != NULL)
9074 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9075 ctxt->state->
9076 seq);
9077 }
9078 ctxt->flags = oldflags;
9079 return (ret);
9080 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009081 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009082
9083 /*
9084 * Build arrays to store the first and last node of the chain
9085 * pertaining to each group
9086 */
9087 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9088 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009089 xmlRngVErrMemory(ctxt, "validating\n");
9090 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009091 }
9092 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9093 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9094 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009095 xmlRngVErrMemory(ctxt, "validating\n");
9096 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009097 }
9098 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9099
9100 /*
9101 * Walk the sequence of children finding the right group and
9102 * sorting them in sequences.
9103 */
9104 cur = ctxt->state->seq;
9105 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9106 start = cur;
9107 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009108 ctxt->state->seq = cur;
9109 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009110 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009111 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009112
Daniel Veillard4c004142003-10-07 11:33:24 +00009113 if ((cur->type == XML_TEXT_NODE) ||
9114 (cur->type == XML_CDATA_SECTION_NODE)) {
9115 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9116 NULL);
9117 } else if (cur->type == XML_ELEMENT_NODE) {
9118 if (cur->ns != NULL) {
9119 tmp = xmlHashLookup2(partitions->triage, cur->name,
9120 cur->ns->href);
9121 if (tmp == NULL)
9122 tmp = xmlHashLookup2(partitions->triage,
9123 BAD_CAST "#any",
9124 cur->ns->href);
9125 } else
9126 tmp =
9127 xmlHashLookup2(partitions->triage, cur->name,
9128 NULL);
9129 if (tmp == NULL)
9130 tmp =
9131 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9132 NULL);
9133 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009134
Daniel Veillard4c004142003-10-07 11:33:24 +00009135 if (tmp == NULL) {
9136 i = nbgroups;
9137 } else {
9138 i = ((long) tmp) - 1;
9139 if (partitions->flags & IS_NEEDCHECK) {
9140 group = partitions->groups[i];
9141 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9142 i = nbgroups;
9143 }
9144 }
9145 } else {
9146 for (i = 0; i < nbgroups; i++) {
9147 group = partitions->groups[i];
9148 if (group == NULL)
9149 continue;
9150 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9151 break;
9152 }
9153 }
9154 /*
9155 * We break as soon as an element not matched is found
9156 */
9157 if (i >= nbgroups) {
9158 break;
9159 }
9160 if (lasts[i] != NULL) {
9161 lasts[i]->next = cur;
9162 lasts[i] = cur;
9163 } else {
9164 list[i] = cur;
9165 lasts[i] = cur;
9166 }
9167 if (cur->next != NULL)
9168 lastchg = cur->next;
9169 else
9170 lastchg = cur;
9171 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009172 }
9173 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009174 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9175 ret = -1;
9176 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009177 }
9178 lastelem = cur;
9179 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009180 for (i = 0; i < nbgroups; i++) {
9181 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9182 group = partitions->groups[i];
9183 if (lasts[i] != NULL) {
9184 last = lasts[i]->next;
9185 lasts[i]->next = NULL;
9186 }
9187 ctxt->state->seq = list[i];
9188 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9189 if (ret != 0)
9190 break;
9191 if (ctxt->state != NULL) {
9192 cur = ctxt->state->seq;
9193 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9194 xmlRelaxNGFreeValidState(ctxt, oldstate);
9195 oldstate = ctxt->state;
9196 ctxt->state = NULL;
9197 if (cur != NULL) {
9198 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9199 ret = -1;
9200 ctxt->state = oldstate;
9201 goto done;
9202 }
9203 } else if (ctxt->states != NULL) {
9204 int j;
9205 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009206
Daniel Veillard4c004142003-10-07 11:33:24 +00009207 for (j = 0; j < ctxt->states->nbState; j++) {
9208 cur = ctxt->states->tabState[j]->seq;
9209 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9210 if (cur == NULL) {
9211 found = 1;
9212 break;
9213 }
9214 }
9215 if (ctxt->states->nbState > 0) {
9216 xmlRelaxNGFreeValidState(ctxt, oldstate);
9217 oldstate =
9218 ctxt->states->tabState[ctxt->states->nbState - 1];
9219 }
9220 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9221 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9222 }
9223 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9224 ctxt->states = NULL;
9225 if (found == 0) {
9226 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9227 ret = -1;
9228 ctxt->state = oldstate;
9229 goto done;
9230 }
9231 } else {
9232 ret = -1;
9233 break;
9234 }
9235 if (lasts[i] != NULL) {
9236 lasts[i]->next = last;
9237 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009238 }
9239 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009240 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009241 ctxt->state = oldstate;
9242 ctxt->state->seq = lastelem;
9243 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009244 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9245 ret = -1;
9246 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009247 }
9248
Daniel Veillard4c004142003-10-07 11:33:24 +00009249 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009250 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009251 /*
9252 * builds the next links chain from the prev one
9253 */
9254 cur = lastchg;
9255 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009256 if ((cur == start) || (cur->prev == NULL))
9257 break;
9258 cur->prev->next = cur;
9259 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009260 }
9261 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009262 if (ctxt->errNr > errNr)
9263 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009264 }
9265
9266 xmlFree(list);
9267 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009268 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009269}
9270
9271/**
9272 * xmlRelaxNGValidateDefinitionList:
9273 * @ctxt: a Relax-NG validation context
9274 * @define: the list of definition to verify
9275 *
9276 * Validate the given node content against the (list) of definitions
9277 *
9278 * Returns 0 if the validation succeeded or an error code.
9279 */
9280static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009281xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9282 xmlRelaxNGDefinePtr defines)
9283{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009284 int ret = 0, res;
9285
9286
Daniel Veillard952379b2003-03-17 15:37:12 +00009287 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009288 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9289 BAD_CAST "NULL definition list");
9290 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009291 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009292 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009293 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9294 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9295 if (res < 0)
9296 ret = -1;
9297 } else {
9298 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9299 return (-1);
9300 }
9301 if (res == -1) /* continues on -2 */
9302 break;
9303 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009304 }
9305
Daniel Veillard4c004142003-10-07 11:33:24 +00009306 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009307}
9308
9309/**
9310 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009311 * @ctxt: a Relax-NG validation context
9312 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009313 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009314 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009315 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009316 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009317 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009318 */
9319static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009320xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9321 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9322{
Daniel Veillard580ced82003-03-21 21:22:48 +00009323 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009324
Daniel Veillardfd573f12003-03-16 17:52:32 +00009325 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009326 if (!xmlStrEqual(elem->name, define->name)) {
9327 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9328 return (0);
9329 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009330 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009331 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009332 if (elem->ns == NULL) {
9333 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9334 return (0);
9335 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9336 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9337 elem->name, define->ns);
9338 return (0);
9339 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009340 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009341 (define->name == NULL)) {
9342 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9343 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009344 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009345 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9346 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009347 }
9348
9349 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009350 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009351
9352 define = define->nameClass;
9353 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009354 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009355
Daniel Veillard4c004142003-10-07 11:33:24 +00009356 if (ctxt != NULL) {
9357 oldflags = ctxt->flags;
9358 ctxt->flags |= FLAGS_IGNORABLE;
9359 }
9360
9361 list = define->content;
9362 while (list != NULL) {
9363 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9364 if (ret == 1) {
9365 if (ctxt != NULL)
9366 ctxt->flags = oldflags;
9367 return (0);
9368 }
9369 if (ret < 0) {
9370 if (ctxt != NULL)
9371 ctxt->flags = oldflags;
9372 return (ret);
9373 }
9374 list = list->next;
9375 }
9376 ret = 1;
9377 if (ctxt != NULL) {
9378 ctxt->flags = oldflags;
9379 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009380 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009381 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009382
Daniel Veillard4c004142003-10-07 11:33:24 +00009383 if (ctxt != NULL) {
9384 oldflags = ctxt->flags;
9385 ctxt->flags |= FLAGS_IGNORABLE;
9386 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009387
Daniel Veillard4c004142003-10-07 11:33:24 +00009388 list = define->nameClass;
9389 while (list != NULL) {
9390 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9391 if (ret == 1) {
9392 if (ctxt != NULL)
9393 ctxt->flags = oldflags;
9394 return (1);
9395 }
9396 if (ret < 0) {
9397 if (ctxt != NULL)
9398 ctxt->flags = oldflags;
9399 return (ret);
9400 }
9401 list = list->next;
9402 }
9403 if (ctxt != NULL) {
9404 if (ret != 0) {
9405 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9406 xmlRelaxNGDumpValidError(ctxt);
9407 } else {
9408 if (ctxt->errNr > 0)
9409 xmlRelaxNGPopErrors(ctxt, 0);
9410 }
9411 }
9412 ret = 0;
9413 if (ctxt != NULL) {
9414 ctxt->flags = oldflags;
9415 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009416 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009417 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009418 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009419 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009420}
9421
9422/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009423 * xmlRelaxNGBestState:
9424 * @ctxt: a Relax-NG validation context
9425 *
9426 * Find the "best" state in the ctxt->states list of states to report
9427 * errors about. I.e. a state with no element left in the child list
9428 * or the one with the less attributes left.
9429 * This is called only if a falidation error was detected
9430 *
9431 * Returns the index of the "best" state or -1 in case of error
9432 */
9433static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009434xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9435{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009436 xmlRelaxNGValidStatePtr state;
9437 int i, tmp;
9438 int best = -1;
9439 int value = 1000000;
9440
9441 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9442 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009443 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009444
Daniel Veillard4c004142003-10-07 11:33:24 +00009445 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009446 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009447 if (state == NULL)
9448 continue;
9449 if (state->seq != NULL) {
9450 if ((best == -1) || (value > 100000)) {
9451 value = 100000;
9452 best = i;
9453 }
9454 } else {
9455 tmp = state->nbAttrLeft;
9456 if ((best == -1) || (value > tmp)) {
9457 value = tmp;
9458 best = i;
9459 }
9460 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009461 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009462 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009463}
9464
9465/**
9466 * xmlRelaxNGLogBestError:
9467 * @ctxt: a Relax-NG validation context
9468 *
9469 * Find the "best" state in the ctxt->states list of states to report
9470 * errors about and log it.
9471 */
9472static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009473xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9474{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009475 int best;
9476
9477 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9478 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009479 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009480
9481 best = xmlRelaxNGBestState(ctxt);
9482 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009483 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009484
Daniel Veillard4c004142003-10-07 11:33:24 +00009485 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009486 }
9487}
9488
9489/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009490 * xmlRelaxNGValidateElementEnd:
9491 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009492 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009493 *
9494 * Validate the end of the element, implements check that
9495 * there is nothing left not consumed in the element content
9496 * or in the attribute list.
9497 *
9498 * Returns 0 if the validation succeeded or an error code.
9499 */
9500static int
William M. Brack272693c2003-11-14 16:20:34 +00009501xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009502{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009503 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009504 xmlRelaxNGValidStatePtr state;
9505
9506 state = ctxt->state;
9507 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009508 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9509 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009510 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009511 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9512 state->node->name, state->seq->name);
9513 }
9514 return (-1);
9515 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009516 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009517 for (i = 0; i < state->nbAttrs; i++) {
9518 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009519 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009520 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9521 state->attrs[i]->name, state->node->name);
9522 }
9523 return (-1 - i);
9524 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009525 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009526 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009527}
9528
9529/**
9530 * xmlRelaxNGValidateState:
9531 * @ctxt: a Relax-NG validation context
9532 * @define: the definition to verify
9533 *
9534 * Validate the current state against the definition
9535 *
9536 * Returns 0 if the validation succeeded or an error code.
9537 */
9538static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009539xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9540 xmlRelaxNGDefinePtr define)
9541{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009542 xmlNodePtr node;
9543 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009544 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009545
9546 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009547 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9548 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009549 }
9550
9551 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009552 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009553 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009554 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009555 }
9556#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009557 for (i = 0; i < ctxt->depth; i++)
9558 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009559 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009560 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009561 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009562 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009563 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009564 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009565 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009566 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009567#endif
9568 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009569 switch (define->type) {
9570 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009571 node = xmlRelaxNGSkipIgnored(ctxt, node);
9572 ret = 0;
9573 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009574 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009575 ret = -1;
9576 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009577 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009578 while ((node != NULL) &&
9579 ((node->type == XML_TEXT_NODE) ||
9580 (node->type == XML_COMMENT_NODE) ||
9581 (node->type == XML_PI_NODE) ||
9582 (node->type == XML_CDATA_SECTION_NODE)))
9583 node = node->next;
9584 ctxt->state->seq = node;
9585 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009586 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009587 errNr = ctxt->errNr;
9588 node = xmlRelaxNGSkipIgnored(ctxt, node);
9589 if (node == NULL) {
9590 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9591 ret = -1;
9592 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9593 xmlRelaxNGDumpValidError(ctxt);
9594 break;
9595 }
9596 if (node->type != XML_ELEMENT_NODE) {
9597 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9598 ret = -1;
9599 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9600 xmlRelaxNGDumpValidError(ctxt);
9601 break;
9602 }
9603 /*
9604 * This node was already validated successfully against
9605 * this definition.
9606 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009607 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009608 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9609 if (ctxt->errNr > errNr)
9610 xmlRelaxNGPopErrors(ctxt, errNr);
9611 if (ctxt->errNr != 0) {
9612 while ((ctxt->err != NULL) &&
9613 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9614 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9615 ||
9616 ((ctxt->err->err ==
9617 XML_RELAXNG_ERR_ELEMEXTRANS)
9618 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9619 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9620 || (ctxt->err->err ==
9621 XML_RELAXNG_ERR_NOTELEM)))
9622 xmlRelaxNGValidErrorPop(ctxt);
9623 }
9624 break;
9625 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009626
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009627 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9628 if (ret <= 0) {
9629 ret = -1;
9630 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9631 xmlRelaxNGDumpValidError(ctxt);
9632 break;
9633 }
9634 ret = 0;
9635 if (ctxt->errNr != 0) {
9636 if (ctxt->errNr > errNr)
9637 xmlRelaxNGPopErrors(ctxt, errNr);
9638 while ((ctxt->err != NULL) &&
9639 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9640 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9641 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9642 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9643 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9644 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9645 xmlRelaxNGValidErrorPop(ctxt);
9646 }
9647 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009648
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009649 oldflags = ctxt->flags;
9650 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9651 ctxt->flags -= FLAGS_MIXED_CONTENT;
9652 }
9653 state = xmlRelaxNGNewValidState(ctxt, node);
9654 if (state == NULL) {
9655 ret = -1;
9656 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9657 xmlRelaxNGDumpValidError(ctxt);
9658 break;
9659 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009660
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009661 oldstate = ctxt->state;
9662 ctxt->state = state;
9663 if (define->attrs != NULL) {
9664 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9665 if (tmp != 0) {
9666 ret = -1;
9667 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9668 }
9669 }
9670 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009671 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9672 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9673 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009674
Daniel Veillard4c004142003-10-07 11:33:24 +00009675 nstate = xmlRelaxNGNewValidState(ctxt, node);
9676 ctxt->state = nstate;
9677 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009678
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009679 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9680 define->contModel,
9681 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009682 nseq = ctxt->state->seq;
9683 ctxt->state = tmpstate;
9684 ctxt->states = tmpstates;
9685 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009686
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009687#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009688 xmlGenericError(xmlGenericErrorContext,
9689 "Validating content of '%s' : %d\n",
9690 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009691#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009692 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009693 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009694
9695 if (ctxt->states != NULL) {
9696 tmp = -1;
9697
Daniel Veillardce192eb2003-04-16 15:58:05 +00009698 for (i = 0; i < ctxt->states->nbState; i++) {
9699 state = ctxt->states->tabState[i];
9700 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009701 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009702
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009703 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009704 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009705 break;
9706 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009707 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009708 if (tmp != 0) {
9709 /*
9710 * validation error, log the message for the "best" one
9711 */
9712 ctxt->flags |= FLAGS_IGNORABLE;
9713 xmlRelaxNGLogBestError(ctxt);
9714 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009715 for (i = 0; i < ctxt->states->nbState; i++) {
9716 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009717 ctxt->states->
9718 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009719 }
9720 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9721 ctxt->flags = oldflags;
9722 ctxt->states = NULL;
9723 if ((ret == 0) && (tmp == -1))
9724 ret = -1;
9725 } else {
9726 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009727 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009728 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009729 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009730 xmlRelaxNGFreeValidState(ctxt, state);
9731 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009732 } else {
9733 if (define->content != NULL) {
9734 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009735 define->
9736 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009737 if (tmp != 0) {
9738 ret = -1;
9739 if (ctxt->state == NULL) {
9740 ctxt->state = oldstate;
9741 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9742 node->name);
9743 ctxt->state = NULL;
9744 } else {
9745 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9746 node->name);
9747 }
9748
9749 }
9750 }
9751 if (ctxt->states != NULL) {
9752 tmp = -1;
9753
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009754 for (i = 0; i < ctxt->states->nbState; i++) {
9755 state = ctxt->states->tabState[i];
9756 ctxt->state = state;
9757
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009758 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009759 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009760 break;
9761 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009762 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009763 if (tmp != 0) {
9764 /*
9765 * validation error, log the message for the "best" one
9766 */
9767 ctxt->flags |= FLAGS_IGNORABLE;
9768 xmlRelaxNGLogBestError(ctxt);
9769 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009770 for (i = 0; i < ctxt->states->nbState; i++) {
9771 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009772 ctxt->states->
9773 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009774 }
9775 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9776 ctxt->flags = oldflags;
9777 ctxt->states = NULL;
9778 if ((ret == 0) && (tmp == -1))
9779 ret = -1;
9780 } else {
9781 state = ctxt->state;
9782 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009783 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009784 xmlRelaxNGFreeValidState(ctxt, state);
9785 }
9786 }
9787 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +00009788 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009789 }
9790 ctxt->flags = oldflags;
9791 ctxt->state = oldstate;
9792 if (oldstate != NULL)
9793 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9794 if (ret != 0) {
9795 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9796 xmlRelaxNGDumpValidError(ctxt);
9797 ret = 0;
9798 } else {
9799 ret = -2;
9800 }
9801 } else {
9802 if (ctxt->errNr > errNr)
9803 xmlRelaxNGPopErrors(ctxt, errNr);
9804 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009805
9806#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009807 xmlGenericError(xmlGenericErrorContext,
9808 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9809 node->name, ret);
9810 if (oldstate == NULL)
9811 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9812 else if (oldstate->seq == NULL)
9813 xmlGenericError(xmlGenericErrorContext, ": done\n");
9814 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9815 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9816 oldstate->seq->name);
9817 else
9818 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9819 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009820#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009821 break;
9822 case XML_RELAXNG_OPTIONAL:{
9823 errNr = ctxt->errNr;
9824 oldflags = ctxt->flags;
9825 ctxt->flags |= FLAGS_IGNORABLE;
9826 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9827 ret =
9828 xmlRelaxNGValidateDefinitionList(ctxt,
9829 define->content);
9830 if (ret != 0) {
9831 if (ctxt->state != NULL)
9832 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9833 ctxt->state = oldstate;
9834 ctxt->flags = oldflags;
9835 ret = 0;
9836 if (ctxt->errNr > errNr)
9837 xmlRelaxNGPopErrors(ctxt, errNr);
9838 break;
9839 }
9840 if (ctxt->states != NULL) {
9841 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9842 } else {
9843 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9844 if (ctxt->states == NULL) {
9845 xmlRelaxNGFreeValidState(ctxt, oldstate);
9846 ctxt->flags = oldflags;
9847 ret = -1;
9848 if (ctxt->errNr > errNr)
9849 xmlRelaxNGPopErrors(ctxt, errNr);
9850 break;
9851 }
9852 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9853 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9854 ctxt->state = NULL;
9855 }
9856 ctxt->flags = oldflags;
9857 ret = 0;
9858 if (ctxt->errNr > errNr)
9859 xmlRelaxNGPopErrors(ctxt, errNr);
9860 break;
9861 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009862 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009863 errNr = ctxt->errNr;
9864 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9865 if (ret != 0) {
9866 break;
9867 }
9868 if (ctxt->errNr > errNr)
9869 xmlRelaxNGPopErrors(ctxt, errNr);
9870 /* no break on purpose */
9871 case XML_RELAXNG_ZEROORMORE:{
9872 int progress;
9873 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9874 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009875
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009876 errNr = ctxt->errNr;
9877 res = xmlRelaxNGNewStates(ctxt, 1);
9878 if (res == NULL) {
9879 ret = -1;
9880 break;
9881 }
9882 /*
9883 * All the input states are also exit states
9884 */
9885 if (ctxt->state != NULL) {
9886 xmlRelaxNGAddStates(ctxt, res,
9887 xmlRelaxNGCopyValidState(ctxt,
9888 ctxt->
9889 state));
9890 } else {
9891 for (j = 0; j < ctxt->states->nbState; j++) {
9892 xmlRelaxNGAddStates(ctxt, res,
9893 xmlRelaxNGCopyValidState(ctxt,
9894 ctxt->
9895 states->
9896 tabState
9897 [j]));
9898 }
9899 }
9900 oldflags = ctxt->flags;
9901 ctxt->flags |= FLAGS_IGNORABLE;
9902 do {
9903 progress = 0;
9904 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009905
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009906 if (ctxt->states != NULL) {
9907 states = ctxt->states;
9908 for (i = 0; i < states->nbState; i++) {
9909 ctxt->state = states->tabState[i];
9910 ctxt->states = NULL;
9911 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9912 define->
9913 content);
9914 if (ret == 0) {
9915 if (ctxt->state != NULL) {
9916 tmp = xmlRelaxNGAddStates(ctxt, res,
9917 ctxt->state);
9918 ctxt->state = NULL;
9919 if (tmp == 1)
9920 progress = 1;
9921 } else if (ctxt->states != NULL) {
9922 for (j = 0; j < ctxt->states->nbState;
9923 j++) {
9924 tmp =
9925 xmlRelaxNGAddStates(ctxt, res,
9926 ctxt->
9927 states->
9928 tabState
9929 [j]);
9930 if (tmp == 1)
9931 progress = 1;
9932 }
9933 xmlRelaxNGFreeStates(ctxt,
9934 ctxt->states);
9935 ctxt->states = NULL;
9936 }
9937 } else {
9938 if (ctxt->state != NULL) {
9939 xmlRelaxNGFreeValidState(ctxt,
9940 ctxt->state);
9941 ctxt->state = NULL;
9942 }
9943 }
9944 }
9945 } else {
9946 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9947 define->
9948 content);
9949 if (ret != 0) {
9950 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9951 ctxt->state = NULL;
9952 } else {
9953 base = res->nbState;
9954 if (ctxt->state != NULL) {
9955 tmp = xmlRelaxNGAddStates(ctxt, res,
9956 ctxt->state);
9957 ctxt->state = NULL;
9958 if (tmp == 1)
9959 progress = 1;
9960 } else if (ctxt->states != NULL) {
9961 for (j = 0; j < ctxt->states->nbState; j++) {
9962 tmp = xmlRelaxNGAddStates(ctxt, res,
9963 ctxt->
9964 states->
9965 tabState[j]);
9966 if (tmp == 1)
9967 progress = 1;
9968 }
9969 if (states == NULL) {
9970 states = ctxt->states;
9971 } else {
9972 xmlRelaxNGFreeStates(ctxt,
9973 ctxt->states);
9974 }
9975 ctxt->states = NULL;
9976 }
9977 }
9978 }
9979 if (progress) {
9980 /*
9981 * Collect all the new nodes added at that step
9982 * and make them the new node set
9983 */
9984 if (res->nbState - base == 1) {
9985 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9986 res->
9987 tabState
9988 [base]);
9989 } else {
9990 if (states == NULL) {
9991 xmlRelaxNGNewStates(ctxt,
9992 res->nbState - base);
9993 }
9994 states->nbState = 0;
9995 for (i = base; i < res->nbState; i++)
9996 xmlRelaxNGAddStates(ctxt, states,
9997 xmlRelaxNGCopyValidState
9998 (ctxt,
9999 res->tabState[i]));
10000 ctxt->states = states;
10001 }
10002 }
10003 } while (progress == 1);
10004 if (states != NULL) {
10005 xmlRelaxNGFreeStates(ctxt, states);
10006 }
10007 ctxt->states = res;
10008 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010009#if 0
10010 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010011 * errors may have to be propagated back...
10012 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010013 if (ctxt->errNr > errNr)
10014 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010015#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010016 ret = 0;
10017 break;
10018 }
10019 case XML_RELAXNG_CHOICE:{
10020 xmlRelaxNGDefinePtr list = NULL;
10021 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010022
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010023 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010024
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010025 errNr = ctxt->errNr;
10026 if ((define->dflags & IS_TRIABLE)
10027 && (define->data != NULL)) {
10028 xmlHashTablePtr triage =
10029 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010030
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010031 /*
10032 * Something we can optimize cleanly there is only one
10033 * possble branch out !
10034 */
10035 if (node == NULL) {
10036 ret = -1;
10037 break;
10038 }
10039 if ((node->type == XML_TEXT_NODE) ||
10040 (node->type == XML_CDATA_SECTION_NODE)) {
10041 list =
10042 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10043 } else if (node->type == XML_ELEMENT_NODE) {
10044 if (node->ns != NULL) {
10045 list = xmlHashLookup2(triage, node->name,
10046 node->ns->href);
10047 if (list == NULL)
10048 list =
10049 xmlHashLookup2(triage, BAD_CAST "#any",
10050 node->ns->href);
10051 } else
10052 list =
10053 xmlHashLookup2(triage, node->name, NULL);
10054 if (list == NULL)
10055 list =
10056 xmlHashLookup2(triage, BAD_CAST "#any",
10057 NULL);
10058 }
10059 if (list == NULL) {
10060 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010061 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010062 break;
10063 }
10064 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10065 if (ret == 0) {
10066 }
10067 break;
10068 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010069
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010070 list = define->content;
10071 oldflags = ctxt->flags;
10072 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010073
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010074 while (list != NULL) {
10075 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10076 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10077 if (ret == 0) {
10078 if (states == NULL) {
10079 states = xmlRelaxNGNewStates(ctxt, 1);
10080 }
10081 if (ctxt->state != NULL) {
10082 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10083 } else if (ctxt->states != NULL) {
10084 for (i = 0; i < ctxt->states->nbState; i++) {
10085 xmlRelaxNGAddStates(ctxt, states,
10086 ctxt->states->
10087 tabState[i]);
10088 }
10089 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10090 ctxt->states = NULL;
10091 }
10092 } else {
10093 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10094 }
10095 ctxt->state = oldstate;
10096 list = list->next;
10097 }
10098 if (states != NULL) {
10099 xmlRelaxNGFreeValidState(ctxt, oldstate);
10100 ctxt->states = states;
10101 ctxt->state = NULL;
10102 ret = 0;
10103 } else {
10104 ctxt->states = NULL;
10105 }
10106 ctxt->flags = oldflags;
10107 if (ret != 0) {
10108 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10109 xmlRelaxNGDumpValidError(ctxt);
10110 }
10111 } else {
10112 if (ctxt->errNr > errNr)
10113 xmlRelaxNGPopErrors(ctxt, errNr);
10114 }
10115 break;
10116 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010117 case XML_RELAXNG_DEF:
10118 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010119 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10120 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010121 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010122 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10123 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010124 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010125 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10126 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010127 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010128 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010129 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010130 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010131 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010132 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10133 break;
10134 case XML_RELAXNG_DATATYPE:{
10135 xmlNodePtr child;
10136 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010137
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010138 child = node;
10139 while (child != NULL) {
10140 if (child->type == XML_ELEMENT_NODE) {
10141 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10142 node->parent->name);
10143 ret = -1;
10144 break;
10145 } else if ((child->type == XML_TEXT_NODE) ||
10146 (child->type == XML_CDATA_SECTION_NODE)) {
10147 content = xmlStrcat(content, child->content);
10148 }
10149 /* TODO: handle entities ... */
10150 child = child->next;
10151 }
10152 if (ret == -1) {
10153 if (content != NULL)
10154 xmlFree(content);
10155 break;
10156 }
10157 if (content == NULL) {
10158 content = xmlStrdup(BAD_CAST "");
10159 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010160 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010161 ret = -1;
10162 break;
10163 }
10164 }
10165 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10166 ctxt->state->seq);
10167 if (ret == -1) {
10168 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10169 } else if (ret == 0) {
10170 ctxt->state->seq = NULL;
10171 }
10172 if (content != NULL)
10173 xmlFree(content);
10174 break;
10175 }
10176 case XML_RELAXNG_VALUE:{
10177 xmlChar *content = NULL;
10178 xmlChar *oldvalue;
10179 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010180
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010181 child = node;
10182 while (child != NULL) {
10183 if (child->type == XML_ELEMENT_NODE) {
10184 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10185 node->parent->name);
10186 ret = -1;
10187 break;
10188 } else if ((child->type == XML_TEXT_NODE) ||
10189 (child->type == XML_CDATA_SECTION_NODE)) {
10190 content = xmlStrcat(content, child->content);
10191 }
10192 /* TODO: handle entities ... */
10193 child = child->next;
10194 }
10195 if (ret == -1) {
10196 if (content != NULL)
10197 xmlFree(content);
10198 break;
10199 }
10200 if (content == NULL) {
10201 content = xmlStrdup(BAD_CAST "");
10202 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010203 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010204 ret = -1;
10205 break;
10206 }
10207 }
10208 oldvalue = ctxt->state->value;
10209 ctxt->state->value = content;
10210 ret = xmlRelaxNGValidateValue(ctxt, define);
10211 ctxt->state->value = oldvalue;
10212 if (ret == -1) {
10213 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10214 } else if (ret == 0) {
10215 ctxt->state->seq = NULL;
10216 }
10217 if (content != NULL)
10218 xmlFree(content);
10219 break;
10220 }
10221 case XML_RELAXNG_LIST:{
10222 xmlChar *content;
10223 xmlNodePtr child;
10224 xmlChar *oldvalue, *oldendvalue;
10225 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010226
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010227 /*
10228 * Make sure it's only text nodes
10229 */
10230
10231 content = NULL;
10232 child = node;
10233 while (child != NULL) {
10234 if (child->type == XML_ELEMENT_NODE) {
10235 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10236 node->parent->name);
10237 ret = -1;
10238 break;
10239 } else if ((child->type == XML_TEXT_NODE) ||
10240 (child->type == XML_CDATA_SECTION_NODE)) {
10241 content = xmlStrcat(content, child->content);
10242 }
10243 /* TODO: handle entities ... */
10244 child = child->next;
10245 }
10246 if (ret == -1) {
10247 if (content != NULL)
10248 xmlFree(content);
10249 break;
10250 }
10251 if (content == NULL) {
10252 content = xmlStrdup(BAD_CAST "");
10253 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010254 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010255 ret = -1;
10256 break;
10257 }
10258 }
10259 len = xmlStrlen(content);
10260 oldvalue = ctxt->state->value;
10261 oldendvalue = ctxt->state->endvalue;
10262 ctxt->state->value = content;
10263 ctxt->state->endvalue = content + len;
10264 ret = xmlRelaxNGValidateValue(ctxt, define);
10265 ctxt->state->value = oldvalue;
10266 ctxt->state->endvalue = oldendvalue;
10267 if (ret == -1) {
10268 VALID_ERR(XML_RELAXNG_ERR_LIST);
10269 } else if ((ret == 0) && (node != NULL)) {
10270 ctxt->state->seq = node->next;
10271 }
10272 if (content != NULL)
10273 xmlFree(content);
10274 break;
10275 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010276 case XML_RELAXNG_EXCEPT:
10277 case XML_RELAXNG_PARAM:
10278 TODO ret = -1;
10279 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010280 }
10281 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010282#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010283 for (i = 0; i < ctxt->depth; i++)
10284 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010285 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010286 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010287 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010288 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010289 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010290 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010291 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010292 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010293#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010294 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010295}
10296
10297/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010298 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010299 * @ctxt: a Relax-NG validation context
10300 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010301 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010302 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010303 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010304 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010305 */
10306static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010307xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10308 xmlRelaxNGDefinePtr define)
10309{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010310 xmlRelaxNGStatesPtr states, res;
10311 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010312
Daniel Veillardfd573f12003-03-16 17:52:32 +000010313 /*
10314 * We should NOT have both ctxt->state and ctxt->states
10315 */
10316 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010317 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10318 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010319 }
10320
10321 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010322 if (ctxt->states != NULL) {
10323 ctxt->state = ctxt->states->tabState[0];
10324 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10325 ctxt->states = NULL;
10326 }
10327 ret = xmlRelaxNGValidateState(ctxt, define);
10328 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10329 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10330 ctxt->state = NULL;
10331 }
10332 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10333 ctxt->state = ctxt->states->tabState[0];
10334 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10335 ctxt->states = NULL;
10336 }
10337 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010338 }
10339
10340 states = ctxt->states;
10341 ctxt->states = NULL;
10342 res = NULL;
10343 j = 0;
10344 oldflags = ctxt->flags;
10345 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010346 for (i = 0; i < states->nbState; i++) {
10347 ctxt->state = states->tabState[i];
10348 ctxt->states = NULL;
10349 ret = xmlRelaxNGValidateState(ctxt, define);
10350 /*
10351 * We should NOT have both ctxt->state and ctxt->states
10352 */
10353 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10354 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10355 ctxt->state = NULL;
10356 }
10357 if (ret == 0) {
10358 if (ctxt->states == NULL) {
10359 if (res != NULL) {
10360 /* add the state to the container */
10361 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10362 ctxt->state = NULL;
10363 } else {
10364 /* add the state directly in states */
10365 states->tabState[j++] = ctxt->state;
10366 ctxt->state = NULL;
10367 }
10368 } else {
10369 if (res == NULL) {
10370 /* make it the new container and copy other results */
10371 res = ctxt->states;
10372 ctxt->states = NULL;
10373 for (k = 0; k < j; k++)
10374 xmlRelaxNGAddStates(ctxt, res,
10375 states->tabState[k]);
10376 } else {
10377 /* add all the new results to res and reff the container */
10378 for (k = 0; k < ctxt->states->nbState; k++)
10379 xmlRelaxNGAddStates(ctxt, res,
10380 ctxt->states->tabState[k]);
10381 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10382 ctxt->states = NULL;
10383 }
10384 }
10385 } else {
10386 if (ctxt->state != NULL) {
10387 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10388 ctxt->state = NULL;
10389 } else if (ctxt->states != NULL) {
10390 for (k = 0; k < ctxt->states->nbState; k++)
10391 xmlRelaxNGFreeValidState(ctxt,
10392 ctxt->states->tabState[k]);
10393 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10394 ctxt->states = NULL;
10395 }
10396 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010397 }
10398 ctxt->flags = oldflags;
10399 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010400 xmlRelaxNGFreeStates(ctxt, states);
10401 ctxt->states = res;
10402 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010403 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010404 states->nbState = j;
10405 ctxt->states = states;
10406 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010407 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010408 ctxt->state = states->tabState[0];
10409 xmlRelaxNGFreeStates(ctxt, states);
10410 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010411 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010412 ret = -1;
10413 xmlRelaxNGFreeStates(ctxt, states);
10414 if (ctxt->states != NULL) {
10415 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10416 ctxt->states = NULL;
10417 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010418 }
10419 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010420 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10421 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010422 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010423 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010424}
10425
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010426/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010427 * xmlRelaxNGValidateDocument:
10428 * @ctxt: a Relax-NG validation context
10429 * @doc: the document
10430 *
10431 * Validate the given document
10432 *
10433 * Returns 0 if the validation succeeded or an error code.
10434 */
10435static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010436xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10437{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010438 int ret;
10439 xmlRelaxNGPtr schema;
10440 xmlRelaxNGGrammarPtr grammar;
10441 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010442 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010443
10444 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010445 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010446
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010447 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010448 schema = ctxt->schema;
10449 grammar = schema->topgrammar;
10450 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010451 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10452 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010453 }
10454 state = xmlRelaxNGNewValidState(ctxt, NULL);
10455 ctxt->state = state;
10456 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010457 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010458 state = ctxt->state;
10459 node = state->seq;
10460 node = xmlRelaxNGSkipIgnored(ctxt, node);
10461 if (node != NULL) {
10462 if (ret != -1) {
10463 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10464 ret = -1;
10465 }
10466 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010467 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010468 int i;
10469 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010470
Daniel Veillard4c004142003-10-07 11:33:24 +000010471 for (i = 0; i < ctxt->states->nbState; i++) {
10472 state = ctxt->states->tabState[i];
10473 node = state->seq;
10474 node = xmlRelaxNGSkipIgnored(ctxt, node);
10475 if (node == NULL)
10476 tmp = 0;
10477 xmlRelaxNGFreeValidState(ctxt, state);
10478 }
10479 if (tmp == -1) {
10480 if (ret != -1) {
10481 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10482 ret = -1;
10483 }
10484 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010485 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010486 if (ctxt->state != NULL) {
10487 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010488 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010489 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010490 if (ret != 0)
10491 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010492#ifdef DEBUG
10493 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010494 ctxt->error(ctxt->userData,
10495 "%d Extra error messages left on stack !\n",
10496 ctxt->errNr);
10497 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010498 }
10499#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010500#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010501 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010502 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010503
Daniel Veillard4c004142003-10-07 11:33:24 +000010504 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10505 vctxt.valid = 1;
10506 vctxt.error = ctxt->error;
10507 vctxt.warning = ctxt->warning;
10508 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010509
Daniel Veillard4c004142003-10-07 11:33:24 +000010510 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10511 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010512 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010513#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010514 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010515 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010516
Daniel Veillard4c004142003-10-07 11:33:24 +000010517 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010518}
10519
Daniel Veillardfd573f12003-03-16 17:52:32 +000010520/************************************************************************
10521 * *
10522 * Validation interfaces *
10523 * *
10524 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010525
Daniel Veillard6eadf632003-01-23 18:29:16 +000010526/**
10527 * xmlRelaxNGNewValidCtxt:
10528 * @schema: a precompiled XML RelaxNGs
10529 *
10530 * Create an XML RelaxNGs validation context based on the given schema
10531 *
10532 * Returns the validation context or NULL in case of error
10533 */
10534xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010535xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10536{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010537 xmlRelaxNGValidCtxtPtr ret;
10538
10539 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10540 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010541 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010542 return (NULL);
10543 }
10544 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10545 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010546 ret->error = xmlGenericError;
10547 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010548 ret->errNr = 0;
10549 ret->errMax = 0;
10550 ret->err = NULL;
10551 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010552 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010553 ret->states = NULL;
10554 ret->freeState = NULL;
10555 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010556 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010557 return (ret);
10558}
10559
10560/**
10561 * xmlRelaxNGFreeValidCtxt:
10562 * @ctxt: the schema validation context
10563 *
10564 * Free the resources associated to the schema validation context
10565 */
10566void
Daniel Veillard4c004142003-10-07 11:33:24 +000010567xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10568{
Daniel Veillard798024a2003-03-19 10:36:09 +000010569 int k;
10570
Daniel Veillard6eadf632003-01-23 18:29:16 +000010571 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010572 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010573 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010574 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010575 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010576 for (k = 0; k < ctxt->freeState->nbState; k++) {
10577 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10578 }
10579 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010580 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010581 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010582 for (k = 0; k < ctxt->freeStatesNr; k++) {
10583 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10584 }
10585 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010586 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010587 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010588 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010589 if (ctxt->elemTab != NULL) {
10590 xmlRegExecCtxtPtr exec;
10591
Daniel Veillard4c004142003-10-07 11:33:24 +000010592 exec = xmlRelaxNGElemPop(ctxt);
10593 while (exec != NULL) {
10594 xmlRegFreeExecCtxt(exec);
10595 exec = xmlRelaxNGElemPop(ctxt);
10596 }
10597 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010598 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010599 xmlFree(ctxt);
10600}
10601
10602/**
10603 * xmlRelaxNGSetValidErrors:
10604 * @ctxt: a Relax-NG validation context
10605 * @err: the error function
10606 * @warn: the warning function
10607 * @ctx: the functions context
10608 *
10609 * Set the error and warning callback informations
10610 */
10611void
10612xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010613 xmlRelaxNGValidityErrorFunc err,
10614 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10615{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010616 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010617 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010618 ctxt->error = err;
10619 ctxt->warning = warn;
10620 ctxt->userData = ctx;
10621}
10622
10623/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010624 * xmlRelaxNGGetValidErrors:
10625 * @ctxt: a Relax-NG validation context
10626 * @err: the error function result
10627 * @warn: the warning function result
10628 * @ctx: the functions context result
10629 *
10630 * Get the error and warning callback informations
10631 *
10632 * Returns -1 in case of error and 0 otherwise
10633 */
10634int
10635xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010636 xmlRelaxNGValidityErrorFunc * err,
10637 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10638{
Daniel Veillard409a8142003-07-18 15:16:57 +000010639 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010640 return (-1);
10641 if (err != NULL)
10642 *err = ctxt->error;
10643 if (warn != NULL)
10644 *warn = ctxt->warning;
10645 if (ctx != NULL)
10646 *ctx = ctxt->userData;
10647 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010648}
10649
10650/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010651 * xmlRelaxNGValidateDoc:
10652 * @ctxt: a Relax-NG validation context
10653 * @doc: a parsed document tree
10654 *
10655 * Validate a document tree in memory.
10656 *
10657 * Returns 0 if the document is valid, a positive error code
10658 * number otherwise and -1 in case of internal or API error.
10659 */
10660int
Daniel Veillard4c004142003-10-07 11:33:24 +000010661xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10662{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010663 int ret;
10664
10665 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010666 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010667
10668 ctxt->doc = doc;
10669
10670 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010671 /*
10672 * TODO: build error codes
10673 */
10674 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010675 return (1);
10676 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010677}
10678
10679#endif /* LIBXML_SCHEMAS_ENABLED */