blob: 7e92057d7b927374da716af2d47de0041072121e [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 Veillard42595322004-11-08 10:52:06 +0000251 int freedoc; /* need to free the document */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000252};
253
254#define FLAGS_IGNORABLE 1
255#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000256#define FLAGS_MIXED_CONTENT 4
Daniel Veillardb30ca312005-09-04 13:50:03 +0000257#define FLAGS_NOERROR 8
Daniel Veillard6eadf632003-01-23 18:29:16 +0000258
259/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000260 * xmlRelaxNGInterleaveGroup:
261 *
262 * A RelaxNGs partition set associated to lists of definitions
263 */
264typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
265typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
266struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000267 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
268 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
269 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000270};
271
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000272#define IS_DETERMINIST 1
273#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000274
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000275/**
276 * xmlRelaxNGPartitions:
277 *
278 * A RelaxNGs partition associated to an interleave group
279 */
280typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
281typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
282struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000283 int nbgroups; /* number of groups in the partitions */
284 xmlHashTablePtr triage; /* hash table used to direct nodes to the
285 * right group when possible */
286 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000287 xmlRelaxNGInterleaveGroupPtr *groups;
288};
289
290/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000291 * xmlRelaxNGValidState:
292 *
293 * A RelaxNGs validation state
294 */
295#define MAX_ATTR 20
296typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
297typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
298struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000299 xmlNodePtr node; /* the current node */
300 xmlNodePtr seq; /* the sequence of children left to validate */
301 int nbAttrs; /* the number of attributes */
302 int maxAttrs; /* the size of attrs */
303 int nbAttrLeft; /* the number of attributes left to validate */
304 xmlChar *value; /* the value when operating on string */
305 xmlChar *endvalue; /* the end value when operating on string */
306 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000307};
308
309/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000310 * xmlRelaxNGStates:
311 *
312 * A RelaxNGs container for validation state
313 */
314typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
315typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
316struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000317 int nbState; /* the number of states */
318 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000319 xmlRelaxNGValidStatePtr *tabState;
320};
321
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000322#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000323
Daniel Veillardfd573f12003-03-16 17:52:32 +0000324/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000325 * xmlRelaxNGValidError:
326 *
327 * A RelaxNGs validation error
328 */
329typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
330typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
331struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000332 xmlRelaxNGValidErr err; /* the error number */
333 int flags; /* flags */
334 xmlNodePtr node; /* the current node */
335 xmlNodePtr seq; /* the current child */
336 const xmlChar *arg1; /* first arg */
337 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000338};
339
340/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000341 * xmlRelaxNGValidCtxt:
342 *
343 * A RelaxNGs validation context
344 */
345
346struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000347 void *userData; /* user specific data block */
348 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
349 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000350 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000351 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000352
Daniel Veillard4c004142003-10-07 11:33:24 +0000353 xmlRelaxNGPtr schema; /* The schema in use */
354 xmlDocPtr doc; /* the document being validated */
355 int flags; /* validation flags */
356 int depth; /* validation depth */
357 int idref; /* requires idref checking */
358 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000359
360 /*
361 * Errors accumulated in branches may have to be stacked to be
362 * provided back when it's sure they affect validation.
363 */
364 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000365 int errNr; /* Depth of the error stack */
366 int errMax; /* Max depth of the error stack */
367 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000368
Daniel Veillard4c004142003-10-07 11:33:24 +0000369 xmlRelaxNGValidStatePtr state; /* the current validation state */
370 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000371
Daniel Veillard4c004142003-10-07 11:33:24 +0000372 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
373 int freeStatesNr;
374 int freeStatesMax;
375 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000376
377 /*
378 * This is used for "progressive" validation
379 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000380 xmlRegExecCtxtPtr elem; /* the current element regexp */
381 int elemNr; /* the number of element validated */
382 int elemMax; /* the max depth of elements */
383 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
384 int pstate; /* progressive state */
385 xmlNodePtr pnode; /* the current node */
386 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
387 int perr; /* signal error in content model
388 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000389};
390
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000391/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000392 * xmlRelaxNGInclude:
393 *
394 * Structure associated to a RelaxNGs document element
395 */
396struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000397 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
398 xmlChar *href; /* the normalized href value */
399 xmlDocPtr doc; /* the associated XML document */
400 xmlRelaxNGDefinePtr content; /* the definitions */
401 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000402};
403
404/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000405 * xmlRelaxNGDocument:
406 *
407 * Structure associated to a RelaxNGs document element
408 */
409struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000410 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000411 xmlChar *href; /* the normalized href value */
412 xmlDocPtr doc; /* the associated XML document */
413 xmlRelaxNGDefinePtr content; /* the definitions */
414 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000415};
416
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000417
Daniel Veillard6eadf632003-01-23 18:29:16 +0000418/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000419 * *
420 * Some factorized error routines *
421 * *
422 ************************************************************************/
423
424/**
425 * xmlRngPErrMemory:
426 * @ctxt: an Relax-NG parser context
427 * @extra: extra informations
428 *
429 * Handle a redefinition of attribute error
430 */
431static void
432xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
433{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000434 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000435 xmlGenericErrorFunc channel = NULL;
436 void *data = NULL;
437
438 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000439 if (ctxt->serror != NULL)
440 schannel = ctxt->serror;
441 else
442 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000443 data = ctxt->userData;
444 ctxt->nbErrors++;
445 }
446 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000447 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000448 NULL, NULL, XML_FROM_RELAXNGP,
449 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
450 NULL, NULL, 0, 0,
451 "Memory allocation failed : %s\n", extra);
452 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000453 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000454 NULL, NULL, XML_FROM_RELAXNGP,
455 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
456 NULL, NULL, 0, 0, "Memory allocation failed\n");
457}
458
459/**
460 * xmlRngVErrMemory:
461 * @ctxt: a Relax-NG validation context
462 * @extra: extra informations
463 *
464 * Handle a redefinition of attribute error
465 */
466static void
467xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
468{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000469 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000470 xmlGenericErrorFunc channel = NULL;
471 void *data = NULL;
472
473 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000474 if (ctxt->serror != NULL)
475 schannel = ctxt->serror;
476 else
477 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000478 data = ctxt->userData;
479 ctxt->nbErrors++;
480 }
481 if (extra)
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, extra,
485 NULL, NULL, 0, 0,
486 "Memory allocation failed : %s\n", extra);
487 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000488 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000489 NULL, NULL, XML_FROM_RELAXNGV,
490 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
491 NULL, NULL, 0, 0, "Memory allocation failed\n");
492}
493
494/**
495 * xmlRngPErr:
496 * @ctxt: a Relax-NG parser context
497 * @node: the node raising the error
498 * @error: the error code
499 * @msg: message
500 * @str1: extra info
501 * @str2: extra info
502 *
503 * Handle a Relax NG Parsing error
504 */
505static void
506xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
507 const char *msg, const xmlChar * str1, const xmlChar * str2)
508{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000509 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000510 xmlGenericErrorFunc channel = NULL;
511 void *data = NULL;
512
513 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000514 if (ctxt->serror != NULL)
515 schannel = ctxt->serror;
516 else
517 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000518 data = ctxt->userData;
519 ctxt->nbErrors++;
520 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000521 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000522 NULL, node, XML_FROM_RELAXNGP,
523 error, XML_ERR_ERROR, NULL, 0,
524 (const char *) str1, (const char *) str2, NULL, 0, 0,
525 msg, str1, str2);
526}
527
528/**
529 * xmlRngVErr:
530 * @ctxt: a Relax-NG validation context
531 * @node: the node raising the error
532 * @error: the error code
533 * @msg: message
534 * @str1: extra info
535 * @str2: extra info
536 *
537 * Handle a Relax NG Validation error
538 */
539static void
540xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
541 const char *msg, const xmlChar * str1, const xmlChar * str2)
542{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000543 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000544 xmlGenericErrorFunc channel = NULL;
545 void *data = NULL;
546
547 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000548 if (ctxt->serror != NULL)
549 schannel = ctxt->serror;
550 else
551 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000552 data = ctxt->userData;
553 ctxt->nbErrors++;
554 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000555 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000556 NULL, node, XML_FROM_RELAXNGV,
557 error, XML_ERR_ERROR, NULL, 0,
558 (const char *) str1, (const char *) str2, NULL, 0, 0,
559 msg, str1, str2);
560}
561
562/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000563 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000564 * Preliminary type checking interfaces *
565 * *
566 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000567
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000568/**
569 * xmlRelaxNGTypeHave:
570 * @data: data needed for the library
571 * @type: the type name
572 * @value: the value to check
573 *
574 * Function provided by a type library to check if a type is exported
575 *
576 * Returns 1 if yes, 0 if no and -1 in case of error.
577 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000578typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000579
580/**
581 * xmlRelaxNGTypeCheck:
582 * @data: data needed for the library
583 * @type: the type name
584 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000585 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000586 *
587 * Function provided by a type library to check if a value match a type
588 *
589 * Returns 1 if yes, 0 if no and -1 in case of error.
590 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000591typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
592 const xmlChar * value, void **result,
593 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000594
595/**
596 * xmlRelaxNGFacetCheck:
597 * @data: data needed for the library
598 * @type: the type name
599 * @facet: the facet name
600 * @val: the facet value
601 * @strval: the string value
602 * @value: the value to check
603 *
604 * Function provided by a type library to check a value facet
605 *
606 * Returns 1 if yes, 0 if no and -1 in case of error.
607 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000608typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
609 const xmlChar * facet,
610 const xmlChar * val,
611 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000612
613/**
614 * xmlRelaxNGTypeFree:
615 * @data: data needed for the library
616 * @result: the value to free
617 *
618 * Function provided by a type library to free a returned result
619 */
620typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000621
622/**
623 * xmlRelaxNGTypeCompare:
624 * @data: data needed for the library
625 * @type: the type name
626 * @value1: the first value
627 * @value2: the second value
628 *
629 * Function provided by a type library to compare two values accordingly
630 * to a type.
631 *
632 * Returns 1 if yes, 0 if no and -1 in case of error.
633 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000634typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
635 const xmlChar * value1,
636 xmlNodePtr ctxt1,
637 void *comp1,
638 const xmlChar * value2,
639 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000640typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
641typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
642struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000643 const xmlChar *namespace; /* the datatypeLibrary value */
644 void *data; /* data needed for the library */
645 xmlRelaxNGTypeHave have; /* the export function */
646 xmlRelaxNGTypeCheck check; /* the checking function */
647 xmlRelaxNGTypeCompare comp; /* the compare function */
648 xmlRelaxNGFacetCheck facet; /* the facet check function */
649 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000650};
651
652/************************************************************************
653 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000654 * Allocation functions *
655 * *
656 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000657static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
658static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000659static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000660static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000661static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
662 ATTRIBUTE_UNUSED,
663 xmlRelaxNGValidStatePtr state1,
664 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000665static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000666 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000667
668/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000669 * xmlRelaxNGFreeDocument:
670 * @docu: a document structure
671 *
672 * Deallocate a RelaxNG document structure.
673 */
674static void
675xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
676{
677 if (docu == NULL)
678 return;
679
680 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000681 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000682 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000683 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000684 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000685 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000686 xmlFree(docu);
687}
688
689/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000690 * xmlRelaxNGFreeDocumentList:
691 * @docu: a list of document structure
692 *
693 * Deallocate a RelaxNG document structures.
694 */
695static void
696xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
697{
698 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000699
Daniel Veillardc482e262003-02-26 14:48:48 +0000700 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000701 next = docu->next;
702 xmlRelaxNGFreeDocument(docu);
703 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000704 }
705}
706
707/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000708 * xmlRelaxNGFreeInclude:
709 * @incl: a include structure
710 *
711 * Deallocate a RelaxNG include structure.
712 */
713static void
714xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
715{
716 if (incl == NULL)
717 return;
718
719 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000720 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000721 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000722 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000723 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000724 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000725 xmlFree(incl);
726}
727
728/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000729 * xmlRelaxNGFreeIncludeList:
730 * @incl: a include structure list
731 *
732 * Deallocate a RelaxNG include structure.
733 */
734static void
735xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
736{
737 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000738
Daniel Veillardc482e262003-02-26 14:48:48 +0000739 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000740 next = incl->next;
741 xmlRelaxNGFreeInclude(incl);
742 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000743 }
744}
745
746/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000747 * xmlRelaxNGNewRelaxNG:
748 * @ctxt: a Relax-NG validation context (optional)
749 *
750 * Allocate a new RelaxNG structure.
751 *
752 * Returns the newly allocated structure or NULL in case or error
753 */
754static xmlRelaxNGPtr
755xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
756{
757 xmlRelaxNGPtr ret;
758
759 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
760 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000761 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000762 return (NULL);
763 }
764 memset(ret, 0, sizeof(xmlRelaxNG));
765
766 return (ret);
767}
768
769/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000770 * xmlRelaxNGFreeInnerSchema:
771 * @schema: a schema structure
772 *
773 * Deallocate a RelaxNG schema structure.
774 */
775static void
776xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
777{
778 if (schema == NULL)
779 return;
780
781 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000782 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000783 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000784 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000785
Daniel Veillard4c004142003-10-07 11:33:24 +0000786 for (i = 0; i < schema->defNr; i++)
787 xmlRelaxNGFreeDefine(schema->defTab[i]);
788 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000789 }
790
791 xmlFree(schema);
792}
793
794/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000795 * xmlRelaxNGFree:
796 * @schema: a schema structure
797 *
798 * Deallocate a RelaxNG structure.
799 */
800void
801xmlRelaxNGFree(xmlRelaxNGPtr schema)
802{
803 if (schema == NULL)
804 return;
805
Daniel Veillard6eadf632003-01-23 18:29:16 +0000806 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000807 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000808 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000809 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000810 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000811 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000812 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000813 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000814 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000815 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000816
Daniel Veillard4c004142003-10-07 11:33:24 +0000817 for (i = 0; i < schema->defNr; i++)
818 xmlRelaxNGFreeDefine(schema->defTab[i]);
819 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000820 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000821
822 xmlFree(schema);
823}
824
825/**
826 * xmlRelaxNGNewGrammar:
827 * @ctxt: a Relax-NG validation context (optional)
828 *
829 * Allocate a new RelaxNG grammar.
830 *
831 * Returns the newly allocated structure or NULL in case or error
832 */
833static xmlRelaxNGGrammarPtr
834xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
835{
836 xmlRelaxNGGrammarPtr ret;
837
838 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
839 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000840 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000841 return (NULL);
842 }
843 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
844
845 return (ret);
846}
847
848/**
849 * xmlRelaxNGFreeGrammar:
850 * @grammar: a grammar structure
851 *
852 * Deallocate a RelaxNG grammar structure.
853 */
854static void
855xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
856{
857 if (grammar == NULL)
858 return;
859
Daniel Veillardc482e262003-02-26 14:48:48 +0000860 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000861 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000862 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000863 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000864 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000865 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000866 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000867 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000868 }
869 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000870 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000871 }
872
873 xmlFree(grammar);
874}
875
876/**
877 * xmlRelaxNGNewDefine:
878 * @ctxt: a Relax-NG validation context
879 * @node: the node in the input document.
880 *
881 * Allocate a new RelaxNG define.
882 *
883 * Returns the newly allocated structure or NULL in case or error
884 */
885static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000886xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000887{
888 xmlRelaxNGDefinePtr ret;
889
Daniel Veillard419a7682003-02-03 23:22:49 +0000890 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000891 ctxt->defMax = 16;
892 ctxt->defNr = 0;
893 ctxt->defTab = (xmlRelaxNGDefinePtr *)
894 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
895 if (ctxt->defTab == NULL) {
896 xmlRngPErrMemory(ctxt, "allocating define\n");
897 return (NULL);
898 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000899 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000900 xmlRelaxNGDefinePtr *tmp;
901
902 ctxt->defMax *= 2;
903 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
904 ctxt->defMax *
905 sizeof
906 (xmlRelaxNGDefinePtr));
907 if (tmp == NULL) {
908 xmlRngPErrMemory(ctxt, "allocating define\n");
909 return (NULL);
910 }
911 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000912 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000913 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
914 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000915 xmlRngPErrMemory(ctxt, "allocating define\n");
916 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000917 }
918 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000919 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000920 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000921 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000922 return (ret);
923}
924
925/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000926 * xmlRelaxNGFreePartition:
927 * @partitions: a partition set structure
928 *
929 * Deallocate RelaxNG partition set structures.
930 */
931static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000932xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
933{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000934 xmlRelaxNGInterleaveGroupPtr group;
935 int j;
936
937 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000938 if (partitions->groups != NULL) {
939 for (j = 0; j < partitions->nbgroups; j++) {
940 group = partitions->groups[j];
941 if (group != NULL) {
942 if (group->defs != NULL)
943 xmlFree(group->defs);
944 if (group->attrs != NULL)
945 xmlFree(group->attrs);
946 xmlFree(group);
947 }
948 }
949 xmlFree(partitions->groups);
950 }
951 if (partitions->triage != NULL) {
952 xmlHashFree(partitions->triage, NULL);
953 }
954 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000955 }
956}
Daniel Veillard4c004142003-10-07 11:33:24 +0000957
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000958/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000959 * xmlRelaxNGFreeDefine:
960 * @define: a define structure
961 *
962 * Deallocate a RelaxNG define structure.
963 */
964static void
965xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
966{
967 if (define == NULL)
968 return;
969
Daniel Veillard4c004142003-10-07 11:33:24 +0000970 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
971 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000972
Daniel Veillard4c004142003-10-07 11:33:24 +0000973 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
974 if ((lib != NULL) && (lib->freef != NULL))
975 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000976 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000977 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
978 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
979 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
980 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000981 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000982 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000983 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000984 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000985 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000986 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000987 if (define->contModel != NULL)
988 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000989 xmlFree(define);
990}
991
992/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000993 * xmlRelaxNGNewStates:
994 * @ctxt: a Relax-NG validation context
995 * @size: the default size for the container
996 *
997 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000998 *
999 * Returns the newly allocated structure or NULL in case or error
1000 */
1001static xmlRelaxNGStatesPtr
1002xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1003{
1004 xmlRelaxNGStatesPtr ret;
1005
Daniel Veillard798024a2003-03-19 10:36:09 +00001006 if ((ctxt != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001007 (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) {
1008 ctxt->freeStatesNr--;
1009 ret = ctxt->freeStates[ctxt->freeStatesNr];
1010 ret->nbState = 0;
1011 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001012 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001013 if (size < 16)
1014 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001015
1016 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001017 (size -
1018 1) *
1019 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001020 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001021 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001022 return (NULL);
1023 }
1024 ret->nbState = 0;
1025 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001026 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1027 sizeof
1028 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001029 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001030 xmlRngVErrMemory(ctxt, "allocating states\n");
1031 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001032 return (NULL);
1033 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001034 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001035}
1036
1037/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001038 * xmlRelaxNGAddStateUniq:
1039 * @ctxt: a Relax-NG validation context
1040 * @states: the states container
1041 * @state: the validation state
1042 *
1043 * Add a RelaxNG validation state to the container without checking
1044 * for unicity.
1045 *
1046 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1047 */
1048static int
1049xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001050 xmlRelaxNGStatesPtr states,
1051 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001052{
1053 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001054 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001055 }
1056 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001057 xmlRelaxNGValidStatePtr *tmp;
1058 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001059
Daniel Veillard4c004142003-10-07 11:33:24 +00001060 size = states->maxState * 2;
1061 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1062 (size) *
1063 sizeof
1064 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001065 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001066 xmlRngVErrMemory(ctxt, "adding states\n");
1067 return (-1);
1068 }
1069 states->tabState = tmp;
1070 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001071 }
1072 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001073 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001074}
1075
1076/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001077 * xmlRelaxNGAddState:
1078 * @ctxt: a Relax-NG validation context
1079 * @states: the states container
1080 * @state: the validation state
1081 *
1082 * Add a RelaxNG validation state to the container
1083 *
1084 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1085 */
1086static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001087xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1088 xmlRelaxNGStatesPtr states,
1089 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001090{
1091 int i;
1092
1093 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001094 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001095 }
1096 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001097 xmlRelaxNGValidStatePtr *tmp;
1098 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001099
Daniel Veillard4c004142003-10-07 11:33:24 +00001100 size = states->maxState * 2;
1101 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1102 (size) *
1103 sizeof
1104 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001105 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001106 xmlRngVErrMemory(ctxt, "adding states\n");
1107 return (-1);
1108 }
1109 states->tabState = tmp;
1110 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001111 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001112 for (i = 0; i < states->nbState; i++) {
1113 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1114 xmlRelaxNGFreeValidState(ctxt, state);
1115 return (0);
1116 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001117 }
1118 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001119 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001120}
1121
1122/**
1123 * xmlRelaxNGFreeStates:
1124 * @ctxt: a Relax-NG validation context
1125 * @states: teh container
1126 *
1127 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001128 */
1129static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001130xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001131 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001132{
Daniel Veillard798024a2003-03-19 10:36:09 +00001133 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001134 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001135 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001136 ctxt->freeStatesMax = 40;
1137 ctxt->freeStatesNr = 0;
1138 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1139 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1140 if (ctxt->freeStates == NULL) {
1141 xmlRngVErrMemory(ctxt, "storing states\n");
1142 }
1143 } else if ((ctxt != NULL)
1144 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1145 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001146
Daniel Veillard4c004142003-10-07 11:33:24 +00001147 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1148 2 * ctxt->freeStatesMax *
1149 sizeof
1150 (xmlRelaxNGStatesPtr));
1151 if (tmp == NULL) {
1152 xmlRngVErrMemory(ctxt, "storing states\n");
1153 xmlFree(states->tabState);
1154 xmlFree(states);
1155 return;
1156 }
1157 ctxt->freeStates = tmp;
1158 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001159 }
1160 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001161 xmlFree(states->tabState);
1162 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001163 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001164 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001165 }
1166}
1167
1168/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001169 * xmlRelaxNGNewValidState:
1170 * @ctxt: a Relax-NG validation context
1171 * @node: the current node or NULL for the document
1172 *
1173 * Allocate a new RelaxNG validation state
1174 *
1175 * Returns the newly allocated structure or NULL in case or error
1176 */
1177static xmlRelaxNGValidStatePtr
1178xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1179{
1180 xmlRelaxNGValidStatePtr ret;
1181 xmlAttrPtr attr;
1182 xmlAttrPtr attrs[MAX_ATTR];
1183 int nbAttrs = 0;
1184 xmlNodePtr root = NULL;
1185
1186 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001187 root = xmlDocGetRootElement(ctxt->doc);
1188 if (root == NULL)
1189 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001190 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001191 attr = node->properties;
1192 while (attr != NULL) {
1193 if (nbAttrs < MAX_ATTR)
1194 attrs[nbAttrs++] = attr;
1195 else
1196 nbAttrs++;
1197 attr = attr->next;
1198 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001199 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001200 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1201 ctxt->freeState->nbState--;
1202 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001203 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001204 ret =
1205 (xmlRelaxNGValidStatePtr)
1206 xmlMalloc(sizeof(xmlRelaxNGValidState));
1207 if (ret == NULL) {
1208 xmlRngVErrMemory(ctxt, "allocating states\n");
1209 return (NULL);
1210 }
1211 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001212 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001213 ret->value = NULL;
1214 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001215 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001216 ret->node = (xmlNodePtr) ctxt->doc;
1217 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001218 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001219 ret->node = node;
1220 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001221 }
1222 ret->nbAttrs = 0;
1223 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001224 if (ret->attrs == NULL) {
1225 if (nbAttrs < 4)
1226 ret->maxAttrs = 4;
1227 else
1228 ret->maxAttrs = nbAttrs;
1229 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1230 sizeof(xmlAttrPtr));
1231 if (ret->attrs == NULL) {
1232 xmlRngVErrMemory(ctxt, "allocating states\n");
1233 return (ret);
1234 }
1235 } else if (ret->maxAttrs < nbAttrs) {
1236 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001237
Daniel Veillard4c004142003-10-07 11:33:24 +00001238 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1239 sizeof(xmlAttrPtr));
1240 if (tmp == NULL) {
1241 xmlRngVErrMemory(ctxt, "allocating states\n");
1242 return (ret);
1243 }
1244 ret->attrs = tmp;
1245 ret->maxAttrs = nbAttrs;
1246 }
1247 ret->nbAttrs = nbAttrs;
1248 if (nbAttrs < MAX_ATTR) {
1249 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1250 } else {
1251 attr = node->properties;
1252 nbAttrs = 0;
1253 while (attr != NULL) {
1254 ret->attrs[nbAttrs++] = attr;
1255 attr = attr->next;
1256 }
1257 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001258 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001259 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001260 return (ret);
1261}
1262
1263/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001264 * xmlRelaxNGCopyValidState:
1265 * @ctxt: a Relax-NG validation context
1266 * @state: a validation state
1267 *
1268 * Copy the validation state
1269 *
1270 * Returns the newly allocated structure or NULL in case or error
1271 */
1272static xmlRelaxNGValidStatePtr
1273xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001274 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001275{
1276 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001277 unsigned int maxAttrs;
1278 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001279
1280 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001281 return (NULL);
1282 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1283 ctxt->freeState->nbState--;
1284 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001285 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001286 ret =
1287 (xmlRelaxNGValidStatePtr)
1288 xmlMalloc(sizeof(xmlRelaxNGValidState));
1289 if (ret == NULL) {
1290 xmlRngVErrMemory(ctxt, "allocating states\n");
1291 return (NULL);
1292 }
1293 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001294 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001295 attrs = ret->attrs;
1296 maxAttrs = ret->maxAttrs;
1297 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1298 ret->attrs = attrs;
1299 ret->maxAttrs = maxAttrs;
1300 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001301 if (ret->attrs == NULL) {
1302 ret->maxAttrs = state->maxAttrs;
1303 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1304 sizeof(xmlAttrPtr));
1305 if (ret->attrs == NULL) {
1306 xmlRngVErrMemory(ctxt, "allocating states\n");
1307 ret->nbAttrs = 0;
1308 return (ret);
1309 }
1310 } else if (ret->maxAttrs < state->nbAttrs) {
1311 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001312
Daniel Veillard4c004142003-10-07 11:33:24 +00001313 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1314 sizeof(xmlAttrPtr));
1315 if (tmp == NULL) {
1316 xmlRngVErrMemory(ctxt, "allocating states\n");
1317 ret->nbAttrs = 0;
1318 return (ret);
1319 }
1320 ret->maxAttrs = state->maxAttrs;
1321 ret->attrs = tmp;
1322 }
1323 memcpy(ret->attrs, state->attrs,
1324 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001325 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001326 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001327}
1328
1329/**
1330 * xmlRelaxNGEqualValidState:
1331 * @ctxt: a Relax-NG validation context
1332 * @state1: a validation state
1333 * @state2: a validation state
1334 *
1335 * Compare the validation states for equality
1336 *
1337 * Returns 1 if equald, 0 otherwise
1338 */
1339static int
1340xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001341 xmlRelaxNGValidStatePtr state1,
1342 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001343{
1344 int i;
1345
1346 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001347 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001348 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001349 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001350 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001351 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001352 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001353 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001354 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001355 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001356 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001357 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001358 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001359 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001360 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001361 (!xmlStrEqual(state1->value, state2->value)))
1362 return (0);
1363 for (i = 0; i < state1->nbAttrs; i++) {
1364 if (state1->attrs[i] != state2->attrs[i])
1365 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001366 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001367 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001368}
1369
1370/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001371 * xmlRelaxNGFreeValidState:
1372 * @state: a validation state structure
1373 *
1374 * Deallocate a RelaxNG validation state structure.
1375 */
1376static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001377xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001378 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001379{
1380 if (state == NULL)
1381 return;
1382
Daniel Veillard798024a2003-03-19 10:36:09 +00001383 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001384 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001385 }
1386 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001387 if (state->attrs != NULL)
1388 xmlFree(state->attrs);
1389 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001390 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001391 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001392 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001393}
1394
1395/************************************************************************
1396 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001397 * Semi internal functions *
1398 * *
1399 ************************************************************************/
1400
1401/**
1402 * xmlRelaxParserSetFlag:
1403 * @ctxt: a RelaxNG parser context
1404 * @flags: a set of flags values
1405 *
1406 * Semi private function used to pass informations to a parser context
1407 * which are a combination of xmlRelaxNGParserFlag .
1408 *
1409 * Returns 0 if success and -1 in case of error
1410 */
1411int
1412xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1413{
1414 if (ctxt == NULL) return(-1);
1415 if (flags & XML_RELAXNGP_FREE_DOC) {
1416 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1417 flags -= XML_RELAXNGP_FREE_DOC;
1418 }
1419 if (flags & XML_RELAXNGP_CRNG) {
1420 ctxt->crng |= XML_RELAXNGP_CRNG;
1421 flags -= XML_RELAXNGP_CRNG;
1422 }
1423 if (flags != 0) return(-1);
1424 return(0);
1425}
1426
1427/************************************************************************
1428 * *
1429 * Document functions *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001430 * *
1431 ************************************************************************/
1432static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001433 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001434
1435/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001436 * xmlRelaxNGIncludePush:
1437 * @ctxt: the parser context
1438 * @value: the element doc
1439 *
1440 * Pushes a new include on top of the include stack
1441 *
1442 * Returns 0 in case of error, the index in the stack otherwise
1443 */
1444static int
1445xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001446 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001447{
1448 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001449 ctxt->incMax = 4;
1450 ctxt->incNr = 0;
1451 ctxt->incTab =
1452 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1453 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001454 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001455 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001456 return (0);
1457 }
1458 }
1459 if (ctxt->incNr >= ctxt->incMax) {
1460 ctxt->incMax *= 2;
1461 ctxt->incTab =
1462 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001463 ctxt->incMax *
1464 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001465 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001466 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001467 return (0);
1468 }
1469 }
1470 ctxt->incTab[ctxt->incNr] = value;
1471 ctxt->inc = value;
1472 return (ctxt->incNr++);
1473}
1474
1475/**
1476 * xmlRelaxNGIncludePop:
1477 * @ctxt: the parser context
1478 *
1479 * Pops the top include from the include stack
1480 *
1481 * Returns the include just removed
1482 */
1483static xmlRelaxNGIncludePtr
1484xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1485{
1486 xmlRelaxNGIncludePtr ret;
1487
1488 if (ctxt->incNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001489 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001490 ctxt->incNr--;
1491 if (ctxt->incNr > 0)
1492 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1493 else
1494 ctxt->inc = NULL;
1495 ret = ctxt->incTab[ctxt->incNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001496 ctxt->incTab[ctxt->incNr] = NULL;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001497 return (ret);
1498}
1499
1500/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001501 * xmlRelaxNGRemoveRedefine:
1502 * @ctxt: the parser context
1503 * @URL: the normalized URL
1504 * @target: the included target
1505 * @name: the define name to eliminate
1506 *
1507 * Applies the elimination algorithm of 4.7
1508 *
1509 * Returns 0 in case of error, 1 in case of success.
1510 */
1511static int
1512xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001513 const xmlChar * URL ATTRIBUTE_UNUSED,
1514 xmlNodePtr target, const xmlChar * name)
1515{
Daniel Veillard5add8682003-03-10 13:13:58 +00001516 int found = 0;
1517 xmlNodePtr tmp, tmp2;
1518 xmlChar *name2;
1519
1520#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001521 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001522 xmlGenericError(xmlGenericErrorContext,
1523 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001524 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001525 xmlGenericError(xmlGenericErrorContext,
1526 "Elimination of <include> define %s from %s\n",
1527 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001528#endif
1529 tmp = target;
1530 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001531 tmp2 = tmp->next;
1532 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1533 found = 1;
1534 xmlUnlinkNode(tmp);
1535 xmlFreeNode(tmp);
1536 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1537 name2 = xmlGetProp(tmp, BAD_CAST "name");
1538 xmlRelaxNGNormExtSpace(name2);
1539 if (name2 != NULL) {
1540 if (xmlStrEqual(name, name2)) {
1541 found = 1;
1542 xmlUnlinkNode(tmp);
1543 xmlFreeNode(tmp);
1544 }
1545 xmlFree(name2);
1546 }
1547 } else if (IS_RELAXNG(tmp, "include")) {
1548 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001549 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001550
Daniel Veillard4c004142003-10-07 11:33:24 +00001551 if ((inc != NULL) && (inc->doc != NULL) &&
1552 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001553
Daniel Veillard4c004142003-10-07 11:33:24 +00001554 if (xmlStrEqual
1555 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001556#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001557 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001558#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001559 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1560 inc->doc->children->
1561 children, name) == 1) {
1562 found = 1;
1563 }
1564 if (href != NULL)
1565 xmlFree(href);
1566 }
1567 }
1568 }
1569 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001570 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001571 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001572}
1573
1574/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001575 * xmlRelaxNGLoadInclude:
1576 * @ctxt: the parser context
1577 * @URL: the normalized URL
1578 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001579 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001580 *
1581 * First lookup if the document is already loaded into the parser context,
1582 * check against recursion. If not found the resource is loaded and
1583 * the content is preprocessed before being returned back to the caller.
1584 *
1585 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1586 */
1587static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001588xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1589 xmlNodePtr node, const xmlChar * ns)
1590{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001591 xmlRelaxNGIncludePtr ret = NULL;
1592 xmlDocPtr doc;
1593 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001594 xmlNodePtr root, cur;
1595
1596#ifdef DEBUG_INCLUDE
1597 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001598 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001599#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001600
1601 /*
1602 * check against recursion in the stack
1603 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001604 for (i = 0; i < ctxt->incNr; i++) {
1605 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1606 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1607 "Detected an Include recursion for %s\n", URL,
1608 NULL);
1609 return (NULL);
1610 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001611 }
1612
1613 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001614 * load the document
1615 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001616 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001617 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001618 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1619 "xmlRelaxNG: could not load %s\n", URL, NULL);
1620 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001621 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001622#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001623 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001624#endif
1625
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001626 /*
1627 * Allocate the document structures and register it first.
1628 */
1629 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1630 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001631 xmlRngPErrMemory(ctxt, "allocating include\n");
1632 xmlFreeDoc(doc);
1633 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001634 }
1635 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1636 ret->doc = doc;
1637 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001638 ret->next = ctxt->includes;
1639 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001640
1641 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001642 * transmit the ns if needed
1643 */
1644 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001645 root = xmlDocGetRootElement(doc);
1646 if (root != NULL) {
1647 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1648 xmlSetProp(root, BAD_CAST "ns", ns);
1649 }
1650 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001651 }
1652
1653 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001654 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001655 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001656 xmlRelaxNGIncludePush(ctxt, ret);
1657
1658 /*
1659 * Some preprocessing of the document content, this include recursing
1660 * in the include stack.
1661 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001662#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001663 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001664#endif
1665
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001666 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1667 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001668 ctxt->inc = NULL;
1669 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001670 }
1671
1672 /*
1673 * Pop up the include from the stack
1674 */
1675 xmlRelaxNGIncludePop(ctxt);
1676
Daniel Veillard5add8682003-03-10 13:13:58 +00001677#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001678 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001679#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001680 /*
1681 * Check that the top element is a grammar
1682 */
1683 root = xmlDocGetRootElement(doc);
1684 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001685 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1686 "xmlRelaxNG: included document is empty %s\n", URL,
1687 NULL);
1688 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001689 }
1690 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001691 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1692 "xmlRelaxNG: included document %s root is not a grammar\n",
1693 URL, NULL);
1694 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001695 }
1696
1697 /*
1698 * Elimination of redefined rules in the include.
1699 */
1700 cur = node->children;
1701 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001702 if (IS_RELAXNG(cur, "start")) {
1703 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001704
Daniel Veillard4c004142003-10-07 11:33:24 +00001705 found =
1706 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1707 if (!found) {
1708 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1709 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1710 URL, NULL);
1711 }
1712 } else if (IS_RELAXNG(cur, "define")) {
1713 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001714
Daniel Veillard4c004142003-10-07 11:33:24 +00001715 name = xmlGetProp(cur, BAD_CAST "name");
1716 if (name == NULL) {
1717 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1718 "xmlRelaxNG: include %s has define without name\n",
1719 URL, NULL);
1720 } else {
1721 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001722
Daniel Veillard4c004142003-10-07 11:33:24 +00001723 xmlRelaxNGNormExtSpace(name);
1724 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1725 root->children, name);
1726 if (!found) {
1727 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1728 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1729 URL, name);
1730 }
1731 xmlFree(name);
1732 }
1733 }
1734 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001735 }
1736
1737
Daniel Veillard4c004142003-10-07 11:33:24 +00001738 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001739}
1740
1741/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001742 * xmlRelaxNGValidErrorPush:
1743 * @ctxt: the validation context
1744 * @err: the error code
1745 * @arg1: the first string argument
1746 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001747 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001748 *
1749 * Pushes a new error on top of the error stack
1750 *
1751 * Returns 0 in case of error, the index in the stack otherwise
1752 */
1753static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001754xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1755 xmlRelaxNGValidErr err, const xmlChar * arg1,
1756 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001757{
1758 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001759
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001760#ifdef DEBUG_ERROR
1761 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001762 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001763#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001764 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001765 ctxt->errMax = 8;
1766 ctxt->errNr = 0;
1767 ctxt->errTab =
1768 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1769 sizeof
1770 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001771 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001772 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001773 return (0);
1774 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001775 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001776 }
1777 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001778 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001779 ctxt->errTab =
1780 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001781 ctxt->errMax *
1782 sizeof
1783 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001784 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001785 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001786 return (0);
1787 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001788 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001789 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001790 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001791 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1792 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001793 cur = &ctxt->errTab[ctxt->errNr];
1794 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001795 if (dup) {
1796 cur->arg1 = xmlStrdup(arg1);
1797 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001798 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001799 } else {
1800 cur->arg1 = arg1;
1801 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001802 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001803 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001804 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001805 cur->node = ctxt->state->node;
1806 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001807 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001808 cur->node = NULL;
1809 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001810 }
1811 ctxt->err = cur;
1812 return (ctxt->errNr++);
1813}
1814
1815/**
1816 * xmlRelaxNGValidErrorPop:
1817 * @ctxt: the validation context
1818 *
1819 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001820 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001821static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001822xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1823{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001824 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001825
Daniel Veillard580ced82003-03-21 21:22:48 +00001826 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001827 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001828 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001829 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001830 ctxt->errNr--;
1831 if (ctxt->errNr > 0)
1832 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1833 else
1834 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001835 cur = &ctxt->errTab[ctxt->errNr];
1836 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001837 if (cur->arg1 != NULL)
1838 xmlFree((xmlChar *) cur->arg1);
1839 cur->arg1 = NULL;
1840 if (cur->arg2 != NULL)
1841 xmlFree((xmlChar *) cur->arg2);
1842 cur->arg2 = NULL;
1843 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001844 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001845}
1846
Daniel Veillard42f12e92003-03-07 18:32:59 +00001847/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001848 * xmlRelaxNGDocumentPush:
1849 * @ctxt: the parser context
1850 * @value: the element doc
1851 *
1852 * Pushes a new doc on top of the doc stack
1853 *
1854 * Returns 0 in case of error, the index in the stack otherwise
1855 */
1856static int
1857xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001858 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001859{
1860 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001861 ctxt->docMax = 4;
1862 ctxt->docNr = 0;
1863 ctxt->docTab =
1864 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1865 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001866 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001867 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001868 return (0);
1869 }
1870 }
1871 if (ctxt->docNr >= ctxt->docMax) {
1872 ctxt->docMax *= 2;
1873 ctxt->docTab =
1874 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001875 ctxt->docMax *
1876 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001877 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001878 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001879 return (0);
1880 }
1881 }
1882 ctxt->docTab[ctxt->docNr] = value;
1883 ctxt->doc = value;
1884 return (ctxt->docNr++);
1885}
1886
1887/**
1888 * xmlRelaxNGDocumentPop:
1889 * @ctxt: the parser context
1890 *
1891 * Pops the top doc from the doc stack
1892 *
1893 * Returns the doc just removed
1894 */
1895static xmlRelaxNGDocumentPtr
1896xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1897{
1898 xmlRelaxNGDocumentPtr ret;
1899
1900 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001901 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001902 ctxt->docNr--;
1903 if (ctxt->docNr > 0)
1904 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1905 else
1906 ctxt->doc = NULL;
1907 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001908 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001909 return (ret);
1910}
1911
1912/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001913 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001914 * @ctxt: the parser context
1915 * @URL: the normalized URL
1916 * @ns: the inherited ns if any
1917 *
1918 * First lookup if the document is already loaded into the parser context,
1919 * check against recursion. If not found the resource is loaded and
1920 * the content is preprocessed before being returned back to the caller.
1921 *
1922 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1923 */
1924static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001925xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1926 const xmlChar * URL, const xmlChar * ns)
1927{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001928 xmlRelaxNGDocumentPtr ret = NULL;
1929 xmlDocPtr doc;
1930 xmlNodePtr root;
1931 int i;
1932
1933 /*
1934 * check against recursion in the stack
1935 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001936 for (i = 0; i < ctxt->docNr; i++) {
1937 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1938 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1939 "Detected an externalRef recursion for %s\n", URL,
1940 NULL);
1941 return (NULL);
1942 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001943 }
1944
1945 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001946 * load the document
1947 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001948 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001949 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001950 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1951 "xmlRelaxNG: could not load %s\n", URL, NULL);
1952 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001953 }
1954
1955 /*
1956 * Allocate the document structures and register it first.
1957 */
1958 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1959 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001960 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1961 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1962 xmlFreeDoc(doc);
1963 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001964 }
1965 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1966 ret->doc = doc;
1967 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001968 ret->next = ctxt->documents;
1969 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001970
1971 /*
1972 * transmit the ns if needed
1973 */
1974 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001975 root = xmlDocGetRootElement(doc);
1976 if (root != NULL) {
1977 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1978 xmlSetProp(root, BAD_CAST "ns", ns);
1979 }
1980 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001981 }
1982
1983 /*
1984 * push it on the stack and register it in the hash table
1985 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001986 xmlRelaxNGDocumentPush(ctxt, ret);
1987
1988 /*
1989 * Some preprocessing of the document content
1990 */
1991 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1992 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001993 ctxt->doc = NULL;
1994 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001995 }
1996
1997 xmlRelaxNGDocumentPop(ctxt);
1998
Daniel Veillard4c004142003-10-07 11:33:24 +00001999 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002000}
2001
2002/************************************************************************
2003 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002004 * Error functions *
2005 * *
2006 ************************************************************************/
2007
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002008#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2009#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2010#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2011#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2012#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002013
Daniel Veillard231d7912003-02-09 14:22:17 +00002014static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002015xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2016{
Daniel Veillard231d7912003-02-09 14:22:17 +00002017 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002018 return ("none");
2019 switch (def->type) {
2020 case XML_RELAXNG_EMPTY:
2021 return ("empty");
2022 case XML_RELAXNG_NOT_ALLOWED:
2023 return ("notAllowed");
2024 case XML_RELAXNG_EXCEPT:
2025 return ("except");
2026 case XML_RELAXNG_TEXT:
2027 return ("text");
2028 case XML_RELAXNG_ELEMENT:
2029 return ("element");
2030 case XML_RELAXNG_DATATYPE:
2031 return ("datatype");
2032 case XML_RELAXNG_VALUE:
2033 return ("value");
2034 case XML_RELAXNG_LIST:
2035 return ("list");
2036 case XML_RELAXNG_ATTRIBUTE:
2037 return ("attribute");
2038 case XML_RELAXNG_DEF:
2039 return ("def");
2040 case XML_RELAXNG_REF:
2041 return ("ref");
2042 case XML_RELAXNG_EXTERNALREF:
2043 return ("externalRef");
2044 case XML_RELAXNG_PARENTREF:
2045 return ("parentRef");
2046 case XML_RELAXNG_OPTIONAL:
2047 return ("optional");
2048 case XML_RELAXNG_ZEROORMORE:
2049 return ("zeroOrMore");
2050 case XML_RELAXNG_ONEORMORE:
2051 return ("oneOrMore");
2052 case XML_RELAXNG_CHOICE:
2053 return ("choice");
2054 case XML_RELAXNG_GROUP:
2055 return ("group");
2056 case XML_RELAXNG_INTERLEAVE:
2057 return ("interleave");
2058 case XML_RELAXNG_START:
2059 return ("start");
2060 case XML_RELAXNG_NOOP:
2061 return ("noop");
2062 case XML_RELAXNG_PARAM:
2063 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002064 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002065 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002066}
Daniel Veillardd2298792003-02-14 16:54:11 +00002067
Daniel Veillard6eadf632003-01-23 18:29:16 +00002068/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002069 * xmlRelaxNGGetErrorString:
2070 * @err: the error code
2071 * @arg1: the first string argument
2072 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002073 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002074 * computes a formatted error string for the given error code and args
2075 *
2076 * Returns the error string, it must be deallocated by the caller
2077 */
2078static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002079xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2080 const xmlChar * arg2)
2081{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002082 char msg[1000];
2083
2084 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002085 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002086 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002087 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002088
2089 msg[0] = 0;
2090 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002091 case XML_RELAXNG_OK:
2092 return (NULL);
2093 case XML_RELAXNG_ERR_MEMORY:
2094 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002095 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002096 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2097 break;
2098 case XML_RELAXNG_ERR_TYPEVAL:
2099 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2100 arg2);
2101 break;
2102 case XML_RELAXNG_ERR_DUPID:
2103 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2104 break;
2105 case XML_RELAXNG_ERR_TYPECMP:
2106 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2107 break;
2108 case XML_RELAXNG_ERR_NOSTATE:
2109 return (xmlCharStrdup("Internal error: no state\n"));
2110 case XML_RELAXNG_ERR_NODEFINE:
2111 return (xmlCharStrdup("Internal error: no define\n"));
2112 case XML_RELAXNG_ERR_INTERNAL:
2113 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2114 break;
2115 case XML_RELAXNG_ERR_LISTEXTRA:
2116 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2117 break;
2118 case XML_RELAXNG_ERR_INTERNODATA:
2119 return (xmlCharStrdup
2120 ("Internal: interleave block has no data\n"));
2121 case XML_RELAXNG_ERR_INTERSEQ:
2122 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2123 case XML_RELAXNG_ERR_INTEREXTRA:
2124 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2125 break;
2126 case XML_RELAXNG_ERR_ELEMNAME:
2127 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2128 arg2);
2129 break;
2130 case XML_RELAXNG_ERR_ELEMNONS:
2131 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2132 arg1);
2133 break;
2134 case XML_RELAXNG_ERR_ELEMWRONGNS:
2135 snprintf(msg, 1000,
2136 "Element %s has wrong namespace: expecting %s\n", arg1,
2137 arg2);
2138 break;
2139 case XML_RELAXNG_ERR_ELEMWRONG:
2140 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2141 break;
2142 case XML_RELAXNG_ERR_TEXTWRONG:
2143 snprintf(msg, 1000,
2144 "Did not expect text in element %s content\n", arg1);
2145 break;
2146 case XML_RELAXNG_ERR_ELEMEXTRANS:
2147 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2148 arg1);
2149 break;
2150 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2151 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2152 break;
2153 case XML_RELAXNG_ERR_NOELEM:
2154 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2155 arg1);
2156 break;
2157 case XML_RELAXNG_ERR_NOTELEM:
2158 return (xmlCharStrdup("Expecting an element got text\n"));
2159 case XML_RELAXNG_ERR_ATTRVALID:
2160 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2161 arg1);
2162 break;
2163 case XML_RELAXNG_ERR_CONTENTVALID:
2164 snprintf(msg, 1000, "Element %s failed to validate content\n",
2165 arg1);
2166 break;
2167 case XML_RELAXNG_ERR_EXTRACONTENT:
2168 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2169 arg1, arg2);
2170 break;
2171 case XML_RELAXNG_ERR_INVALIDATTR:
2172 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2173 arg1, arg2);
2174 break;
2175 case XML_RELAXNG_ERR_LACKDATA:
2176 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2177 arg1);
2178 break;
2179 case XML_RELAXNG_ERR_DATAELEM:
2180 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2181 arg1);
2182 break;
2183 case XML_RELAXNG_ERR_VALELEM:
2184 snprintf(msg, 1000, "Value element %s has child elements\n",
2185 arg1);
2186 break;
2187 case XML_RELAXNG_ERR_LISTELEM:
2188 snprintf(msg, 1000, "List element %s has child elements\n",
2189 arg1);
2190 break;
2191 case XML_RELAXNG_ERR_DATATYPE:
2192 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2193 break;
2194 case XML_RELAXNG_ERR_VALUE:
2195 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2196 break;
2197 case XML_RELAXNG_ERR_LIST:
2198 return (xmlCharStrdup("Error validating list\n"));
2199 case XML_RELAXNG_ERR_NOGRAMMAR:
2200 return (xmlCharStrdup("No top grammar defined\n"));
2201 case XML_RELAXNG_ERR_EXTRADATA:
2202 return (xmlCharStrdup("Extra data in the document\n"));
2203 default:
2204 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002205 }
2206 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002207 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002208 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002209 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002210 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002211}
2212
2213/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002214 * xmlRelaxNGShowValidError:
2215 * @ctxt: the validation context
2216 * @err: the error number
2217 * @node: the node
2218 * @child: the node child generating the problem.
2219 * @arg1: the first argument
2220 * @arg2: the second argument
2221 *
2222 * Show a validation error.
2223 */
2224static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002225xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2226 xmlRelaxNGValidErr err, xmlNodePtr node,
2227 xmlNodePtr child, const xmlChar * arg1,
2228 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002229{
2230 xmlChar *msg;
2231
Daniel Veillardb30ca312005-09-04 13:50:03 +00002232 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002233 return;
2234
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002235#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002236 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002237#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002238 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2239 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002240 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002241
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002242 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002243 ctxt->errNo = err;
2244 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2245 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002246 xmlFree(msg);
2247}
2248
2249/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002250 * xmlRelaxNGPopErrors:
2251 * @ctxt: the validation context
2252 * @level: the error level in the stack
2253 *
2254 * pop and discard all errors until the given level is reached
2255 */
2256static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002257xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2258{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002259 int i;
2260 xmlRelaxNGValidErrorPtr err;
2261
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002262#ifdef DEBUG_ERROR
2263 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002264 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002265#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002266 for (i = level; i < ctxt->errNr; i++) {
2267 err = &ctxt->errTab[i];
2268 if (err->flags & ERROR_IS_DUP) {
2269 if (err->arg1 != NULL)
2270 xmlFree((xmlChar *) err->arg1);
2271 err->arg1 = NULL;
2272 if (err->arg2 != NULL)
2273 xmlFree((xmlChar *) err->arg2);
2274 err->arg2 = NULL;
2275 err->flags = 0;
2276 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002277 }
2278 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002279 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002280 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002281}
Daniel Veillard4c004142003-10-07 11:33:24 +00002282
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002283/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002284 * xmlRelaxNGDumpValidError:
2285 * @ctxt: the validation context
2286 *
2287 * Show all validation error over a given index.
2288 */
2289static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002290xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2291{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002292 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002293 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002294
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002295#ifdef DEBUG_ERROR
2296 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002297 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002298#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002299 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2300 err = &ctxt->errTab[i];
2301 if (k < MAX_ERROR) {
2302 for (j = 0; j < i; j++) {
2303 dup = &ctxt->errTab[j];
2304 if ((err->err == dup->err) && (err->node == dup->node) &&
2305 (xmlStrEqual(err->arg1, dup->arg1)) &&
2306 (xmlStrEqual(err->arg2, dup->arg2))) {
2307 goto skip;
2308 }
2309 }
2310 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2311 err->arg1, err->arg2);
2312 k++;
2313 }
2314 skip:
2315 if (err->flags & ERROR_IS_DUP) {
2316 if (err->arg1 != NULL)
2317 xmlFree((xmlChar *) err->arg1);
2318 err->arg1 = NULL;
2319 if (err->arg2 != NULL)
2320 xmlFree((xmlChar *) err->arg2);
2321 err->arg2 = NULL;
2322 err->flags = 0;
2323 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002324 }
2325 ctxt->errNr = 0;
2326}
Daniel Veillard4c004142003-10-07 11:33:24 +00002327
Daniel Veillard42f12e92003-03-07 18:32:59 +00002328/**
2329 * xmlRelaxNGAddValidError:
2330 * @ctxt: the validation context
2331 * @err: the error number
2332 * @arg1: the first argument
2333 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002334 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002335 *
2336 * Register a validation error, either generating it if it's sure
2337 * or stacking it for later handling if unsure.
2338 */
2339static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002340xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2341 xmlRelaxNGValidErr err, const xmlChar * arg1,
2342 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002343{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002344 if (ctxt == NULL)
2345 return;
2346 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002347 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002348
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002349#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002350 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002351#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002352 /*
2353 * generate the error directly
2354 */
William M. Brack60929622004-03-27 17:54:18 +00002355 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2356 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002357 xmlNodePtr node, seq;
2358
2359 /*
2360 * Flush first any stacked error which might be the
2361 * real cause of the problem.
2362 */
2363 if (ctxt->errNr != 0)
2364 xmlRelaxNGDumpValidError(ctxt);
2365 if (ctxt->state != NULL) {
2366 node = ctxt->state->node;
2367 seq = ctxt->state->seq;
2368 } else {
2369 node = seq = NULL;
2370 }
2371 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002372 }
2373 /*
2374 * Stack the error for later processing if needed
2375 */
2376 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002377 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002378 }
2379}
2380
Daniel Veillard6eadf632003-01-23 18:29:16 +00002381
2382/************************************************************************
2383 * *
2384 * Type library hooks *
2385 * *
2386 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002387static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002388 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002389
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002390/**
2391 * xmlRelaxNGSchemaTypeHave:
2392 * @data: data needed for the library
2393 * @type: the type name
2394 *
2395 * Check if the given type is provided by
2396 * the W3C XMLSchema Datatype library.
2397 *
2398 * Returns 1 if yes, 0 if no and -1 in case of error.
2399 */
2400static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002401xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2402{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002403 xmlSchemaTypePtr typ;
2404
2405 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002406 return (-1);
2407 typ = xmlSchemaGetPredefinedType(type,
2408 BAD_CAST
2409 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002410 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002411 return (0);
2412 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002413}
2414
2415/**
2416 * xmlRelaxNGSchemaTypeCheck:
2417 * @data: data needed for the library
2418 * @type: the type name
2419 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002420 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002421 *
2422 * Check if the given type and value are validated by
2423 * the W3C XMLSchema Datatype library.
2424 *
2425 * Returns 1 if yes, 0 if no and -1 in case of error.
2426 */
2427static int
2428xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002429 const xmlChar * type,
2430 const xmlChar * value,
2431 void **result, xmlNodePtr node)
2432{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002433 xmlSchemaTypePtr typ;
2434 int ret;
2435
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002436 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002437 return (-1);
2438 typ = xmlSchemaGetPredefinedType(type,
2439 BAD_CAST
2440 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002441 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002442 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002443 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002444 (xmlSchemaValPtr *) result, node);
2445 if (ret == 2) /* special ID error code */
2446 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002447 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002448 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002449 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002450 return (0);
2451 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002452}
2453
2454/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002455 * xmlRelaxNGSchemaFacetCheck:
2456 * @data: data needed for the library
2457 * @type: the type name
2458 * @facet: the facet name
2459 * @val: the facet value
2460 * @strval: the string value
2461 * @value: the value to check
2462 *
2463 * Function provided by a type library to check a value facet
2464 *
2465 * Returns 1 if yes, 0 if no and -1 in case of error.
2466 */
2467static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002468xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2469 const xmlChar * type, const xmlChar * facetname,
2470 const xmlChar * val, const xmlChar * strval,
2471 void *value)
2472{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002473 xmlSchemaFacetPtr facet;
2474 xmlSchemaTypePtr typ;
2475 int ret;
2476
2477 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002478 return (-1);
2479 typ = xmlSchemaGetPredefinedType(type,
2480 BAD_CAST
2481 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002482 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002483 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002484
2485 facet = xmlSchemaNewFacet();
2486 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002487 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002488
Daniel Veillard4c004142003-10-07 11:33:24 +00002489 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002490 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002491 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002492 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002493 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002494 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002495 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002496 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002497 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002498 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002499 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002500 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002501 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002502 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002503 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002504 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002505 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002506 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002507 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002508 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002509 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002510 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2511 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2512 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2513 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002514 xmlSchemaFreeFacet(facet);
2515 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002516 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002517 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002518 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2519 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002520 xmlSchemaFreeFacet(facet);
2521 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002522 }
2523 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2524 xmlSchemaFreeFacet(facet);
2525 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002526 return (-1);
2527 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002528}
2529
2530/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002531 * xmlRelaxNGSchemaFreeValue:
2532 * @data: data needed for the library
2533 * @value: the value to free
2534 *
2535 * Function provided by a type library to free a Schemas value
2536 *
2537 * Returns 1 if yes, 0 if no and -1 in case of error.
2538 */
2539static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002540xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2541{
Daniel Veillard80b19092003-03-28 13:29:53 +00002542 xmlSchemaFreeValue(value);
2543}
2544
2545/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002546 * xmlRelaxNGSchemaTypeCompare:
2547 * @data: data needed for the library
2548 * @type: the type name
2549 * @value1: the first value
2550 * @value2: the second value
2551 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002552 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002553 * Datatype library.
2554 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002555 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002556 */
2557static int
2558xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002559 const xmlChar * type,
2560 const xmlChar * value1,
2561 xmlNodePtr ctxt1,
2562 void *comp1,
2563 const xmlChar * value2, xmlNodePtr ctxt2)
2564{
Daniel Veillard80b19092003-03-28 13:29:53 +00002565 int ret;
2566 xmlSchemaTypePtr typ;
2567 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2568
2569 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002570 return (-1);
2571 typ = xmlSchemaGetPredefinedType(type,
2572 BAD_CAST
2573 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002574 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002575 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002576 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002577 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2578 if (ret != 0)
2579 return (-1);
2580 if (res1 == NULL)
2581 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002582 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002583 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002584 }
2585 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002586 if (ret != 0) {
Daniel Veillardf4644032005-06-13 11:41:31 +00002587 if ((comp1 == NULL) && (res1 != NULL))
2588 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002589 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002590 }
2591 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002592 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002593 }
2594 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002595 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002596 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002597 xmlSchemaFreeValue(res2);
2598 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002599 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002600 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002601 return (1);
2602 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002603}
Daniel Veillard4c004142003-10-07 11:33:24 +00002604
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002605/**
2606 * xmlRelaxNGDefaultTypeHave:
2607 * @data: data needed for the library
2608 * @type: the type name
2609 *
2610 * Check if the given type is provided by
2611 * the default datatype library.
2612 *
2613 * Returns 1 if yes, 0 if no and -1 in case of error.
2614 */
2615static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002616xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2617 const xmlChar * type)
2618{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002619 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002620 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002621 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002622 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002623 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002624 return (1);
2625 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002626}
2627
2628/**
2629 * xmlRelaxNGDefaultTypeCheck:
2630 * @data: data needed for the library
2631 * @type: the type name
2632 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002633 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002634 *
2635 * Check if the given type and value are validated by
2636 * the default datatype library.
2637 *
2638 * Returns 1 if yes, 0 if no and -1 in case of error.
2639 */
2640static int
2641xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002642 const xmlChar * type ATTRIBUTE_UNUSED,
2643 const xmlChar * value ATTRIBUTE_UNUSED,
2644 void **result ATTRIBUTE_UNUSED,
2645 xmlNodePtr node ATTRIBUTE_UNUSED)
2646{
Daniel Veillardd4310742003-02-18 21:12:46 +00002647 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002648 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002649 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002650 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002651 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002652 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002653 }
2654
Daniel Veillard4c004142003-10-07 11:33:24 +00002655 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002656}
2657
2658/**
2659 * xmlRelaxNGDefaultTypeCompare:
2660 * @data: data needed for the library
2661 * @type: the type name
2662 * @value1: the first value
2663 * @value2: the second value
2664 *
2665 * Compare two values accordingly a type from the default
2666 * datatype library.
2667 *
2668 * Returns 1 if yes, 0 if no and -1 in case of error.
2669 */
2670static int
2671xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002672 const xmlChar * type,
2673 const xmlChar * value1,
2674 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2675 void *comp1 ATTRIBUTE_UNUSED,
2676 const xmlChar * value2,
2677 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2678{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002679 int ret = -1;
2680
2681 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002682 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002683 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002684 if (!xmlStrEqual(value1, value2)) {
2685 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002686
Daniel Veillard4c004142003-10-07 11:33:24 +00002687 /*
2688 * TODO: trivial optimizations are possible by
2689 * computing at compile-time
2690 */
2691 nval = xmlRelaxNGNormalize(NULL, value1);
2692 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002693
Daniel Veillard4c004142003-10-07 11:33:24 +00002694 if ((nval == NULL) || (nvalue == NULL))
2695 ret = -1;
2696 else if (xmlStrEqual(nval, nvalue))
2697 ret = 1;
2698 else
2699 ret = 0;
2700 if (nval != NULL)
2701 xmlFree(nval);
2702 if (nvalue != NULL)
2703 xmlFree(nvalue);
2704 } else
2705 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002706 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002707 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002708}
Daniel Veillard4c004142003-10-07 11:33:24 +00002709
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002710static int xmlRelaxNGTypeInitialized = 0;
2711static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2712
2713/**
2714 * xmlRelaxNGFreeTypeLibrary:
2715 * @lib: the type library structure
2716 * @namespace: the URI bound to the library
2717 *
2718 * Free the structure associated to the type library
2719 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002720static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002721xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002722 const xmlChar * namespace ATTRIBUTE_UNUSED)
2723{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002724 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002725 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002726 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002727 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002728 xmlFree(lib);
2729}
2730
2731/**
2732 * xmlRelaxNGRegisterTypeLibrary:
2733 * @namespace: the URI bound to the library
2734 * @data: data associated to the library
2735 * @have: the provide function
2736 * @check: the checking function
2737 * @comp: the comparison function
2738 *
2739 * Register a new type library
2740 *
2741 * Returns 0 in case of success and -1 in case of error.
2742 */
2743static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002744xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2745 xmlRelaxNGTypeHave have,
2746 xmlRelaxNGTypeCheck check,
2747 xmlRelaxNGTypeCompare comp,
2748 xmlRelaxNGFacetCheck facet,
2749 xmlRelaxNGTypeFree freef)
2750{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002751 xmlRelaxNGTypeLibraryPtr lib;
2752 int ret;
2753
2754 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002755 (check == NULL) || (comp == NULL))
2756 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002757 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002758 xmlGenericError(xmlGenericErrorContext,
2759 "Relax-NG types library '%s' already registered\n",
2760 namespace);
2761 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002762 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002763 lib =
2764 (xmlRelaxNGTypeLibraryPtr)
2765 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002766 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002767 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002768 return (-1);
2769 }
2770 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2771 lib->namespace = xmlStrdup(namespace);
2772 lib->data = data;
2773 lib->have = have;
2774 lib->comp = comp;
2775 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002776 lib->facet = facet;
2777 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002778 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2779 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002780 xmlGenericError(xmlGenericErrorContext,
2781 "Relax-NG types library failed to register '%s'\n",
2782 namespace);
2783 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2784 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002785 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002786 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002787}
2788
2789/**
2790 * xmlRelaxNGInitTypes:
2791 *
2792 * Initilize the default type libraries.
2793 *
2794 * Returns 0 in case of success and -1 in case of error.
2795 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002796int
Daniel Veillard4c004142003-10-07 11:33:24 +00002797xmlRelaxNGInitTypes(void)
2798{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002799 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002800 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002801 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2802 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002803 xmlGenericError(xmlGenericErrorContext,
2804 "Failed to allocate sh table for Relax-NG types\n");
2805 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002806 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002807 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2808 "http://www.w3.org/2001/XMLSchema-datatypes",
2809 NULL, xmlRelaxNGSchemaTypeHave,
2810 xmlRelaxNGSchemaTypeCheck,
2811 xmlRelaxNGSchemaTypeCompare,
2812 xmlRelaxNGSchemaFacetCheck,
2813 xmlRelaxNGSchemaFreeValue);
2814 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2815 xmlRelaxNGDefaultTypeHave,
2816 xmlRelaxNGDefaultTypeCheck,
2817 xmlRelaxNGDefaultTypeCompare, NULL,
2818 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002819 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002820 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002821}
2822
2823/**
2824 * xmlRelaxNGCleanupTypes:
2825 *
2826 * Cleanup the default Schemas type library associated to RelaxNG
2827 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002828void
2829xmlRelaxNGCleanupTypes(void)
2830{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002831 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002832 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002833 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002834 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002835 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002836 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002837}
2838
2839/************************************************************************
2840 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002841 * Compiling element content into regexp *
2842 * *
2843 * Sometime the element content can be compiled into a pure regexp, *
2844 * This allows a faster execution and streamability at that level *
2845 * *
2846 ************************************************************************/
2847
Daniel Veillard52b48c72003-04-13 19:53:42 +00002848static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2849 xmlRelaxNGDefinePtr def);
2850
Daniel Veillard952379b2003-03-17 15:37:12 +00002851/**
2852 * xmlRelaxNGIsCompileable:
2853 * @define: the definition to check
2854 *
2855 * Check if a definition is nullable.
2856 *
2857 * Returns 1 if yes, 0 if no and -1 in case of error
2858 */
2859static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002860xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2861{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002862 int ret = -1;
2863
Daniel Veillard952379b2003-03-17 15:37:12 +00002864 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002865 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002866 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002867 if ((def->type != XML_RELAXNG_ELEMENT) &&
2868 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002869 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002870 if ((def->type != XML_RELAXNG_ELEMENT) &&
2871 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002872 return (0);
2873 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002874 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002875 ret = xmlRelaxNGIsCompileable(def->content);
2876 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002877 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002878 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002879 ret = 1;
2880 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002881 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002882 /*
2883 * Check if the element content is compileable
2884 */
2885 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2886 ((def->dflags & IS_COMPILABLE) == 0)) {
2887 xmlRelaxNGDefinePtr list;
2888
2889 list = def->content;
2890 while (list != NULL) {
2891 ret = xmlRelaxNGIsCompileable(list);
2892 if (ret != 1)
2893 break;
2894 list = list->next;
2895 }
William M. Brack60929622004-03-27 17:54:18 +00002896 /*
2897 * Because the routine is recursive, we must guard against
2898 * discovering both COMPILABLE and NOT_COMPILABLE
2899 */
2900 if (ret == 0) {
2901 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002902 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002903 }
2904 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002905 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002906#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002907 if (ret == 1) {
2908 xmlGenericError(xmlGenericErrorContext,
2909 "element content for %s is compilable\n",
2910 def->name);
2911 } else if (ret == 0) {
2912 xmlGenericError(xmlGenericErrorContext,
2913 "element content for %s is not compilable\n",
2914 def->name);
2915 } else {
2916 xmlGenericError(xmlGenericErrorContext,
2917 "Problem in RelaxNGIsCompileable for element %s\n",
2918 def->name);
2919 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002920#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002921 }
2922 /*
2923 * All elements return a compileable status unless they
2924 * are generic like anyName
2925 */
2926 if ((def->nameClass != NULL) || (def->name == NULL))
2927 ret = 0;
2928 else
2929 ret = 1;
2930 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002931 case XML_RELAXNG_REF:
2932 case XML_RELAXNG_EXTERNALREF:
2933 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002934 if (def->depth == -20) {
2935 return (1);
2936 } else {
2937 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002938
Daniel Veillard4c004142003-10-07 11:33:24 +00002939 def->depth = -20;
2940 list = def->content;
2941 while (list != NULL) {
2942 ret = xmlRelaxNGIsCompileable(list);
2943 if (ret != 1)
2944 break;
2945 list = list->next;
2946 }
2947 }
2948 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002949 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002950 case XML_RELAXNG_OPTIONAL:
2951 case XML_RELAXNG_ZEROORMORE:
2952 case XML_RELAXNG_ONEORMORE:
2953 case XML_RELAXNG_CHOICE:
2954 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002955 case XML_RELAXNG_DEF:{
2956 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002957
Daniel Veillard4c004142003-10-07 11:33:24 +00002958 list = def->content;
2959 while (list != NULL) {
2960 ret = xmlRelaxNGIsCompileable(list);
2961 if (ret != 1)
2962 break;
2963 list = list->next;
2964 }
2965 break;
2966 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002967 case XML_RELAXNG_EXCEPT:
2968 case XML_RELAXNG_ATTRIBUTE:
2969 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002970 case XML_RELAXNG_DATATYPE:
2971 case XML_RELAXNG_LIST:
2972 case XML_RELAXNG_PARAM:
2973 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002974 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002975 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002976 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002977 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002978 if (ret == 0)
2979 def->dflags |= IS_NOT_COMPILABLE;
2980 if (ret == 1)
2981 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002982#ifdef DEBUG_COMPILE
2983 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002984 xmlGenericError(xmlGenericErrorContext,
2985 "RelaxNGIsCompileable %s : true\n",
2986 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002987 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002988 xmlGenericError(xmlGenericErrorContext,
2989 "RelaxNGIsCompileable %s : false\n",
2990 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002991 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002992 xmlGenericError(xmlGenericErrorContext,
2993 "Problem in RelaxNGIsCompileable %s\n",
2994 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002995 }
2996#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002997 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002998}
2999
3000/**
3001 * xmlRelaxNGCompile:
3002 * ctxt: the RelaxNG parser context
3003 * @define: the definition tree to compile
3004 *
3005 * Compile the set of definitions, it works recursively, till the
3006 * element boundaries, where it tries to compile the content if possible
3007 *
3008 * Returns 0 if success and -1 in case of error
3009 */
3010static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003011xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3012{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003013 int ret = 0;
3014 xmlRelaxNGDefinePtr list;
3015
Daniel Veillard4c004142003-10-07 11:33:24 +00003016 if ((ctxt == NULL) || (def == NULL))
3017 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003018
Daniel Veillard4c004142003-10-07 11:33:24 +00003019 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003020 case XML_RELAXNG_START:
3021 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003022 xmlAutomataPtr oldam = ctxt->am;
3023 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003024
3025 def->depth = -25;
3026
Daniel Veillard4c004142003-10-07 11:33:24 +00003027 list = def->content;
3028 ctxt->am = xmlNewAutomata();
3029 if (ctxt->am == NULL)
3030 return (-1);
3031 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3032 while (list != NULL) {
3033 xmlRelaxNGCompile(ctxt, list);
3034 list = list->next;
3035 }
3036 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3037 def->contModel = xmlAutomataCompile(ctxt->am);
3038 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003039
Daniel Veillard4c004142003-10-07 11:33:24 +00003040 xmlFreeAutomata(ctxt->am);
3041 ctxt->state = oldstate;
3042 ctxt->am = oldam;
3043 }
3044 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003045 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003046 if ((ctxt->am != NULL) && (def->name != NULL)) {
3047 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3048 ctxt->state, NULL,
3049 def->name, def->ns,
3050 def);
3051 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003052 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003053 xmlAutomataPtr oldam = ctxt->am;
3054 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003055
3056 def->depth = -25;
3057
Daniel Veillard4c004142003-10-07 11:33:24 +00003058 list = def->content;
3059 ctxt->am = xmlNewAutomata();
3060 if (ctxt->am == NULL)
3061 return (-1);
3062 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3063 while (list != NULL) {
3064 xmlRelaxNGCompile(ctxt, list);
3065 list = list->next;
3066 }
3067 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3068 def->contModel = xmlAutomataCompile(ctxt->am);
3069 if (!xmlRegexpIsDeterminist(def->contModel)) {
3070 /*
3071 * we can only use the automata if it is determinist
3072 */
3073 xmlRegFreeRegexp(def->contModel);
3074 def->contModel = NULL;
3075 }
3076 xmlFreeAutomata(ctxt->am);
3077 ctxt->state = oldstate;
3078 ctxt->am = oldam;
3079 } else {
3080 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003081
Daniel Veillard4c004142003-10-07 11:33:24 +00003082 /*
3083 * we can't build the content model for this element content
3084 * but it still might be possible to build it for some of its
3085 * children, recurse.
3086 */
3087 ret = xmlRelaxNGTryCompile(ctxt, def);
3088 ctxt->am = oldam;
3089 }
3090 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003091 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003092 ret = xmlRelaxNGCompile(ctxt, def->content);
3093 break;
3094 case XML_RELAXNG_OPTIONAL:{
3095 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003096
Daniel Veillard4c004142003-10-07 11:33:24 +00003097 xmlRelaxNGCompile(ctxt, def->content);
3098 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3099 break;
3100 }
3101 case XML_RELAXNG_ZEROORMORE:{
3102 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003103
Daniel Veillard4c004142003-10-07 11:33:24 +00003104 ctxt->state =
3105 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3106 oldstate = ctxt->state;
3107 list = def->content;
3108 while (list != NULL) {
3109 xmlRelaxNGCompile(ctxt, list);
3110 list = list->next;
3111 }
3112 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3113 ctxt->state =
3114 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3115 break;
3116 }
3117 case XML_RELAXNG_ONEORMORE:{
3118 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003119
Daniel Veillard4c004142003-10-07 11:33:24 +00003120 list = def->content;
3121 while (list != NULL) {
3122 xmlRelaxNGCompile(ctxt, list);
3123 list = list->next;
3124 }
3125 oldstate = ctxt->state;
3126 list = def->content;
3127 while (list != NULL) {
3128 xmlRelaxNGCompile(ctxt, list);
3129 list = list->next;
3130 }
3131 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3132 ctxt->state =
3133 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3134 break;
3135 }
3136 case XML_RELAXNG_CHOICE:{
3137 xmlAutomataStatePtr target = NULL;
3138 xmlAutomataStatePtr oldstate = ctxt->state;
3139
3140 list = def->content;
3141 while (list != NULL) {
3142 ctxt->state = oldstate;
3143 ret = xmlRelaxNGCompile(ctxt, list);
3144 if (ret != 0)
3145 break;
3146 if (target == NULL)
3147 target = ctxt->state;
3148 else {
3149 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3150 target);
3151 }
3152 list = list->next;
3153 }
3154 ctxt->state = target;
3155
3156 break;
3157 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003158 case XML_RELAXNG_REF:
3159 case XML_RELAXNG_EXTERNALREF:
3160 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003161 case XML_RELAXNG_GROUP:
3162 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003163 list = def->content;
3164 while (list != NULL) {
3165 ret = xmlRelaxNGCompile(ctxt, list);
3166 if (ret != 0)
3167 break;
3168 list = list->next;
3169 }
3170 break;
3171 case XML_RELAXNG_TEXT:{
3172 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003173
Daniel Veillard4c004142003-10-07 11:33:24 +00003174 ctxt->state =
3175 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3176 oldstate = ctxt->state;
3177 xmlRelaxNGCompile(ctxt, def->content);
3178 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3179 ctxt->state, BAD_CAST "#text",
3180 NULL);
3181 ctxt->state =
3182 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3183 break;
3184 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003185 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003186 ctxt->state =
3187 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3188 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003189 case XML_RELAXNG_EXCEPT:
3190 case XML_RELAXNG_ATTRIBUTE:
3191 case XML_RELAXNG_INTERLEAVE:
3192 case XML_RELAXNG_NOT_ALLOWED:
3193 case XML_RELAXNG_DATATYPE:
3194 case XML_RELAXNG_LIST:
3195 case XML_RELAXNG_PARAM:
3196 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003197 /* This should not happen and generate an internal error */
3198 fprintf(stderr, "RNG internal error trying to compile %s\n",
3199 xmlRelaxNGDefName(def));
3200 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003201 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003202 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003203}
3204
3205/**
3206 * xmlRelaxNGTryCompile:
3207 * ctxt: the RelaxNG parser context
3208 * @define: the definition tree to compile
3209 *
3210 * Try to compile the set of definitions, it works recursively,
3211 * possibly ignoring parts which cannot be compiled.
3212 *
3213 * Returns 0 if success and -1 in case of error
3214 */
3215static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003216xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3217{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003218 int ret = 0;
3219 xmlRelaxNGDefinePtr list;
3220
Daniel Veillard4c004142003-10-07 11:33:24 +00003221 if ((ctxt == NULL) || (def == NULL))
3222 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003223
3224 if ((def->type == XML_RELAXNG_START) ||
3225 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003226 ret = xmlRelaxNGIsCompileable(def);
3227 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3228 ctxt->am = NULL;
3229 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003230#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003231 if (ret == 0) {
3232 if (def->type == XML_RELAXNG_START)
3233 xmlGenericError(xmlGenericErrorContext,
3234 "compiled the start\n");
3235 else
3236 xmlGenericError(xmlGenericErrorContext,
3237 "compiled element %s\n", def->name);
3238 } else {
3239 if (def->type == XML_RELAXNG_START)
3240 xmlGenericError(xmlGenericErrorContext,
3241 "failed to compile the start\n");
3242 else
3243 xmlGenericError(xmlGenericErrorContext,
3244 "failed to compile element %s\n",
3245 def->name);
3246 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003247#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003248 return (ret);
3249 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003250 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003251 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003252 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003253 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3254 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003255 case XML_RELAXNG_TEXT:
3256 case XML_RELAXNG_DATATYPE:
3257 case XML_RELAXNG_LIST:
3258 case XML_RELAXNG_PARAM:
3259 case XML_RELAXNG_VALUE:
3260 case XML_RELAXNG_EMPTY:
3261 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003262 ret = 0;
3263 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003264 case XML_RELAXNG_OPTIONAL:
3265 case XML_RELAXNG_ZEROORMORE:
3266 case XML_RELAXNG_ONEORMORE:
3267 case XML_RELAXNG_CHOICE:
3268 case XML_RELAXNG_GROUP:
3269 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003270 case XML_RELAXNG_START:
3271 case XML_RELAXNG_REF:
3272 case XML_RELAXNG_EXTERNALREF:
3273 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003274 list = def->content;
3275 while (list != NULL) {
3276 ret = xmlRelaxNGTryCompile(ctxt, list);
3277 if (ret != 0)
3278 break;
3279 list = list->next;
3280 }
3281 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003282 case XML_RELAXNG_EXCEPT:
3283 case XML_RELAXNG_ATTRIBUTE:
3284 case XML_RELAXNG_INTERLEAVE:
3285 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003286 ret = 0;
3287 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003288 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003289 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003290}
3291
3292/************************************************************************
3293 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003294 * Parsing functions *
3295 * *
3296 ************************************************************************/
3297
Daniel Veillard4c004142003-10-07 11:33:24 +00003298static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3299 ctxt, xmlNodePtr node);
3300static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3301 ctxt, xmlNodePtr node);
3302static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3303 ctxt, xmlNodePtr nodes,
3304 int group);
3305static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3306 ctxt, xmlNodePtr node);
3307static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3308 xmlNodePtr node);
3309static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3310 xmlNodePtr nodes);
3311static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3312 ctxt, xmlNodePtr node,
3313 xmlRelaxNGDefinePtr
3314 def);
3315static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3316 ctxt, xmlNodePtr nodes);
3317static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3318 xmlRelaxNGDefinePtr define,
3319 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003320
3321
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003322#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003323
3324/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003325 * xmlRelaxNGIsNullable:
3326 * @define: the definition to verify
3327 *
3328 * Check if a definition is nullable.
3329 *
3330 * Returns 1 if yes, 0 if no and -1 in case of error
3331 */
3332static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003333xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3334{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003335 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003336
Daniel Veillardfd573f12003-03-16 17:52:32 +00003337 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003338 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003339
Daniel Veillarde063f482003-03-21 16:53:17 +00003340 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003341 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003342 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003343 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003344 switch (define->type) {
3345 case XML_RELAXNG_EMPTY:
3346 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003347 ret = 1;
3348 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003349 case XML_RELAXNG_NOOP:
3350 case XML_RELAXNG_DEF:
3351 case XML_RELAXNG_REF:
3352 case XML_RELAXNG_EXTERNALREF:
3353 case XML_RELAXNG_PARENTREF:
3354 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003355 ret = xmlRelaxNGIsNullable(define->content);
3356 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003357 case XML_RELAXNG_EXCEPT:
3358 case XML_RELAXNG_NOT_ALLOWED:
3359 case XML_RELAXNG_ELEMENT:
3360 case XML_RELAXNG_DATATYPE:
3361 case XML_RELAXNG_PARAM:
3362 case XML_RELAXNG_VALUE:
3363 case XML_RELAXNG_LIST:
3364 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003365 ret = 0;
3366 break;
3367 case XML_RELAXNG_CHOICE:{
3368 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003369
Daniel Veillard4c004142003-10-07 11:33:24 +00003370 while (list != NULL) {
3371 ret = xmlRelaxNGIsNullable(list);
3372 if (ret != 0)
3373 goto done;
3374 list = list->next;
3375 }
3376 ret = 0;
3377 break;
3378 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003379 case XML_RELAXNG_START:
3380 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003381 case XML_RELAXNG_GROUP:{
3382 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003383
Daniel Veillard4c004142003-10-07 11:33:24 +00003384 while (list != NULL) {
3385 ret = xmlRelaxNGIsNullable(list);
3386 if (ret != 1)
3387 goto done;
3388 list = list->next;
3389 }
3390 return (1);
3391 }
3392 default:
3393 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003394 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003395 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003396 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003397 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003398 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003399 define->dflags |= IS_NULLABLE;
3400 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003401}
3402
3403/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003404 * xmlRelaxNGIsBlank:
3405 * @str: a string
3406 *
3407 * Check if a string is ignorable c.f. 4.2. Whitespace
3408 *
3409 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3410 */
3411static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003412xmlRelaxNGIsBlank(xmlChar * str)
3413{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003414 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003415 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003416 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003417 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003418 return (0);
3419 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003420 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003421 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003422}
3423
Daniel Veillard6eadf632003-01-23 18:29:16 +00003424/**
3425 * xmlRelaxNGGetDataTypeLibrary:
3426 * @ctxt: a Relax-NG parser context
3427 * @node: the current data or value element
3428 *
3429 * Applies algorithm from 4.3. datatypeLibrary attribute
3430 *
3431 * Returns the datatypeLibary value or NULL if not found
3432 */
3433static xmlChar *
3434xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003435 xmlNodePtr node)
3436{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003437 xmlChar *ret, *escape;
3438
Daniel Veillard6eadf632003-01-23 18:29:16 +00003439 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003440 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3441 if (ret != NULL) {
3442 if (ret[0] == 0) {
3443 xmlFree(ret);
3444 return (NULL);
3445 }
3446 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3447 if (escape == NULL) {
3448 return (ret);
3449 }
3450 xmlFree(ret);
3451 return (escape);
3452 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003453 }
3454 node = node->parent;
3455 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003456 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3457 if (ret != NULL) {
3458 if (ret[0] == 0) {
3459 xmlFree(ret);
3460 return (NULL);
3461 }
3462 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3463 if (escape == NULL) {
3464 return (ret);
3465 }
3466 xmlFree(ret);
3467 return (escape);
3468 }
3469 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003470 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003471 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003472}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003473
3474/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003475 * xmlRelaxNGParseValue:
3476 * @ctxt: a Relax-NG parser context
3477 * @node: the data node.
3478 *
3479 * parse the content of a RelaxNG value node.
3480 *
3481 * Returns the definition pointer or NULL in case of error
3482 */
3483static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003484xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3485{
Daniel Veillardedc91922003-01-26 00:52:04 +00003486 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003487 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003488 xmlChar *type;
3489 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003490 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003491
Daniel Veillardfd573f12003-03-16 17:52:32 +00003492 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003493 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003494 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003495 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003496
3497 type = xmlGetProp(node, BAD_CAST "type");
3498 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003499 xmlRelaxNGNormExtSpace(type);
3500 if (xmlValidateNCName(type, 0)) {
3501 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3502 "value type '%s' is not an NCName\n", type, NULL);
3503 }
3504 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3505 if (library == NULL)
3506 library =
3507 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003508
Daniel Veillard4c004142003-10-07 11:33:24 +00003509 def->name = type;
3510 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003511
Daniel Veillard4c004142003-10-07 11:33:24 +00003512 lib = (xmlRelaxNGTypeLibraryPtr)
3513 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3514 if (lib == NULL) {
3515 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3516 "Use of unregistered type library '%s'\n", library,
3517 NULL);
3518 def->data = NULL;
3519 } else {
3520 def->data = lib;
3521 if (lib->have == NULL) {
3522 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3523 "Internal error with type library '%s': no 'have'\n",
3524 library, NULL);
3525 } else {
3526 success = lib->have(lib->data, def->name);
3527 if (success != 1) {
3528 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3529 "Error type '%s' is not exported by type library '%s'\n",
3530 def->name, library);
3531 }
3532 }
3533 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003534 }
3535 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003536 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003537 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003538 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3539 (node->children->next != NULL)) {
3540 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3541 "Expecting a single text value for <value>content\n",
3542 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003543 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003544 def->value = xmlNodeGetContent(node);
3545 if (def->value == NULL) {
3546 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3547 "Element <value> has no content\n", NULL, NULL);
3548 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3549 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003550
Daniel Veillard4c004142003-10-07 11:33:24 +00003551 success =
3552 lib->check(lib->data, def->name, def->value, &val, node);
3553 if (success != 1) {
3554 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3555 "Value '%s' is not acceptable for type '%s'\n",
3556 def->value, def->name);
3557 } else {
3558 if (val != NULL)
3559 def->attrs = val;
3560 }
3561 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003562 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003563 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003564}
3565
3566/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003567 * xmlRelaxNGParseData:
3568 * @ctxt: a Relax-NG parser context
3569 * @node: the data node.
3570 *
3571 * parse the content of a RelaxNG data node.
3572 *
3573 * Returns the definition pointer or NULL in case of error
3574 */
3575static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003576xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3577{
Daniel Veillard416589a2003-02-17 17:25:42 +00003578 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003579 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003580 xmlRelaxNGTypeLibraryPtr lib;
3581 xmlChar *type;
3582 xmlChar *library;
3583 xmlNodePtr content;
3584 int tmp;
3585
3586 type = xmlGetProp(node, BAD_CAST "type");
3587 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003588 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3589 NULL);
3590 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003591 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003592 xmlRelaxNGNormExtSpace(type);
3593 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003594 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3595 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003596 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003597 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3598 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003599 library =
3600 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003601
Daniel Veillardfd573f12003-03-16 17:52:32 +00003602 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003603 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003604 xmlFree(type);
3605 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003606 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003607 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003608 def->name = type;
3609 def->ns = library;
3610
3611 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003612 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003613 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003614 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3615 "Use of unregistered type library '%s'\n", library,
3616 NULL);
3617 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003618 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003619 def->data = lib;
3620 if (lib->have == NULL) {
3621 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3622 "Internal error with type library '%s': no 'have'\n",
3623 library, NULL);
3624 } else {
3625 tmp = lib->have(lib->data, def->name);
3626 if (tmp != 1) {
3627 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3628 "Error type '%s' is not exported by type library '%s'\n",
3629 def->name, library);
3630 } else
3631 if ((xmlStrEqual
3632 (library,
3633 BAD_CAST
3634 "http://www.w3.org/2001/XMLSchema-datatypes"))
3635 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3636 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3637 ctxt->idref = 1;
3638 }
3639 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003640 }
3641 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003642
3643 /*
3644 * Handle optional params
3645 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003646 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003647 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3648 break;
3649 if (xmlStrEqual(library,
3650 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3651 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3652 "Type library '%s' does not allow type parameters\n",
3653 library, NULL);
3654 content = content->next;
3655 while ((content != NULL) &&
3656 (xmlStrEqual(content->name, BAD_CAST "param")))
3657 content = content->next;
3658 } else {
3659 param = xmlRelaxNGNewDefine(ctxt, node);
3660 if (param != NULL) {
3661 param->type = XML_RELAXNG_PARAM;
3662 param->name = xmlGetProp(content, BAD_CAST "name");
3663 if (param->name == NULL) {
3664 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3665 "param has no name\n", NULL, NULL);
3666 }
3667 param->value = xmlNodeGetContent(content);
3668 if (lastparam == NULL) {
3669 def->attrs = lastparam = param;
3670 } else {
3671 lastparam->next = param;
3672 lastparam = param;
3673 }
3674 if (lib != NULL) {
3675 }
3676 }
3677 content = content->next;
3678 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003679 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003680 /*
3681 * Handle optional except
3682 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003683 if ((content != NULL)
3684 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3685 xmlNodePtr child;
3686 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003687
Daniel Veillard4c004142003-10-07 11:33:24 +00003688 except = xmlRelaxNGNewDefine(ctxt, node);
3689 if (except == NULL) {
3690 return (def);
3691 }
3692 except->type = XML_RELAXNG_EXCEPT;
3693 child = content->children;
3694 if (last == NULL) {
3695 def->content = except;
3696 } else {
3697 last->next = except;
3698 }
3699 if (child == NULL) {
3700 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3701 "except has no content\n", NULL, NULL);
3702 }
3703 while (child != NULL) {
3704 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3705 if (tmp2 != NULL) {
3706 if (last2 == NULL) {
3707 except->content = last2 = tmp2;
3708 } else {
3709 last2->next = tmp2;
3710 last2 = tmp2;
3711 }
3712 }
3713 child = child->next;
3714 }
3715 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003716 }
3717 /*
3718 * Check there is no unhandled data
3719 */
3720 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003721 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3722 "Element data has unexpected content %s\n",
3723 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003724 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003725
Daniel Veillard4c004142003-10-07 11:33:24 +00003726 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003727}
3728
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003729static const xmlChar *invalidName = BAD_CAST "\1";
3730
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003731/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003732 * xmlRelaxNGCompareNameClasses:
3733 * @defs1: the first element/attribute defs
3734 * @defs2: the second element/attribute defs
3735 * @name: the restriction on the name
3736 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003737 *
3738 * Compare the 2 lists of element definitions. The comparison is
3739 * that if both lists do not accept the same QNames, it returns 1
3740 * If the 2 lists can accept the same QName the comparison returns 0
3741 *
3742 * Returns 1 disttinct, 0 if equal
3743 */
3744static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003745xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003746 xmlRelaxNGDefinePtr def2)
3747{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003748 int ret = 1;
3749 xmlNode node;
3750 xmlNs ns;
3751 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003752
Daniel Veillard42f12e92003-03-07 18:32:59 +00003753 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3754
Daniel Veillardb30ca312005-09-04 13:50:03 +00003755 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3756
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003757 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003758 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3759 if (def2->type == XML_RELAXNG_TEXT)
3760 return (1);
3761 if (def1->name != NULL) {
3762 node.name = def1->name;
3763 } else {
3764 node.name = invalidName;
3765 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003766 if (def1->ns != NULL) {
3767 if (def1->ns[0] == 0) {
3768 node.ns = NULL;
3769 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003770 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003771 ns.href = def1->ns;
3772 }
3773 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003774 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003775 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003776 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003777 if (def1->nameClass != NULL) {
3778 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3779 } else {
3780 ret = 0;
3781 }
3782 } else {
3783 ret = 1;
3784 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003785 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003786 if (def2->type == XML_RELAXNG_TEXT)
3787 return (0);
3788 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003789 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003790 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003791 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003792 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003793 }
3794 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003795 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003796 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003797 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3798 if (def2->name != NULL) {
3799 node.name = def2->name;
3800 } else {
3801 node.name = invalidName;
3802 }
3803 node.ns = &ns;
3804 if (def2->ns != NULL) {
3805 if (def2->ns[0] == 0) {
3806 node.ns = NULL;
3807 } else {
3808 ns.href = def2->ns;
3809 }
3810 } else {
3811 ns.href = invalidName;
3812 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003813 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003814 if (def2->nameClass != NULL) {
3815 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3816 } else {
3817 ret = 0;
3818 }
3819 } else {
3820 ret = 1;
3821 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003822 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003823 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003824 }
3825
Daniel Veillard4c004142003-10-07 11:33:24 +00003826 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003827}
3828
3829/**
3830 * xmlRelaxNGCompareElemDefLists:
3831 * @ctxt: a Relax-NG parser context
3832 * @defs1: the first list of element/attribute defs
3833 * @defs2: the second list of element/attribute defs
3834 *
3835 * Compare the 2 lists of element or attribute definitions. The comparison
3836 * is that if both lists do not accept the same QNames, it returns 1
3837 * If the 2 lists can accept the same QName the comparison returns 0
3838 *
3839 * Returns 1 disttinct, 0 if equal
3840 */
3841static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003842xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3843 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3844 xmlRelaxNGDefinePtr * def2)
3845{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003846 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003847
Daniel Veillard154877e2003-01-30 12:17:05 +00003848 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003849 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003850 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003851 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003852 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003853 while ((*def2) != NULL) {
3854 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3855 return (0);
3856 def2++;
3857 }
3858 def2 = basedef2;
3859 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003860 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003861 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003862}
3863
3864/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003865 * xmlRelaxNGGenerateAttributes:
3866 * @ctxt: a Relax-NG parser context
3867 * @def: the definition definition
3868 *
3869 * Check if the definition can only generate attributes
3870 *
3871 * Returns 1 if yes, 0 if no and -1 in case of error.
3872 */
3873static int
3874xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003875 xmlRelaxNGDefinePtr def)
3876{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003877 xmlRelaxNGDefinePtr parent, cur, tmp;
3878
3879 /*
3880 * Don't run that check in case of error. Infinite recursion
3881 * becomes possible.
3882 */
3883 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003884 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003885
3886 parent = NULL;
3887 cur = def;
3888 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003889 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3890 (cur->type == XML_RELAXNG_TEXT) ||
3891 (cur->type == XML_RELAXNG_DATATYPE) ||
3892 (cur->type == XML_RELAXNG_PARAM) ||
3893 (cur->type == XML_RELAXNG_LIST) ||
3894 (cur->type == XML_RELAXNG_VALUE) ||
3895 (cur->type == XML_RELAXNG_EMPTY))
3896 return (0);
3897 if ((cur->type == XML_RELAXNG_CHOICE) ||
3898 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3899 (cur->type == XML_RELAXNG_GROUP) ||
3900 (cur->type == XML_RELAXNG_ONEORMORE) ||
3901 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3902 (cur->type == XML_RELAXNG_OPTIONAL) ||
3903 (cur->type == XML_RELAXNG_PARENTREF) ||
3904 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3905 (cur->type == XML_RELAXNG_REF) ||
3906 (cur->type == XML_RELAXNG_DEF)) {
3907 if (cur->content != NULL) {
3908 parent = cur;
3909 cur = cur->content;
3910 tmp = cur;
3911 while (tmp != NULL) {
3912 tmp->parent = parent;
3913 tmp = tmp->next;
3914 }
3915 continue;
3916 }
3917 }
3918 if (cur == def)
3919 break;
3920 if (cur->next != NULL) {
3921 cur = cur->next;
3922 continue;
3923 }
3924 do {
3925 cur = cur->parent;
3926 if (cur == NULL)
3927 break;
3928 if (cur == def)
3929 return (1);
3930 if (cur->next != NULL) {
3931 cur = cur->next;
3932 break;
3933 }
3934 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003935 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003936 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003937}
Daniel Veillard4c004142003-10-07 11:33:24 +00003938
Daniel Veillardce192eb2003-04-16 15:58:05 +00003939/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003940 * xmlRelaxNGGetElements:
3941 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003942 * @def: the definition definition
3943 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003944 *
3945 * Compute the list of top elements a definition can generate
3946 *
3947 * Returns a list of elements or NULL if none was found.
3948 */
3949static xmlRelaxNGDefinePtr *
3950xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003951 xmlRelaxNGDefinePtr def, int eora)
3952{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003953 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003954 int len = 0;
3955 int max = 0;
3956
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003957 /*
3958 * Don't run that check in case of error. Infinite recursion
3959 * becomes possible.
3960 */
3961 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003962 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003963
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003964 parent = NULL;
3965 cur = def;
3966 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003967 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3968 (cur->type == XML_RELAXNG_TEXT))) ||
3969 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3970 if (ret == NULL) {
3971 max = 10;
3972 ret = (xmlRelaxNGDefinePtr *)
3973 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3974 if (ret == NULL) {
3975 xmlRngPErrMemory(ctxt, "getting element list\n");
3976 return (NULL);
3977 }
3978 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00003979 xmlRelaxNGDefinePtr *temp;
3980
Daniel Veillard4c004142003-10-07 11:33:24 +00003981 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00003982 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00003983 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00003984 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003985 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00003986 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00003987 return (NULL);
3988 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00003989 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00003990 }
3991 ret[len++] = cur;
3992 ret[len] = NULL;
3993 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3994 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3995 (cur->type == XML_RELAXNG_GROUP) ||
3996 (cur->type == XML_RELAXNG_ONEORMORE) ||
3997 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3998 (cur->type == XML_RELAXNG_OPTIONAL) ||
3999 (cur->type == XML_RELAXNG_PARENTREF) ||
4000 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004001 (cur->type == XML_RELAXNG_DEF) ||
4002 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004003 /*
4004 * Don't go within elements or attributes or string values.
4005 * Just gather the element top list
4006 */
4007 if (cur->content != NULL) {
4008 parent = cur;
4009 cur = cur->content;
4010 tmp = cur;
4011 while (tmp != NULL) {
4012 tmp->parent = parent;
4013 tmp = tmp->next;
4014 }
4015 continue;
4016 }
4017 }
4018 if (cur == def)
4019 break;
4020 if (cur->next != NULL) {
4021 cur = cur->next;
4022 continue;
4023 }
4024 do {
4025 cur = cur->parent;
4026 if (cur == NULL)
4027 break;
4028 if (cur == def)
4029 return (ret);
4030 if (cur->next != NULL) {
4031 cur = cur->next;
4032 break;
4033 }
4034 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004035 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004036 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004037}
Daniel Veillard4c004142003-10-07 11:33:24 +00004038
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004039/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004040 * xmlRelaxNGCheckChoiceDeterminism:
4041 * @ctxt: a Relax-NG parser context
4042 * @def: the choice definition
4043 *
4044 * Also used to find indeterministic pattern in choice
4045 */
4046static void
4047xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004048 xmlRelaxNGDefinePtr def)
4049{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004050 xmlRelaxNGDefinePtr **list;
4051 xmlRelaxNGDefinePtr cur;
4052 int nbchild = 0, i, j, ret;
4053 int is_nullable = 0;
4054 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004055 xmlHashTablePtr triage = NULL;
4056 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004057
Daniel Veillard4c004142003-10-07 11:33:24 +00004058 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4059 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004060
Daniel Veillarde063f482003-03-21 16:53:17 +00004061 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004062 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004063
Daniel Veillardfd573f12003-03-16 17:52:32 +00004064 /*
4065 * Don't run that check in case of error. Infinite recursion
4066 * becomes possible.
4067 */
4068 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004069 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004070
4071 is_nullable = xmlRelaxNGIsNullable(def);
4072
4073 cur = def->content;
4074 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004075 nbchild++;
4076 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004077 }
4078
4079 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004080 sizeof(xmlRelaxNGDefinePtr
4081 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004082 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004083 xmlRngPErrMemory(ctxt, "building choice\n");
4084 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004085 }
4086 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004087 /*
4088 * a bit strong but safe
4089 */
4090 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004091 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004092 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004093 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004094 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004095 cur = def->content;
4096 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004097 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4098 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4099 is_triable = 0;
4100 } else if (is_triable == 1) {
4101 xmlRelaxNGDefinePtr *tmp;
4102 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004103
Daniel Veillard4c004142003-10-07 11:33:24 +00004104 tmp = list[i];
4105 while ((*tmp != NULL) && (is_triable == 1)) {
4106 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4107 res = xmlHashAddEntry2(triage,
4108 BAD_CAST "#text", NULL,
4109 (void *) cur);
4110 if (res != 0)
4111 is_triable = -1;
4112 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4113 ((*tmp)->name != NULL)) {
4114 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4115 res = xmlHashAddEntry2(triage,
4116 (*tmp)->name, NULL,
4117 (void *) cur);
4118 else
4119 res = xmlHashAddEntry2(triage,
4120 (*tmp)->name, (*tmp)->ns,
4121 (void *) cur);
4122 if (res != 0)
4123 is_triable = -1;
4124 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4125 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4126 res = xmlHashAddEntry2(triage,
4127 BAD_CAST "#any", NULL,
4128 (void *) cur);
4129 else
4130 res = xmlHashAddEntry2(triage,
4131 BAD_CAST "#any", (*tmp)->ns,
4132 (void *) cur);
4133 if (res != 0)
4134 is_triable = -1;
4135 } else {
4136 is_triable = -1;
4137 }
4138 tmp++;
4139 }
4140 }
4141 i++;
4142 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004143 }
4144
Daniel Veillard4c004142003-10-07 11:33:24 +00004145 for (i = 0; i < nbchild; i++) {
4146 if (list[i] == NULL)
4147 continue;
4148 for (j = 0; j < i; j++) {
4149 if (list[j] == NULL)
4150 continue;
4151 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4152 if (ret == 0) {
4153 is_indeterminist = 1;
4154 }
4155 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004156 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004157 for (i = 0; i < nbchild; i++) {
4158 if (list[i] != NULL)
4159 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004160 }
4161
4162 xmlFree(list);
4163 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004164 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004165 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004166 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004167 def->dflags |= IS_TRIABLE;
4168 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004169 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004170 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004171 }
4172 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004173}
4174
4175/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004176 * xmlRelaxNGCheckGroupAttrs:
4177 * @ctxt: a Relax-NG parser context
4178 * @def: the group definition
4179 *
4180 * Detects violations of rule 7.3
4181 */
4182static void
4183xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004184 xmlRelaxNGDefinePtr def)
4185{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004186 xmlRelaxNGDefinePtr **list;
4187 xmlRelaxNGDefinePtr cur;
4188 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004189
4190 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004191 ((def->type != XML_RELAXNG_GROUP) &&
4192 (def->type != XML_RELAXNG_ELEMENT)))
4193 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004194
Daniel Veillarde063f482003-03-21 16:53:17 +00004195 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004196 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004197
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004198 /*
4199 * Don't run that check in case of error. Infinite recursion
4200 * becomes possible.
4201 */
4202 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004203 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004204
Daniel Veillardfd573f12003-03-16 17:52:32 +00004205 cur = def->attrs;
4206 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004207 nbchild++;
4208 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004209 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004210 cur = def->content;
4211 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004212 nbchild++;
4213 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004214 }
4215
4216 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004217 sizeof(xmlRelaxNGDefinePtr
4218 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004219 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004220 xmlRngPErrMemory(ctxt, "building group\n");
4221 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004222 }
4223 i = 0;
4224 cur = def->attrs;
4225 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004226 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4227 i++;
4228 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004229 }
4230 cur = def->content;
4231 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004232 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4233 i++;
4234 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004235 }
4236
Daniel Veillard4c004142003-10-07 11:33:24 +00004237 for (i = 0; i < nbchild; i++) {
4238 if (list[i] == NULL)
4239 continue;
4240 for (j = 0; j < i; j++) {
4241 if (list[j] == NULL)
4242 continue;
4243 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4244 if (ret == 0) {
4245 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4246 "Attributes conflicts in group\n", NULL, NULL);
4247 }
4248 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004249 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004250 for (i = 0; i < nbchild; i++) {
4251 if (list[i] != NULL)
4252 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004253 }
4254
4255 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004256 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004257}
4258
4259/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004260 * xmlRelaxNGComputeInterleaves:
4261 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004262 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004263 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004264 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004265 * A lot of work for preprocessing interleave definitions
4266 * is potentially needed to get a decent execution speed at runtime
4267 * - trying to get a total order on the element nodes generated
4268 * by the interleaves, order the list of interleave definitions
4269 * following that order.
4270 * - if <text/> is used to handle mixed content, it is better to
4271 * flag this in the define and simplify the runtime checking
4272 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004273 */
4274static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004275xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004276 xmlRelaxNGParserCtxtPtr ctxt,
4277 xmlChar * name ATTRIBUTE_UNUSED)
4278{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004279 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004280
Daniel Veillardfd573f12003-03-16 17:52:32 +00004281 xmlRelaxNGPartitionPtr partitions = NULL;
4282 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4283 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004284 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004285 int nbgroups = 0;
4286 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004287 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004288 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004289
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004290 /*
4291 * Don't run that check in case of error. Infinite recursion
4292 * becomes possible.
4293 */
4294 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004295 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004296
Daniel Veillardfd573f12003-03-16 17:52:32 +00004297#ifdef DEBUG_INTERLEAVE
4298 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004299 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004300#endif
4301 cur = def->content;
4302 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004303 nbchild++;
4304 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004305 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004306
Daniel Veillardfd573f12003-03-16 17:52:32 +00004307#ifdef DEBUG_INTERLEAVE
4308 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4309#endif
4310 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004311 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004312 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004313 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004314 cur = def->content;
4315 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004316 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4317 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4318 if (groups[nbgroups] == NULL)
4319 goto error;
4320 if (cur->type == XML_RELAXNG_TEXT)
4321 is_mixed++;
4322 groups[nbgroups]->rule = cur;
4323 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4324 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4325 nbgroups++;
4326 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004327 }
4328#ifdef DEBUG_INTERLEAVE
4329 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4330#endif
4331
4332 /*
4333 * Let's check that all rules makes a partitions according to 7.4
4334 */
4335 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004336 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004337 if (partitions == NULL)
4338 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004339 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004340 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004341 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004342 for (i = 0; i < nbgroups; i++) {
4343 group = groups[i];
4344 for (j = i + 1; j < nbgroups; j++) {
4345 if (groups[j] == NULL)
4346 continue;
4347
4348 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4349 groups[j]->defs);
4350 if (ret == 0) {
4351 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4352 "Element or text conflicts in interleave\n",
4353 NULL, NULL);
4354 }
4355 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4356 groups[j]->attrs);
4357 if (ret == 0) {
4358 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4359 "Attributes conflicts in interleave\n", NULL,
4360 NULL);
4361 }
4362 }
4363 tmp = group->defs;
4364 if ((tmp != NULL) && (*tmp != NULL)) {
4365 while (*tmp != NULL) {
4366 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4367 res = xmlHashAddEntry2(partitions->triage,
4368 BAD_CAST "#text", NULL,
4369 (void *) (long) (i + 1));
4370 if (res != 0)
4371 is_determinist = -1;
4372 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4373 ((*tmp)->name != NULL)) {
4374 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4375 res = xmlHashAddEntry2(partitions->triage,
4376 (*tmp)->name, NULL,
4377 (void *) (long) (i + 1));
4378 else
4379 res = xmlHashAddEntry2(partitions->triage,
4380 (*tmp)->name, (*tmp)->ns,
4381 (void *) (long) (i + 1));
4382 if (res != 0)
4383 is_determinist = -1;
4384 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4385 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4386 res = xmlHashAddEntry2(partitions->triage,
4387 BAD_CAST "#any", NULL,
4388 (void *) (long) (i + 1));
4389 else
4390 res = xmlHashAddEntry2(partitions->triage,
4391 BAD_CAST "#any", (*tmp)->ns,
4392 (void *) (long) (i + 1));
4393 if ((*tmp)->nameClass != NULL)
4394 is_determinist = 2;
4395 if (res != 0)
4396 is_determinist = -1;
4397 } else {
4398 is_determinist = -1;
4399 }
4400 tmp++;
4401 }
4402 } else {
4403 is_determinist = 0;
4404 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004405 }
4406 partitions->groups = groups;
4407
4408 /*
4409 * and save the partition list back in the def
4410 */
4411 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004412 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004413 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004414 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004415 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004416 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004417 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004418 return;
4419
Daniel Veillard4c004142003-10-07 11:33:24 +00004420 error:
4421 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004422 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004423 for (i = 0; i < nbgroups; i++)
4424 if (groups[i] != NULL) {
4425 if (groups[i]->defs != NULL)
4426 xmlFree(groups[i]->defs);
4427 xmlFree(groups[i]);
4428 }
4429 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004430 }
4431 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004432}
4433
4434/**
4435 * xmlRelaxNGParseInterleave:
4436 * @ctxt: a Relax-NG parser context
4437 * @node: the data node.
4438 *
4439 * parse the content of a RelaxNG interleave node.
4440 *
4441 * Returns the definition pointer or NULL in case of error
4442 */
4443static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004444xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4445{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004446 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004447 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004448 xmlNodePtr child;
4449
Daniel Veillardfd573f12003-03-16 17:52:32 +00004450 def = xmlRelaxNGNewDefine(ctxt, node);
4451 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004452 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004453 }
4454 def->type = XML_RELAXNG_INTERLEAVE;
4455
4456 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004457 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004458 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004459 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004460 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004461 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004462
Daniel Veillard4c004142003-10-07 11:33:24 +00004463 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4464 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4465 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4466 "Failed to add %s to hash table\n",
4467 (const xmlChar *) name, NULL);
4468 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004469 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004470 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004471 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004472 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4473 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004474 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004475 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004476 if (IS_RELAXNG(child, "element")) {
4477 cur = xmlRelaxNGParseElement(ctxt, child);
4478 } else {
4479 cur = xmlRelaxNGParsePattern(ctxt, child);
4480 }
4481 if (cur != NULL) {
4482 cur->parent = def;
4483 if (last == NULL) {
4484 def->content = last = cur;
4485 } else {
4486 last->next = cur;
4487 last = cur;
4488 }
4489 }
4490 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004491 }
4492
Daniel Veillard4c004142003-10-07 11:33:24 +00004493 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004494}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004495
4496/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004497 * xmlRelaxNGParseInclude:
4498 * @ctxt: a Relax-NG parser context
4499 * @node: the include node
4500 *
4501 * Integrate the content of an include node in the current grammar
4502 *
4503 * Returns 0 in case of success or -1 in case of error
4504 */
4505static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004506xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4507{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004508 xmlRelaxNGIncludePtr incl;
4509 xmlNodePtr root;
4510 int ret = 0, tmp;
4511
Daniel Veillard807daf82004-02-22 22:13:27 +00004512 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004513 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004514 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4515 "Include node has no data\n", NULL, NULL);
4516 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004517 }
4518 root = xmlDocGetRootElement(incl->doc);
4519 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004520 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4521 NULL, NULL);
4522 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004523 }
4524 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004525 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4526 "Include document root is not a grammar\n", NULL, NULL);
4527 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004528 }
4529
4530 /*
4531 * Merge the definition from both the include and the internal list
4532 */
4533 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004534 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4535 if (tmp != 0)
4536 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004537 }
4538 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004539 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4540 if (tmp != 0)
4541 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004542 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004543 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004544}
4545
4546/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004547 * xmlRelaxNGParseDefine:
4548 * @ctxt: a Relax-NG parser context
4549 * @node: the define node
4550 *
4551 * parse the content of a RelaxNG define element node.
4552 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004553 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004554 */
4555static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004556xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4557{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004558 xmlChar *name;
4559 int ret = 0, tmp;
4560 xmlRelaxNGDefinePtr def;
4561 const xmlChar *olddefine;
4562
4563 name = xmlGetProp(node, BAD_CAST "name");
4564 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004565 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4566 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004567 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004568 xmlRelaxNGNormExtSpace(name);
4569 if (xmlValidateNCName(name, 0)) {
4570 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4571 "define name '%s' is not an NCName\n", name, NULL);
4572 }
4573 def = xmlRelaxNGNewDefine(ctxt, node);
4574 if (def == NULL) {
4575 xmlFree(name);
4576 return (-1);
4577 }
4578 def->type = XML_RELAXNG_DEF;
4579 def->name = name;
4580 if (node->children == NULL) {
4581 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4582 "define has no children\n", NULL, NULL);
4583 } else {
4584 olddefine = ctxt->define;
4585 ctxt->define = name;
4586 def->content =
4587 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4588 ctxt->define = olddefine;
4589 }
4590 if (ctxt->grammar->defs == NULL)
4591 ctxt->grammar->defs = xmlHashCreate(10);
4592 if (ctxt->grammar->defs == NULL) {
4593 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4594 "Could not create definition hash\n", NULL, NULL);
4595 ret = -1;
4596 } else {
4597 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4598 if (tmp < 0) {
4599 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004600
Daniel Veillard4c004142003-10-07 11:33:24 +00004601 prev = xmlHashLookup(ctxt->grammar->defs, name);
4602 if (prev == NULL) {
4603 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4604 "Internal error on define aggregation of %s\n",
4605 name, NULL);
4606 ret = -1;
4607 } else {
4608 while (prev->nextHash != NULL)
4609 prev = prev->nextHash;
4610 prev->nextHash = def;
4611 }
4612 }
4613 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004614 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004615 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004616}
4617
4618/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004619 * xmlRelaxNGProcessExternalRef:
4620 * @ctxt: the parser context
4621 * @node: the externlRef node
4622 *
4623 * Process and compile an externlRef node
4624 *
4625 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4626 */
4627static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004628xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4629{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004630 xmlRelaxNGDocumentPtr docu;
4631 xmlNodePtr root, tmp;
4632 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004633 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004634 xmlRelaxNGDefinePtr def;
4635
Daniel Veillard807daf82004-02-22 22:13:27 +00004636 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004637 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004638 def = xmlRelaxNGNewDefine(ctxt, node);
4639 if (def == NULL)
4640 return (NULL);
4641 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004642
Daniel Veillard4c004142003-10-07 11:33:24 +00004643 if (docu->content == NULL) {
4644 /*
4645 * Then do the parsing for good
4646 */
4647 root = xmlDocGetRootElement(docu->doc);
4648 if (root == NULL) {
4649 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4650 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4651 NULL);
4652 return (NULL);
4653 }
4654 /*
4655 * ns transmission rules
4656 */
4657 ns = xmlGetProp(root, BAD_CAST "ns");
4658 if (ns == NULL) {
4659 tmp = node;
4660 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4661 ns = xmlGetProp(tmp, BAD_CAST "ns");
4662 if (ns != NULL) {
4663 break;
4664 }
4665 tmp = tmp->parent;
4666 }
4667 if (ns != NULL) {
4668 xmlSetProp(root, BAD_CAST "ns", ns);
4669 newNs = 1;
4670 xmlFree(ns);
4671 }
4672 } else {
4673 xmlFree(ns);
4674 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004675
Daniel Veillard4c004142003-10-07 11:33:24 +00004676 /*
4677 * Parsing to get a precompiled schemas.
4678 */
4679 oldflags = ctxt->flags;
4680 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4681 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4682 ctxt->flags = oldflags;
4683 if ((docu->schema != NULL) &&
4684 (docu->schema->topgrammar != NULL)) {
4685 docu->content = docu->schema->topgrammar->start;
4686 }
4687
4688 /*
4689 * the externalRef may be reused in a different ns context
4690 */
4691 if (newNs == 1) {
4692 xmlUnsetProp(root, BAD_CAST "ns");
4693 }
4694 }
4695 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004696 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004697 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004698 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004699 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004700}
4701
4702/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004703 * xmlRelaxNGParsePattern:
4704 * @ctxt: a Relax-NG parser context
4705 * @node: the pattern node.
4706 *
4707 * parse the content of a RelaxNG pattern node.
4708 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004709 * Returns the definition pointer or NULL in case of error or if no
4710 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004711 */
4712static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004713xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4714{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004715 xmlRelaxNGDefinePtr def = NULL;
4716
Daniel Veillardd2298792003-02-14 16:54:11 +00004717 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004718 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004719 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004720 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004721 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004722 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004723 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004724 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004725 def = xmlRelaxNGNewDefine(ctxt, node);
4726 if (def == NULL)
4727 return (NULL);
4728 def->type = XML_RELAXNG_EMPTY;
4729 if (node->children != NULL) {
4730 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4731 "empty: had a child node\n", NULL, NULL);
4732 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004733 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004734 def = xmlRelaxNGNewDefine(ctxt, node);
4735 if (def == NULL)
4736 return (NULL);
4737 def->type = XML_RELAXNG_TEXT;
4738 if (node->children != NULL) {
4739 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4740 "text: had a child node\n", NULL, NULL);
4741 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004742 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004743 def = xmlRelaxNGNewDefine(ctxt, node);
4744 if (def == NULL)
4745 return (NULL);
4746 def->type = XML_RELAXNG_ZEROORMORE;
4747 if (node->children == NULL) {
4748 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4749 "Element %s is empty\n", node->name, NULL);
4750 } else {
4751 def->content =
4752 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4753 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004754 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004755 def = xmlRelaxNGNewDefine(ctxt, node);
4756 if (def == NULL)
4757 return (NULL);
4758 def->type = XML_RELAXNG_ONEORMORE;
4759 if (node->children == NULL) {
4760 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4761 "Element %s is empty\n", node->name, NULL);
4762 } else {
4763 def->content =
4764 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4765 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004766 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004767 def = xmlRelaxNGNewDefine(ctxt, node);
4768 if (def == NULL)
4769 return (NULL);
4770 def->type = XML_RELAXNG_OPTIONAL;
4771 if (node->children == NULL) {
4772 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4773 "Element %s is empty\n", node->name, NULL);
4774 } else {
4775 def->content =
4776 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4777 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004778 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004779 def = xmlRelaxNGNewDefine(ctxt, node);
4780 if (def == NULL)
4781 return (NULL);
4782 def->type = XML_RELAXNG_CHOICE;
4783 if (node->children == NULL) {
4784 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4785 "Element %s is empty\n", node->name, NULL);
4786 } else {
4787 def->content =
4788 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4789 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004790 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004791 def = xmlRelaxNGNewDefine(ctxt, node);
4792 if (def == NULL)
4793 return (NULL);
4794 def->type = XML_RELAXNG_GROUP;
4795 if (node->children == NULL) {
4796 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4797 "Element %s is empty\n", node->name, NULL);
4798 } else {
4799 def->content =
4800 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4801 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004802 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004803 def = xmlRelaxNGNewDefine(ctxt, node);
4804 if (def == NULL)
4805 return (NULL);
4806 def->type = XML_RELAXNG_REF;
4807 def->name = xmlGetProp(node, BAD_CAST "name");
4808 if (def->name == NULL) {
4809 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4810 NULL, NULL);
4811 } else {
4812 xmlRelaxNGNormExtSpace(def->name);
4813 if (xmlValidateNCName(def->name, 0)) {
4814 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4815 "ref name '%s' is not an NCName\n", def->name,
4816 NULL);
4817 }
4818 }
4819 if (node->children != NULL) {
4820 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4821 NULL, NULL);
4822 }
4823 if (ctxt->grammar->refs == NULL)
4824 ctxt->grammar->refs = xmlHashCreate(10);
4825 if (ctxt->grammar->refs == NULL) {
4826 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4827 "Could not create references hash\n", NULL, NULL);
4828 def = NULL;
4829 } else {
4830 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004831
Daniel Veillard4c004142003-10-07 11:33:24 +00004832 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4833 if (tmp < 0) {
4834 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004835
Daniel Veillard4c004142003-10-07 11:33:24 +00004836 prev = (xmlRelaxNGDefinePtr)
4837 xmlHashLookup(ctxt->grammar->refs, def->name);
4838 if (prev == NULL) {
4839 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004840 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4841 "Error refs definitions '%s'\n",
4842 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004843 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004844 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4845 "Error refs definitions\n",
4846 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004847 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004848 def = NULL;
4849 } else {
4850 def->nextHash = prev->nextHash;
4851 prev->nextHash = def;
4852 }
4853 }
4854 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004855 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004856 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004857 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004858 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004859 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004860 def = xmlRelaxNGNewDefine(ctxt, node);
4861 if (def == NULL)
4862 return (NULL);
4863 def->type = XML_RELAXNG_LIST;
4864 if (node->children == NULL) {
4865 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4866 "Element %s is empty\n", node->name, NULL);
4867 } else {
4868 def->content =
4869 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4870 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004871 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004872 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004873 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004874 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004875 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004876 def = xmlRelaxNGNewDefine(ctxt, node);
4877 if (def == NULL)
4878 return (NULL);
4879 def->type = XML_RELAXNG_NOT_ALLOWED;
4880 if (node->children != NULL) {
4881 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4882 "xmlRelaxNGParse: notAllowed element is not empty\n",
4883 NULL, NULL);
4884 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004885 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004886 xmlRelaxNGGrammarPtr grammar, old;
4887 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004888
Daniel Veillardc482e262003-02-26 14:48:48 +00004889#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004890 xmlGenericError(xmlGenericErrorContext,
4891 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004892#endif
4893
Daniel Veillard4c004142003-10-07 11:33:24 +00004894 oldparent = ctxt->parentgrammar;
4895 old = ctxt->grammar;
4896 ctxt->parentgrammar = old;
4897 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4898 if (old != NULL) {
4899 ctxt->grammar = old;
4900 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004901#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004902 if (grammar != NULL) {
4903 grammar->next = old->next;
4904 old->next = grammar;
4905 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004906#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004907 }
4908 if (grammar != NULL)
4909 def = grammar->start;
4910 else
4911 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004912 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004913 if (ctxt->parentgrammar == NULL) {
4914 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4915 "Use of parentRef without a parent grammar\n", NULL,
4916 NULL);
4917 return (NULL);
4918 }
4919 def = xmlRelaxNGNewDefine(ctxt, node);
4920 if (def == NULL)
4921 return (NULL);
4922 def->type = XML_RELAXNG_PARENTREF;
4923 def->name = xmlGetProp(node, BAD_CAST "name");
4924 if (def->name == NULL) {
4925 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4926 "parentRef has no name\n", NULL, NULL);
4927 } else {
4928 xmlRelaxNGNormExtSpace(def->name);
4929 if (xmlValidateNCName(def->name, 0)) {
4930 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4931 "parentRef name '%s' is not an NCName\n",
4932 def->name, NULL);
4933 }
4934 }
4935 if (node->children != NULL) {
4936 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4937 "parentRef is not empty\n", NULL, NULL);
4938 }
4939 if (ctxt->parentgrammar->refs == NULL)
4940 ctxt->parentgrammar->refs = xmlHashCreate(10);
4941 if (ctxt->parentgrammar->refs == NULL) {
4942 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4943 "Could not create references hash\n", NULL, NULL);
4944 def = NULL;
4945 } else if (def->name != NULL) {
4946 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00004947
Daniel Veillard4c004142003-10-07 11:33:24 +00004948 tmp =
4949 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4950 if (tmp < 0) {
4951 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00004952
Daniel Veillard4c004142003-10-07 11:33:24 +00004953 prev = (xmlRelaxNGDefinePtr)
4954 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4955 if (prev == NULL) {
4956 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4957 "Internal error parentRef definitions '%s'\n",
4958 def->name, NULL);
4959 def = NULL;
4960 } else {
4961 def->nextHash = prev->nextHash;
4962 prev->nextHash = def;
4963 }
4964 }
4965 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004966 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004967 if (node->children == NULL) {
4968 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4969 NULL, NULL);
4970 def = NULL;
4971 } else {
4972 def = xmlRelaxNGParseInterleave(ctxt, node);
4973 if (def != NULL) {
4974 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004975
Daniel Veillard4c004142003-10-07 11:33:24 +00004976 if ((def->content != NULL) && (def->content->next != NULL)) {
4977 tmp = xmlRelaxNGNewDefine(ctxt, node);
4978 if (tmp != NULL) {
4979 tmp->type = XML_RELAXNG_GROUP;
4980 tmp->content = def->content;
4981 def->content = tmp;
4982 }
4983 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004984
Daniel Veillard4c004142003-10-07 11:33:24 +00004985 tmp = xmlRelaxNGNewDefine(ctxt, node);
4986 if (tmp == NULL)
4987 return (def);
4988 tmp->type = XML_RELAXNG_TEXT;
4989 tmp->next = def->content;
4990 def->content = tmp;
4991 }
4992 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004993 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004994 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4995 "Unexpected node %s is not a pattern\n", node->name,
4996 NULL);
4997 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004998 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004999 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005000}
5001
5002/**
5003 * xmlRelaxNGParseAttribute:
5004 * @ctxt: a Relax-NG parser context
5005 * @node: the element node
5006 *
5007 * parse the content of a RelaxNG attribute node.
5008 *
5009 * Returns the definition pointer or NULL in case of error.
5010 */
5011static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005012xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5013{
Daniel Veillardd2298792003-02-14 16:54:11 +00005014 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005015 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005016 int old_flags;
5017
Daniel Veillardfd573f12003-03-16 17:52:32 +00005018 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005019 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005020 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005021 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005022 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005023 child = node->children;
5024 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005025 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5026 "xmlRelaxNGParseattribute: attribute has no children\n",
5027 NULL, NULL);
5028 return (ret);
5029 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005030 old_flags = ctxt->flags;
5031 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005032 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5033 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005034 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005035
Daniel Veillardd2298792003-02-14 16:54:11 +00005036 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005037 cur = xmlRelaxNGParsePattern(ctxt, child);
5038 if (cur != NULL) {
5039 switch (cur->type) {
5040 case XML_RELAXNG_EMPTY:
5041 case XML_RELAXNG_NOT_ALLOWED:
5042 case XML_RELAXNG_TEXT:
5043 case XML_RELAXNG_ELEMENT:
5044 case XML_RELAXNG_DATATYPE:
5045 case XML_RELAXNG_VALUE:
5046 case XML_RELAXNG_LIST:
5047 case XML_RELAXNG_REF:
5048 case XML_RELAXNG_PARENTREF:
5049 case XML_RELAXNG_EXTERNALREF:
5050 case XML_RELAXNG_DEF:
5051 case XML_RELAXNG_ONEORMORE:
5052 case XML_RELAXNG_ZEROORMORE:
5053 case XML_RELAXNG_OPTIONAL:
5054 case XML_RELAXNG_CHOICE:
5055 case XML_RELAXNG_GROUP:
5056 case XML_RELAXNG_INTERLEAVE:
5057 case XML_RELAXNG_ATTRIBUTE:
5058 ret->content = cur;
5059 cur->parent = ret;
5060 break;
5061 case XML_RELAXNG_START:
5062 case XML_RELAXNG_PARAM:
5063 case XML_RELAXNG_EXCEPT:
5064 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5065 "attribute has invalid content\n", NULL,
5066 NULL);
5067 break;
5068 case XML_RELAXNG_NOOP:
5069 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5070 "RNG Internal error, noop found in attribute\n",
5071 NULL, NULL);
5072 break;
5073 }
5074 }
5075 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005076 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005077 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005078 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5079 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005080 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005081 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005082 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005083}
5084
5085/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005086 * xmlRelaxNGParseExceptNameClass:
5087 * @ctxt: a Relax-NG parser context
5088 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005089 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005090 *
5091 * parse the content of a RelaxNG nameClass node.
5092 *
5093 * Returns the definition pointer or NULL in case of error.
5094 */
5095static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005096xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005097 xmlNodePtr node, int attr)
5098{
Daniel Veillard144fae12003-02-03 13:17:57 +00005099 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5100 xmlNodePtr child;
5101
Daniel Veillardd2298792003-02-14 16:54:11 +00005102 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005103 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5104 "Expecting an except node\n", NULL, NULL);
5105 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005106 }
5107 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005108 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5109 "exceptNameClass allows only a single except node\n",
5110 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005111 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005112 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005113 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5114 NULL, NULL);
5115 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005116 }
5117
Daniel Veillardfd573f12003-03-16 17:52:32 +00005118 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005119 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005120 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005121 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005122 child = node->children;
5123 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005124 cur = xmlRelaxNGNewDefine(ctxt, child);
5125 if (cur == NULL)
5126 break;
5127 if (attr)
5128 cur->type = XML_RELAXNG_ATTRIBUTE;
5129 else
5130 cur->type = XML_RELAXNG_ELEMENT;
5131
Daniel Veillard419a7682003-02-03 23:22:49 +00005132 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005133 if (last == NULL) {
5134 ret->content = cur;
5135 } else {
5136 last->next = cur;
5137 }
5138 last = cur;
5139 }
5140 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005141 }
5142
Daniel Veillard4c004142003-10-07 11:33:24 +00005143 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005144}
5145
5146/**
5147 * xmlRelaxNGParseNameClass:
5148 * @ctxt: a Relax-NG parser context
5149 * @node: the nameClass node
5150 * @def: the current definition
5151 *
5152 * parse the content of a RelaxNG nameClass node.
5153 *
5154 * Returns the definition pointer or NULL in case of error.
5155 */
5156static xmlRelaxNGDefinePtr
5157xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005158 xmlRelaxNGDefinePtr def)
5159{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005160 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005161 xmlChar *val;
5162
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005163 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005164 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005165 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005166 if ((def->type != XML_RELAXNG_ELEMENT) &&
5167 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5168 ret = xmlRelaxNGNewDefine(ctxt, node);
5169 if (ret == NULL)
5170 return (NULL);
5171 ret->parent = def;
5172 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5173 ret->type = XML_RELAXNG_ATTRIBUTE;
5174 else
5175 ret->type = XML_RELAXNG_ELEMENT;
5176 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005177 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005178 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005179 val = xmlNodeGetContent(node);
5180 xmlRelaxNGNormExtSpace(val);
5181 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005182 if (node->parent != NULL)
5183 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5184 "Element %s name '%s' is not an NCName\n",
5185 node->parent->name, val);
5186 else
5187 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5188 "name '%s' is not an NCName\n",
5189 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005190 }
5191 ret->name = val;
5192 val = xmlGetProp(node, BAD_CAST "ns");
5193 ret->ns = val;
5194 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5195 (val != NULL) &&
5196 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005197 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005198 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005199 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005200 }
5201 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5202 (val != NULL) &&
5203 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005204 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5205 "Attribute with QName 'xmlns' is not allowed\n",
5206 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005207 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005208 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005209 ret->name = NULL;
5210 ret->ns = NULL;
5211 if (node->children != NULL) {
5212 ret->nameClass =
5213 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5214 (def->type ==
5215 XML_RELAXNG_ATTRIBUTE));
5216 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005217 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005218 ret->name = NULL;
5219 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5220 if (ret->ns == NULL) {
5221 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5222 "nsName has no ns attribute\n", NULL, NULL);
5223 }
5224 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5225 (ret->ns != NULL) &&
5226 (xmlStrEqual
5227 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5228 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5229 "Attribute with namespace '%s' is not allowed\n",
5230 ret->ns, NULL);
5231 }
5232 if (node->children != NULL) {
5233 ret->nameClass =
5234 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5235 (def->type ==
5236 XML_RELAXNG_ATTRIBUTE));
5237 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005238 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005239 xmlNodePtr child;
5240 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005241
Daniel Veillard4c004142003-10-07 11:33:24 +00005242 ret = xmlRelaxNGNewDefine(ctxt, node);
5243 if (ret == NULL)
5244 return (NULL);
5245 ret->parent = def;
5246 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005247
Daniel Veillard4c004142003-10-07 11:33:24 +00005248 if (node->children == NULL) {
5249 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5250 "Element choice is empty\n", NULL, NULL);
5251 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005252
Daniel Veillard4c004142003-10-07 11:33:24 +00005253 child = node->children;
5254 while (child != NULL) {
5255 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5256 if (tmp != NULL) {
5257 if (last == NULL) {
5258 last = ret->nameClass = tmp;
5259 } else {
5260 last->next = tmp;
5261 last = tmp;
5262 }
5263 }
5264 child = child->next;
5265 }
5266 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005267 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005268 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5269 "expecting name, anyName, nsName or choice : got %s\n",
5270 node->name, NULL);
5271 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005272 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005273 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005274 if (def->nameClass == NULL) {
5275 def->nameClass = ret;
5276 } else {
5277 tmp = def->nameClass;
5278 while (tmp->next != NULL) {
5279 tmp = tmp->next;
5280 }
5281 tmp->next = ret;
5282 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005283 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005284 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005285}
5286
5287/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005288 * xmlRelaxNGParseElement:
5289 * @ctxt: a Relax-NG parser context
5290 * @node: the element node
5291 *
5292 * parse the content of a RelaxNG element node.
5293 *
5294 * Returns the definition pointer or NULL in case of error.
5295 */
5296static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005297xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5298{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005299 xmlRelaxNGDefinePtr ret, cur, last;
5300 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005301 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005302
Daniel Veillardfd573f12003-03-16 17:52:32 +00005303 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005304 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005305 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005306 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005307 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005308 child = node->children;
5309 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005310 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5311 "xmlRelaxNGParseElement: element has no children\n",
5312 NULL, NULL);
5313 return (ret);
5314 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005315 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5316 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005317 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005318
Daniel Veillard6eadf632003-01-23 18:29:16 +00005319 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005320 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5321 "xmlRelaxNGParseElement: element has no content\n",
5322 NULL, NULL);
5323 return (ret);
5324 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005325 olddefine = ctxt->define;
5326 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005327 last = NULL;
5328 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005329 cur = xmlRelaxNGParsePattern(ctxt, child);
5330 if (cur != NULL) {
5331 cur->parent = ret;
5332 switch (cur->type) {
5333 case XML_RELAXNG_EMPTY:
5334 case XML_RELAXNG_NOT_ALLOWED:
5335 case XML_RELAXNG_TEXT:
5336 case XML_RELAXNG_ELEMENT:
5337 case XML_RELAXNG_DATATYPE:
5338 case XML_RELAXNG_VALUE:
5339 case XML_RELAXNG_LIST:
5340 case XML_RELAXNG_REF:
5341 case XML_RELAXNG_PARENTREF:
5342 case XML_RELAXNG_EXTERNALREF:
5343 case XML_RELAXNG_DEF:
5344 case XML_RELAXNG_ZEROORMORE:
5345 case XML_RELAXNG_ONEORMORE:
5346 case XML_RELAXNG_OPTIONAL:
5347 case XML_RELAXNG_CHOICE:
5348 case XML_RELAXNG_GROUP:
5349 case XML_RELAXNG_INTERLEAVE:
5350 if (last == NULL) {
5351 ret->content = last = cur;
5352 } else {
5353 if ((last->type == XML_RELAXNG_ELEMENT) &&
5354 (ret->content == last)) {
5355 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5356 if (ret->content != NULL) {
5357 ret->content->type = XML_RELAXNG_GROUP;
5358 ret->content->content = last;
5359 } else {
5360 ret->content = last;
5361 }
5362 }
5363 last->next = cur;
5364 last = cur;
5365 }
5366 break;
5367 case XML_RELAXNG_ATTRIBUTE:
5368 cur->next = ret->attrs;
5369 ret->attrs = cur;
5370 break;
5371 case XML_RELAXNG_START:
5372 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5373 "RNG Internal error, start found in element\n",
5374 NULL, NULL);
5375 break;
5376 case XML_RELAXNG_PARAM:
5377 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5378 "RNG Internal error, param found in element\n",
5379 NULL, NULL);
5380 break;
5381 case XML_RELAXNG_EXCEPT:
5382 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5383 "RNG Internal error, except found in element\n",
5384 NULL, NULL);
5385 break;
5386 case XML_RELAXNG_NOOP:
5387 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5388 "RNG Internal error, noop found in element\n",
5389 NULL, NULL);
5390 break;
5391 }
5392 }
5393 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005394 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005395 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005396 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005397}
5398
5399/**
5400 * xmlRelaxNGParsePatterns:
5401 * @ctxt: a Relax-NG parser context
5402 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005403 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005404 *
5405 * parse the content of a RelaxNG start node.
5406 *
5407 * Returns the definition pointer or NULL in case of error.
5408 */
5409static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005410xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005411 int group)
5412{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005413 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005414
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005415 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005416 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005417 if (IS_RELAXNG(nodes, "element")) {
5418 cur = xmlRelaxNGParseElement(ctxt, nodes);
5419 if (def == NULL) {
5420 def = last = cur;
5421 } else {
5422 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5423 (def == last)) {
5424 def = xmlRelaxNGNewDefine(ctxt, nodes);
5425 def->type = XML_RELAXNG_GROUP;
5426 def->content = last;
5427 }
5428 last->next = cur;
5429 last = cur;
5430 }
5431 cur->parent = parent;
5432 } else {
5433 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5434 if (cur != NULL) {
5435 if (def == NULL) {
5436 def = last = cur;
5437 } else {
5438 last->next = cur;
5439 last = cur;
5440 }
5441 }
5442 }
5443 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005444 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005445 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005446}
5447
5448/**
5449 * xmlRelaxNGParseStart:
5450 * @ctxt: a Relax-NG parser context
5451 * @nodes: start children nodes
5452 *
5453 * parse the content of a RelaxNG start node.
5454 *
5455 * Returns 0 in case of success, -1 in case of error
5456 */
5457static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005458xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5459{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005460 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005461 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005462
Daniel Veillardd2298792003-02-14 16:54:11 +00005463 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005464 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5465 NULL, NULL);
5466 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005467 }
5468 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005469 def = xmlRelaxNGNewDefine(ctxt, nodes);
5470 if (def == NULL)
5471 return (-1);
5472 def->type = XML_RELAXNG_EMPTY;
5473 if (nodes->children != NULL) {
5474 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5475 "element empty is not empty\n", NULL, NULL);
5476 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005477 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005478 def = xmlRelaxNGNewDefine(ctxt, nodes);
5479 if (def == NULL)
5480 return (-1);
5481 def->type = XML_RELAXNG_NOT_ALLOWED;
5482 if (nodes->children != NULL) {
5483 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5484 "element notAllowed is not empty\n", NULL, NULL);
5485 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005486 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005487 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005488 }
5489 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005490 last = ctxt->grammar->start;
5491 while (last->next != NULL)
5492 last = last->next;
5493 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005494 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005495 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005496 }
5497 nodes = nodes->next;
5498 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005499 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5500 "start more than one children\n", NULL, NULL);
5501 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005502 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005503 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005504}
5505
5506/**
5507 * xmlRelaxNGParseGrammarContent:
5508 * @ctxt: a Relax-NG parser context
5509 * @nodes: grammar children nodes
5510 *
5511 * parse the content of a RelaxNG grammar node.
5512 *
5513 * Returns 0 in case of success, -1 in case of error
5514 */
5515static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005516xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5517 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005518{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005519 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005520
5521 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005522 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5523 "grammar has no children\n", NULL, NULL);
5524 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005525 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005526 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005527 if (IS_RELAXNG(nodes, "start")) {
5528 if (nodes->children == NULL) {
5529 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5530 "start has no children\n", NULL, NULL);
5531 } else {
5532 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5533 if (tmp != 0)
5534 ret = -1;
5535 }
5536 } else if (IS_RELAXNG(nodes, "define")) {
5537 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5538 if (tmp != 0)
5539 ret = -1;
5540 } else if (IS_RELAXNG(nodes, "include")) {
5541 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5542 if (tmp != 0)
5543 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005544 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005545 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5546 "grammar has unexpected child %s\n", nodes->name,
5547 NULL);
5548 ret = -1;
5549 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005550 nodes = nodes->next;
5551 }
5552 return (ret);
5553}
5554
5555/**
5556 * xmlRelaxNGCheckReference:
5557 * @ref: the ref
5558 * @ctxt: a Relax-NG parser context
5559 * @name: the name associated to the defines
5560 *
5561 * Applies the 4.17. combine attribute rule for all the define
5562 * element of a given grammar using the same name.
5563 */
5564static void
5565xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005566 xmlRelaxNGParserCtxtPtr ctxt,
5567 const xmlChar * name)
5568{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005569 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005570 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005571
5572 grammar = ctxt->grammar;
5573 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005574 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5575 "Internal error: no grammar in CheckReference %s\n",
5576 name, NULL);
5577 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005578 }
5579 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005580 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5581 "Internal error: reference has content in CheckReference %s\n",
5582 name, NULL);
5583 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005584 }
5585 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005586 def = xmlHashLookup(grammar->defs, name);
5587 if (def != NULL) {
5588 cur = ref;
5589 while (cur != NULL) {
5590 cur->content = def;
5591 cur = cur->nextHash;
5592 }
5593 } else {
5594 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5595 "Reference %s has no matching definition\n", name,
5596 NULL);
5597 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005598 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005599 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5600 "Reference %s has no matching definition\n", name,
5601 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005602 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005603}
5604
5605/**
5606 * xmlRelaxNGCheckCombine:
5607 * @define: the define(s) list
5608 * @ctxt: a Relax-NG parser context
5609 * @name: the name associated to the defines
5610 *
5611 * Applies the 4.17. combine attribute rule for all the define
5612 * element of a given grammar using the same name.
5613 */
5614static void
5615xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005616 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5617{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005618 xmlChar *combine;
5619 int choiceOrInterleave = -1;
5620 int missing = 0;
5621 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5622
5623 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005624 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005625 cur = define;
5626 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005627 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5628 if (combine != NULL) {
5629 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5630 if (choiceOrInterleave == -1)
5631 choiceOrInterleave = 1;
5632 else if (choiceOrInterleave == 0) {
5633 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5634 "Defines for %s use both 'choice' and 'interleave'\n",
5635 name, NULL);
5636 }
5637 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5638 if (choiceOrInterleave == -1)
5639 choiceOrInterleave = 0;
5640 else if (choiceOrInterleave == 1) {
5641 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5642 "Defines for %s use both 'choice' and 'interleave'\n",
5643 name, NULL);
5644 }
5645 } else {
5646 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5647 "Defines for %s use unknown combine value '%s''\n",
5648 name, combine);
5649 }
5650 xmlFree(combine);
5651 } else {
5652 if (missing == 0)
5653 missing = 1;
5654 else {
5655 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5656 "Some defines for %s needs the combine attribute\n",
5657 name, NULL);
5658 }
5659 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005660
Daniel Veillard4c004142003-10-07 11:33:24 +00005661 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005662 }
5663#ifdef DEBUG
5664 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005665 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5666 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005667#endif
5668 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005669 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005670 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005671 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005672 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005673 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005674 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005675 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005676 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005677 tmp = define;
5678 last = NULL;
5679 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005680 if (tmp->content != NULL) {
5681 if (tmp->content->next != NULL) {
5682 /*
5683 * we need first to create a wrapper.
5684 */
5685 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5686 if (tmp2 == NULL)
5687 break;
5688 tmp2->type = XML_RELAXNG_GROUP;
5689 tmp2->content = tmp->content;
5690 } else {
5691 tmp2 = tmp->content;
5692 }
5693 if (last == NULL) {
5694 cur->content = tmp2;
5695 } else {
5696 last->next = tmp2;
5697 }
5698 last = tmp2;
5699 }
5700 tmp->content = cur;
5701 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005702 }
5703 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005704 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005705 if (ctxt->interleaves == NULL)
5706 ctxt->interleaves = xmlHashCreate(10);
5707 if (ctxt->interleaves == NULL) {
5708 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5709 "Failed to create interleaves hash table\n", NULL,
5710 NULL);
5711 } else {
5712 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005713
Daniel Veillard4c004142003-10-07 11:33:24 +00005714 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5715 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5716 0) {
5717 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5718 "Failed to add %s to hash table\n",
5719 (const xmlChar *) tmpname, NULL);
5720 }
5721 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005722 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005723}
5724
5725/**
5726 * xmlRelaxNGCombineStart:
5727 * @ctxt: a Relax-NG parser context
5728 * @grammar: the grammar
5729 *
5730 * Applies the 4.17. combine rule for all the start
5731 * element of a given grammar.
5732 */
5733static void
5734xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005735 xmlRelaxNGGrammarPtr grammar)
5736{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005737 xmlRelaxNGDefinePtr starts;
5738 xmlChar *combine;
5739 int choiceOrInterleave = -1;
5740 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005741 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005742
Daniel Veillard2df2de22003-02-17 23:34:33 +00005743 starts = grammar->start;
5744 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005745 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005746 cur = starts;
5747 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005748 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5749 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5750 combine = NULL;
5751 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5752 "Internal error: start element not found\n", NULL,
5753 NULL);
5754 } else {
5755 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5756 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005757
Daniel Veillard4c004142003-10-07 11:33:24 +00005758 if (combine != NULL) {
5759 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5760 if (choiceOrInterleave == -1)
5761 choiceOrInterleave = 1;
5762 else if (choiceOrInterleave == 0) {
5763 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5764 "<start> use both 'choice' and 'interleave'\n",
5765 NULL, NULL);
5766 }
5767 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5768 if (choiceOrInterleave == -1)
5769 choiceOrInterleave = 0;
5770 else if (choiceOrInterleave == 1) {
5771 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5772 "<start> use both 'choice' and 'interleave'\n",
5773 NULL, NULL);
5774 }
5775 } else {
5776 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5777 "<start> uses unknown combine value '%s''\n",
5778 combine, NULL);
5779 }
5780 xmlFree(combine);
5781 } else {
5782 if (missing == 0)
5783 missing = 1;
5784 else {
5785 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5786 "Some <start> element miss the combine attribute\n",
5787 NULL, NULL);
5788 }
5789 }
5790
5791 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005792 }
5793#ifdef DEBUG
5794 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005795 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5796 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005797#endif
5798 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005799 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005800 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005801 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005802 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005803 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005804 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005805 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005806 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005807 cur->content = grammar->start;
5808 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005809 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005810 if (ctxt->interleaves == NULL)
5811 ctxt->interleaves = xmlHashCreate(10);
5812 if (ctxt->interleaves == NULL) {
5813 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5814 "Failed to create interleaves hash table\n", NULL,
5815 NULL);
5816 } else {
5817 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005818
Daniel Veillard4c004142003-10-07 11:33:24 +00005819 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5820 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5821 0) {
5822 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5823 "Failed to add %s to hash table\n",
5824 (const xmlChar *) tmpname, NULL);
5825 }
5826 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005827 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005828}
5829
5830/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005831 * xmlRelaxNGCheckCycles:
5832 * @ctxt: a Relax-NG parser context
5833 * @nodes: grammar children nodes
5834 * @depth: the counter
5835 *
5836 * Check for cycles.
5837 *
5838 * Returns 0 if check passed, and -1 in case of error
5839 */
5840static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005841xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5842 xmlRelaxNGDefinePtr cur, int depth)
5843{
Daniel Veillardd4310742003-02-18 21:12:46 +00005844 int ret = 0;
5845
5846 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005847 if ((cur->type == XML_RELAXNG_REF) ||
5848 (cur->type == XML_RELAXNG_PARENTREF)) {
5849 if (cur->depth == -1) {
5850 cur->depth = depth;
5851 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5852 cur->depth = -2;
5853 } else if (depth == cur->depth) {
5854 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5855 "Detected a cycle in %s references\n",
5856 cur->name, NULL);
5857 return (-1);
5858 }
5859 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5860 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5861 } else {
5862 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5863 }
5864 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005865 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005866 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005867}
5868
5869/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005870 * xmlRelaxNGTryUnlink:
5871 * @ctxt: a Relax-NG parser context
5872 * @cur: the definition to unlink
5873 * @parent: the parent definition
5874 * @prev: the previous sibling definition
5875 *
5876 * Try to unlink a definition. If not possble make it a NOOP
5877 *
5878 * Returns the new prev definition
5879 */
5880static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005881xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5882 xmlRelaxNGDefinePtr cur,
5883 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5884{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005885 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005886 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005887 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005888 if (parent != NULL) {
5889 if (parent->content == cur)
5890 parent->content = cur->next;
5891 else if (parent->attrs == cur)
5892 parent->attrs = cur->next;
5893 else if (parent->nameClass == cur)
5894 parent->nameClass = cur->next;
5895 } else {
5896 cur->type = XML_RELAXNG_NOOP;
5897 prev = cur;
5898 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005899 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005900 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005901}
5902
5903/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005904 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005905 * @ctxt: a Relax-NG parser context
5906 * @nodes: grammar children nodes
5907 *
5908 * Check for simplification of empty and notAllowed
5909 */
5910static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005911xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5912 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5913{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005914 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005915
Daniel Veillardfd573f12003-03-16 17:52:32 +00005916 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005917 if ((cur->type == XML_RELAXNG_REF) ||
5918 (cur->type == XML_RELAXNG_PARENTREF)) {
5919 if (cur->depth != -3) {
5920 cur->depth = -3;
5921 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5922 }
5923 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5924 cur->parent = parent;
5925 if ((parent != NULL) &&
5926 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5927 (parent->type == XML_RELAXNG_LIST) ||
5928 (parent->type == XML_RELAXNG_GROUP) ||
5929 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5930 (parent->type == XML_RELAXNG_ONEORMORE) ||
5931 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5932 parent->type = XML_RELAXNG_NOT_ALLOWED;
5933 break;
5934 }
5935 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5936 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5937 } else
5938 prev = cur;
5939 } else if (cur->type == XML_RELAXNG_EMPTY) {
5940 cur->parent = parent;
5941 if ((parent != NULL) &&
5942 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5943 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5944 parent->type = XML_RELAXNG_EMPTY;
5945 break;
5946 }
5947 if ((parent != NULL) &&
5948 ((parent->type == XML_RELAXNG_GROUP) ||
5949 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5950 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5951 } else
5952 prev = cur;
5953 } else {
5954 cur->parent = parent;
5955 if (cur->content != NULL)
5956 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5957 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5958 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5959 if (cur->nameClass != NULL)
5960 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5961 /*
5962 * On Elements, try to move attribute only generating rules on
5963 * the attrs rules.
5964 */
5965 if (cur->type == XML_RELAXNG_ELEMENT) {
5966 int attronly;
5967 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00005968
Daniel Veillard4c004142003-10-07 11:33:24 +00005969 while (cur->content != NULL) {
5970 attronly =
5971 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5972 if (attronly == 1) {
5973 /*
5974 * migrate cur->content to attrs
5975 */
5976 tmp = cur->content;
5977 cur->content = tmp->next;
5978 tmp->next = cur->attrs;
5979 cur->attrs = tmp;
5980 } else {
5981 /*
5982 * cur->content can generate elements or text
5983 */
5984 break;
5985 }
5986 }
5987 pre = cur->content;
5988 while ((pre != NULL) && (pre->next != NULL)) {
5989 tmp = pre->next;
5990 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5991 if (attronly == 1) {
5992 /*
5993 * migrate tmp to attrs
5994 */
5995 pre->next = tmp->next;
5996 tmp->next = cur->attrs;
5997 cur->attrs = tmp;
5998 } else {
5999 pre = tmp;
6000 }
6001 }
6002 }
6003 /*
6004 * This may result in a simplification
6005 */
6006 if ((cur->type == XML_RELAXNG_GROUP) ||
6007 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6008 if (cur->content == NULL)
6009 cur->type = XML_RELAXNG_EMPTY;
6010 else if (cur->content->next == NULL) {
6011 if ((parent == NULL) && (prev == NULL)) {
6012 cur->type = XML_RELAXNG_NOOP;
6013 } else if (prev == NULL) {
6014 parent->content = cur->content;
6015 cur->content->next = cur->next;
6016 cur = cur->content;
6017 } else {
6018 cur->content->next = cur->next;
6019 prev->next = cur->content;
6020 cur = cur->content;
6021 }
6022 }
6023 }
6024 /*
6025 * the current node may have been transformed back
6026 */
6027 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6028 (cur->content != NULL) &&
6029 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6030 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6031 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6032 if ((parent != NULL) &&
6033 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6034 (parent->type == XML_RELAXNG_LIST) ||
6035 (parent->type == XML_RELAXNG_GROUP) ||
6036 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6037 (parent->type == XML_RELAXNG_ONEORMORE) ||
6038 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6039 parent->type = XML_RELAXNG_NOT_ALLOWED;
6040 break;
6041 }
6042 if ((parent != NULL) &&
6043 (parent->type == XML_RELAXNG_CHOICE)) {
6044 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6045 } else
6046 prev = cur;
6047 } else if (cur->type == XML_RELAXNG_EMPTY) {
6048 if ((parent != NULL) &&
6049 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6050 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6051 parent->type = XML_RELAXNG_EMPTY;
6052 break;
6053 }
6054 if ((parent != NULL) &&
6055 ((parent->type == XML_RELAXNG_GROUP) ||
6056 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6057 (parent->type == XML_RELAXNG_CHOICE))) {
6058 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6059 } else
6060 prev = cur;
6061 } else {
6062 prev = cur;
6063 }
6064 }
6065 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006066 }
6067}
6068
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006069/**
6070 * xmlRelaxNGGroupContentType:
6071 * @ct1: the first content type
6072 * @ct2: the second content type
6073 *
6074 * Try to group 2 content types
6075 *
6076 * Returns the content type
6077 */
6078static xmlRelaxNGContentType
6079xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006080 xmlRelaxNGContentType ct2)
6081{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006082 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006083 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6084 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006085 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006086 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006087 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006088 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006089 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006090 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6091 return (XML_RELAXNG_CONTENT_COMPLEX);
6092 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006093}
6094
6095/**
6096 * xmlRelaxNGMaxContentType:
6097 * @ct1: the first content type
6098 * @ct2: the second content type
6099 *
6100 * Compute the max content-type
6101 *
6102 * Returns the content type
6103 */
6104static xmlRelaxNGContentType
6105xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006106 xmlRelaxNGContentType ct2)
6107{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006108 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006109 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6110 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006111 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006112 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6113 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006114 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006115 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6116 return (XML_RELAXNG_CONTENT_COMPLEX);
6117 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006118}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006119
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006120/**
6121 * xmlRelaxNGCheckRules:
6122 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006123 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006124 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006125 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006126 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006127 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006128 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006129 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006130 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006131static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006132xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6133 xmlRelaxNGDefinePtr cur, int flags,
6134 xmlRelaxNGType ptype)
6135{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006136 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006137 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006138
Daniel Veillardfd573f12003-03-16 17:52:32 +00006139 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006140 ret = XML_RELAXNG_CONTENT_EMPTY;
6141 if ((cur->type == XML_RELAXNG_REF) ||
6142 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006143 /*
6144 * This should actually be caught by list//element(ref) at the
6145 * element boundaries, c.f. Bug #159968 local refs are dropped
6146 * in step 4.19.
6147 */
6148#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006149 if (flags & XML_RELAXNG_IN_LIST) {
6150 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6151 "Found forbidden pattern list//ref\n", NULL,
6152 NULL);
6153 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006154#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006155 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6156 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6157 "Found forbidden pattern data/except//ref\n",
6158 NULL, NULL);
6159 }
6160 if (cur->depth > -4) {
6161 cur->depth = -4;
6162 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6163 flags, cur->type);
6164 cur->depth = ret - 15;
6165 } else if (cur->depth == -4) {
6166 ret = XML_RELAXNG_CONTENT_COMPLEX;
6167 } else {
6168 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6169 }
6170 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6171 /*
6172 * The 7.3 Attribute derivation rule for groups is plugged there
6173 */
6174 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6175 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6176 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6177 "Found forbidden pattern data/except//element(ref)\n",
6178 NULL, NULL);
6179 }
6180 if (flags & XML_RELAXNG_IN_LIST) {
6181 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6182 "Found forbidden pattern list//element(ref)\n",
6183 NULL, NULL);
6184 }
6185 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6186 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6187 "Found forbidden pattern attribute//element(ref)\n",
6188 NULL, NULL);
6189 }
6190 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6191 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6192 "Found forbidden pattern attribute//element(ref)\n",
6193 NULL, NULL);
6194 }
6195 /*
6196 * reset since in the simple form elements are only child
6197 * of grammar/define
6198 */
6199 nflags = 0;
6200 ret =
6201 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6202 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6203 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6204 "Element %s attributes have a content type error\n",
6205 cur->name, NULL);
6206 }
6207 ret =
6208 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6209 cur->type);
6210 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6211 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6212 "Element %s has a content type error\n",
6213 cur->name, NULL);
6214 } else {
6215 ret = XML_RELAXNG_CONTENT_COMPLEX;
6216 }
6217 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6218 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6219 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6220 "Found forbidden pattern attribute//attribute\n",
6221 NULL, NULL);
6222 }
6223 if (flags & XML_RELAXNG_IN_LIST) {
6224 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6225 "Found forbidden pattern list//attribute\n",
6226 NULL, NULL);
6227 }
6228 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6229 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6230 "Found forbidden pattern oneOrMore//group//attribute\n",
6231 NULL, NULL);
6232 }
6233 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6234 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6235 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6236 NULL, NULL);
6237 }
6238 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6239 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6240 "Found forbidden pattern data/except//attribute\n",
6241 NULL, NULL);
6242 }
6243 if (flags & XML_RELAXNG_IN_START) {
6244 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6245 "Found forbidden pattern start//attribute\n",
6246 NULL, NULL);
6247 }
6248 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6249 && (cur->name == NULL)) {
6250 if (cur->ns == NULL) {
6251 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6252 "Found anyName attribute without oneOrMore ancestor\n",
6253 NULL, NULL);
6254 } else {
6255 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6256 "Found nsName attribute without oneOrMore ancestor\n",
6257 NULL, NULL);
6258 }
6259 }
6260 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6261 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6262 ret = XML_RELAXNG_CONTENT_EMPTY;
6263 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6264 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6265 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6266 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6267 "Found forbidden pattern data/except//oneOrMore\n",
6268 NULL, NULL);
6269 }
6270 if (flags & XML_RELAXNG_IN_START) {
6271 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6272 "Found forbidden pattern start//oneOrMore\n",
6273 NULL, NULL);
6274 }
6275 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6276 ret =
6277 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6278 cur->type);
6279 ret = xmlRelaxNGGroupContentType(ret, ret);
6280 } else if (cur->type == XML_RELAXNG_LIST) {
6281 if (flags & XML_RELAXNG_IN_LIST) {
6282 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6283 "Found forbidden pattern list//list\n", NULL,
6284 NULL);
6285 }
6286 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6287 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6288 "Found forbidden pattern data/except//list\n",
6289 NULL, NULL);
6290 }
6291 if (flags & XML_RELAXNG_IN_START) {
6292 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6293 "Found forbidden pattern start//list\n", NULL,
6294 NULL);
6295 }
6296 nflags = flags | XML_RELAXNG_IN_LIST;
6297 ret =
6298 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6299 cur->type);
6300 } else if (cur->type == XML_RELAXNG_GROUP) {
6301 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6302 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6303 "Found forbidden pattern data/except//group\n",
6304 NULL, NULL);
6305 }
6306 if (flags & XML_RELAXNG_IN_START) {
6307 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6308 "Found forbidden pattern start//group\n", NULL,
6309 NULL);
6310 }
6311 if (flags & XML_RELAXNG_IN_ONEORMORE)
6312 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6313 else
6314 nflags = flags;
6315 ret =
6316 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6317 cur->type);
6318 /*
6319 * The 7.3 Attribute derivation rule for groups is plugged there
6320 */
6321 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6322 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6323 if (flags & XML_RELAXNG_IN_LIST) {
6324 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6325 "Found forbidden pattern list//interleave\n",
6326 NULL, NULL);
6327 }
6328 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6329 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6330 "Found forbidden pattern data/except//interleave\n",
6331 NULL, NULL);
6332 }
6333 if (flags & XML_RELAXNG_IN_START) {
6334 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6335 "Found forbidden pattern start//interleave\n",
6336 NULL, NULL);
6337 }
6338 if (flags & XML_RELAXNG_IN_ONEORMORE)
6339 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6340 else
6341 nflags = flags;
6342 ret =
6343 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6344 cur->type);
6345 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6346 if ((cur->parent != NULL) &&
6347 (cur->parent->type == XML_RELAXNG_DATATYPE))
6348 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6349 else
6350 nflags = flags;
6351 ret =
6352 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6353 cur->type);
6354 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6355 if (flags & XML_RELAXNG_IN_START) {
6356 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6357 "Found forbidden pattern start//data\n", NULL,
6358 NULL);
6359 }
6360 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6361 ret = XML_RELAXNG_CONTENT_SIMPLE;
6362 } else if (cur->type == XML_RELAXNG_VALUE) {
6363 if (flags & XML_RELAXNG_IN_START) {
6364 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6365 "Found forbidden pattern start//value\n", NULL,
6366 NULL);
6367 }
6368 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6369 ret = XML_RELAXNG_CONTENT_SIMPLE;
6370 } else if (cur->type == XML_RELAXNG_TEXT) {
6371 if (flags & XML_RELAXNG_IN_LIST) {
6372 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6373 "Found forbidden pattern list//text\n", NULL,
6374 NULL);
6375 }
6376 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6377 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6378 "Found forbidden pattern data/except//text\n",
6379 NULL, NULL);
6380 }
6381 if (flags & XML_RELAXNG_IN_START) {
6382 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6383 "Found forbidden pattern start//text\n", NULL,
6384 NULL);
6385 }
6386 ret = XML_RELAXNG_CONTENT_COMPLEX;
6387 } else if (cur->type == XML_RELAXNG_EMPTY) {
6388 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6389 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6390 "Found forbidden pattern data/except//empty\n",
6391 NULL, NULL);
6392 }
6393 if (flags & XML_RELAXNG_IN_START) {
6394 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6395 "Found forbidden pattern start//empty\n", NULL,
6396 NULL);
6397 }
6398 ret = XML_RELAXNG_CONTENT_EMPTY;
6399 } else if (cur->type == XML_RELAXNG_CHOICE) {
6400 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6401 ret =
6402 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6403 } else {
6404 ret =
6405 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6406 }
6407 cur = cur->next;
6408 if (ptype == XML_RELAXNG_GROUP) {
6409 val = xmlRelaxNGGroupContentType(val, ret);
6410 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6411 tmp = xmlRelaxNGGroupContentType(val, ret);
6412 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6413 tmp = xmlRelaxNGMaxContentType(val, ret);
6414 } else if (ptype == XML_RELAXNG_CHOICE) {
6415 val = xmlRelaxNGMaxContentType(val, ret);
6416 } else if (ptype == XML_RELAXNG_LIST) {
6417 val = XML_RELAXNG_CONTENT_SIMPLE;
6418 } else if (ptype == XML_RELAXNG_EXCEPT) {
6419 if (ret == XML_RELAXNG_CONTENT_ERROR)
6420 val = XML_RELAXNG_CONTENT_ERROR;
6421 else
6422 val = XML_RELAXNG_CONTENT_SIMPLE;
6423 } else {
6424 val = xmlRelaxNGGroupContentType(val, ret);
6425 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006426
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006427 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006428 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006429}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006430
6431/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006432 * xmlRelaxNGParseGrammar:
6433 * @ctxt: a Relax-NG parser context
6434 * @nodes: grammar children nodes
6435 *
6436 * parse a Relax-NG <grammar> node
6437 *
6438 * Returns the internal xmlRelaxNGGrammarPtr built or
6439 * NULL in case of error
6440 */
6441static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006442xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6443{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006444 xmlRelaxNGGrammarPtr ret, tmp, old;
6445
Daniel Veillardc482e262003-02-26 14:48:48 +00006446#ifdef DEBUG_GRAMMAR
6447 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6448#endif
6449
Daniel Veillard6eadf632003-01-23 18:29:16 +00006450 ret = xmlRelaxNGNewGrammar(ctxt);
6451 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006452 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006453
6454 /*
6455 * Link the new grammar in the tree
6456 */
6457 ret->parent = ctxt->grammar;
6458 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006459 tmp = ctxt->grammar->children;
6460 if (tmp == NULL) {
6461 ctxt->grammar->children = ret;
6462 } else {
6463 while (tmp->next != NULL)
6464 tmp = tmp->next;
6465 tmp->next = ret;
6466 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006467 }
6468
6469 old = ctxt->grammar;
6470 ctxt->grammar = ret;
6471 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6472 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006473 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006474 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6475 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006476 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006477 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6478 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006479 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006480
6481 /*
6482 * Apply 4.17 mergingd rules to defines and starts
6483 */
6484 xmlRelaxNGCombineStart(ctxt, ret);
6485 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006486 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6487 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006488 }
6489
6490 /*
6491 * link together defines and refs in this grammar
6492 */
6493 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006494 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6495 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006496 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006497
Daniel Veillard6eadf632003-01-23 18:29:16 +00006498 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006499 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006500}
6501
6502/**
6503 * xmlRelaxNGParseDocument:
6504 * @ctxt: a Relax-NG parser context
6505 * @node: the root node of the RelaxNG schema
6506 *
6507 * parse a Relax-NG definition resource and build an internal
6508 * xmlRelaxNG struture which can be used to validate instances.
6509 *
6510 * Returns the internal XML RelaxNG structure built or
6511 * NULL in case of error
6512 */
6513static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006514xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6515{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006516 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006517 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006518 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006519
6520 if ((ctxt == NULL) || (node == NULL))
6521 return (NULL);
6522
6523 schema = xmlRelaxNGNewRelaxNG(ctxt);
6524 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006525 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006526
Daniel Veillard276be4a2003-01-24 01:03:34 +00006527 olddefine = ctxt->define;
6528 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006529 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006530 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006531 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006532 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006533
Daniel Veillard4c004142003-10-07 11:33:24 +00006534 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6535 if (schema->topgrammar == NULL) {
6536 return (schema);
6537 }
6538 /*
6539 * Link the new grammar in the tree
6540 */
6541 ret->parent = ctxt->grammar;
6542 if (ctxt->grammar != NULL) {
6543 tmp = ctxt->grammar->children;
6544 if (tmp == NULL) {
6545 ctxt->grammar->children = ret;
6546 } else {
6547 while (tmp->next != NULL)
6548 tmp = tmp->next;
6549 tmp->next = ret;
6550 }
6551 }
6552 old = ctxt->grammar;
6553 ctxt->grammar = ret;
6554 xmlRelaxNGParseStart(ctxt, node);
6555 if (old != NULL)
6556 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006557 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006558 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006559 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006560 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6561 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6562 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6563 while ((schema->topgrammar->start != NULL) &&
6564 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6565 (schema->topgrammar->start->next != NULL))
6566 schema->topgrammar->start =
6567 schema->topgrammar->start->content;
6568 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6569 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6570 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006571 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006572#ifdef DEBUG
6573 if (schema == NULL)
6574 xmlGenericError(xmlGenericErrorContext,
6575 "xmlRelaxNGParseDocument() failed\n");
6576#endif
6577
6578 return (schema);
6579}
6580
6581/************************************************************************
6582 * *
6583 * Reading RelaxNGs *
6584 * *
6585 ************************************************************************/
6586
6587/**
6588 * xmlRelaxNGNewParserCtxt:
6589 * @URL: the location of the schema
6590 *
6591 * Create an XML RelaxNGs parse context for that file/resource expected
6592 * to contain an XML RelaxNGs file.
6593 *
6594 * Returns the parser context or NULL in case of error
6595 */
6596xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006597xmlRelaxNGNewParserCtxt(const char *URL)
6598{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006599 xmlRelaxNGParserCtxtPtr ret;
6600
6601 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006602 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006603
Daniel Veillard4c004142003-10-07 11:33:24 +00006604 ret =
6605 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006606 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006607 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006608 return (NULL);
6609 }
6610 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006611 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006612 ret->error = xmlGenericError;
6613 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006614 return (ret);
6615}
6616
6617/**
6618 * xmlRelaxNGNewMemParserCtxt:
6619 * @buffer: a pointer to a char array containing the schemas
6620 * @size: the size of the array
6621 *
6622 * Create an XML RelaxNGs parse context for that memory buffer expected
6623 * to contain an XML RelaxNGs file.
6624 *
6625 * Returns the parser context or NULL in case of error
6626 */
6627xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006628xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6629{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006630 xmlRelaxNGParserCtxtPtr ret;
6631
6632 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006633 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006634
Daniel Veillard4c004142003-10-07 11:33:24 +00006635 ret =
6636 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006637 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006638 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006639 return (NULL);
6640 }
6641 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6642 ret->buffer = buffer;
6643 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006644 ret->error = xmlGenericError;
6645 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006646 return (ret);
6647}
6648
6649/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006650 * xmlRelaxNGNewDocParserCtxt:
6651 * @doc: a preparsed document tree
6652 *
6653 * Create an XML RelaxNGs parser context for that document.
6654 * Note: since the process of compiling a RelaxNG schemas modifies the
6655 * document, the @doc parameter is duplicated internally.
6656 *
6657 * Returns the parser context or NULL in case of error
6658 */
6659xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006660xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6661{
Daniel Veillard33300b42003-04-17 09:09:19 +00006662 xmlRelaxNGParserCtxtPtr ret;
6663 xmlDocPtr copy;
6664
6665 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006666 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006667 copy = xmlCopyDoc(doc, 1);
6668 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006669 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006670
Daniel Veillard4c004142003-10-07 11:33:24 +00006671 ret =
6672 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006673 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006674 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006675 return (NULL);
6676 }
6677 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6678 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006679 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006680 ret->userData = xmlGenericErrorContext;
6681 return (ret);
6682}
6683
6684/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006685 * xmlRelaxNGFreeParserCtxt:
6686 * @ctxt: the schema parser context
6687 *
6688 * Free the resources associated to the schema parser context
6689 */
6690void
Daniel Veillard4c004142003-10-07 11:33:24 +00006691xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6692{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006693 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006694 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006695 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006696 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006697 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006698 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006699 if (ctxt->interleaves != NULL)
6700 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006701 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006702 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006703 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006704 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006705 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006706 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006707 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006708 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006709 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006710 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006711
Daniel Veillard4c004142003-10-07 11:33:24 +00006712 for (i = 0; i < ctxt->defNr; i++)
6713 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6714 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006715 }
Daniel Veillard42595322004-11-08 10:52:06 +00006716 if ((ctxt->document != NULL) && (ctxt->freedoc))
6717 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006718 xmlFree(ctxt);
6719}
6720
Daniel Veillard6eadf632003-01-23 18:29:16 +00006721/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006722 * xmlRelaxNGNormExtSpace:
6723 * @value: a value
6724 *
6725 * Removes the leading and ending spaces of the value
6726 * The string is modified "in situ"
6727 */
6728static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006729xmlRelaxNGNormExtSpace(xmlChar * value)
6730{
Daniel Veillardd2298792003-02-14 16:54:11 +00006731 xmlChar *start = value;
6732 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006733
Daniel Veillard4c004142003-10-07 11:33:24 +00006734 if (value == NULL)
6735 return;
6736
William M. Brack76e95df2003-10-18 16:20:14 +00006737 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006738 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006739 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006740 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006741 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006742 cur++;
6743 if (*cur == 0)
6744 return;
6745 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006746 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006747 cur++;
6748 if (*cur == 0) {
6749 *start = 0;
6750 return;
6751 }
6752 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006753 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006754 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006755 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006756 *start++ = *cur++;
6757 if (*cur == 0) {
6758 *start = 0;
6759 return;
6760 }
6761 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006762 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006763 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006764 if (*cur == 0) {
6765 *start = 0;
6766 return;
6767 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006768 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006769 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006770 }
6771}
6772
6773/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006774 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006775 * @ctxt: a Relax-NG parser context
6776 * @node: a Relax-NG node
6777 *
6778 * Check all the attributes on the given node
6779 */
6780static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006781xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6782{
Daniel Veillardd2298792003-02-14 16:54:11 +00006783 xmlAttrPtr cur, next;
6784
6785 cur = node->properties;
6786 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006787 next = cur->next;
6788 if ((cur->ns == NULL) ||
6789 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6790 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6791 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6792 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6793 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6794 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6795 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6796 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6797 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6798 "Attribute %s is not allowed on %s\n",
6799 cur->name, node->name);
6800 }
6801 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6802 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6803 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6804 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6805 "Attribute %s is not allowed on %s\n",
6806 cur->name, node->name);
6807 }
6808 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6809 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6810 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6811 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6812 "Attribute %s is not allowed on %s\n",
6813 cur->name, node->name);
6814 }
6815 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6816 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6817 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6818 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6819 "Attribute %s is not allowed on %s\n",
6820 cur->name, node->name);
6821 }
6822 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6823 xmlChar *val;
6824 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006825
Daniel Veillard4c004142003-10-07 11:33:24 +00006826 val = xmlNodeListGetString(node->doc, cur->children, 1);
6827 if (val != NULL) {
6828 if (val[0] != 0) {
6829 uri = xmlParseURI((const char *) val);
6830 if (uri == NULL) {
6831 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6832 "Attribute %s contains invalid URI %s\n",
6833 cur->name, val);
6834 } else {
6835 if (uri->scheme == NULL) {
6836 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6837 "Attribute %s URI %s is not absolute\n",
6838 cur->name, val);
6839 }
6840 if (uri->fragment != NULL) {
6841 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6842 "Attribute %s URI %s has a fragment ID\n",
6843 cur->name, val);
6844 }
6845 xmlFreeURI(uri);
6846 }
6847 }
6848 xmlFree(val);
6849 }
6850 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6851 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6852 "Unknown attribute %s on %s\n", cur->name,
6853 node->name);
6854 }
6855 }
6856 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006857 }
6858}
6859
6860/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006861 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006862 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006863 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006864 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006865 * Cleanup the subtree from unwanted nodes for parsing, resolve
6866 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006867 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006868static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006869xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6870{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006871 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006872
Daniel Veillard6eadf632003-01-23 18:29:16 +00006873 delete = NULL;
6874 cur = root;
6875 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006876 if (delete != NULL) {
6877 xmlUnlinkNode(delete);
6878 xmlFreeNode(delete);
6879 delete = NULL;
6880 }
6881 if (cur->type == XML_ELEMENT_NODE) {
6882 /*
6883 * Simplification 4.1. Annotations
6884 */
6885 if ((cur->ns == NULL) ||
6886 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6887 if ((cur->parent != NULL) &&
6888 (cur->parent->type == XML_ELEMENT_NODE) &&
6889 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6890 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6891 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6892 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6893 "element %s doesn't allow foreign elements\n",
6894 cur->parent->name, NULL);
6895 }
6896 delete = cur;
6897 goto skip_children;
6898 } else {
6899 xmlRelaxNGCleanupAttributes(ctxt, cur);
6900 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6901 xmlChar *href, *ns, *base, *URL;
6902 xmlRelaxNGDocumentPtr docu;
6903 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00006904 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006905
Daniel Veillard4c004142003-10-07 11:33:24 +00006906 ns = xmlGetProp(cur, BAD_CAST "ns");
6907 if (ns == NULL) {
6908 tmp = cur->parent;
6909 while ((tmp != NULL) &&
6910 (tmp->type == XML_ELEMENT_NODE)) {
6911 ns = xmlGetProp(tmp, BAD_CAST "ns");
6912 if (ns != NULL)
6913 break;
6914 tmp = tmp->parent;
6915 }
6916 }
6917 href = xmlGetProp(cur, BAD_CAST "href");
6918 if (href == NULL) {
6919 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6920 "xmlRelaxNGParse: externalRef has no href attribute\n",
6921 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00006922 if (ns != NULL)
6923 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00006924 delete = cur;
6925 goto skip_children;
6926 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00006927 uri = xmlParseURI((const char *) href);
6928 if (uri == NULL) {
6929 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6930 "Incorrect URI for externalRef %s\n",
6931 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00006932 if (ns != NULL)
6933 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00006934 if (href != NULL)
6935 xmlFree(href);
6936 delete = cur;
6937 goto skip_children;
6938 }
6939 if (uri->fragment != NULL) {
6940 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6941 "Fragment forbidden in URI for externalRef %s\n",
6942 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00006943 if (ns != NULL)
6944 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00006945 xmlFreeURI(uri);
6946 if (href != NULL)
6947 xmlFree(href);
6948 delete = cur;
6949 goto skip_children;
6950 }
6951 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00006952 base = xmlNodeGetBase(cur->doc, cur);
6953 URL = xmlBuildURI(href, base);
6954 if (URL == NULL) {
6955 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6956 "Failed to compute URL for externalRef %s\n",
6957 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00006958 if (ns != NULL)
6959 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00006960 if (href != NULL)
6961 xmlFree(href);
6962 if (base != NULL)
6963 xmlFree(base);
6964 delete = cur;
6965 goto skip_children;
6966 }
6967 if (href != NULL)
6968 xmlFree(href);
6969 if (base != NULL)
6970 xmlFree(base);
6971 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6972 if (docu == NULL) {
6973 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6974 "Failed to load externalRef %s\n", URL,
6975 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00006976 if (ns != NULL)
6977 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00006978 xmlFree(URL);
6979 delete = cur;
6980 goto skip_children;
6981 }
6982 if (ns != NULL)
6983 xmlFree(ns);
6984 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00006985 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00006986 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6987 xmlChar *href, *ns, *base, *URL;
6988 xmlRelaxNGIncludePtr incl;
6989 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006990
Daniel Veillard4c004142003-10-07 11:33:24 +00006991 href = xmlGetProp(cur, BAD_CAST "href");
6992 if (href == NULL) {
6993 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6994 "xmlRelaxNGParse: include has no href attribute\n",
6995 NULL, NULL);
6996 delete = cur;
6997 goto skip_children;
6998 }
6999 base = xmlNodeGetBase(cur->doc, cur);
7000 URL = xmlBuildURI(href, base);
7001 if (URL == NULL) {
7002 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7003 "Failed to compute URL for include %s\n",
7004 href, NULL);
7005 if (href != NULL)
7006 xmlFree(href);
7007 if (base != NULL)
7008 xmlFree(base);
7009 delete = cur;
7010 goto skip_children;
7011 }
7012 if (href != NULL)
7013 xmlFree(href);
7014 if (base != NULL)
7015 xmlFree(base);
7016 ns = xmlGetProp(cur, BAD_CAST "ns");
7017 if (ns == NULL) {
7018 tmp = cur->parent;
7019 while ((tmp != NULL) &&
7020 (tmp->type == XML_ELEMENT_NODE)) {
7021 ns = xmlGetProp(tmp, BAD_CAST "ns");
7022 if (ns != NULL)
7023 break;
7024 tmp = tmp->parent;
7025 }
7026 }
7027 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7028 if (ns != NULL)
7029 xmlFree(ns);
7030 if (incl == NULL) {
7031 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7032 "Failed to load include %s\n", URL,
7033 NULL);
7034 xmlFree(URL);
7035 delete = cur;
7036 goto skip_children;
7037 }
7038 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007039 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007040 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7041 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7042 {
7043 xmlChar *name, *ns;
7044 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007045
Daniel Veillard4c004142003-10-07 11:33:24 +00007046 /*
7047 * Simplification 4.8. name attribute of element
7048 * and attribute elements
7049 */
7050 name = xmlGetProp(cur, BAD_CAST "name");
7051 if (name != NULL) {
7052 if (cur->children == NULL) {
7053 text =
7054 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7055 name);
7056 } else {
7057 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007058
Daniel Veillard03a53c32004-10-26 16:06:51 +00007059 node = xmlNewDocNode(cur->doc, cur->ns,
7060 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007061 if (node != NULL) {
7062 xmlAddPrevSibling(cur->children, node);
7063 text = xmlNewText(name);
7064 xmlAddChild(node, text);
7065 text = node;
7066 }
7067 }
7068 if (text == NULL) {
7069 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7070 "Failed to create a name %s element\n",
7071 name, NULL);
7072 }
7073 xmlUnsetProp(cur, BAD_CAST "name");
7074 xmlFree(name);
7075 ns = xmlGetProp(cur, BAD_CAST "ns");
7076 if (ns != NULL) {
7077 if (text != NULL) {
7078 xmlSetProp(text, BAD_CAST "ns", ns);
7079 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7080 }
7081 xmlFree(ns);
7082 } else if (xmlStrEqual(cur->name,
7083 BAD_CAST "attribute")) {
7084 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7085 }
7086 }
7087 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7088 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7089 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7090 /*
7091 * Simplification 4.8. name attribute of element
7092 * and attribute elements
7093 */
7094 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7095 xmlNodePtr node;
7096 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007097
Daniel Veillard4c004142003-10-07 11:33:24 +00007098 node = cur->parent;
7099 while ((node != NULL) &&
7100 (node->type == XML_ELEMENT_NODE)) {
7101 ns = xmlGetProp(node, BAD_CAST "ns");
7102 if (ns != NULL) {
7103 break;
7104 }
7105 node = node->parent;
7106 }
7107 if (ns == NULL) {
7108 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7109 } else {
7110 xmlSetProp(cur, BAD_CAST "ns", ns);
7111 xmlFree(ns);
7112 }
7113 }
7114 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7115 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007116
Daniel Veillard4c004142003-10-07 11:33:24 +00007117 /*
7118 * Simplification: 4.10. QNames
7119 */
7120 name = xmlNodeGetContent(cur);
7121 if (name != NULL) {
7122 local = xmlSplitQName2(name, &prefix);
7123 if (local != NULL) {
7124 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007125
Daniel Veillard4c004142003-10-07 11:33:24 +00007126 ns = xmlSearchNs(cur->doc, cur, prefix);
7127 if (ns == NULL) {
7128 xmlRngPErr(ctxt, cur,
7129 XML_RNGP_PREFIX_UNDEFINED,
7130 "xmlRelaxNGParse: no namespace for prefix %s\n",
7131 prefix, NULL);
7132 } else {
7133 xmlSetProp(cur, BAD_CAST "ns",
7134 ns->href);
7135 xmlNodeSetContent(cur, local);
7136 }
7137 xmlFree(local);
7138 xmlFree(prefix);
7139 }
7140 xmlFree(name);
7141 }
7142 }
7143 /*
7144 * 4.16
7145 */
7146 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7147 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7148 xmlRngPErr(ctxt, cur,
7149 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7150 "Found nsName/except//nsName forbidden construct\n",
7151 NULL, NULL);
7152 }
7153 }
7154 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7155 (cur != root)) {
7156 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007157
Daniel Veillard4c004142003-10-07 11:33:24 +00007158 /*
7159 * 4.16
7160 */
7161 if ((cur->parent != NULL) &&
7162 (xmlStrEqual
7163 (cur->parent->name, BAD_CAST "anyName"))) {
7164 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7165 xmlRelaxNGCleanupTree(ctxt, cur);
7166 ctxt->flags = oldflags;
7167 goto skip_children;
7168 } else if ((cur->parent != NULL) &&
7169 (xmlStrEqual
7170 (cur->parent->name, BAD_CAST "nsName"))) {
7171 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7172 xmlRelaxNGCleanupTree(ctxt, cur);
7173 ctxt->flags = oldflags;
7174 goto skip_children;
7175 }
7176 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7177 /*
7178 * 4.16
7179 */
7180 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7181 xmlRngPErr(ctxt, cur,
7182 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7183 "Found anyName/except//anyName forbidden construct\n",
7184 NULL, NULL);
7185 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7186 xmlRngPErr(ctxt, cur,
7187 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7188 "Found nsName/except//anyName forbidden construct\n",
7189 NULL, NULL);
7190 }
7191 }
7192 /*
7193 * Thisd is not an else since "include" is transformed
7194 * into a div
7195 */
7196 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7197 xmlChar *ns;
7198 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007199
Daniel Veillard4c004142003-10-07 11:33:24 +00007200 /*
7201 * implements rule 4.11
7202 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007203
Daniel Veillard4c004142003-10-07 11:33:24 +00007204 ns = xmlGetProp(cur, BAD_CAST "ns");
7205
7206 child = cur->children;
7207 ins = cur;
7208 while (child != NULL) {
7209 if (ns != NULL) {
7210 if (!xmlHasProp(child, BAD_CAST "ns")) {
7211 xmlSetProp(child, BAD_CAST "ns", ns);
7212 }
7213 }
7214 tmp = child->next;
7215 xmlUnlinkNode(child);
7216 ins = xmlAddNextSibling(ins, child);
7217 child = tmp;
7218 }
7219 if (ns != NULL)
7220 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007221 /*
7222 * Since we are about to delete cur, if it's nsDef is non-NULL we
7223 * need to preserve it (it contains the ns definitions for the
7224 * children we just moved). We'll just stick it on to the end
7225 * of cur->parent's list, since it's never going to be re-serialized
7226 * (bug 143738).
7227 */
7228 if (cur->nsDef != NULL) {
7229 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7230 while (parDef->next != NULL)
7231 parDef = parDef->next;
7232 parDef->next = cur->nsDef;
7233 cur->nsDef = NULL;
7234 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007235 delete = cur;
7236 goto skip_children;
7237 }
7238 }
7239 }
7240 /*
7241 * Simplification 4.2 whitespaces
7242 */
7243 else if ((cur->type == XML_TEXT_NODE) ||
7244 (cur->type == XML_CDATA_SECTION_NODE)) {
7245 if (IS_BLANK_NODE(cur)) {
7246 if (cur->parent->type == XML_ELEMENT_NODE) {
7247 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7248 &&
7249 (!xmlStrEqual
7250 (cur->parent->name, BAD_CAST "param")))
7251 delete = cur;
7252 } else {
7253 delete = cur;
7254 goto skip_children;
7255 }
7256 }
7257 } else {
7258 delete = cur;
7259 goto skip_children;
7260 }
7261
7262 /*
7263 * Skip to next node
7264 */
7265 if (cur->children != NULL) {
7266 if ((cur->children->type != XML_ENTITY_DECL) &&
7267 (cur->children->type != XML_ENTITY_REF_NODE) &&
7268 (cur->children->type != XML_ENTITY_NODE)) {
7269 cur = cur->children;
7270 continue;
7271 }
7272 }
7273 skip_children:
7274 if (cur->next != NULL) {
7275 cur = cur->next;
7276 continue;
7277 }
7278
7279 do {
7280 cur = cur->parent;
7281 if (cur == NULL)
7282 break;
7283 if (cur == root) {
7284 cur = NULL;
7285 break;
7286 }
7287 if (cur->next != NULL) {
7288 cur = cur->next;
7289 break;
7290 }
7291 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007292 }
7293 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007294 xmlUnlinkNode(delete);
7295 xmlFreeNode(delete);
7296 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007297 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007298}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007299
Daniel Veillardc5312d72003-02-21 17:14:10 +00007300/**
7301 * xmlRelaxNGCleanupDoc:
7302 * @ctxt: a Relax-NG parser context
7303 * @doc: an xmldocPtr document pointer
7304 *
7305 * Cleanup the document from unwanted nodes for parsing, resolve
7306 * Include and externalRef lookups.
7307 *
7308 * Returns the cleaned up document or NULL in case of error
7309 */
7310static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007311xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7312{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007313 xmlNodePtr root;
7314
7315 /*
7316 * Extract the root
7317 */
7318 root = xmlDocGetRootElement(doc);
7319 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007320 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7321 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007322 return (NULL);
7323 }
7324 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007325 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007326}
7327
7328/**
7329 * xmlRelaxNGParse:
7330 * @ctxt: a Relax-NG parser context
7331 *
7332 * parse a schema definition resource and build an internal
7333 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007334 *
7335 * Returns the internal XML RelaxNG structure built from the resource or
7336 * NULL in case of error
7337 */
7338xmlRelaxNGPtr
7339xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7340{
7341 xmlRelaxNGPtr ret = NULL;
7342 xmlDocPtr doc;
7343 xmlNodePtr root;
7344
7345 xmlRelaxNGInitTypes();
7346
7347 if (ctxt == NULL)
7348 return (NULL);
7349
7350 /*
7351 * First step is to parse the input document into an DOM/Infoset
7352 */
7353 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007354 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007355 if (doc == NULL) {
7356 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7357 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7358 NULL);
7359 return (NULL);
7360 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007361 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007362 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007363 if (doc == NULL) {
7364 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7365 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7366 NULL);
7367 return (NULL);
7368 }
7369 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7370 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007371 } else if (ctxt->document != NULL) {
7372 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007373 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007374 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7375 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7376 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007377 }
7378 ctxt->document = doc;
7379
7380 /*
7381 * Some preprocessing of the document content
7382 */
7383 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7384 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007385 xmlFreeDoc(ctxt->document);
7386 ctxt->document = NULL;
7387 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007388 }
7389
Daniel Veillard6eadf632003-01-23 18:29:16 +00007390 /*
7391 * Then do the parsing for good
7392 */
7393 root = xmlDocGetRootElement(doc);
7394 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007395 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7396 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7397 ctxt->URL, NULL);
7398 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007399 return (NULL);
7400 }
7401 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007402 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007403 xmlFreeDoc(doc);
7404 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007405 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007406
7407 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007408 * Check the ref/defines links
7409 */
7410 /*
7411 * try to preprocess interleaves
7412 */
7413 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007414 xmlHashScan(ctxt->interleaves,
7415 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007416 }
7417
7418 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007419 * if there was a parsing error return NULL
7420 */
7421 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007422 xmlRelaxNGFree(ret);
7423 ctxt->document = NULL;
7424 xmlFreeDoc(doc);
7425 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007426 }
7427
7428 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007429 * try to compile (parts of) the schemas
7430 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007431 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7432 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007433 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007434
Daniel Veillard4c004142003-10-07 11:33:24 +00007435 def = xmlRelaxNGNewDefine(ctxt, NULL);
7436 if (def != NULL) {
7437 def->type = XML_RELAXNG_START;
7438 def->content = ret->topgrammar->start;
7439 ret->topgrammar->start = def;
7440 }
7441 }
7442 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007443 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007444
7445 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007446 * Transfer the pointer for cleanup at the schema level.
7447 */
7448 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007449 ctxt->document = NULL;
7450 ret->documents = ctxt->documents;
7451 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007452
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007453 ret->includes = ctxt->includes;
7454 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007455 ret->defNr = ctxt->defNr;
7456 ret->defTab = ctxt->defTab;
7457 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007458 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007459 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007460
7461 return (ret);
7462}
Daniel Veillard4c004142003-10-07 11:33:24 +00007463
Daniel Veillard6eadf632003-01-23 18:29:16 +00007464/**
7465 * xmlRelaxNGSetParserErrors:
7466 * @ctxt: a Relax-NG validation context
7467 * @err: the error callback
7468 * @warn: the warning callback
7469 * @ctx: contextual data for the callbacks
7470 *
7471 * Set the callback functions used to handle errors for a validation context
7472 */
7473void
7474xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007475 xmlRelaxNGValidityErrorFunc err,
7476 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7477{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007478 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007479 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007480 ctxt->error = err;
7481 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007482 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007483 ctxt->userData = ctx;
7484}
Daniel Veillard409a8142003-07-18 15:16:57 +00007485
7486/**
7487 * xmlRelaxNGGetParserErrors:
7488 * @ctxt: a Relax-NG validation context
7489 * @err: the error callback result
7490 * @warn: the warning callback result
7491 * @ctx: contextual data for the callbacks result
7492 *
7493 * Get the callback information used to handle errors for a validation context
7494 *
7495 * Returns -1 in case of failure, 0 otherwise.
7496 */
7497int
7498xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007499 xmlRelaxNGValidityErrorFunc * err,
7500 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7501{
Daniel Veillard409a8142003-07-18 15:16:57 +00007502 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007503 return (-1);
7504 if (err != NULL)
7505 *err = ctxt->error;
7506 if (warn != NULL)
7507 *warn = ctxt->warning;
7508 if (ctx != NULL)
7509 *ctx = ctxt->userData;
7510 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007511}
7512
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007513#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007514
Daniel Veillard6eadf632003-01-23 18:29:16 +00007515/************************************************************************
7516 * *
7517 * Dump back a compiled form *
7518 * *
7519 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007520static void xmlRelaxNGDumpDefine(FILE * output,
7521 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007522
7523/**
7524 * xmlRelaxNGDumpDefines:
7525 * @output: the file output
7526 * @defines: a list of define structures
7527 *
7528 * Dump a RelaxNG structure back
7529 */
7530static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007531xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7532{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007533 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007534 xmlRelaxNGDumpDefine(output, defines);
7535 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007536 }
7537}
7538
7539/**
7540 * xmlRelaxNGDumpDefine:
7541 * @output: the file output
7542 * @define: a define structure
7543 *
7544 * Dump a RelaxNG structure back
7545 */
7546static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007547xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7548{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007549 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007550 return;
7551 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007552 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007553 fprintf(output, "<empty/>\n");
7554 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007555 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007556 fprintf(output, "<notAllowed/>\n");
7557 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007558 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007559 fprintf(output, "<text/>\n");
7560 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007561 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007562 fprintf(output, "<element>\n");
7563 if (define->name != NULL) {
7564 fprintf(output, "<name");
7565 if (define->ns != NULL)
7566 fprintf(output, " ns=\"%s\"", define->ns);
7567 fprintf(output, ">%s</name>\n", define->name);
7568 }
7569 xmlRelaxNGDumpDefines(output, define->attrs);
7570 xmlRelaxNGDumpDefines(output, define->content);
7571 fprintf(output, "</element>\n");
7572 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007573 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007574 fprintf(output, "<list>\n");
7575 xmlRelaxNGDumpDefines(output, define->content);
7576 fprintf(output, "</list>\n");
7577 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007578 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007579 fprintf(output, "<oneOrMore>\n");
7580 xmlRelaxNGDumpDefines(output, define->content);
7581 fprintf(output, "</oneOrMore>\n");
7582 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007583 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007584 fprintf(output, "<zeroOrMore>\n");
7585 xmlRelaxNGDumpDefines(output, define->content);
7586 fprintf(output, "</zeroOrMore>\n");
7587 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007588 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007589 fprintf(output, "<choice>\n");
7590 xmlRelaxNGDumpDefines(output, define->content);
7591 fprintf(output, "</choice>\n");
7592 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007593 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 fprintf(output, "<group>\n");
7595 xmlRelaxNGDumpDefines(output, define->content);
7596 fprintf(output, "</group>\n");
7597 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007598 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007599 fprintf(output, "<interleave>\n");
7600 xmlRelaxNGDumpDefines(output, define->content);
7601 fprintf(output, "</interleave>\n");
7602 break;
7603 case XML_RELAXNG_OPTIONAL:
7604 fprintf(output, "<optional>\n");
7605 xmlRelaxNGDumpDefines(output, define->content);
7606 fprintf(output, "</optional>\n");
7607 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007608 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007609 fprintf(output, "<attribute>\n");
7610 xmlRelaxNGDumpDefines(output, define->content);
7611 fprintf(output, "</attribute>\n");
7612 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007613 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007614 fprintf(output, "<define");
7615 if (define->name != NULL)
7616 fprintf(output, " name=\"%s\"", define->name);
7617 fprintf(output, ">\n");
7618 xmlRelaxNGDumpDefines(output, define->content);
7619 fprintf(output, "</define>\n");
7620 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007621 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007622 fprintf(output, "<ref");
7623 if (define->name != NULL)
7624 fprintf(output, " name=\"%s\"", define->name);
7625 fprintf(output, ">\n");
7626 xmlRelaxNGDumpDefines(output, define->content);
7627 fprintf(output, "</ref>\n");
7628 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007629 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007630 fprintf(output, "<parentRef");
7631 if (define->name != NULL)
7632 fprintf(output, " name=\"%s\"", define->name);
7633 fprintf(output, ">\n");
7634 xmlRelaxNGDumpDefines(output, define->content);
7635 fprintf(output, "</parentRef>\n");
7636 break;
7637 case XML_RELAXNG_EXTERNALREF:
7638 fprintf(output, "<externalRef>");
7639 xmlRelaxNGDumpDefines(output, define->content);
7640 fprintf(output, "</externalRef>\n");
7641 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007642 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007643 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007644 TODO break;
7645 case XML_RELAXNG_START:
7646 case XML_RELAXNG_EXCEPT:
7647 case XML_RELAXNG_PARAM:
7648 TODO break;
7649 case XML_RELAXNG_NOOP:
7650 xmlRelaxNGDumpDefines(output, define->content);
7651 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007652 }
7653}
Daniel Veillard4c004142003-10-07 11:33:24 +00007654
Daniel Veillard6eadf632003-01-23 18:29:16 +00007655/**
7656 * xmlRelaxNGDumpGrammar:
7657 * @output: the file output
7658 * @grammar: a grammar structure
7659 * @top: is this a top grammar
7660 *
7661 * Dump a RelaxNG structure back
7662 */
7663static void
7664xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7665{
7666 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007667 return;
7668
Daniel Veillard6eadf632003-01-23 18:29:16 +00007669 fprintf(output, "<grammar");
7670 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007671 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7672 switch (grammar->combine) {
7673 case XML_RELAXNG_COMBINE_UNDEFINED:
7674 break;
7675 case XML_RELAXNG_COMBINE_CHOICE:
7676 fprintf(output, " combine=\"choice\"");
7677 break;
7678 case XML_RELAXNG_COMBINE_INTERLEAVE:
7679 fprintf(output, " combine=\"interleave\"");
7680 break;
7681 default:
7682 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007683 }
7684 fprintf(output, ">\n");
7685 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007686 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007687 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007688 fprintf(output, "<start>\n");
7689 xmlRelaxNGDumpDefine(output, grammar->start);
7690 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007691 }
7692 /* TODO ? Dump the defines ? */
7693 fprintf(output, "</grammar>\n");
7694}
7695
7696/**
7697 * xmlRelaxNGDump:
7698 * @output: the file output
7699 * @schema: a schema structure
7700 *
7701 * Dump a RelaxNG structure back
7702 */
7703void
7704xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7705{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007706 if (output == NULL)
7707 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007708 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007709 fprintf(output, "RelaxNG empty or failed to compile\n");
7710 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007711 }
7712 fprintf(output, "RelaxNG: ");
7713 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007714 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007715 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007716 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007717 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007718 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007719 }
7720 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007721 fprintf(output, "RelaxNG has no top grammar\n");
7722 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007723 }
7724 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7725}
7726
Daniel Veillardfebcca42003-02-16 15:44:18 +00007727/**
7728 * xmlRelaxNGDumpTree:
7729 * @output: the file output
7730 * @schema: a schema structure
7731 *
7732 * Dump the transformed RelaxNG tree.
7733 */
7734void
7735xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7736{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007737 if (output == NULL)
7738 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007739 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007740 fprintf(output, "RelaxNG empty or failed to compile\n");
7741 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007742 }
7743 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007744 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007745 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007746 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007747 }
7748}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007749#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007750
Daniel Veillard6eadf632003-01-23 18:29:16 +00007751/************************************************************************
7752 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007753 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007754 * *
7755 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007756static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7757 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007758
7759/**
7760 * xmlRelaxNGValidateCompiledCallback:
7761 * @exec: the regular expression instance
7762 * @token: the token which matched
7763 * @transdata: callback data, the define for the subelement if available
7764 @ @inputdata: callback data, the Relax NG validation context
7765 *
7766 * Handle the callback and if needed validate the element children.
7767 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007768static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007769xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007770 const xmlChar * token,
7771 void *transdata, void *inputdata)
7772{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007773 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7774 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7775 int ret;
7776
7777#ifdef DEBUG_COMPILE
7778 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007779 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007780#endif
7781 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007782 fprintf(stderr, "callback on %s missing context\n", token);
7783 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7784 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7785 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007786 }
7787 if (define == NULL) {
7788 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007789 return;
7790 fprintf(stderr, "callback on %s missing define\n", token);
7791 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7792 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7793 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007794 }
7795 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007796 fprintf(stderr, "callback on %s missing info\n", token);
7797 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7798 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7799 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007800 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007801 fprintf(stderr, "callback on %s define is not element\n", token);
7802 if (ctxt->errNo == XML_RELAXNG_OK)
7803 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7804 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007805 }
7806 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007807 if (ret != 0)
7808 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007809}
7810
7811/**
7812 * xmlRelaxNGValidateCompiledContent:
7813 * @ctxt: the RelaxNG validation context
7814 * @regexp: the regular expression as compiled
7815 * @content: list of children to test against the regexp
7816 *
7817 * Validate the content model of an element or start using the regexp
7818 *
7819 * Returns 0 in case of success, -1 in case of error.
7820 */
7821static int
7822xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007823 xmlRegexpPtr regexp, xmlNodePtr content)
7824{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007825 xmlRegExecCtxtPtr exec;
7826 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007827 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007828 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007829
7830 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007831 return (-1);
7832 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007833 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007834 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007835 cur = content;
7836 while (cur != NULL) {
7837 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007838 switch (cur->type) {
7839 case XML_TEXT_NODE:
7840 case XML_CDATA_SECTION_NODE:
7841 if (xmlIsBlankNode(cur))
7842 break;
7843 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7844 if (ret < 0) {
7845 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7846 cur->parent->name);
7847 }
7848 break;
7849 case XML_ELEMENT_NODE:
7850 if (cur->ns != NULL) {
7851 ret = xmlRegExecPushString2(exec, cur->name,
7852 cur->ns->href, ctxt);
7853 } else {
7854 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7855 }
7856 if (ret < 0) {
7857 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7858 }
7859 break;
7860 default:
7861 break;
7862 }
7863 if (ret < 0)
7864 break;
7865 /*
7866 * Switch to next element
7867 */
7868 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007869 }
7870 ret = xmlRegExecPushString(exec, NULL, NULL);
7871 if (ret == 1) {
7872 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007873 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007874 } else if (ret == 0) {
7875 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007876 * TODO: get some of the names needed to exit the current state of exec
7877 */
7878 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7879 ret = -1;
7880 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7881 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007882 } else {
7883 ret = -1;
7884 }
7885 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007886 /*
7887 * There might be content model errors outside of the pure
7888 * regexp validation, e.g. for attribute values.
7889 */
7890 if ((ret == 0) && (ctxt->perr != 0)) {
7891 ret = ctxt->perr;
7892 }
7893 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007894 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007895}
7896
7897/************************************************************************
7898 * *
7899 * Progressive validation of when possible *
7900 * *
7901 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007902static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7903 xmlRelaxNGDefinePtr defines);
7904static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00007905 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007906static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007907
7908/**
7909 * xmlRelaxNGElemPush:
7910 * @ctxt: the validation context
7911 * @exec: the regexp runtime for the new content model
7912 *
7913 * Push a new regexp for the current node content model on the stack
7914 *
7915 * Returns 0 in case of success and -1 in case of error.
7916 */
7917static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007918xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7919{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007920 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007921 ctxt->elemMax = 10;
7922 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7923 sizeof
7924 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007925 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007926 xmlRngVErrMemory(ctxt, "validating\n");
7927 return (-1);
7928 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007929 }
7930 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007931 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007932 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007933 ctxt->elemMax *
7934 sizeof
7935 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007936 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007937 xmlRngVErrMemory(ctxt, "validating\n");
7938 return (-1);
7939 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007940 }
7941 ctxt->elemTab[ctxt->elemNr++] = exec;
7942 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007943 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007944}
7945
7946/**
7947 * xmlRelaxNGElemPop:
7948 * @ctxt: the validation context
7949 *
7950 * Pop the regexp of the current node content model from the stack
7951 *
7952 * Returns the exec or NULL if empty
7953 */
7954static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007955xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7956{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007957 xmlRegExecCtxtPtr ret;
7958
Daniel Veillard4c004142003-10-07 11:33:24 +00007959 if (ctxt->elemNr <= 0)
7960 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007961 ctxt->elemNr--;
7962 ret = ctxt->elemTab[ctxt->elemNr];
7963 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007964 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007965 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7966 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007967 ctxt->elem = NULL;
7968 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007969}
7970
7971/**
7972 * xmlRelaxNGValidateProgressiveCallback:
7973 * @exec: the regular expression instance
7974 * @token: the token which matched
7975 * @transdata: callback data, the define for the subelement if available
7976 @ @inputdata: callback data, the Relax NG validation context
7977 *
7978 * Handle the callback and if needed validate the element children.
7979 * some of the in/out informations are passed via the context in @inputdata.
7980 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007981static void
7982xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7983 ATTRIBUTE_UNUSED,
7984 const xmlChar * token,
7985 void *transdata, void *inputdata)
7986{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007987 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7988 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007989 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007990 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007991 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007992
7993#ifdef DEBUG_PROGRESSIVE
7994 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007995 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007996#endif
7997 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007998 fprintf(stderr, "callback on %s missing context\n", token);
7999 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008000 }
8001 ctxt->pstate = 1;
8002 if (define == NULL) {
8003 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008004 return;
8005 fprintf(stderr, "callback on %s missing define\n", token);
8006 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8007 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8008 ctxt->pstate = -1;
8009 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008010 }
8011 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008012 fprintf(stderr, "callback on %s missing info\n", token);
8013 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8014 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8015 ctxt->pstate = -1;
8016 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008017 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008018 fprintf(stderr, "callback on %s define is not element\n", token);
8019 if (ctxt->errNo == XML_RELAXNG_OK)
8020 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8021 ctxt->pstate = -1;
8022 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008023 }
8024 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008025 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8026 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8027 xmlRelaxNGDumpValidError(ctxt);
8028 ctxt->pstate = -1;
8029 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008030 }
8031 if (define->contModel == NULL) {
8032 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008033 * this node cannot be validated in a streamable fashion
8034 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008035#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008036 xmlGenericError(xmlGenericErrorContext,
8037 "Element '%s' validation is not streamable\n",
8038 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008039#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008040 ctxt->pstate = 0;
8041 ctxt->pdef = define;
8042 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008043 }
8044 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008045 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008046 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008047 ctxt->pstate = -1;
8048 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008049 }
8050 xmlRelaxNGElemPush(ctxt, exec);
8051
8052 /*
8053 * Validate the attributes part of the content.
8054 */
8055 state = xmlRelaxNGNewValidState(ctxt, node);
8056 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008057 ctxt->pstate = -1;
8058 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008059 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008060 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008061 ctxt->state = state;
8062 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008063 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8064 if (ret != 0) {
8065 ctxt->pstate = -1;
8066 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8067 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008068 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008069 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008070 ctxt->state->seq = NULL;
8071 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8072 if (ret != 0) {
8073 ctxt->pstate = -1;
8074 }
8075 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008076 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008077 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008078
8079 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008080
Daniel Veillard4c004142003-10-07 11:33:24 +00008081 for (i = 0; i < ctxt->states->nbState; i++) {
8082 state = ctxt->states->tabState[i];
8083 ctxt->state = state;
8084 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008085
Daniel Veillard4c004142003-10-07 11:33:24 +00008086 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8087 tmp = 0;
8088 break;
8089 }
8090 }
8091 if (tmp != 0) {
8092 /*
8093 * validation error, log the message for the "best" one
8094 */
8095 ctxt->flags |= FLAGS_IGNORABLE;
8096 xmlRelaxNGLogBestError(ctxt);
8097 }
8098 for (i = 0; i < ctxt->states->nbState; i++) {
8099 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8100 }
8101 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8102 ctxt->states = NULL;
8103 if ((ret == 0) && (tmp == -1))
8104 ctxt->pstate = -1;
8105 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008106 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008107 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008108 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8109 xmlRelaxNGDumpValidError(ctxt);
8110 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008111 }
8112 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008113}
8114
8115/**
8116 * xmlRelaxNGValidatePushElement:
8117 * @ctxt: the validation context
8118 * @doc: a document instance
8119 * @elem: an element instance
8120 *
8121 * Push a new element start on the RelaxNG validation stack.
8122 *
8123 * returns 1 if no validation problem was found or 0 if validating the
8124 * element requires a full node, and -1 in case of error.
8125 */
8126int
Daniel Veillard33300b42003-04-17 09:09:19 +00008127xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8128 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008129 xmlNodePtr elem)
8130{
8131 int ret = 1;
8132
8133 if ((ctxt == NULL) || (elem == NULL))
8134 return (-1);
8135
8136#ifdef DEBUG_PROGRESSIVE
8137 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8138#endif
8139 if (ctxt->elem == 0) {
8140 xmlRelaxNGPtr schema;
8141 xmlRelaxNGGrammarPtr grammar;
8142 xmlRegExecCtxtPtr exec;
8143 xmlRelaxNGDefinePtr define;
8144
8145 schema = ctxt->schema;
8146 if (schema == NULL) {
8147 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8148 return (-1);
8149 }
8150 grammar = schema->topgrammar;
8151 if ((grammar == NULL) || (grammar->start == NULL)) {
8152 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8153 return (-1);
8154 }
8155 define = grammar->start;
8156 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008157 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008158 return (0);
8159 }
8160 exec = xmlRegNewExecCtxt(define->contModel,
8161 xmlRelaxNGValidateProgressiveCallback,
8162 ctxt);
8163 if (exec == NULL) {
8164 return (-1);
8165 }
8166 xmlRelaxNGElemPush(ctxt, exec);
8167 }
8168 ctxt->pnode = elem;
8169 ctxt->pstate = 0;
8170 if (elem->ns != NULL) {
8171 ret =
8172 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8173 ctxt);
8174 } else {
8175 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8176 }
8177 if (ret < 0) {
8178 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8179 } else {
8180 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008181 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008182 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008183 ret = -1;
8184 else
8185 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008186 }
8187#ifdef DEBUG_PROGRESSIVE
8188 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008189 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8190 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008191#endif
8192 return (ret);
8193}
8194
8195/**
8196 * xmlRelaxNGValidatePushCData:
8197 * @ctxt: the RelaxNG validation context
8198 * @data: some character data read
8199 * @len: the lenght of the data
8200 *
8201 * check the CData parsed for validation in the current stack
8202 *
8203 * returns 1 if no validation problem was found or -1 otherwise
8204 */
8205int
8206xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008207 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008208{
8209 int ret = 1;
8210
8211 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8212 return (-1);
8213
8214#ifdef DEBUG_PROGRESSIVE
8215 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8216#endif
8217
8218 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008219 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008220 break;
8221 data++;
8222 }
8223 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008224 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008225
8226 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8227 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008228 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008229#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008230 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008231#endif
8232
Daniel Veillard4c004142003-10-07 11:33:24 +00008233 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008234 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008235 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008236}
8237
8238/**
8239 * xmlRelaxNGValidatePopElement:
8240 * @ctxt: the RelaxNG validation context
8241 * @doc: a document instance
8242 * @elem: an element instance
8243 *
8244 * Pop the element end from the RelaxNG validation stack.
8245 *
8246 * returns 1 if no validation problem was found or 0 otherwise
8247 */
8248int
8249xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8250 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008251 xmlNodePtr elem)
8252{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008253 int ret;
8254 xmlRegExecCtxtPtr exec;
8255
Daniel Veillard4c004142003-10-07 11:33:24 +00008256 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8257 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008258#ifdef DEBUG_PROGRESSIVE
8259 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8260#endif
8261 /*
8262 * verify that we reached a terminal state of the content model.
8263 */
8264 exec = xmlRelaxNGElemPop(ctxt);
8265 ret = xmlRegExecPushString(exec, NULL, NULL);
8266 if (ret == 0) {
8267 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008268 * TODO: get some of the names needed to exit the current state of exec
8269 */
8270 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8271 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008272 } else if (ret < 0) {
8273 ret = -1;
8274 } else {
8275 ret = 1;
8276 }
8277 xmlRegFreeExecCtxt(exec);
8278#ifdef DEBUG_PROGRESSIVE
8279 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008280 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8281 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008282#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008283 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008284}
8285
8286/**
8287 * xmlRelaxNGValidateFullElement:
8288 * @ctxt: the validation context
8289 * @doc: a document instance
8290 * @elem: an element instance
8291 *
8292 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8293 * 0 and the content of the node has been expanded.
8294 *
8295 * returns 1 if no validation problem was found or -1 in case of error.
8296 */
8297int
Daniel Veillard33300b42003-04-17 09:09:19 +00008298xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8299 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008300 xmlNodePtr elem)
8301{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008302 int ret;
8303 xmlRelaxNGValidStatePtr state;
8304
Daniel Veillard4c004142003-10-07 11:33:24 +00008305 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8306 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008307#ifdef DEBUG_PROGRESSIVE
8308 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8309#endif
8310 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8311 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008312 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008313 }
8314 state->seq = elem;
8315 ctxt->state = state;
8316 ctxt->errNo = XML_RELAXNG_OK;
8317 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008318 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8319 ret = -1;
8320 else
8321 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008322 xmlRelaxNGFreeValidState(ctxt, state);
8323 ctxt->state = NULL;
8324#ifdef DEBUG_PROGRESSIVE
8325 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008326 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8327 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008328#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008329 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008330}
8331
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008332/************************************************************************
8333 * *
8334 * Generic interpreted validation implementation *
8335 * *
8336 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008337static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8338 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008339
8340/**
8341 * xmlRelaxNGSkipIgnored:
8342 * @ctxt: a schema validation context
8343 * @node: the top node.
8344 *
8345 * Skip ignorable nodes in that context
8346 *
8347 * Returns the new sibling or NULL in case of error.
8348 */
8349static xmlNodePtr
8350xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008351 xmlNodePtr node)
8352{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008353 /*
8354 * TODO complete and handle entities
8355 */
8356 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008357 ((node->type == XML_COMMENT_NODE) ||
8358 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008359 (node->type == XML_XINCLUDE_START) ||
8360 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008361 (((node->type == XML_TEXT_NODE) ||
8362 (node->type == XML_CDATA_SECTION_NODE)) &&
8363 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8364 (IS_BLANK_NODE(node)))))) {
8365 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008366 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008367 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008368}
8369
8370/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008371 * xmlRelaxNGNormalize:
8372 * @ctxt: a schema validation context
8373 * @str: the string to normalize
8374 *
8375 * Implements the normalizeWhiteSpace( s ) function from
8376 * section 6.2.9 of the spec
8377 *
8378 * Returns the new string or NULL in case of error.
8379 */
8380static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008381xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8382{
Daniel Veillardedc91922003-01-26 00:52:04 +00008383 xmlChar *ret, *p;
8384 const xmlChar *tmp;
8385 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008386
Daniel Veillardedc91922003-01-26 00:52:04 +00008387 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008388 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008389 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008390 while (*tmp != 0)
8391 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008392 len = tmp - str;
8393
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008394 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008395 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008396 xmlRngVErrMemory(ctxt, "validating\n");
8397 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008398 }
8399 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008400 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008401 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008402 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008403 if (IS_BLANK_CH(*str)) {
8404 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008405 str++;
8406 if (*str == 0)
8407 break;
8408 *p++ = ' ';
8409 } else
8410 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008411 }
8412 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008413 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008414}
8415
8416/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008417 * xmlRelaxNGValidateDatatype:
8418 * @ctxt: a Relax-NG validation context
8419 * @value: the string value
8420 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008421 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008422 *
8423 * Validate the given value against the dataype
8424 *
8425 * Returns 0 if the validation succeeded or an error code.
8426 */
8427static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008428xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8429 const xmlChar * value,
8430 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8431{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008432 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008433 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008434 void *result = NULL;
8435 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008436
8437 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008438 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008439 }
8440 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008441 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008442 if ((define->attrs != NULL) &&
8443 (define->attrs->type == XML_RELAXNG_PARAM)) {
8444 ret =
8445 lib->check(lib->data, define->name, value, &result, node);
8446 } else {
8447 ret = lib->check(lib->data, define->name, value, NULL, node);
8448 }
8449 } else
8450 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008451 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008452 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8453 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8454 lib->freef(lib->data, result);
8455 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008456 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008457 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008458 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008459 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008460 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008461 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8462 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008463 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008464 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008465 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008466 if (lib->facet != NULL) {
8467 tmp = lib->facet(lib->data, define->name, cur->name,
8468 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008469 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008470 ret = -1;
8471 }
8472 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008473 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008474 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008475 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008476
Daniel Veillard4c004142003-10-07 11:33:24 +00008477 oldvalue = ctxt->state->value;
8478 oldendvalue = ctxt->state->endvalue;
8479 ctxt->state->value = (xmlChar *) value;
8480 ctxt->state->endvalue = NULL;
8481 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8482 ctxt->state->value = (xmlChar *) oldvalue;
8483 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008484 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008485 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008486 lib->freef(lib->data, result);
8487 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008488}
8489
8490/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008491 * xmlRelaxNGNextValue:
8492 * @ctxt: a Relax-NG validation context
8493 *
8494 * Skip to the next value when validating within a list
8495 *
8496 * Returns 0 if the operation succeeded or an error code.
8497 */
8498static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008499xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8500{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008501 xmlChar *cur;
8502
8503 cur = ctxt->state->value;
8504 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008505 ctxt->state->value = NULL;
8506 ctxt->state->endvalue = NULL;
8507 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008508 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008509 while (*cur != 0)
8510 cur++;
8511 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8512 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008513 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008514 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008515 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008516 ctxt->state->value = cur;
8517 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008518}
8519
8520/**
8521 * xmlRelaxNGValidateValueList:
8522 * @ctxt: a Relax-NG validation context
8523 * @defines: the list of definitions to verify
8524 *
8525 * Validate the given set of definitions for the current value
8526 *
8527 * Returns 0 if the validation succeeded or an error code.
8528 */
8529static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008530xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8531 xmlRelaxNGDefinePtr defines)
8532{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008533 int ret = 0;
8534
8535 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008536 ret = xmlRelaxNGValidateValue(ctxt, defines);
8537 if (ret != 0)
8538 break;
8539 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008540 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008541 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008542}
8543
8544/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008545 * xmlRelaxNGValidateValue:
8546 * @ctxt: a Relax-NG validation context
8547 * @define: the definition to verify
8548 *
8549 * Validate the given definition for the current value
8550 *
8551 * Returns 0 if the validation succeeded or an error code.
8552 */
8553static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008554xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8555 xmlRelaxNGDefinePtr define)
8556{
Daniel Veillardedc91922003-01-26 00:52:04 +00008557 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008558 xmlChar *value;
8559
8560 value = ctxt->state->value;
8561 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008562 case XML_RELAXNG_EMPTY:{
8563 if ((value != NULL) && (value[0] != 0)) {
8564 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008565
William M. Brack76e95df2003-10-18 16:20:14 +00008566 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008567 idx++;
8568 if (value[idx] != 0)
8569 ret = -1;
8570 }
8571 break;
8572 }
8573 case XML_RELAXNG_TEXT:
8574 break;
8575 case XML_RELAXNG_VALUE:{
8576 if (!xmlStrEqual(value, define->value)) {
8577 if (define->name != NULL) {
8578 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008579
Daniel Veillard4c004142003-10-07 11:33:24 +00008580 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8581 if ((lib != NULL) && (lib->comp != NULL)) {
8582 ret = lib->comp(lib->data, define->name,
8583 define->value, define->node,
8584 (void *) define->attrs,
8585 value, ctxt->state->node);
8586 } else
8587 ret = -1;
8588 if (ret < 0) {
8589 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8590 define->name);
8591 return (-1);
8592 } else if (ret == 1) {
8593 ret = 0;
8594 } else {
8595 ret = -1;
8596 }
8597 } else {
8598 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008599
Daniel Veillard4c004142003-10-07 11:33:24 +00008600 /*
8601 * TODO: trivial optimizations are possible by
8602 * computing at compile-time
8603 */
8604 nval = xmlRelaxNGNormalize(ctxt, define->value);
8605 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008606
Daniel Veillard4c004142003-10-07 11:33:24 +00008607 if ((nval == NULL) || (nvalue == NULL) ||
8608 (!xmlStrEqual(nval, nvalue)))
8609 ret = -1;
8610 if (nval != NULL)
8611 xmlFree(nval);
8612 if (nvalue != NULL)
8613 xmlFree(nvalue);
8614 }
8615 }
8616 if (ret == 0)
8617 xmlRelaxNGNextValue(ctxt);
8618 break;
8619 }
8620 case XML_RELAXNG_DATATYPE:{
8621 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8622 ctxt->state->seq);
8623 if (ret == 0)
8624 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008625
Daniel Veillard4c004142003-10-07 11:33:24 +00008626 break;
8627 }
8628 case XML_RELAXNG_CHOICE:{
8629 xmlRelaxNGDefinePtr list = define->content;
8630 xmlChar *oldvalue;
8631
8632 oldflags = ctxt->flags;
8633 ctxt->flags |= FLAGS_IGNORABLE;
8634
8635 oldvalue = ctxt->state->value;
8636 while (list != NULL) {
8637 ret = xmlRelaxNGValidateValue(ctxt, list);
8638 if (ret == 0) {
8639 break;
8640 }
8641 ctxt->state->value = oldvalue;
8642 list = list->next;
8643 }
8644 ctxt->flags = oldflags;
8645 if (ret != 0) {
8646 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8647 xmlRelaxNGDumpValidError(ctxt);
8648 } else {
8649 if (ctxt->errNr > 0)
8650 xmlRelaxNGPopErrors(ctxt, 0);
8651 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008652 break;
8653 }
8654 case XML_RELAXNG_LIST:{
8655 xmlRelaxNGDefinePtr list = define->content;
8656 xmlChar *oldvalue, *oldend, *val, *cur;
8657
Daniel Veillard416589a2003-02-17 17:25:42 +00008658#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008659 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008660#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008661
Daniel Veillard4c004142003-10-07 11:33:24 +00008662 oldvalue = ctxt->state->value;
8663 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008664
Daniel Veillard4c004142003-10-07 11:33:24 +00008665 val = xmlStrdup(oldvalue);
8666 if (val == NULL) {
8667 val = xmlStrdup(BAD_CAST "");
8668 }
8669 if (val == NULL) {
8670 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8671 return (-1);
8672 }
8673 cur = val;
8674 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008675 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008676 *cur = 0;
8677 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008678#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008679 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008680#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008681 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008682 *cur++ = 0;
8683 } else
8684 cur++;
8685 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008686#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008687 xmlGenericError(xmlGenericErrorContext,
8688 "list value: '%s' found %d items\n",
8689 oldvalue, nb_values);
8690 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008691#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008692 ctxt->state->endvalue = cur;
8693 cur = val;
8694 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8695 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008696
Daniel Veillard4c004142003-10-07 11:33:24 +00008697 ctxt->state->value = cur;
8698
8699 while (list != NULL) {
8700 if (ctxt->state->value == ctxt->state->endvalue)
8701 ctxt->state->value = NULL;
8702 ret = xmlRelaxNGValidateValue(ctxt, list);
8703 if (ret != 0) {
8704#ifdef DEBUG_LIST
8705 xmlGenericError(xmlGenericErrorContext,
8706 "Failed to validate value: '%s' with %d rule\n",
8707 ctxt->state->value, nb_values);
8708#endif
8709 break;
8710 }
8711#ifdef DEBUG_LIST
8712 nb_values++;
8713#endif
8714 list = list->next;
8715 }
8716
8717 if ((ret == 0) && (ctxt->state->value != NULL) &&
8718 (ctxt->state->value != ctxt->state->endvalue)) {
8719 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8720 ctxt->state->value);
8721 ret = -1;
8722 }
8723 xmlFree(val);
8724 ctxt->state->value = oldvalue;
8725 ctxt->state->endvalue = oldend;
8726 break;
8727 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008728 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008729 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8730 if (ret != 0) {
8731 break;
8732 }
8733 /* no break on purpose */
8734 case XML_RELAXNG_ZEROORMORE:{
8735 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008736
Daniel Veillard4c004142003-10-07 11:33:24 +00008737 oldflags = ctxt->flags;
8738 ctxt->flags |= FLAGS_IGNORABLE;
8739 cur = ctxt->state->value;
8740 temp = NULL;
8741 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8742 (temp != cur)) {
8743 temp = cur;
8744 ret =
8745 xmlRelaxNGValidateValueList(ctxt, define->content);
8746 if (ret != 0) {
8747 ctxt->state->value = temp;
8748 ret = 0;
8749 break;
8750 }
8751 cur = ctxt->state->value;
8752 }
8753 ctxt->flags = oldflags;
8754 if (ret != 0) {
8755 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8756 xmlRelaxNGDumpValidError(ctxt);
8757 } else {
8758 if (ctxt->errNr > 0)
8759 xmlRelaxNGPopErrors(ctxt, 0);
8760 }
8761 break;
8762 }
8763 case XML_RELAXNG_EXCEPT:{
8764 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008765
Daniel Veillard4c004142003-10-07 11:33:24 +00008766 list = define->content;
8767 while (list != NULL) {
8768 ret = xmlRelaxNGValidateValue(ctxt, list);
8769 if (ret == 0) {
8770 ret = -1;
8771 break;
8772 } else
8773 ret = 0;
8774 list = list->next;
8775 }
8776 break;
8777 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008778 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008779 case XML_RELAXNG_GROUP:{
8780 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008781
Daniel Veillard4c004142003-10-07 11:33:24 +00008782 list = define->content;
8783 while (list != NULL) {
8784 ret = xmlRelaxNGValidateValue(ctxt, list);
8785 if (ret != 0) {
8786 ret = -1;
8787 break;
8788 } else
8789 ret = 0;
8790 list = list->next;
8791 }
8792 break;
8793 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008794 case XML_RELAXNG_REF:
8795 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008796 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8797 break;
8798 default:
8799 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008800 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008801 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008802}
8803
8804/**
8805 * xmlRelaxNGValidateValueContent:
8806 * @ctxt: a Relax-NG validation context
8807 * @defines: the list of definitions to verify
8808 *
8809 * Validate the given definitions for the current value
8810 *
8811 * Returns 0 if the validation succeeded or an error code.
8812 */
8813static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008814xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8815 xmlRelaxNGDefinePtr defines)
8816{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008817 int ret = 0;
8818
8819 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008820 ret = xmlRelaxNGValidateValue(ctxt, defines);
8821 if (ret != 0)
8822 break;
8823 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008824 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008825 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008826}
8827
8828/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008829 * xmlRelaxNGAttributeMatch:
8830 * @ctxt: a Relax-NG validation context
8831 * @define: the definition to check
8832 * @prop: the attribute
8833 *
8834 * Check if the attribute matches the definition nameClass
8835 *
8836 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8837 */
8838static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008839xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8840 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8841{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008842 int ret;
8843
8844 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008845 if (!xmlStrEqual(define->name, prop->name))
8846 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008847 }
8848 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008849 if (define->ns[0] == 0) {
8850 if (prop->ns != NULL)
8851 return (0);
8852 } else {
8853 if ((prop->ns == NULL) ||
8854 (!xmlStrEqual(define->ns, prop->ns->href)))
8855 return (0);
8856 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008857 }
8858 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008859 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008860 define = define->nameClass;
8861 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008862 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008863
Daniel Veillard4c004142003-10-07 11:33:24 +00008864 list = define->content;
8865 while (list != NULL) {
8866 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8867 if (ret == 1)
8868 return (0);
8869 if (ret < 0)
8870 return (ret);
8871 list = list->next;
8872 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008873 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008874 TODO}
8875 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008876}
8877
8878/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008879 * xmlRelaxNGValidateAttribute:
8880 * @ctxt: a Relax-NG validation context
8881 * @define: the definition to verify
8882 *
8883 * Validate the given attribute definition for that node
8884 *
8885 * Returns 0 if the validation succeeded or an error code.
8886 */
8887static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008888xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8889 xmlRelaxNGDefinePtr define)
8890{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008891 int ret = 0, i;
8892 xmlChar *value, *oldvalue;
8893 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008894 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008895
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008896 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008897 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008898 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008899 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8900 tmp = ctxt->state->attrs[i];
8901 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8902 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8903 (tmp->ns == NULL)) ||
8904 ((tmp->ns != NULL) &&
8905 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8906 prop = tmp;
8907 break;
8908 }
8909 }
8910 }
8911 if (prop != NULL) {
8912 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8913 oldvalue = ctxt->state->value;
8914 oldseq = ctxt->state->seq;
8915 ctxt->state->seq = (xmlNodePtr) prop;
8916 ctxt->state->value = value;
8917 ctxt->state->endvalue = NULL;
8918 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8919 if (ctxt->state->value != NULL)
8920 value = ctxt->state->value;
8921 if (value != NULL)
8922 xmlFree(value);
8923 ctxt->state->value = oldvalue;
8924 ctxt->state->seq = oldseq;
8925 if (ret == 0) {
8926 /*
8927 * flag the attribute as processed
8928 */
8929 ctxt->state->attrs[i] = NULL;
8930 ctxt->state->nbAttrLeft--;
8931 }
8932 } else {
8933 ret = -1;
8934 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008935#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008936 xmlGenericError(xmlGenericErrorContext,
8937 "xmlRelaxNGValidateAttribute(%s): %d\n",
8938 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008939#endif
8940 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008941 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8942 tmp = ctxt->state->attrs[i];
8943 if ((tmp != NULL) &&
8944 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8945 prop = tmp;
8946 break;
8947 }
8948 }
8949 if (prop != NULL) {
8950 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8951 oldvalue = ctxt->state->value;
8952 oldseq = ctxt->state->seq;
8953 ctxt->state->seq = (xmlNodePtr) prop;
8954 ctxt->state->value = value;
8955 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8956 if (ctxt->state->value != NULL)
8957 value = ctxt->state->value;
8958 if (value != NULL)
8959 xmlFree(value);
8960 ctxt->state->value = oldvalue;
8961 ctxt->state->seq = oldseq;
8962 if (ret == 0) {
8963 /*
8964 * flag the attribute as processed
8965 */
8966 ctxt->state->attrs[i] = NULL;
8967 ctxt->state->nbAttrLeft--;
8968 }
8969 } else {
8970 ret = -1;
8971 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008972#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008973 if (define->ns != NULL) {
8974 xmlGenericError(xmlGenericErrorContext,
8975 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8976 define->ns, ret);
8977 } else {
8978 xmlGenericError(xmlGenericErrorContext,
8979 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8980 ret);
8981 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008982#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008983 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008984
8985 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008986}
8987
8988/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008989 * xmlRelaxNGValidateAttributeList:
8990 * @ctxt: a Relax-NG validation context
8991 * @define: the list of definition to verify
8992 *
8993 * Validate the given node against the list of attribute definitions
8994 *
8995 * Returns 0 if the validation succeeded or an error code.
8996 */
8997static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008998xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8999 xmlRelaxNGDefinePtr defines)
9000{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009001 int ret = 0, res;
9002 int needmore = 0;
9003 xmlRelaxNGDefinePtr cur;
9004
9005 cur = defines;
9006 while (cur != NULL) {
9007 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009008 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9009 ret = -1;
9010 } else
9011 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009012 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009013 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009014 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009015 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009016 cur = defines;
9017 while (cur != NULL) {
9018 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009019 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9020 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9021 if (res < 0)
9022 ret = -1;
9023 } else {
9024 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9025 return (-1);
9026 }
9027 if (res == -1) /* continues on -2 */
9028 break;
9029 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009030 cur = cur->next;
9031 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009032
9033 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009034}
9035
9036/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009037 * xmlRelaxNGNodeMatchesList:
9038 * @node: the node
9039 * @list: a NULL terminated array of definitions
9040 *
9041 * Check if a node can be matched by one of the definitions
9042 *
9043 * Returns 1 if matches 0 otherwise
9044 */
9045static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009046xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9047{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009048 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009049 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009050
9051 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009052 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009053
9054 cur = list[i++];
9055 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009056 if ((node->type == XML_ELEMENT_NODE) &&
9057 (cur->type == XML_RELAXNG_ELEMENT)) {
9058 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9059 if (tmp == 1)
9060 return (1);
9061 } else if (((node->type == XML_TEXT_NODE) ||
9062 (node->type == XML_CDATA_SECTION_NODE)) &&
9063 (cur->type == XML_RELAXNG_TEXT)) {
9064 return (1);
9065 }
9066 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009067 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009068 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009069}
9070
9071/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009072 * xmlRelaxNGValidateInterleave:
9073 * @ctxt: a Relax-NG validation context
9074 * @define: the definition to verify
9075 *
9076 * Validate an interleave definition for a node.
9077 *
9078 * Returns 0 if the validation succeeded or an error code.
9079 */
9080static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009081xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9082 xmlRelaxNGDefinePtr define)
9083{
William M. Brack779af002003-08-01 15:55:39 +00009084 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009085 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009086 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009087
9088 xmlRelaxNGValidStatePtr oldstate;
9089 xmlRelaxNGPartitionPtr partitions;
9090 xmlRelaxNGInterleaveGroupPtr group = NULL;
9091 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9092 xmlNodePtr *list = NULL, *lasts = NULL;
9093
9094 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009095 partitions = (xmlRelaxNGPartitionPtr) define->data;
9096 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009097 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009098 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9099 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009100 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009101 /*
9102 * Optimizations for MIXED
9103 */
9104 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009105 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009106 ctxt->flags |= FLAGS_MIXED_CONTENT;
9107 if (nbgroups == 2) {
9108 /*
9109 * this is a pure <mixed> case
9110 */
9111 if (ctxt->state != NULL)
9112 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9113 ctxt->state->seq);
9114 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9115 ret = xmlRelaxNGValidateDefinition(ctxt,
9116 partitions->groups[1]->
9117 rule);
9118 else
9119 ret = xmlRelaxNGValidateDefinition(ctxt,
9120 partitions->groups[0]->
9121 rule);
9122 if (ret == 0) {
9123 if (ctxt->state != NULL)
9124 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9125 ctxt->state->
9126 seq);
9127 }
9128 ctxt->flags = oldflags;
9129 return (ret);
9130 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009131 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009132
9133 /*
9134 * Build arrays to store the first and last node of the chain
9135 * pertaining to each group
9136 */
9137 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9138 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009139 xmlRngVErrMemory(ctxt, "validating\n");
9140 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009141 }
9142 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9143 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9144 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009145 xmlRngVErrMemory(ctxt, "validating\n");
9146 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009147 }
9148 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9149
9150 /*
9151 * Walk the sequence of children finding the right group and
9152 * sorting them in sequences.
9153 */
9154 cur = ctxt->state->seq;
9155 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9156 start = cur;
9157 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009158 ctxt->state->seq = cur;
9159 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009160 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009161 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009162
Daniel Veillard4c004142003-10-07 11:33:24 +00009163 if ((cur->type == XML_TEXT_NODE) ||
9164 (cur->type == XML_CDATA_SECTION_NODE)) {
9165 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9166 NULL);
9167 } else if (cur->type == XML_ELEMENT_NODE) {
9168 if (cur->ns != NULL) {
9169 tmp = xmlHashLookup2(partitions->triage, cur->name,
9170 cur->ns->href);
9171 if (tmp == NULL)
9172 tmp = xmlHashLookup2(partitions->triage,
9173 BAD_CAST "#any",
9174 cur->ns->href);
9175 } else
9176 tmp =
9177 xmlHashLookup2(partitions->triage, cur->name,
9178 NULL);
9179 if (tmp == NULL)
9180 tmp =
9181 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9182 NULL);
9183 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009184
Daniel Veillard4c004142003-10-07 11:33:24 +00009185 if (tmp == NULL) {
9186 i = nbgroups;
9187 } else {
9188 i = ((long) tmp) - 1;
9189 if (partitions->flags & IS_NEEDCHECK) {
9190 group = partitions->groups[i];
9191 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9192 i = nbgroups;
9193 }
9194 }
9195 } else {
9196 for (i = 0; i < nbgroups; i++) {
9197 group = partitions->groups[i];
9198 if (group == NULL)
9199 continue;
9200 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9201 break;
9202 }
9203 }
9204 /*
9205 * We break as soon as an element not matched is found
9206 */
9207 if (i >= nbgroups) {
9208 break;
9209 }
9210 if (lasts[i] != NULL) {
9211 lasts[i]->next = cur;
9212 lasts[i] = cur;
9213 } else {
9214 list[i] = cur;
9215 lasts[i] = cur;
9216 }
9217 if (cur->next != NULL)
9218 lastchg = cur->next;
9219 else
9220 lastchg = cur;
9221 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009222 }
9223 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009224 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9225 ret = -1;
9226 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009227 }
9228 lastelem = cur;
9229 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009230 for (i = 0; i < nbgroups; i++) {
9231 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9232 group = partitions->groups[i];
9233 if (lasts[i] != NULL) {
9234 last = lasts[i]->next;
9235 lasts[i]->next = NULL;
9236 }
9237 ctxt->state->seq = list[i];
9238 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9239 if (ret != 0)
9240 break;
9241 if (ctxt->state != NULL) {
9242 cur = ctxt->state->seq;
9243 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9244 xmlRelaxNGFreeValidState(ctxt, oldstate);
9245 oldstate = ctxt->state;
9246 ctxt->state = NULL;
9247 if (cur != NULL) {
9248 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9249 ret = -1;
9250 ctxt->state = oldstate;
9251 goto done;
9252 }
9253 } else if (ctxt->states != NULL) {
9254 int j;
9255 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009256
Daniel Veillard4c004142003-10-07 11:33:24 +00009257 for (j = 0; j < ctxt->states->nbState; j++) {
9258 cur = ctxt->states->tabState[j]->seq;
9259 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9260 if (cur == NULL) {
9261 found = 1;
9262 break;
9263 }
9264 }
9265 if (ctxt->states->nbState > 0) {
9266 xmlRelaxNGFreeValidState(ctxt, oldstate);
9267 oldstate =
9268 ctxt->states->tabState[ctxt->states->nbState - 1];
9269 }
9270 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9271 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9272 }
9273 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9274 ctxt->states = NULL;
9275 if (found == 0) {
9276 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9277 ret = -1;
9278 ctxt->state = oldstate;
9279 goto done;
9280 }
9281 } else {
9282 ret = -1;
9283 break;
9284 }
9285 if (lasts[i] != NULL) {
9286 lasts[i]->next = last;
9287 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009288 }
9289 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009290 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009291 ctxt->state = oldstate;
9292 ctxt->state->seq = lastelem;
9293 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009294 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9295 ret = -1;
9296 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009297 }
9298
Daniel Veillard4c004142003-10-07 11:33:24 +00009299 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009300 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009301 /*
9302 * builds the next links chain from the prev one
9303 */
9304 cur = lastchg;
9305 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009306 if ((cur == start) || (cur->prev == NULL))
9307 break;
9308 cur->prev->next = cur;
9309 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009310 }
9311 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009312 if (ctxt->errNr > errNr)
9313 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009314 }
9315
9316 xmlFree(list);
9317 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009318 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009319}
9320
9321/**
9322 * xmlRelaxNGValidateDefinitionList:
9323 * @ctxt: a Relax-NG validation context
9324 * @define: the list of definition to verify
9325 *
9326 * Validate the given node content against the (list) of definitions
9327 *
9328 * Returns 0 if the validation succeeded or an error code.
9329 */
9330static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009331xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9332 xmlRelaxNGDefinePtr defines)
9333{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009334 int ret = 0, res;
9335
9336
Daniel Veillard952379b2003-03-17 15:37:12 +00009337 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009338 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9339 BAD_CAST "NULL definition list");
9340 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009341 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009342 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009343 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9344 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9345 if (res < 0)
9346 ret = -1;
9347 } else {
9348 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9349 return (-1);
9350 }
9351 if (res == -1) /* continues on -2 */
9352 break;
9353 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009354 }
9355
Daniel Veillard4c004142003-10-07 11:33:24 +00009356 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009357}
9358
9359/**
9360 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009361 * @ctxt: a Relax-NG validation context
9362 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009363 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009364 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009365 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009366 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009367 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009368 */
9369static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009370xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9371 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9372{
Daniel Veillard580ced82003-03-21 21:22:48 +00009373 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009374
Daniel Veillardfd573f12003-03-16 17:52:32 +00009375 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009376 if (!xmlStrEqual(elem->name, define->name)) {
9377 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9378 return (0);
9379 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009380 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009381 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009382 if (elem->ns == NULL) {
9383 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9384 return (0);
9385 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9386 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9387 elem->name, define->ns);
9388 return (0);
9389 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009390 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009391 (define->name == NULL)) {
9392 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9393 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009394 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009395 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9396 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009397 }
9398
9399 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009400 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009401
9402 define = define->nameClass;
9403 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009404 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009405
Daniel Veillard4c004142003-10-07 11:33:24 +00009406 if (ctxt != NULL) {
9407 oldflags = ctxt->flags;
9408 ctxt->flags |= FLAGS_IGNORABLE;
9409 }
9410
9411 list = define->content;
9412 while (list != NULL) {
9413 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9414 if (ret == 1) {
9415 if (ctxt != NULL)
9416 ctxt->flags = oldflags;
9417 return (0);
9418 }
9419 if (ret < 0) {
9420 if (ctxt != NULL)
9421 ctxt->flags = oldflags;
9422 return (ret);
9423 }
9424 list = list->next;
9425 }
9426 ret = 1;
9427 if (ctxt != NULL) {
9428 ctxt->flags = oldflags;
9429 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009430 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009431 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009432
Daniel Veillard4c004142003-10-07 11:33:24 +00009433 if (ctxt != NULL) {
9434 oldflags = ctxt->flags;
9435 ctxt->flags |= FLAGS_IGNORABLE;
9436 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009437
Daniel Veillard4c004142003-10-07 11:33:24 +00009438 list = define->nameClass;
9439 while (list != NULL) {
9440 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9441 if (ret == 1) {
9442 if (ctxt != NULL)
9443 ctxt->flags = oldflags;
9444 return (1);
9445 }
9446 if (ret < 0) {
9447 if (ctxt != NULL)
9448 ctxt->flags = oldflags;
9449 return (ret);
9450 }
9451 list = list->next;
9452 }
9453 if (ctxt != NULL) {
9454 if (ret != 0) {
9455 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9456 xmlRelaxNGDumpValidError(ctxt);
9457 } else {
9458 if (ctxt->errNr > 0)
9459 xmlRelaxNGPopErrors(ctxt, 0);
9460 }
9461 }
9462 ret = 0;
9463 if (ctxt != NULL) {
9464 ctxt->flags = oldflags;
9465 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009466 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009467 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009468 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009469 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009470}
9471
9472/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009473 * xmlRelaxNGBestState:
9474 * @ctxt: a Relax-NG validation context
9475 *
9476 * Find the "best" state in the ctxt->states list of states to report
9477 * errors about. I.e. a state with no element left in the child list
9478 * or the one with the less attributes left.
9479 * This is called only if a falidation error was detected
9480 *
9481 * Returns the index of the "best" state or -1 in case of error
9482 */
9483static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009484xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9485{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009486 xmlRelaxNGValidStatePtr state;
9487 int i, tmp;
9488 int best = -1;
9489 int value = 1000000;
9490
9491 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9492 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009493 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009494
Daniel Veillard4c004142003-10-07 11:33:24 +00009495 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009496 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009497 if (state == NULL)
9498 continue;
9499 if (state->seq != NULL) {
9500 if ((best == -1) || (value > 100000)) {
9501 value = 100000;
9502 best = i;
9503 }
9504 } else {
9505 tmp = state->nbAttrLeft;
9506 if ((best == -1) || (value > tmp)) {
9507 value = tmp;
9508 best = i;
9509 }
9510 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009511 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009512 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009513}
9514
9515/**
9516 * xmlRelaxNGLogBestError:
9517 * @ctxt: a Relax-NG validation context
9518 *
9519 * Find the "best" state in the ctxt->states list of states to report
9520 * errors about and log it.
9521 */
9522static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009523xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9524{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009525 int best;
9526
9527 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9528 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009529 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009530
9531 best = xmlRelaxNGBestState(ctxt);
9532 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009533 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009534
Daniel Veillard4c004142003-10-07 11:33:24 +00009535 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009536 }
9537}
9538
9539/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009540 * xmlRelaxNGValidateElementEnd:
9541 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009542 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009543 *
9544 * Validate the end of the element, implements check that
9545 * there is nothing left not consumed in the element content
9546 * or in the attribute list.
9547 *
9548 * Returns 0 if the validation succeeded or an error code.
9549 */
9550static int
William M. Brack272693c2003-11-14 16:20:34 +00009551xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009552{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009553 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009554 xmlRelaxNGValidStatePtr state;
9555
9556 state = ctxt->state;
9557 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009558 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9559 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009560 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009561 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9562 state->node->name, state->seq->name);
9563 }
9564 return (-1);
9565 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009566 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009567 for (i = 0; i < state->nbAttrs; i++) {
9568 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009569 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009570 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9571 state->attrs[i]->name, state->node->name);
9572 }
9573 return (-1 - i);
9574 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009575 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009576 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009577}
9578
9579/**
9580 * xmlRelaxNGValidateState:
9581 * @ctxt: a Relax-NG validation context
9582 * @define: the definition to verify
9583 *
9584 * Validate the current state against the definition
9585 *
9586 * Returns 0 if the validation succeeded or an error code.
9587 */
9588static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009589xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9590 xmlRelaxNGDefinePtr define)
9591{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009592 xmlNodePtr node;
9593 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009594 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009595
9596 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009597 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9598 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009599 }
9600
9601 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009602 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009603 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009604 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009605 }
9606#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009607 for (i = 0; i < ctxt->depth; i++)
9608 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009609 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009610 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009611 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009612 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009613 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009614 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009615 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009616 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009617#endif
9618 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009619 switch (define->type) {
9620 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009621 node = xmlRelaxNGSkipIgnored(ctxt, node);
9622 ret = 0;
9623 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009624 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009625 ret = -1;
9626 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009627 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009628 while ((node != NULL) &&
9629 ((node->type == XML_TEXT_NODE) ||
9630 (node->type == XML_COMMENT_NODE) ||
9631 (node->type == XML_PI_NODE) ||
9632 (node->type == XML_CDATA_SECTION_NODE)))
9633 node = node->next;
9634 ctxt->state->seq = node;
9635 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009636 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009637 errNr = ctxt->errNr;
9638 node = xmlRelaxNGSkipIgnored(ctxt, node);
9639 if (node == NULL) {
9640 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9641 ret = -1;
9642 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9643 xmlRelaxNGDumpValidError(ctxt);
9644 break;
9645 }
9646 if (node->type != XML_ELEMENT_NODE) {
9647 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9648 ret = -1;
9649 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9650 xmlRelaxNGDumpValidError(ctxt);
9651 break;
9652 }
9653 /*
9654 * This node was already validated successfully against
9655 * this definition.
9656 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009657 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009658 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9659 if (ctxt->errNr > errNr)
9660 xmlRelaxNGPopErrors(ctxt, errNr);
9661 if (ctxt->errNr != 0) {
9662 while ((ctxt->err != NULL) &&
9663 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9664 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9665 ||
9666 ((ctxt->err->err ==
9667 XML_RELAXNG_ERR_ELEMEXTRANS)
9668 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9669 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9670 || (ctxt->err->err ==
9671 XML_RELAXNG_ERR_NOTELEM)))
9672 xmlRelaxNGValidErrorPop(ctxt);
9673 }
9674 break;
9675 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009676
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009677 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9678 if (ret <= 0) {
9679 ret = -1;
9680 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9681 xmlRelaxNGDumpValidError(ctxt);
9682 break;
9683 }
9684 ret = 0;
9685 if (ctxt->errNr != 0) {
9686 if (ctxt->errNr > errNr)
9687 xmlRelaxNGPopErrors(ctxt, errNr);
9688 while ((ctxt->err != NULL) &&
9689 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9690 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9691 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9692 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9693 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9694 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9695 xmlRelaxNGValidErrorPop(ctxt);
9696 }
9697 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009698
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009699 oldflags = ctxt->flags;
9700 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9701 ctxt->flags -= FLAGS_MIXED_CONTENT;
9702 }
9703 state = xmlRelaxNGNewValidState(ctxt, node);
9704 if (state == NULL) {
9705 ret = -1;
9706 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9707 xmlRelaxNGDumpValidError(ctxt);
9708 break;
9709 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009710
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009711 oldstate = ctxt->state;
9712 ctxt->state = state;
9713 if (define->attrs != NULL) {
9714 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9715 if (tmp != 0) {
9716 ret = -1;
9717 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9718 }
9719 }
9720 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009721 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9722 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9723 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009724
Daniel Veillard4c004142003-10-07 11:33:24 +00009725 nstate = xmlRelaxNGNewValidState(ctxt, node);
9726 ctxt->state = nstate;
9727 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009728
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009729 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9730 define->contModel,
9731 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009732 nseq = ctxt->state->seq;
9733 ctxt->state = tmpstate;
9734 ctxt->states = tmpstates;
9735 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009736
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009737#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009738 xmlGenericError(xmlGenericErrorContext,
9739 "Validating content of '%s' : %d\n",
9740 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009741#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009742 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009743 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009744
9745 if (ctxt->states != NULL) {
9746 tmp = -1;
9747
Daniel Veillardce192eb2003-04-16 15:58:05 +00009748 for (i = 0; i < ctxt->states->nbState; i++) {
9749 state = ctxt->states->tabState[i];
9750 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009751 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009752
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009753 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009754 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009755 break;
9756 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009757 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009758 if (tmp != 0) {
9759 /*
9760 * validation error, log the message for the "best" one
9761 */
9762 ctxt->flags |= FLAGS_IGNORABLE;
9763 xmlRelaxNGLogBestError(ctxt);
9764 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009765 for (i = 0; i < ctxt->states->nbState; i++) {
9766 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009767 ctxt->states->
9768 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009769 }
9770 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9771 ctxt->flags = oldflags;
9772 ctxt->states = NULL;
9773 if ((ret == 0) && (tmp == -1))
9774 ret = -1;
9775 } else {
9776 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009777 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009778 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009779 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009780 xmlRelaxNGFreeValidState(ctxt, state);
9781 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009782 } else {
9783 if (define->content != NULL) {
9784 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009785 define->
9786 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009787 if (tmp != 0) {
9788 ret = -1;
9789 if (ctxt->state == NULL) {
9790 ctxt->state = oldstate;
9791 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9792 node->name);
9793 ctxt->state = NULL;
9794 } else {
9795 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9796 node->name);
9797 }
9798
9799 }
9800 }
9801 if (ctxt->states != NULL) {
9802 tmp = -1;
9803
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009804 for (i = 0; i < ctxt->states->nbState; i++) {
9805 state = ctxt->states->tabState[i];
9806 ctxt->state = state;
9807
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009808 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009809 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009810 break;
9811 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009812 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009813 if (tmp != 0) {
9814 /*
9815 * validation error, log the message for the "best" one
9816 */
9817 ctxt->flags |= FLAGS_IGNORABLE;
9818 xmlRelaxNGLogBestError(ctxt);
9819 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009820 for (i = 0; i < ctxt->states->nbState; i++) {
9821 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009822 ctxt->states->
9823 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009824 }
9825 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9826 ctxt->flags = oldflags;
9827 ctxt->states = NULL;
9828 if ((ret == 0) && (tmp == -1))
9829 ret = -1;
9830 } else {
9831 state = ctxt->state;
9832 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009833 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009834 xmlRelaxNGFreeValidState(ctxt, state);
9835 }
9836 }
9837 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +00009838 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009839 }
9840 ctxt->flags = oldflags;
9841 ctxt->state = oldstate;
9842 if (oldstate != NULL)
9843 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9844 if (ret != 0) {
9845 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9846 xmlRelaxNGDumpValidError(ctxt);
9847 ret = 0;
9848 } else {
9849 ret = -2;
9850 }
9851 } else {
9852 if (ctxt->errNr > errNr)
9853 xmlRelaxNGPopErrors(ctxt, errNr);
9854 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009855
9856#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009857 xmlGenericError(xmlGenericErrorContext,
9858 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9859 node->name, ret);
9860 if (oldstate == NULL)
9861 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9862 else if (oldstate->seq == NULL)
9863 xmlGenericError(xmlGenericErrorContext, ": done\n");
9864 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9865 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9866 oldstate->seq->name);
9867 else
9868 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9869 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009870#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009871 break;
9872 case XML_RELAXNG_OPTIONAL:{
9873 errNr = ctxt->errNr;
9874 oldflags = ctxt->flags;
9875 ctxt->flags |= FLAGS_IGNORABLE;
9876 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9877 ret =
9878 xmlRelaxNGValidateDefinitionList(ctxt,
9879 define->content);
9880 if (ret != 0) {
9881 if (ctxt->state != NULL)
9882 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9883 ctxt->state = oldstate;
9884 ctxt->flags = oldflags;
9885 ret = 0;
9886 if (ctxt->errNr > errNr)
9887 xmlRelaxNGPopErrors(ctxt, errNr);
9888 break;
9889 }
9890 if (ctxt->states != NULL) {
9891 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9892 } else {
9893 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9894 if (ctxt->states == NULL) {
9895 xmlRelaxNGFreeValidState(ctxt, oldstate);
9896 ctxt->flags = oldflags;
9897 ret = -1;
9898 if (ctxt->errNr > errNr)
9899 xmlRelaxNGPopErrors(ctxt, errNr);
9900 break;
9901 }
9902 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9903 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9904 ctxt->state = NULL;
9905 }
9906 ctxt->flags = oldflags;
9907 ret = 0;
9908 if (ctxt->errNr > errNr)
9909 xmlRelaxNGPopErrors(ctxt, errNr);
9910 break;
9911 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009912 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009913 errNr = ctxt->errNr;
9914 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9915 if (ret != 0) {
9916 break;
9917 }
9918 if (ctxt->errNr > errNr)
9919 xmlRelaxNGPopErrors(ctxt, errNr);
9920 /* no break on purpose */
9921 case XML_RELAXNG_ZEROORMORE:{
9922 int progress;
9923 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9924 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009925
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009926 errNr = ctxt->errNr;
9927 res = xmlRelaxNGNewStates(ctxt, 1);
9928 if (res == NULL) {
9929 ret = -1;
9930 break;
9931 }
9932 /*
9933 * All the input states are also exit states
9934 */
9935 if (ctxt->state != NULL) {
9936 xmlRelaxNGAddStates(ctxt, res,
9937 xmlRelaxNGCopyValidState(ctxt,
9938 ctxt->
9939 state));
9940 } else {
9941 for (j = 0; j < ctxt->states->nbState; j++) {
9942 xmlRelaxNGAddStates(ctxt, res,
9943 xmlRelaxNGCopyValidState(ctxt,
9944 ctxt->
9945 states->
9946 tabState
9947 [j]));
9948 }
9949 }
9950 oldflags = ctxt->flags;
9951 ctxt->flags |= FLAGS_IGNORABLE;
9952 do {
9953 progress = 0;
9954 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009955
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009956 if (ctxt->states != NULL) {
9957 states = ctxt->states;
9958 for (i = 0; i < states->nbState; i++) {
9959 ctxt->state = states->tabState[i];
9960 ctxt->states = NULL;
9961 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9962 define->
9963 content);
9964 if (ret == 0) {
9965 if (ctxt->state != NULL) {
9966 tmp = xmlRelaxNGAddStates(ctxt, res,
9967 ctxt->state);
9968 ctxt->state = NULL;
9969 if (tmp == 1)
9970 progress = 1;
9971 } else if (ctxt->states != NULL) {
9972 for (j = 0; j < ctxt->states->nbState;
9973 j++) {
9974 tmp =
9975 xmlRelaxNGAddStates(ctxt, res,
9976 ctxt->
9977 states->
9978 tabState
9979 [j]);
9980 if (tmp == 1)
9981 progress = 1;
9982 }
9983 xmlRelaxNGFreeStates(ctxt,
9984 ctxt->states);
9985 ctxt->states = NULL;
9986 }
9987 } else {
9988 if (ctxt->state != NULL) {
9989 xmlRelaxNGFreeValidState(ctxt,
9990 ctxt->state);
9991 ctxt->state = NULL;
9992 }
9993 }
9994 }
9995 } else {
9996 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9997 define->
9998 content);
9999 if (ret != 0) {
10000 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10001 ctxt->state = NULL;
10002 } else {
10003 base = res->nbState;
10004 if (ctxt->state != NULL) {
10005 tmp = xmlRelaxNGAddStates(ctxt, res,
10006 ctxt->state);
10007 ctxt->state = NULL;
10008 if (tmp == 1)
10009 progress = 1;
10010 } else if (ctxt->states != NULL) {
10011 for (j = 0; j < ctxt->states->nbState; j++) {
10012 tmp = xmlRelaxNGAddStates(ctxt, res,
10013 ctxt->
10014 states->
10015 tabState[j]);
10016 if (tmp == 1)
10017 progress = 1;
10018 }
10019 if (states == NULL) {
10020 states = ctxt->states;
10021 } else {
10022 xmlRelaxNGFreeStates(ctxt,
10023 ctxt->states);
10024 }
10025 ctxt->states = NULL;
10026 }
10027 }
10028 }
10029 if (progress) {
10030 /*
10031 * Collect all the new nodes added at that step
10032 * and make them the new node set
10033 */
10034 if (res->nbState - base == 1) {
10035 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10036 res->
10037 tabState
10038 [base]);
10039 } else {
10040 if (states == NULL) {
10041 xmlRelaxNGNewStates(ctxt,
10042 res->nbState - base);
10043 }
10044 states->nbState = 0;
10045 for (i = base; i < res->nbState; i++)
10046 xmlRelaxNGAddStates(ctxt, states,
10047 xmlRelaxNGCopyValidState
10048 (ctxt,
10049 res->tabState[i]));
10050 ctxt->states = states;
10051 }
10052 }
10053 } while (progress == 1);
10054 if (states != NULL) {
10055 xmlRelaxNGFreeStates(ctxt, states);
10056 }
10057 ctxt->states = res;
10058 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010059#if 0
10060 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010061 * errors may have to be propagated back...
10062 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010063 if (ctxt->errNr > errNr)
10064 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010065#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010066 ret = 0;
10067 break;
10068 }
10069 case XML_RELAXNG_CHOICE:{
10070 xmlRelaxNGDefinePtr list = NULL;
10071 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010072
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010073 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010074
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010075 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010076 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10077 (node != NULL)) {
10078 /*
10079 * node == NULL can't be optimized since IS_TRIABLE
10080 * doesn't account for choice which may lead to
10081 * only attributes.
10082 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010083 xmlHashTablePtr triage =
10084 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010085
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010086 /*
10087 * Something we can optimize cleanly there is only one
10088 * possble branch out !
10089 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010090 if ((node->type == XML_TEXT_NODE) ||
10091 (node->type == XML_CDATA_SECTION_NODE)) {
10092 list =
10093 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10094 } else if (node->type == XML_ELEMENT_NODE) {
10095 if (node->ns != NULL) {
10096 list = xmlHashLookup2(triage, node->name,
10097 node->ns->href);
10098 if (list == NULL)
10099 list =
10100 xmlHashLookup2(triage, BAD_CAST "#any",
10101 node->ns->href);
10102 } else
10103 list =
10104 xmlHashLookup2(triage, node->name, NULL);
10105 if (list == NULL)
10106 list =
10107 xmlHashLookup2(triage, BAD_CAST "#any",
10108 NULL);
10109 }
10110 if (list == NULL) {
10111 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010112 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010113 break;
10114 }
10115 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10116 if (ret == 0) {
10117 }
10118 break;
10119 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010120
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010121 list = define->content;
10122 oldflags = ctxt->flags;
10123 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010124
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010125 while (list != NULL) {
10126 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10127 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10128 if (ret == 0) {
10129 if (states == NULL) {
10130 states = xmlRelaxNGNewStates(ctxt, 1);
10131 }
10132 if (ctxt->state != NULL) {
10133 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10134 } else if (ctxt->states != NULL) {
10135 for (i = 0; i < ctxt->states->nbState; i++) {
10136 xmlRelaxNGAddStates(ctxt, states,
10137 ctxt->states->
10138 tabState[i]);
10139 }
10140 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10141 ctxt->states = NULL;
10142 }
10143 } else {
10144 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10145 }
10146 ctxt->state = oldstate;
10147 list = list->next;
10148 }
10149 if (states != NULL) {
10150 xmlRelaxNGFreeValidState(ctxt, oldstate);
10151 ctxt->states = states;
10152 ctxt->state = NULL;
10153 ret = 0;
10154 } else {
10155 ctxt->states = NULL;
10156 }
10157 ctxt->flags = oldflags;
10158 if (ret != 0) {
10159 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10160 xmlRelaxNGDumpValidError(ctxt);
10161 }
10162 } else {
10163 if (ctxt->errNr > errNr)
10164 xmlRelaxNGPopErrors(ctxt, errNr);
10165 }
10166 break;
10167 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010168 case XML_RELAXNG_DEF:
10169 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010170 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10171 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010172 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010173 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10174 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010175 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010176 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10177 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010178 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010179 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010180 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010181 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010182 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010183 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10184 break;
10185 case XML_RELAXNG_DATATYPE:{
10186 xmlNodePtr child;
10187 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010188
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010189 child = node;
10190 while (child != NULL) {
10191 if (child->type == XML_ELEMENT_NODE) {
10192 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10193 node->parent->name);
10194 ret = -1;
10195 break;
10196 } else if ((child->type == XML_TEXT_NODE) ||
10197 (child->type == XML_CDATA_SECTION_NODE)) {
10198 content = xmlStrcat(content, child->content);
10199 }
10200 /* TODO: handle entities ... */
10201 child = child->next;
10202 }
10203 if (ret == -1) {
10204 if (content != NULL)
10205 xmlFree(content);
10206 break;
10207 }
10208 if (content == NULL) {
10209 content = xmlStrdup(BAD_CAST "");
10210 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010211 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010212 ret = -1;
10213 break;
10214 }
10215 }
10216 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10217 ctxt->state->seq);
10218 if (ret == -1) {
10219 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10220 } else if (ret == 0) {
10221 ctxt->state->seq = NULL;
10222 }
10223 if (content != NULL)
10224 xmlFree(content);
10225 break;
10226 }
10227 case XML_RELAXNG_VALUE:{
10228 xmlChar *content = NULL;
10229 xmlChar *oldvalue;
10230 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010231
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010232 child = node;
10233 while (child != NULL) {
10234 if (child->type == XML_ELEMENT_NODE) {
10235 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
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 oldvalue = ctxt->state->value;
10260 ctxt->state->value = content;
10261 ret = xmlRelaxNGValidateValue(ctxt, define);
10262 ctxt->state->value = oldvalue;
10263 if (ret == -1) {
10264 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10265 } else if (ret == 0) {
10266 ctxt->state->seq = NULL;
10267 }
10268 if (content != NULL)
10269 xmlFree(content);
10270 break;
10271 }
10272 case XML_RELAXNG_LIST:{
10273 xmlChar *content;
10274 xmlNodePtr child;
10275 xmlChar *oldvalue, *oldendvalue;
10276 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010277
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010278 /*
10279 * Make sure it's only text nodes
10280 */
10281
10282 content = NULL;
10283 child = node;
10284 while (child != NULL) {
10285 if (child->type == XML_ELEMENT_NODE) {
10286 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10287 node->parent->name);
10288 ret = -1;
10289 break;
10290 } else if ((child->type == XML_TEXT_NODE) ||
10291 (child->type == XML_CDATA_SECTION_NODE)) {
10292 content = xmlStrcat(content, child->content);
10293 }
10294 /* TODO: handle entities ... */
10295 child = child->next;
10296 }
10297 if (ret == -1) {
10298 if (content != NULL)
10299 xmlFree(content);
10300 break;
10301 }
10302 if (content == NULL) {
10303 content = xmlStrdup(BAD_CAST "");
10304 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010305 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010306 ret = -1;
10307 break;
10308 }
10309 }
10310 len = xmlStrlen(content);
10311 oldvalue = ctxt->state->value;
10312 oldendvalue = ctxt->state->endvalue;
10313 ctxt->state->value = content;
10314 ctxt->state->endvalue = content + len;
10315 ret = xmlRelaxNGValidateValue(ctxt, define);
10316 ctxt->state->value = oldvalue;
10317 ctxt->state->endvalue = oldendvalue;
10318 if (ret == -1) {
10319 VALID_ERR(XML_RELAXNG_ERR_LIST);
10320 } else if ((ret == 0) && (node != NULL)) {
10321 ctxt->state->seq = node->next;
10322 }
10323 if (content != NULL)
10324 xmlFree(content);
10325 break;
10326 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010327 case XML_RELAXNG_EXCEPT:
10328 case XML_RELAXNG_PARAM:
10329 TODO ret = -1;
10330 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010331 }
10332 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010333#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010334 for (i = 0; i < ctxt->depth; i++)
10335 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010336 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010337 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010338 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010339 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010340 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010341 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010342 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010343 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010344#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010345 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010346}
10347
10348/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010349 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010350 * @ctxt: a Relax-NG validation context
10351 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010352 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010353 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010354 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010355 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010356 */
10357static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010358xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10359 xmlRelaxNGDefinePtr define)
10360{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010361 xmlRelaxNGStatesPtr states, res;
10362 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010363
Daniel Veillardfd573f12003-03-16 17:52:32 +000010364 /*
10365 * We should NOT have both ctxt->state and ctxt->states
10366 */
10367 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010368 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10369 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010370 }
10371
10372 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010373 if (ctxt->states != NULL) {
10374 ctxt->state = ctxt->states->tabState[0];
10375 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10376 ctxt->states = NULL;
10377 }
10378 ret = xmlRelaxNGValidateState(ctxt, define);
10379 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10380 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10381 ctxt->state = NULL;
10382 }
10383 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10384 ctxt->state = ctxt->states->tabState[0];
10385 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10386 ctxt->states = NULL;
10387 }
10388 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010389 }
10390
10391 states = ctxt->states;
10392 ctxt->states = NULL;
10393 res = NULL;
10394 j = 0;
10395 oldflags = ctxt->flags;
10396 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010397 for (i = 0; i < states->nbState; i++) {
10398 ctxt->state = states->tabState[i];
10399 ctxt->states = NULL;
10400 ret = xmlRelaxNGValidateState(ctxt, define);
10401 /*
10402 * We should NOT have both ctxt->state and ctxt->states
10403 */
10404 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10405 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10406 ctxt->state = NULL;
10407 }
10408 if (ret == 0) {
10409 if (ctxt->states == NULL) {
10410 if (res != NULL) {
10411 /* add the state to the container */
10412 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10413 ctxt->state = NULL;
10414 } else {
10415 /* add the state directly in states */
10416 states->tabState[j++] = ctxt->state;
10417 ctxt->state = NULL;
10418 }
10419 } else {
10420 if (res == NULL) {
10421 /* make it the new container and copy other results */
10422 res = ctxt->states;
10423 ctxt->states = NULL;
10424 for (k = 0; k < j; k++)
10425 xmlRelaxNGAddStates(ctxt, res,
10426 states->tabState[k]);
10427 } else {
10428 /* add all the new results to res and reff the container */
10429 for (k = 0; k < ctxt->states->nbState; k++)
10430 xmlRelaxNGAddStates(ctxt, res,
10431 ctxt->states->tabState[k]);
10432 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10433 ctxt->states = NULL;
10434 }
10435 }
10436 } else {
10437 if (ctxt->state != NULL) {
10438 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10439 ctxt->state = NULL;
10440 } else if (ctxt->states != NULL) {
10441 for (k = 0; k < ctxt->states->nbState; k++)
10442 xmlRelaxNGFreeValidState(ctxt,
10443 ctxt->states->tabState[k]);
10444 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10445 ctxt->states = NULL;
10446 }
10447 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010448 }
10449 ctxt->flags = oldflags;
10450 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010451 xmlRelaxNGFreeStates(ctxt, states);
10452 ctxt->states = res;
10453 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010454 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010455 states->nbState = j;
10456 ctxt->states = states;
10457 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010458 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010459 ctxt->state = states->tabState[0];
10460 xmlRelaxNGFreeStates(ctxt, states);
10461 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010462 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010463 ret = -1;
10464 xmlRelaxNGFreeStates(ctxt, states);
10465 if (ctxt->states != NULL) {
10466 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10467 ctxt->states = NULL;
10468 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010469 }
10470 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010471 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10472 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010473 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010474 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010475}
10476
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010477/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010478 * xmlRelaxNGValidateDocument:
10479 * @ctxt: a Relax-NG validation context
10480 * @doc: the document
10481 *
10482 * Validate the given document
10483 *
10484 * Returns 0 if the validation succeeded or an error code.
10485 */
10486static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010487xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10488{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010489 int ret;
10490 xmlRelaxNGPtr schema;
10491 xmlRelaxNGGrammarPtr grammar;
10492 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010493 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010494
10495 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010496 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010497
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010498 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010499 schema = ctxt->schema;
10500 grammar = schema->topgrammar;
10501 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010502 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10503 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010504 }
10505 state = xmlRelaxNGNewValidState(ctxt, NULL);
10506 ctxt->state = state;
10507 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010508 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010509 state = ctxt->state;
10510 node = state->seq;
10511 node = xmlRelaxNGSkipIgnored(ctxt, node);
10512 if (node != NULL) {
10513 if (ret != -1) {
10514 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10515 ret = -1;
10516 }
10517 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010518 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010519 int i;
10520 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010521
Daniel Veillard4c004142003-10-07 11:33:24 +000010522 for (i = 0; i < ctxt->states->nbState; i++) {
10523 state = ctxt->states->tabState[i];
10524 node = state->seq;
10525 node = xmlRelaxNGSkipIgnored(ctxt, node);
10526 if (node == NULL)
10527 tmp = 0;
10528 xmlRelaxNGFreeValidState(ctxt, state);
10529 }
10530 if (tmp == -1) {
10531 if (ret != -1) {
10532 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10533 ret = -1;
10534 }
10535 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010536 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010537 if (ctxt->state != NULL) {
10538 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010539 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010540 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010541 if (ret != 0)
10542 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010543#ifdef DEBUG
10544 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010545 ctxt->error(ctxt->userData,
10546 "%d Extra error messages left on stack !\n",
10547 ctxt->errNr);
10548 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010549 }
10550#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010551#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010552 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010553 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010554
Daniel Veillard4c004142003-10-07 11:33:24 +000010555 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10556 vctxt.valid = 1;
10557 vctxt.error = ctxt->error;
10558 vctxt.warning = ctxt->warning;
10559 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010560
Daniel Veillard4c004142003-10-07 11:33:24 +000010561 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10562 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010563 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010564#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010565 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010566 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010567
Daniel Veillard4c004142003-10-07 11:33:24 +000010568 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010569}
10570
Daniel Veillardfd573f12003-03-16 17:52:32 +000010571/************************************************************************
10572 * *
10573 * Validation interfaces *
10574 * *
10575 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010576
Daniel Veillard6eadf632003-01-23 18:29:16 +000010577/**
10578 * xmlRelaxNGNewValidCtxt:
10579 * @schema: a precompiled XML RelaxNGs
10580 *
10581 * Create an XML RelaxNGs validation context based on the given schema
10582 *
10583 * Returns the validation context or NULL in case of error
10584 */
10585xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010586xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10587{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010588 xmlRelaxNGValidCtxtPtr ret;
10589
10590 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10591 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010592 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010593 return (NULL);
10594 }
10595 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10596 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010597 ret->error = xmlGenericError;
10598 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010599 ret->errNr = 0;
10600 ret->errMax = 0;
10601 ret->err = NULL;
10602 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010603 if (schema != NULL)
10604 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010605 ret->states = NULL;
10606 ret->freeState = NULL;
10607 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010608 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010609 return (ret);
10610}
10611
10612/**
10613 * xmlRelaxNGFreeValidCtxt:
10614 * @ctxt: the schema validation context
10615 *
10616 * Free the resources associated to the schema validation context
10617 */
10618void
Daniel Veillard4c004142003-10-07 11:33:24 +000010619xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10620{
Daniel Veillard798024a2003-03-19 10:36:09 +000010621 int k;
10622
Daniel Veillard6eadf632003-01-23 18:29:16 +000010623 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010624 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010625 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010626 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010627 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010628 for (k = 0; k < ctxt->freeState->nbState; k++) {
10629 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10630 }
10631 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010632 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010633 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010634 for (k = 0; k < ctxt->freeStatesNr; k++) {
10635 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10636 }
10637 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010638 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010639 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010640 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010641 if (ctxt->elemTab != NULL) {
10642 xmlRegExecCtxtPtr exec;
10643
Daniel Veillard4c004142003-10-07 11:33:24 +000010644 exec = xmlRelaxNGElemPop(ctxt);
10645 while (exec != NULL) {
10646 xmlRegFreeExecCtxt(exec);
10647 exec = xmlRelaxNGElemPop(ctxt);
10648 }
10649 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010650 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010651 xmlFree(ctxt);
10652}
10653
10654/**
10655 * xmlRelaxNGSetValidErrors:
10656 * @ctxt: a Relax-NG validation context
10657 * @err: the error function
10658 * @warn: the warning function
10659 * @ctx: the functions context
10660 *
10661 * Set the error and warning callback informations
10662 */
10663void
10664xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010665 xmlRelaxNGValidityErrorFunc err,
10666 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10667{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010668 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010669 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010670 ctxt->error = err;
10671 ctxt->warning = warn;
10672 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010673 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010674}
10675
10676/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010677 * xmlRelaxNGSetValidStructuredErrors:
10678 * @ctxt: a Relax-NG validation context
10679 * @serror: the structured error function
10680 * @ctx: the functions context
10681 *
10682 * Set the structured error callback
10683 */
10684void
10685xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010686 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010687{
10688 if (ctxt == NULL)
10689 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010690 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010691 ctxt->error = NULL;
10692 ctxt->warning = NULL;
10693 ctxt->userData = ctx;
10694}
10695
10696/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010697 * xmlRelaxNGGetValidErrors:
10698 * @ctxt: a Relax-NG validation context
10699 * @err: the error function result
10700 * @warn: the warning function result
10701 * @ctx: the functions context result
10702 *
10703 * Get the error and warning callback informations
10704 *
10705 * Returns -1 in case of error and 0 otherwise
10706 */
10707int
10708xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010709 xmlRelaxNGValidityErrorFunc * err,
10710 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10711{
Daniel Veillard409a8142003-07-18 15:16:57 +000010712 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010713 return (-1);
10714 if (err != NULL)
10715 *err = ctxt->error;
10716 if (warn != NULL)
10717 *warn = ctxt->warning;
10718 if (ctx != NULL)
10719 *ctx = ctxt->userData;
10720 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010721}
10722
10723/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010724 * xmlRelaxNGValidateDoc:
10725 * @ctxt: a Relax-NG validation context
10726 * @doc: a parsed document tree
10727 *
10728 * Validate a document tree in memory.
10729 *
10730 * Returns 0 if the document is valid, a positive error code
10731 * number otherwise and -1 in case of internal or API error.
10732 */
10733int
Daniel Veillard4c004142003-10-07 11:33:24 +000010734xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10735{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010736 int ret;
10737
10738 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010739 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010740
10741 ctxt->doc = doc;
10742
10743 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010744 /*
10745 * TODO: build error codes
10746 */
10747 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010748 return (1);
10749 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010750}
10751
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010752#define bottom_relaxng
10753#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000010754#endif /* LIBXML_SCHEMAS_ENABLED */