blob: c6e577d896c13c3a08635d08a7e5309dadb74667 [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 Veillard42f12e92003-03-07 18:32:59 +0000205 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000206
Daniel Veillard4c004142003-10-07 11:33:24 +0000207 xmlRelaxNGPtr schema; /* The schema in use */
208 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
209 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
210 int flags; /* parser flags */
211 int nbErrors; /* number of errors at parse time */
212 int nbWarnings; /* number of warnings at parse time */
213 const xmlChar *define; /* the current define scope */
214 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000215
Daniel Veillard4c004142003-10-07 11:33:24 +0000216 int nbInterleaves;
217 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000218
Daniel Veillard4c004142003-10-07 11:33:24 +0000219 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
220 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
221 xmlChar *URL;
222 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000223
Daniel Veillard4c004142003-10-07 11:33:24 +0000224 int defNr; /* number of defines used */
225 int defMax; /* number of defines aloocated */
226 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000227
Daniel Veillard4c004142003-10-07 11:33:24 +0000228 const char *buffer;
229 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000230
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000231 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000232 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
233 int docNr; /* Depth of the parsing stack */
234 int docMax; /* Max depth of the parsing stack */
235 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000236
237 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000238 xmlRelaxNGIncludePtr inc; /* Current parsed include */
239 int incNr; /* Depth of the include parsing stack */
240 int incMax; /* Max depth of the parsing stack */
241 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000242
Daniel Veillard4c004142003-10-07 11:33:24 +0000243 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000244
245 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000246 xmlAutomataPtr am; /* the automata */
247 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000248};
249
250#define FLAGS_IGNORABLE 1
251#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000252#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000253
254/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000255 * xmlRelaxNGInterleaveGroup:
256 *
257 * A RelaxNGs partition set associated to lists of definitions
258 */
259typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
260typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
261struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000262 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
263 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
264 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000265};
266
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000267#define IS_DETERMINIST 1
268#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000269
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000270/**
271 * xmlRelaxNGPartitions:
272 *
273 * A RelaxNGs partition associated to an interleave group
274 */
275typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
276typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
277struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000278 int nbgroups; /* number of groups in the partitions */
279 xmlHashTablePtr triage; /* hash table used to direct nodes to the
280 * right group when possible */
281 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000282 xmlRelaxNGInterleaveGroupPtr *groups;
283};
284
285/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000286 * xmlRelaxNGValidState:
287 *
288 * A RelaxNGs validation state
289 */
290#define MAX_ATTR 20
291typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
292typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
293struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000294 xmlNodePtr node; /* the current node */
295 xmlNodePtr seq; /* the sequence of children left to validate */
296 int nbAttrs; /* the number of attributes */
297 int maxAttrs; /* the size of attrs */
298 int nbAttrLeft; /* the number of attributes left to validate */
299 xmlChar *value; /* the value when operating on string */
300 xmlChar *endvalue; /* the end value when operating on string */
301 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000302};
303
304/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000305 * xmlRelaxNGStates:
306 *
307 * A RelaxNGs container for validation state
308 */
309typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
310typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
311struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000312 int nbState; /* the number of states */
313 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000314 xmlRelaxNGValidStatePtr *tabState;
315};
316
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000317#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000318
Daniel Veillardfd573f12003-03-16 17:52:32 +0000319/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000320 * xmlRelaxNGValidError:
321 *
322 * A RelaxNGs validation error
323 */
324typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
325typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
326struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000327 xmlRelaxNGValidErr err; /* the error number */
328 int flags; /* flags */
329 xmlNodePtr node; /* the current node */
330 xmlNodePtr seq; /* the current child */
331 const xmlChar *arg1; /* first arg */
332 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000333};
334
335/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000336 * xmlRelaxNGValidCtxt:
337 *
338 * A RelaxNGs validation context
339 */
340
341struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000342 void *userData; /* user specific data block */
343 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
344 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
345 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000346
Daniel Veillard4c004142003-10-07 11:33:24 +0000347 xmlRelaxNGPtr schema; /* The schema in use */
348 xmlDocPtr doc; /* the document being validated */
349 int flags; /* validation flags */
350 int depth; /* validation depth */
351 int idref; /* requires idref checking */
352 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000353
354 /*
355 * Errors accumulated in branches may have to be stacked to be
356 * provided back when it's sure they affect validation.
357 */
358 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000359 int errNr; /* Depth of the error stack */
360 int errMax; /* Max depth of the error stack */
361 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000362
Daniel Veillard4c004142003-10-07 11:33:24 +0000363 xmlRelaxNGValidStatePtr state; /* the current validation state */
364 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000365
Daniel Veillard4c004142003-10-07 11:33:24 +0000366 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
367 int freeStatesNr;
368 int freeStatesMax;
369 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000370
371 /*
372 * This is used for "progressive" validation
373 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000374 xmlRegExecCtxtPtr elem; /* the current element regexp */
375 int elemNr; /* the number of element validated */
376 int elemMax; /* the max depth of elements */
377 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
378 int pstate; /* progressive state */
379 xmlNodePtr pnode; /* the current node */
380 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
381 int perr; /* signal error in content model
382 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000383};
384
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000385/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000386 * xmlRelaxNGInclude:
387 *
388 * Structure associated to a RelaxNGs document element
389 */
390struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000391 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
392 xmlChar *href; /* the normalized href value */
393 xmlDocPtr doc; /* the associated XML document */
394 xmlRelaxNGDefinePtr content; /* the definitions */
395 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000396};
397
398/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000399 * xmlRelaxNGDocument:
400 *
401 * Structure associated to a RelaxNGs document element
402 */
403struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000404 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000405 xmlChar *href; /* the normalized href value */
406 xmlDocPtr doc; /* the associated XML document */
407 xmlRelaxNGDefinePtr content; /* the definitions */
408 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000409};
410
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000411
Daniel Veillard6eadf632003-01-23 18:29:16 +0000412/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000413 * *
414 * Some factorized error routines *
415 * *
416 ************************************************************************/
417
418/**
419 * xmlRngPErrMemory:
420 * @ctxt: an Relax-NG parser context
421 * @extra: extra informations
422 *
423 * Handle a redefinition of attribute error
424 */
425static void
426xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
427{
428 xmlGenericErrorFunc channel = NULL;
429 void *data = NULL;
430
431 if (ctxt != NULL) {
432 channel = ctxt->error;
433 data = ctxt->userData;
434 ctxt->nbErrors++;
435 }
436 if (extra)
437 __xmlRaiseError(channel, data,
438 NULL, NULL, XML_FROM_RELAXNGP,
439 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
440 NULL, NULL, 0, 0,
441 "Memory allocation failed : %s\n", extra);
442 else
443 __xmlRaiseError(channel, data,
444 NULL, NULL, XML_FROM_RELAXNGP,
445 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
446 NULL, NULL, 0, 0, "Memory allocation failed\n");
447}
448
449/**
450 * xmlRngVErrMemory:
451 * @ctxt: a Relax-NG validation context
452 * @extra: extra informations
453 *
454 * Handle a redefinition of attribute error
455 */
456static void
457xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
458{
459 xmlGenericErrorFunc channel = NULL;
460 void *data = NULL;
461
462 if (ctxt != NULL) {
463 channel = ctxt->error;
464 data = ctxt->userData;
465 ctxt->nbErrors++;
466 }
467 if (extra)
468 __xmlRaiseError(channel, data,
469 NULL, NULL, XML_FROM_RELAXNGV,
470 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
471 NULL, NULL, 0, 0,
472 "Memory allocation failed : %s\n", extra);
473 else
474 __xmlRaiseError(channel, data,
475 NULL, NULL, XML_FROM_RELAXNGV,
476 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
477 NULL, NULL, 0, 0, "Memory allocation failed\n");
478}
479
480/**
481 * xmlRngPErr:
482 * @ctxt: a Relax-NG parser context
483 * @node: the node raising the error
484 * @error: the error code
485 * @msg: message
486 * @str1: extra info
487 * @str2: extra info
488 *
489 * Handle a Relax NG Parsing error
490 */
491static void
492xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
493 const char *msg, const xmlChar * str1, const xmlChar * str2)
494{
495 xmlGenericErrorFunc channel = NULL;
496 void *data = NULL;
497
498 if (ctxt != NULL) {
499 channel = ctxt->error;
500 data = ctxt->userData;
501 ctxt->nbErrors++;
502 }
503 __xmlRaiseError(channel, data,
504 NULL, node, XML_FROM_RELAXNGP,
505 error, XML_ERR_ERROR, NULL, 0,
506 (const char *) str1, (const char *) str2, NULL, 0, 0,
507 msg, str1, str2);
508}
509
510/**
511 * xmlRngVErr:
512 * @ctxt: a Relax-NG validation context
513 * @node: the node raising the error
514 * @error: the error code
515 * @msg: message
516 * @str1: extra info
517 * @str2: extra info
518 *
519 * Handle a Relax NG Validation error
520 */
521static void
522xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
523 const char *msg, const xmlChar * str1, const xmlChar * str2)
524{
525 xmlGenericErrorFunc channel = NULL;
526 void *data = NULL;
527
528 if (ctxt != NULL) {
529 channel = ctxt->error;
530 data = ctxt->userData;
531 ctxt->nbErrors++;
532 }
533 __xmlRaiseError(channel, data,
534 NULL, node, XML_FROM_RELAXNGV,
535 error, XML_ERR_ERROR, NULL, 0,
536 (const char *) str1, (const char *) str2, NULL, 0, 0,
537 msg, str1, str2);
538}
539
540/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000541 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000542 * Preliminary type checking interfaces *
543 * *
544 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000545
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000546/**
547 * xmlRelaxNGTypeHave:
548 * @data: data needed for the library
549 * @type: the type name
550 * @value: the value to check
551 *
552 * Function provided by a type library to check if a type is exported
553 *
554 * Returns 1 if yes, 0 if no and -1 in case of error.
555 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000556typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000557
558/**
559 * xmlRelaxNGTypeCheck:
560 * @data: data needed for the library
561 * @type: the type name
562 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000563 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000564 *
565 * Function provided by a type library to check if a value match a type
566 *
567 * Returns 1 if yes, 0 if no and -1 in case of error.
568 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000569typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
570 const xmlChar * value, void **result,
571 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000572
573/**
574 * xmlRelaxNGFacetCheck:
575 * @data: data needed for the library
576 * @type: the type name
577 * @facet: the facet name
578 * @val: the facet value
579 * @strval: the string value
580 * @value: the value to check
581 *
582 * Function provided by a type library to check a value facet
583 *
584 * Returns 1 if yes, 0 if no and -1 in case of error.
585 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000586typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
587 const xmlChar * facet,
588 const xmlChar * val,
589 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000590
591/**
592 * xmlRelaxNGTypeFree:
593 * @data: data needed for the library
594 * @result: the value to free
595 *
596 * Function provided by a type library to free a returned result
597 */
598typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000599
600/**
601 * xmlRelaxNGTypeCompare:
602 * @data: data needed for the library
603 * @type: the type name
604 * @value1: the first value
605 * @value2: the second value
606 *
607 * Function provided by a type library to compare two values accordingly
608 * to a type.
609 *
610 * Returns 1 if yes, 0 if no and -1 in case of error.
611 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000612typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
613 const xmlChar * value1,
614 xmlNodePtr ctxt1,
615 void *comp1,
616 const xmlChar * value2,
617 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000618typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
619typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
620struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000621 const xmlChar *namespace; /* the datatypeLibrary value */
622 void *data; /* data needed for the library */
623 xmlRelaxNGTypeHave have; /* the export function */
624 xmlRelaxNGTypeCheck check; /* the checking function */
625 xmlRelaxNGTypeCompare comp; /* the compare function */
626 xmlRelaxNGFacetCheck facet; /* the facet check function */
627 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000628};
629
630/************************************************************************
631 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000632 * Allocation functions *
633 * *
634 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000635static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
636static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000637static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000638static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000639static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
640 ATTRIBUTE_UNUSED,
641 xmlRelaxNGValidStatePtr state1,
642 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000643static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000644 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000645
646/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000647 * xmlRelaxNGFreeDocument:
648 * @docu: a document structure
649 *
650 * Deallocate a RelaxNG document structure.
651 */
652static void
653xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
654{
655 if (docu == NULL)
656 return;
657
658 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000659 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000660 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000661 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000662 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000663 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000664 xmlFree(docu);
665}
666
667/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000668 * xmlRelaxNGFreeDocumentList:
669 * @docu: a list of document structure
670 *
671 * Deallocate a RelaxNG document structures.
672 */
673static void
674xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
675{
676 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000677
Daniel Veillardc482e262003-02-26 14:48:48 +0000678 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000679 next = docu->next;
680 xmlRelaxNGFreeDocument(docu);
681 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000682 }
683}
684
685/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000686 * xmlRelaxNGFreeInclude:
687 * @incl: a include structure
688 *
689 * Deallocate a RelaxNG include structure.
690 */
691static void
692xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
693{
694 if (incl == NULL)
695 return;
696
697 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000698 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000699 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000700 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000701 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000702 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000703 xmlFree(incl);
704}
705
706/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000707 * xmlRelaxNGFreeIncludeList:
708 * @incl: a include structure list
709 *
710 * Deallocate a RelaxNG include structure.
711 */
712static void
713xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
714{
715 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000716
Daniel Veillardc482e262003-02-26 14:48:48 +0000717 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000718 next = incl->next;
719 xmlRelaxNGFreeInclude(incl);
720 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000721 }
722}
723
724/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000725 * xmlRelaxNGNewRelaxNG:
726 * @ctxt: a Relax-NG validation context (optional)
727 *
728 * Allocate a new RelaxNG structure.
729 *
730 * Returns the newly allocated structure or NULL in case or error
731 */
732static xmlRelaxNGPtr
733xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
734{
735 xmlRelaxNGPtr ret;
736
737 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
738 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000739 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000740 return (NULL);
741 }
742 memset(ret, 0, sizeof(xmlRelaxNG));
743
744 return (ret);
745}
746
747/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000748 * xmlRelaxNGFreeInnerSchema:
749 * @schema: a schema structure
750 *
751 * Deallocate a RelaxNG schema structure.
752 */
753static void
754xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
755{
756 if (schema == NULL)
757 return;
758
759 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000760 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000761 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000762 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000763
Daniel Veillard4c004142003-10-07 11:33:24 +0000764 for (i = 0; i < schema->defNr; i++)
765 xmlRelaxNGFreeDefine(schema->defTab[i]);
766 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000767 }
768
769 xmlFree(schema);
770}
771
772/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000773 * xmlRelaxNGFree:
774 * @schema: a schema structure
775 *
776 * Deallocate a RelaxNG structure.
777 */
778void
779xmlRelaxNGFree(xmlRelaxNGPtr schema)
780{
781 if (schema == NULL)
782 return;
783
Daniel Veillard6eadf632003-01-23 18:29:16 +0000784 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000785 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000786 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000787 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000788 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000789 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000790 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000791 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000792 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000793 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000794
Daniel Veillard4c004142003-10-07 11:33:24 +0000795 for (i = 0; i < schema->defNr; i++)
796 xmlRelaxNGFreeDefine(schema->defTab[i]);
797 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000798 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000799
800 xmlFree(schema);
801}
802
803/**
804 * xmlRelaxNGNewGrammar:
805 * @ctxt: a Relax-NG validation context (optional)
806 *
807 * Allocate a new RelaxNG grammar.
808 *
809 * Returns the newly allocated structure or NULL in case or error
810 */
811static xmlRelaxNGGrammarPtr
812xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
813{
814 xmlRelaxNGGrammarPtr ret;
815
816 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
817 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000818 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000819 return (NULL);
820 }
821 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
822
823 return (ret);
824}
825
826/**
827 * xmlRelaxNGFreeGrammar:
828 * @grammar: a grammar structure
829 *
830 * Deallocate a RelaxNG grammar structure.
831 */
832static void
833xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
834{
835 if (grammar == NULL)
836 return;
837
Daniel Veillardc482e262003-02-26 14:48:48 +0000838 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000839 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000840 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000841 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000842 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000843 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000844 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000845 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000846 }
847 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000848 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000849 }
850
851 xmlFree(grammar);
852}
853
854/**
855 * xmlRelaxNGNewDefine:
856 * @ctxt: a Relax-NG validation context
857 * @node: the node in the input document.
858 *
859 * Allocate a new RelaxNG define.
860 *
861 * Returns the newly allocated structure or NULL in case or error
862 */
863static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000864xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000865{
866 xmlRelaxNGDefinePtr ret;
867
Daniel Veillard419a7682003-02-03 23:22:49 +0000868 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000869 ctxt->defMax = 16;
870 ctxt->defNr = 0;
871 ctxt->defTab = (xmlRelaxNGDefinePtr *)
872 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
873 if (ctxt->defTab == NULL) {
874 xmlRngPErrMemory(ctxt, "allocating define\n");
875 return (NULL);
876 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000877 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000878 xmlRelaxNGDefinePtr *tmp;
879
880 ctxt->defMax *= 2;
881 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
882 ctxt->defMax *
883 sizeof
884 (xmlRelaxNGDefinePtr));
885 if (tmp == NULL) {
886 xmlRngPErrMemory(ctxt, "allocating define\n");
887 return (NULL);
888 }
889 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000890 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000891 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
892 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000893 xmlRngPErrMemory(ctxt, "allocating define\n");
894 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000895 }
896 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000897 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000898 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000899 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000900 return (ret);
901}
902
903/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000904 * xmlRelaxNGFreePartition:
905 * @partitions: a partition set structure
906 *
907 * Deallocate RelaxNG partition set structures.
908 */
909static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000910xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
911{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000912 xmlRelaxNGInterleaveGroupPtr group;
913 int j;
914
915 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000916 if (partitions->groups != NULL) {
917 for (j = 0; j < partitions->nbgroups; j++) {
918 group = partitions->groups[j];
919 if (group != NULL) {
920 if (group->defs != NULL)
921 xmlFree(group->defs);
922 if (group->attrs != NULL)
923 xmlFree(group->attrs);
924 xmlFree(group);
925 }
926 }
927 xmlFree(partitions->groups);
928 }
929 if (partitions->triage != NULL) {
930 xmlHashFree(partitions->triage, NULL);
931 }
932 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000933 }
934}
Daniel Veillard4c004142003-10-07 11:33:24 +0000935
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000936/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000937 * xmlRelaxNGFreeDefine:
938 * @define: a define structure
939 *
940 * Deallocate a RelaxNG define structure.
941 */
942static void
943xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
944{
945 if (define == NULL)
946 return;
947
Daniel Veillard4c004142003-10-07 11:33:24 +0000948 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
949 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000950
Daniel Veillard4c004142003-10-07 11:33:24 +0000951 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
952 if ((lib != NULL) && (lib->freef != NULL))
953 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000954 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000955 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
956 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
957 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
958 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000959 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000960 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000961 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000962 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000963 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000964 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000965 if (define->contModel != NULL)
966 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000967 xmlFree(define);
968}
969
970/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000971 * xmlRelaxNGNewStates:
972 * @ctxt: a Relax-NG validation context
973 * @size: the default size for the container
974 *
975 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000976 *
977 * Returns the newly allocated structure or NULL in case or error
978 */
979static xmlRelaxNGStatesPtr
980xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
981{
982 xmlRelaxNGStatesPtr ret;
983
Daniel Veillard798024a2003-03-19 10:36:09 +0000984 if ((ctxt != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +0000985 (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) {
986 ctxt->freeStatesNr--;
987 ret = ctxt->freeStates[ctxt->freeStatesNr];
988 ret->nbState = 0;
989 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +0000990 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000991 if (size < 16)
992 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +0000993
994 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +0000995 (size -
996 1) *
997 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +0000998 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000999 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001000 return (NULL);
1001 }
1002 ret->nbState = 0;
1003 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001004 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1005 sizeof
1006 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001007 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001008 xmlRngVErrMemory(ctxt, "allocating states\n");
1009 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001010 return (NULL);
1011 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001012 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001013}
1014
1015/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001016 * xmlRelaxNGAddStateUniq:
1017 * @ctxt: a Relax-NG validation context
1018 * @states: the states container
1019 * @state: the validation state
1020 *
1021 * Add a RelaxNG validation state to the container without checking
1022 * for unicity.
1023 *
1024 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1025 */
1026static int
1027xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001028 xmlRelaxNGStatesPtr states,
1029 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001030{
1031 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001032 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001033 }
1034 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001035 xmlRelaxNGValidStatePtr *tmp;
1036 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001037
Daniel Veillard4c004142003-10-07 11:33:24 +00001038 size = states->maxState * 2;
1039 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1040 (size) *
1041 sizeof
1042 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001043 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001044 xmlRngVErrMemory(ctxt, "adding states\n");
1045 return (-1);
1046 }
1047 states->tabState = tmp;
1048 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001049 }
1050 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001051 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001052}
1053
1054/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001055 * xmlRelaxNGAddState:
1056 * @ctxt: a Relax-NG validation context
1057 * @states: the states container
1058 * @state: the validation state
1059 *
1060 * Add a RelaxNG validation state to the container
1061 *
1062 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1063 */
1064static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001065xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1066 xmlRelaxNGStatesPtr states,
1067 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001068{
1069 int i;
1070
1071 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001072 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001073 }
1074 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001075 xmlRelaxNGValidStatePtr *tmp;
1076 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001077
Daniel Veillard4c004142003-10-07 11:33:24 +00001078 size = states->maxState * 2;
1079 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1080 (size) *
1081 sizeof
1082 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001083 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001084 xmlRngVErrMemory(ctxt, "adding states\n");
1085 return (-1);
1086 }
1087 states->tabState = tmp;
1088 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001089 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001090 for (i = 0; i < states->nbState; i++) {
1091 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1092 xmlRelaxNGFreeValidState(ctxt, state);
1093 return (0);
1094 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001095 }
1096 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001097 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001098}
1099
1100/**
1101 * xmlRelaxNGFreeStates:
1102 * @ctxt: a Relax-NG validation context
1103 * @states: teh container
1104 *
1105 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001106 */
1107static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001108xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001109 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001110{
Daniel Veillard798024a2003-03-19 10:36:09 +00001111 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001112 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001113 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001114 ctxt->freeStatesMax = 40;
1115 ctxt->freeStatesNr = 0;
1116 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1117 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1118 if (ctxt->freeStates == NULL) {
1119 xmlRngVErrMemory(ctxt, "storing states\n");
1120 }
1121 } else if ((ctxt != NULL)
1122 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1123 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001124
Daniel Veillard4c004142003-10-07 11:33:24 +00001125 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1126 2 * ctxt->freeStatesMax *
1127 sizeof
1128 (xmlRelaxNGStatesPtr));
1129 if (tmp == NULL) {
1130 xmlRngVErrMemory(ctxt, "storing states\n");
1131 xmlFree(states->tabState);
1132 xmlFree(states);
1133 return;
1134 }
1135 ctxt->freeStates = tmp;
1136 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001137 }
1138 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001139 xmlFree(states->tabState);
1140 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001141 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001142 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001143 }
1144}
1145
1146/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001147 * xmlRelaxNGNewValidState:
1148 * @ctxt: a Relax-NG validation context
1149 * @node: the current node or NULL for the document
1150 *
1151 * Allocate a new RelaxNG validation state
1152 *
1153 * Returns the newly allocated structure or NULL in case or error
1154 */
1155static xmlRelaxNGValidStatePtr
1156xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1157{
1158 xmlRelaxNGValidStatePtr ret;
1159 xmlAttrPtr attr;
1160 xmlAttrPtr attrs[MAX_ATTR];
1161 int nbAttrs = 0;
1162 xmlNodePtr root = NULL;
1163
1164 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001165 root = xmlDocGetRootElement(ctxt->doc);
1166 if (root == NULL)
1167 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001168 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001169 attr = node->properties;
1170 while (attr != NULL) {
1171 if (nbAttrs < MAX_ATTR)
1172 attrs[nbAttrs++] = attr;
1173 else
1174 nbAttrs++;
1175 attr = attr->next;
1176 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001177 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001178 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1179 ctxt->freeState->nbState--;
1180 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001181 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001182 ret =
1183 (xmlRelaxNGValidStatePtr)
1184 xmlMalloc(sizeof(xmlRelaxNGValidState));
1185 if (ret == NULL) {
1186 xmlRngVErrMemory(ctxt, "allocating states\n");
1187 return (NULL);
1188 }
1189 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001190 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001191 ret->value = NULL;
1192 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001193 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001194 ret->node = (xmlNodePtr) ctxt->doc;
1195 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001196 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001197 ret->node = node;
1198 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001199 }
1200 ret->nbAttrs = 0;
1201 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001202 if (ret->attrs == NULL) {
1203 if (nbAttrs < 4)
1204 ret->maxAttrs = 4;
1205 else
1206 ret->maxAttrs = nbAttrs;
1207 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1208 sizeof(xmlAttrPtr));
1209 if (ret->attrs == NULL) {
1210 xmlRngVErrMemory(ctxt, "allocating states\n");
1211 return (ret);
1212 }
1213 } else if (ret->maxAttrs < nbAttrs) {
1214 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001215
Daniel Veillard4c004142003-10-07 11:33:24 +00001216 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1217 sizeof(xmlAttrPtr));
1218 if (tmp == NULL) {
1219 xmlRngVErrMemory(ctxt, "allocating states\n");
1220 return (ret);
1221 }
1222 ret->attrs = tmp;
1223 ret->maxAttrs = nbAttrs;
1224 }
1225 ret->nbAttrs = nbAttrs;
1226 if (nbAttrs < MAX_ATTR) {
1227 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1228 } else {
1229 attr = node->properties;
1230 nbAttrs = 0;
1231 while (attr != NULL) {
1232 ret->attrs[nbAttrs++] = attr;
1233 attr = attr->next;
1234 }
1235 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001236 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001237 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001238 return (ret);
1239}
1240
1241/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001242 * xmlRelaxNGCopyValidState:
1243 * @ctxt: a Relax-NG validation context
1244 * @state: a validation state
1245 *
1246 * Copy the validation state
1247 *
1248 * Returns the newly allocated structure or NULL in case or error
1249 */
1250static xmlRelaxNGValidStatePtr
1251xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001252 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001253{
1254 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001255 unsigned int maxAttrs;
1256 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001257
1258 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001259 return (NULL);
1260 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1261 ctxt->freeState->nbState--;
1262 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001263 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001264 ret =
1265 (xmlRelaxNGValidStatePtr)
1266 xmlMalloc(sizeof(xmlRelaxNGValidState));
1267 if (ret == NULL) {
1268 xmlRngVErrMemory(ctxt, "allocating states\n");
1269 return (NULL);
1270 }
1271 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001272 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001273 attrs = ret->attrs;
1274 maxAttrs = ret->maxAttrs;
1275 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1276 ret->attrs = attrs;
1277 ret->maxAttrs = maxAttrs;
1278 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001279 if (ret->attrs == NULL) {
1280 ret->maxAttrs = state->maxAttrs;
1281 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1282 sizeof(xmlAttrPtr));
1283 if (ret->attrs == NULL) {
1284 xmlRngVErrMemory(ctxt, "allocating states\n");
1285 ret->nbAttrs = 0;
1286 return (ret);
1287 }
1288 } else if (ret->maxAttrs < state->nbAttrs) {
1289 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001290
Daniel Veillard4c004142003-10-07 11:33:24 +00001291 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1292 sizeof(xmlAttrPtr));
1293 if (tmp == NULL) {
1294 xmlRngVErrMemory(ctxt, "allocating states\n");
1295 ret->nbAttrs = 0;
1296 return (ret);
1297 }
1298 ret->maxAttrs = state->maxAttrs;
1299 ret->attrs = tmp;
1300 }
1301 memcpy(ret->attrs, state->attrs,
1302 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001303 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001304 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001305}
1306
1307/**
1308 * xmlRelaxNGEqualValidState:
1309 * @ctxt: a Relax-NG validation context
1310 * @state1: a validation state
1311 * @state2: a validation state
1312 *
1313 * Compare the validation states for equality
1314 *
1315 * Returns 1 if equald, 0 otherwise
1316 */
1317static int
1318xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001319 xmlRelaxNGValidStatePtr state1,
1320 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001321{
1322 int i;
1323
1324 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001325 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001326 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001327 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001328 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001329 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001330 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001331 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001332 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001333 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001334 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001335 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001336 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001337 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001338 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001339 (!xmlStrEqual(state1->value, state2->value)))
1340 return (0);
1341 for (i = 0; i < state1->nbAttrs; i++) {
1342 if (state1->attrs[i] != state2->attrs[i])
1343 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001344 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001345 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001346}
1347
1348/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001349 * xmlRelaxNGFreeValidState:
1350 * @state: a validation state structure
1351 *
1352 * Deallocate a RelaxNG validation state structure.
1353 */
1354static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001355xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001356 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001357{
1358 if (state == NULL)
1359 return;
1360
Daniel Veillard798024a2003-03-19 10:36:09 +00001361 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001362 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001363 }
1364 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001365 if (state->attrs != NULL)
1366 xmlFree(state->attrs);
1367 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001368 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001369 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001370 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001371}
1372
1373/************************************************************************
1374 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001375 * Document functions *
1376 * *
1377 ************************************************************************/
1378static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001379 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001380
1381/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001382 * xmlRelaxNGIncludePush:
1383 * @ctxt: the parser context
1384 * @value: the element doc
1385 *
1386 * Pushes a new include on top of the include stack
1387 *
1388 * Returns 0 in case of error, the index in the stack otherwise
1389 */
1390static int
1391xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001392 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001393{
1394 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001395 ctxt->incMax = 4;
1396 ctxt->incNr = 0;
1397 ctxt->incTab =
1398 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1399 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001400 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001401 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001402 return (0);
1403 }
1404 }
1405 if (ctxt->incNr >= ctxt->incMax) {
1406 ctxt->incMax *= 2;
1407 ctxt->incTab =
1408 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001409 ctxt->incMax *
1410 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001411 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001412 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001413 return (0);
1414 }
1415 }
1416 ctxt->incTab[ctxt->incNr] = value;
1417 ctxt->inc = value;
1418 return (ctxt->incNr++);
1419}
1420
1421/**
1422 * xmlRelaxNGIncludePop:
1423 * @ctxt: the parser context
1424 *
1425 * Pops the top include from the include stack
1426 *
1427 * Returns the include just removed
1428 */
1429static xmlRelaxNGIncludePtr
1430xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1431{
1432 xmlRelaxNGIncludePtr ret;
1433
1434 if (ctxt->incNr <= 0)
1435 return (0);
1436 ctxt->incNr--;
1437 if (ctxt->incNr > 0)
1438 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1439 else
1440 ctxt->inc = NULL;
1441 ret = ctxt->incTab[ctxt->incNr];
1442 ctxt->incTab[ctxt->incNr] = 0;
1443 return (ret);
1444}
1445
1446/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001447 * xmlRelaxNGRemoveRedefine:
1448 * @ctxt: the parser context
1449 * @URL: the normalized URL
1450 * @target: the included target
1451 * @name: the define name to eliminate
1452 *
1453 * Applies the elimination algorithm of 4.7
1454 *
1455 * Returns 0 in case of error, 1 in case of success.
1456 */
1457static int
1458xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001459 const xmlChar * URL ATTRIBUTE_UNUSED,
1460 xmlNodePtr target, const xmlChar * name)
1461{
Daniel Veillard5add8682003-03-10 13:13:58 +00001462 int found = 0;
1463 xmlNodePtr tmp, tmp2;
1464 xmlChar *name2;
1465
1466#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001467 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001468 xmlGenericError(xmlGenericErrorContext,
1469 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001470 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001471 xmlGenericError(xmlGenericErrorContext,
1472 "Elimination of <include> define %s from %s\n",
1473 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001474#endif
1475 tmp = target;
1476 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001477 tmp2 = tmp->next;
1478 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1479 found = 1;
1480 xmlUnlinkNode(tmp);
1481 xmlFreeNode(tmp);
1482 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1483 name2 = xmlGetProp(tmp, BAD_CAST "name");
1484 xmlRelaxNGNormExtSpace(name2);
1485 if (name2 != NULL) {
1486 if (xmlStrEqual(name, name2)) {
1487 found = 1;
1488 xmlUnlinkNode(tmp);
1489 xmlFreeNode(tmp);
1490 }
1491 xmlFree(name2);
1492 }
1493 } else if (IS_RELAXNG(tmp, "include")) {
1494 xmlChar *href = NULL;
1495 xmlRelaxNGDocumentPtr inc = tmp->_private;
Daniel Veillard5add8682003-03-10 13:13:58 +00001496
Daniel Veillard4c004142003-10-07 11:33:24 +00001497 if ((inc != NULL) && (inc->doc != NULL) &&
1498 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001499
Daniel Veillard4c004142003-10-07 11:33:24 +00001500 if (xmlStrEqual
1501 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001502#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001503 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001504#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001505 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1506 inc->doc->children->
1507 children, name) == 1) {
1508 found = 1;
1509 }
1510 if (href != NULL)
1511 xmlFree(href);
1512 }
1513 }
1514 }
1515 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001516 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001517 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001518}
1519
1520/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001521 * xmlRelaxNGLoadInclude:
1522 * @ctxt: the parser context
1523 * @URL: the normalized URL
1524 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001525 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001526 *
1527 * First lookup if the document is already loaded into the parser context,
1528 * check against recursion. If not found the resource is loaded and
1529 * the content is preprocessed before being returned back to the caller.
1530 *
1531 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1532 */
1533static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001534xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1535 xmlNodePtr node, const xmlChar * ns)
1536{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001537 xmlRelaxNGIncludePtr ret = NULL;
1538 xmlDocPtr doc;
1539 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001540 xmlNodePtr root, cur;
1541
1542#ifdef DEBUG_INCLUDE
1543 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001544 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001545#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001546
1547 /*
1548 * check against recursion in the stack
1549 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001550 for (i = 0; i < ctxt->incNr; i++) {
1551 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1552 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1553 "Detected an Include recursion for %s\n", URL,
1554 NULL);
1555 return (NULL);
1556 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001557 }
1558
1559 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001560 * load the document
1561 */
1562 doc = xmlParseFile((const char *) URL);
1563 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001564 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1565 "xmlRelaxNG: could not load %s\n", URL, NULL);
1566 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001567 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001568#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001569 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001570#endif
1571
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001572 /*
1573 * Allocate the document structures and register it first.
1574 */
1575 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1576 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001577 xmlRngPErrMemory(ctxt, "allocating include\n");
1578 xmlFreeDoc(doc);
1579 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001580 }
1581 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1582 ret->doc = doc;
1583 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001584 ret->next = ctxt->includes;
1585 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001586
1587 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001588 * transmit the ns if needed
1589 */
1590 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001591 root = xmlDocGetRootElement(doc);
1592 if (root != NULL) {
1593 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1594 xmlSetProp(root, BAD_CAST "ns", ns);
1595 }
1596 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001597 }
1598
1599 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001600 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001601 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001602 xmlRelaxNGIncludePush(ctxt, ret);
1603
1604 /*
1605 * Some preprocessing of the document content, this include recursing
1606 * in the include stack.
1607 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001608#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001609 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001610#endif
1611
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001612 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1613 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001614 ctxt->inc = NULL;
1615 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001616 }
1617
1618 /*
1619 * Pop up the include from the stack
1620 */
1621 xmlRelaxNGIncludePop(ctxt);
1622
Daniel Veillard5add8682003-03-10 13:13:58 +00001623#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001624 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001625#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001626 /*
1627 * Check that the top element is a grammar
1628 */
1629 root = xmlDocGetRootElement(doc);
1630 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001631 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1632 "xmlRelaxNG: included document is empty %s\n", URL,
1633 NULL);
1634 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001635 }
1636 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001637 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1638 "xmlRelaxNG: included document %s root is not a grammar\n",
1639 URL, NULL);
1640 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001641 }
1642
1643 /*
1644 * Elimination of redefined rules in the include.
1645 */
1646 cur = node->children;
1647 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001648 if (IS_RELAXNG(cur, "start")) {
1649 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001650
Daniel Veillard4c004142003-10-07 11:33:24 +00001651 found =
1652 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1653 if (!found) {
1654 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1655 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1656 URL, NULL);
1657 }
1658 } else if (IS_RELAXNG(cur, "define")) {
1659 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001660
Daniel Veillard4c004142003-10-07 11:33:24 +00001661 name = xmlGetProp(cur, BAD_CAST "name");
1662 if (name == NULL) {
1663 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1664 "xmlRelaxNG: include %s has define without name\n",
1665 URL, NULL);
1666 } else {
1667 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001668
Daniel Veillard4c004142003-10-07 11:33:24 +00001669 xmlRelaxNGNormExtSpace(name);
1670 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1671 root->children, name);
1672 if (!found) {
1673 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1674 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1675 URL, name);
1676 }
1677 xmlFree(name);
1678 }
1679 }
1680 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001681 }
1682
1683
Daniel Veillard4c004142003-10-07 11:33:24 +00001684 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001685}
1686
1687/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001688 * xmlRelaxNGValidErrorPush:
1689 * @ctxt: the validation context
1690 * @err: the error code
1691 * @arg1: the first string argument
1692 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001693 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001694 *
1695 * Pushes a new error on top of the error stack
1696 *
1697 * Returns 0 in case of error, the index in the stack otherwise
1698 */
1699static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001700xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1701 xmlRelaxNGValidErr err, const xmlChar * arg1,
1702 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001703{
1704 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001705
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001706#ifdef DEBUG_ERROR
1707 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001708 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001709#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001710 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001711 ctxt->errMax = 8;
1712 ctxt->errNr = 0;
1713 ctxt->errTab =
1714 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1715 sizeof
1716 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001717 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001718 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001719 return (0);
1720 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001721 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001722 }
1723 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001724 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001725 ctxt->errTab =
1726 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001727 ctxt->errMax *
1728 sizeof
1729 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001730 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001731 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001732 return (0);
1733 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001734 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001735 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001736 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001737 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1738 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001739 cur = &ctxt->errTab[ctxt->errNr];
1740 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001741 if (dup) {
1742 cur->arg1 = xmlStrdup(arg1);
1743 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001744 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001745 } else {
1746 cur->arg1 = arg1;
1747 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001748 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001749 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001750 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001751 cur->node = ctxt->state->node;
1752 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001753 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001754 cur->node = NULL;
1755 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001756 }
1757 ctxt->err = cur;
1758 return (ctxt->errNr++);
1759}
1760
1761/**
1762 * xmlRelaxNGValidErrorPop:
1763 * @ctxt: the validation context
1764 *
1765 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001766 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001767static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001768xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1769{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001770 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001771
Daniel Veillard580ced82003-03-21 21:22:48 +00001772 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001773 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001774 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001775 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001776 ctxt->errNr--;
1777 if (ctxt->errNr > 0)
1778 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1779 else
1780 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001781 cur = &ctxt->errTab[ctxt->errNr];
1782 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001783 if (cur->arg1 != NULL)
1784 xmlFree((xmlChar *) cur->arg1);
1785 cur->arg1 = NULL;
1786 if (cur->arg2 != NULL)
1787 xmlFree((xmlChar *) cur->arg2);
1788 cur->arg2 = NULL;
1789 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001790 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001791}
1792
Daniel Veillard42f12e92003-03-07 18:32:59 +00001793/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001794 * xmlRelaxNGDocumentPush:
1795 * @ctxt: the parser context
1796 * @value: the element doc
1797 *
1798 * Pushes a new doc on top of the doc stack
1799 *
1800 * Returns 0 in case of error, the index in the stack otherwise
1801 */
1802static int
1803xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001804 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001805{
1806 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001807 ctxt->docMax = 4;
1808 ctxt->docNr = 0;
1809 ctxt->docTab =
1810 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1811 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001812 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001813 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001814 return (0);
1815 }
1816 }
1817 if (ctxt->docNr >= ctxt->docMax) {
1818 ctxt->docMax *= 2;
1819 ctxt->docTab =
1820 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001821 ctxt->docMax *
1822 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001823 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001824 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001825 return (0);
1826 }
1827 }
1828 ctxt->docTab[ctxt->docNr] = value;
1829 ctxt->doc = value;
1830 return (ctxt->docNr++);
1831}
1832
1833/**
1834 * xmlRelaxNGDocumentPop:
1835 * @ctxt: the parser context
1836 *
1837 * Pops the top doc from the doc stack
1838 *
1839 * Returns the doc just removed
1840 */
1841static xmlRelaxNGDocumentPtr
1842xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1843{
1844 xmlRelaxNGDocumentPtr ret;
1845
1846 if (ctxt->docNr <= 0)
1847 return (0);
1848 ctxt->docNr--;
1849 if (ctxt->docNr > 0)
1850 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1851 else
1852 ctxt->doc = NULL;
1853 ret = ctxt->docTab[ctxt->docNr];
1854 ctxt->docTab[ctxt->docNr] = 0;
1855 return (ret);
1856}
1857
1858/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001859 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001860 * @ctxt: the parser context
1861 * @URL: the normalized URL
1862 * @ns: the inherited ns if any
1863 *
1864 * First lookup if the document is already loaded into the parser context,
1865 * check against recursion. If not found the resource is loaded and
1866 * the content is preprocessed before being returned back to the caller.
1867 *
1868 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1869 */
1870static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001871xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1872 const xmlChar * URL, const xmlChar * ns)
1873{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001874 xmlRelaxNGDocumentPtr ret = NULL;
1875 xmlDocPtr doc;
1876 xmlNodePtr root;
1877 int i;
1878
1879 /*
1880 * check against recursion in the stack
1881 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001882 for (i = 0; i < ctxt->docNr; i++) {
1883 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1884 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1885 "Detected an externalRef recursion for %s\n", URL,
1886 NULL);
1887 return (NULL);
1888 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001889 }
1890
1891 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001892 * load the document
1893 */
1894 doc = xmlParseFile((const char *) URL);
1895 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001896 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1897 "xmlRelaxNG: could not load %s\n", URL, NULL);
1898 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001899 }
1900
1901 /*
1902 * Allocate the document structures and register it first.
1903 */
1904 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1905 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001906 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1907 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1908 xmlFreeDoc(doc);
1909 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001910 }
1911 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1912 ret->doc = doc;
1913 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001914 ret->next = ctxt->documents;
1915 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001916
1917 /*
1918 * transmit the ns if needed
1919 */
1920 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001921 root = xmlDocGetRootElement(doc);
1922 if (root != NULL) {
1923 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1924 xmlSetProp(root, BAD_CAST "ns", ns);
1925 }
1926 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001927 }
1928
1929 /*
1930 * push it on the stack and register it in the hash table
1931 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001932 xmlRelaxNGDocumentPush(ctxt, ret);
1933
1934 /*
1935 * Some preprocessing of the document content
1936 */
1937 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1938 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001939 ctxt->doc = NULL;
1940 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001941 }
1942
1943 xmlRelaxNGDocumentPop(ctxt);
1944
Daniel Veillard4c004142003-10-07 11:33:24 +00001945 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001946}
1947
1948/************************************************************************
1949 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001950 * Error functions *
1951 * *
1952 ************************************************************************/
1953
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001954#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1955#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
1956#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
1957#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
1958#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001959
Daniel Veillard231d7912003-02-09 14:22:17 +00001960static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00001961xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
1962{
Daniel Veillard231d7912003-02-09 14:22:17 +00001963 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001964 return ("none");
1965 switch (def->type) {
1966 case XML_RELAXNG_EMPTY:
1967 return ("empty");
1968 case XML_RELAXNG_NOT_ALLOWED:
1969 return ("notAllowed");
1970 case XML_RELAXNG_EXCEPT:
1971 return ("except");
1972 case XML_RELAXNG_TEXT:
1973 return ("text");
1974 case XML_RELAXNG_ELEMENT:
1975 return ("element");
1976 case XML_RELAXNG_DATATYPE:
1977 return ("datatype");
1978 case XML_RELAXNG_VALUE:
1979 return ("value");
1980 case XML_RELAXNG_LIST:
1981 return ("list");
1982 case XML_RELAXNG_ATTRIBUTE:
1983 return ("attribute");
1984 case XML_RELAXNG_DEF:
1985 return ("def");
1986 case XML_RELAXNG_REF:
1987 return ("ref");
1988 case XML_RELAXNG_EXTERNALREF:
1989 return ("externalRef");
1990 case XML_RELAXNG_PARENTREF:
1991 return ("parentRef");
1992 case XML_RELAXNG_OPTIONAL:
1993 return ("optional");
1994 case XML_RELAXNG_ZEROORMORE:
1995 return ("zeroOrMore");
1996 case XML_RELAXNG_ONEORMORE:
1997 return ("oneOrMore");
1998 case XML_RELAXNG_CHOICE:
1999 return ("choice");
2000 case XML_RELAXNG_GROUP:
2001 return ("group");
2002 case XML_RELAXNG_INTERLEAVE:
2003 return ("interleave");
2004 case XML_RELAXNG_START:
2005 return ("start");
2006 case XML_RELAXNG_NOOP:
2007 return ("noop");
2008 case XML_RELAXNG_PARAM:
2009 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002010 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002011 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002012}
Daniel Veillardd2298792003-02-14 16:54:11 +00002013
Daniel Veillard6eadf632003-01-23 18:29:16 +00002014/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002015 * xmlRelaxNGGetErrorString:
2016 * @err: the error code
2017 * @arg1: the first string argument
2018 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002019 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002020 * computes a formatted error string for the given error code and args
2021 *
2022 * Returns the error string, it must be deallocated by the caller
2023 */
2024static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002025xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2026 const xmlChar * arg2)
2027{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002028 char msg[1000];
2029
2030 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002031 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002032 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002033 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002034
2035 msg[0] = 0;
2036 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002037 case XML_RELAXNG_OK:
2038 return (NULL);
2039 case XML_RELAXNG_ERR_MEMORY:
2040 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002041 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002042 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2043 break;
2044 case XML_RELAXNG_ERR_TYPEVAL:
2045 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2046 arg2);
2047 break;
2048 case XML_RELAXNG_ERR_DUPID:
2049 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2050 break;
2051 case XML_RELAXNG_ERR_TYPECMP:
2052 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2053 break;
2054 case XML_RELAXNG_ERR_NOSTATE:
2055 return (xmlCharStrdup("Internal error: no state\n"));
2056 case XML_RELAXNG_ERR_NODEFINE:
2057 return (xmlCharStrdup("Internal error: no define\n"));
2058 case XML_RELAXNG_ERR_INTERNAL:
2059 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2060 break;
2061 case XML_RELAXNG_ERR_LISTEXTRA:
2062 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2063 break;
2064 case XML_RELAXNG_ERR_INTERNODATA:
2065 return (xmlCharStrdup
2066 ("Internal: interleave block has no data\n"));
2067 case XML_RELAXNG_ERR_INTERSEQ:
2068 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2069 case XML_RELAXNG_ERR_INTEREXTRA:
2070 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2071 break;
2072 case XML_RELAXNG_ERR_ELEMNAME:
2073 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2074 arg2);
2075 break;
2076 case XML_RELAXNG_ERR_ELEMNONS:
2077 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2078 arg1);
2079 break;
2080 case XML_RELAXNG_ERR_ELEMWRONGNS:
2081 snprintf(msg, 1000,
2082 "Element %s has wrong namespace: expecting %s\n", arg1,
2083 arg2);
2084 break;
2085 case XML_RELAXNG_ERR_ELEMWRONG:
2086 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2087 break;
2088 case XML_RELAXNG_ERR_TEXTWRONG:
2089 snprintf(msg, 1000,
2090 "Did not expect text in element %s content\n", arg1);
2091 break;
2092 case XML_RELAXNG_ERR_ELEMEXTRANS:
2093 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2094 arg1);
2095 break;
2096 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2097 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2098 break;
2099 case XML_RELAXNG_ERR_NOELEM:
2100 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2101 arg1);
2102 break;
2103 case XML_RELAXNG_ERR_NOTELEM:
2104 return (xmlCharStrdup("Expecting an element got text\n"));
2105 case XML_RELAXNG_ERR_ATTRVALID:
2106 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2107 arg1);
2108 break;
2109 case XML_RELAXNG_ERR_CONTENTVALID:
2110 snprintf(msg, 1000, "Element %s failed to validate content\n",
2111 arg1);
2112 break;
2113 case XML_RELAXNG_ERR_EXTRACONTENT:
2114 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2115 arg1, arg2);
2116 break;
2117 case XML_RELAXNG_ERR_INVALIDATTR:
2118 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2119 arg1, arg2);
2120 break;
2121 case XML_RELAXNG_ERR_LACKDATA:
2122 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2123 arg1);
2124 break;
2125 case XML_RELAXNG_ERR_DATAELEM:
2126 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2127 arg1);
2128 break;
2129 case XML_RELAXNG_ERR_VALELEM:
2130 snprintf(msg, 1000, "Value element %s has child elements\n",
2131 arg1);
2132 break;
2133 case XML_RELAXNG_ERR_LISTELEM:
2134 snprintf(msg, 1000, "List element %s has child elements\n",
2135 arg1);
2136 break;
2137 case XML_RELAXNG_ERR_DATATYPE:
2138 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2139 break;
2140 case XML_RELAXNG_ERR_VALUE:
2141 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2142 break;
2143 case XML_RELAXNG_ERR_LIST:
2144 return (xmlCharStrdup("Error validating list\n"));
2145 case XML_RELAXNG_ERR_NOGRAMMAR:
2146 return (xmlCharStrdup("No top grammar defined\n"));
2147 case XML_RELAXNG_ERR_EXTRADATA:
2148 return (xmlCharStrdup("Extra data in the document\n"));
2149 default:
2150 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002151 }
2152 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002153 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002154 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002155 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002156 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002157}
2158
2159/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002160 * xmlRelaxNGShowValidError:
2161 * @ctxt: the validation context
2162 * @err: the error number
2163 * @node: the node
2164 * @child: the node child generating the problem.
2165 * @arg1: the first argument
2166 * @arg2: the second argument
2167 *
2168 * Show a validation error.
2169 */
2170static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002171xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2172 xmlRelaxNGValidErr err, xmlNodePtr node,
2173 xmlNodePtr child, const xmlChar * arg1,
2174 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002175{
2176 xmlChar *msg;
2177
2178 if (ctxt->error == NULL)
2179 return;
2180
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002181#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002182 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002183#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002184 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2185 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002186 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002187
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002188 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002189 ctxt->errNo = err;
2190 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2191 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002192 xmlFree(msg);
2193}
2194
2195/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002196 * xmlRelaxNGPopErrors:
2197 * @ctxt: the validation context
2198 * @level: the error level in the stack
2199 *
2200 * pop and discard all errors until the given level is reached
2201 */
2202static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002203xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2204{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002205 int i;
2206 xmlRelaxNGValidErrorPtr err;
2207
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002208#ifdef DEBUG_ERROR
2209 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002210 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002211#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002212 for (i = level; i < ctxt->errNr; i++) {
2213 err = &ctxt->errTab[i];
2214 if (err->flags & ERROR_IS_DUP) {
2215 if (err->arg1 != NULL)
2216 xmlFree((xmlChar *) err->arg1);
2217 err->arg1 = NULL;
2218 if (err->arg2 != NULL)
2219 xmlFree((xmlChar *) err->arg2);
2220 err->arg2 = NULL;
2221 err->flags = 0;
2222 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002223 }
2224 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002225 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002226 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002227}
Daniel Veillard4c004142003-10-07 11:33:24 +00002228
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002229/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002230 * xmlRelaxNGDumpValidError:
2231 * @ctxt: the validation context
2232 *
2233 * Show all validation error over a given index.
2234 */
2235static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002236xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2237{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002238 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002239 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002240
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002241#ifdef DEBUG_ERROR
2242 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002243 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002244#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002245 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2246 err = &ctxt->errTab[i];
2247 if (k < MAX_ERROR) {
2248 for (j = 0; j < i; j++) {
2249 dup = &ctxt->errTab[j];
2250 if ((err->err == dup->err) && (err->node == dup->node) &&
2251 (xmlStrEqual(err->arg1, dup->arg1)) &&
2252 (xmlStrEqual(err->arg2, dup->arg2))) {
2253 goto skip;
2254 }
2255 }
2256 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2257 err->arg1, err->arg2);
2258 k++;
2259 }
2260 skip:
2261 if (err->flags & ERROR_IS_DUP) {
2262 if (err->arg1 != NULL)
2263 xmlFree((xmlChar *) err->arg1);
2264 err->arg1 = NULL;
2265 if (err->arg2 != NULL)
2266 xmlFree((xmlChar *) err->arg2);
2267 err->arg2 = NULL;
2268 err->flags = 0;
2269 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002270 }
2271 ctxt->errNr = 0;
2272}
Daniel Veillard4c004142003-10-07 11:33:24 +00002273
Daniel Veillard42f12e92003-03-07 18:32:59 +00002274/**
2275 * xmlRelaxNGAddValidError:
2276 * @ctxt: the validation context
2277 * @err: the error number
2278 * @arg1: the first argument
2279 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002280 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002281 *
2282 * Register a validation error, either generating it if it's sure
2283 * or stacking it for later handling if unsure.
2284 */
2285static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002286xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2287 xmlRelaxNGValidErr err, const xmlChar * arg1,
2288 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002289{
2290 if ((ctxt == NULL) || (ctxt->error == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002291 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002292
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002293#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002294 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002295#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002296 /*
2297 * generate the error directly
2298 */
2299 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002300 xmlNodePtr node, seq;
2301
2302 /*
2303 * Flush first any stacked error which might be the
2304 * real cause of the problem.
2305 */
2306 if (ctxt->errNr != 0)
2307 xmlRelaxNGDumpValidError(ctxt);
2308 if (ctxt->state != NULL) {
2309 node = ctxt->state->node;
2310 seq = ctxt->state->seq;
2311 } else {
2312 node = seq = NULL;
2313 }
2314 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002315 }
2316 /*
2317 * Stack the error for later processing if needed
2318 */
2319 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002320 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002321 }
2322}
2323
Daniel Veillard6eadf632003-01-23 18:29:16 +00002324
2325/************************************************************************
2326 * *
2327 * Type library hooks *
2328 * *
2329 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002330static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002331 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002332
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002333/**
2334 * xmlRelaxNGSchemaTypeHave:
2335 * @data: data needed for the library
2336 * @type: the type name
2337 *
2338 * Check if the given type is provided by
2339 * the W3C XMLSchema Datatype library.
2340 *
2341 * Returns 1 if yes, 0 if no and -1 in case of error.
2342 */
2343static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002344xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2345{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002346 xmlSchemaTypePtr typ;
2347
2348 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002349 return (-1);
2350 typ = xmlSchemaGetPredefinedType(type,
2351 BAD_CAST
2352 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002353 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002354 return (0);
2355 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002356}
2357
2358/**
2359 * xmlRelaxNGSchemaTypeCheck:
2360 * @data: data needed for the library
2361 * @type: the type name
2362 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002363 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002364 *
2365 * Check if the given type and value are validated by
2366 * the W3C XMLSchema Datatype library.
2367 *
2368 * Returns 1 if yes, 0 if no and -1 in case of error.
2369 */
2370static int
2371xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002372 const xmlChar * type,
2373 const xmlChar * value,
2374 void **result, xmlNodePtr node)
2375{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002376 xmlSchemaTypePtr typ;
2377 int ret;
2378
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002379 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002380 return (-1);
2381 typ = xmlSchemaGetPredefinedType(type,
2382 BAD_CAST
2383 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002384 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002385 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002386 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002387 (xmlSchemaValPtr *) result, node);
2388 if (ret == 2) /* special ID error code */
2389 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002390 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002391 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002392 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002393 return (0);
2394 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002395}
2396
2397/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002398 * xmlRelaxNGSchemaFacetCheck:
2399 * @data: data needed for the library
2400 * @type: the type name
2401 * @facet: the facet name
2402 * @val: the facet value
2403 * @strval: the string value
2404 * @value: the value to check
2405 *
2406 * Function provided by a type library to check a value facet
2407 *
2408 * Returns 1 if yes, 0 if no and -1 in case of error.
2409 */
2410static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002411xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2412 const xmlChar * type, const xmlChar * facetname,
2413 const xmlChar * val, const xmlChar * strval,
2414 void *value)
2415{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002416 xmlSchemaFacetPtr facet;
2417 xmlSchemaTypePtr typ;
2418 int ret;
2419
2420 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002421 return (-1);
2422 typ = xmlSchemaGetPredefinedType(type,
2423 BAD_CAST
2424 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002425 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002426 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002427
2428 facet = xmlSchemaNewFacet();
2429 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002430 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002431
Daniel Veillard4c004142003-10-07 11:33:24 +00002432 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002433 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002434 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002435 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002436 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002437 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002438 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002439 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002440 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002441 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002442 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002443 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002444 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002445 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002446 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002447 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002448 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002449 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002450 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002451 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002452 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002453 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2454 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2455 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2456 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002457 xmlSchemaFreeFacet(facet);
2458 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002459 }
2460 facet->value = xmlStrdup(val);
2461 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2462 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002463 xmlSchemaFreeFacet(facet);
2464 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002465 }
2466 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2467 xmlSchemaFreeFacet(facet);
2468 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002469 return (-1);
2470 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002471}
2472
2473/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002474 * xmlRelaxNGSchemaFreeValue:
2475 * @data: data needed for the library
2476 * @value: the value to free
2477 *
2478 * Function provided by a type library to free a Schemas value
2479 *
2480 * Returns 1 if yes, 0 if no and -1 in case of error.
2481 */
2482static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002483xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2484{
Daniel Veillard80b19092003-03-28 13:29:53 +00002485 xmlSchemaFreeValue(value);
2486}
2487
2488/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002489 * xmlRelaxNGSchemaTypeCompare:
2490 * @data: data needed for the library
2491 * @type: the type name
2492 * @value1: the first value
2493 * @value2: the second value
2494 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002495 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002496 * Datatype library.
2497 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002498 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002499 */
2500static int
2501xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002502 const xmlChar * type,
2503 const xmlChar * value1,
2504 xmlNodePtr ctxt1,
2505 void *comp1,
2506 const xmlChar * value2, xmlNodePtr ctxt2)
2507{
Daniel Veillard80b19092003-03-28 13:29:53 +00002508 int ret;
2509 xmlSchemaTypePtr typ;
2510 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2511
2512 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002513 return (-1);
2514 typ = xmlSchemaGetPredefinedType(type,
2515 BAD_CAST
2516 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002517 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002518 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002519 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002520 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2521 if (ret != 0)
2522 return (-1);
2523 if (res1 == NULL)
2524 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002525 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002526 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002527 }
2528 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002529 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002530 xmlSchemaFreeValue(res1);
2531 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002532 }
2533 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002534 xmlSchemaFreeValue(res1);
2535 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002536 }
2537 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002538 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002539 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002540 xmlSchemaFreeValue(res2);
2541 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002542 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002543 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002544 return (1);
2545 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002546}
Daniel Veillard4c004142003-10-07 11:33:24 +00002547
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002548/**
2549 * xmlRelaxNGDefaultTypeHave:
2550 * @data: data needed for the library
2551 * @type: the type name
2552 *
2553 * Check if the given type is provided by
2554 * the default datatype library.
2555 *
2556 * Returns 1 if yes, 0 if no and -1 in case of error.
2557 */
2558static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002559xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2560 const xmlChar * type)
2561{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002562 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002563 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002564 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002565 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002566 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002567 return (1);
2568 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002569}
2570
2571/**
2572 * xmlRelaxNGDefaultTypeCheck:
2573 * @data: data needed for the library
2574 * @type: the type name
2575 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002576 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002577 *
2578 * Check if the given type and value are validated by
2579 * the default datatype library.
2580 *
2581 * Returns 1 if yes, 0 if no and -1 in case of error.
2582 */
2583static int
2584xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002585 const xmlChar * type ATTRIBUTE_UNUSED,
2586 const xmlChar * value ATTRIBUTE_UNUSED,
2587 void **result ATTRIBUTE_UNUSED,
2588 xmlNodePtr node ATTRIBUTE_UNUSED)
2589{
Daniel Veillardd4310742003-02-18 21:12:46 +00002590 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002591 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002592 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002593 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002594 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002595 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002596 }
2597
Daniel Veillard4c004142003-10-07 11:33:24 +00002598 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002599}
2600
2601/**
2602 * xmlRelaxNGDefaultTypeCompare:
2603 * @data: data needed for the library
2604 * @type: the type name
2605 * @value1: the first value
2606 * @value2: the second value
2607 *
2608 * Compare two values accordingly a type from the default
2609 * datatype library.
2610 *
2611 * Returns 1 if yes, 0 if no and -1 in case of error.
2612 */
2613static int
2614xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002615 const xmlChar * type,
2616 const xmlChar * value1,
2617 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2618 void *comp1 ATTRIBUTE_UNUSED,
2619 const xmlChar * value2,
2620 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2621{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002622 int ret = -1;
2623
2624 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002625 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002626 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002627 if (!xmlStrEqual(value1, value2)) {
2628 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002629
Daniel Veillard4c004142003-10-07 11:33:24 +00002630 /*
2631 * TODO: trivial optimizations are possible by
2632 * computing at compile-time
2633 */
2634 nval = xmlRelaxNGNormalize(NULL, value1);
2635 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002636
Daniel Veillard4c004142003-10-07 11:33:24 +00002637 if ((nval == NULL) || (nvalue == NULL))
2638 ret = -1;
2639 else if (xmlStrEqual(nval, nvalue))
2640 ret = 1;
2641 else
2642 ret = 0;
2643 if (nval != NULL)
2644 xmlFree(nval);
2645 if (nvalue != NULL)
2646 xmlFree(nvalue);
2647 } else
2648 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002649 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002650 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002651}
Daniel Veillard4c004142003-10-07 11:33:24 +00002652
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002653static int xmlRelaxNGTypeInitialized = 0;
2654static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2655
2656/**
2657 * xmlRelaxNGFreeTypeLibrary:
2658 * @lib: the type library structure
2659 * @namespace: the URI bound to the library
2660 *
2661 * Free the structure associated to the type library
2662 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002663static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002664xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002665 const xmlChar * namespace ATTRIBUTE_UNUSED)
2666{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002667 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002668 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002669 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002670 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002671 xmlFree(lib);
2672}
2673
2674/**
2675 * xmlRelaxNGRegisterTypeLibrary:
2676 * @namespace: the URI bound to the library
2677 * @data: data associated to the library
2678 * @have: the provide function
2679 * @check: the checking function
2680 * @comp: the comparison function
2681 *
2682 * Register a new type library
2683 *
2684 * Returns 0 in case of success and -1 in case of error.
2685 */
2686static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002687xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2688 xmlRelaxNGTypeHave have,
2689 xmlRelaxNGTypeCheck check,
2690 xmlRelaxNGTypeCompare comp,
2691 xmlRelaxNGFacetCheck facet,
2692 xmlRelaxNGTypeFree freef)
2693{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002694 xmlRelaxNGTypeLibraryPtr lib;
2695 int ret;
2696
2697 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002698 (check == NULL) || (comp == NULL))
2699 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002700 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002701 xmlGenericError(xmlGenericErrorContext,
2702 "Relax-NG types library '%s' already registered\n",
2703 namespace);
2704 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002705 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002706 lib =
2707 (xmlRelaxNGTypeLibraryPtr)
2708 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002709 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002710 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002711 return (-1);
2712 }
2713 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2714 lib->namespace = xmlStrdup(namespace);
2715 lib->data = data;
2716 lib->have = have;
2717 lib->comp = comp;
2718 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002719 lib->facet = facet;
2720 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002721 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2722 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002723 xmlGenericError(xmlGenericErrorContext,
2724 "Relax-NG types library failed to register '%s'\n",
2725 namespace);
2726 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2727 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002728 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002729 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002730}
2731
2732/**
2733 * xmlRelaxNGInitTypes:
2734 *
2735 * Initilize the default type libraries.
2736 *
2737 * Returns 0 in case of success and -1 in case of error.
2738 */
2739static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002740xmlRelaxNGInitTypes(void)
2741{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002742 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002743 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002744 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2745 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002746 xmlGenericError(xmlGenericErrorContext,
2747 "Failed to allocate sh table for Relax-NG types\n");
2748 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002749 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002750 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2751 "http://www.w3.org/2001/XMLSchema-datatypes",
2752 NULL, xmlRelaxNGSchemaTypeHave,
2753 xmlRelaxNGSchemaTypeCheck,
2754 xmlRelaxNGSchemaTypeCompare,
2755 xmlRelaxNGSchemaFacetCheck,
2756 xmlRelaxNGSchemaFreeValue);
2757 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2758 xmlRelaxNGDefaultTypeHave,
2759 xmlRelaxNGDefaultTypeCheck,
2760 xmlRelaxNGDefaultTypeCompare, NULL,
2761 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002762 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002763 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002764}
2765
2766/**
2767 * xmlRelaxNGCleanupTypes:
2768 *
2769 * Cleanup the default Schemas type library associated to RelaxNG
2770 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002771void
2772xmlRelaxNGCleanupTypes(void)
2773{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002774 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002775 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002776 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002777 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002778 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002779 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002780}
2781
2782/************************************************************************
2783 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002784 * Compiling element content into regexp *
2785 * *
2786 * Sometime the element content can be compiled into a pure regexp, *
2787 * This allows a faster execution and streamability at that level *
2788 * *
2789 ************************************************************************/
2790
Daniel Veillard52b48c72003-04-13 19:53:42 +00002791static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2792 xmlRelaxNGDefinePtr def);
2793
Daniel Veillard952379b2003-03-17 15:37:12 +00002794/**
2795 * xmlRelaxNGIsCompileable:
2796 * @define: the definition to check
2797 *
2798 * Check if a definition is nullable.
2799 *
2800 * Returns 1 if yes, 0 if no and -1 in case of error
2801 */
2802static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002803xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2804{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002805 int ret = -1;
2806
Daniel Veillard952379b2003-03-17 15:37:12 +00002807 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002808 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002809 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002810 if ((def->type != XML_RELAXNG_ELEMENT) &&
2811 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002812 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002813 if ((def->type != XML_RELAXNG_ELEMENT) &&
2814 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002815 return (0);
2816 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002817 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002818 ret = xmlRelaxNGIsCompileable(def->content);
2819 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002820 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002821 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002822 ret = 1;
2823 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002824 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002825 /*
2826 * Check if the element content is compileable
2827 */
2828 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2829 ((def->dflags & IS_COMPILABLE) == 0)) {
2830 xmlRelaxNGDefinePtr list;
2831
2832 list = def->content;
2833 while (list != NULL) {
2834 ret = xmlRelaxNGIsCompileable(list);
2835 if (ret != 1)
2836 break;
2837 list = list->next;
2838 }
2839 if (ret == 0)
2840 def->dflags |= IS_NOT_COMPILABLE;
2841 if (ret == 1)
2842 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002843#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002844 if (ret == 1) {
2845 xmlGenericError(xmlGenericErrorContext,
2846 "element content for %s is compilable\n",
2847 def->name);
2848 } else if (ret == 0) {
2849 xmlGenericError(xmlGenericErrorContext,
2850 "element content for %s is not compilable\n",
2851 def->name);
2852 } else {
2853 xmlGenericError(xmlGenericErrorContext,
2854 "Problem in RelaxNGIsCompileable for element %s\n",
2855 def->name);
2856 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002857#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002858 }
2859 /*
2860 * All elements return a compileable status unless they
2861 * are generic like anyName
2862 */
2863 if ((def->nameClass != NULL) || (def->name == NULL))
2864 ret = 0;
2865 else
2866 ret = 1;
2867 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002868 case XML_RELAXNG_REF:
2869 case XML_RELAXNG_EXTERNALREF:
2870 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002871 if (def->depth == -20) {
2872 return (1);
2873 } else {
2874 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002875
Daniel Veillard4c004142003-10-07 11:33:24 +00002876 def->depth = -20;
2877 list = def->content;
2878 while (list != NULL) {
2879 ret = xmlRelaxNGIsCompileable(list);
2880 if (ret != 1)
2881 break;
2882 list = list->next;
2883 }
2884 }
2885 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002886 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002887 case XML_RELAXNG_OPTIONAL:
2888 case XML_RELAXNG_ZEROORMORE:
2889 case XML_RELAXNG_ONEORMORE:
2890 case XML_RELAXNG_CHOICE:
2891 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002892 case XML_RELAXNG_DEF:{
2893 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002894
Daniel Veillard4c004142003-10-07 11:33:24 +00002895 list = def->content;
2896 while (list != NULL) {
2897 ret = xmlRelaxNGIsCompileable(list);
2898 if (ret != 1)
2899 break;
2900 list = list->next;
2901 }
2902 break;
2903 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002904 case XML_RELAXNG_EXCEPT:
2905 case XML_RELAXNG_ATTRIBUTE:
2906 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002907 case XML_RELAXNG_DATATYPE:
2908 case XML_RELAXNG_LIST:
2909 case XML_RELAXNG_PARAM:
2910 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002911 ret = 0;
2912 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002913 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00002914 ret = -1;
2915 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002916 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002917 if (ret == 0)
2918 def->dflags |= IS_NOT_COMPILABLE;
2919 if (ret == 1)
2920 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002921#ifdef DEBUG_COMPILE
2922 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002923 xmlGenericError(xmlGenericErrorContext,
2924 "RelaxNGIsCompileable %s : true\n",
2925 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002926 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002927 xmlGenericError(xmlGenericErrorContext,
2928 "RelaxNGIsCompileable %s : false\n",
2929 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002930 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002931 xmlGenericError(xmlGenericErrorContext,
2932 "Problem in RelaxNGIsCompileable %s\n",
2933 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002934 }
2935#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002936 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002937}
2938
2939/**
2940 * xmlRelaxNGCompile:
2941 * ctxt: the RelaxNG parser context
2942 * @define: the definition tree to compile
2943 *
2944 * Compile the set of definitions, it works recursively, till the
2945 * element boundaries, where it tries to compile the content if possible
2946 *
2947 * Returns 0 if success and -1 in case of error
2948 */
2949static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002950xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
2951{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002952 int ret = 0;
2953 xmlRelaxNGDefinePtr list;
2954
Daniel Veillard4c004142003-10-07 11:33:24 +00002955 if ((ctxt == NULL) || (def == NULL))
2956 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002957
Daniel Veillard4c004142003-10-07 11:33:24 +00002958 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00002959 case XML_RELAXNG_START:
2960 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002961 xmlAutomataPtr oldam = ctxt->am;
2962 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002963
2964 def->depth = -25;
2965
Daniel Veillard4c004142003-10-07 11:33:24 +00002966 list = def->content;
2967 ctxt->am = xmlNewAutomata();
2968 if (ctxt->am == NULL)
2969 return (-1);
2970 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2971 while (list != NULL) {
2972 xmlRelaxNGCompile(ctxt, list);
2973 list = list->next;
2974 }
2975 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2976 def->contModel = xmlAutomataCompile(ctxt->am);
2977 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002978
Daniel Veillard4c004142003-10-07 11:33:24 +00002979 xmlFreeAutomata(ctxt->am);
2980 ctxt->state = oldstate;
2981 ctxt->am = oldam;
2982 }
2983 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002984 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002985 if ((ctxt->am != NULL) && (def->name != NULL)) {
2986 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
2987 ctxt->state, NULL,
2988 def->name, def->ns,
2989 def);
2990 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002991 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002992 xmlAutomataPtr oldam = ctxt->am;
2993 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002994
2995 def->depth = -25;
2996
Daniel Veillard4c004142003-10-07 11:33:24 +00002997 list = def->content;
2998 ctxt->am = xmlNewAutomata();
2999 if (ctxt->am == NULL)
3000 return (-1);
3001 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3002 while (list != NULL) {
3003 xmlRelaxNGCompile(ctxt, list);
3004 list = list->next;
3005 }
3006 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3007 def->contModel = xmlAutomataCompile(ctxt->am);
3008 if (!xmlRegexpIsDeterminist(def->contModel)) {
3009 /*
3010 * we can only use the automata if it is determinist
3011 */
3012 xmlRegFreeRegexp(def->contModel);
3013 def->contModel = NULL;
3014 }
3015 xmlFreeAutomata(ctxt->am);
3016 ctxt->state = oldstate;
3017 ctxt->am = oldam;
3018 } else {
3019 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003020
Daniel Veillard4c004142003-10-07 11:33:24 +00003021 /*
3022 * we can't build the content model for this element content
3023 * but it still might be possible to build it for some of its
3024 * children, recurse.
3025 */
3026 ret = xmlRelaxNGTryCompile(ctxt, def);
3027 ctxt->am = oldam;
3028 }
3029 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003030 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003031 ret = xmlRelaxNGCompile(ctxt, def->content);
3032 break;
3033 case XML_RELAXNG_OPTIONAL:{
3034 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003035
Daniel Veillard4c004142003-10-07 11:33:24 +00003036 xmlRelaxNGCompile(ctxt, def->content);
3037 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3038 break;
3039 }
3040 case XML_RELAXNG_ZEROORMORE:{
3041 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003042
Daniel Veillard4c004142003-10-07 11:33:24 +00003043 ctxt->state =
3044 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3045 oldstate = ctxt->state;
3046 list = def->content;
3047 while (list != NULL) {
3048 xmlRelaxNGCompile(ctxt, list);
3049 list = list->next;
3050 }
3051 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3052 ctxt->state =
3053 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3054 break;
3055 }
3056 case XML_RELAXNG_ONEORMORE:{
3057 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003058
Daniel Veillard4c004142003-10-07 11:33:24 +00003059 list = def->content;
3060 while (list != NULL) {
3061 xmlRelaxNGCompile(ctxt, list);
3062 list = list->next;
3063 }
3064 oldstate = ctxt->state;
3065 list = def->content;
3066 while (list != NULL) {
3067 xmlRelaxNGCompile(ctxt, list);
3068 list = list->next;
3069 }
3070 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3071 ctxt->state =
3072 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3073 break;
3074 }
3075 case XML_RELAXNG_CHOICE:{
3076 xmlAutomataStatePtr target = NULL;
3077 xmlAutomataStatePtr oldstate = ctxt->state;
3078
3079 list = def->content;
3080 while (list != NULL) {
3081 ctxt->state = oldstate;
3082 ret = xmlRelaxNGCompile(ctxt, list);
3083 if (ret != 0)
3084 break;
3085 if (target == NULL)
3086 target = ctxt->state;
3087 else {
3088 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3089 target);
3090 }
3091 list = list->next;
3092 }
3093 ctxt->state = target;
3094
3095 break;
3096 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003097 case XML_RELAXNG_REF:
3098 case XML_RELAXNG_EXTERNALREF:
3099 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003100 case XML_RELAXNG_GROUP:
3101 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003102 list = def->content;
3103 while (list != NULL) {
3104 ret = xmlRelaxNGCompile(ctxt, list);
3105 if (ret != 0)
3106 break;
3107 list = list->next;
3108 }
3109 break;
3110 case XML_RELAXNG_TEXT:{
3111 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003112
Daniel Veillard4c004142003-10-07 11:33:24 +00003113 ctxt->state =
3114 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3115 oldstate = ctxt->state;
3116 xmlRelaxNGCompile(ctxt, def->content);
3117 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3118 ctxt->state, BAD_CAST "#text",
3119 NULL);
3120 ctxt->state =
3121 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3122 break;
3123 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003124 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003125 ctxt->state =
3126 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3127 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003128 case XML_RELAXNG_EXCEPT:
3129 case XML_RELAXNG_ATTRIBUTE:
3130 case XML_RELAXNG_INTERLEAVE:
3131 case XML_RELAXNG_NOT_ALLOWED:
3132 case XML_RELAXNG_DATATYPE:
3133 case XML_RELAXNG_LIST:
3134 case XML_RELAXNG_PARAM:
3135 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003136 /* This should not happen and generate an internal error */
3137 fprintf(stderr, "RNG internal error trying to compile %s\n",
3138 xmlRelaxNGDefName(def));
3139 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003140 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003141 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003142}
3143
3144/**
3145 * xmlRelaxNGTryCompile:
3146 * ctxt: the RelaxNG parser context
3147 * @define: the definition tree to compile
3148 *
3149 * Try to compile the set of definitions, it works recursively,
3150 * possibly ignoring parts which cannot be compiled.
3151 *
3152 * Returns 0 if success and -1 in case of error
3153 */
3154static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003155xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3156{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003157 int ret = 0;
3158 xmlRelaxNGDefinePtr list;
3159
Daniel Veillard4c004142003-10-07 11:33:24 +00003160 if ((ctxt == NULL) || (def == NULL))
3161 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003162
3163 if ((def->type == XML_RELAXNG_START) ||
3164 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003165 ret = xmlRelaxNGIsCompileable(def);
3166 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3167 ctxt->am = NULL;
3168 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003169#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003170 if (ret == 0) {
3171 if (def->type == XML_RELAXNG_START)
3172 xmlGenericError(xmlGenericErrorContext,
3173 "compiled the start\n");
3174 else
3175 xmlGenericError(xmlGenericErrorContext,
3176 "compiled element %s\n", def->name);
3177 } else {
3178 if (def->type == XML_RELAXNG_START)
3179 xmlGenericError(xmlGenericErrorContext,
3180 "failed to compile the start\n");
3181 else
3182 xmlGenericError(xmlGenericErrorContext,
3183 "failed to compile element %s\n",
3184 def->name);
3185 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003186#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003187 return (ret);
3188 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003189 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003190 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003191 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003192 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3193 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003194 case XML_RELAXNG_TEXT:
3195 case XML_RELAXNG_DATATYPE:
3196 case XML_RELAXNG_LIST:
3197 case XML_RELAXNG_PARAM:
3198 case XML_RELAXNG_VALUE:
3199 case XML_RELAXNG_EMPTY:
3200 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003201 ret = 0;
3202 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003203 case XML_RELAXNG_OPTIONAL:
3204 case XML_RELAXNG_ZEROORMORE:
3205 case XML_RELAXNG_ONEORMORE:
3206 case XML_RELAXNG_CHOICE:
3207 case XML_RELAXNG_GROUP:
3208 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003209 case XML_RELAXNG_START:
3210 case XML_RELAXNG_REF:
3211 case XML_RELAXNG_EXTERNALREF:
3212 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003213 list = def->content;
3214 while (list != NULL) {
3215 ret = xmlRelaxNGTryCompile(ctxt, list);
3216 if (ret != 0)
3217 break;
3218 list = list->next;
3219 }
3220 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003221 case XML_RELAXNG_EXCEPT:
3222 case XML_RELAXNG_ATTRIBUTE:
3223 case XML_RELAXNG_INTERLEAVE:
3224 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003225 ret = 0;
3226 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003227 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003228 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003229}
3230
3231/************************************************************************
3232 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003233 * Parsing functions *
3234 * *
3235 ************************************************************************/
3236
Daniel Veillard4c004142003-10-07 11:33:24 +00003237static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3238 ctxt, xmlNodePtr node);
3239static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3240 ctxt, xmlNodePtr node);
3241static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3242 ctxt, xmlNodePtr nodes,
3243 int group);
3244static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3245 ctxt, xmlNodePtr node);
3246static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3247 xmlNodePtr node);
3248static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3249 xmlNodePtr nodes);
3250static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3251 ctxt, xmlNodePtr node,
3252 xmlRelaxNGDefinePtr
3253 def);
3254static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3255 ctxt, xmlNodePtr nodes);
3256static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3257 xmlRelaxNGDefinePtr define,
3258 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003259
3260
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003261#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003262
3263/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003264 * xmlRelaxNGIsNullable:
3265 * @define: the definition to verify
3266 *
3267 * Check if a definition is nullable.
3268 *
3269 * Returns 1 if yes, 0 if no and -1 in case of error
3270 */
3271static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003272xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3273{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003274 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003275
Daniel Veillardfd573f12003-03-16 17:52:32 +00003276 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003277 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003278
Daniel Veillarde063f482003-03-21 16:53:17 +00003279 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003280 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003281 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003282 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003283 switch (define->type) {
3284 case XML_RELAXNG_EMPTY:
3285 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003286 ret = 1;
3287 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003288 case XML_RELAXNG_NOOP:
3289 case XML_RELAXNG_DEF:
3290 case XML_RELAXNG_REF:
3291 case XML_RELAXNG_EXTERNALREF:
3292 case XML_RELAXNG_PARENTREF:
3293 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003294 ret = xmlRelaxNGIsNullable(define->content);
3295 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003296 case XML_RELAXNG_EXCEPT:
3297 case XML_RELAXNG_NOT_ALLOWED:
3298 case XML_RELAXNG_ELEMENT:
3299 case XML_RELAXNG_DATATYPE:
3300 case XML_RELAXNG_PARAM:
3301 case XML_RELAXNG_VALUE:
3302 case XML_RELAXNG_LIST:
3303 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003304 ret = 0;
3305 break;
3306 case XML_RELAXNG_CHOICE:{
3307 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003308
Daniel Veillard4c004142003-10-07 11:33:24 +00003309 while (list != NULL) {
3310 ret = xmlRelaxNGIsNullable(list);
3311 if (ret != 0)
3312 goto done;
3313 list = list->next;
3314 }
3315 ret = 0;
3316 break;
3317 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003318 case XML_RELAXNG_START:
3319 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003320 case XML_RELAXNG_GROUP:{
3321 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003322
Daniel Veillard4c004142003-10-07 11:33:24 +00003323 while (list != NULL) {
3324 ret = xmlRelaxNGIsNullable(list);
3325 if (ret != 1)
3326 goto done;
3327 list = list->next;
3328 }
3329 return (1);
3330 }
3331 default:
3332 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003333 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003334 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003335 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003336 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003337 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003338 define->dflags |= IS_NULLABLE;
3339 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003340}
3341
3342/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003343 * xmlRelaxNGIsBlank:
3344 * @str: a string
3345 *
3346 * Check if a string is ignorable c.f. 4.2. Whitespace
3347 *
3348 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3349 */
3350static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003351xmlRelaxNGIsBlank(xmlChar * str)
3352{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003353 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003354 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003355 while (*str != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003356 if (!(IS_BLANK(*str)))
3357 return (0);
3358 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003359 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003360 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003361}
3362
Daniel Veillard6eadf632003-01-23 18:29:16 +00003363/**
3364 * xmlRelaxNGGetDataTypeLibrary:
3365 * @ctxt: a Relax-NG parser context
3366 * @node: the current data or value element
3367 *
3368 * Applies algorithm from 4.3. datatypeLibrary attribute
3369 *
3370 * Returns the datatypeLibary value or NULL if not found
3371 */
3372static xmlChar *
3373xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003374 xmlNodePtr node)
3375{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003376 xmlChar *ret, *escape;
3377
Daniel Veillard6eadf632003-01-23 18:29:16 +00003378 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003379 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3380 if (ret != NULL) {
3381 if (ret[0] == 0) {
3382 xmlFree(ret);
3383 return (NULL);
3384 }
3385 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3386 if (escape == NULL) {
3387 return (ret);
3388 }
3389 xmlFree(ret);
3390 return (escape);
3391 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003392 }
3393 node = node->parent;
3394 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003395 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3396 if (ret != NULL) {
3397 if (ret[0] == 0) {
3398 xmlFree(ret);
3399 return (NULL);
3400 }
3401 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3402 if (escape == NULL) {
3403 return (ret);
3404 }
3405 xmlFree(ret);
3406 return (escape);
3407 }
3408 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003409 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003410 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003411}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003412
3413/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003414 * xmlRelaxNGParseValue:
3415 * @ctxt: a Relax-NG parser context
3416 * @node: the data node.
3417 *
3418 * parse the content of a RelaxNG value node.
3419 *
3420 * Returns the definition pointer or NULL in case of error
3421 */
3422static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003423xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3424{
Daniel Veillardedc91922003-01-26 00:52:04 +00003425 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003426 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003427 xmlChar *type;
3428 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003429 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003430
Daniel Veillardfd573f12003-03-16 17:52:32 +00003431 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003432 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003433 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003434 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003435
3436 type = xmlGetProp(node, BAD_CAST "type");
3437 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003438 xmlRelaxNGNormExtSpace(type);
3439 if (xmlValidateNCName(type, 0)) {
3440 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3441 "value type '%s' is not an NCName\n", type, NULL);
3442 }
3443 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3444 if (library == NULL)
3445 library =
3446 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003447
Daniel Veillard4c004142003-10-07 11:33:24 +00003448 def->name = type;
3449 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003450
Daniel Veillard4c004142003-10-07 11:33:24 +00003451 lib = (xmlRelaxNGTypeLibraryPtr)
3452 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3453 if (lib == NULL) {
3454 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3455 "Use of unregistered type library '%s'\n", library,
3456 NULL);
3457 def->data = NULL;
3458 } else {
3459 def->data = lib;
3460 if (lib->have == NULL) {
3461 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3462 "Internal error with type library '%s': no 'have'\n",
3463 library, NULL);
3464 } else {
3465 success = lib->have(lib->data, def->name);
3466 if (success != 1) {
3467 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3468 "Error type '%s' is not exported by type library '%s'\n",
3469 def->name, library);
3470 }
3471 }
3472 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003473 }
3474 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003475 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003476 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003477 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3478 (node->children->next != NULL)) {
3479 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3480 "Expecting a single text value for <value>content\n",
3481 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003482 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003483 def->value = xmlNodeGetContent(node);
3484 if (def->value == NULL) {
3485 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3486 "Element <value> has no content\n", NULL, NULL);
3487 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3488 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003489
Daniel Veillard4c004142003-10-07 11:33:24 +00003490 success =
3491 lib->check(lib->data, def->name, def->value, &val, node);
3492 if (success != 1) {
3493 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3494 "Value '%s' is not acceptable for type '%s'\n",
3495 def->value, def->name);
3496 } else {
3497 if (val != NULL)
3498 def->attrs = val;
3499 }
3500 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003501 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003502 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003503}
3504
3505/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003506 * xmlRelaxNGParseData:
3507 * @ctxt: a Relax-NG parser context
3508 * @node: the data node.
3509 *
3510 * parse the content of a RelaxNG data node.
3511 *
3512 * Returns the definition pointer or NULL in case of error
3513 */
3514static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003515xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3516{
Daniel Veillard416589a2003-02-17 17:25:42 +00003517 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003518 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003519 xmlRelaxNGTypeLibraryPtr lib;
3520 xmlChar *type;
3521 xmlChar *library;
3522 xmlNodePtr content;
3523 int tmp;
3524
3525 type = xmlGetProp(node, BAD_CAST "type");
3526 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003527 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3528 NULL);
3529 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003530 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003531 xmlRelaxNGNormExtSpace(type);
3532 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003533 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3534 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003535 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003536 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3537 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003538 library =
3539 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003540
Daniel Veillardfd573f12003-03-16 17:52:32 +00003541 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003542 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003543 xmlFree(type);
3544 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003545 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003546 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003547 def->name = type;
3548 def->ns = library;
3549
3550 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003551 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003552 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003553 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3554 "Use of unregistered type library '%s'\n", library,
3555 NULL);
3556 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003557 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003558 def->data = lib;
3559 if (lib->have == NULL) {
3560 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3561 "Internal error with type library '%s': no 'have'\n",
3562 library, NULL);
3563 } else {
3564 tmp = lib->have(lib->data, def->name);
3565 if (tmp != 1) {
3566 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3567 "Error type '%s' is not exported by type library '%s'\n",
3568 def->name, library);
3569 } else
3570 if ((xmlStrEqual
3571 (library,
3572 BAD_CAST
3573 "http://www.w3.org/2001/XMLSchema-datatypes"))
3574 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3575 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3576 ctxt->idref = 1;
3577 }
3578 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003579 }
3580 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003581
3582 /*
3583 * Handle optional params
3584 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003585 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003586 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3587 break;
3588 if (xmlStrEqual(library,
3589 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3590 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3591 "Type library '%s' does not allow type parameters\n",
3592 library, NULL);
3593 content = content->next;
3594 while ((content != NULL) &&
3595 (xmlStrEqual(content->name, BAD_CAST "param")))
3596 content = content->next;
3597 } else {
3598 param = xmlRelaxNGNewDefine(ctxt, node);
3599 if (param != NULL) {
3600 param->type = XML_RELAXNG_PARAM;
3601 param->name = xmlGetProp(content, BAD_CAST "name");
3602 if (param->name == NULL) {
3603 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3604 "param has no name\n", NULL, NULL);
3605 }
3606 param->value = xmlNodeGetContent(content);
3607 if (lastparam == NULL) {
3608 def->attrs = lastparam = param;
3609 } else {
3610 lastparam->next = param;
3611 lastparam = param;
3612 }
3613 if (lib != NULL) {
3614 }
3615 }
3616 content = content->next;
3617 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003618 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003619 /*
3620 * Handle optional except
3621 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003622 if ((content != NULL)
3623 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3624 xmlNodePtr child;
3625 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003626
Daniel Veillard4c004142003-10-07 11:33:24 +00003627 except = xmlRelaxNGNewDefine(ctxt, node);
3628 if (except == NULL) {
3629 return (def);
3630 }
3631 except->type = XML_RELAXNG_EXCEPT;
3632 child = content->children;
3633 if (last == NULL) {
3634 def->content = except;
3635 } else {
3636 last->next = except;
3637 }
3638 if (child == NULL) {
3639 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3640 "except has no content\n", NULL, NULL);
3641 }
3642 while (child != NULL) {
3643 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3644 if (tmp2 != NULL) {
3645 if (last2 == NULL) {
3646 except->content = last2 = tmp2;
3647 } else {
3648 last2->next = tmp2;
3649 last2 = tmp2;
3650 }
3651 }
3652 child = child->next;
3653 }
3654 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003655 }
3656 /*
3657 * Check there is no unhandled data
3658 */
3659 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003660 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3661 "Element data has unexpected content %s\n",
3662 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003663 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003664
Daniel Veillard4c004142003-10-07 11:33:24 +00003665 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003666}
3667
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003668static const xmlChar *invalidName = BAD_CAST "\1";
3669
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003670/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003671 * xmlRelaxNGCompareNameClasses:
3672 * @defs1: the first element/attribute defs
3673 * @defs2: the second element/attribute defs
3674 * @name: the restriction on the name
3675 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003676 *
3677 * Compare the 2 lists of element definitions. The comparison is
3678 * that if both lists do not accept the same QNames, it returns 1
3679 * If the 2 lists can accept the same QName the comparison returns 0
3680 *
3681 * Returns 1 disttinct, 0 if equal
3682 */
3683static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003684xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003685 xmlRelaxNGDefinePtr def2)
3686{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003687 int ret = 1;
3688 xmlNode node;
3689 xmlNs ns;
3690 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003691
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003692 ctxt.flags = FLAGS_IGNORABLE;
3693
Daniel Veillard42f12e92003-03-07 18:32:59 +00003694 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3695
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003696 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003697 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3698 if (def2->type == XML_RELAXNG_TEXT)
3699 return (1);
3700 if (def1->name != NULL) {
3701 node.name = def1->name;
3702 } else {
3703 node.name = invalidName;
3704 }
3705 node.ns = &ns;
3706 if (def1->ns != NULL) {
3707 if (def1->ns[0] == 0) {
3708 node.ns = NULL;
3709 } else {
3710 ns.href = def1->ns;
3711 }
3712 } else {
3713 ns.href = invalidName;
3714 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003715 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003716 if (def1->nameClass != NULL) {
3717 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3718 } else {
3719 ret = 0;
3720 }
3721 } else {
3722 ret = 1;
3723 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003724 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003725 if (def2->type == XML_RELAXNG_TEXT)
3726 return (0);
3727 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003728 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003729 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003730 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003731 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003732 }
3733 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003734 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003735 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003736 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3737 if (def2->name != NULL) {
3738 node.name = def2->name;
3739 } else {
3740 node.name = invalidName;
3741 }
3742 node.ns = &ns;
3743 if (def2->ns != NULL) {
3744 if (def2->ns[0] == 0) {
3745 node.ns = NULL;
3746 } else {
3747 ns.href = def2->ns;
3748 }
3749 } else {
3750 ns.href = invalidName;
3751 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003752 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003753 if (def2->nameClass != NULL) {
3754 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3755 } else {
3756 ret = 0;
3757 }
3758 } else {
3759 ret = 1;
3760 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003761 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003762 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003763 }
3764
Daniel Veillard4c004142003-10-07 11:33:24 +00003765 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003766}
3767
3768/**
3769 * xmlRelaxNGCompareElemDefLists:
3770 * @ctxt: a Relax-NG parser context
3771 * @defs1: the first list of element/attribute defs
3772 * @defs2: the second list of element/attribute defs
3773 *
3774 * Compare the 2 lists of element or attribute definitions. The comparison
3775 * is that if both lists do not accept the same QNames, it returns 1
3776 * If the 2 lists can accept the same QName the comparison returns 0
3777 *
3778 * Returns 1 disttinct, 0 if equal
3779 */
3780static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003781xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3782 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3783 xmlRelaxNGDefinePtr * def2)
3784{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003785 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003786
Daniel Veillard154877e2003-01-30 12:17:05 +00003787 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003788 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003789 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003790 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003791 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003792 while ((*def2) != NULL) {
3793 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3794 return (0);
3795 def2++;
3796 }
3797 def2 = basedef2;
3798 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003799 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003800 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003801}
3802
3803/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003804 * xmlRelaxNGGenerateAttributes:
3805 * @ctxt: a Relax-NG parser context
3806 * @def: the definition definition
3807 *
3808 * Check if the definition can only generate attributes
3809 *
3810 * Returns 1 if yes, 0 if no and -1 in case of error.
3811 */
3812static int
3813xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003814 xmlRelaxNGDefinePtr def)
3815{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003816 xmlRelaxNGDefinePtr parent, cur, tmp;
3817
3818 /*
3819 * Don't run that check in case of error. Infinite recursion
3820 * becomes possible.
3821 */
3822 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003823 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003824
3825 parent = NULL;
3826 cur = def;
3827 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003828 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3829 (cur->type == XML_RELAXNG_TEXT) ||
3830 (cur->type == XML_RELAXNG_DATATYPE) ||
3831 (cur->type == XML_RELAXNG_PARAM) ||
3832 (cur->type == XML_RELAXNG_LIST) ||
3833 (cur->type == XML_RELAXNG_VALUE) ||
3834 (cur->type == XML_RELAXNG_EMPTY))
3835 return (0);
3836 if ((cur->type == XML_RELAXNG_CHOICE) ||
3837 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3838 (cur->type == XML_RELAXNG_GROUP) ||
3839 (cur->type == XML_RELAXNG_ONEORMORE) ||
3840 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3841 (cur->type == XML_RELAXNG_OPTIONAL) ||
3842 (cur->type == XML_RELAXNG_PARENTREF) ||
3843 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3844 (cur->type == XML_RELAXNG_REF) ||
3845 (cur->type == XML_RELAXNG_DEF)) {
3846 if (cur->content != NULL) {
3847 parent = cur;
3848 cur = cur->content;
3849 tmp = cur;
3850 while (tmp != NULL) {
3851 tmp->parent = parent;
3852 tmp = tmp->next;
3853 }
3854 continue;
3855 }
3856 }
3857 if (cur == def)
3858 break;
3859 if (cur->next != NULL) {
3860 cur = cur->next;
3861 continue;
3862 }
3863 do {
3864 cur = cur->parent;
3865 if (cur == NULL)
3866 break;
3867 if (cur == def)
3868 return (1);
3869 if (cur->next != NULL) {
3870 cur = cur->next;
3871 break;
3872 }
3873 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003874 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003875 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003876}
Daniel Veillard4c004142003-10-07 11:33:24 +00003877
Daniel Veillardce192eb2003-04-16 15:58:05 +00003878/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003879 * xmlRelaxNGGetElements:
3880 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003881 * @def: the definition definition
3882 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003883 *
3884 * Compute the list of top elements a definition can generate
3885 *
3886 * Returns a list of elements or NULL if none was found.
3887 */
3888static xmlRelaxNGDefinePtr *
3889xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003890 xmlRelaxNGDefinePtr def, int eora)
3891{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003892 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003893 int len = 0;
3894 int max = 0;
3895
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003896 /*
3897 * Don't run that check in case of error. Infinite recursion
3898 * becomes possible.
3899 */
3900 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003901 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003902
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003903 parent = NULL;
3904 cur = def;
3905 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003906 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3907 (cur->type == XML_RELAXNG_TEXT))) ||
3908 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3909 if (ret == NULL) {
3910 max = 10;
3911 ret = (xmlRelaxNGDefinePtr *)
3912 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3913 if (ret == NULL) {
3914 xmlRngPErrMemory(ctxt, "getting element list\n");
3915 return (NULL);
3916 }
3917 } else if (max <= len) {
3918 max *= 2;
3919 ret =
3920 xmlRealloc(ret,
3921 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3922 if (ret == NULL) {
3923 xmlRngPErrMemory(ctxt, "getting element list\n");
3924 return (NULL);
3925 }
3926 }
3927 ret[len++] = cur;
3928 ret[len] = NULL;
3929 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3930 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3931 (cur->type == XML_RELAXNG_GROUP) ||
3932 (cur->type == XML_RELAXNG_ONEORMORE) ||
3933 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3934 (cur->type == XML_RELAXNG_OPTIONAL) ||
3935 (cur->type == XML_RELAXNG_PARENTREF) ||
3936 (cur->type == XML_RELAXNG_REF) ||
3937 (cur->type == XML_RELAXNG_DEF)) {
3938 /*
3939 * Don't go within elements or attributes or string values.
3940 * Just gather the element top list
3941 */
3942 if (cur->content != NULL) {
3943 parent = cur;
3944 cur = cur->content;
3945 tmp = cur;
3946 while (tmp != NULL) {
3947 tmp->parent = parent;
3948 tmp = tmp->next;
3949 }
3950 continue;
3951 }
3952 }
3953 if (cur == def)
3954 break;
3955 if (cur->next != NULL) {
3956 cur = cur->next;
3957 continue;
3958 }
3959 do {
3960 cur = cur->parent;
3961 if (cur == NULL)
3962 break;
3963 if (cur == def)
3964 return (ret);
3965 if (cur->next != NULL) {
3966 cur = cur->next;
3967 break;
3968 }
3969 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003970 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003971 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003972}
Daniel Veillard4c004142003-10-07 11:33:24 +00003973
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003974/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003975 * xmlRelaxNGCheckChoiceDeterminism:
3976 * @ctxt: a Relax-NG parser context
3977 * @def: the choice definition
3978 *
3979 * Also used to find indeterministic pattern in choice
3980 */
3981static void
3982xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003983 xmlRelaxNGDefinePtr def)
3984{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003985 xmlRelaxNGDefinePtr **list;
3986 xmlRelaxNGDefinePtr cur;
3987 int nbchild = 0, i, j, ret;
3988 int is_nullable = 0;
3989 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003990 xmlHashTablePtr triage = NULL;
3991 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003992
Daniel Veillard4c004142003-10-07 11:33:24 +00003993 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
3994 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003995
Daniel Veillarde063f482003-03-21 16:53:17 +00003996 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00003997 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00003998
Daniel Veillardfd573f12003-03-16 17:52:32 +00003999 /*
4000 * Don't run that check in case of error. Infinite recursion
4001 * becomes possible.
4002 */
4003 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004004 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004005
4006 is_nullable = xmlRelaxNGIsNullable(def);
4007
4008 cur = def->content;
4009 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004010 nbchild++;
4011 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004012 }
4013
4014 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004015 sizeof(xmlRelaxNGDefinePtr
4016 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004017 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004018 xmlRngPErrMemory(ctxt, "building choice\n");
4019 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004020 }
4021 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004022 /*
4023 * a bit strong but safe
4024 */
4025 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004026 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004027 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004028 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004029 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004030 cur = def->content;
4031 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004032 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4033 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4034 is_triable = 0;
4035 } else if (is_triable == 1) {
4036 xmlRelaxNGDefinePtr *tmp;
4037 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004038
Daniel Veillard4c004142003-10-07 11:33:24 +00004039 tmp = list[i];
4040 while ((*tmp != NULL) && (is_triable == 1)) {
4041 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4042 res = xmlHashAddEntry2(triage,
4043 BAD_CAST "#text", NULL,
4044 (void *) cur);
4045 if (res != 0)
4046 is_triable = -1;
4047 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4048 ((*tmp)->name != NULL)) {
4049 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4050 res = xmlHashAddEntry2(triage,
4051 (*tmp)->name, NULL,
4052 (void *) cur);
4053 else
4054 res = xmlHashAddEntry2(triage,
4055 (*tmp)->name, (*tmp)->ns,
4056 (void *) cur);
4057 if (res != 0)
4058 is_triable = -1;
4059 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4060 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4061 res = xmlHashAddEntry2(triage,
4062 BAD_CAST "#any", NULL,
4063 (void *) cur);
4064 else
4065 res = xmlHashAddEntry2(triage,
4066 BAD_CAST "#any", (*tmp)->ns,
4067 (void *) cur);
4068 if (res != 0)
4069 is_triable = -1;
4070 } else {
4071 is_triable = -1;
4072 }
4073 tmp++;
4074 }
4075 }
4076 i++;
4077 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004078 }
4079
Daniel Veillard4c004142003-10-07 11:33:24 +00004080 for (i = 0; i < nbchild; i++) {
4081 if (list[i] == NULL)
4082 continue;
4083 for (j = 0; j < i; j++) {
4084 if (list[j] == NULL)
4085 continue;
4086 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4087 if (ret == 0) {
4088 is_indeterminist = 1;
4089 }
4090 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004091 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004092 for (i = 0; i < nbchild; i++) {
4093 if (list[i] != NULL)
4094 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004095 }
4096
4097 xmlFree(list);
4098 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004099 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004100 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004101 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004102 def->dflags |= IS_TRIABLE;
4103 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004104 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004105 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004106 }
4107 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004108}
4109
4110/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004111 * xmlRelaxNGCheckGroupAttrs:
4112 * @ctxt: a Relax-NG parser context
4113 * @def: the group definition
4114 *
4115 * Detects violations of rule 7.3
4116 */
4117static void
4118xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004119 xmlRelaxNGDefinePtr def)
4120{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004121 xmlRelaxNGDefinePtr **list;
4122 xmlRelaxNGDefinePtr cur;
4123 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004124
4125 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004126 ((def->type != XML_RELAXNG_GROUP) &&
4127 (def->type != XML_RELAXNG_ELEMENT)))
4128 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004129
Daniel Veillarde063f482003-03-21 16:53:17 +00004130 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004131 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004132
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004133 /*
4134 * Don't run that check in case of error. Infinite recursion
4135 * becomes possible.
4136 */
4137 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004138 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004139
Daniel Veillardfd573f12003-03-16 17:52:32 +00004140 cur = def->attrs;
4141 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004142 nbchild++;
4143 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004144 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004145 cur = def->content;
4146 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004147 nbchild++;
4148 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004149 }
4150
4151 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004152 sizeof(xmlRelaxNGDefinePtr
4153 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004154 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004155 xmlRngPErrMemory(ctxt, "building group\n");
4156 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004157 }
4158 i = 0;
4159 cur = def->attrs;
4160 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004161 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4162 i++;
4163 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004164 }
4165 cur = def->content;
4166 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004167 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4168 i++;
4169 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004170 }
4171
Daniel Veillard4c004142003-10-07 11:33:24 +00004172 for (i = 0; i < nbchild; i++) {
4173 if (list[i] == NULL)
4174 continue;
4175 for (j = 0; j < i; j++) {
4176 if (list[j] == NULL)
4177 continue;
4178 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4179 if (ret == 0) {
4180 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4181 "Attributes conflicts in group\n", NULL, NULL);
4182 }
4183 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004184 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004185 for (i = 0; i < nbchild; i++) {
4186 if (list[i] != NULL)
4187 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004188 }
4189
4190 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004191 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004192}
4193
4194/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004195 * xmlRelaxNGComputeInterleaves:
4196 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004197 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004198 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004199 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004200 * A lot of work for preprocessing interleave definitions
4201 * is potentially needed to get a decent execution speed at runtime
4202 * - trying to get a total order on the element nodes generated
4203 * by the interleaves, order the list of interleave definitions
4204 * following that order.
4205 * - if <text/> is used to handle mixed content, it is better to
4206 * flag this in the define and simplify the runtime checking
4207 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004208 */
4209static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004210xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004211 xmlRelaxNGParserCtxtPtr ctxt,
4212 xmlChar * name ATTRIBUTE_UNUSED)
4213{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004214 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004215
Daniel Veillardfd573f12003-03-16 17:52:32 +00004216 xmlRelaxNGPartitionPtr partitions = NULL;
4217 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4218 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004219 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004220 int nbgroups = 0;
4221 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004222 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004223 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004224
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004225 /*
4226 * Don't run that check in case of error. Infinite recursion
4227 * becomes possible.
4228 */
4229 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004230 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004231
Daniel Veillardfd573f12003-03-16 17:52:32 +00004232#ifdef DEBUG_INTERLEAVE
4233 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004234 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004235#endif
4236 cur = def->content;
4237 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004238 nbchild++;
4239 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004240 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004241
Daniel Veillardfd573f12003-03-16 17:52:32 +00004242#ifdef DEBUG_INTERLEAVE
4243 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4244#endif
4245 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004246 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004247 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004248 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004249 cur = def->content;
4250 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004251 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4252 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4253 if (groups[nbgroups] == NULL)
4254 goto error;
4255 if (cur->type == XML_RELAXNG_TEXT)
4256 is_mixed++;
4257 groups[nbgroups]->rule = cur;
4258 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4259 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4260 nbgroups++;
4261 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004262 }
4263#ifdef DEBUG_INTERLEAVE
4264 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4265#endif
4266
4267 /*
4268 * Let's check that all rules makes a partitions according to 7.4
4269 */
4270 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004271 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004272 if (partitions == NULL)
4273 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004274 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004275 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004276 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004277 for (i = 0; i < nbgroups; i++) {
4278 group = groups[i];
4279 for (j = i + 1; j < nbgroups; j++) {
4280 if (groups[j] == NULL)
4281 continue;
4282
4283 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4284 groups[j]->defs);
4285 if (ret == 0) {
4286 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4287 "Element or text conflicts in interleave\n",
4288 NULL, NULL);
4289 }
4290 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4291 groups[j]->attrs);
4292 if (ret == 0) {
4293 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4294 "Attributes conflicts in interleave\n", NULL,
4295 NULL);
4296 }
4297 }
4298 tmp = group->defs;
4299 if ((tmp != NULL) && (*tmp != NULL)) {
4300 while (*tmp != NULL) {
4301 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4302 res = xmlHashAddEntry2(partitions->triage,
4303 BAD_CAST "#text", NULL,
4304 (void *) (long) (i + 1));
4305 if (res != 0)
4306 is_determinist = -1;
4307 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4308 ((*tmp)->name != NULL)) {
4309 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4310 res = xmlHashAddEntry2(partitions->triage,
4311 (*tmp)->name, NULL,
4312 (void *) (long) (i + 1));
4313 else
4314 res = xmlHashAddEntry2(partitions->triage,
4315 (*tmp)->name, (*tmp)->ns,
4316 (void *) (long) (i + 1));
4317 if (res != 0)
4318 is_determinist = -1;
4319 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4320 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4321 res = xmlHashAddEntry2(partitions->triage,
4322 BAD_CAST "#any", NULL,
4323 (void *) (long) (i + 1));
4324 else
4325 res = xmlHashAddEntry2(partitions->triage,
4326 BAD_CAST "#any", (*tmp)->ns,
4327 (void *) (long) (i + 1));
4328 if ((*tmp)->nameClass != NULL)
4329 is_determinist = 2;
4330 if (res != 0)
4331 is_determinist = -1;
4332 } else {
4333 is_determinist = -1;
4334 }
4335 tmp++;
4336 }
4337 } else {
4338 is_determinist = 0;
4339 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004340 }
4341 partitions->groups = groups;
4342
4343 /*
4344 * and save the partition list back in the def
4345 */
4346 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004347 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004348 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004349 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004350 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004351 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004352 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004353 return;
4354
Daniel Veillard4c004142003-10-07 11:33:24 +00004355 error:
4356 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004357 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004358 for (i = 0; i < nbgroups; i++)
4359 if (groups[i] != NULL) {
4360 if (groups[i]->defs != NULL)
4361 xmlFree(groups[i]->defs);
4362 xmlFree(groups[i]);
4363 }
4364 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004365 }
4366 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004367}
4368
4369/**
4370 * xmlRelaxNGParseInterleave:
4371 * @ctxt: a Relax-NG parser context
4372 * @node: the data node.
4373 *
4374 * parse the content of a RelaxNG interleave node.
4375 *
4376 * Returns the definition pointer or NULL in case of error
4377 */
4378static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004379xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4380{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004381 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004382 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004383 xmlNodePtr child;
4384
Daniel Veillardfd573f12003-03-16 17:52:32 +00004385 def = xmlRelaxNGNewDefine(ctxt, node);
4386 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004387 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004388 }
4389 def->type = XML_RELAXNG_INTERLEAVE;
4390
4391 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004392 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004393 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004394 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004395 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004396 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004397
Daniel Veillard4c004142003-10-07 11:33:24 +00004398 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4399 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4400 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4401 "Failed to add %s to hash table\n",
4402 (const xmlChar *) name, NULL);
4403 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004404 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004405 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004406 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004407 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4408 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004409 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004410 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004411 if (IS_RELAXNG(child, "element")) {
4412 cur = xmlRelaxNGParseElement(ctxt, child);
4413 } else {
4414 cur = xmlRelaxNGParsePattern(ctxt, child);
4415 }
4416 if (cur != NULL) {
4417 cur->parent = def;
4418 if (last == NULL) {
4419 def->content = last = cur;
4420 } else {
4421 last->next = cur;
4422 last = cur;
4423 }
4424 }
4425 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004426 }
4427
Daniel Veillard4c004142003-10-07 11:33:24 +00004428 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004429}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004430
4431/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004432 * xmlRelaxNGParseInclude:
4433 * @ctxt: a Relax-NG parser context
4434 * @node: the include node
4435 *
4436 * Integrate the content of an include node in the current grammar
4437 *
4438 * Returns 0 in case of success or -1 in case of error
4439 */
4440static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004441xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4442{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004443 xmlRelaxNGIncludePtr incl;
4444 xmlNodePtr root;
4445 int ret = 0, tmp;
4446
4447 incl = node->_private;
4448 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004449 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4450 "Include node has no data\n", NULL, NULL);
4451 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004452 }
4453 root = xmlDocGetRootElement(incl->doc);
4454 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004455 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4456 NULL, NULL);
4457 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004458 }
4459 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004460 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4461 "Include document root is not a grammar\n", NULL, NULL);
4462 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004463 }
4464
4465 /*
4466 * Merge the definition from both the include and the internal list
4467 */
4468 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004469 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4470 if (tmp != 0)
4471 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004472 }
4473 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004474 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4475 if (tmp != 0)
4476 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004477 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004478 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004479}
4480
4481/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004482 * xmlRelaxNGParseDefine:
4483 * @ctxt: a Relax-NG parser context
4484 * @node: the define node
4485 *
4486 * parse the content of a RelaxNG define element node.
4487 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004488 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004489 */
4490static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004491xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4492{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004493 xmlChar *name;
4494 int ret = 0, tmp;
4495 xmlRelaxNGDefinePtr def;
4496 const xmlChar *olddefine;
4497
4498 name = xmlGetProp(node, BAD_CAST "name");
4499 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004500 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4501 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004502 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004503 xmlRelaxNGNormExtSpace(name);
4504 if (xmlValidateNCName(name, 0)) {
4505 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4506 "define name '%s' is not an NCName\n", name, NULL);
4507 }
4508 def = xmlRelaxNGNewDefine(ctxt, node);
4509 if (def == NULL) {
4510 xmlFree(name);
4511 return (-1);
4512 }
4513 def->type = XML_RELAXNG_DEF;
4514 def->name = name;
4515 if (node->children == NULL) {
4516 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4517 "define has no children\n", NULL, NULL);
4518 } else {
4519 olddefine = ctxt->define;
4520 ctxt->define = name;
4521 def->content =
4522 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4523 ctxt->define = olddefine;
4524 }
4525 if (ctxt->grammar->defs == NULL)
4526 ctxt->grammar->defs = xmlHashCreate(10);
4527 if (ctxt->grammar->defs == NULL) {
4528 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4529 "Could not create definition hash\n", NULL, NULL);
4530 ret = -1;
4531 } else {
4532 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4533 if (tmp < 0) {
4534 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004535
Daniel Veillard4c004142003-10-07 11:33:24 +00004536 prev = xmlHashLookup(ctxt->grammar->defs, name);
4537 if (prev == NULL) {
4538 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4539 "Internal error on define aggregation of %s\n",
4540 name, NULL);
4541 ret = -1;
4542 } else {
4543 while (prev->nextHash != NULL)
4544 prev = prev->nextHash;
4545 prev->nextHash = def;
4546 }
4547 }
4548 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004549 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004550 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004551}
4552
4553/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004554 * xmlRelaxNGProcessExternalRef:
4555 * @ctxt: the parser context
4556 * @node: the externlRef node
4557 *
4558 * Process and compile an externlRef node
4559 *
4560 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4561 */
4562static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004563xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4564{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004565 xmlRelaxNGDocumentPtr docu;
4566 xmlNodePtr root, tmp;
4567 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004568 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004569 xmlRelaxNGDefinePtr def;
4570
4571 docu = node->_private;
4572 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004573 def = xmlRelaxNGNewDefine(ctxt, node);
4574 if (def == NULL)
4575 return (NULL);
4576 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004577
Daniel Veillard4c004142003-10-07 11:33:24 +00004578 if (docu->content == NULL) {
4579 /*
4580 * Then do the parsing for good
4581 */
4582 root = xmlDocGetRootElement(docu->doc);
4583 if (root == NULL) {
4584 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4585 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4586 NULL);
4587 return (NULL);
4588 }
4589 /*
4590 * ns transmission rules
4591 */
4592 ns = xmlGetProp(root, BAD_CAST "ns");
4593 if (ns == NULL) {
4594 tmp = node;
4595 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4596 ns = xmlGetProp(tmp, BAD_CAST "ns");
4597 if (ns != NULL) {
4598 break;
4599 }
4600 tmp = tmp->parent;
4601 }
4602 if (ns != NULL) {
4603 xmlSetProp(root, BAD_CAST "ns", ns);
4604 newNs = 1;
4605 xmlFree(ns);
4606 }
4607 } else {
4608 xmlFree(ns);
4609 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004610
Daniel Veillard4c004142003-10-07 11:33:24 +00004611 /*
4612 * Parsing to get a precompiled schemas.
4613 */
4614 oldflags = ctxt->flags;
4615 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4616 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4617 ctxt->flags = oldflags;
4618 if ((docu->schema != NULL) &&
4619 (docu->schema->topgrammar != NULL)) {
4620 docu->content = docu->schema->topgrammar->start;
4621 }
4622
4623 /*
4624 * the externalRef may be reused in a different ns context
4625 */
4626 if (newNs == 1) {
4627 xmlUnsetProp(root, BAD_CAST "ns");
4628 }
4629 }
4630 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004631 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004632 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004633 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004634 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004635}
4636
4637/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004638 * xmlRelaxNGParsePattern:
4639 * @ctxt: a Relax-NG parser context
4640 * @node: the pattern node.
4641 *
4642 * parse the content of a RelaxNG pattern node.
4643 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004644 * Returns the definition pointer or NULL in case of error or if no
4645 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004646 */
4647static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004648xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4649{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004650 xmlRelaxNGDefinePtr def = NULL;
4651
Daniel Veillardd2298792003-02-14 16:54:11 +00004652 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004653 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004654 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004655 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004656 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004657 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004658 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004659 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004660 def = xmlRelaxNGNewDefine(ctxt, node);
4661 if (def == NULL)
4662 return (NULL);
4663 def->type = XML_RELAXNG_EMPTY;
4664 if (node->children != NULL) {
4665 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4666 "empty: had a child node\n", NULL, NULL);
4667 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004668 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004669 def = xmlRelaxNGNewDefine(ctxt, node);
4670 if (def == NULL)
4671 return (NULL);
4672 def->type = XML_RELAXNG_TEXT;
4673 if (node->children != NULL) {
4674 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4675 "text: had a child node\n", NULL, NULL);
4676 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004677 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004678 def = xmlRelaxNGNewDefine(ctxt, node);
4679 if (def == NULL)
4680 return (NULL);
4681 def->type = XML_RELAXNG_ZEROORMORE;
4682 if (node->children == NULL) {
4683 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4684 "Element %s is empty\n", node->name, NULL);
4685 } else {
4686 def->content =
4687 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4688 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004689 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004690 def = xmlRelaxNGNewDefine(ctxt, node);
4691 if (def == NULL)
4692 return (NULL);
4693 def->type = XML_RELAXNG_ONEORMORE;
4694 if (node->children == NULL) {
4695 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4696 "Element %s is empty\n", node->name, NULL);
4697 } else {
4698 def->content =
4699 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4700 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004701 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004702 def = xmlRelaxNGNewDefine(ctxt, node);
4703 if (def == NULL)
4704 return (NULL);
4705 def->type = XML_RELAXNG_OPTIONAL;
4706 if (node->children == NULL) {
4707 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4708 "Element %s is empty\n", node->name, NULL);
4709 } else {
4710 def->content =
4711 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4712 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004713 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004714 def = xmlRelaxNGNewDefine(ctxt, node);
4715 if (def == NULL)
4716 return (NULL);
4717 def->type = XML_RELAXNG_CHOICE;
4718 if (node->children == NULL) {
4719 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4720 "Element %s is empty\n", node->name, NULL);
4721 } else {
4722 def->content =
4723 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4724 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004725 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004726 def = xmlRelaxNGNewDefine(ctxt, node);
4727 if (def == NULL)
4728 return (NULL);
4729 def->type = XML_RELAXNG_GROUP;
4730 if (node->children == NULL) {
4731 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4732 "Element %s is empty\n", node->name, NULL);
4733 } else {
4734 def->content =
4735 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4736 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004737 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004738 def = xmlRelaxNGNewDefine(ctxt, node);
4739 if (def == NULL)
4740 return (NULL);
4741 def->type = XML_RELAXNG_REF;
4742 def->name = xmlGetProp(node, BAD_CAST "name");
4743 if (def->name == NULL) {
4744 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4745 NULL, NULL);
4746 } else {
4747 xmlRelaxNGNormExtSpace(def->name);
4748 if (xmlValidateNCName(def->name, 0)) {
4749 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4750 "ref name '%s' is not an NCName\n", def->name,
4751 NULL);
4752 }
4753 }
4754 if (node->children != NULL) {
4755 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4756 NULL, NULL);
4757 }
4758 if (ctxt->grammar->refs == NULL)
4759 ctxt->grammar->refs = xmlHashCreate(10);
4760 if (ctxt->grammar->refs == NULL) {
4761 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4762 "Could not create references hash\n", NULL, NULL);
4763 def = NULL;
4764 } else {
4765 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004766
Daniel Veillard4c004142003-10-07 11:33:24 +00004767 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4768 if (tmp < 0) {
4769 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004770
Daniel Veillard4c004142003-10-07 11:33:24 +00004771 prev = (xmlRelaxNGDefinePtr)
4772 xmlHashLookup(ctxt->grammar->refs, def->name);
4773 if (prev == NULL) {
4774 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004775 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4776 "Error refs definitions '%s'\n",
4777 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004778 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004779 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4780 "Error refs definitions\n",
4781 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004782 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004783 def = NULL;
4784 } else {
4785 def->nextHash = prev->nextHash;
4786 prev->nextHash = def;
4787 }
4788 }
4789 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004790 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004791 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004792 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004793 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004794 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004795 def = xmlRelaxNGNewDefine(ctxt, node);
4796 if (def == NULL)
4797 return (NULL);
4798 def->type = XML_RELAXNG_LIST;
4799 if (node->children == NULL) {
4800 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4801 "Element %s is empty\n", node->name, NULL);
4802 } else {
4803 def->content =
4804 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4805 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004806 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004807 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004808 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004809 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004810 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004811 def = xmlRelaxNGNewDefine(ctxt, node);
4812 if (def == NULL)
4813 return (NULL);
4814 def->type = XML_RELAXNG_NOT_ALLOWED;
4815 if (node->children != NULL) {
4816 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4817 "xmlRelaxNGParse: notAllowed element is not empty\n",
4818 NULL, NULL);
4819 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004820 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004821 xmlRelaxNGGrammarPtr grammar, old;
4822 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004823
Daniel Veillardc482e262003-02-26 14:48:48 +00004824#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004825 xmlGenericError(xmlGenericErrorContext,
4826 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004827#endif
4828
Daniel Veillard4c004142003-10-07 11:33:24 +00004829 oldparent = ctxt->parentgrammar;
4830 old = ctxt->grammar;
4831 ctxt->parentgrammar = old;
4832 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4833 if (old != NULL) {
4834 ctxt->grammar = old;
4835 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004836#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004837 if (grammar != NULL) {
4838 grammar->next = old->next;
4839 old->next = grammar;
4840 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004841#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004842 }
4843 if (grammar != NULL)
4844 def = grammar->start;
4845 else
4846 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004847 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004848 if (ctxt->parentgrammar == NULL) {
4849 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4850 "Use of parentRef without a parent grammar\n", NULL,
4851 NULL);
4852 return (NULL);
4853 }
4854 def = xmlRelaxNGNewDefine(ctxt, node);
4855 if (def == NULL)
4856 return (NULL);
4857 def->type = XML_RELAXNG_PARENTREF;
4858 def->name = xmlGetProp(node, BAD_CAST "name");
4859 if (def->name == NULL) {
4860 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4861 "parentRef has no name\n", NULL, NULL);
4862 } else {
4863 xmlRelaxNGNormExtSpace(def->name);
4864 if (xmlValidateNCName(def->name, 0)) {
4865 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4866 "parentRef name '%s' is not an NCName\n",
4867 def->name, NULL);
4868 }
4869 }
4870 if (node->children != NULL) {
4871 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4872 "parentRef is not empty\n", NULL, NULL);
4873 }
4874 if (ctxt->parentgrammar->refs == NULL)
4875 ctxt->parentgrammar->refs = xmlHashCreate(10);
4876 if (ctxt->parentgrammar->refs == NULL) {
4877 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4878 "Could not create references hash\n", NULL, NULL);
4879 def = NULL;
4880 } else if (def->name != NULL) {
4881 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00004882
Daniel Veillard4c004142003-10-07 11:33:24 +00004883 tmp =
4884 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4885 if (tmp < 0) {
4886 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00004887
Daniel Veillard4c004142003-10-07 11:33:24 +00004888 prev = (xmlRelaxNGDefinePtr)
4889 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4890 if (prev == NULL) {
4891 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4892 "Internal error parentRef definitions '%s'\n",
4893 def->name, NULL);
4894 def = NULL;
4895 } else {
4896 def->nextHash = prev->nextHash;
4897 prev->nextHash = def;
4898 }
4899 }
4900 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004901 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004902 if (node->children == NULL) {
4903 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4904 NULL, NULL);
4905 def = NULL;
4906 } else {
4907 def = xmlRelaxNGParseInterleave(ctxt, node);
4908 if (def != NULL) {
4909 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004910
Daniel Veillard4c004142003-10-07 11:33:24 +00004911 if ((def->content != NULL) && (def->content->next != NULL)) {
4912 tmp = xmlRelaxNGNewDefine(ctxt, node);
4913 if (tmp != NULL) {
4914 tmp->type = XML_RELAXNG_GROUP;
4915 tmp->content = def->content;
4916 def->content = tmp;
4917 }
4918 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004919
Daniel Veillard4c004142003-10-07 11:33:24 +00004920 tmp = xmlRelaxNGNewDefine(ctxt, node);
4921 if (tmp == NULL)
4922 return (def);
4923 tmp->type = XML_RELAXNG_TEXT;
4924 tmp->next = def->content;
4925 def->content = tmp;
4926 }
4927 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004928 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004929 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4930 "Unexpected node %s is not a pattern\n", node->name,
4931 NULL);
4932 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004933 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004934 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004935}
4936
4937/**
4938 * xmlRelaxNGParseAttribute:
4939 * @ctxt: a Relax-NG parser context
4940 * @node: the element node
4941 *
4942 * parse the content of a RelaxNG attribute node.
4943 *
4944 * Returns the definition pointer or NULL in case of error.
4945 */
4946static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004947xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4948{
Daniel Veillardd2298792003-02-14 16:54:11 +00004949 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004950 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004951 int old_flags;
4952
Daniel Veillardfd573f12003-03-16 17:52:32 +00004953 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004954 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004955 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004956 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004957 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004958 child = node->children;
4959 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004960 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
4961 "xmlRelaxNGParseattribute: attribute has no children\n",
4962 NULL, NULL);
4963 return (ret);
4964 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004965 old_flags = ctxt->flags;
4966 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004967 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4968 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004969 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004970
Daniel Veillardd2298792003-02-14 16:54:11 +00004971 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004972 cur = xmlRelaxNGParsePattern(ctxt, child);
4973 if (cur != NULL) {
4974 switch (cur->type) {
4975 case XML_RELAXNG_EMPTY:
4976 case XML_RELAXNG_NOT_ALLOWED:
4977 case XML_RELAXNG_TEXT:
4978 case XML_RELAXNG_ELEMENT:
4979 case XML_RELAXNG_DATATYPE:
4980 case XML_RELAXNG_VALUE:
4981 case XML_RELAXNG_LIST:
4982 case XML_RELAXNG_REF:
4983 case XML_RELAXNG_PARENTREF:
4984 case XML_RELAXNG_EXTERNALREF:
4985 case XML_RELAXNG_DEF:
4986 case XML_RELAXNG_ONEORMORE:
4987 case XML_RELAXNG_ZEROORMORE:
4988 case XML_RELAXNG_OPTIONAL:
4989 case XML_RELAXNG_CHOICE:
4990 case XML_RELAXNG_GROUP:
4991 case XML_RELAXNG_INTERLEAVE:
4992 case XML_RELAXNG_ATTRIBUTE:
4993 ret->content = cur;
4994 cur->parent = ret;
4995 break;
4996 case XML_RELAXNG_START:
4997 case XML_RELAXNG_PARAM:
4998 case XML_RELAXNG_EXCEPT:
4999 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5000 "attribute has invalid content\n", NULL,
5001 NULL);
5002 break;
5003 case XML_RELAXNG_NOOP:
5004 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5005 "RNG Internal error, noop found in attribute\n",
5006 NULL, NULL);
5007 break;
5008 }
5009 }
5010 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005011 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005012 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005013 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5014 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005015 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005016 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005017 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005018}
5019
5020/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005021 * xmlRelaxNGParseExceptNameClass:
5022 * @ctxt: a Relax-NG parser context
5023 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005024 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005025 *
5026 * parse the content of a RelaxNG nameClass node.
5027 *
5028 * Returns the definition pointer or NULL in case of error.
5029 */
5030static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005031xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005032 xmlNodePtr node, int attr)
5033{
Daniel Veillard144fae12003-02-03 13:17:57 +00005034 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5035 xmlNodePtr child;
5036
Daniel Veillardd2298792003-02-14 16:54:11 +00005037 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005038 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5039 "Expecting an except node\n", NULL, NULL);
5040 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005041 }
5042 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005043 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5044 "exceptNameClass allows only a single except node\n",
5045 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005046 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005047 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005048 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5049 NULL, NULL);
5050 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005051 }
5052
Daniel Veillardfd573f12003-03-16 17:52:32 +00005053 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005054 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005055 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005056 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005057 child = node->children;
5058 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005059 cur = xmlRelaxNGNewDefine(ctxt, child);
5060 if (cur == NULL)
5061 break;
5062 if (attr)
5063 cur->type = XML_RELAXNG_ATTRIBUTE;
5064 else
5065 cur->type = XML_RELAXNG_ELEMENT;
5066
Daniel Veillard419a7682003-02-03 23:22:49 +00005067 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005068 if (last == NULL) {
5069 ret->content = cur;
5070 } else {
5071 last->next = cur;
5072 }
5073 last = cur;
5074 }
5075 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005076 }
5077
Daniel Veillard4c004142003-10-07 11:33:24 +00005078 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005079}
5080
5081/**
5082 * xmlRelaxNGParseNameClass:
5083 * @ctxt: a Relax-NG parser context
5084 * @node: the nameClass node
5085 * @def: the current definition
5086 *
5087 * parse the content of a RelaxNG nameClass node.
5088 *
5089 * Returns the definition pointer or NULL in case of error.
5090 */
5091static xmlRelaxNGDefinePtr
5092xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005093 xmlRelaxNGDefinePtr def)
5094{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005095 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005096 xmlChar *val;
5097
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005098 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005099 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005100 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005101 if ((def->type != XML_RELAXNG_ELEMENT) &&
5102 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5103 ret = xmlRelaxNGNewDefine(ctxt, node);
5104 if (ret == NULL)
5105 return (NULL);
5106 ret->parent = def;
5107 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5108 ret->type = XML_RELAXNG_ATTRIBUTE;
5109 else
5110 ret->type = XML_RELAXNG_ELEMENT;
5111 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005112 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005113 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005114 val = xmlNodeGetContent(node);
5115 xmlRelaxNGNormExtSpace(val);
5116 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005117 if (node->parent != NULL)
5118 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5119 "Element %s name '%s' is not an NCName\n",
5120 node->parent->name, val);
5121 else
5122 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5123 "name '%s' is not an NCName\n",
5124 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005125 }
5126 ret->name = val;
5127 val = xmlGetProp(node, BAD_CAST "ns");
5128 ret->ns = val;
5129 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5130 (val != NULL) &&
5131 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005132 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005133 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005134 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005135 }
5136 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5137 (val != NULL) &&
5138 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005139 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5140 "Attribute with QName 'xmlns' is not allowed\n",
5141 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005142 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005143 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005144 ret->name = NULL;
5145 ret->ns = NULL;
5146 if (node->children != NULL) {
5147 ret->nameClass =
5148 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5149 (def->type ==
5150 XML_RELAXNG_ATTRIBUTE));
5151 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005152 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005153 ret->name = NULL;
5154 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5155 if (ret->ns == NULL) {
5156 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5157 "nsName has no ns attribute\n", NULL, NULL);
5158 }
5159 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5160 (ret->ns != NULL) &&
5161 (xmlStrEqual
5162 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5163 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5164 "Attribute with namespace '%s' is not allowed\n",
5165 ret->ns, NULL);
5166 }
5167 if (node->children != NULL) {
5168 ret->nameClass =
5169 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5170 (def->type ==
5171 XML_RELAXNG_ATTRIBUTE));
5172 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005173 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005174 xmlNodePtr child;
5175 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005176
Daniel Veillard4c004142003-10-07 11:33:24 +00005177 ret = xmlRelaxNGNewDefine(ctxt, node);
5178 if (ret == NULL)
5179 return (NULL);
5180 ret->parent = def;
5181 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005182
Daniel Veillard4c004142003-10-07 11:33:24 +00005183 if (node->children == NULL) {
5184 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5185 "Element choice is empty\n", NULL, NULL);
5186 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005187
Daniel Veillard4c004142003-10-07 11:33:24 +00005188 child = node->children;
5189 while (child != NULL) {
5190 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5191 if (tmp != NULL) {
5192 if (last == NULL) {
5193 last = ret->nameClass = tmp;
5194 } else {
5195 last->next = tmp;
5196 last = tmp;
5197 }
5198 }
5199 child = child->next;
5200 }
5201 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005202 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005203 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5204 "expecting name, anyName, nsName or choice : got %s\n",
5205 node->name, NULL);
5206 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005207 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005208 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005209 if (def->nameClass == NULL) {
5210 def->nameClass = ret;
5211 } else {
5212 tmp = def->nameClass;
5213 while (tmp->next != NULL) {
5214 tmp = tmp->next;
5215 }
5216 tmp->next = ret;
5217 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005218 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005219 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005220}
5221
5222/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005223 * xmlRelaxNGParseElement:
5224 * @ctxt: a Relax-NG parser context
5225 * @node: the element node
5226 *
5227 * parse the content of a RelaxNG element node.
5228 *
5229 * Returns the definition pointer or NULL in case of error.
5230 */
5231static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005232xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5233{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005234 xmlRelaxNGDefinePtr ret, cur, last;
5235 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005236 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005237
Daniel Veillardfd573f12003-03-16 17:52:32 +00005238 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005239 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005240 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005241 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005242 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005243 child = node->children;
5244 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005245 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5246 "xmlRelaxNGParseElement: element has no children\n",
5247 NULL, NULL);
5248 return (ret);
5249 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005250 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5251 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005252 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005253
Daniel Veillard6eadf632003-01-23 18:29:16 +00005254 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005255 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5256 "xmlRelaxNGParseElement: element has no content\n",
5257 NULL, NULL);
5258 return (ret);
5259 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005260 olddefine = ctxt->define;
5261 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005262 last = NULL;
5263 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005264 cur = xmlRelaxNGParsePattern(ctxt, child);
5265 if (cur != NULL) {
5266 cur->parent = ret;
5267 switch (cur->type) {
5268 case XML_RELAXNG_EMPTY:
5269 case XML_RELAXNG_NOT_ALLOWED:
5270 case XML_RELAXNG_TEXT:
5271 case XML_RELAXNG_ELEMENT:
5272 case XML_RELAXNG_DATATYPE:
5273 case XML_RELAXNG_VALUE:
5274 case XML_RELAXNG_LIST:
5275 case XML_RELAXNG_REF:
5276 case XML_RELAXNG_PARENTREF:
5277 case XML_RELAXNG_EXTERNALREF:
5278 case XML_RELAXNG_DEF:
5279 case XML_RELAXNG_ZEROORMORE:
5280 case XML_RELAXNG_ONEORMORE:
5281 case XML_RELAXNG_OPTIONAL:
5282 case XML_RELAXNG_CHOICE:
5283 case XML_RELAXNG_GROUP:
5284 case XML_RELAXNG_INTERLEAVE:
5285 if (last == NULL) {
5286 ret->content = last = cur;
5287 } else {
5288 if ((last->type == XML_RELAXNG_ELEMENT) &&
5289 (ret->content == last)) {
5290 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5291 if (ret->content != NULL) {
5292 ret->content->type = XML_RELAXNG_GROUP;
5293 ret->content->content = last;
5294 } else {
5295 ret->content = last;
5296 }
5297 }
5298 last->next = cur;
5299 last = cur;
5300 }
5301 break;
5302 case XML_RELAXNG_ATTRIBUTE:
5303 cur->next = ret->attrs;
5304 ret->attrs = cur;
5305 break;
5306 case XML_RELAXNG_START:
5307 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5308 "RNG Internal error, start found in element\n",
5309 NULL, NULL);
5310 break;
5311 case XML_RELAXNG_PARAM:
5312 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5313 "RNG Internal error, param found in element\n",
5314 NULL, NULL);
5315 break;
5316 case XML_RELAXNG_EXCEPT:
5317 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5318 "RNG Internal error, except found in element\n",
5319 NULL, NULL);
5320 break;
5321 case XML_RELAXNG_NOOP:
5322 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5323 "RNG Internal error, noop found in element\n",
5324 NULL, NULL);
5325 break;
5326 }
5327 }
5328 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005329 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005330 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005331 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005332}
5333
5334/**
5335 * xmlRelaxNGParsePatterns:
5336 * @ctxt: a Relax-NG parser context
5337 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005338 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005339 *
5340 * parse the content of a RelaxNG start node.
5341 *
5342 * Returns the definition pointer or NULL in case of error.
5343 */
5344static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005345xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005346 int group)
5347{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005348 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005349
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005350 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005351 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005352 if (IS_RELAXNG(nodes, "element")) {
5353 cur = xmlRelaxNGParseElement(ctxt, nodes);
5354 if (def == NULL) {
5355 def = last = cur;
5356 } else {
5357 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5358 (def == last)) {
5359 def = xmlRelaxNGNewDefine(ctxt, nodes);
5360 def->type = XML_RELAXNG_GROUP;
5361 def->content = last;
5362 }
5363 last->next = cur;
5364 last = cur;
5365 }
5366 cur->parent = parent;
5367 } else {
5368 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5369 if (cur != NULL) {
5370 if (def == NULL) {
5371 def = last = cur;
5372 } else {
5373 last->next = cur;
5374 last = cur;
5375 }
5376 }
5377 }
5378 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005379 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005380 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005381}
5382
5383/**
5384 * xmlRelaxNGParseStart:
5385 * @ctxt: a Relax-NG parser context
5386 * @nodes: start children nodes
5387 *
5388 * parse the content of a RelaxNG start node.
5389 *
5390 * Returns 0 in case of success, -1 in case of error
5391 */
5392static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005393xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5394{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005395 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005396 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005397
Daniel Veillardd2298792003-02-14 16:54:11 +00005398 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005399 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5400 NULL, NULL);
5401 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005402 }
5403 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005404 def = xmlRelaxNGNewDefine(ctxt, nodes);
5405 if (def == NULL)
5406 return (-1);
5407 def->type = XML_RELAXNG_EMPTY;
5408 if (nodes->children != NULL) {
5409 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5410 "element empty is not empty\n", NULL, NULL);
5411 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005412 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005413 def = xmlRelaxNGNewDefine(ctxt, nodes);
5414 if (def == NULL)
5415 return (-1);
5416 def->type = XML_RELAXNG_NOT_ALLOWED;
5417 if (nodes->children != NULL) {
5418 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5419 "element notAllowed is not empty\n", NULL, NULL);
5420 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005421 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005422 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005423 }
5424 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005425 last = ctxt->grammar->start;
5426 while (last->next != NULL)
5427 last = last->next;
5428 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005429 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005430 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005431 }
5432 nodes = nodes->next;
5433 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005434 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5435 "start more than one children\n", NULL, NULL);
5436 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005437 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005438 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005439}
5440
5441/**
5442 * xmlRelaxNGParseGrammarContent:
5443 * @ctxt: a Relax-NG parser context
5444 * @nodes: grammar children nodes
5445 *
5446 * parse the content of a RelaxNG grammar node.
5447 *
5448 * Returns 0 in case of success, -1 in case of error
5449 */
5450static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005451xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5452 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005453{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005454 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005455
5456 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005457 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5458 "grammar has no children\n", NULL, NULL);
5459 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005460 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005461 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005462 if (IS_RELAXNG(nodes, "start")) {
5463 if (nodes->children == NULL) {
5464 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5465 "start has no children\n", NULL, NULL);
5466 } else {
5467 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5468 if (tmp != 0)
5469 ret = -1;
5470 }
5471 } else if (IS_RELAXNG(nodes, "define")) {
5472 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5473 if (tmp != 0)
5474 ret = -1;
5475 } else if (IS_RELAXNG(nodes, "include")) {
5476 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5477 if (tmp != 0)
5478 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005479 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005480 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5481 "grammar has unexpected child %s\n", nodes->name,
5482 NULL);
5483 ret = -1;
5484 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005485 nodes = nodes->next;
5486 }
5487 return (ret);
5488}
5489
5490/**
5491 * xmlRelaxNGCheckReference:
5492 * @ref: the ref
5493 * @ctxt: a Relax-NG parser context
5494 * @name: the name associated to the defines
5495 *
5496 * Applies the 4.17. combine attribute rule for all the define
5497 * element of a given grammar using the same name.
5498 */
5499static void
5500xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005501 xmlRelaxNGParserCtxtPtr ctxt,
5502 const xmlChar * name)
5503{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005504 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005505 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005506
5507 grammar = ctxt->grammar;
5508 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005509 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5510 "Internal error: no grammar in CheckReference %s\n",
5511 name, NULL);
5512 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005513 }
5514 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005515 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5516 "Internal error: reference has content in CheckReference %s\n",
5517 name, NULL);
5518 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005519 }
5520 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005521 def = xmlHashLookup(grammar->defs, name);
5522 if (def != NULL) {
5523 cur = ref;
5524 while (cur != NULL) {
5525 cur->content = def;
5526 cur = cur->nextHash;
5527 }
5528 } else {
5529 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5530 "Reference %s has no matching definition\n", name,
5531 NULL);
5532 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005533 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005534 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5535 "Reference %s has no matching definition\n", name,
5536 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005537 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005538}
5539
5540/**
5541 * xmlRelaxNGCheckCombine:
5542 * @define: the define(s) list
5543 * @ctxt: a Relax-NG parser context
5544 * @name: the name associated to the defines
5545 *
5546 * Applies the 4.17. combine attribute rule for all the define
5547 * element of a given grammar using the same name.
5548 */
5549static void
5550xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005551 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5552{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005553 xmlChar *combine;
5554 int choiceOrInterleave = -1;
5555 int missing = 0;
5556 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5557
5558 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005559 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005560 cur = define;
5561 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005562 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5563 if (combine != NULL) {
5564 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5565 if (choiceOrInterleave == -1)
5566 choiceOrInterleave = 1;
5567 else if (choiceOrInterleave == 0) {
5568 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5569 "Defines for %s use both 'choice' and 'interleave'\n",
5570 name, NULL);
5571 }
5572 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5573 if (choiceOrInterleave == -1)
5574 choiceOrInterleave = 0;
5575 else if (choiceOrInterleave == 1) {
5576 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5577 "Defines for %s use both 'choice' and 'interleave'\n",
5578 name, NULL);
5579 }
5580 } else {
5581 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5582 "Defines for %s use unknown combine value '%s''\n",
5583 name, combine);
5584 }
5585 xmlFree(combine);
5586 } else {
5587 if (missing == 0)
5588 missing = 1;
5589 else {
5590 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5591 "Some defines for %s needs the combine attribute\n",
5592 name, NULL);
5593 }
5594 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005595
Daniel Veillard4c004142003-10-07 11:33:24 +00005596 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005597 }
5598#ifdef DEBUG
5599 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005600 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5601 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005602#endif
5603 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005604 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005605 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005606 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005607 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005608 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005609 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005610 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005611 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005612 tmp = define;
5613 last = NULL;
5614 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005615 if (tmp->content != NULL) {
5616 if (tmp->content->next != NULL) {
5617 /*
5618 * we need first to create a wrapper.
5619 */
5620 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5621 if (tmp2 == NULL)
5622 break;
5623 tmp2->type = XML_RELAXNG_GROUP;
5624 tmp2->content = tmp->content;
5625 } else {
5626 tmp2 = tmp->content;
5627 }
5628 if (last == NULL) {
5629 cur->content = tmp2;
5630 } else {
5631 last->next = tmp2;
5632 }
5633 last = tmp2;
5634 }
5635 tmp->content = cur;
5636 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005637 }
5638 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005639 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005640 if (ctxt->interleaves == NULL)
5641 ctxt->interleaves = xmlHashCreate(10);
5642 if (ctxt->interleaves == NULL) {
5643 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5644 "Failed to create interleaves hash table\n", NULL,
5645 NULL);
5646 } else {
5647 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005648
Daniel Veillard4c004142003-10-07 11:33:24 +00005649 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5650 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5651 0) {
5652 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5653 "Failed to add %s to hash table\n",
5654 (const xmlChar *) tmpname, NULL);
5655 }
5656 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005657 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005658}
5659
5660/**
5661 * xmlRelaxNGCombineStart:
5662 * @ctxt: a Relax-NG parser context
5663 * @grammar: the grammar
5664 *
5665 * Applies the 4.17. combine rule for all the start
5666 * element of a given grammar.
5667 */
5668static void
5669xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005670 xmlRelaxNGGrammarPtr grammar)
5671{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005672 xmlRelaxNGDefinePtr starts;
5673 xmlChar *combine;
5674 int choiceOrInterleave = -1;
5675 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005676 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005677
Daniel Veillard2df2de22003-02-17 23:34:33 +00005678 starts = grammar->start;
5679 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005680 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005681 cur = starts;
5682 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005683 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5684 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5685 combine = NULL;
5686 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5687 "Internal error: start element not found\n", NULL,
5688 NULL);
5689 } else {
5690 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5691 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005692
Daniel Veillard4c004142003-10-07 11:33:24 +00005693 if (combine != NULL) {
5694 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5695 if (choiceOrInterleave == -1)
5696 choiceOrInterleave = 1;
5697 else if (choiceOrInterleave == 0) {
5698 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5699 "<start> use both 'choice' and 'interleave'\n",
5700 NULL, NULL);
5701 }
5702 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5703 if (choiceOrInterleave == -1)
5704 choiceOrInterleave = 0;
5705 else if (choiceOrInterleave == 1) {
5706 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5707 "<start> use both 'choice' and 'interleave'\n",
5708 NULL, NULL);
5709 }
5710 } else {
5711 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5712 "<start> uses unknown combine value '%s''\n",
5713 combine, NULL);
5714 }
5715 xmlFree(combine);
5716 } else {
5717 if (missing == 0)
5718 missing = 1;
5719 else {
5720 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5721 "Some <start> element miss the combine attribute\n",
5722 NULL, NULL);
5723 }
5724 }
5725
5726 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005727 }
5728#ifdef DEBUG
5729 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005730 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5731 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005732#endif
5733 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005734 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005735 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005736 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005737 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005738 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005739 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005740 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005741 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005742 cur->content = grammar->start;
5743 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005744 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005745 if (ctxt->interleaves == NULL)
5746 ctxt->interleaves = xmlHashCreate(10);
5747 if (ctxt->interleaves == NULL) {
5748 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5749 "Failed to create interleaves hash table\n", NULL,
5750 NULL);
5751 } else {
5752 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005753
Daniel Veillard4c004142003-10-07 11:33:24 +00005754 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5755 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5756 0) {
5757 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5758 "Failed to add %s to hash table\n",
5759 (const xmlChar *) tmpname, NULL);
5760 }
5761 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005762 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005763}
5764
5765/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005766 * xmlRelaxNGCheckCycles:
5767 * @ctxt: a Relax-NG parser context
5768 * @nodes: grammar children nodes
5769 * @depth: the counter
5770 *
5771 * Check for cycles.
5772 *
5773 * Returns 0 if check passed, and -1 in case of error
5774 */
5775static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005776xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5777 xmlRelaxNGDefinePtr cur, int depth)
5778{
Daniel Veillardd4310742003-02-18 21:12:46 +00005779 int ret = 0;
5780
5781 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005782 if ((cur->type == XML_RELAXNG_REF) ||
5783 (cur->type == XML_RELAXNG_PARENTREF)) {
5784 if (cur->depth == -1) {
5785 cur->depth = depth;
5786 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5787 cur->depth = -2;
5788 } else if (depth == cur->depth) {
5789 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5790 "Detected a cycle in %s references\n",
5791 cur->name, NULL);
5792 return (-1);
5793 }
5794 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5795 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5796 } else {
5797 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5798 }
5799 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005800 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005801 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005802}
5803
5804/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005805 * xmlRelaxNGTryUnlink:
5806 * @ctxt: a Relax-NG parser context
5807 * @cur: the definition to unlink
5808 * @parent: the parent definition
5809 * @prev: the previous sibling definition
5810 *
5811 * Try to unlink a definition. If not possble make it a NOOP
5812 *
5813 * Returns the new prev definition
5814 */
5815static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005816xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5817 xmlRelaxNGDefinePtr cur,
5818 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5819{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005820 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005821 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005822 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005823 if (parent != NULL) {
5824 if (parent->content == cur)
5825 parent->content = cur->next;
5826 else if (parent->attrs == cur)
5827 parent->attrs = cur->next;
5828 else if (parent->nameClass == cur)
5829 parent->nameClass = cur->next;
5830 } else {
5831 cur->type = XML_RELAXNG_NOOP;
5832 prev = cur;
5833 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005834 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005835 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005836}
5837
5838/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005839 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005840 * @ctxt: a Relax-NG parser context
5841 * @nodes: grammar children nodes
5842 *
5843 * Check for simplification of empty and notAllowed
5844 */
5845static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005846xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5847 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5848{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005849 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005850
Daniel Veillardfd573f12003-03-16 17:52:32 +00005851 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005852 if ((cur->type == XML_RELAXNG_REF) ||
5853 (cur->type == XML_RELAXNG_PARENTREF)) {
5854 if (cur->depth != -3) {
5855 cur->depth = -3;
5856 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5857 }
5858 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5859 cur->parent = parent;
5860 if ((parent != NULL) &&
5861 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5862 (parent->type == XML_RELAXNG_LIST) ||
5863 (parent->type == XML_RELAXNG_GROUP) ||
5864 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5865 (parent->type == XML_RELAXNG_ONEORMORE) ||
5866 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5867 parent->type = XML_RELAXNG_NOT_ALLOWED;
5868 break;
5869 }
5870 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5871 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5872 } else
5873 prev = cur;
5874 } else if (cur->type == XML_RELAXNG_EMPTY) {
5875 cur->parent = parent;
5876 if ((parent != NULL) &&
5877 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5878 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5879 parent->type = XML_RELAXNG_EMPTY;
5880 break;
5881 }
5882 if ((parent != NULL) &&
5883 ((parent->type == XML_RELAXNG_GROUP) ||
5884 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5885 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5886 } else
5887 prev = cur;
5888 } else {
5889 cur->parent = parent;
5890 if (cur->content != NULL)
5891 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5892 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5893 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5894 if (cur->nameClass != NULL)
5895 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5896 /*
5897 * On Elements, try to move attribute only generating rules on
5898 * the attrs rules.
5899 */
5900 if (cur->type == XML_RELAXNG_ELEMENT) {
5901 int attronly;
5902 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00005903
Daniel Veillard4c004142003-10-07 11:33:24 +00005904 while (cur->content != NULL) {
5905 attronly =
5906 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5907 if (attronly == 1) {
5908 /*
5909 * migrate cur->content to attrs
5910 */
5911 tmp = cur->content;
5912 cur->content = tmp->next;
5913 tmp->next = cur->attrs;
5914 cur->attrs = tmp;
5915 } else {
5916 /*
5917 * cur->content can generate elements or text
5918 */
5919 break;
5920 }
5921 }
5922 pre = cur->content;
5923 while ((pre != NULL) && (pre->next != NULL)) {
5924 tmp = pre->next;
5925 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5926 if (attronly == 1) {
5927 /*
5928 * migrate tmp to attrs
5929 */
5930 pre->next = tmp->next;
5931 tmp->next = cur->attrs;
5932 cur->attrs = tmp;
5933 } else {
5934 pre = tmp;
5935 }
5936 }
5937 }
5938 /*
5939 * This may result in a simplification
5940 */
5941 if ((cur->type == XML_RELAXNG_GROUP) ||
5942 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5943 if (cur->content == NULL)
5944 cur->type = XML_RELAXNG_EMPTY;
5945 else if (cur->content->next == NULL) {
5946 if ((parent == NULL) && (prev == NULL)) {
5947 cur->type = XML_RELAXNG_NOOP;
5948 } else if (prev == NULL) {
5949 parent->content = cur->content;
5950 cur->content->next = cur->next;
5951 cur = cur->content;
5952 } else {
5953 cur->content->next = cur->next;
5954 prev->next = cur->content;
5955 cur = cur->content;
5956 }
5957 }
5958 }
5959 /*
5960 * the current node may have been transformed back
5961 */
5962 if ((cur->type == XML_RELAXNG_EXCEPT) &&
5963 (cur->content != NULL) &&
5964 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
5965 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5966 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5967 if ((parent != NULL) &&
5968 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5969 (parent->type == XML_RELAXNG_LIST) ||
5970 (parent->type == XML_RELAXNG_GROUP) ||
5971 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5972 (parent->type == XML_RELAXNG_ONEORMORE) ||
5973 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5974 parent->type = XML_RELAXNG_NOT_ALLOWED;
5975 break;
5976 }
5977 if ((parent != NULL) &&
5978 (parent->type == XML_RELAXNG_CHOICE)) {
5979 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5980 } else
5981 prev = cur;
5982 } else if (cur->type == XML_RELAXNG_EMPTY) {
5983 if ((parent != NULL) &&
5984 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5985 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5986 parent->type = XML_RELAXNG_EMPTY;
5987 break;
5988 }
5989 if ((parent != NULL) &&
5990 ((parent->type == XML_RELAXNG_GROUP) ||
5991 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5992 (parent->type == XML_RELAXNG_CHOICE))) {
5993 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5994 } else
5995 prev = cur;
5996 } else {
5997 prev = cur;
5998 }
5999 }
6000 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006001 }
6002}
6003
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006004/**
6005 * xmlRelaxNGGroupContentType:
6006 * @ct1: the first content type
6007 * @ct2: the second content type
6008 *
6009 * Try to group 2 content types
6010 *
6011 * Returns the content type
6012 */
6013static xmlRelaxNGContentType
6014xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006015 xmlRelaxNGContentType ct2)
6016{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006017 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006018 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6019 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006020 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006021 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006022 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006023 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006024 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006025 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6026 return (XML_RELAXNG_CONTENT_COMPLEX);
6027 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006028}
6029
6030/**
6031 * xmlRelaxNGMaxContentType:
6032 * @ct1: the first content type
6033 * @ct2: the second content type
6034 *
6035 * Compute the max content-type
6036 *
6037 * Returns the content type
6038 */
6039static xmlRelaxNGContentType
6040xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006041 xmlRelaxNGContentType ct2)
6042{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006043 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006044 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6045 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006046 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006047 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6048 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006049 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006050 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6051 return (XML_RELAXNG_CONTENT_COMPLEX);
6052 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006053}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006054
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006055/**
6056 * xmlRelaxNGCheckRules:
6057 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006058 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006059 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006060 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006061 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006062 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006063 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006064 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006065 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006066static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006067xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6068 xmlRelaxNGDefinePtr cur, int flags,
6069 xmlRelaxNGType ptype)
6070{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006071 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006072 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006073
Daniel Veillardfd573f12003-03-16 17:52:32 +00006074 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006075 ret = XML_RELAXNG_CONTENT_EMPTY;
6076 if ((cur->type == XML_RELAXNG_REF) ||
6077 (cur->type == XML_RELAXNG_PARENTREF)) {
6078 if (flags & XML_RELAXNG_IN_LIST) {
6079 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6080 "Found forbidden pattern list//ref\n", NULL,
6081 NULL);
6082 }
6083 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6084 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6085 "Found forbidden pattern data/except//ref\n",
6086 NULL, NULL);
6087 }
6088 if (cur->depth > -4) {
6089 cur->depth = -4;
6090 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6091 flags, cur->type);
6092 cur->depth = ret - 15;
6093 } else if (cur->depth == -4) {
6094 ret = XML_RELAXNG_CONTENT_COMPLEX;
6095 } else {
6096 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6097 }
6098 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6099 /*
6100 * The 7.3 Attribute derivation rule for groups is plugged there
6101 */
6102 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6103 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6104 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6105 "Found forbidden pattern data/except//element(ref)\n",
6106 NULL, NULL);
6107 }
6108 if (flags & XML_RELAXNG_IN_LIST) {
6109 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6110 "Found forbidden pattern list//element(ref)\n",
6111 NULL, NULL);
6112 }
6113 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6114 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6115 "Found forbidden pattern attribute//element(ref)\n",
6116 NULL, NULL);
6117 }
6118 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6119 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6120 "Found forbidden pattern attribute//element(ref)\n",
6121 NULL, NULL);
6122 }
6123 /*
6124 * reset since in the simple form elements are only child
6125 * of grammar/define
6126 */
6127 nflags = 0;
6128 ret =
6129 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6130 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6131 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6132 "Element %s attributes have a content type error\n",
6133 cur->name, NULL);
6134 }
6135 ret =
6136 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6137 cur->type);
6138 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6139 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6140 "Element %s has a content type error\n",
6141 cur->name, NULL);
6142 } else {
6143 ret = XML_RELAXNG_CONTENT_COMPLEX;
6144 }
6145 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6146 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6147 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6148 "Found forbidden pattern attribute//attribute\n",
6149 NULL, NULL);
6150 }
6151 if (flags & XML_RELAXNG_IN_LIST) {
6152 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6153 "Found forbidden pattern list//attribute\n",
6154 NULL, NULL);
6155 }
6156 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6157 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6158 "Found forbidden pattern oneOrMore//group//attribute\n",
6159 NULL, NULL);
6160 }
6161 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6162 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6163 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6164 NULL, NULL);
6165 }
6166 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6167 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6168 "Found forbidden pattern data/except//attribute\n",
6169 NULL, NULL);
6170 }
6171 if (flags & XML_RELAXNG_IN_START) {
6172 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6173 "Found forbidden pattern start//attribute\n",
6174 NULL, NULL);
6175 }
6176 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6177 && (cur->name == NULL)) {
6178 if (cur->ns == NULL) {
6179 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6180 "Found anyName attribute without oneOrMore ancestor\n",
6181 NULL, NULL);
6182 } else {
6183 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6184 "Found nsName attribute without oneOrMore ancestor\n",
6185 NULL, NULL);
6186 }
6187 }
6188 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6189 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6190 ret = XML_RELAXNG_CONTENT_EMPTY;
6191 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6192 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6193 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6194 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6195 "Found forbidden pattern data/except//oneOrMore\n",
6196 NULL, NULL);
6197 }
6198 if (flags & XML_RELAXNG_IN_START) {
6199 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6200 "Found forbidden pattern start//oneOrMore\n",
6201 NULL, NULL);
6202 }
6203 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6204 ret =
6205 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6206 cur->type);
6207 ret = xmlRelaxNGGroupContentType(ret, ret);
6208 } else if (cur->type == XML_RELAXNG_LIST) {
6209 if (flags & XML_RELAXNG_IN_LIST) {
6210 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6211 "Found forbidden pattern list//list\n", NULL,
6212 NULL);
6213 }
6214 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6215 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6216 "Found forbidden pattern data/except//list\n",
6217 NULL, NULL);
6218 }
6219 if (flags & XML_RELAXNG_IN_START) {
6220 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6221 "Found forbidden pattern start//list\n", NULL,
6222 NULL);
6223 }
6224 nflags = flags | XML_RELAXNG_IN_LIST;
6225 ret =
6226 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6227 cur->type);
6228 } else if (cur->type == XML_RELAXNG_GROUP) {
6229 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6230 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6231 "Found forbidden pattern data/except//group\n",
6232 NULL, NULL);
6233 }
6234 if (flags & XML_RELAXNG_IN_START) {
6235 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6236 "Found forbidden pattern start//group\n", NULL,
6237 NULL);
6238 }
6239 if (flags & XML_RELAXNG_IN_ONEORMORE)
6240 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6241 else
6242 nflags = flags;
6243 ret =
6244 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6245 cur->type);
6246 /*
6247 * The 7.3 Attribute derivation rule for groups is plugged there
6248 */
6249 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6250 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6251 if (flags & XML_RELAXNG_IN_LIST) {
6252 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6253 "Found forbidden pattern list//interleave\n",
6254 NULL, NULL);
6255 }
6256 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6257 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6258 "Found forbidden pattern data/except//interleave\n",
6259 NULL, NULL);
6260 }
6261 if (flags & XML_RELAXNG_IN_START) {
6262 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6263 "Found forbidden pattern start//interleave\n",
6264 NULL, NULL);
6265 }
6266 if (flags & XML_RELAXNG_IN_ONEORMORE)
6267 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6268 else
6269 nflags = flags;
6270 ret =
6271 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6272 cur->type);
6273 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6274 if ((cur->parent != NULL) &&
6275 (cur->parent->type == XML_RELAXNG_DATATYPE))
6276 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6277 else
6278 nflags = flags;
6279 ret =
6280 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6281 cur->type);
6282 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6283 if (flags & XML_RELAXNG_IN_START) {
6284 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6285 "Found forbidden pattern start//data\n", NULL,
6286 NULL);
6287 }
6288 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6289 ret = XML_RELAXNG_CONTENT_SIMPLE;
6290 } else if (cur->type == XML_RELAXNG_VALUE) {
6291 if (flags & XML_RELAXNG_IN_START) {
6292 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6293 "Found forbidden pattern start//value\n", NULL,
6294 NULL);
6295 }
6296 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6297 ret = XML_RELAXNG_CONTENT_SIMPLE;
6298 } else if (cur->type == XML_RELAXNG_TEXT) {
6299 if (flags & XML_RELAXNG_IN_LIST) {
6300 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6301 "Found forbidden pattern list//text\n", NULL,
6302 NULL);
6303 }
6304 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6305 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6306 "Found forbidden pattern data/except//text\n",
6307 NULL, NULL);
6308 }
6309 if (flags & XML_RELAXNG_IN_START) {
6310 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6311 "Found forbidden pattern start//text\n", NULL,
6312 NULL);
6313 }
6314 ret = XML_RELAXNG_CONTENT_COMPLEX;
6315 } else if (cur->type == XML_RELAXNG_EMPTY) {
6316 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6317 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6318 "Found forbidden pattern data/except//empty\n",
6319 NULL, NULL);
6320 }
6321 if (flags & XML_RELAXNG_IN_START) {
6322 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6323 "Found forbidden pattern start//empty\n", NULL,
6324 NULL);
6325 }
6326 ret = XML_RELAXNG_CONTENT_EMPTY;
6327 } else if (cur->type == XML_RELAXNG_CHOICE) {
6328 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6329 ret =
6330 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6331 } else {
6332 ret =
6333 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6334 }
6335 cur = cur->next;
6336 if (ptype == XML_RELAXNG_GROUP) {
6337 val = xmlRelaxNGGroupContentType(val, ret);
6338 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6339 tmp = xmlRelaxNGGroupContentType(val, ret);
6340 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6341 tmp = xmlRelaxNGMaxContentType(val, ret);
6342 } else if (ptype == XML_RELAXNG_CHOICE) {
6343 val = xmlRelaxNGMaxContentType(val, ret);
6344 } else if (ptype == XML_RELAXNG_LIST) {
6345 val = XML_RELAXNG_CONTENT_SIMPLE;
6346 } else if (ptype == XML_RELAXNG_EXCEPT) {
6347 if (ret == XML_RELAXNG_CONTENT_ERROR)
6348 val = XML_RELAXNG_CONTENT_ERROR;
6349 else
6350 val = XML_RELAXNG_CONTENT_SIMPLE;
6351 } else {
6352 val = xmlRelaxNGGroupContentType(val, ret);
6353 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006354
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006355 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006356 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006357}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006358
6359/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006360 * xmlRelaxNGParseGrammar:
6361 * @ctxt: a Relax-NG parser context
6362 * @nodes: grammar children nodes
6363 *
6364 * parse a Relax-NG <grammar> node
6365 *
6366 * Returns the internal xmlRelaxNGGrammarPtr built or
6367 * NULL in case of error
6368 */
6369static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006370xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6371{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006372 xmlRelaxNGGrammarPtr ret, tmp, old;
6373
Daniel Veillardc482e262003-02-26 14:48:48 +00006374#ifdef DEBUG_GRAMMAR
6375 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6376#endif
6377
Daniel Veillard6eadf632003-01-23 18:29:16 +00006378 ret = xmlRelaxNGNewGrammar(ctxt);
6379 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006380 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006381
6382 /*
6383 * Link the new grammar in the tree
6384 */
6385 ret->parent = ctxt->grammar;
6386 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006387 tmp = ctxt->grammar->children;
6388 if (tmp == NULL) {
6389 ctxt->grammar->children = ret;
6390 } else {
6391 while (tmp->next != NULL)
6392 tmp = tmp->next;
6393 tmp->next = ret;
6394 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006395 }
6396
6397 old = ctxt->grammar;
6398 ctxt->grammar = ret;
6399 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6400 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006401 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006402 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6403 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006404 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006405 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6406 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006407 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006408
6409 /*
6410 * Apply 4.17 mergingd rules to defines and starts
6411 */
6412 xmlRelaxNGCombineStart(ctxt, ret);
6413 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006414 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6415 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006416 }
6417
6418 /*
6419 * link together defines and refs in this grammar
6420 */
6421 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006422 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6423 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006424 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006425
Daniel Veillard6eadf632003-01-23 18:29:16 +00006426 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006427 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006428}
6429
6430/**
6431 * xmlRelaxNGParseDocument:
6432 * @ctxt: a Relax-NG parser context
6433 * @node: the root node of the RelaxNG schema
6434 *
6435 * parse a Relax-NG definition resource and build an internal
6436 * xmlRelaxNG struture which can be used to validate instances.
6437 *
6438 * Returns the internal XML RelaxNG structure built or
6439 * NULL in case of error
6440 */
6441static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006442xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6443{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006444 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006445 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006446 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006447
6448 if ((ctxt == NULL) || (node == NULL))
6449 return (NULL);
6450
6451 schema = xmlRelaxNGNewRelaxNG(ctxt);
6452 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006453 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006454
Daniel Veillard276be4a2003-01-24 01:03:34 +00006455 olddefine = ctxt->define;
6456 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006457 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006458 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006459 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006460 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006461
Daniel Veillard4c004142003-10-07 11:33:24 +00006462 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6463 if (schema->topgrammar == NULL) {
6464 return (schema);
6465 }
6466 /*
6467 * Link the new grammar in the tree
6468 */
6469 ret->parent = ctxt->grammar;
6470 if (ctxt->grammar != NULL) {
6471 tmp = ctxt->grammar->children;
6472 if (tmp == NULL) {
6473 ctxt->grammar->children = ret;
6474 } else {
6475 while (tmp->next != NULL)
6476 tmp = tmp->next;
6477 tmp->next = ret;
6478 }
6479 }
6480 old = ctxt->grammar;
6481 ctxt->grammar = ret;
6482 xmlRelaxNGParseStart(ctxt, node);
6483 if (old != NULL)
6484 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006485 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006486 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006487 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006488 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6489 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6490 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6491 while ((schema->topgrammar->start != NULL) &&
6492 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6493 (schema->topgrammar->start->next != NULL))
6494 schema->topgrammar->start =
6495 schema->topgrammar->start->content;
6496 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6497 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6498 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006499 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006500#ifdef DEBUG
6501 if (schema == NULL)
6502 xmlGenericError(xmlGenericErrorContext,
6503 "xmlRelaxNGParseDocument() failed\n");
6504#endif
6505
6506 return (schema);
6507}
6508
6509/************************************************************************
6510 * *
6511 * Reading RelaxNGs *
6512 * *
6513 ************************************************************************/
6514
6515/**
6516 * xmlRelaxNGNewParserCtxt:
6517 * @URL: the location of the schema
6518 *
6519 * Create an XML RelaxNGs parse context for that file/resource expected
6520 * to contain an XML RelaxNGs file.
6521 *
6522 * Returns the parser context or NULL in case of error
6523 */
6524xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006525xmlRelaxNGNewParserCtxt(const char *URL)
6526{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006527 xmlRelaxNGParserCtxtPtr ret;
6528
6529 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006530 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006531
Daniel Veillard4c004142003-10-07 11:33:24 +00006532 ret =
6533 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006534 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006535 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006536 return (NULL);
6537 }
6538 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006539 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006540 ret->error = xmlGenericError;
6541 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006542 return (ret);
6543}
6544
6545/**
6546 * xmlRelaxNGNewMemParserCtxt:
6547 * @buffer: a pointer to a char array containing the schemas
6548 * @size: the size of the array
6549 *
6550 * Create an XML RelaxNGs parse context for that memory buffer expected
6551 * to contain an XML RelaxNGs file.
6552 *
6553 * Returns the parser context or NULL in case of error
6554 */
6555xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006556xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6557{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006558 xmlRelaxNGParserCtxtPtr ret;
6559
6560 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006561 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006562
Daniel Veillard4c004142003-10-07 11:33:24 +00006563 ret =
6564 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006565 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006566 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006567 return (NULL);
6568 }
6569 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6570 ret->buffer = buffer;
6571 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006572 ret->error = xmlGenericError;
6573 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006574 return (ret);
6575}
6576
6577/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006578 * xmlRelaxNGNewDocParserCtxt:
6579 * @doc: a preparsed document tree
6580 *
6581 * Create an XML RelaxNGs parser context for that document.
6582 * Note: since the process of compiling a RelaxNG schemas modifies the
6583 * document, the @doc parameter is duplicated internally.
6584 *
6585 * Returns the parser context or NULL in case of error
6586 */
6587xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006588xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6589{
Daniel Veillard33300b42003-04-17 09:09:19 +00006590 xmlRelaxNGParserCtxtPtr ret;
6591 xmlDocPtr copy;
6592
6593 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006594 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006595 copy = xmlCopyDoc(doc, 1);
6596 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006597 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006598
Daniel Veillard4c004142003-10-07 11:33:24 +00006599 ret =
6600 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006601 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006602 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006603 return (NULL);
6604 }
6605 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6606 ret->document = copy;
6607 ret->userData = xmlGenericErrorContext;
6608 return (ret);
6609}
6610
6611/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006612 * xmlRelaxNGFreeParserCtxt:
6613 * @ctxt: the schema parser context
6614 *
6615 * Free the resources associated to the schema parser context
6616 */
6617void
Daniel Veillard4c004142003-10-07 11:33:24 +00006618xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6619{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006620 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006621 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006622 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006623 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006624 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006625 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006626 if (ctxt->interleaves != NULL)
6627 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006628 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006629 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006630 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006631 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006632 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006633 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006634 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006635 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006636 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006637 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006638
Daniel Veillard4c004142003-10-07 11:33:24 +00006639 for (i = 0; i < ctxt->defNr; i++)
6640 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6641 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006642 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006643 xmlFree(ctxt);
6644}
6645
Daniel Veillard6eadf632003-01-23 18:29:16 +00006646/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006647 * xmlRelaxNGNormExtSpace:
6648 * @value: a value
6649 *
6650 * Removes the leading and ending spaces of the value
6651 * The string is modified "in situ"
6652 */
6653static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006654xmlRelaxNGNormExtSpace(xmlChar * value)
6655{
Daniel Veillardd2298792003-02-14 16:54:11 +00006656 xmlChar *start = value;
6657 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006658
Daniel Veillard4c004142003-10-07 11:33:24 +00006659 if (value == NULL)
6660 return;
6661
6662 while (IS_BLANK(*cur))
6663 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006664 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006665 do {
6666 while ((*cur != 0) && (!IS_BLANK(*cur)))
6667 cur++;
6668 if (*cur == 0)
6669 return;
6670 start = cur;
6671 while (IS_BLANK(*cur))
6672 cur++;
6673 if (*cur == 0) {
6674 *start = 0;
6675 return;
6676 }
6677 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006678 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006679 do {
6680 while ((*cur != 0) && (!IS_BLANK(*cur)))
6681 *start++ = *cur++;
6682 if (*cur == 0) {
6683 *start = 0;
6684 return;
6685 }
6686 /* don't try to normalize the inner spaces */
6687 while (IS_BLANK(*cur))
6688 cur++;
6689 *start++ = *cur++;
6690 if (*cur == 0) {
6691 *start = 0;
6692 return;
6693 }
6694 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006695 }
6696}
6697
6698/**
6699 * xmlRelaxNGCheckAttributes:
6700 * @ctxt: a Relax-NG parser context
6701 * @node: a Relax-NG node
6702 *
6703 * Check all the attributes on the given node
6704 */
6705static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006706xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6707{
Daniel Veillardd2298792003-02-14 16:54:11 +00006708 xmlAttrPtr cur, next;
6709
6710 cur = node->properties;
6711 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006712 next = cur->next;
6713 if ((cur->ns == NULL) ||
6714 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6715 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6716 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6717 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6718 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6719 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6720 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6721 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6722 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6723 "Attribute %s is not allowed on %s\n",
6724 cur->name, node->name);
6725 }
6726 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6727 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6728 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6729 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6730 "Attribute %s is not allowed on %s\n",
6731 cur->name, node->name);
6732 }
6733 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6734 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6735 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6736 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6737 "Attribute %s is not allowed on %s\n",
6738 cur->name, node->name);
6739 }
6740 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6741 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6742 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6743 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6744 "Attribute %s is not allowed on %s\n",
6745 cur->name, node->name);
6746 }
6747 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6748 xmlChar *val;
6749 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006750
Daniel Veillard4c004142003-10-07 11:33:24 +00006751 val = xmlNodeListGetString(node->doc, cur->children, 1);
6752 if (val != NULL) {
6753 if (val[0] != 0) {
6754 uri = xmlParseURI((const char *) val);
6755 if (uri == NULL) {
6756 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6757 "Attribute %s contains invalid URI %s\n",
6758 cur->name, val);
6759 } else {
6760 if (uri->scheme == NULL) {
6761 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6762 "Attribute %s URI %s is not absolute\n",
6763 cur->name, val);
6764 }
6765 if (uri->fragment != NULL) {
6766 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6767 "Attribute %s URI %s has a fragment ID\n",
6768 cur->name, val);
6769 }
6770 xmlFreeURI(uri);
6771 }
6772 }
6773 xmlFree(val);
6774 }
6775 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6776 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6777 "Unknown attribute %s on %s\n", cur->name,
6778 node->name);
6779 }
6780 }
6781 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006782 }
6783}
6784
6785/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006786 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006787 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006788 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006789 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006790 * Cleanup the subtree from unwanted nodes for parsing, resolve
6791 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006792 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006793static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006794xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6795{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006796 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006797
Daniel Veillard6eadf632003-01-23 18:29:16 +00006798 delete = NULL;
6799 cur = root;
6800 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006801 if (delete != NULL) {
6802 xmlUnlinkNode(delete);
6803 xmlFreeNode(delete);
6804 delete = NULL;
6805 }
6806 if (cur->type == XML_ELEMENT_NODE) {
6807 /*
6808 * Simplification 4.1. Annotations
6809 */
6810 if ((cur->ns == NULL) ||
6811 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6812 if ((cur->parent != NULL) &&
6813 (cur->parent->type == XML_ELEMENT_NODE) &&
6814 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6815 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6816 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6817 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6818 "element %s doesn't allow foreign elements\n",
6819 cur->parent->name, NULL);
6820 }
6821 delete = cur;
6822 goto skip_children;
6823 } else {
6824 xmlRelaxNGCleanupAttributes(ctxt, cur);
6825 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6826 xmlChar *href, *ns, *base, *URL;
6827 xmlRelaxNGDocumentPtr docu;
6828 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006829
Daniel Veillard4c004142003-10-07 11:33:24 +00006830 ns = xmlGetProp(cur, BAD_CAST "ns");
6831 if (ns == NULL) {
6832 tmp = cur->parent;
6833 while ((tmp != NULL) &&
6834 (tmp->type == XML_ELEMENT_NODE)) {
6835 ns = xmlGetProp(tmp, BAD_CAST "ns");
6836 if (ns != NULL)
6837 break;
6838 tmp = tmp->parent;
6839 }
6840 }
6841 href = xmlGetProp(cur, BAD_CAST "href");
6842 if (href == NULL) {
6843 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6844 "xmlRelaxNGParse: externalRef has no href attribute\n",
6845 NULL, NULL);
6846 delete = cur;
6847 goto skip_children;
6848 }
6849 base = xmlNodeGetBase(cur->doc, cur);
6850 URL = xmlBuildURI(href, base);
6851 if (URL == NULL) {
6852 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6853 "Failed to compute URL for externalRef %s\n",
6854 href, NULL);
6855 if (href != NULL)
6856 xmlFree(href);
6857 if (base != NULL)
6858 xmlFree(base);
6859 delete = cur;
6860 goto skip_children;
6861 }
6862 if (href != NULL)
6863 xmlFree(href);
6864 if (base != NULL)
6865 xmlFree(base);
6866 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6867 if (docu == NULL) {
6868 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6869 "Failed to load externalRef %s\n", URL,
6870 NULL);
6871 xmlFree(URL);
6872 delete = cur;
6873 goto skip_children;
6874 }
6875 if (ns != NULL)
6876 xmlFree(ns);
6877 xmlFree(URL);
6878 cur->_private = docu;
6879 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6880 xmlChar *href, *ns, *base, *URL;
6881 xmlRelaxNGIncludePtr incl;
6882 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006883
Daniel Veillard4c004142003-10-07 11:33:24 +00006884 href = xmlGetProp(cur, BAD_CAST "href");
6885 if (href == NULL) {
6886 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6887 "xmlRelaxNGParse: include has no href attribute\n",
6888 NULL, NULL);
6889 delete = cur;
6890 goto skip_children;
6891 }
6892 base = xmlNodeGetBase(cur->doc, cur);
6893 URL = xmlBuildURI(href, base);
6894 if (URL == NULL) {
6895 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6896 "Failed to compute URL for include %s\n",
6897 href, NULL);
6898 if (href != NULL)
6899 xmlFree(href);
6900 if (base != NULL)
6901 xmlFree(base);
6902 delete = cur;
6903 goto skip_children;
6904 }
6905 if (href != NULL)
6906 xmlFree(href);
6907 if (base != NULL)
6908 xmlFree(base);
6909 ns = xmlGetProp(cur, BAD_CAST "ns");
6910 if (ns == NULL) {
6911 tmp = cur->parent;
6912 while ((tmp != NULL) &&
6913 (tmp->type == XML_ELEMENT_NODE)) {
6914 ns = xmlGetProp(tmp, BAD_CAST "ns");
6915 if (ns != NULL)
6916 break;
6917 tmp = tmp->parent;
6918 }
6919 }
6920 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6921 if (ns != NULL)
6922 xmlFree(ns);
6923 if (incl == NULL) {
6924 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
6925 "Failed to load include %s\n", URL,
6926 NULL);
6927 xmlFree(URL);
6928 delete = cur;
6929 goto skip_children;
6930 }
6931 xmlFree(URL);
6932 cur->_private = incl;
6933 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6934 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
6935 {
6936 xmlChar *name, *ns;
6937 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006938
Daniel Veillard4c004142003-10-07 11:33:24 +00006939 /*
6940 * Simplification 4.8. name attribute of element
6941 * and attribute elements
6942 */
6943 name = xmlGetProp(cur, BAD_CAST "name");
6944 if (name != NULL) {
6945 if (cur->children == NULL) {
6946 text =
6947 xmlNewChild(cur, cur->ns, BAD_CAST "name",
6948 name);
6949 } else {
6950 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006951
Daniel Veillard4c004142003-10-07 11:33:24 +00006952 node = xmlNewNode(cur->ns, BAD_CAST "name");
6953 if (node != NULL) {
6954 xmlAddPrevSibling(cur->children, node);
6955 text = xmlNewText(name);
6956 xmlAddChild(node, text);
6957 text = node;
6958 }
6959 }
6960 if (text == NULL) {
6961 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
6962 "Failed to create a name %s element\n",
6963 name, NULL);
6964 }
6965 xmlUnsetProp(cur, BAD_CAST "name");
6966 xmlFree(name);
6967 ns = xmlGetProp(cur, BAD_CAST "ns");
6968 if (ns != NULL) {
6969 if (text != NULL) {
6970 xmlSetProp(text, BAD_CAST "ns", ns);
6971 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
6972 }
6973 xmlFree(ns);
6974 } else if (xmlStrEqual(cur->name,
6975 BAD_CAST "attribute")) {
6976 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
6977 }
6978 }
6979 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
6980 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
6981 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
6982 /*
6983 * Simplification 4.8. name attribute of element
6984 * and attribute elements
6985 */
6986 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
6987 xmlNodePtr node;
6988 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006989
Daniel Veillard4c004142003-10-07 11:33:24 +00006990 node = cur->parent;
6991 while ((node != NULL) &&
6992 (node->type == XML_ELEMENT_NODE)) {
6993 ns = xmlGetProp(node, BAD_CAST "ns");
6994 if (ns != NULL) {
6995 break;
6996 }
6997 node = node->parent;
6998 }
6999 if (ns == NULL) {
7000 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7001 } else {
7002 xmlSetProp(cur, BAD_CAST "ns", ns);
7003 xmlFree(ns);
7004 }
7005 }
7006 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7007 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007008
Daniel Veillard4c004142003-10-07 11:33:24 +00007009 /*
7010 * Simplification: 4.10. QNames
7011 */
7012 name = xmlNodeGetContent(cur);
7013 if (name != NULL) {
7014 local = xmlSplitQName2(name, &prefix);
7015 if (local != NULL) {
7016 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007017
Daniel Veillard4c004142003-10-07 11:33:24 +00007018 ns = xmlSearchNs(cur->doc, cur, prefix);
7019 if (ns == NULL) {
7020 xmlRngPErr(ctxt, cur,
7021 XML_RNGP_PREFIX_UNDEFINED,
7022 "xmlRelaxNGParse: no namespace for prefix %s\n",
7023 prefix, NULL);
7024 } else {
7025 xmlSetProp(cur, BAD_CAST "ns",
7026 ns->href);
7027 xmlNodeSetContent(cur, local);
7028 }
7029 xmlFree(local);
7030 xmlFree(prefix);
7031 }
7032 xmlFree(name);
7033 }
7034 }
7035 /*
7036 * 4.16
7037 */
7038 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7039 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7040 xmlRngPErr(ctxt, cur,
7041 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7042 "Found nsName/except//nsName forbidden construct\n",
7043 NULL, NULL);
7044 }
7045 }
7046 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7047 (cur != root)) {
7048 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007049
Daniel Veillard4c004142003-10-07 11:33:24 +00007050 /*
7051 * 4.16
7052 */
7053 if ((cur->parent != NULL) &&
7054 (xmlStrEqual
7055 (cur->parent->name, BAD_CAST "anyName"))) {
7056 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7057 xmlRelaxNGCleanupTree(ctxt, cur);
7058 ctxt->flags = oldflags;
7059 goto skip_children;
7060 } else if ((cur->parent != NULL) &&
7061 (xmlStrEqual
7062 (cur->parent->name, BAD_CAST "nsName"))) {
7063 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7064 xmlRelaxNGCleanupTree(ctxt, cur);
7065 ctxt->flags = oldflags;
7066 goto skip_children;
7067 }
7068 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7069 /*
7070 * 4.16
7071 */
7072 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7073 xmlRngPErr(ctxt, cur,
7074 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7075 "Found anyName/except//anyName forbidden construct\n",
7076 NULL, NULL);
7077 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7078 xmlRngPErr(ctxt, cur,
7079 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7080 "Found nsName/except//anyName forbidden construct\n",
7081 NULL, NULL);
7082 }
7083 }
7084 /*
7085 * Thisd is not an else since "include" is transformed
7086 * into a div
7087 */
7088 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7089 xmlChar *ns;
7090 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007091
Daniel Veillard4c004142003-10-07 11:33:24 +00007092 /*
7093 * implements rule 4.11
7094 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007095
Daniel Veillard4c004142003-10-07 11:33:24 +00007096 ns = xmlGetProp(cur, BAD_CAST "ns");
7097
7098 child = cur->children;
7099 ins = cur;
7100 while (child != NULL) {
7101 if (ns != NULL) {
7102 if (!xmlHasProp(child, BAD_CAST "ns")) {
7103 xmlSetProp(child, BAD_CAST "ns", ns);
7104 }
7105 }
7106 tmp = child->next;
7107 xmlUnlinkNode(child);
7108 ins = xmlAddNextSibling(ins, child);
7109 child = tmp;
7110 }
7111 if (ns != NULL)
7112 xmlFree(ns);
7113 delete = cur;
7114 goto skip_children;
7115 }
7116 }
7117 }
7118 /*
7119 * Simplification 4.2 whitespaces
7120 */
7121 else if ((cur->type == XML_TEXT_NODE) ||
7122 (cur->type == XML_CDATA_SECTION_NODE)) {
7123 if (IS_BLANK_NODE(cur)) {
7124 if (cur->parent->type == XML_ELEMENT_NODE) {
7125 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7126 &&
7127 (!xmlStrEqual
7128 (cur->parent->name, BAD_CAST "param")))
7129 delete = cur;
7130 } else {
7131 delete = cur;
7132 goto skip_children;
7133 }
7134 }
7135 } else {
7136 delete = cur;
7137 goto skip_children;
7138 }
7139
7140 /*
7141 * Skip to next node
7142 */
7143 if (cur->children != NULL) {
7144 if ((cur->children->type != XML_ENTITY_DECL) &&
7145 (cur->children->type != XML_ENTITY_REF_NODE) &&
7146 (cur->children->type != XML_ENTITY_NODE)) {
7147 cur = cur->children;
7148 continue;
7149 }
7150 }
7151 skip_children:
7152 if (cur->next != NULL) {
7153 cur = cur->next;
7154 continue;
7155 }
7156
7157 do {
7158 cur = cur->parent;
7159 if (cur == NULL)
7160 break;
7161 if (cur == root) {
7162 cur = NULL;
7163 break;
7164 }
7165 if (cur->next != NULL) {
7166 cur = cur->next;
7167 break;
7168 }
7169 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007170 }
7171 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007172 xmlUnlinkNode(delete);
7173 xmlFreeNode(delete);
7174 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007175 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007176}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007177
Daniel Veillardc5312d72003-02-21 17:14:10 +00007178/**
7179 * xmlRelaxNGCleanupDoc:
7180 * @ctxt: a Relax-NG parser context
7181 * @doc: an xmldocPtr document pointer
7182 *
7183 * Cleanup the document from unwanted nodes for parsing, resolve
7184 * Include and externalRef lookups.
7185 *
7186 * Returns the cleaned up document or NULL in case of error
7187 */
7188static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007189xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7190{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007191 xmlNodePtr root;
7192
7193 /*
7194 * Extract the root
7195 */
7196 root = xmlDocGetRootElement(doc);
7197 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007198 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7199 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007200 return (NULL);
7201 }
7202 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007203 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007204}
7205
7206/**
7207 * xmlRelaxNGParse:
7208 * @ctxt: a Relax-NG parser context
7209 *
7210 * parse a schema definition resource and build an internal
7211 * XML Shema struture which can be used to validate instances.
7212 * *WARNING* this interface is highly subject to change
7213 *
7214 * Returns the internal XML RelaxNG structure built from the resource or
7215 * NULL in case of error
7216 */
7217xmlRelaxNGPtr
7218xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7219{
7220 xmlRelaxNGPtr ret = NULL;
7221 xmlDocPtr doc;
7222 xmlNodePtr root;
7223
7224 xmlRelaxNGInitTypes();
7225
7226 if (ctxt == NULL)
7227 return (NULL);
7228
7229 /*
7230 * First step is to parse the input document into an DOM/Infoset
7231 */
7232 if (ctxt->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007233 doc = xmlParseFile((const char *) ctxt->URL);
7234 if (doc == NULL) {
7235 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7236 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7237 NULL);
7238 return (NULL);
7239 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007240 } else if (ctxt->buffer != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007241 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
7242 if (doc == NULL) {
7243 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7244 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7245 NULL);
7246 return (NULL);
7247 }
7248 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7249 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007250 } else if (ctxt->document != NULL) {
7251 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007252 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007253 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7254 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7255 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007256 }
7257 ctxt->document = doc;
7258
7259 /*
7260 * Some preprocessing of the document content
7261 */
7262 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7263 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007264 xmlFreeDoc(ctxt->document);
7265 ctxt->document = NULL;
7266 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007267 }
7268
Daniel Veillard6eadf632003-01-23 18:29:16 +00007269 /*
7270 * Then do the parsing for good
7271 */
7272 root = xmlDocGetRootElement(doc);
7273 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007274 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7275 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7276 ctxt->URL, NULL);
7277 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007278 return (NULL);
7279 }
7280 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007281 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007282 xmlFreeDoc(doc);
7283 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007284 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007285
7286 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007287 * Check the ref/defines links
7288 */
7289 /*
7290 * try to preprocess interleaves
7291 */
7292 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007293 xmlHashScan(ctxt->interleaves,
7294 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007295 }
7296
7297 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007298 * if there was a parsing error return NULL
7299 */
7300 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007301 xmlRelaxNGFree(ret);
7302 ctxt->document = NULL;
7303 xmlFreeDoc(doc);
7304 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007305 }
7306
7307 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007308 * try to compile (parts of) the schemas
7309 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007310 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7311 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007312 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007313
Daniel Veillard4c004142003-10-07 11:33:24 +00007314 def = xmlRelaxNGNewDefine(ctxt, NULL);
7315 if (def != NULL) {
7316 def->type = XML_RELAXNG_START;
7317 def->content = ret->topgrammar->start;
7318 ret->topgrammar->start = def;
7319 }
7320 }
7321 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007322 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007323
7324 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007325 * Transfer the pointer for cleanup at the schema level.
7326 */
7327 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007328 ctxt->document = NULL;
7329 ret->documents = ctxt->documents;
7330 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007331
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007332 ret->includes = ctxt->includes;
7333 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007334 ret->defNr = ctxt->defNr;
7335 ret->defTab = ctxt->defTab;
7336 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007337 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007338 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007339
7340 return (ret);
7341}
Daniel Veillard4c004142003-10-07 11:33:24 +00007342
Daniel Veillard6eadf632003-01-23 18:29:16 +00007343/**
7344 * xmlRelaxNGSetParserErrors:
7345 * @ctxt: a Relax-NG validation context
7346 * @err: the error callback
7347 * @warn: the warning callback
7348 * @ctx: contextual data for the callbacks
7349 *
7350 * Set the callback functions used to handle errors for a validation context
7351 */
7352void
7353xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007354 xmlRelaxNGValidityErrorFunc err,
7355 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7356{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007357 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007358 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007359 ctxt->error = err;
7360 ctxt->warning = warn;
7361 ctxt->userData = ctx;
7362}
Daniel Veillard409a8142003-07-18 15:16:57 +00007363
7364/**
7365 * xmlRelaxNGGetParserErrors:
7366 * @ctxt: a Relax-NG validation context
7367 * @err: the error callback result
7368 * @warn: the warning callback result
7369 * @ctx: contextual data for the callbacks result
7370 *
7371 * Get the callback information used to handle errors for a validation context
7372 *
7373 * Returns -1 in case of failure, 0 otherwise.
7374 */
7375int
7376xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007377 xmlRelaxNGValidityErrorFunc * err,
7378 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7379{
Daniel Veillard409a8142003-07-18 15:16:57 +00007380 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007381 return (-1);
7382 if (err != NULL)
7383 *err = ctxt->error;
7384 if (warn != NULL)
7385 *warn = ctxt->warning;
7386 if (ctx != NULL)
7387 *ctx = ctxt->userData;
7388 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007389}
7390
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007391#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007392
Daniel Veillard6eadf632003-01-23 18:29:16 +00007393/************************************************************************
7394 * *
7395 * Dump back a compiled form *
7396 * *
7397 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007398static void xmlRelaxNGDumpDefine(FILE * output,
7399 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007400
7401/**
7402 * xmlRelaxNGDumpDefines:
7403 * @output: the file output
7404 * @defines: a list of define structures
7405 *
7406 * Dump a RelaxNG structure back
7407 */
7408static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007409xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7410{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007411 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007412 xmlRelaxNGDumpDefine(output, defines);
7413 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007414 }
7415}
7416
7417/**
7418 * xmlRelaxNGDumpDefine:
7419 * @output: the file output
7420 * @define: a define structure
7421 *
7422 * Dump a RelaxNG structure back
7423 */
7424static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007425xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7426{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007427 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007428 return;
7429 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007430 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007431 fprintf(output, "<empty/>\n");
7432 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007433 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007434 fprintf(output, "<notAllowed/>\n");
7435 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007436 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007437 fprintf(output, "<text/>\n");
7438 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007439 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007440 fprintf(output, "<element>\n");
7441 if (define->name != NULL) {
7442 fprintf(output, "<name");
7443 if (define->ns != NULL)
7444 fprintf(output, " ns=\"%s\"", define->ns);
7445 fprintf(output, ">%s</name>\n", define->name);
7446 }
7447 xmlRelaxNGDumpDefines(output, define->attrs);
7448 xmlRelaxNGDumpDefines(output, define->content);
7449 fprintf(output, "</element>\n");
7450 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007451 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007452 fprintf(output, "<list>\n");
7453 xmlRelaxNGDumpDefines(output, define->content);
7454 fprintf(output, "</list>\n");
7455 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007456 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007457 fprintf(output, "<oneOrMore>\n");
7458 xmlRelaxNGDumpDefines(output, define->content);
7459 fprintf(output, "</oneOrMore>\n");
7460 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007461 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007462 fprintf(output, "<zeroOrMore>\n");
7463 xmlRelaxNGDumpDefines(output, define->content);
7464 fprintf(output, "</zeroOrMore>\n");
7465 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007466 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007467 fprintf(output, "<choice>\n");
7468 xmlRelaxNGDumpDefines(output, define->content);
7469 fprintf(output, "</choice>\n");
7470 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007471 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007472 fprintf(output, "<group>\n");
7473 xmlRelaxNGDumpDefines(output, define->content);
7474 fprintf(output, "</group>\n");
7475 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007476 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007477 fprintf(output, "<interleave>\n");
7478 xmlRelaxNGDumpDefines(output, define->content);
7479 fprintf(output, "</interleave>\n");
7480 break;
7481 case XML_RELAXNG_OPTIONAL:
7482 fprintf(output, "<optional>\n");
7483 xmlRelaxNGDumpDefines(output, define->content);
7484 fprintf(output, "</optional>\n");
7485 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007486 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007487 fprintf(output, "<attribute>\n");
7488 xmlRelaxNGDumpDefines(output, define->content);
7489 fprintf(output, "</attribute>\n");
7490 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007491 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007492 fprintf(output, "<define");
7493 if (define->name != NULL)
7494 fprintf(output, " name=\"%s\"", define->name);
7495 fprintf(output, ">\n");
7496 xmlRelaxNGDumpDefines(output, define->content);
7497 fprintf(output, "</define>\n");
7498 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007499 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007500 fprintf(output, "<ref");
7501 if (define->name != NULL)
7502 fprintf(output, " name=\"%s\"", define->name);
7503 fprintf(output, ">\n");
7504 xmlRelaxNGDumpDefines(output, define->content);
7505 fprintf(output, "</ref>\n");
7506 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007507 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007508 fprintf(output, "<parentRef");
7509 if (define->name != NULL)
7510 fprintf(output, " name=\"%s\"", define->name);
7511 fprintf(output, ">\n");
7512 xmlRelaxNGDumpDefines(output, define->content);
7513 fprintf(output, "</parentRef>\n");
7514 break;
7515 case XML_RELAXNG_EXTERNALREF:
7516 fprintf(output, "<externalRef>");
7517 xmlRelaxNGDumpDefines(output, define->content);
7518 fprintf(output, "</externalRef>\n");
7519 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007520 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007521 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007522 TODO break;
7523 case XML_RELAXNG_START:
7524 case XML_RELAXNG_EXCEPT:
7525 case XML_RELAXNG_PARAM:
7526 TODO break;
7527 case XML_RELAXNG_NOOP:
7528 xmlRelaxNGDumpDefines(output, define->content);
7529 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007530 }
7531}
Daniel Veillard4c004142003-10-07 11:33:24 +00007532
Daniel Veillard6eadf632003-01-23 18:29:16 +00007533/**
7534 * xmlRelaxNGDumpGrammar:
7535 * @output: the file output
7536 * @grammar: a grammar structure
7537 * @top: is this a top grammar
7538 *
7539 * Dump a RelaxNG structure back
7540 */
7541static void
7542xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7543{
7544 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007545 return;
7546
Daniel Veillard6eadf632003-01-23 18:29:16 +00007547 fprintf(output, "<grammar");
7548 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007549 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7550 switch (grammar->combine) {
7551 case XML_RELAXNG_COMBINE_UNDEFINED:
7552 break;
7553 case XML_RELAXNG_COMBINE_CHOICE:
7554 fprintf(output, " combine=\"choice\"");
7555 break;
7556 case XML_RELAXNG_COMBINE_INTERLEAVE:
7557 fprintf(output, " combine=\"interleave\"");
7558 break;
7559 default:
7560 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007561 }
7562 fprintf(output, ">\n");
7563 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007564 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007565 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007566 fprintf(output, "<start>\n");
7567 xmlRelaxNGDumpDefine(output, grammar->start);
7568 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007569 }
7570 /* TODO ? Dump the defines ? */
7571 fprintf(output, "</grammar>\n");
7572}
7573
7574/**
7575 * xmlRelaxNGDump:
7576 * @output: the file output
7577 * @schema: a schema structure
7578 *
7579 * Dump a RelaxNG structure back
7580 */
7581void
7582xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7583{
7584 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007585 fprintf(output, "RelaxNG empty or failed to compile\n");
7586 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007587 }
7588 fprintf(output, "RelaxNG: ");
7589 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007590 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007591 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007592 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007593 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007595 }
7596 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007597 fprintf(output, "RelaxNG has no top grammar\n");
7598 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007599 }
7600 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7601}
7602
Daniel Veillardfebcca42003-02-16 15:44:18 +00007603/**
7604 * xmlRelaxNGDumpTree:
7605 * @output: the file output
7606 * @schema: a schema structure
7607 *
7608 * Dump the transformed RelaxNG tree.
7609 */
7610void
7611xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7612{
7613 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007614 fprintf(output, "RelaxNG empty or failed to compile\n");
7615 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007616 }
7617 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007618 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007619 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007620 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007621 }
7622}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007623#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007624
Daniel Veillard6eadf632003-01-23 18:29:16 +00007625/************************************************************************
7626 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007627 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007628 * *
7629 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007630static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7631 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007632
7633/**
7634 * xmlRelaxNGValidateCompiledCallback:
7635 * @exec: the regular expression instance
7636 * @token: the token which matched
7637 * @transdata: callback data, the define for the subelement if available
7638 @ @inputdata: callback data, the Relax NG validation context
7639 *
7640 * Handle the callback and if needed validate the element children.
7641 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007642static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007643xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007644 const xmlChar * token,
7645 void *transdata, void *inputdata)
7646{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007647 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7648 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7649 int ret;
7650
7651#ifdef DEBUG_COMPILE
7652 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007653 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007654#endif
7655 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007656 fprintf(stderr, "callback on %s missing context\n", token);
7657 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7658 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7659 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007660 }
7661 if (define == NULL) {
7662 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007663 return;
7664 fprintf(stderr, "callback on %s missing define\n", token);
7665 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7666 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7667 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007668 }
7669 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007670 fprintf(stderr, "callback on %s missing info\n", token);
7671 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7672 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7673 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007674 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007675 fprintf(stderr, "callback on %s define is not element\n", token);
7676 if (ctxt->errNo == XML_RELAXNG_OK)
7677 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7678 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007679 }
7680 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007681 if (ret != 0)
7682 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007683}
7684
7685/**
7686 * xmlRelaxNGValidateCompiledContent:
7687 * @ctxt: the RelaxNG validation context
7688 * @regexp: the regular expression as compiled
7689 * @content: list of children to test against the regexp
7690 *
7691 * Validate the content model of an element or start using the regexp
7692 *
7693 * Returns 0 in case of success, -1 in case of error.
7694 */
7695static int
7696xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007697 xmlRegexpPtr regexp, xmlNodePtr content)
7698{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007699 xmlRegExecCtxtPtr exec;
7700 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007701 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007702 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007703
7704 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007705 return (-1);
7706 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007707 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007708 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007709 cur = content;
7710 while (cur != NULL) {
7711 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007712 switch (cur->type) {
7713 case XML_TEXT_NODE:
7714 case XML_CDATA_SECTION_NODE:
7715 if (xmlIsBlankNode(cur))
7716 break;
7717 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7718 if (ret < 0) {
7719 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7720 cur->parent->name);
7721 }
7722 break;
7723 case XML_ELEMENT_NODE:
7724 if (cur->ns != NULL) {
7725 ret = xmlRegExecPushString2(exec, cur->name,
7726 cur->ns->href, ctxt);
7727 } else {
7728 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7729 }
7730 if (ret < 0) {
7731 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7732 }
7733 break;
7734 default:
7735 break;
7736 }
7737 if (ret < 0)
7738 break;
7739 /*
7740 * Switch to next element
7741 */
7742 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007743 }
7744 ret = xmlRegExecPushString(exec, NULL, NULL);
7745 if (ret == 1) {
7746 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007747 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007748 } else if (ret == 0) {
7749 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007750 * TODO: get some of the names needed to exit the current state of exec
7751 */
7752 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7753 ret = -1;
7754 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7755 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007756 } else {
7757 ret = -1;
7758 }
7759 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007760 /*
7761 * There might be content model errors outside of the pure
7762 * regexp validation, e.g. for attribute values.
7763 */
7764 if ((ret == 0) && (ctxt->perr != 0)) {
7765 ret = ctxt->perr;
7766 }
7767 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007768 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007769}
7770
7771/************************************************************************
7772 * *
7773 * Progressive validation of when possible *
7774 * *
7775 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007776static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7777 xmlRelaxNGDefinePtr defines);
7778static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
7779 int log);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007780static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007781
7782/**
7783 * xmlRelaxNGElemPush:
7784 * @ctxt: the validation context
7785 * @exec: the regexp runtime for the new content model
7786 *
7787 * Push a new regexp for the current node content model on the stack
7788 *
7789 * Returns 0 in case of success and -1 in case of error.
7790 */
7791static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007792xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7793{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007794 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007795 ctxt->elemMax = 10;
7796 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7797 sizeof
7798 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007799 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007800 xmlRngVErrMemory(ctxt, "validating\n");
7801 return (-1);
7802 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007803 }
7804 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007805 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007806 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007807 ctxt->elemMax *
7808 sizeof
7809 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007810 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007811 xmlRngVErrMemory(ctxt, "validating\n");
7812 return (-1);
7813 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007814 }
7815 ctxt->elemTab[ctxt->elemNr++] = exec;
7816 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007817 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007818}
7819
7820/**
7821 * xmlRelaxNGElemPop:
7822 * @ctxt: the validation context
7823 *
7824 * Pop the regexp of the current node content model from the stack
7825 *
7826 * Returns the exec or NULL if empty
7827 */
7828static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007829xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7830{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007831 xmlRegExecCtxtPtr ret;
7832
Daniel Veillard4c004142003-10-07 11:33:24 +00007833 if (ctxt->elemNr <= 0)
7834 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007835 ctxt->elemNr--;
7836 ret = ctxt->elemTab[ctxt->elemNr];
7837 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007838 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007839 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7840 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007841 ctxt->elem = NULL;
7842 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007843}
7844
7845/**
7846 * xmlRelaxNGValidateProgressiveCallback:
7847 * @exec: the regular expression instance
7848 * @token: the token which matched
7849 * @transdata: callback data, the define for the subelement if available
7850 @ @inputdata: callback data, the Relax NG validation context
7851 *
7852 * Handle the callback and if needed validate the element children.
7853 * some of the in/out informations are passed via the context in @inputdata.
7854 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007855static void
7856xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7857 ATTRIBUTE_UNUSED,
7858 const xmlChar * token,
7859 void *transdata, void *inputdata)
7860{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007861 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7862 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007863 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007864 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007865 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007866
7867#ifdef DEBUG_PROGRESSIVE
7868 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007869 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007870#endif
7871 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007872 fprintf(stderr, "callback on %s missing context\n", token);
7873 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007874 }
7875 ctxt->pstate = 1;
7876 if (define == NULL) {
7877 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007878 return;
7879 fprintf(stderr, "callback on %s missing define\n", token);
7880 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7881 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7882 ctxt->pstate = -1;
7883 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007884 }
7885 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007886 fprintf(stderr, "callback on %s missing info\n", token);
7887 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7888 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7889 ctxt->pstate = -1;
7890 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007891 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007892 fprintf(stderr, "callback on %s define is not element\n", token);
7893 if (ctxt->errNo == XML_RELAXNG_OK)
7894 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7895 ctxt->pstate = -1;
7896 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007897 }
7898 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007899 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7900 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7901 xmlRelaxNGDumpValidError(ctxt);
7902 ctxt->pstate = -1;
7903 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007904 }
7905 if (define->contModel == NULL) {
7906 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007907 * this node cannot be validated in a streamable fashion
7908 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00007909#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00007910 xmlGenericError(xmlGenericErrorContext,
7911 "Element '%s' validation is not streamable\n",
7912 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007913#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00007914 ctxt->pstate = 0;
7915 ctxt->pdef = define;
7916 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007917 }
7918 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00007919 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007920 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007921 ctxt->pstate = -1;
7922 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007923 }
7924 xmlRelaxNGElemPush(ctxt, exec);
7925
7926 /*
7927 * Validate the attributes part of the content.
7928 */
7929 state = xmlRelaxNGNewValidState(ctxt, node);
7930 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007931 ctxt->pstate = -1;
7932 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007933 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007934 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007935 ctxt->state = state;
7936 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007937 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
7938 if (ret != 0) {
7939 ctxt->pstate = -1;
7940 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
7941 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007942 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007943 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007944 ctxt->state->seq = NULL;
7945 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
7946 if (ret != 0) {
7947 ctxt->pstate = -1;
7948 }
7949 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00007950 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007951 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007952
7953 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007954
Daniel Veillard4c004142003-10-07 11:33:24 +00007955 for (i = 0; i < ctxt->states->nbState; i++) {
7956 state = ctxt->states->tabState[i];
7957 ctxt->state = state;
7958 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007959
Daniel Veillard4c004142003-10-07 11:33:24 +00007960 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
7961 tmp = 0;
7962 break;
7963 }
7964 }
7965 if (tmp != 0) {
7966 /*
7967 * validation error, log the message for the "best" one
7968 */
7969 ctxt->flags |= FLAGS_IGNORABLE;
7970 xmlRelaxNGLogBestError(ctxt);
7971 }
7972 for (i = 0; i < ctxt->states->nbState; i++) {
7973 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
7974 }
7975 xmlRelaxNGFreeStates(ctxt, ctxt->states);
7976 ctxt->states = NULL;
7977 if ((ret == 0) && (tmp == -1))
7978 ctxt->pstate = -1;
7979 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007980 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007981 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007982 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
7983 xmlRelaxNGDumpValidError(ctxt);
7984 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007985 }
7986 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007987}
7988
7989/**
7990 * xmlRelaxNGValidatePushElement:
7991 * @ctxt: the validation context
7992 * @doc: a document instance
7993 * @elem: an element instance
7994 *
7995 * Push a new element start on the RelaxNG validation stack.
7996 *
7997 * returns 1 if no validation problem was found or 0 if validating the
7998 * element requires a full node, and -1 in case of error.
7999 */
8000int
Daniel Veillard33300b42003-04-17 09:09:19 +00008001xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8002 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008003 xmlNodePtr elem)
8004{
8005 int ret = 1;
8006
8007 if ((ctxt == NULL) || (elem == NULL))
8008 return (-1);
8009
8010#ifdef DEBUG_PROGRESSIVE
8011 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8012#endif
8013 if (ctxt->elem == 0) {
8014 xmlRelaxNGPtr schema;
8015 xmlRelaxNGGrammarPtr grammar;
8016 xmlRegExecCtxtPtr exec;
8017 xmlRelaxNGDefinePtr define;
8018
8019 schema = ctxt->schema;
8020 if (schema == NULL) {
8021 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8022 return (-1);
8023 }
8024 grammar = schema->topgrammar;
8025 if ((grammar == NULL) || (grammar->start == NULL)) {
8026 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8027 return (-1);
8028 }
8029 define = grammar->start;
8030 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008031 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008032 return (0);
8033 }
8034 exec = xmlRegNewExecCtxt(define->contModel,
8035 xmlRelaxNGValidateProgressiveCallback,
8036 ctxt);
8037 if (exec == NULL) {
8038 return (-1);
8039 }
8040 xmlRelaxNGElemPush(ctxt, exec);
8041 }
8042 ctxt->pnode = elem;
8043 ctxt->pstate = 0;
8044 if (elem->ns != NULL) {
8045 ret =
8046 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8047 ctxt);
8048 } else {
8049 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8050 }
8051 if (ret < 0) {
8052 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8053 } else {
8054 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008055 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008056 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008057 ret = -1;
8058 else
8059 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008060 }
8061#ifdef DEBUG_PROGRESSIVE
8062 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008063 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8064 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008065#endif
8066 return (ret);
8067}
8068
8069/**
8070 * xmlRelaxNGValidatePushCData:
8071 * @ctxt: the RelaxNG validation context
8072 * @data: some character data read
8073 * @len: the lenght of the data
8074 *
8075 * check the CData parsed for validation in the current stack
8076 *
8077 * returns 1 if no validation problem was found or -1 otherwise
8078 */
8079int
8080xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008081 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008082{
8083 int ret = 1;
8084
8085 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8086 return (-1);
8087
8088#ifdef DEBUG_PROGRESSIVE
8089 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8090#endif
8091
8092 while (*data != 0) {
8093 if (!IS_BLANK(*data))
8094 break;
8095 data++;
8096 }
8097 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008098 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008099
8100 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8101 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008102 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008103#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008104 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008105#endif
8106
Daniel Veillard4c004142003-10-07 11:33:24 +00008107 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008108 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008109 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008110}
8111
8112/**
8113 * xmlRelaxNGValidatePopElement:
8114 * @ctxt: the RelaxNG validation context
8115 * @doc: a document instance
8116 * @elem: an element instance
8117 *
8118 * Pop the element end from the RelaxNG validation stack.
8119 *
8120 * returns 1 if no validation problem was found or 0 otherwise
8121 */
8122int
8123xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8124 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008125 xmlNodePtr elem)
8126{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008127 int ret;
8128 xmlRegExecCtxtPtr exec;
8129
Daniel Veillard4c004142003-10-07 11:33:24 +00008130 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8131 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008132#ifdef DEBUG_PROGRESSIVE
8133 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8134#endif
8135 /*
8136 * verify that we reached a terminal state of the content model.
8137 */
8138 exec = xmlRelaxNGElemPop(ctxt);
8139 ret = xmlRegExecPushString(exec, NULL, NULL);
8140 if (ret == 0) {
8141 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008142 * TODO: get some of the names needed to exit the current state of exec
8143 */
8144 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8145 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008146 } else if (ret < 0) {
8147 ret = -1;
8148 } else {
8149 ret = 1;
8150 }
8151 xmlRegFreeExecCtxt(exec);
8152#ifdef DEBUG_PROGRESSIVE
8153 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008154 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8155 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008156#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008157 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008158}
8159
8160/**
8161 * xmlRelaxNGValidateFullElement:
8162 * @ctxt: the validation context
8163 * @doc: a document instance
8164 * @elem: an element instance
8165 *
8166 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8167 * 0 and the content of the node has been expanded.
8168 *
8169 * returns 1 if no validation problem was found or -1 in case of error.
8170 */
8171int
Daniel Veillard33300b42003-04-17 09:09:19 +00008172xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8173 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008174 xmlNodePtr elem)
8175{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008176 int ret;
8177 xmlRelaxNGValidStatePtr state;
8178
Daniel Veillard4c004142003-10-07 11:33:24 +00008179 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8180 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008181#ifdef DEBUG_PROGRESSIVE
8182 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8183#endif
8184 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8185 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008186 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008187 }
8188 state->seq = elem;
8189 ctxt->state = state;
8190 ctxt->errNo = XML_RELAXNG_OK;
8191 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008192 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8193 ret = -1;
8194 else
8195 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008196 xmlRelaxNGFreeValidState(ctxt, state);
8197 ctxt->state = NULL;
8198#ifdef DEBUG_PROGRESSIVE
8199 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008200 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8201 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008202#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008203 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008204}
8205
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008206/************************************************************************
8207 * *
8208 * Generic interpreted validation implementation *
8209 * *
8210 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008211static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8212 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008213
8214/**
8215 * xmlRelaxNGSkipIgnored:
8216 * @ctxt: a schema validation context
8217 * @node: the top node.
8218 *
8219 * Skip ignorable nodes in that context
8220 *
8221 * Returns the new sibling or NULL in case of error.
8222 */
8223static xmlNodePtr
8224xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008225 xmlNodePtr node)
8226{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008227 /*
8228 * TODO complete and handle entities
8229 */
8230 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008231 ((node->type == XML_COMMENT_NODE) ||
8232 (node->type == XML_PI_NODE) ||
8233 (((node->type == XML_TEXT_NODE) ||
8234 (node->type == XML_CDATA_SECTION_NODE)) &&
8235 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8236 (IS_BLANK_NODE(node)))))) {
8237 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008238 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008239 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008240}
8241
8242/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008243 * xmlRelaxNGNormalize:
8244 * @ctxt: a schema validation context
8245 * @str: the string to normalize
8246 *
8247 * Implements the normalizeWhiteSpace( s ) function from
8248 * section 6.2.9 of the spec
8249 *
8250 * Returns the new string or NULL in case of error.
8251 */
8252static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008253xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8254{
Daniel Veillardedc91922003-01-26 00:52:04 +00008255 xmlChar *ret, *p;
8256 const xmlChar *tmp;
8257 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008258
Daniel Veillardedc91922003-01-26 00:52:04 +00008259 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008260 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008261 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008262 while (*tmp != 0)
8263 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008264 len = tmp - str;
8265
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008266 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008267 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008268 xmlRngVErrMemory(ctxt, "validating\n");
8269 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008270 }
8271 p = ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00008272 while (IS_BLANK(*str))
8273 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008274 while (*str != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008275 if (IS_BLANK(*str)) {
8276 while (IS_BLANK(*str))
8277 str++;
8278 if (*str == 0)
8279 break;
8280 *p++ = ' ';
8281 } else
8282 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008283 }
8284 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008285 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008286}
8287
8288/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008289 * xmlRelaxNGValidateDatatype:
8290 * @ctxt: a Relax-NG validation context
8291 * @value: the string value
8292 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008293 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008294 *
8295 * Validate the given value against the dataype
8296 *
8297 * Returns 0 if the validation succeeded or an error code.
8298 */
8299static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008300xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8301 const xmlChar * value,
8302 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8303{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008304 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008305 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008306 void *result = NULL;
8307 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008308
8309 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008310 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008311 }
8312 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008313 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008314 if ((define->attrs != NULL) &&
8315 (define->attrs->type == XML_RELAXNG_PARAM)) {
8316 ret =
8317 lib->check(lib->data, define->name, value, &result, node);
8318 } else {
8319 ret = lib->check(lib->data, define->name, value, NULL, node);
8320 }
8321 } else
8322 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008323 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008324 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8325 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8326 lib->freef(lib->data, result);
8327 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008328 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008329 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008330 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008331 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008332 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008333 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8334 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008335 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008336 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008337 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008338 if (lib->facet != NULL) {
8339 tmp = lib->facet(lib->data, define->name, cur->name,
8340 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008341 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008342 ret = -1;
8343 }
8344 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008345 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008346 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008347 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008348
Daniel Veillard4c004142003-10-07 11:33:24 +00008349 oldvalue = ctxt->state->value;
8350 oldendvalue = ctxt->state->endvalue;
8351 ctxt->state->value = (xmlChar *) value;
8352 ctxt->state->endvalue = NULL;
8353 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8354 ctxt->state->value = (xmlChar *) oldvalue;
8355 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008356 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008357 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008358 lib->freef(lib->data, result);
8359 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008360}
8361
8362/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008363 * xmlRelaxNGNextValue:
8364 * @ctxt: a Relax-NG validation context
8365 *
8366 * Skip to the next value when validating within a list
8367 *
8368 * Returns 0 if the operation succeeded or an error code.
8369 */
8370static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008371xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8372{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008373 xmlChar *cur;
8374
8375 cur = ctxt->state->value;
8376 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008377 ctxt->state->value = NULL;
8378 ctxt->state->endvalue = NULL;
8379 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008380 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008381 while (*cur != 0)
8382 cur++;
8383 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8384 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008385 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008386 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008387 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008388 ctxt->state->value = cur;
8389 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008390}
8391
8392/**
8393 * xmlRelaxNGValidateValueList:
8394 * @ctxt: a Relax-NG validation context
8395 * @defines: the list of definitions to verify
8396 *
8397 * Validate the given set of definitions for the current value
8398 *
8399 * Returns 0 if the validation succeeded or an error code.
8400 */
8401static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008402xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8403 xmlRelaxNGDefinePtr defines)
8404{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008405 int ret = 0;
8406
8407 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008408 ret = xmlRelaxNGValidateValue(ctxt, defines);
8409 if (ret != 0)
8410 break;
8411 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008412 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008413 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008414}
8415
8416/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008417 * xmlRelaxNGValidateValue:
8418 * @ctxt: a Relax-NG validation context
8419 * @define: the definition to verify
8420 *
8421 * Validate the given definition for the current value
8422 *
8423 * Returns 0 if the validation succeeded or an error code.
8424 */
8425static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008426xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8427 xmlRelaxNGDefinePtr define)
8428{
Daniel Veillardedc91922003-01-26 00:52:04 +00008429 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008430 xmlChar *value;
8431
8432 value = ctxt->state->value;
8433 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008434 case XML_RELAXNG_EMPTY:{
8435 if ((value != NULL) && (value[0] != 0)) {
8436 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008437
Daniel Veillard4c004142003-10-07 11:33:24 +00008438 while (IS_BLANK(value[idx]))
8439 idx++;
8440 if (value[idx] != 0)
8441 ret = -1;
8442 }
8443 break;
8444 }
8445 case XML_RELAXNG_TEXT:
8446 break;
8447 case XML_RELAXNG_VALUE:{
8448 if (!xmlStrEqual(value, define->value)) {
8449 if (define->name != NULL) {
8450 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008451
Daniel Veillard4c004142003-10-07 11:33:24 +00008452 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8453 if ((lib != NULL) && (lib->comp != NULL)) {
8454 ret = lib->comp(lib->data, define->name,
8455 define->value, define->node,
8456 (void *) define->attrs,
8457 value, ctxt->state->node);
8458 } else
8459 ret = -1;
8460 if (ret < 0) {
8461 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8462 define->name);
8463 return (-1);
8464 } else if (ret == 1) {
8465 ret = 0;
8466 } else {
8467 ret = -1;
8468 }
8469 } else {
8470 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008471
Daniel Veillard4c004142003-10-07 11:33:24 +00008472 /*
8473 * TODO: trivial optimizations are possible by
8474 * computing at compile-time
8475 */
8476 nval = xmlRelaxNGNormalize(ctxt, define->value);
8477 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008478
Daniel Veillard4c004142003-10-07 11:33:24 +00008479 if ((nval == NULL) || (nvalue == NULL) ||
8480 (!xmlStrEqual(nval, nvalue)))
8481 ret = -1;
8482 if (nval != NULL)
8483 xmlFree(nval);
8484 if (nvalue != NULL)
8485 xmlFree(nvalue);
8486 }
8487 }
8488 if (ret == 0)
8489 xmlRelaxNGNextValue(ctxt);
8490 break;
8491 }
8492 case XML_RELAXNG_DATATYPE:{
8493 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8494 ctxt->state->seq);
8495 if (ret == 0)
8496 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008497
Daniel Veillard4c004142003-10-07 11:33:24 +00008498 break;
8499 }
8500 case XML_RELAXNG_CHOICE:{
8501 xmlRelaxNGDefinePtr list = define->content;
8502 xmlChar *oldvalue;
8503
8504 oldflags = ctxt->flags;
8505 ctxt->flags |= FLAGS_IGNORABLE;
8506
8507 oldvalue = ctxt->state->value;
8508 while (list != NULL) {
8509 ret = xmlRelaxNGValidateValue(ctxt, list);
8510 if (ret == 0) {
8511 break;
8512 }
8513 ctxt->state->value = oldvalue;
8514 list = list->next;
8515 }
8516 ctxt->flags = oldflags;
8517 if (ret != 0) {
8518 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8519 xmlRelaxNGDumpValidError(ctxt);
8520 } else {
8521 if (ctxt->errNr > 0)
8522 xmlRelaxNGPopErrors(ctxt, 0);
8523 }
8524 if (ret == 0)
8525 xmlRelaxNGNextValue(ctxt);
8526 break;
8527 }
8528 case XML_RELAXNG_LIST:{
8529 xmlRelaxNGDefinePtr list = define->content;
8530 xmlChar *oldvalue, *oldend, *val, *cur;
8531
Daniel Veillard416589a2003-02-17 17:25:42 +00008532#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008533 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008534#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008535
Daniel Veillard4c004142003-10-07 11:33:24 +00008536 oldvalue = ctxt->state->value;
8537 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008538
Daniel Veillard4c004142003-10-07 11:33:24 +00008539 val = xmlStrdup(oldvalue);
8540 if (val == NULL) {
8541 val = xmlStrdup(BAD_CAST "");
8542 }
8543 if (val == NULL) {
8544 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8545 return (-1);
8546 }
8547 cur = val;
8548 while (*cur != 0) {
8549 if (IS_BLANK(*cur)) {
8550 *cur = 0;
8551 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008552#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008553 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008554#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008555 while (IS_BLANK(*cur))
8556 *cur++ = 0;
8557 } else
8558 cur++;
8559 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008560#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008561 xmlGenericError(xmlGenericErrorContext,
8562 "list value: '%s' found %d items\n",
8563 oldvalue, nb_values);
8564 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008565#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008566 ctxt->state->endvalue = cur;
8567 cur = val;
8568 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8569 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008570
Daniel Veillard4c004142003-10-07 11:33:24 +00008571 ctxt->state->value = cur;
8572
8573 while (list != NULL) {
8574 if (ctxt->state->value == ctxt->state->endvalue)
8575 ctxt->state->value = NULL;
8576 ret = xmlRelaxNGValidateValue(ctxt, list);
8577 if (ret != 0) {
8578#ifdef DEBUG_LIST
8579 xmlGenericError(xmlGenericErrorContext,
8580 "Failed to validate value: '%s' with %d rule\n",
8581 ctxt->state->value, nb_values);
8582#endif
8583 break;
8584 }
8585#ifdef DEBUG_LIST
8586 nb_values++;
8587#endif
8588 list = list->next;
8589 }
8590
8591 if ((ret == 0) && (ctxt->state->value != NULL) &&
8592 (ctxt->state->value != ctxt->state->endvalue)) {
8593 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8594 ctxt->state->value);
8595 ret = -1;
8596 }
8597 xmlFree(val);
8598 ctxt->state->value = oldvalue;
8599 ctxt->state->endvalue = oldend;
8600 break;
8601 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008602 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008603 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8604 if (ret != 0) {
8605 break;
8606 }
8607 /* no break on purpose */
8608 case XML_RELAXNG_ZEROORMORE:{
8609 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008610
Daniel Veillard4c004142003-10-07 11:33:24 +00008611 oldflags = ctxt->flags;
8612 ctxt->flags |= FLAGS_IGNORABLE;
8613 cur = ctxt->state->value;
8614 temp = NULL;
8615 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8616 (temp != cur)) {
8617 temp = cur;
8618 ret =
8619 xmlRelaxNGValidateValueList(ctxt, define->content);
8620 if (ret != 0) {
8621 ctxt->state->value = temp;
8622 ret = 0;
8623 break;
8624 }
8625 cur = ctxt->state->value;
8626 }
8627 ctxt->flags = oldflags;
8628 if (ret != 0) {
8629 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8630 xmlRelaxNGDumpValidError(ctxt);
8631 } else {
8632 if (ctxt->errNr > 0)
8633 xmlRelaxNGPopErrors(ctxt, 0);
8634 }
8635 break;
8636 }
8637 case XML_RELAXNG_EXCEPT:{
8638 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008639
Daniel Veillard4c004142003-10-07 11:33:24 +00008640 list = define->content;
8641 while (list != NULL) {
8642 ret = xmlRelaxNGValidateValue(ctxt, list);
8643 if (ret == 0) {
8644 ret = -1;
8645 break;
8646 } else
8647 ret = 0;
8648 list = list->next;
8649 }
8650 break;
8651 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008652 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008653 case XML_RELAXNG_GROUP:{
8654 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008655
Daniel Veillard4c004142003-10-07 11:33:24 +00008656 list = define->content;
8657 while (list != NULL) {
8658 ret = xmlRelaxNGValidateValue(ctxt, list);
8659 if (ret != 0) {
8660 ret = -1;
8661 break;
8662 } else
8663 ret = 0;
8664 list = list->next;
8665 }
8666 break;
8667 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008668 case XML_RELAXNG_REF:
8669 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008670 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8671 break;
8672 default:
8673 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008674 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008675 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008676}
8677
8678/**
8679 * xmlRelaxNGValidateValueContent:
8680 * @ctxt: a Relax-NG validation context
8681 * @defines: the list of definitions to verify
8682 *
8683 * Validate the given definitions for the current value
8684 *
8685 * Returns 0 if the validation succeeded or an error code.
8686 */
8687static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008688xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8689 xmlRelaxNGDefinePtr defines)
8690{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008691 int ret = 0;
8692
8693 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008694 ret = xmlRelaxNGValidateValue(ctxt, defines);
8695 if (ret != 0)
8696 break;
8697 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008698 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008699 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008700}
8701
8702/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008703 * xmlRelaxNGAttributeMatch:
8704 * @ctxt: a Relax-NG validation context
8705 * @define: the definition to check
8706 * @prop: the attribute
8707 *
8708 * Check if the attribute matches the definition nameClass
8709 *
8710 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8711 */
8712static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008713xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8714 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8715{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008716 int ret;
8717
8718 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008719 if (!xmlStrEqual(define->name, prop->name))
8720 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008721 }
8722 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008723 if (define->ns[0] == 0) {
8724 if (prop->ns != NULL)
8725 return (0);
8726 } else {
8727 if ((prop->ns == NULL) ||
8728 (!xmlStrEqual(define->ns, prop->ns->href)))
8729 return (0);
8730 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008731 }
8732 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008733 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008734 define = define->nameClass;
8735 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008736 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008737
Daniel Veillard4c004142003-10-07 11:33:24 +00008738 list = define->content;
8739 while (list != NULL) {
8740 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8741 if (ret == 1)
8742 return (0);
8743 if (ret < 0)
8744 return (ret);
8745 list = list->next;
8746 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008747 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008748 TODO}
8749 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008750}
8751
8752/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008753 * xmlRelaxNGValidateAttribute:
8754 * @ctxt: a Relax-NG validation context
8755 * @define: the definition to verify
8756 *
8757 * Validate the given attribute definition for that node
8758 *
8759 * Returns 0 if the validation succeeded or an error code.
8760 */
8761static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008762xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8763 xmlRelaxNGDefinePtr define)
8764{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008765 int ret = 0, i;
8766 xmlChar *value, *oldvalue;
8767 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008768 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008769
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008770 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008771 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008772 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008773 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8774 tmp = ctxt->state->attrs[i];
8775 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8776 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8777 (tmp->ns == NULL)) ||
8778 ((tmp->ns != NULL) &&
8779 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8780 prop = tmp;
8781 break;
8782 }
8783 }
8784 }
8785 if (prop != NULL) {
8786 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8787 oldvalue = ctxt->state->value;
8788 oldseq = ctxt->state->seq;
8789 ctxt->state->seq = (xmlNodePtr) prop;
8790 ctxt->state->value = value;
8791 ctxt->state->endvalue = NULL;
8792 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8793 if (ctxt->state->value != NULL)
8794 value = ctxt->state->value;
8795 if (value != NULL)
8796 xmlFree(value);
8797 ctxt->state->value = oldvalue;
8798 ctxt->state->seq = oldseq;
8799 if (ret == 0) {
8800 /*
8801 * flag the attribute as processed
8802 */
8803 ctxt->state->attrs[i] = NULL;
8804 ctxt->state->nbAttrLeft--;
8805 }
8806 } else {
8807 ret = -1;
8808 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008809#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008810 xmlGenericError(xmlGenericErrorContext,
8811 "xmlRelaxNGValidateAttribute(%s): %d\n",
8812 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008813#endif
8814 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008815 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8816 tmp = ctxt->state->attrs[i];
8817 if ((tmp != NULL) &&
8818 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8819 prop = tmp;
8820 break;
8821 }
8822 }
8823 if (prop != NULL) {
8824 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8825 oldvalue = ctxt->state->value;
8826 oldseq = ctxt->state->seq;
8827 ctxt->state->seq = (xmlNodePtr) prop;
8828 ctxt->state->value = value;
8829 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8830 if (ctxt->state->value != NULL)
8831 value = ctxt->state->value;
8832 if (value != NULL)
8833 xmlFree(value);
8834 ctxt->state->value = oldvalue;
8835 ctxt->state->seq = oldseq;
8836 if (ret == 0) {
8837 /*
8838 * flag the attribute as processed
8839 */
8840 ctxt->state->attrs[i] = NULL;
8841 ctxt->state->nbAttrLeft--;
8842 }
8843 } else {
8844 ret = -1;
8845 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008846#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008847 if (define->ns != NULL) {
8848 xmlGenericError(xmlGenericErrorContext,
8849 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8850 define->ns, ret);
8851 } else {
8852 xmlGenericError(xmlGenericErrorContext,
8853 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8854 ret);
8855 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008856#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008857 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008858
8859 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008860}
8861
8862/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008863 * xmlRelaxNGValidateAttributeList:
8864 * @ctxt: a Relax-NG validation context
8865 * @define: the list of definition to verify
8866 *
8867 * Validate the given node against the list of attribute definitions
8868 *
8869 * Returns 0 if the validation succeeded or an error code.
8870 */
8871static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008872xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8873 xmlRelaxNGDefinePtr defines)
8874{
Daniel Veillardce192eb2003-04-16 15:58:05 +00008875 int ret = 0, res;
8876 int needmore = 0;
8877 xmlRelaxNGDefinePtr cur;
8878
8879 cur = defines;
8880 while (cur != NULL) {
8881 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008882 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8883 ret = -1;
8884 } else
8885 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008886 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008887 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008888 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00008889 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008890 cur = defines;
8891 while (cur != NULL) {
8892 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008893 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8894 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8895 if (res < 0)
8896 ret = -1;
8897 } else {
8898 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8899 return (-1);
8900 }
8901 if (res == -1) /* continues on -2 */
8902 break;
8903 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008904 cur = cur->next;
8905 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008906
8907 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008908}
8909
8910/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008911 * xmlRelaxNGNodeMatchesList:
8912 * @node: the node
8913 * @list: a NULL terminated array of definitions
8914 *
8915 * Check if a node can be matched by one of the definitions
8916 *
8917 * Returns 1 if matches 0 otherwise
8918 */
8919static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008920xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
8921{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008922 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008923 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008924
8925 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008926 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008927
8928 cur = list[i++];
8929 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008930 if ((node->type == XML_ELEMENT_NODE) &&
8931 (cur->type == XML_RELAXNG_ELEMENT)) {
8932 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8933 if (tmp == 1)
8934 return (1);
8935 } else if (((node->type == XML_TEXT_NODE) ||
8936 (node->type == XML_CDATA_SECTION_NODE)) &&
8937 (cur->type == XML_RELAXNG_TEXT)) {
8938 return (1);
8939 }
8940 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008941 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008942 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008943}
8944
8945/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008946 * xmlRelaxNGValidateInterleave:
8947 * @ctxt: a Relax-NG validation context
8948 * @define: the definition to verify
8949 *
8950 * Validate an interleave definition for a node.
8951 *
8952 * Returns 0 if the validation succeeded or an error code.
8953 */
8954static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008955xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
8956 xmlRelaxNGDefinePtr define)
8957{
William M. Brack779af002003-08-01 15:55:39 +00008958 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008959 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008960 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008961
8962 xmlRelaxNGValidStatePtr oldstate;
8963 xmlRelaxNGPartitionPtr partitions;
8964 xmlRelaxNGInterleaveGroupPtr group = NULL;
8965 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
8966 xmlNodePtr *list = NULL, *lasts = NULL;
8967
8968 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008969 partitions = (xmlRelaxNGPartitionPtr) define->data;
8970 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008971 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008972 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
8973 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008974 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008975 /*
8976 * Optimizations for MIXED
8977 */
8978 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00008979 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008980 ctxt->flags |= FLAGS_MIXED_CONTENT;
8981 if (nbgroups == 2) {
8982 /*
8983 * this is a pure <mixed> case
8984 */
8985 if (ctxt->state != NULL)
8986 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8987 ctxt->state->seq);
8988 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
8989 ret = xmlRelaxNGValidateDefinition(ctxt,
8990 partitions->groups[1]->
8991 rule);
8992 else
8993 ret = xmlRelaxNGValidateDefinition(ctxt,
8994 partitions->groups[0]->
8995 rule);
8996 if (ret == 0) {
8997 if (ctxt->state != NULL)
8998 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8999 ctxt->state->
9000 seq);
9001 }
9002 ctxt->flags = oldflags;
9003 return (ret);
9004 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009005 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009006
9007 /*
9008 * Build arrays to store the first and last node of the chain
9009 * pertaining to each group
9010 */
9011 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9012 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009013 xmlRngVErrMemory(ctxt, "validating\n");
9014 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009015 }
9016 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9017 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9018 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009019 xmlRngVErrMemory(ctxt, "validating\n");
9020 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009021 }
9022 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9023
9024 /*
9025 * Walk the sequence of children finding the right group and
9026 * sorting them in sequences.
9027 */
9028 cur = ctxt->state->seq;
9029 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9030 start = cur;
9031 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009032 ctxt->state->seq = cur;
9033 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009034 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009035 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009036
Daniel Veillard4c004142003-10-07 11:33:24 +00009037 if ((cur->type == XML_TEXT_NODE) ||
9038 (cur->type == XML_CDATA_SECTION_NODE)) {
9039 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9040 NULL);
9041 } else if (cur->type == XML_ELEMENT_NODE) {
9042 if (cur->ns != NULL) {
9043 tmp = xmlHashLookup2(partitions->triage, cur->name,
9044 cur->ns->href);
9045 if (tmp == NULL)
9046 tmp = xmlHashLookup2(partitions->triage,
9047 BAD_CAST "#any",
9048 cur->ns->href);
9049 } else
9050 tmp =
9051 xmlHashLookup2(partitions->triage, cur->name,
9052 NULL);
9053 if (tmp == NULL)
9054 tmp =
9055 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9056 NULL);
9057 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009058
Daniel Veillard4c004142003-10-07 11:33:24 +00009059 if (tmp == NULL) {
9060 i = nbgroups;
9061 } else {
9062 i = ((long) tmp) - 1;
9063 if (partitions->flags & IS_NEEDCHECK) {
9064 group = partitions->groups[i];
9065 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9066 i = nbgroups;
9067 }
9068 }
9069 } else {
9070 for (i = 0; i < nbgroups; i++) {
9071 group = partitions->groups[i];
9072 if (group == NULL)
9073 continue;
9074 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9075 break;
9076 }
9077 }
9078 /*
9079 * We break as soon as an element not matched is found
9080 */
9081 if (i >= nbgroups) {
9082 break;
9083 }
9084 if (lasts[i] != NULL) {
9085 lasts[i]->next = cur;
9086 lasts[i] = cur;
9087 } else {
9088 list[i] = cur;
9089 lasts[i] = cur;
9090 }
9091 if (cur->next != NULL)
9092 lastchg = cur->next;
9093 else
9094 lastchg = cur;
9095 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009096 }
9097 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009098 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9099 ret = -1;
9100 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009101 }
9102 lastelem = cur;
9103 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009104 for (i = 0; i < nbgroups; i++) {
9105 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9106 group = partitions->groups[i];
9107 if (lasts[i] != NULL) {
9108 last = lasts[i]->next;
9109 lasts[i]->next = NULL;
9110 }
9111 ctxt->state->seq = list[i];
9112 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9113 if (ret != 0)
9114 break;
9115 if (ctxt->state != NULL) {
9116 cur = ctxt->state->seq;
9117 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9118 xmlRelaxNGFreeValidState(ctxt, oldstate);
9119 oldstate = ctxt->state;
9120 ctxt->state = NULL;
9121 if (cur != NULL) {
9122 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9123 ret = -1;
9124 ctxt->state = oldstate;
9125 goto done;
9126 }
9127 } else if (ctxt->states != NULL) {
9128 int j;
9129 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009130
Daniel Veillard4c004142003-10-07 11:33:24 +00009131 for (j = 0; j < ctxt->states->nbState; j++) {
9132 cur = ctxt->states->tabState[j]->seq;
9133 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9134 if (cur == NULL) {
9135 found = 1;
9136 break;
9137 }
9138 }
9139 if (ctxt->states->nbState > 0) {
9140 xmlRelaxNGFreeValidState(ctxt, oldstate);
9141 oldstate =
9142 ctxt->states->tabState[ctxt->states->nbState - 1];
9143 }
9144 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9145 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9146 }
9147 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9148 ctxt->states = NULL;
9149 if (found == 0) {
9150 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9151 ret = -1;
9152 ctxt->state = oldstate;
9153 goto done;
9154 }
9155 } else {
9156 ret = -1;
9157 break;
9158 }
9159 if (lasts[i] != NULL) {
9160 lasts[i]->next = last;
9161 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009162 }
9163 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009164 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009165 ctxt->state = oldstate;
9166 ctxt->state->seq = lastelem;
9167 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009168 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9169 ret = -1;
9170 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009171 }
9172
Daniel Veillard4c004142003-10-07 11:33:24 +00009173 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009174 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009175 /*
9176 * builds the next links chain from the prev one
9177 */
9178 cur = lastchg;
9179 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009180 if ((cur == start) || (cur->prev == NULL))
9181 break;
9182 cur->prev->next = cur;
9183 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009184 }
9185 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009186 if (ctxt->errNr > errNr)
9187 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009188 }
9189
9190 xmlFree(list);
9191 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009192 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009193}
9194
9195/**
9196 * xmlRelaxNGValidateDefinitionList:
9197 * @ctxt: a Relax-NG validation context
9198 * @define: the list of definition to verify
9199 *
9200 * Validate the given node content against the (list) of definitions
9201 *
9202 * Returns 0 if the validation succeeded or an error code.
9203 */
9204static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009205xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9206 xmlRelaxNGDefinePtr defines)
9207{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009208 int ret = 0, res;
9209
9210
Daniel Veillard952379b2003-03-17 15:37:12 +00009211 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009212 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9213 BAD_CAST "NULL definition list");
9214 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009215 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009216 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009217 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9218 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9219 if (res < 0)
9220 ret = -1;
9221 } else {
9222 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9223 return (-1);
9224 }
9225 if (res == -1) /* continues on -2 */
9226 break;
9227 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009228 }
9229
Daniel Veillard4c004142003-10-07 11:33:24 +00009230 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009231}
9232
9233/**
9234 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009235 * @ctxt: a Relax-NG validation context
9236 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009237 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009238 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009239 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009240 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009241 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009242 */
9243static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009244xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9245 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9246{
Daniel Veillard580ced82003-03-21 21:22:48 +00009247 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009248
Daniel Veillardfd573f12003-03-16 17:52:32 +00009249 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009250 if (!xmlStrEqual(elem->name, define->name)) {
9251 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9252 return (0);
9253 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009254 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009255 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009256 if (elem->ns == NULL) {
9257 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9258 return (0);
9259 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9260 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9261 elem->name, define->ns);
9262 return (0);
9263 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009264 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009265 (define->name == NULL)) {
9266 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9267 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009268 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009269 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9270 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009271 }
9272
9273 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009274 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009275
9276 define = define->nameClass;
9277 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009278 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009279
Daniel Veillard4c004142003-10-07 11:33:24 +00009280 if (ctxt != NULL) {
9281 oldflags = ctxt->flags;
9282 ctxt->flags |= FLAGS_IGNORABLE;
9283 }
9284
9285 list = define->content;
9286 while (list != NULL) {
9287 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9288 if (ret == 1) {
9289 if (ctxt != NULL)
9290 ctxt->flags = oldflags;
9291 return (0);
9292 }
9293 if (ret < 0) {
9294 if (ctxt != NULL)
9295 ctxt->flags = oldflags;
9296 return (ret);
9297 }
9298 list = list->next;
9299 }
9300 ret = 1;
9301 if (ctxt != NULL) {
9302 ctxt->flags = oldflags;
9303 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009304 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009305 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009306
Daniel Veillard4c004142003-10-07 11:33:24 +00009307 if (ctxt != NULL) {
9308 oldflags = ctxt->flags;
9309 ctxt->flags |= FLAGS_IGNORABLE;
9310 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009311
Daniel Veillard4c004142003-10-07 11:33:24 +00009312 list = define->nameClass;
9313 while (list != NULL) {
9314 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9315 if (ret == 1) {
9316 if (ctxt != NULL)
9317 ctxt->flags = oldflags;
9318 return (1);
9319 }
9320 if (ret < 0) {
9321 if (ctxt != NULL)
9322 ctxt->flags = oldflags;
9323 return (ret);
9324 }
9325 list = list->next;
9326 }
9327 if (ctxt != NULL) {
9328 if (ret != 0) {
9329 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9330 xmlRelaxNGDumpValidError(ctxt);
9331 } else {
9332 if (ctxt->errNr > 0)
9333 xmlRelaxNGPopErrors(ctxt, 0);
9334 }
9335 }
9336 ret = 0;
9337 if (ctxt != NULL) {
9338 ctxt->flags = oldflags;
9339 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009340 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009341 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009342 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009343 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009344}
9345
9346/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009347 * xmlRelaxNGBestState:
9348 * @ctxt: a Relax-NG validation context
9349 *
9350 * Find the "best" state in the ctxt->states list of states to report
9351 * errors about. I.e. a state with no element left in the child list
9352 * or the one with the less attributes left.
9353 * This is called only if a falidation error was detected
9354 *
9355 * Returns the index of the "best" state or -1 in case of error
9356 */
9357static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009358xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9359{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009360 xmlRelaxNGValidStatePtr state;
9361 int i, tmp;
9362 int best = -1;
9363 int value = 1000000;
9364
9365 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9366 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009367 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009368
Daniel Veillard4c004142003-10-07 11:33:24 +00009369 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009370 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009371 if (state == NULL)
9372 continue;
9373 if (state->seq != NULL) {
9374 if ((best == -1) || (value > 100000)) {
9375 value = 100000;
9376 best = i;
9377 }
9378 } else {
9379 tmp = state->nbAttrLeft;
9380 if ((best == -1) || (value > tmp)) {
9381 value = tmp;
9382 best = i;
9383 }
9384 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009385 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009386 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009387}
9388
9389/**
9390 * xmlRelaxNGLogBestError:
9391 * @ctxt: a Relax-NG validation context
9392 *
9393 * Find the "best" state in the ctxt->states list of states to report
9394 * errors about and log it.
9395 */
9396static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009397xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9398{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009399 int best;
9400
9401 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9402 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009403 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009404
9405 best = xmlRelaxNGBestState(ctxt);
9406 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009407 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009408
Daniel Veillard4c004142003-10-07 11:33:24 +00009409 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009410 }
9411}
9412
9413/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009414 * xmlRelaxNGValidateElementEnd:
9415 * @ctxt: a Relax-NG validation context
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009416 * @log: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009417 *
9418 * Validate the end of the element, implements check that
9419 * there is nothing left not consumed in the element content
9420 * or in the attribute list.
9421 *
9422 * Returns 0 if the validation succeeded or an error code.
9423 */
9424static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009425xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log)
9426{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009427 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009428 xmlRelaxNGValidStatePtr state;
9429
9430 state = ctxt->state;
9431 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009432 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9433 if (state->seq != NULL) {
9434 if (log) {
9435 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9436 state->node->name, state->seq->name);
9437 }
9438 return (-1);
9439 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009440 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009441 for (i = 0; i < state->nbAttrs; i++) {
9442 if (state->attrs[i] != NULL) {
9443 if (log) {
9444 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9445 state->attrs[i]->name, state->node->name);
9446 }
9447 return (-1 - i);
9448 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009449 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009450 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009451}
9452
9453/**
9454 * xmlRelaxNGValidateState:
9455 * @ctxt: a Relax-NG validation context
9456 * @define: the definition to verify
9457 *
9458 * Validate the current state against the definition
9459 *
9460 * Returns 0 if the validation succeeded or an error code.
9461 */
9462static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009463xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9464 xmlRelaxNGDefinePtr define)
9465{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009466 xmlNodePtr node;
9467 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009468 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009469
9470 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009471 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9472 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009473 }
9474
9475 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009476 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009477 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009478 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009479 }
9480#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009481 for (i = 0; i < ctxt->depth; i++)
9482 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009483 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009484 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009485 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009486 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009487 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009488 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009489 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009490 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009491#endif
9492 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009493 switch (define->type) {
9494 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009495 node = xmlRelaxNGSkipIgnored(ctxt, node);
9496 ret = 0;
9497 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009498 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009499 ret = -1;
9500 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009501 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009502 while ((node != NULL) &&
9503 ((node->type == XML_TEXT_NODE) ||
9504 (node->type == XML_COMMENT_NODE) ||
9505 (node->type == XML_PI_NODE) ||
9506 (node->type == XML_CDATA_SECTION_NODE)))
9507 node = node->next;
9508 ctxt->state->seq = node;
9509 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009510 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009511 errNr = ctxt->errNr;
9512 node = xmlRelaxNGSkipIgnored(ctxt, node);
9513 if (node == NULL) {
9514 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9515 ret = -1;
9516 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9517 xmlRelaxNGDumpValidError(ctxt);
9518 break;
9519 }
9520 if (node->type != XML_ELEMENT_NODE) {
9521 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9522 ret = -1;
9523 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9524 xmlRelaxNGDumpValidError(ctxt);
9525 break;
9526 }
9527 /*
9528 * This node was already validated successfully against
9529 * this definition.
9530 */
9531 if (node->_private == define) {
9532 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9533 if (ctxt->errNr > errNr)
9534 xmlRelaxNGPopErrors(ctxt, errNr);
9535 if (ctxt->errNr != 0) {
9536 while ((ctxt->err != NULL) &&
9537 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9538 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9539 ||
9540 ((ctxt->err->err ==
9541 XML_RELAXNG_ERR_ELEMEXTRANS)
9542 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9543 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9544 || (ctxt->err->err ==
9545 XML_RELAXNG_ERR_NOTELEM)))
9546 xmlRelaxNGValidErrorPop(ctxt);
9547 }
9548 break;
9549 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009550
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009551 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9552 if (ret <= 0) {
9553 ret = -1;
9554 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9555 xmlRelaxNGDumpValidError(ctxt);
9556 break;
9557 }
9558 ret = 0;
9559 if (ctxt->errNr != 0) {
9560 if (ctxt->errNr > errNr)
9561 xmlRelaxNGPopErrors(ctxt, errNr);
9562 while ((ctxt->err != NULL) &&
9563 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9564 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9565 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9566 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9567 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9568 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9569 xmlRelaxNGValidErrorPop(ctxt);
9570 }
9571 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009572
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009573 oldflags = ctxt->flags;
9574 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9575 ctxt->flags -= FLAGS_MIXED_CONTENT;
9576 }
9577 state = xmlRelaxNGNewValidState(ctxt, node);
9578 if (state == NULL) {
9579 ret = -1;
9580 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9581 xmlRelaxNGDumpValidError(ctxt);
9582 break;
9583 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009584
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009585 oldstate = ctxt->state;
9586 ctxt->state = state;
9587 if (define->attrs != NULL) {
9588 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9589 if (tmp != 0) {
9590 ret = -1;
9591 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9592 }
9593 }
9594 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009595 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9596 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9597 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009598
Daniel Veillard4c004142003-10-07 11:33:24 +00009599 nstate = xmlRelaxNGNewValidState(ctxt, node);
9600 ctxt->state = nstate;
9601 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009602
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009603 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9604 define->contModel,
9605 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009606 nseq = ctxt->state->seq;
9607 ctxt->state = tmpstate;
9608 ctxt->states = tmpstates;
9609 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009610
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009611#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009612 xmlGenericError(xmlGenericErrorContext,
9613 "Validating content of '%s' : %d\n",
9614 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009615#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009616 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009617 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009618
9619 if (ctxt->states != NULL) {
9620 tmp = -1;
9621
Daniel Veillardce192eb2003-04-16 15:58:05 +00009622 for (i = 0; i < ctxt->states->nbState; i++) {
9623 state = ctxt->states->tabState[i];
9624 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009625 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009626
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009627 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009628 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009629 break;
9630 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009631 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009632 if (tmp != 0) {
9633 /*
9634 * validation error, log the message for the "best" one
9635 */
9636 ctxt->flags |= FLAGS_IGNORABLE;
9637 xmlRelaxNGLogBestError(ctxt);
9638 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009639 for (i = 0; i < ctxt->states->nbState; i++) {
9640 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009641 ctxt->states->
9642 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009643 }
9644 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9645 ctxt->flags = oldflags;
9646 ctxt->states = NULL;
9647 if ((ret == 0) && (tmp == -1))
9648 ret = -1;
9649 } else {
9650 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009651 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009652 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009653 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009654 xmlRelaxNGFreeValidState(ctxt, state);
9655 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009656 } else {
9657 if (define->content != NULL) {
9658 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009659 define->
9660 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009661 if (tmp != 0) {
9662 ret = -1;
9663 if (ctxt->state == NULL) {
9664 ctxt->state = oldstate;
9665 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9666 node->name);
9667 ctxt->state = NULL;
9668 } else {
9669 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9670 node->name);
9671 }
9672
9673 }
9674 }
9675 if (ctxt->states != NULL) {
9676 tmp = -1;
9677
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009678 for (i = 0; i < ctxt->states->nbState; i++) {
9679 state = ctxt->states->tabState[i];
9680 ctxt->state = state;
9681
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009682 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009683 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009684 break;
9685 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009686 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009687 if (tmp != 0) {
9688 /*
9689 * validation error, log the message for the "best" one
9690 */
9691 ctxt->flags |= FLAGS_IGNORABLE;
9692 xmlRelaxNGLogBestError(ctxt);
9693 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009694 for (i = 0; i < ctxt->states->nbState; i++) {
9695 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009696 ctxt->states->
9697 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009698 }
9699 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9700 ctxt->flags = oldflags;
9701 ctxt->states = NULL;
9702 if ((ret == 0) && (tmp == -1))
9703 ret = -1;
9704 } else {
9705 state = ctxt->state;
9706 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009707 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009708 xmlRelaxNGFreeValidState(ctxt, state);
9709 }
9710 }
9711 if (ret == 0) {
9712 node->_private = define;
9713 }
9714 ctxt->flags = oldflags;
9715 ctxt->state = oldstate;
9716 if (oldstate != NULL)
9717 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9718 if (ret != 0) {
9719 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9720 xmlRelaxNGDumpValidError(ctxt);
9721 ret = 0;
9722 } else {
9723 ret = -2;
9724 }
9725 } else {
9726 if (ctxt->errNr > errNr)
9727 xmlRelaxNGPopErrors(ctxt, errNr);
9728 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009729
9730#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009731 xmlGenericError(xmlGenericErrorContext,
9732 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9733 node->name, ret);
9734 if (oldstate == NULL)
9735 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9736 else if (oldstate->seq == NULL)
9737 xmlGenericError(xmlGenericErrorContext, ": done\n");
9738 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9739 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9740 oldstate->seq->name);
9741 else
9742 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9743 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009744#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009745 break;
9746 case XML_RELAXNG_OPTIONAL:{
9747 errNr = ctxt->errNr;
9748 oldflags = ctxt->flags;
9749 ctxt->flags |= FLAGS_IGNORABLE;
9750 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9751 ret =
9752 xmlRelaxNGValidateDefinitionList(ctxt,
9753 define->content);
9754 if (ret != 0) {
9755 if (ctxt->state != NULL)
9756 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9757 ctxt->state = oldstate;
9758 ctxt->flags = oldflags;
9759 ret = 0;
9760 if (ctxt->errNr > errNr)
9761 xmlRelaxNGPopErrors(ctxt, errNr);
9762 break;
9763 }
9764 if (ctxt->states != NULL) {
9765 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9766 } else {
9767 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9768 if (ctxt->states == NULL) {
9769 xmlRelaxNGFreeValidState(ctxt, oldstate);
9770 ctxt->flags = oldflags;
9771 ret = -1;
9772 if (ctxt->errNr > errNr)
9773 xmlRelaxNGPopErrors(ctxt, errNr);
9774 break;
9775 }
9776 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9777 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9778 ctxt->state = NULL;
9779 }
9780 ctxt->flags = oldflags;
9781 ret = 0;
9782 if (ctxt->errNr > errNr)
9783 xmlRelaxNGPopErrors(ctxt, errNr);
9784 break;
9785 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009786 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009787 errNr = ctxt->errNr;
9788 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9789 if (ret != 0) {
9790 break;
9791 }
9792 if (ctxt->errNr > errNr)
9793 xmlRelaxNGPopErrors(ctxt, errNr);
9794 /* no break on purpose */
9795 case XML_RELAXNG_ZEROORMORE:{
9796 int progress;
9797 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9798 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009799
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009800 errNr = ctxt->errNr;
9801 res = xmlRelaxNGNewStates(ctxt, 1);
9802 if (res == NULL) {
9803 ret = -1;
9804 break;
9805 }
9806 /*
9807 * All the input states are also exit states
9808 */
9809 if (ctxt->state != NULL) {
9810 xmlRelaxNGAddStates(ctxt, res,
9811 xmlRelaxNGCopyValidState(ctxt,
9812 ctxt->
9813 state));
9814 } else {
9815 for (j = 0; j < ctxt->states->nbState; j++) {
9816 xmlRelaxNGAddStates(ctxt, res,
9817 xmlRelaxNGCopyValidState(ctxt,
9818 ctxt->
9819 states->
9820 tabState
9821 [j]));
9822 }
9823 }
9824 oldflags = ctxt->flags;
9825 ctxt->flags |= FLAGS_IGNORABLE;
9826 do {
9827 progress = 0;
9828 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009829
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009830 if (ctxt->states != NULL) {
9831 states = ctxt->states;
9832 for (i = 0; i < states->nbState; i++) {
9833 ctxt->state = states->tabState[i];
9834 ctxt->states = NULL;
9835 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9836 define->
9837 content);
9838 if (ret == 0) {
9839 if (ctxt->state != NULL) {
9840 tmp = xmlRelaxNGAddStates(ctxt, res,
9841 ctxt->state);
9842 ctxt->state = NULL;
9843 if (tmp == 1)
9844 progress = 1;
9845 } else if (ctxt->states != NULL) {
9846 for (j = 0; j < ctxt->states->nbState;
9847 j++) {
9848 tmp =
9849 xmlRelaxNGAddStates(ctxt, res,
9850 ctxt->
9851 states->
9852 tabState
9853 [j]);
9854 if (tmp == 1)
9855 progress = 1;
9856 }
9857 xmlRelaxNGFreeStates(ctxt,
9858 ctxt->states);
9859 ctxt->states = NULL;
9860 }
9861 } else {
9862 if (ctxt->state != NULL) {
9863 xmlRelaxNGFreeValidState(ctxt,
9864 ctxt->state);
9865 ctxt->state = NULL;
9866 }
9867 }
9868 }
9869 } else {
9870 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9871 define->
9872 content);
9873 if (ret != 0) {
9874 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9875 ctxt->state = NULL;
9876 } else {
9877 base = res->nbState;
9878 if (ctxt->state != NULL) {
9879 tmp = xmlRelaxNGAddStates(ctxt, res,
9880 ctxt->state);
9881 ctxt->state = NULL;
9882 if (tmp == 1)
9883 progress = 1;
9884 } else if (ctxt->states != NULL) {
9885 for (j = 0; j < ctxt->states->nbState; j++) {
9886 tmp = xmlRelaxNGAddStates(ctxt, res,
9887 ctxt->
9888 states->
9889 tabState[j]);
9890 if (tmp == 1)
9891 progress = 1;
9892 }
9893 if (states == NULL) {
9894 states = ctxt->states;
9895 } else {
9896 xmlRelaxNGFreeStates(ctxt,
9897 ctxt->states);
9898 }
9899 ctxt->states = NULL;
9900 }
9901 }
9902 }
9903 if (progress) {
9904 /*
9905 * Collect all the new nodes added at that step
9906 * and make them the new node set
9907 */
9908 if (res->nbState - base == 1) {
9909 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9910 res->
9911 tabState
9912 [base]);
9913 } else {
9914 if (states == NULL) {
9915 xmlRelaxNGNewStates(ctxt,
9916 res->nbState - base);
9917 }
9918 states->nbState = 0;
9919 for (i = base; i < res->nbState; i++)
9920 xmlRelaxNGAddStates(ctxt, states,
9921 xmlRelaxNGCopyValidState
9922 (ctxt,
9923 res->tabState[i]));
9924 ctxt->states = states;
9925 }
9926 }
9927 } while (progress == 1);
9928 if (states != NULL) {
9929 xmlRelaxNGFreeStates(ctxt, states);
9930 }
9931 ctxt->states = res;
9932 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009933#if 0
9934 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00009935 * errors may have to be propagated back...
9936 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009937 if (ctxt->errNr > errNr)
9938 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009939#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009940 ret = 0;
9941 break;
9942 }
9943 case XML_RELAXNG_CHOICE:{
9944 xmlRelaxNGDefinePtr list = NULL;
9945 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009946
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009947 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009948
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009949 errNr = ctxt->errNr;
9950 if ((define->dflags & IS_TRIABLE)
9951 && (define->data != NULL)) {
9952 xmlHashTablePtr triage =
9953 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +00009954
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009955 /*
9956 * Something we can optimize cleanly there is only one
9957 * possble branch out !
9958 */
9959 if (node == NULL) {
9960 ret = -1;
9961 break;
9962 }
9963 if ((node->type == XML_TEXT_NODE) ||
9964 (node->type == XML_CDATA_SECTION_NODE)) {
9965 list =
9966 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
9967 } else if (node->type == XML_ELEMENT_NODE) {
9968 if (node->ns != NULL) {
9969 list = xmlHashLookup2(triage, node->name,
9970 node->ns->href);
9971 if (list == NULL)
9972 list =
9973 xmlHashLookup2(triage, BAD_CAST "#any",
9974 node->ns->href);
9975 } else
9976 list =
9977 xmlHashLookup2(triage, node->name, NULL);
9978 if (list == NULL)
9979 list =
9980 xmlHashLookup2(triage, BAD_CAST "#any",
9981 NULL);
9982 }
9983 if (list == NULL) {
9984 ret = -1;
9985 break;
9986 }
9987 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9988 if (ret == 0) {
9989 }
9990 break;
9991 }
Daniel Veillarde063f482003-03-21 16:53:17 +00009992
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009993 list = define->content;
9994 oldflags = ctxt->flags;
9995 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009996
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009997 while (list != NULL) {
9998 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9999 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10000 if (ret == 0) {
10001 if (states == NULL) {
10002 states = xmlRelaxNGNewStates(ctxt, 1);
10003 }
10004 if (ctxt->state != NULL) {
10005 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10006 } else if (ctxt->states != NULL) {
10007 for (i = 0; i < ctxt->states->nbState; i++) {
10008 xmlRelaxNGAddStates(ctxt, states,
10009 ctxt->states->
10010 tabState[i]);
10011 }
10012 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10013 ctxt->states = NULL;
10014 }
10015 } else {
10016 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10017 }
10018 ctxt->state = oldstate;
10019 list = list->next;
10020 }
10021 if (states != NULL) {
10022 xmlRelaxNGFreeValidState(ctxt, oldstate);
10023 ctxt->states = states;
10024 ctxt->state = NULL;
10025 ret = 0;
10026 } else {
10027 ctxt->states = NULL;
10028 }
10029 ctxt->flags = oldflags;
10030 if (ret != 0) {
10031 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10032 xmlRelaxNGDumpValidError(ctxt);
10033 }
10034 } else {
10035 if (ctxt->errNr > errNr)
10036 xmlRelaxNGPopErrors(ctxt, errNr);
10037 }
10038 break;
10039 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010040 case XML_RELAXNG_DEF:
10041 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010042 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10043 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010044 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010045 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10046 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010047 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010048 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10049 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010050 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010051 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010052 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010053 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010054 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010055 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10056 break;
10057 case XML_RELAXNG_DATATYPE:{
10058 xmlNodePtr child;
10059 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010060
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010061 child = node;
10062 while (child != NULL) {
10063 if (child->type == XML_ELEMENT_NODE) {
10064 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10065 node->parent->name);
10066 ret = -1;
10067 break;
10068 } else if ((child->type == XML_TEXT_NODE) ||
10069 (child->type == XML_CDATA_SECTION_NODE)) {
10070 content = xmlStrcat(content, child->content);
10071 }
10072 /* TODO: handle entities ... */
10073 child = child->next;
10074 }
10075 if (ret == -1) {
10076 if (content != NULL)
10077 xmlFree(content);
10078 break;
10079 }
10080 if (content == NULL) {
10081 content = xmlStrdup(BAD_CAST "");
10082 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010083 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010084 ret = -1;
10085 break;
10086 }
10087 }
10088 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10089 ctxt->state->seq);
10090 if (ret == -1) {
10091 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10092 } else if (ret == 0) {
10093 ctxt->state->seq = NULL;
10094 }
10095 if (content != NULL)
10096 xmlFree(content);
10097 break;
10098 }
10099 case XML_RELAXNG_VALUE:{
10100 xmlChar *content = NULL;
10101 xmlChar *oldvalue;
10102 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010103
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010104 child = node;
10105 while (child != NULL) {
10106 if (child->type == XML_ELEMENT_NODE) {
10107 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10108 node->parent->name);
10109 ret = -1;
10110 break;
10111 } else if ((child->type == XML_TEXT_NODE) ||
10112 (child->type == XML_CDATA_SECTION_NODE)) {
10113 content = xmlStrcat(content, child->content);
10114 }
10115 /* TODO: handle entities ... */
10116 child = child->next;
10117 }
10118 if (ret == -1) {
10119 if (content != NULL)
10120 xmlFree(content);
10121 break;
10122 }
10123 if (content == NULL) {
10124 content = xmlStrdup(BAD_CAST "");
10125 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010126 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010127 ret = -1;
10128 break;
10129 }
10130 }
10131 oldvalue = ctxt->state->value;
10132 ctxt->state->value = content;
10133 ret = xmlRelaxNGValidateValue(ctxt, define);
10134 ctxt->state->value = oldvalue;
10135 if (ret == -1) {
10136 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10137 } else if (ret == 0) {
10138 ctxt->state->seq = NULL;
10139 }
10140 if (content != NULL)
10141 xmlFree(content);
10142 break;
10143 }
10144 case XML_RELAXNG_LIST:{
10145 xmlChar *content;
10146 xmlNodePtr child;
10147 xmlChar *oldvalue, *oldendvalue;
10148 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010149
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010150 /*
10151 * Make sure it's only text nodes
10152 */
10153
10154 content = NULL;
10155 child = node;
10156 while (child != NULL) {
10157 if (child->type == XML_ELEMENT_NODE) {
10158 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10159 node->parent->name);
10160 ret = -1;
10161 break;
10162 } else if ((child->type == XML_TEXT_NODE) ||
10163 (child->type == XML_CDATA_SECTION_NODE)) {
10164 content = xmlStrcat(content, child->content);
10165 }
10166 /* TODO: handle entities ... */
10167 child = child->next;
10168 }
10169 if (ret == -1) {
10170 if (content != NULL)
10171 xmlFree(content);
10172 break;
10173 }
10174 if (content == NULL) {
10175 content = xmlStrdup(BAD_CAST "");
10176 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010177 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010178 ret = -1;
10179 break;
10180 }
10181 }
10182 len = xmlStrlen(content);
10183 oldvalue = ctxt->state->value;
10184 oldendvalue = ctxt->state->endvalue;
10185 ctxt->state->value = content;
10186 ctxt->state->endvalue = content + len;
10187 ret = xmlRelaxNGValidateValue(ctxt, define);
10188 ctxt->state->value = oldvalue;
10189 ctxt->state->endvalue = oldendvalue;
10190 if (ret == -1) {
10191 VALID_ERR(XML_RELAXNG_ERR_LIST);
10192 } else if ((ret == 0) && (node != NULL)) {
10193 ctxt->state->seq = node->next;
10194 }
10195 if (content != NULL)
10196 xmlFree(content);
10197 break;
10198 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010199 case XML_RELAXNG_EXCEPT:
10200 case XML_RELAXNG_PARAM:
10201 TODO ret = -1;
10202 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010203 }
10204 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010205#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010206 for (i = 0; i < ctxt->depth; i++)
10207 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010208 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010209 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010210 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010211 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010212 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010213 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010214 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010215 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010216#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010217 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010218}
10219
10220/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010221 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010222 * @ctxt: a Relax-NG validation context
10223 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010224 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010225 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010226 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010227 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010228 */
10229static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010230xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10231 xmlRelaxNGDefinePtr define)
10232{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010233 xmlRelaxNGStatesPtr states, res;
10234 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010235
Daniel Veillardfd573f12003-03-16 17:52:32 +000010236 /*
10237 * We should NOT have both ctxt->state and ctxt->states
10238 */
10239 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010240 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10241 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010242 }
10243
10244 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010245 if (ctxt->states != NULL) {
10246 ctxt->state = ctxt->states->tabState[0];
10247 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10248 ctxt->states = NULL;
10249 }
10250 ret = xmlRelaxNGValidateState(ctxt, define);
10251 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10252 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10253 ctxt->state = NULL;
10254 }
10255 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10256 ctxt->state = ctxt->states->tabState[0];
10257 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10258 ctxt->states = NULL;
10259 }
10260 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010261 }
10262
10263 states = ctxt->states;
10264 ctxt->states = NULL;
10265 res = NULL;
10266 j = 0;
10267 oldflags = ctxt->flags;
10268 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010269 for (i = 0; i < states->nbState; i++) {
10270 ctxt->state = states->tabState[i];
10271 ctxt->states = NULL;
10272 ret = xmlRelaxNGValidateState(ctxt, define);
10273 /*
10274 * We should NOT have both ctxt->state and ctxt->states
10275 */
10276 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10277 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10278 ctxt->state = NULL;
10279 }
10280 if (ret == 0) {
10281 if (ctxt->states == NULL) {
10282 if (res != NULL) {
10283 /* add the state to the container */
10284 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10285 ctxt->state = NULL;
10286 } else {
10287 /* add the state directly in states */
10288 states->tabState[j++] = ctxt->state;
10289 ctxt->state = NULL;
10290 }
10291 } else {
10292 if (res == NULL) {
10293 /* make it the new container and copy other results */
10294 res = ctxt->states;
10295 ctxt->states = NULL;
10296 for (k = 0; k < j; k++)
10297 xmlRelaxNGAddStates(ctxt, res,
10298 states->tabState[k]);
10299 } else {
10300 /* add all the new results to res and reff the container */
10301 for (k = 0; k < ctxt->states->nbState; k++)
10302 xmlRelaxNGAddStates(ctxt, res,
10303 ctxt->states->tabState[k]);
10304 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10305 ctxt->states = NULL;
10306 }
10307 }
10308 } else {
10309 if (ctxt->state != NULL) {
10310 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10311 ctxt->state = NULL;
10312 } else if (ctxt->states != NULL) {
10313 for (k = 0; k < ctxt->states->nbState; k++)
10314 xmlRelaxNGFreeValidState(ctxt,
10315 ctxt->states->tabState[k]);
10316 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10317 ctxt->states = NULL;
10318 }
10319 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010320 }
10321 ctxt->flags = oldflags;
10322 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010323 xmlRelaxNGFreeStates(ctxt, states);
10324 ctxt->states = res;
10325 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010326 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010327 states->nbState = j;
10328 ctxt->states = states;
10329 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010330 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010331 ctxt->state = states->tabState[0];
10332 xmlRelaxNGFreeStates(ctxt, states);
10333 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010334 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010335 ret = -1;
10336 xmlRelaxNGFreeStates(ctxt, states);
10337 if (ctxt->states != NULL) {
10338 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10339 ctxt->states = NULL;
10340 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010341 }
10342 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010343 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10344 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010345 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010346 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010347}
10348
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010349/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010350 * xmlRelaxNGValidateDocument:
10351 * @ctxt: a Relax-NG validation context
10352 * @doc: the document
10353 *
10354 * Validate the given document
10355 *
10356 * Returns 0 if the validation succeeded or an error code.
10357 */
10358static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010359xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10360{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010361 int ret;
10362 xmlRelaxNGPtr schema;
10363 xmlRelaxNGGrammarPtr grammar;
10364 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010365 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010366
10367 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010368 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010369
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010370 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010371 schema = ctxt->schema;
10372 grammar = schema->topgrammar;
10373 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010374 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10375 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010376 }
10377 state = xmlRelaxNGNewValidState(ctxt, NULL);
10378 ctxt->state = state;
10379 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010380 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010381 state = ctxt->state;
10382 node = state->seq;
10383 node = xmlRelaxNGSkipIgnored(ctxt, node);
10384 if (node != NULL) {
10385 if (ret != -1) {
10386 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10387 ret = -1;
10388 }
10389 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010390 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010391 int i;
10392 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010393
Daniel Veillard4c004142003-10-07 11:33:24 +000010394 for (i = 0; i < ctxt->states->nbState; i++) {
10395 state = ctxt->states->tabState[i];
10396 node = state->seq;
10397 node = xmlRelaxNGSkipIgnored(ctxt, node);
10398 if (node == NULL)
10399 tmp = 0;
10400 xmlRelaxNGFreeValidState(ctxt, state);
10401 }
10402 if (tmp == -1) {
10403 if (ret != -1) {
10404 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10405 ret = -1;
10406 }
10407 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010408 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010409 if (ctxt->state != NULL) {
10410 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010411 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010412 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010413 if (ret != 0)
10414 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010415#ifdef DEBUG
10416 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010417 ctxt->error(ctxt->userData,
10418 "%d Extra error messages left on stack !\n",
10419 ctxt->errNr);
10420 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010421 }
10422#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010423 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010424 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010425
Daniel Veillard4c004142003-10-07 11:33:24 +000010426 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10427 vctxt.valid = 1;
10428 vctxt.error = ctxt->error;
10429 vctxt.warning = ctxt->warning;
10430 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010431
Daniel Veillard4c004142003-10-07 11:33:24 +000010432 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10433 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010434 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010435 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010436 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010437
Daniel Veillard4c004142003-10-07 11:33:24 +000010438 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010439}
10440
Daniel Veillardfd573f12003-03-16 17:52:32 +000010441/************************************************************************
10442 * *
10443 * Validation interfaces *
10444 * *
10445 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010446
Daniel Veillard6eadf632003-01-23 18:29:16 +000010447/**
10448 * xmlRelaxNGNewValidCtxt:
10449 * @schema: a precompiled XML RelaxNGs
10450 *
10451 * Create an XML RelaxNGs validation context based on the given schema
10452 *
10453 * Returns the validation context or NULL in case of error
10454 */
10455xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010456xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10457{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010458 xmlRelaxNGValidCtxtPtr ret;
10459
10460 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10461 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010462 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010463 return (NULL);
10464 }
10465 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10466 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010467 ret->error = xmlGenericError;
10468 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010469 ret->errNr = 0;
10470 ret->errMax = 0;
10471 ret->err = NULL;
10472 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010473 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010474 ret->states = NULL;
10475 ret->freeState = NULL;
10476 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010477 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010478 return (ret);
10479}
10480
10481/**
10482 * xmlRelaxNGFreeValidCtxt:
10483 * @ctxt: the schema validation context
10484 *
10485 * Free the resources associated to the schema validation context
10486 */
10487void
Daniel Veillard4c004142003-10-07 11:33:24 +000010488xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10489{
Daniel Veillard798024a2003-03-19 10:36:09 +000010490 int k;
10491
Daniel Veillard6eadf632003-01-23 18:29:16 +000010492 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010493 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010494 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010495 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010496 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010497 for (k = 0; k < ctxt->freeState->nbState; k++) {
10498 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10499 }
10500 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010501 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010502 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010503 for (k = 0; k < ctxt->freeStatesNr; k++) {
10504 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10505 }
10506 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010507 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010508 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010509 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010510 if (ctxt->elemTab != NULL) {
10511 xmlRegExecCtxtPtr exec;
10512
Daniel Veillard4c004142003-10-07 11:33:24 +000010513 exec = xmlRelaxNGElemPop(ctxt);
10514 while (exec != NULL) {
10515 xmlRegFreeExecCtxt(exec);
10516 exec = xmlRelaxNGElemPop(ctxt);
10517 }
10518 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010519 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010520 xmlFree(ctxt);
10521}
10522
10523/**
10524 * xmlRelaxNGSetValidErrors:
10525 * @ctxt: a Relax-NG validation context
10526 * @err: the error function
10527 * @warn: the warning function
10528 * @ctx: the functions context
10529 *
10530 * Set the error and warning callback informations
10531 */
10532void
10533xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010534 xmlRelaxNGValidityErrorFunc err,
10535 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10536{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010537 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010538 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010539 ctxt->error = err;
10540 ctxt->warning = warn;
10541 ctxt->userData = ctx;
10542}
10543
10544/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010545 * xmlRelaxNGGetValidErrors:
10546 * @ctxt: a Relax-NG validation context
10547 * @err: the error function result
10548 * @warn: the warning function result
10549 * @ctx: the functions context result
10550 *
10551 * Get the error and warning callback informations
10552 *
10553 * Returns -1 in case of error and 0 otherwise
10554 */
10555int
10556xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010557 xmlRelaxNGValidityErrorFunc * err,
10558 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10559{
Daniel Veillard409a8142003-07-18 15:16:57 +000010560 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010561 return (-1);
10562 if (err != NULL)
10563 *err = ctxt->error;
10564 if (warn != NULL)
10565 *warn = ctxt->warning;
10566 if (ctx != NULL)
10567 *ctx = ctxt->userData;
10568 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010569}
10570
10571/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010572 * xmlRelaxNGValidateDoc:
10573 * @ctxt: a Relax-NG validation context
10574 * @doc: a parsed document tree
10575 *
10576 * Validate a document tree in memory.
10577 *
10578 * Returns 0 if the document is valid, a positive error code
10579 * number otherwise and -1 in case of internal or API error.
10580 */
10581int
Daniel Veillard4c004142003-10-07 11:33:24 +000010582xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10583{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010584 int ret;
10585
10586 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010587 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010588
10589 ctxt->doc = doc;
10590
10591 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010592 /*
10593 * TODO: build error codes
10594 */
10595 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010596 return (1);
10597 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010598}
10599
10600#endif /* LIBXML_SCHEMAS_ENABLED */