blob: 0330bec0f704fde5471364809ffd963b8ae291f9 [file] [log] [blame]
Daniel Veillard6eadf632003-01-23 18:29:16 +00001/*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
Daniel Veillardd41f4f42003-01-29 21:07:52 +00009/**
10 * TODO:
Daniel Veillardf4b4f982003-02-13 11:02:08 +000011 * - add support for DTD compatibility spec
12 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
Daniel Veillardac297932003-04-17 12:55:35 +000013 * - report better mem allocations pbms at runtime and abort immediately.
Daniel Veillardd41f4f42003-01-29 21:07:52 +000014 */
15
Daniel Veillard6eadf632003-01-23 18:29:16 +000016#define IN_LIBXML
17#include "libxml.h"
18
19#ifdef LIBXML_SCHEMAS_ENABLED
20
21#include <string.h>
22#include <stdio.h>
23#include <libxml/xmlmemory.h>
24#include <libxml/parser.h>
25#include <libxml/parserInternals.h>
26#include <libxml/hash.h>
27#include <libxml/uri.h>
28
29#include <libxml/relaxng.h>
30
31#include <libxml/xmlschemastypes.h>
32#include <libxml/xmlautomata.h>
33#include <libxml/xmlregexp.h>
Daniel Veillardc6e997c2003-01-27 12:35:42 +000034#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000035
36/*
37 * The Relax-NG namespace
38 */
39static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
40 "http://relaxng.org/ns/structure/1.0";
41
42#define IS_RELAXNG(node, type) \
43 ((node != NULL) && (node->ns != NULL) && \
44 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
45 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
46
47
Daniel Veillard952379b2003-03-17 15:37:12 +000048/* #define DEBUG 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000049
Daniel Veillardc482e262003-02-26 14:48:48 +000050/* #define DEBUG_GRAMMAR 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000051
Daniel Veillard71531f32003-02-05 13:19:53 +000052/* #define DEBUG_CONTENT 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000053
Daniel Veillard71531f32003-02-05 13:19:53 +000054/* #define DEBUG_TYPE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000055
Daniel Veillard71531f32003-02-05 13:19:53 +000056/* #define DEBUG_VALID 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000057
Daniel Veillarde5b110b2003-02-04 14:43:39 +000058/* #define DEBUG_INTERLEAVE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000059
Daniel Veillard416589a2003-02-17 17:25:42 +000060/* #define DEBUG_LIST 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000061
Daniel Veillard5add8682003-03-10 13:13:58 +000062/* #define DEBUG_INCLUDE */
Daniel Veillard4c004142003-10-07 11:33:24 +000063
Daniel Veillarda507fbf2003-03-31 16:09:37 +000064/* #define DEBUG_ERROR 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000065
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000066/* #define DEBUG_COMPILE 1 */
Daniel Veillard4c004142003-10-07 11:33:24 +000067
Daniel Veillardf4e55762003-04-15 23:32:22 +000068/* #define DEBUG_PROGRESSIVE 1 */
Daniel Veillard6eadf632003-01-23 18:29:16 +000069
Daniel Veillard5f1946a2003-03-31 16:38:16 +000070#define MAX_ERROR 5
71
Daniel Veillard6eadf632003-01-23 18:29:16 +000072#define TODO \
73 xmlGenericError(xmlGenericErrorContext, \
74 "Unimplemented block at %s:%d\n", \
75 __FILE__, __LINE__);
76
77typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
78typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
79
80typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
81typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
82
Daniel Veillardd41f4f42003-01-29 21:07:52 +000083typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
84typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
85
Daniel Veillarda9d912d2003-02-01 17:43:10 +000086typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
87typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
88
Daniel Veillard6eadf632003-01-23 18:29:16 +000089typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000090 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
91 XML_RELAXNG_COMBINE_CHOICE, /* choice */
92 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000093} xmlRelaxNGCombine;
94
Daniel Veillard4c5cf702003-02-21 15:40:34 +000095typedef enum {
96 XML_RELAXNG_CONTENT_ERROR = -1,
97 XML_RELAXNG_CONTENT_EMPTY = 0,
98 XML_RELAXNG_CONTENT_SIMPLE,
99 XML_RELAXNG_CONTENT_COMPLEX
100} xmlRelaxNGContentType;
101
Daniel Veillard6eadf632003-01-23 18:29:16 +0000102typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
103typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
104
105struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000106 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
107 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
108 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
109 xmlRelaxNGDefinePtr start; /* <start> content */
110 xmlRelaxNGCombine combine; /* the default combine value */
111 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
112 xmlHashTablePtr defs; /* define* */
113 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000114};
115
116
Daniel Veillard6eadf632003-01-23 18:29:16 +0000117typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000118 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
119 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000120 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000121 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
122 XML_RELAXNG_TEXT, /* textual content */
123 XML_RELAXNG_ELEMENT, /* an element */
124 XML_RELAXNG_DATATYPE, /* extenal data type definition */
125 XML_RELAXNG_PARAM, /* extenal data type parameter */
126 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
127 XML_RELAXNG_LIST, /* a list of patterns */
128 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
129 XML_RELAXNG_DEF, /* a definition */
130 XML_RELAXNG_REF, /* reference to a definition */
131 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
132 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
133 XML_RELAXNG_OPTIONAL, /* optional patterns */
134 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
135 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
136 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
137 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
138 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
139 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000140} xmlRelaxNGType;
141
Daniel Veillard52b48c72003-04-13 19:53:42 +0000142#define IS_NULLABLE (1 << 0)
143#define IS_NOT_NULLABLE (1 << 1)
144#define IS_INDETERMINIST (1 << 2)
145#define IS_MIXED (1 << 3)
146#define IS_TRIABLE (1 << 4)
147#define IS_PROCESSED (1 << 5)
148#define IS_COMPILABLE (1 << 6)
149#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000150
Daniel Veillard6eadf632003-01-23 18:29:16 +0000151struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000152 xmlRelaxNGType type; /* the type of definition */
153 xmlNodePtr node; /* the node in the source */
154 xmlChar *name; /* the element local name if present */
155 xmlChar *ns; /* the namespace local name if present */
156 xmlChar *value; /* value when available */
157 void *data; /* data lib or specific pointer */
158 xmlRelaxNGDefinePtr content; /* the expected content */
159 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
160 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
161 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
162 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
163 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
164 short depth; /* used for the cycle detection */
165 short dflags; /* define related flags */
166 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000167};
168
169/**
170 * _xmlRelaxNG:
171 *
172 * A RelaxNGs definition
173 */
174struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000175 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000176 xmlRelaxNGGrammarPtr topgrammar;
177 xmlDocPtr doc;
178
Daniel Veillard4c004142003-10-07 11:33:24 +0000179 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000180
Daniel Veillard4c004142003-10-07 11:33:24 +0000181 xmlHashTablePtr defs; /* define */
182 xmlHashTablePtr refs; /* references */
183 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
184 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
185 int defNr; /* number of defines used */
186 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000187
Daniel Veillard6eadf632003-01-23 18:29:16 +0000188};
189
Daniel Veillard77648bb2003-02-20 15:03:22 +0000190#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
191#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
192#define XML_RELAXNG_IN_LIST (1 << 2)
193#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
194#define XML_RELAXNG_IN_START (1 << 4)
195#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
196#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
197#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000198#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
199#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000200
201struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000202 void *userData; /* user specific data block */
203 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
204 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000205 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000206 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000207
Daniel Veillard4c004142003-10-07 11:33:24 +0000208 xmlRelaxNGPtr schema; /* The schema in use */
209 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
210 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
211 int flags; /* parser flags */
212 int nbErrors; /* number of errors at parse time */
213 int nbWarnings; /* number of warnings at parse time */
214 const xmlChar *define; /* the current define scope */
215 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000216
Daniel Veillard4c004142003-10-07 11:33:24 +0000217 int nbInterleaves;
218 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000219
Daniel Veillard4c004142003-10-07 11:33:24 +0000220 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
221 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
222 xmlChar *URL;
223 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000224
Daniel Veillard4c004142003-10-07 11:33:24 +0000225 int defNr; /* number of defines used */
226 int defMax; /* number of defines aloocated */
227 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000228
Daniel Veillard4c004142003-10-07 11:33:24 +0000229 const char *buffer;
230 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000231
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000232 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000233 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
234 int docNr; /* Depth of the parsing stack */
235 int docMax; /* Max depth of the parsing stack */
236 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000237
238 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000239 xmlRelaxNGIncludePtr inc; /* Current parsed include */
240 int incNr; /* Depth of the include parsing stack */
241 int incMax; /* Max depth of the parsing stack */
242 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000243
Daniel Veillard4c004142003-10-07 11:33:24 +0000244 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000245
246 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000247 xmlAutomataPtr am; /* the automata */
248 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000249};
250
251#define FLAGS_IGNORABLE 1
252#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000253#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000254
255/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000256 * xmlRelaxNGInterleaveGroup:
257 *
258 * A RelaxNGs partition set associated to lists of definitions
259 */
260typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
261typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
262struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000263 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
264 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
265 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000266};
267
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000268#define IS_DETERMINIST 1
269#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000270
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000271/**
272 * xmlRelaxNGPartitions:
273 *
274 * A RelaxNGs partition associated to an interleave group
275 */
276typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
277typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
278struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000279 int nbgroups; /* number of groups in the partitions */
280 xmlHashTablePtr triage; /* hash table used to direct nodes to the
281 * right group when possible */
282 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000283 xmlRelaxNGInterleaveGroupPtr *groups;
284};
285
286/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000287 * xmlRelaxNGValidState:
288 *
289 * A RelaxNGs validation state
290 */
291#define MAX_ATTR 20
292typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
293typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
294struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000295 xmlNodePtr node; /* the current node */
296 xmlNodePtr seq; /* the sequence of children left to validate */
297 int nbAttrs; /* the number of attributes */
298 int maxAttrs; /* the size of attrs */
299 int nbAttrLeft; /* the number of attributes left to validate */
300 xmlChar *value; /* the value when operating on string */
301 xmlChar *endvalue; /* the end value when operating on string */
302 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000303};
304
305/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000306 * xmlRelaxNGStates:
307 *
308 * A RelaxNGs container for validation state
309 */
310typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
311typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
312struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000313 int nbState; /* the number of states */
314 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000315 xmlRelaxNGValidStatePtr *tabState;
316};
317
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000318#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000319
Daniel Veillardfd573f12003-03-16 17:52:32 +0000320/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000321 * xmlRelaxNGValidError:
322 *
323 * A RelaxNGs validation error
324 */
325typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
326typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
327struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000328 xmlRelaxNGValidErr err; /* the error number */
329 int flags; /* flags */
330 xmlNodePtr node; /* the current node */
331 xmlNodePtr seq; /* the current child */
332 const xmlChar *arg1; /* first arg */
333 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000334};
335
336/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000337 * xmlRelaxNGValidCtxt:
338 *
339 * A RelaxNGs validation context
340 */
341
342struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000343 void *userData; /* user specific data block */
344 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
345 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000346 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000347 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000348
Daniel Veillard4c004142003-10-07 11:33:24 +0000349 xmlRelaxNGPtr schema; /* The schema in use */
350 xmlDocPtr doc; /* the document being validated */
351 int flags; /* validation flags */
352 int depth; /* validation depth */
353 int idref; /* requires idref checking */
354 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000355
356 /*
357 * Errors accumulated in branches may have to be stacked to be
358 * provided back when it's sure they affect validation.
359 */
360 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000361 int errNr; /* Depth of the error stack */
362 int errMax; /* Max depth of the error stack */
363 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000364
Daniel Veillard4c004142003-10-07 11:33:24 +0000365 xmlRelaxNGValidStatePtr state; /* the current validation state */
366 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000367
Daniel Veillard4c004142003-10-07 11:33:24 +0000368 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
369 int freeStatesNr;
370 int freeStatesMax;
371 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000372
373 /*
374 * This is used for "progressive" validation
375 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000376 xmlRegExecCtxtPtr elem; /* the current element regexp */
377 int elemNr; /* the number of element validated */
378 int elemMax; /* the max depth of elements */
379 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
380 int pstate; /* progressive state */
381 xmlNodePtr pnode; /* the current node */
382 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
383 int perr; /* signal error in content model
384 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000385};
386
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000387/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000388 * xmlRelaxNGInclude:
389 *
390 * Structure associated to a RelaxNGs document element
391 */
392struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000393 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
394 xmlChar *href; /* the normalized href value */
395 xmlDocPtr doc; /* the associated XML document */
396 xmlRelaxNGDefinePtr content; /* the definitions */
397 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000398};
399
400/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000401 * xmlRelaxNGDocument:
402 *
403 * Structure associated to a RelaxNGs document element
404 */
405struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000406 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000407 xmlChar *href; /* the normalized href value */
408 xmlDocPtr doc; /* the associated XML document */
409 xmlRelaxNGDefinePtr content; /* the definitions */
410 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000411};
412
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000413
Daniel Veillard6eadf632003-01-23 18:29:16 +0000414/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000415 * *
416 * Some factorized error routines *
417 * *
418 ************************************************************************/
419
420/**
421 * xmlRngPErrMemory:
422 * @ctxt: an Relax-NG parser context
423 * @extra: extra informations
424 *
425 * Handle a redefinition of attribute error
426 */
427static void
428xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
429{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000430 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000431 xmlGenericErrorFunc channel = NULL;
432 void *data = NULL;
433
434 if (ctxt != NULL) {
435 channel = ctxt->error;
436 data = ctxt->userData;
437 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000438 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000439 }
440 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000441 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000442 NULL, NULL, XML_FROM_RELAXNGP,
443 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
444 NULL, NULL, 0, 0,
445 "Memory allocation failed : %s\n", extra);
446 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000447 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000448 NULL, NULL, XML_FROM_RELAXNGP,
449 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
450 NULL, NULL, 0, 0, "Memory allocation failed\n");
451}
452
453/**
454 * xmlRngVErrMemory:
455 * @ctxt: a Relax-NG validation context
456 * @extra: extra informations
457 *
458 * Handle a redefinition of attribute error
459 */
460static void
461xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
462{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000463 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000464 xmlGenericErrorFunc channel = NULL;
465 void *data = NULL;
466
467 if (ctxt != NULL) {
468 channel = ctxt->error;
469 data = ctxt->userData;
470 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000471 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000472 }
473 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000474 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000475 NULL, NULL, XML_FROM_RELAXNGV,
476 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
477 NULL, NULL, 0, 0,
478 "Memory allocation failed : %s\n", extra);
479 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000480 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000481 NULL, NULL, XML_FROM_RELAXNGV,
482 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
483 NULL, NULL, 0, 0, "Memory allocation failed\n");
484}
485
486/**
487 * xmlRngPErr:
488 * @ctxt: a Relax-NG parser context
489 * @node: the node raising the error
490 * @error: the error code
491 * @msg: message
492 * @str1: extra info
493 * @str2: extra info
494 *
495 * Handle a Relax NG Parsing error
496 */
497static void
498xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
499 const char *msg, const xmlChar * str1, const xmlChar * str2)
500{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000501 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000502 xmlGenericErrorFunc channel = NULL;
503 void *data = NULL;
504
505 if (ctxt != NULL) {
506 channel = ctxt->error;
507 data = ctxt->userData;
508 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000509 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000510 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000511 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000512 NULL, node, XML_FROM_RELAXNGP,
513 error, XML_ERR_ERROR, NULL, 0,
514 (const char *) str1, (const char *) str2, NULL, 0, 0,
515 msg, str1, str2);
516}
517
518/**
519 * xmlRngVErr:
520 * @ctxt: a Relax-NG validation context
521 * @node: the node raising the error
522 * @error: the error code
523 * @msg: message
524 * @str1: extra info
525 * @str2: extra info
526 *
527 * Handle a Relax NG Validation error
528 */
529static void
530xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
531 const char *msg, const xmlChar * str1, const xmlChar * str2)
532{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000533 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000534 xmlGenericErrorFunc channel = NULL;
535 void *data = NULL;
536
537 if (ctxt != NULL) {
538 channel = ctxt->error;
539 data = ctxt->userData;
540 ctxt->nbErrors++;
Daniel Veillard659e71e2003-10-10 14:10:40 +0000541 schannel = ctxt->serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000542 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000543 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000544 NULL, node, XML_FROM_RELAXNGV,
545 error, XML_ERR_ERROR, NULL, 0,
546 (const char *) str1, (const char *) str2, NULL, 0, 0,
547 msg, str1, str2);
548}
549
550/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000551 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000552 * Preliminary type checking interfaces *
553 * *
554 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000555
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000556/**
557 * xmlRelaxNGTypeHave:
558 * @data: data needed for the library
559 * @type: the type name
560 * @value: the value to check
561 *
562 * Function provided by a type library to check if a type is exported
563 *
564 * Returns 1 if yes, 0 if no and -1 in case of error.
565 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000566typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000567
568/**
569 * xmlRelaxNGTypeCheck:
570 * @data: data needed for the library
571 * @type: the type name
572 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000573 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000574 *
575 * Function provided by a type library to check if a value match a type
576 *
577 * Returns 1 if yes, 0 if no and -1 in case of error.
578 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000579typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
580 const xmlChar * value, void **result,
581 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000582
583/**
584 * xmlRelaxNGFacetCheck:
585 * @data: data needed for the library
586 * @type: the type name
587 * @facet: the facet name
588 * @val: the facet value
589 * @strval: the string value
590 * @value: the value to check
591 *
592 * Function provided by a type library to check a value facet
593 *
594 * Returns 1 if yes, 0 if no and -1 in case of error.
595 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000596typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
597 const xmlChar * facet,
598 const xmlChar * val,
599 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000600
601/**
602 * xmlRelaxNGTypeFree:
603 * @data: data needed for the library
604 * @result: the value to free
605 *
606 * Function provided by a type library to free a returned result
607 */
608typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000609
610/**
611 * xmlRelaxNGTypeCompare:
612 * @data: data needed for the library
613 * @type: the type name
614 * @value1: the first value
615 * @value2: the second value
616 *
617 * Function provided by a type library to compare two values accordingly
618 * to a type.
619 *
620 * Returns 1 if yes, 0 if no and -1 in case of error.
621 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000622typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
623 const xmlChar * value1,
624 xmlNodePtr ctxt1,
625 void *comp1,
626 const xmlChar * value2,
627 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000628typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
629typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
630struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000631 const xmlChar *namespace; /* the datatypeLibrary value */
632 void *data; /* data needed for the library */
633 xmlRelaxNGTypeHave have; /* the export function */
634 xmlRelaxNGTypeCheck check; /* the checking function */
635 xmlRelaxNGTypeCompare comp; /* the compare function */
636 xmlRelaxNGFacetCheck facet; /* the facet check function */
637 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000638};
639
640/************************************************************************
641 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000642 * Allocation functions *
643 * *
644 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000645static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
646static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000647static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000648static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000649static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
650 ATTRIBUTE_UNUSED,
651 xmlRelaxNGValidStatePtr state1,
652 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000653static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000654 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000655
656/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000657 * xmlRelaxNGFreeDocument:
658 * @docu: a document structure
659 *
660 * Deallocate a RelaxNG document structure.
661 */
662static void
663xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
664{
665 if (docu == NULL)
666 return;
667
668 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000669 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000670 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000671 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000672 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000673 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000674 xmlFree(docu);
675}
676
677/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000678 * xmlRelaxNGFreeDocumentList:
679 * @docu: a list of document structure
680 *
681 * Deallocate a RelaxNG document structures.
682 */
683static void
684xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
685{
686 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000687
Daniel Veillardc482e262003-02-26 14:48:48 +0000688 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000689 next = docu->next;
690 xmlRelaxNGFreeDocument(docu);
691 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000692 }
693}
694
695/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000696 * xmlRelaxNGFreeInclude:
697 * @incl: a include structure
698 *
699 * Deallocate a RelaxNG include structure.
700 */
701static void
702xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
703{
704 if (incl == NULL)
705 return;
706
707 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000708 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000709 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000710 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000711 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000712 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000713 xmlFree(incl);
714}
715
716/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000717 * xmlRelaxNGFreeIncludeList:
718 * @incl: a include structure list
719 *
720 * Deallocate a RelaxNG include structure.
721 */
722static void
723xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
724{
725 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000726
Daniel Veillardc482e262003-02-26 14:48:48 +0000727 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000728 next = incl->next;
729 xmlRelaxNGFreeInclude(incl);
730 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000731 }
732}
733
734/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000735 * xmlRelaxNGNewRelaxNG:
736 * @ctxt: a Relax-NG validation context (optional)
737 *
738 * Allocate a new RelaxNG structure.
739 *
740 * Returns the newly allocated structure or NULL in case or error
741 */
742static xmlRelaxNGPtr
743xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
744{
745 xmlRelaxNGPtr ret;
746
747 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
748 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000749 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000750 return (NULL);
751 }
752 memset(ret, 0, sizeof(xmlRelaxNG));
753
754 return (ret);
755}
756
757/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000758 * xmlRelaxNGFreeInnerSchema:
759 * @schema: a schema structure
760 *
761 * Deallocate a RelaxNG schema structure.
762 */
763static void
764xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
765{
766 if (schema == NULL)
767 return;
768
769 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000770 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000771 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000772 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000773
Daniel Veillard4c004142003-10-07 11:33:24 +0000774 for (i = 0; i < schema->defNr; i++)
775 xmlRelaxNGFreeDefine(schema->defTab[i]);
776 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000777 }
778
779 xmlFree(schema);
780}
781
782/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000783 * xmlRelaxNGFree:
784 * @schema: a schema structure
785 *
786 * Deallocate a RelaxNG structure.
787 */
788void
789xmlRelaxNGFree(xmlRelaxNGPtr schema)
790{
791 if (schema == NULL)
792 return;
793
Daniel Veillard6eadf632003-01-23 18:29:16 +0000794 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000795 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000796 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000797 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000798 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000799 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000800 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000801 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000802 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000803 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000804
Daniel Veillard4c004142003-10-07 11:33:24 +0000805 for (i = 0; i < schema->defNr; i++)
806 xmlRelaxNGFreeDefine(schema->defTab[i]);
807 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000808 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000809
810 xmlFree(schema);
811}
812
813/**
814 * xmlRelaxNGNewGrammar:
815 * @ctxt: a Relax-NG validation context (optional)
816 *
817 * Allocate a new RelaxNG grammar.
818 *
819 * Returns the newly allocated structure or NULL in case or error
820 */
821static xmlRelaxNGGrammarPtr
822xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
823{
824 xmlRelaxNGGrammarPtr ret;
825
826 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
827 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000828 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000829 return (NULL);
830 }
831 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
832
833 return (ret);
834}
835
836/**
837 * xmlRelaxNGFreeGrammar:
838 * @grammar: a grammar structure
839 *
840 * Deallocate a RelaxNG grammar structure.
841 */
842static void
843xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
844{
845 if (grammar == NULL)
846 return;
847
Daniel Veillardc482e262003-02-26 14:48:48 +0000848 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000849 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000850 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000851 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000852 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000853 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000854 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000855 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000856 }
857 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000858 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000859 }
860
861 xmlFree(grammar);
862}
863
864/**
865 * xmlRelaxNGNewDefine:
866 * @ctxt: a Relax-NG validation context
867 * @node: the node in the input document.
868 *
869 * Allocate a new RelaxNG define.
870 *
871 * Returns the newly allocated structure or NULL in case or error
872 */
873static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000874xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000875{
876 xmlRelaxNGDefinePtr ret;
877
Daniel Veillard419a7682003-02-03 23:22:49 +0000878 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000879 ctxt->defMax = 16;
880 ctxt->defNr = 0;
881 ctxt->defTab = (xmlRelaxNGDefinePtr *)
882 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
883 if (ctxt->defTab == NULL) {
884 xmlRngPErrMemory(ctxt, "allocating define\n");
885 return (NULL);
886 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000887 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000888 xmlRelaxNGDefinePtr *tmp;
889
890 ctxt->defMax *= 2;
891 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
892 ctxt->defMax *
893 sizeof
894 (xmlRelaxNGDefinePtr));
895 if (tmp == NULL) {
896 xmlRngPErrMemory(ctxt, "allocating define\n");
897 return (NULL);
898 }
899 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000900 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000901 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
902 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000903 xmlRngPErrMemory(ctxt, "allocating define\n");
904 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000905 }
906 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000907 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000908 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000909 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000910 return (ret);
911}
912
913/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000914 * xmlRelaxNGFreePartition:
915 * @partitions: a partition set structure
916 *
917 * Deallocate RelaxNG partition set structures.
918 */
919static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000920xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
921{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000922 xmlRelaxNGInterleaveGroupPtr group;
923 int j;
924
925 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000926 if (partitions->groups != NULL) {
927 for (j = 0; j < partitions->nbgroups; j++) {
928 group = partitions->groups[j];
929 if (group != NULL) {
930 if (group->defs != NULL)
931 xmlFree(group->defs);
932 if (group->attrs != NULL)
933 xmlFree(group->attrs);
934 xmlFree(group);
935 }
936 }
937 xmlFree(partitions->groups);
938 }
939 if (partitions->triage != NULL) {
940 xmlHashFree(partitions->triage, NULL);
941 }
942 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000943 }
944}
Daniel Veillard4c004142003-10-07 11:33:24 +0000945
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000946/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000947 * xmlRelaxNGFreeDefine:
948 * @define: a define structure
949 *
950 * Deallocate a RelaxNG define structure.
951 */
952static void
953xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
954{
955 if (define == NULL)
956 return;
957
Daniel Veillard4c004142003-10-07 11:33:24 +0000958 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
959 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000960
Daniel Veillard4c004142003-10-07 11:33:24 +0000961 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
962 if ((lib != NULL) && (lib->freef != NULL))
963 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000964 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000965 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
966 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
967 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
968 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000969 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000970 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000971 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000972 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000973 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000974 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000975 if (define->contModel != NULL)
976 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000977 xmlFree(define);
978}
979
980/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000981 * xmlRelaxNGNewStates:
982 * @ctxt: a Relax-NG validation context
983 * @size: the default size for the container
984 *
985 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000986 *
987 * Returns the newly allocated structure or NULL in case or error
988 */
989static xmlRelaxNGStatesPtr
990xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
991{
992 xmlRelaxNGStatesPtr ret;
993
Daniel Veillard798024a2003-03-19 10:36:09 +0000994 if ((ctxt != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +0000995 (ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) {
996 ctxt->freeStatesNr--;
997 ret = ctxt->freeStates[ctxt->freeStatesNr];
998 ret->nbState = 0;
999 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001000 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001001 if (size < 16)
1002 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001003
1004 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001005 (size -
1006 1) *
1007 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001008 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001009 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001010 return (NULL);
1011 }
1012 ret->nbState = 0;
1013 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001014 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1015 sizeof
1016 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001017 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001018 xmlRngVErrMemory(ctxt, "allocating states\n");
1019 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001020 return (NULL);
1021 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001022 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001023}
1024
1025/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001026 * xmlRelaxNGAddStateUniq:
1027 * @ctxt: a Relax-NG validation context
1028 * @states: the states container
1029 * @state: the validation state
1030 *
1031 * Add a RelaxNG validation state to the container without checking
1032 * for unicity.
1033 *
1034 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1035 */
1036static int
1037xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001038 xmlRelaxNGStatesPtr states,
1039 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001040{
1041 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001042 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001043 }
1044 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001045 xmlRelaxNGValidStatePtr *tmp;
1046 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001047
Daniel Veillard4c004142003-10-07 11:33:24 +00001048 size = states->maxState * 2;
1049 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1050 (size) *
1051 sizeof
1052 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001053 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001054 xmlRngVErrMemory(ctxt, "adding states\n");
1055 return (-1);
1056 }
1057 states->tabState = tmp;
1058 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001059 }
1060 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001061 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001062}
1063
1064/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001065 * xmlRelaxNGAddState:
1066 * @ctxt: a Relax-NG validation context
1067 * @states: the states container
1068 * @state: the validation state
1069 *
1070 * Add a RelaxNG validation state to the container
1071 *
1072 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1073 */
1074static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001075xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1076 xmlRelaxNGStatesPtr states,
1077 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001078{
1079 int i;
1080
1081 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001082 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001083 }
1084 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001085 xmlRelaxNGValidStatePtr *tmp;
1086 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001087
Daniel Veillard4c004142003-10-07 11:33:24 +00001088 size = states->maxState * 2;
1089 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1090 (size) *
1091 sizeof
1092 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001093 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001094 xmlRngVErrMemory(ctxt, "adding states\n");
1095 return (-1);
1096 }
1097 states->tabState = tmp;
1098 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001099 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001100 for (i = 0; i < states->nbState; i++) {
1101 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1102 xmlRelaxNGFreeValidState(ctxt, state);
1103 return (0);
1104 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001105 }
1106 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001107 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001108}
1109
1110/**
1111 * xmlRelaxNGFreeStates:
1112 * @ctxt: a Relax-NG validation context
1113 * @states: teh container
1114 *
1115 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001116 */
1117static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001118xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001119 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001120{
Daniel Veillard798024a2003-03-19 10:36:09 +00001121 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001122 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001123 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001124 ctxt->freeStatesMax = 40;
1125 ctxt->freeStatesNr = 0;
1126 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1127 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1128 if (ctxt->freeStates == NULL) {
1129 xmlRngVErrMemory(ctxt, "storing states\n");
1130 }
1131 } else if ((ctxt != NULL)
1132 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1133 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001134
Daniel Veillard4c004142003-10-07 11:33:24 +00001135 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1136 2 * ctxt->freeStatesMax *
1137 sizeof
1138 (xmlRelaxNGStatesPtr));
1139 if (tmp == NULL) {
1140 xmlRngVErrMemory(ctxt, "storing states\n");
1141 xmlFree(states->tabState);
1142 xmlFree(states);
1143 return;
1144 }
1145 ctxt->freeStates = tmp;
1146 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001147 }
1148 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001149 xmlFree(states->tabState);
1150 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001151 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001152 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001153 }
1154}
1155
1156/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001157 * xmlRelaxNGNewValidState:
1158 * @ctxt: a Relax-NG validation context
1159 * @node: the current node or NULL for the document
1160 *
1161 * Allocate a new RelaxNG validation state
1162 *
1163 * Returns the newly allocated structure or NULL in case or error
1164 */
1165static xmlRelaxNGValidStatePtr
1166xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1167{
1168 xmlRelaxNGValidStatePtr ret;
1169 xmlAttrPtr attr;
1170 xmlAttrPtr attrs[MAX_ATTR];
1171 int nbAttrs = 0;
1172 xmlNodePtr root = NULL;
1173
1174 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001175 root = xmlDocGetRootElement(ctxt->doc);
1176 if (root == NULL)
1177 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001178 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001179 attr = node->properties;
1180 while (attr != NULL) {
1181 if (nbAttrs < MAX_ATTR)
1182 attrs[nbAttrs++] = attr;
1183 else
1184 nbAttrs++;
1185 attr = attr->next;
1186 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001187 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001188 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1189 ctxt->freeState->nbState--;
1190 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001191 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001192 ret =
1193 (xmlRelaxNGValidStatePtr)
1194 xmlMalloc(sizeof(xmlRelaxNGValidState));
1195 if (ret == NULL) {
1196 xmlRngVErrMemory(ctxt, "allocating states\n");
1197 return (NULL);
1198 }
1199 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001200 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001201 ret->value = NULL;
1202 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001203 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001204 ret->node = (xmlNodePtr) ctxt->doc;
1205 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001206 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001207 ret->node = node;
1208 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001209 }
1210 ret->nbAttrs = 0;
1211 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001212 if (ret->attrs == NULL) {
1213 if (nbAttrs < 4)
1214 ret->maxAttrs = 4;
1215 else
1216 ret->maxAttrs = nbAttrs;
1217 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1218 sizeof(xmlAttrPtr));
1219 if (ret->attrs == NULL) {
1220 xmlRngVErrMemory(ctxt, "allocating states\n");
1221 return (ret);
1222 }
1223 } else if (ret->maxAttrs < nbAttrs) {
1224 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001225
Daniel Veillard4c004142003-10-07 11:33:24 +00001226 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1227 sizeof(xmlAttrPtr));
1228 if (tmp == NULL) {
1229 xmlRngVErrMemory(ctxt, "allocating states\n");
1230 return (ret);
1231 }
1232 ret->attrs = tmp;
1233 ret->maxAttrs = nbAttrs;
1234 }
1235 ret->nbAttrs = nbAttrs;
1236 if (nbAttrs < MAX_ATTR) {
1237 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1238 } else {
1239 attr = node->properties;
1240 nbAttrs = 0;
1241 while (attr != NULL) {
1242 ret->attrs[nbAttrs++] = attr;
1243 attr = attr->next;
1244 }
1245 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001246 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001247 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001248 return (ret);
1249}
1250
1251/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001252 * xmlRelaxNGCopyValidState:
1253 * @ctxt: a Relax-NG validation context
1254 * @state: a validation state
1255 *
1256 * Copy the validation state
1257 *
1258 * Returns the newly allocated structure or NULL in case or error
1259 */
1260static xmlRelaxNGValidStatePtr
1261xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001262 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001263{
1264 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001265 unsigned int maxAttrs;
1266 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001267
1268 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001269 return (NULL);
1270 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1271 ctxt->freeState->nbState--;
1272 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001273 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001274 ret =
1275 (xmlRelaxNGValidStatePtr)
1276 xmlMalloc(sizeof(xmlRelaxNGValidState));
1277 if (ret == NULL) {
1278 xmlRngVErrMemory(ctxt, "allocating states\n");
1279 return (NULL);
1280 }
1281 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001282 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001283 attrs = ret->attrs;
1284 maxAttrs = ret->maxAttrs;
1285 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1286 ret->attrs = attrs;
1287 ret->maxAttrs = maxAttrs;
1288 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001289 if (ret->attrs == NULL) {
1290 ret->maxAttrs = state->maxAttrs;
1291 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1292 sizeof(xmlAttrPtr));
1293 if (ret->attrs == NULL) {
1294 xmlRngVErrMemory(ctxt, "allocating states\n");
1295 ret->nbAttrs = 0;
1296 return (ret);
1297 }
1298 } else if (ret->maxAttrs < state->nbAttrs) {
1299 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001300
Daniel Veillard4c004142003-10-07 11:33:24 +00001301 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1302 sizeof(xmlAttrPtr));
1303 if (tmp == NULL) {
1304 xmlRngVErrMemory(ctxt, "allocating states\n");
1305 ret->nbAttrs = 0;
1306 return (ret);
1307 }
1308 ret->maxAttrs = state->maxAttrs;
1309 ret->attrs = tmp;
1310 }
1311 memcpy(ret->attrs, state->attrs,
1312 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001313 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001314 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001315}
1316
1317/**
1318 * xmlRelaxNGEqualValidState:
1319 * @ctxt: a Relax-NG validation context
1320 * @state1: a validation state
1321 * @state2: a validation state
1322 *
1323 * Compare the validation states for equality
1324 *
1325 * Returns 1 if equald, 0 otherwise
1326 */
1327static int
1328xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001329 xmlRelaxNGValidStatePtr state1,
1330 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001331{
1332 int i;
1333
1334 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001335 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001336 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001337 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001338 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001339 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001340 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001341 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001342 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001343 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001344 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001345 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001346 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001347 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001348 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001349 (!xmlStrEqual(state1->value, state2->value)))
1350 return (0);
1351 for (i = 0; i < state1->nbAttrs; i++) {
1352 if (state1->attrs[i] != state2->attrs[i])
1353 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001354 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001355 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001356}
1357
1358/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001359 * xmlRelaxNGFreeValidState:
1360 * @state: a validation state structure
1361 *
1362 * Deallocate a RelaxNG validation state structure.
1363 */
1364static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001365xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001366 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001367{
1368 if (state == NULL)
1369 return;
1370
Daniel Veillard798024a2003-03-19 10:36:09 +00001371 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001372 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001373 }
1374 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001375 if (state->attrs != NULL)
1376 xmlFree(state->attrs);
1377 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001378 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001379 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001380 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001381}
1382
1383/************************************************************************
1384 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001385 * Document functions *
1386 * *
1387 ************************************************************************/
1388static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001389 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001390
1391/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001392 * xmlRelaxNGIncludePush:
1393 * @ctxt: the parser context
1394 * @value: the element doc
1395 *
1396 * Pushes a new include on top of the include stack
1397 *
1398 * Returns 0 in case of error, the index in the stack otherwise
1399 */
1400static int
1401xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001402 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001403{
1404 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001405 ctxt->incMax = 4;
1406 ctxt->incNr = 0;
1407 ctxt->incTab =
1408 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1409 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001410 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001411 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001412 return (0);
1413 }
1414 }
1415 if (ctxt->incNr >= ctxt->incMax) {
1416 ctxt->incMax *= 2;
1417 ctxt->incTab =
1418 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001419 ctxt->incMax *
1420 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001421 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001422 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001423 return (0);
1424 }
1425 }
1426 ctxt->incTab[ctxt->incNr] = value;
1427 ctxt->inc = value;
1428 return (ctxt->incNr++);
1429}
1430
1431/**
1432 * xmlRelaxNGIncludePop:
1433 * @ctxt: the parser context
1434 *
1435 * Pops the top include from the include stack
1436 *
1437 * Returns the include just removed
1438 */
1439static xmlRelaxNGIncludePtr
1440xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1441{
1442 xmlRelaxNGIncludePtr ret;
1443
1444 if (ctxt->incNr <= 0)
1445 return (0);
1446 ctxt->incNr--;
1447 if (ctxt->incNr > 0)
1448 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1449 else
1450 ctxt->inc = NULL;
1451 ret = ctxt->incTab[ctxt->incNr];
1452 ctxt->incTab[ctxt->incNr] = 0;
1453 return (ret);
1454}
1455
1456/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001457 * xmlRelaxNGRemoveRedefine:
1458 * @ctxt: the parser context
1459 * @URL: the normalized URL
1460 * @target: the included target
1461 * @name: the define name to eliminate
1462 *
1463 * Applies the elimination algorithm of 4.7
1464 *
1465 * Returns 0 in case of error, 1 in case of success.
1466 */
1467static int
1468xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001469 const xmlChar * URL ATTRIBUTE_UNUSED,
1470 xmlNodePtr target, const xmlChar * name)
1471{
Daniel Veillard5add8682003-03-10 13:13:58 +00001472 int found = 0;
1473 xmlNodePtr tmp, tmp2;
1474 xmlChar *name2;
1475
1476#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001477 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001478 xmlGenericError(xmlGenericErrorContext,
1479 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001480 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001481 xmlGenericError(xmlGenericErrorContext,
1482 "Elimination of <include> define %s from %s\n",
1483 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001484#endif
1485 tmp = target;
1486 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001487 tmp2 = tmp->next;
1488 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1489 found = 1;
1490 xmlUnlinkNode(tmp);
1491 xmlFreeNode(tmp);
1492 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1493 name2 = xmlGetProp(tmp, BAD_CAST "name");
1494 xmlRelaxNGNormExtSpace(name2);
1495 if (name2 != NULL) {
1496 if (xmlStrEqual(name, name2)) {
1497 found = 1;
1498 xmlUnlinkNode(tmp);
1499 xmlFreeNode(tmp);
1500 }
1501 xmlFree(name2);
1502 }
1503 } else if (IS_RELAXNG(tmp, "include")) {
1504 xmlChar *href = NULL;
1505 xmlRelaxNGDocumentPtr inc = tmp->_private;
Daniel Veillard5add8682003-03-10 13:13:58 +00001506
Daniel Veillard4c004142003-10-07 11:33:24 +00001507 if ((inc != NULL) && (inc->doc != NULL) &&
1508 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001509
Daniel Veillard4c004142003-10-07 11:33:24 +00001510 if (xmlStrEqual
1511 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001512#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001513 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001514#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001515 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1516 inc->doc->children->
1517 children, name) == 1) {
1518 found = 1;
1519 }
1520 if (href != NULL)
1521 xmlFree(href);
1522 }
1523 }
1524 }
1525 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001526 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001527 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001528}
1529
1530/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001531 * xmlRelaxNGLoadInclude:
1532 * @ctxt: the parser context
1533 * @URL: the normalized URL
1534 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001535 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001536 *
1537 * First lookup if the document is already loaded into the parser context,
1538 * check against recursion. If not found the resource is loaded and
1539 * the content is preprocessed before being returned back to the caller.
1540 *
1541 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1542 */
1543static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001544xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1545 xmlNodePtr node, const xmlChar * ns)
1546{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001547 xmlRelaxNGIncludePtr ret = NULL;
1548 xmlDocPtr doc;
1549 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001550 xmlNodePtr root, cur;
1551
1552#ifdef DEBUG_INCLUDE
1553 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001554 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001555#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001556
1557 /*
1558 * check against recursion in the stack
1559 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001560 for (i = 0; i < ctxt->incNr; i++) {
1561 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1562 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1563 "Detected an Include recursion for %s\n", URL,
1564 NULL);
1565 return (NULL);
1566 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001567 }
1568
1569 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001570 * load the document
1571 */
1572 doc = xmlParseFile((const char *) URL);
1573 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001574 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1575 "xmlRelaxNG: could not load %s\n", URL, NULL);
1576 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001577 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001578#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001579 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001580#endif
1581
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001582 /*
1583 * Allocate the document structures and register it first.
1584 */
1585 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1586 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001587 xmlRngPErrMemory(ctxt, "allocating include\n");
1588 xmlFreeDoc(doc);
1589 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001590 }
1591 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1592 ret->doc = doc;
1593 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001594 ret->next = ctxt->includes;
1595 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001596
1597 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001598 * transmit the ns if needed
1599 */
1600 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001601 root = xmlDocGetRootElement(doc);
1602 if (root != NULL) {
1603 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1604 xmlSetProp(root, BAD_CAST "ns", ns);
1605 }
1606 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001607 }
1608
1609 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001610 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001611 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001612 xmlRelaxNGIncludePush(ctxt, ret);
1613
1614 /*
1615 * Some preprocessing of the document content, this include recursing
1616 * in the include stack.
1617 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001618#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001619 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001620#endif
1621
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001622 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1623 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001624 ctxt->inc = NULL;
1625 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001626 }
1627
1628 /*
1629 * Pop up the include from the stack
1630 */
1631 xmlRelaxNGIncludePop(ctxt);
1632
Daniel Veillard5add8682003-03-10 13:13:58 +00001633#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001634 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001635#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001636 /*
1637 * Check that the top element is a grammar
1638 */
1639 root = xmlDocGetRootElement(doc);
1640 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001641 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1642 "xmlRelaxNG: included document is empty %s\n", URL,
1643 NULL);
1644 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001645 }
1646 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001647 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1648 "xmlRelaxNG: included document %s root is not a grammar\n",
1649 URL, NULL);
1650 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001651 }
1652
1653 /*
1654 * Elimination of redefined rules in the include.
1655 */
1656 cur = node->children;
1657 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001658 if (IS_RELAXNG(cur, "start")) {
1659 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001660
Daniel Veillard4c004142003-10-07 11:33:24 +00001661 found =
1662 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1663 if (!found) {
1664 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1665 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1666 URL, NULL);
1667 }
1668 } else if (IS_RELAXNG(cur, "define")) {
1669 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001670
Daniel Veillard4c004142003-10-07 11:33:24 +00001671 name = xmlGetProp(cur, BAD_CAST "name");
1672 if (name == NULL) {
1673 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1674 "xmlRelaxNG: include %s has define without name\n",
1675 URL, NULL);
1676 } else {
1677 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001678
Daniel Veillard4c004142003-10-07 11:33:24 +00001679 xmlRelaxNGNormExtSpace(name);
1680 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1681 root->children, name);
1682 if (!found) {
1683 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1684 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1685 URL, name);
1686 }
1687 xmlFree(name);
1688 }
1689 }
1690 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001691 }
1692
1693
Daniel Veillard4c004142003-10-07 11:33:24 +00001694 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001695}
1696
1697/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001698 * xmlRelaxNGValidErrorPush:
1699 * @ctxt: the validation context
1700 * @err: the error code
1701 * @arg1: the first string argument
1702 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001703 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001704 *
1705 * Pushes a new error on top of the error stack
1706 *
1707 * Returns 0 in case of error, the index in the stack otherwise
1708 */
1709static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001710xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1711 xmlRelaxNGValidErr err, const xmlChar * arg1,
1712 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001713{
1714 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001715
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001716#ifdef DEBUG_ERROR
1717 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001718 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001719#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001720 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001721 ctxt->errMax = 8;
1722 ctxt->errNr = 0;
1723 ctxt->errTab =
1724 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1725 sizeof
1726 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001727 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001728 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001729 return (0);
1730 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001731 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001732 }
1733 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001734 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001735 ctxt->errTab =
1736 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001737 ctxt->errMax *
1738 sizeof
1739 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001740 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001741 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001742 return (0);
1743 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001744 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001745 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001746 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001747 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1748 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001749 cur = &ctxt->errTab[ctxt->errNr];
1750 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001751 if (dup) {
1752 cur->arg1 = xmlStrdup(arg1);
1753 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001754 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001755 } else {
1756 cur->arg1 = arg1;
1757 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001758 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001759 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001760 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001761 cur->node = ctxt->state->node;
1762 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001763 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001764 cur->node = NULL;
1765 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001766 }
1767 ctxt->err = cur;
1768 return (ctxt->errNr++);
1769}
1770
1771/**
1772 * xmlRelaxNGValidErrorPop:
1773 * @ctxt: the validation context
1774 *
1775 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001776 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001777static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001778xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1779{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001780 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001781
Daniel Veillard580ced82003-03-21 21:22:48 +00001782 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001783 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001784 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001785 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001786 ctxt->errNr--;
1787 if (ctxt->errNr > 0)
1788 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1789 else
1790 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001791 cur = &ctxt->errTab[ctxt->errNr];
1792 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001793 if (cur->arg1 != NULL)
1794 xmlFree((xmlChar *) cur->arg1);
1795 cur->arg1 = NULL;
1796 if (cur->arg2 != NULL)
1797 xmlFree((xmlChar *) cur->arg2);
1798 cur->arg2 = NULL;
1799 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001800 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001801}
1802
Daniel Veillard42f12e92003-03-07 18:32:59 +00001803/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001804 * xmlRelaxNGDocumentPush:
1805 * @ctxt: the parser context
1806 * @value: the element doc
1807 *
1808 * Pushes a new doc on top of the doc stack
1809 *
1810 * Returns 0 in case of error, the index in the stack otherwise
1811 */
1812static int
1813xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001814 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001815{
1816 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001817 ctxt->docMax = 4;
1818 ctxt->docNr = 0;
1819 ctxt->docTab =
1820 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1821 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001822 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001823 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001824 return (0);
1825 }
1826 }
1827 if (ctxt->docNr >= ctxt->docMax) {
1828 ctxt->docMax *= 2;
1829 ctxt->docTab =
1830 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001831 ctxt->docMax *
1832 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001833 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001834 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001835 return (0);
1836 }
1837 }
1838 ctxt->docTab[ctxt->docNr] = value;
1839 ctxt->doc = value;
1840 return (ctxt->docNr++);
1841}
1842
1843/**
1844 * xmlRelaxNGDocumentPop:
1845 * @ctxt: the parser context
1846 *
1847 * Pops the top doc from the doc stack
1848 *
1849 * Returns the doc just removed
1850 */
1851static xmlRelaxNGDocumentPtr
1852xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1853{
1854 xmlRelaxNGDocumentPtr ret;
1855
1856 if (ctxt->docNr <= 0)
1857 return (0);
1858 ctxt->docNr--;
1859 if (ctxt->docNr > 0)
1860 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1861 else
1862 ctxt->doc = NULL;
1863 ret = ctxt->docTab[ctxt->docNr];
1864 ctxt->docTab[ctxt->docNr] = 0;
1865 return (ret);
1866}
1867
1868/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001869 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001870 * @ctxt: the parser context
1871 * @URL: the normalized URL
1872 * @ns: the inherited ns if any
1873 *
1874 * First lookup if the document is already loaded into the parser context,
1875 * check against recursion. If not found the resource is loaded and
1876 * the content is preprocessed before being returned back to the caller.
1877 *
1878 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1879 */
1880static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001881xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1882 const xmlChar * URL, const xmlChar * ns)
1883{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001884 xmlRelaxNGDocumentPtr ret = NULL;
1885 xmlDocPtr doc;
1886 xmlNodePtr root;
1887 int i;
1888
1889 /*
1890 * check against recursion in the stack
1891 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001892 for (i = 0; i < ctxt->docNr; i++) {
1893 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1894 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1895 "Detected an externalRef recursion for %s\n", URL,
1896 NULL);
1897 return (NULL);
1898 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001899 }
1900
1901 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001902 * load the document
1903 */
1904 doc = xmlParseFile((const char *) URL);
1905 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001906 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1907 "xmlRelaxNG: could not load %s\n", URL, NULL);
1908 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001909 }
1910
1911 /*
1912 * Allocate the document structures and register it first.
1913 */
1914 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1915 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001916 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1917 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1918 xmlFreeDoc(doc);
1919 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001920 }
1921 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1922 ret->doc = doc;
1923 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001924 ret->next = ctxt->documents;
1925 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001926
1927 /*
1928 * transmit the ns if needed
1929 */
1930 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001931 root = xmlDocGetRootElement(doc);
1932 if (root != NULL) {
1933 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1934 xmlSetProp(root, BAD_CAST "ns", ns);
1935 }
1936 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001937 }
1938
1939 /*
1940 * push it on the stack and register it in the hash table
1941 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001942 xmlRelaxNGDocumentPush(ctxt, ret);
1943
1944 /*
1945 * Some preprocessing of the document content
1946 */
1947 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1948 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001949 ctxt->doc = NULL;
1950 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001951 }
1952
1953 xmlRelaxNGDocumentPop(ctxt);
1954
Daniel Veillard4c004142003-10-07 11:33:24 +00001955 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001956}
1957
1958/************************************************************************
1959 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001960 * Error functions *
1961 * *
1962 ************************************************************************/
1963
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001964#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1965#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
1966#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
1967#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
1968#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001969
Daniel Veillard231d7912003-02-09 14:22:17 +00001970static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00001971xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
1972{
Daniel Veillard231d7912003-02-09 14:22:17 +00001973 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001974 return ("none");
1975 switch (def->type) {
1976 case XML_RELAXNG_EMPTY:
1977 return ("empty");
1978 case XML_RELAXNG_NOT_ALLOWED:
1979 return ("notAllowed");
1980 case XML_RELAXNG_EXCEPT:
1981 return ("except");
1982 case XML_RELAXNG_TEXT:
1983 return ("text");
1984 case XML_RELAXNG_ELEMENT:
1985 return ("element");
1986 case XML_RELAXNG_DATATYPE:
1987 return ("datatype");
1988 case XML_RELAXNG_VALUE:
1989 return ("value");
1990 case XML_RELAXNG_LIST:
1991 return ("list");
1992 case XML_RELAXNG_ATTRIBUTE:
1993 return ("attribute");
1994 case XML_RELAXNG_DEF:
1995 return ("def");
1996 case XML_RELAXNG_REF:
1997 return ("ref");
1998 case XML_RELAXNG_EXTERNALREF:
1999 return ("externalRef");
2000 case XML_RELAXNG_PARENTREF:
2001 return ("parentRef");
2002 case XML_RELAXNG_OPTIONAL:
2003 return ("optional");
2004 case XML_RELAXNG_ZEROORMORE:
2005 return ("zeroOrMore");
2006 case XML_RELAXNG_ONEORMORE:
2007 return ("oneOrMore");
2008 case XML_RELAXNG_CHOICE:
2009 return ("choice");
2010 case XML_RELAXNG_GROUP:
2011 return ("group");
2012 case XML_RELAXNG_INTERLEAVE:
2013 return ("interleave");
2014 case XML_RELAXNG_START:
2015 return ("start");
2016 case XML_RELAXNG_NOOP:
2017 return ("noop");
2018 case XML_RELAXNG_PARAM:
2019 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002020 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002021 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002022}
Daniel Veillardd2298792003-02-14 16:54:11 +00002023
Daniel Veillard6eadf632003-01-23 18:29:16 +00002024/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002025 * xmlRelaxNGGetErrorString:
2026 * @err: the error code
2027 * @arg1: the first string argument
2028 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002029 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002030 * computes a formatted error string for the given error code and args
2031 *
2032 * Returns the error string, it must be deallocated by the caller
2033 */
2034static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002035xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2036 const xmlChar * arg2)
2037{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002038 char msg[1000];
2039
2040 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002041 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002042 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002043 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002044
2045 msg[0] = 0;
2046 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002047 case XML_RELAXNG_OK:
2048 return (NULL);
2049 case XML_RELAXNG_ERR_MEMORY:
2050 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002051 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002052 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2053 break;
2054 case XML_RELAXNG_ERR_TYPEVAL:
2055 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2056 arg2);
2057 break;
2058 case XML_RELAXNG_ERR_DUPID:
2059 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2060 break;
2061 case XML_RELAXNG_ERR_TYPECMP:
2062 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2063 break;
2064 case XML_RELAXNG_ERR_NOSTATE:
2065 return (xmlCharStrdup("Internal error: no state\n"));
2066 case XML_RELAXNG_ERR_NODEFINE:
2067 return (xmlCharStrdup("Internal error: no define\n"));
2068 case XML_RELAXNG_ERR_INTERNAL:
2069 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2070 break;
2071 case XML_RELAXNG_ERR_LISTEXTRA:
2072 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2073 break;
2074 case XML_RELAXNG_ERR_INTERNODATA:
2075 return (xmlCharStrdup
2076 ("Internal: interleave block has no data\n"));
2077 case XML_RELAXNG_ERR_INTERSEQ:
2078 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2079 case XML_RELAXNG_ERR_INTEREXTRA:
2080 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2081 break;
2082 case XML_RELAXNG_ERR_ELEMNAME:
2083 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2084 arg2);
2085 break;
2086 case XML_RELAXNG_ERR_ELEMNONS:
2087 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2088 arg1);
2089 break;
2090 case XML_RELAXNG_ERR_ELEMWRONGNS:
2091 snprintf(msg, 1000,
2092 "Element %s has wrong namespace: expecting %s\n", arg1,
2093 arg2);
2094 break;
2095 case XML_RELAXNG_ERR_ELEMWRONG:
2096 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2097 break;
2098 case XML_RELAXNG_ERR_TEXTWRONG:
2099 snprintf(msg, 1000,
2100 "Did not expect text in element %s content\n", arg1);
2101 break;
2102 case XML_RELAXNG_ERR_ELEMEXTRANS:
2103 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2104 arg1);
2105 break;
2106 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2107 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2108 break;
2109 case XML_RELAXNG_ERR_NOELEM:
2110 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2111 arg1);
2112 break;
2113 case XML_RELAXNG_ERR_NOTELEM:
2114 return (xmlCharStrdup("Expecting an element got text\n"));
2115 case XML_RELAXNG_ERR_ATTRVALID:
2116 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2117 arg1);
2118 break;
2119 case XML_RELAXNG_ERR_CONTENTVALID:
2120 snprintf(msg, 1000, "Element %s failed to validate content\n",
2121 arg1);
2122 break;
2123 case XML_RELAXNG_ERR_EXTRACONTENT:
2124 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2125 arg1, arg2);
2126 break;
2127 case XML_RELAXNG_ERR_INVALIDATTR:
2128 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2129 arg1, arg2);
2130 break;
2131 case XML_RELAXNG_ERR_LACKDATA:
2132 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2133 arg1);
2134 break;
2135 case XML_RELAXNG_ERR_DATAELEM:
2136 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2137 arg1);
2138 break;
2139 case XML_RELAXNG_ERR_VALELEM:
2140 snprintf(msg, 1000, "Value element %s has child elements\n",
2141 arg1);
2142 break;
2143 case XML_RELAXNG_ERR_LISTELEM:
2144 snprintf(msg, 1000, "List element %s has child elements\n",
2145 arg1);
2146 break;
2147 case XML_RELAXNG_ERR_DATATYPE:
2148 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2149 break;
2150 case XML_RELAXNG_ERR_VALUE:
2151 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2152 break;
2153 case XML_RELAXNG_ERR_LIST:
2154 return (xmlCharStrdup("Error validating list\n"));
2155 case XML_RELAXNG_ERR_NOGRAMMAR:
2156 return (xmlCharStrdup("No top grammar defined\n"));
2157 case XML_RELAXNG_ERR_EXTRADATA:
2158 return (xmlCharStrdup("Extra data in the document\n"));
2159 default:
2160 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002161 }
2162 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002163 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002164 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002165 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002166 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002167}
2168
2169/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002170 * xmlRelaxNGShowValidError:
2171 * @ctxt: the validation context
2172 * @err: the error number
2173 * @node: the node
2174 * @child: the node child generating the problem.
2175 * @arg1: the first argument
2176 * @arg2: the second argument
2177 *
2178 * Show a validation error.
2179 */
2180static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002181xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2182 xmlRelaxNGValidErr err, xmlNodePtr node,
2183 xmlNodePtr child, const xmlChar * arg1,
2184 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002185{
2186 xmlChar *msg;
2187
2188 if (ctxt->error == NULL)
2189 return;
2190
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002191#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002192 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002193#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002194 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2195 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002196 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002197
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002198 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002199 ctxt->errNo = err;
2200 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2201 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002202 xmlFree(msg);
2203}
2204
2205/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002206 * xmlRelaxNGPopErrors:
2207 * @ctxt: the validation context
2208 * @level: the error level in the stack
2209 *
2210 * pop and discard all errors until the given level is reached
2211 */
2212static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002213xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2214{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002215 int i;
2216 xmlRelaxNGValidErrorPtr err;
2217
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002218#ifdef DEBUG_ERROR
2219 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002220 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002221#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002222 for (i = level; i < ctxt->errNr; i++) {
2223 err = &ctxt->errTab[i];
2224 if (err->flags & ERROR_IS_DUP) {
2225 if (err->arg1 != NULL)
2226 xmlFree((xmlChar *) err->arg1);
2227 err->arg1 = NULL;
2228 if (err->arg2 != NULL)
2229 xmlFree((xmlChar *) err->arg2);
2230 err->arg2 = NULL;
2231 err->flags = 0;
2232 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002233 }
2234 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002235 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002236 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002237}
Daniel Veillard4c004142003-10-07 11:33:24 +00002238
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002239/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002240 * xmlRelaxNGDumpValidError:
2241 * @ctxt: the validation context
2242 *
2243 * Show all validation error over a given index.
2244 */
2245static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002246xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2247{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002248 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002249 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002250
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002251#ifdef DEBUG_ERROR
2252 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002253 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002254#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002255 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2256 err = &ctxt->errTab[i];
2257 if (k < MAX_ERROR) {
2258 for (j = 0; j < i; j++) {
2259 dup = &ctxt->errTab[j];
2260 if ((err->err == dup->err) && (err->node == dup->node) &&
2261 (xmlStrEqual(err->arg1, dup->arg1)) &&
2262 (xmlStrEqual(err->arg2, dup->arg2))) {
2263 goto skip;
2264 }
2265 }
2266 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2267 err->arg1, err->arg2);
2268 k++;
2269 }
2270 skip:
2271 if (err->flags & ERROR_IS_DUP) {
2272 if (err->arg1 != NULL)
2273 xmlFree((xmlChar *) err->arg1);
2274 err->arg1 = NULL;
2275 if (err->arg2 != NULL)
2276 xmlFree((xmlChar *) err->arg2);
2277 err->arg2 = NULL;
2278 err->flags = 0;
2279 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002280 }
2281 ctxt->errNr = 0;
2282}
Daniel Veillard4c004142003-10-07 11:33:24 +00002283
Daniel Veillard42f12e92003-03-07 18:32:59 +00002284/**
2285 * xmlRelaxNGAddValidError:
2286 * @ctxt: the validation context
2287 * @err: the error number
2288 * @arg1: the first argument
2289 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002290 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002291 *
2292 * Register a validation error, either generating it if it's sure
2293 * or stacking it for later handling if unsure.
2294 */
2295static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002296xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2297 xmlRelaxNGValidErr err, const xmlChar * arg1,
2298 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002299{
2300 if ((ctxt == NULL) || (ctxt->error == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002301 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002302
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002303#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002304 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002305#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002306 /*
2307 * generate the error directly
2308 */
2309 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002310 xmlNodePtr node, seq;
2311
2312 /*
2313 * Flush first any stacked error which might be the
2314 * real cause of the problem.
2315 */
2316 if (ctxt->errNr != 0)
2317 xmlRelaxNGDumpValidError(ctxt);
2318 if (ctxt->state != NULL) {
2319 node = ctxt->state->node;
2320 seq = ctxt->state->seq;
2321 } else {
2322 node = seq = NULL;
2323 }
2324 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002325 }
2326 /*
2327 * Stack the error for later processing if needed
2328 */
2329 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002330 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002331 }
2332}
2333
Daniel Veillard6eadf632003-01-23 18:29:16 +00002334
2335/************************************************************************
2336 * *
2337 * Type library hooks *
2338 * *
2339 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002340static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002341 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002342
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002343/**
2344 * xmlRelaxNGSchemaTypeHave:
2345 * @data: data needed for the library
2346 * @type: the type name
2347 *
2348 * Check if the given type is provided by
2349 * the W3C XMLSchema Datatype library.
2350 *
2351 * Returns 1 if yes, 0 if no and -1 in case of error.
2352 */
2353static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002354xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2355{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002356 xmlSchemaTypePtr typ;
2357
2358 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002359 return (-1);
2360 typ = xmlSchemaGetPredefinedType(type,
2361 BAD_CAST
2362 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002363 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002364 return (0);
2365 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002366}
2367
2368/**
2369 * xmlRelaxNGSchemaTypeCheck:
2370 * @data: data needed for the library
2371 * @type: the type name
2372 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002373 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002374 *
2375 * Check if the given type and value are validated by
2376 * the W3C XMLSchema Datatype library.
2377 *
2378 * Returns 1 if yes, 0 if no and -1 in case of error.
2379 */
2380static int
2381xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002382 const xmlChar * type,
2383 const xmlChar * value,
2384 void **result, xmlNodePtr node)
2385{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002386 xmlSchemaTypePtr typ;
2387 int ret;
2388
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002389 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002390 return (-1);
2391 typ = xmlSchemaGetPredefinedType(type,
2392 BAD_CAST
2393 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002394 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002395 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002396 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002397 (xmlSchemaValPtr *) result, node);
2398 if (ret == 2) /* special ID error code */
2399 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002400 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002401 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002402 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002403 return (0);
2404 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002405}
2406
2407/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002408 * xmlRelaxNGSchemaFacetCheck:
2409 * @data: data needed for the library
2410 * @type: the type name
2411 * @facet: the facet name
2412 * @val: the facet value
2413 * @strval: the string value
2414 * @value: the value to check
2415 *
2416 * Function provided by a type library to check a value facet
2417 *
2418 * Returns 1 if yes, 0 if no and -1 in case of error.
2419 */
2420static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002421xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2422 const xmlChar * type, const xmlChar * facetname,
2423 const xmlChar * val, const xmlChar * strval,
2424 void *value)
2425{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002426 xmlSchemaFacetPtr facet;
2427 xmlSchemaTypePtr typ;
2428 int ret;
2429
2430 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002431 return (-1);
2432 typ = xmlSchemaGetPredefinedType(type,
2433 BAD_CAST
2434 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002435 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002436 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002437
2438 facet = xmlSchemaNewFacet();
2439 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002440 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002441
Daniel Veillard4c004142003-10-07 11:33:24 +00002442 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002443 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002444 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002445 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002446 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002447 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002448 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002449 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002450 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002451 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002452 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002453 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002454 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002455 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002456 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002457 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002458 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002459 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002460 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002461 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002462 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002463 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2464 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2465 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2466 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002467 xmlSchemaFreeFacet(facet);
2468 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002469 }
2470 facet->value = xmlStrdup(val);
2471 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2472 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002473 xmlSchemaFreeFacet(facet);
2474 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002475 }
2476 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2477 xmlSchemaFreeFacet(facet);
2478 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002479 return (-1);
2480 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002481}
2482
2483/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002484 * xmlRelaxNGSchemaFreeValue:
2485 * @data: data needed for the library
2486 * @value: the value to free
2487 *
2488 * Function provided by a type library to free a Schemas value
2489 *
2490 * Returns 1 if yes, 0 if no and -1 in case of error.
2491 */
2492static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002493xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2494{
Daniel Veillard80b19092003-03-28 13:29:53 +00002495 xmlSchemaFreeValue(value);
2496}
2497
2498/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002499 * xmlRelaxNGSchemaTypeCompare:
2500 * @data: data needed for the library
2501 * @type: the type name
2502 * @value1: the first value
2503 * @value2: the second value
2504 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002505 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002506 * Datatype library.
2507 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002508 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002509 */
2510static int
2511xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002512 const xmlChar * type,
2513 const xmlChar * value1,
2514 xmlNodePtr ctxt1,
2515 void *comp1,
2516 const xmlChar * value2, xmlNodePtr ctxt2)
2517{
Daniel Veillard80b19092003-03-28 13:29:53 +00002518 int ret;
2519 xmlSchemaTypePtr typ;
2520 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2521
2522 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002523 return (-1);
2524 typ = xmlSchemaGetPredefinedType(type,
2525 BAD_CAST
2526 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002527 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002528 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002529 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002530 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2531 if (ret != 0)
2532 return (-1);
2533 if (res1 == NULL)
2534 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002535 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002536 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002537 }
2538 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002539 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002540 xmlSchemaFreeValue(res1);
2541 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002542 }
2543 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002544 xmlSchemaFreeValue(res1);
2545 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002546 }
2547 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002548 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002549 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002550 xmlSchemaFreeValue(res2);
2551 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002552 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002553 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002554 return (1);
2555 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002556}
Daniel Veillard4c004142003-10-07 11:33:24 +00002557
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002558/**
2559 * xmlRelaxNGDefaultTypeHave:
2560 * @data: data needed for the library
2561 * @type: the type name
2562 *
2563 * Check if the given type is provided by
2564 * the default datatype library.
2565 *
2566 * Returns 1 if yes, 0 if no and -1 in case of error.
2567 */
2568static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002569xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2570 const xmlChar * type)
2571{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002572 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002573 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002574 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002575 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002576 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002577 return (1);
2578 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002579}
2580
2581/**
2582 * xmlRelaxNGDefaultTypeCheck:
2583 * @data: data needed for the library
2584 * @type: the type name
2585 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002586 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002587 *
2588 * Check if the given type and value are validated by
2589 * the default datatype library.
2590 *
2591 * Returns 1 if yes, 0 if no and -1 in case of error.
2592 */
2593static int
2594xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002595 const xmlChar * type ATTRIBUTE_UNUSED,
2596 const xmlChar * value ATTRIBUTE_UNUSED,
2597 void **result ATTRIBUTE_UNUSED,
2598 xmlNodePtr node ATTRIBUTE_UNUSED)
2599{
Daniel Veillardd4310742003-02-18 21:12:46 +00002600 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002601 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002602 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002603 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002604 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002605 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002606 }
2607
Daniel Veillard4c004142003-10-07 11:33:24 +00002608 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002609}
2610
2611/**
2612 * xmlRelaxNGDefaultTypeCompare:
2613 * @data: data needed for the library
2614 * @type: the type name
2615 * @value1: the first value
2616 * @value2: the second value
2617 *
2618 * Compare two values accordingly a type from the default
2619 * datatype library.
2620 *
2621 * Returns 1 if yes, 0 if no and -1 in case of error.
2622 */
2623static int
2624xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002625 const xmlChar * type,
2626 const xmlChar * value1,
2627 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2628 void *comp1 ATTRIBUTE_UNUSED,
2629 const xmlChar * value2,
2630 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2631{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002632 int ret = -1;
2633
2634 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002635 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002636 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002637 if (!xmlStrEqual(value1, value2)) {
2638 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002639
Daniel Veillard4c004142003-10-07 11:33:24 +00002640 /*
2641 * TODO: trivial optimizations are possible by
2642 * computing at compile-time
2643 */
2644 nval = xmlRelaxNGNormalize(NULL, value1);
2645 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002646
Daniel Veillard4c004142003-10-07 11:33:24 +00002647 if ((nval == NULL) || (nvalue == NULL))
2648 ret = -1;
2649 else if (xmlStrEqual(nval, nvalue))
2650 ret = 1;
2651 else
2652 ret = 0;
2653 if (nval != NULL)
2654 xmlFree(nval);
2655 if (nvalue != NULL)
2656 xmlFree(nvalue);
2657 } else
2658 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002659 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002660 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002661}
Daniel Veillard4c004142003-10-07 11:33:24 +00002662
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002663static int xmlRelaxNGTypeInitialized = 0;
2664static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2665
2666/**
2667 * xmlRelaxNGFreeTypeLibrary:
2668 * @lib: the type library structure
2669 * @namespace: the URI bound to the library
2670 *
2671 * Free the structure associated to the type library
2672 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002673static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002674xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002675 const xmlChar * namespace ATTRIBUTE_UNUSED)
2676{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002677 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002678 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002679 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002680 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002681 xmlFree(lib);
2682}
2683
2684/**
2685 * xmlRelaxNGRegisterTypeLibrary:
2686 * @namespace: the URI bound to the library
2687 * @data: data associated to the library
2688 * @have: the provide function
2689 * @check: the checking function
2690 * @comp: the comparison function
2691 *
2692 * Register a new type library
2693 *
2694 * Returns 0 in case of success and -1 in case of error.
2695 */
2696static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002697xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2698 xmlRelaxNGTypeHave have,
2699 xmlRelaxNGTypeCheck check,
2700 xmlRelaxNGTypeCompare comp,
2701 xmlRelaxNGFacetCheck facet,
2702 xmlRelaxNGTypeFree freef)
2703{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002704 xmlRelaxNGTypeLibraryPtr lib;
2705 int ret;
2706
2707 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002708 (check == NULL) || (comp == NULL))
2709 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002710 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002711 xmlGenericError(xmlGenericErrorContext,
2712 "Relax-NG types library '%s' already registered\n",
2713 namespace);
2714 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002715 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002716 lib =
2717 (xmlRelaxNGTypeLibraryPtr)
2718 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002719 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002720 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002721 return (-1);
2722 }
2723 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2724 lib->namespace = xmlStrdup(namespace);
2725 lib->data = data;
2726 lib->have = have;
2727 lib->comp = comp;
2728 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002729 lib->facet = facet;
2730 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002731 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2732 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002733 xmlGenericError(xmlGenericErrorContext,
2734 "Relax-NG types library failed to register '%s'\n",
2735 namespace);
2736 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2737 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002738 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002739 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002740}
2741
2742/**
2743 * xmlRelaxNGInitTypes:
2744 *
2745 * Initilize the default type libraries.
2746 *
2747 * Returns 0 in case of success and -1 in case of error.
2748 */
2749static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002750xmlRelaxNGInitTypes(void)
2751{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002752 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002753 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002754 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2755 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002756 xmlGenericError(xmlGenericErrorContext,
2757 "Failed to allocate sh table for Relax-NG types\n");
2758 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002759 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002760 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2761 "http://www.w3.org/2001/XMLSchema-datatypes",
2762 NULL, xmlRelaxNGSchemaTypeHave,
2763 xmlRelaxNGSchemaTypeCheck,
2764 xmlRelaxNGSchemaTypeCompare,
2765 xmlRelaxNGSchemaFacetCheck,
2766 xmlRelaxNGSchemaFreeValue);
2767 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2768 xmlRelaxNGDefaultTypeHave,
2769 xmlRelaxNGDefaultTypeCheck,
2770 xmlRelaxNGDefaultTypeCompare, NULL,
2771 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002772 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002773 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002774}
2775
2776/**
2777 * xmlRelaxNGCleanupTypes:
2778 *
2779 * Cleanup the default Schemas type library associated to RelaxNG
2780 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002781void
2782xmlRelaxNGCleanupTypes(void)
2783{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002784 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002785 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002786 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002787 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002788 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002789 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002790}
2791
2792/************************************************************************
2793 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002794 * Compiling element content into regexp *
2795 * *
2796 * Sometime the element content can be compiled into a pure regexp, *
2797 * This allows a faster execution and streamability at that level *
2798 * *
2799 ************************************************************************/
2800
Daniel Veillard52b48c72003-04-13 19:53:42 +00002801static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2802 xmlRelaxNGDefinePtr def);
2803
Daniel Veillard952379b2003-03-17 15:37:12 +00002804/**
2805 * xmlRelaxNGIsCompileable:
2806 * @define: the definition to check
2807 *
2808 * Check if a definition is nullable.
2809 *
2810 * Returns 1 if yes, 0 if no and -1 in case of error
2811 */
2812static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002813xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2814{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002815 int ret = -1;
2816
Daniel Veillard952379b2003-03-17 15:37:12 +00002817 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002818 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002819 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002820 if ((def->type != XML_RELAXNG_ELEMENT) &&
2821 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002822 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002823 if ((def->type != XML_RELAXNG_ELEMENT) &&
2824 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002825 return (0);
2826 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002827 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002828 ret = xmlRelaxNGIsCompileable(def->content);
2829 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002830 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002831 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002832 ret = 1;
2833 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002834 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002835 /*
2836 * Check if the element content is compileable
2837 */
2838 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2839 ((def->dflags & IS_COMPILABLE) == 0)) {
2840 xmlRelaxNGDefinePtr list;
2841
2842 list = def->content;
2843 while (list != NULL) {
2844 ret = xmlRelaxNGIsCompileable(list);
2845 if (ret != 1)
2846 break;
2847 list = list->next;
2848 }
2849 if (ret == 0)
2850 def->dflags |= IS_NOT_COMPILABLE;
2851 if (ret == 1)
2852 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002853#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002854 if (ret == 1) {
2855 xmlGenericError(xmlGenericErrorContext,
2856 "element content for %s is compilable\n",
2857 def->name);
2858 } else if (ret == 0) {
2859 xmlGenericError(xmlGenericErrorContext,
2860 "element content for %s is not compilable\n",
2861 def->name);
2862 } else {
2863 xmlGenericError(xmlGenericErrorContext,
2864 "Problem in RelaxNGIsCompileable for element %s\n",
2865 def->name);
2866 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002867#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002868 }
2869 /*
2870 * All elements return a compileable status unless they
2871 * are generic like anyName
2872 */
2873 if ((def->nameClass != NULL) || (def->name == NULL))
2874 ret = 0;
2875 else
2876 ret = 1;
2877 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002878 case XML_RELAXNG_REF:
2879 case XML_RELAXNG_EXTERNALREF:
2880 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002881 if (def->depth == -20) {
2882 return (1);
2883 } else {
2884 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002885
Daniel Veillard4c004142003-10-07 11:33:24 +00002886 def->depth = -20;
2887 list = def->content;
2888 while (list != NULL) {
2889 ret = xmlRelaxNGIsCompileable(list);
2890 if (ret != 1)
2891 break;
2892 list = list->next;
2893 }
2894 }
2895 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002896 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002897 case XML_RELAXNG_OPTIONAL:
2898 case XML_RELAXNG_ZEROORMORE:
2899 case XML_RELAXNG_ONEORMORE:
2900 case XML_RELAXNG_CHOICE:
2901 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002902 case XML_RELAXNG_DEF:{
2903 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002904
Daniel Veillard4c004142003-10-07 11:33:24 +00002905 list = def->content;
2906 while (list != NULL) {
2907 ret = xmlRelaxNGIsCompileable(list);
2908 if (ret != 1)
2909 break;
2910 list = list->next;
2911 }
2912 break;
2913 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002914 case XML_RELAXNG_EXCEPT:
2915 case XML_RELAXNG_ATTRIBUTE:
2916 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002917 case XML_RELAXNG_DATATYPE:
2918 case XML_RELAXNG_LIST:
2919 case XML_RELAXNG_PARAM:
2920 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002921 ret = 0;
2922 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002923 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00002924 ret = -1;
2925 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002926 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002927 if (ret == 0)
2928 def->dflags |= IS_NOT_COMPILABLE;
2929 if (ret == 1)
2930 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002931#ifdef DEBUG_COMPILE
2932 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002933 xmlGenericError(xmlGenericErrorContext,
2934 "RelaxNGIsCompileable %s : true\n",
2935 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002936 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002937 xmlGenericError(xmlGenericErrorContext,
2938 "RelaxNGIsCompileable %s : false\n",
2939 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002940 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002941 xmlGenericError(xmlGenericErrorContext,
2942 "Problem in RelaxNGIsCompileable %s\n",
2943 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002944 }
2945#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002946 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002947}
2948
2949/**
2950 * xmlRelaxNGCompile:
2951 * ctxt: the RelaxNG parser context
2952 * @define: the definition tree to compile
2953 *
2954 * Compile the set of definitions, it works recursively, till the
2955 * element boundaries, where it tries to compile the content if possible
2956 *
2957 * Returns 0 if success and -1 in case of error
2958 */
2959static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002960xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
2961{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002962 int ret = 0;
2963 xmlRelaxNGDefinePtr list;
2964
Daniel Veillard4c004142003-10-07 11:33:24 +00002965 if ((ctxt == NULL) || (def == NULL))
2966 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002967
Daniel Veillard4c004142003-10-07 11:33:24 +00002968 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00002969 case XML_RELAXNG_START:
2970 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002971 xmlAutomataPtr oldam = ctxt->am;
2972 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002973
2974 def->depth = -25;
2975
Daniel Veillard4c004142003-10-07 11:33:24 +00002976 list = def->content;
2977 ctxt->am = xmlNewAutomata();
2978 if (ctxt->am == NULL)
2979 return (-1);
2980 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2981 while (list != NULL) {
2982 xmlRelaxNGCompile(ctxt, list);
2983 list = list->next;
2984 }
2985 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2986 def->contModel = xmlAutomataCompile(ctxt->am);
2987 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002988
Daniel Veillard4c004142003-10-07 11:33:24 +00002989 xmlFreeAutomata(ctxt->am);
2990 ctxt->state = oldstate;
2991 ctxt->am = oldam;
2992 }
2993 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002994 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002995 if ((ctxt->am != NULL) && (def->name != NULL)) {
2996 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
2997 ctxt->state, NULL,
2998 def->name, def->ns,
2999 def);
3000 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003001 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003002 xmlAutomataPtr oldam = ctxt->am;
3003 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003004
3005 def->depth = -25;
3006
Daniel Veillard4c004142003-10-07 11:33:24 +00003007 list = def->content;
3008 ctxt->am = xmlNewAutomata();
3009 if (ctxt->am == NULL)
3010 return (-1);
3011 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3012 while (list != NULL) {
3013 xmlRelaxNGCompile(ctxt, list);
3014 list = list->next;
3015 }
3016 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3017 def->contModel = xmlAutomataCompile(ctxt->am);
3018 if (!xmlRegexpIsDeterminist(def->contModel)) {
3019 /*
3020 * we can only use the automata if it is determinist
3021 */
3022 xmlRegFreeRegexp(def->contModel);
3023 def->contModel = NULL;
3024 }
3025 xmlFreeAutomata(ctxt->am);
3026 ctxt->state = oldstate;
3027 ctxt->am = oldam;
3028 } else {
3029 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003030
Daniel Veillard4c004142003-10-07 11:33:24 +00003031 /*
3032 * we can't build the content model for this element content
3033 * but it still might be possible to build it for some of its
3034 * children, recurse.
3035 */
3036 ret = xmlRelaxNGTryCompile(ctxt, def);
3037 ctxt->am = oldam;
3038 }
3039 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003040 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003041 ret = xmlRelaxNGCompile(ctxt, def->content);
3042 break;
3043 case XML_RELAXNG_OPTIONAL:{
3044 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003045
Daniel Veillard4c004142003-10-07 11:33:24 +00003046 xmlRelaxNGCompile(ctxt, def->content);
3047 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3048 break;
3049 }
3050 case XML_RELAXNG_ZEROORMORE:{
3051 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003052
Daniel Veillard4c004142003-10-07 11:33:24 +00003053 ctxt->state =
3054 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3055 oldstate = ctxt->state;
3056 list = def->content;
3057 while (list != NULL) {
3058 xmlRelaxNGCompile(ctxt, list);
3059 list = list->next;
3060 }
3061 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3062 ctxt->state =
3063 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3064 break;
3065 }
3066 case XML_RELAXNG_ONEORMORE:{
3067 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003068
Daniel Veillard4c004142003-10-07 11:33:24 +00003069 list = def->content;
3070 while (list != NULL) {
3071 xmlRelaxNGCompile(ctxt, list);
3072 list = list->next;
3073 }
3074 oldstate = ctxt->state;
3075 list = def->content;
3076 while (list != NULL) {
3077 xmlRelaxNGCompile(ctxt, list);
3078 list = list->next;
3079 }
3080 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3081 ctxt->state =
3082 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3083 break;
3084 }
3085 case XML_RELAXNG_CHOICE:{
3086 xmlAutomataStatePtr target = NULL;
3087 xmlAutomataStatePtr oldstate = ctxt->state;
3088
3089 list = def->content;
3090 while (list != NULL) {
3091 ctxt->state = oldstate;
3092 ret = xmlRelaxNGCompile(ctxt, list);
3093 if (ret != 0)
3094 break;
3095 if (target == NULL)
3096 target = ctxt->state;
3097 else {
3098 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3099 target);
3100 }
3101 list = list->next;
3102 }
3103 ctxt->state = target;
3104
3105 break;
3106 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003107 case XML_RELAXNG_REF:
3108 case XML_RELAXNG_EXTERNALREF:
3109 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003110 case XML_RELAXNG_GROUP:
3111 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003112 list = def->content;
3113 while (list != NULL) {
3114 ret = xmlRelaxNGCompile(ctxt, list);
3115 if (ret != 0)
3116 break;
3117 list = list->next;
3118 }
3119 break;
3120 case XML_RELAXNG_TEXT:{
3121 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003122
Daniel Veillard4c004142003-10-07 11:33:24 +00003123 ctxt->state =
3124 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3125 oldstate = ctxt->state;
3126 xmlRelaxNGCompile(ctxt, def->content);
3127 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3128 ctxt->state, BAD_CAST "#text",
3129 NULL);
3130 ctxt->state =
3131 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3132 break;
3133 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003134 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003135 ctxt->state =
3136 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3137 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003138 case XML_RELAXNG_EXCEPT:
3139 case XML_RELAXNG_ATTRIBUTE:
3140 case XML_RELAXNG_INTERLEAVE:
3141 case XML_RELAXNG_NOT_ALLOWED:
3142 case XML_RELAXNG_DATATYPE:
3143 case XML_RELAXNG_LIST:
3144 case XML_RELAXNG_PARAM:
3145 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003146 /* This should not happen and generate an internal error */
3147 fprintf(stderr, "RNG internal error trying to compile %s\n",
3148 xmlRelaxNGDefName(def));
3149 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003150 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003151 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003152}
3153
3154/**
3155 * xmlRelaxNGTryCompile:
3156 * ctxt: the RelaxNG parser context
3157 * @define: the definition tree to compile
3158 *
3159 * Try to compile the set of definitions, it works recursively,
3160 * possibly ignoring parts which cannot be compiled.
3161 *
3162 * Returns 0 if success and -1 in case of error
3163 */
3164static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003165xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3166{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003167 int ret = 0;
3168 xmlRelaxNGDefinePtr list;
3169
Daniel Veillard4c004142003-10-07 11:33:24 +00003170 if ((ctxt == NULL) || (def == NULL))
3171 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003172
3173 if ((def->type == XML_RELAXNG_START) ||
3174 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003175 ret = xmlRelaxNGIsCompileable(def);
3176 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3177 ctxt->am = NULL;
3178 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003179#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003180 if (ret == 0) {
3181 if (def->type == XML_RELAXNG_START)
3182 xmlGenericError(xmlGenericErrorContext,
3183 "compiled the start\n");
3184 else
3185 xmlGenericError(xmlGenericErrorContext,
3186 "compiled element %s\n", def->name);
3187 } else {
3188 if (def->type == XML_RELAXNG_START)
3189 xmlGenericError(xmlGenericErrorContext,
3190 "failed to compile the start\n");
3191 else
3192 xmlGenericError(xmlGenericErrorContext,
3193 "failed to compile element %s\n",
3194 def->name);
3195 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003196#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003197 return (ret);
3198 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003199 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003200 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003201 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003202 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3203 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003204 case XML_RELAXNG_TEXT:
3205 case XML_RELAXNG_DATATYPE:
3206 case XML_RELAXNG_LIST:
3207 case XML_RELAXNG_PARAM:
3208 case XML_RELAXNG_VALUE:
3209 case XML_RELAXNG_EMPTY:
3210 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003211 ret = 0;
3212 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003213 case XML_RELAXNG_OPTIONAL:
3214 case XML_RELAXNG_ZEROORMORE:
3215 case XML_RELAXNG_ONEORMORE:
3216 case XML_RELAXNG_CHOICE:
3217 case XML_RELAXNG_GROUP:
3218 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003219 case XML_RELAXNG_START:
3220 case XML_RELAXNG_REF:
3221 case XML_RELAXNG_EXTERNALREF:
3222 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003223 list = def->content;
3224 while (list != NULL) {
3225 ret = xmlRelaxNGTryCompile(ctxt, list);
3226 if (ret != 0)
3227 break;
3228 list = list->next;
3229 }
3230 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003231 case XML_RELAXNG_EXCEPT:
3232 case XML_RELAXNG_ATTRIBUTE:
3233 case XML_RELAXNG_INTERLEAVE:
3234 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003235 ret = 0;
3236 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003237 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003238 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003239}
3240
3241/************************************************************************
3242 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003243 * Parsing functions *
3244 * *
3245 ************************************************************************/
3246
Daniel Veillard4c004142003-10-07 11:33:24 +00003247static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3248 ctxt, xmlNodePtr node);
3249static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3250 ctxt, xmlNodePtr node);
3251static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3252 ctxt, xmlNodePtr nodes,
3253 int group);
3254static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3255 ctxt, xmlNodePtr node);
3256static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3257 xmlNodePtr node);
3258static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3259 xmlNodePtr nodes);
3260static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3261 ctxt, xmlNodePtr node,
3262 xmlRelaxNGDefinePtr
3263 def);
3264static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3265 ctxt, xmlNodePtr nodes);
3266static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3267 xmlRelaxNGDefinePtr define,
3268 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003269
3270
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003271#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003272
3273/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003274 * xmlRelaxNGIsNullable:
3275 * @define: the definition to verify
3276 *
3277 * Check if a definition is nullable.
3278 *
3279 * Returns 1 if yes, 0 if no and -1 in case of error
3280 */
3281static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003282xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3283{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003284 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003285
Daniel Veillardfd573f12003-03-16 17:52:32 +00003286 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003287 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003288
Daniel Veillarde063f482003-03-21 16:53:17 +00003289 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003290 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003291 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003292 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003293 switch (define->type) {
3294 case XML_RELAXNG_EMPTY:
3295 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003296 ret = 1;
3297 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003298 case XML_RELAXNG_NOOP:
3299 case XML_RELAXNG_DEF:
3300 case XML_RELAXNG_REF:
3301 case XML_RELAXNG_EXTERNALREF:
3302 case XML_RELAXNG_PARENTREF:
3303 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003304 ret = xmlRelaxNGIsNullable(define->content);
3305 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003306 case XML_RELAXNG_EXCEPT:
3307 case XML_RELAXNG_NOT_ALLOWED:
3308 case XML_RELAXNG_ELEMENT:
3309 case XML_RELAXNG_DATATYPE:
3310 case XML_RELAXNG_PARAM:
3311 case XML_RELAXNG_VALUE:
3312 case XML_RELAXNG_LIST:
3313 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003314 ret = 0;
3315 break;
3316 case XML_RELAXNG_CHOICE:{
3317 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003318
Daniel Veillard4c004142003-10-07 11:33:24 +00003319 while (list != NULL) {
3320 ret = xmlRelaxNGIsNullable(list);
3321 if (ret != 0)
3322 goto done;
3323 list = list->next;
3324 }
3325 ret = 0;
3326 break;
3327 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003328 case XML_RELAXNG_START:
3329 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003330 case XML_RELAXNG_GROUP:{
3331 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003332
Daniel Veillard4c004142003-10-07 11:33:24 +00003333 while (list != NULL) {
3334 ret = xmlRelaxNGIsNullable(list);
3335 if (ret != 1)
3336 goto done;
3337 list = list->next;
3338 }
3339 return (1);
3340 }
3341 default:
3342 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003343 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003344 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003345 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003346 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003347 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003348 define->dflags |= IS_NULLABLE;
3349 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003350}
3351
3352/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003353 * xmlRelaxNGIsBlank:
3354 * @str: a string
3355 *
3356 * Check if a string is ignorable c.f. 4.2. Whitespace
3357 *
3358 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3359 */
3360static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003361xmlRelaxNGIsBlank(xmlChar * str)
3362{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003363 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003364 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003365 while (*str != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003366 if (!(IS_BLANK(*str)))
3367 return (0);
3368 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003369 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003370 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003371}
3372
Daniel Veillard6eadf632003-01-23 18:29:16 +00003373/**
3374 * xmlRelaxNGGetDataTypeLibrary:
3375 * @ctxt: a Relax-NG parser context
3376 * @node: the current data or value element
3377 *
3378 * Applies algorithm from 4.3. datatypeLibrary attribute
3379 *
3380 * Returns the datatypeLibary value or NULL if not found
3381 */
3382static xmlChar *
3383xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003384 xmlNodePtr node)
3385{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003386 xmlChar *ret, *escape;
3387
Daniel Veillard6eadf632003-01-23 18:29:16 +00003388 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003389 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3390 if (ret != NULL) {
3391 if (ret[0] == 0) {
3392 xmlFree(ret);
3393 return (NULL);
3394 }
3395 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3396 if (escape == NULL) {
3397 return (ret);
3398 }
3399 xmlFree(ret);
3400 return (escape);
3401 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003402 }
3403 node = node->parent;
3404 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003405 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3406 if (ret != NULL) {
3407 if (ret[0] == 0) {
3408 xmlFree(ret);
3409 return (NULL);
3410 }
3411 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3412 if (escape == NULL) {
3413 return (ret);
3414 }
3415 xmlFree(ret);
3416 return (escape);
3417 }
3418 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003419 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003420 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003421}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003422
3423/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003424 * xmlRelaxNGParseValue:
3425 * @ctxt: a Relax-NG parser context
3426 * @node: the data node.
3427 *
3428 * parse the content of a RelaxNG value node.
3429 *
3430 * Returns the definition pointer or NULL in case of error
3431 */
3432static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003433xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3434{
Daniel Veillardedc91922003-01-26 00:52:04 +00003435 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003436 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003437 xmlChar *type;
3438 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003439 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003440
Daniel Veillardfd573f12003-03-16 17:52:32 +00003441 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003442 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003443 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003444 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003445
3446 type = xmlGetProp(node, BAD_CAST "type");
3447 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003448 xmlRelaxNGNormExtSpace(type);
3449 if (xmlValidateNCName(type, 0)) {
3450 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3451 "value type '%s' is not an NCName\n", type, NULL);
3452 }
3453 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3454 if (library == NULL)
3455 library =
3456 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003457
Daniel Veillard4c004142003-10-07 11:33:24 +00003458 def->name = type;
3459 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003460
Daniel Veillard4c004142003-10-07 11:33:24 +00003461 lib = (xmlRelaxNGTypeLibraryPtr)
3462 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3463 if (lib == NULL) {
3464 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3465 "Use of unregistered type library '%s'\n", library,
3466 NULL);
3467 def->data = NULL;
3468 } else {
3469 def->data = lib;
3470 if (lib->have == NULL) {
3471 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3472 "Internal error with type library '%s': no 'have'\n",
3473 library, NULL);
3474 } else {
3475 success = lib->have(lib->data, def->name);
3476 if (success != 1) {
3477 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3478 "Error type '%s' is not exported by type library '%s'\n",
3479 def->name, library);
3480 }
3481 }
3482 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003483 }
3484 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003485 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003486 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003487 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3488 (node->children->next != NULL)) {
3489 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3490 "Expecting a single text value for <value>content\n",
3491 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003492 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003493 def->value = xmlNodeGetContent(node);
3494 if (def->value == NULL) {
3495 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3496 "Element <value> has no content\n", NULL, NULL);
3497 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3498 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003499
Daniel Veillard4c004142003-10-07 11:33:24 +00003500 success =
3501 lib->check(lib->data, def->name, def->value, &val, node);
3502 if (success != 1) {
3503 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3504 "Value '%s' is not acceptable for type '%s'\n",
3505 def->value, def->name);
3506 } else {
3507 if (val != NULL)
3508 def->attrs = val;
3509 }
3510 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003511 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003512 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003513}
3514
3515/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003516 * xmlRelaxNGParseData:
3517 * @ctxt: a Relax-NG parser context
3518 * @node: the data node.
3519 *
3520 * parse the content of a RelaxNG data node.
3521 *
3522 * Returns the definition pointer or NULL in case of error
3523 */
3524static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003525xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3526{
Daniel Veillard416589a2003-02-17 17:25:42 +00003527 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003528 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003529 xmlRelaxNGTypeLibraryPtr lib;
3530 xmlChar *type;
3531 xmlChar *library;
3532 xmlNodePtr content;
3533 int tmp;
3534
3535 type = xmlGetProp(node, BAD_CAST "type");
3536 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003537 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3538 NULL);
3539 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003540 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003541 xmlRelaxNGNormExtSpace(type);
3542 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003543 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3544 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003545 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003546 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3547 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003548 library =
3549 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003550
Daniel Veillardfd573f12003-03-16 17:52:32 +00003551 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003552 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003553 xmlFree(type);
3554 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003555 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003556 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003557 def->name = type;
3558 def->ns = library;
3559
3560 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003561 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003562 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003563 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3564 "Use of unregistered type library '%s'\n", library,
3565 NULL);
3566 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003567 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003568 def->data = lib;
3569 if (lib->have == NULL) {
3570 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3571 "Internal error with type library '%s': no 'have'\n",
3572 library, NULL);
3573 } else {
3574 tmp = lib->have(lib->data, def->name);
3575 if (tmp != 1) {
3576 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3577 "Error type '%s' is not exported by type library '%s'\n",
3578 def->name, library);
3579 } else
3580 if ((xmlStrEqual
3581 (library,
3582 BAD_CAST
3583 "http://www.w3.org/2001/XMLSchema-datatypes"))
3584 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3585 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3586 ctxt->idref = 1;
3587 }
3588 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003589 }
3590 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003591
3592 /*
3593 * Handle optional params
3594 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003595 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003596 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3597 break;
3598 if (xmlStrEqual(library,
3599 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3600 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3601 "Type library '%s' does not allow type parameters\n",
3602 library, NULL);
3603 content = content->next;
3604 while ((content != NULL) &&
3605 (xmlStrEqual(content->name, BAD_CAST "param")))
3606 content = content->next;
3607 } else {
3608 param = xmlRelaxNGNewDefine(ctxt, node);
3609 if (param != NULL) {
3610 param->type = XML_RELAXNG_PARAM;
3611 param->name = xmlGetProp(content, BAD_CAST "name");
3612 if (param->name == NULL) {
3613 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3614 "param has no name\n", NULL, NULL);
3615 }
3616 param->value = xmlNodeGetContent(content);
3617 if (lastparam == NULL) {
3618 def->attrs = lastparam = param;
3619 } else {
3620 lastparam->next = param;
3621 lastparam = param;
3622 }
3623 if (lib != NULL) {
3624 }
3625 }
3626 content = content->next;
3627 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003628 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003629 /*
3630 * Handle optional except
3631 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003632 if ((content != NULL)
3633 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3634 xmlNodePtr child;
3635 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003636
Daniel Veillard4c004142003-10-07 11:33:24 +00003637 except = xmlRelaxNGNewDefine(ctxt, node);
3638 if (except == NULL) {
3639 return (def);
3640 }
3641 except->type = XML_RELAXNG_EXCEPT;
3642 child = content->children;
3643 if (last == NULL) {
3644 def->content = except;
3645 } else {
3646 last->next = except;
3647 }
3648 if (child == NULL) {
3649 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3650 "except has no content\n", NULL, NULL);
3651 }
3652 while (child != NULL) {
3653 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3654 if (tmp2 != NULL) {
3655 if (last2 == NULL) {
3656 except->content = last2 = tmp2;
3657 } else {
3658 last2->next = tmp2;
3659 last2 = tmp2;
3660 }
3661 }
3662 child = child->next;
3663 }
3664 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003665 }
3666 /*
3667 * Check there is no unhandled data
3668 */
3669 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003670 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3671 "Element data has unexpected content %s\n",
3672 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003673 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003674
Daniel Veillard4c004142003-10-07 11:33:24 +00003675 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003676}
3677
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003678static const xmlChar *invalidName = BAD_CAST "\1";
3679
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003680/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003681 * xmlRelaxNGCompareNameClasses:
3682 * @defs1: the first element/attribute defs
3683 * @defs2: the second element/attribute defs
3684 * @name: the restriction on the name
3685 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003686 *
3687 * Compare the 2 lists of element definitions. The comparison is
3688 * that if both lists do not accept the same QNames, it returns 1
3689 * If the 2 lists can accept the same QName the comparison returns 0
3690 *
3691 * Returns 1 disttinct, 0 if equal
3692 */
3693static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003694xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003695 xmlRelaxNGDefinePtr def2)
3696{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003697 int ret = 1;
3698 xmlNode node;
3699 xmlNs ns;
3700 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003701
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003702 ctxt.flags = FLAGS_IGNORABLE;
3703
Daniel Veillard42f12e92003-03-07 18:32:59 +00003704 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3705
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003706 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003707 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3708 if (def2->type == XML_RELAXNG_TEXT)
3709 return (1);
3710 if (def1->name != NULL) {
3711 node.name = def1->name;
3712 } else {
3713 node.name = invalidName;
3714 }
3715 node.ns = &ns;
3716 if (def1->ns != NULL) {
3717 if (def1->ns[0] == 0) {
3718 node.ns = NULL;
3719 } else {
3720 ns.href = def1->ns;
3721 }
3722 } else {
3723 ns.href = invalidName;
3724 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003725 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003726 if (def1->nameClass != NULL) {
3727 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3728 } else {
3729 ret = 0;
3730 }
3731 } else {
3732 ret = 1;
3733 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003734 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003735 if (def2->type == XML_RELAXNG_TEXT)
3736 return (0);
3737 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003738 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003739 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003740 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003741 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003742 }
3743 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003744 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003745 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003746 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3747 if (def2->name != NULL) {
3748 node.name = def2->name;
3749 } else {
3750 node.name = invalidName;
3751 }
3752 node.ns = &ns;
3753 if (def2->ns != NULL) {
3754 if (def2->ns[0] == 0) {
3755 node.ns = NULL;
3756 } else {
3757 ns.href = def2->ns;
3758 }
3759 } else {
3760 ns.href = invalidName;
3761 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003762 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003763 if (def2->nameClass != NULL) {
3764 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3765 } else {
3766 ret = 0;
3767 }
3768 } else {
3769 ret = 1;
3770 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003771 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003772 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003773 }
3774
Daniel Veillard4c004142003-10-07 11:33:24 +00003775 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003776}
3777
3778/**
3779 * xmlRelaxNGCompareElemDefLists:
3780 * @ctxt: a Relax-NG parser context
3781 * @defs1: the first list of element/attribute defs
3782 * @defs2: the second list of element/attribute defs
3783 *
3784 * Compare the 2 lists of element or attribute definitions. The comparison
3785 * is that if both lists do not accept the same QNames, it returns 1
3786 * If the 2 lists can accept the same QName the comparison returns 0
3787 *
3788 * Returns 1 disttinct, 0 if equal
3789 */
3790static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003791xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3792 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3793 xmlRelaxNGDefinePtr * def2)
3794{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003795 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003796
Daniel Veillard154877e2003-01-30 12:17:05 +00003797 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003798 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003799 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003800 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003801 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003802 while ((*def2) != NULL) {
3803 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3804 return (0);
3805 def2++;
3806 }
3807 def2 = basedef2;
3808 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003809 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003810 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003811}
3812
3813/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003814 * xmlRelaxNGGenerateAttributes:
3815 * @ctxt: a Relax-NG parser context
3816 * @def: the definition definition
3817 *
3818 * Check if the definition can only generate attributes
3819 *
3820 * Returns 1 if yes, 0 if no and -1 in case of error.
3821 */
3822static int
3823xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003824 xmlRelaxNGDefinePtr def)
3825{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003826 xmlRelaxNGDefinePtr parent, cur, tmp;
3827
3828 /*
3829 * Don't run that check in case of error. Infinite recursion
3830 * becomes possible.
3831 */
3832 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003833 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003834
3835 parent = NULL;
3836 cur = def;
3837 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003838 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3839 (cur->type == XML_RELAXNG_TEXT) ||
3840 (cur->type == XML_RELAXNG_DATATYPE) ||
3841 (cur->type == XML_RELAXNG_PARAM) ||
3842 (cur->type == XML_RELAXNG_LIST) ||
3843 (cur->type == XML_RELAXNG_VALUE) ||
3844 (cur->type == XML_RELAXNG_EMPTY))
3845 return (0);
3846 if ((cur->type == XML_RELAXNG_CHOICE) ||
3847 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3848 (cur->type == XML_RELAXNG_GROUP) ||
3849 (cur->type == XML_RELAXNG_ONEORMORE) ||
3850 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3851 (cur->type == XML_RELAXNG_OPTIONAL) ||
3852 (cur->type == XML_RELAXNG_PARENTREF) ||
3853 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3854 (cur->type == XML_RELAXNG_REF) ||
3855 (cur->type == XML_RELAXNG_DEF)) {
3856 if (cur->content != NULL) {
3857 parent = cur;
3858 cur = cur->content;
3859 tmp = cur;
3860 while (tmp != NULL) {
3861 tmp->parent = parent;
3862 tmp = tmp->next;
3863 }
3864 continue;
3865 }
3866 }
3867 if (cur == def)
3868 break;
3869 if (cur->next != NULL) {
3870 cur = cur->next;
3871 continue;
3872 }
3873 do {
3874 cur = cur->parent;
3875 if (cur == NULL)
3876 break;
3877 if (cur == def)
3878 return (1);
3879 if (cur->next != NULL) {
3880 cur = cur->next;
3881 break;
3882 }
3883 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003884 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003885 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003886}
Daniel Veillard4c004142003-10-07 11:33:24 +00003887
Daniel Veillardce192eb2003-04-16 15:58:05 +00003888/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003889 * xmlRelaxNGGetElements:
3890 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003891 * @def: the definition definition
3892 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003893 *
3894 * Compute the list of top elements a definition can generate
3895 *
3896 * Returns a list of elements or NULL if none was found.
3897 */
3898static xmlRelaxNGDefinePtr *
3899xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003900 xmlRelaxNGDefinePtr def, int eora)
3901{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003902 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003903 int len = 0;
3904 int max = 0;
3905
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003906 /*
3907 * Don't run that check in case of error. Infinite recursion
3908 * becomes possible.
3909 */
3910 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003911 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003912
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003913 parent = NULL;
3914 cur = def;
3915 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003916 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3917 (cur->type == XML_RELAXNG_TEXT))) ||
3918 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3919 if (ret == NULL) {
3920 max = 10;
3921 ret = (xmlRelaxNGDefinePtr *)
3922 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3923 if (ret == NULL) {
3924 xmlRngPErrMemory(ctxt, "getting element list\n");
3925 return (NULL);
3926 }
3927 } else if (max <= len) {
3928 max *= 2;
3929 ret =
3930 xmlRealloc(ret,
3931 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3932 if (ret == NULL) {
3933 xmlRngPErrMemory(ctxt, "getting element list\n");
3934 return (NULL);
3935 }
3936 }
3937 ret[len++] = cur;
3938 ret[len] = NULL;
3939 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3940 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3941 (cur->type == XML_RELAXNG_GROUP) ||
3942 (cur->type == XML_RELAXNG_ONEORMORE) ||
3943 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3944 (cur->type == XML_RELAXNG_OPTIONAL) ||
3945 (cur->type == XML_RELAXNG_PARENTREF) ||
3946 (cur->type == XML_RELAXNG_REF) ||
3947 (cur->type == XML_RELAXNG_DEF)) {
3948 /*
3949 * Don't go within elements or attributes or string values.
3950 * Just gather the element top list
3951 */
3952 if (cur->content != NULL) {
3953 parent = cur;
3954 cur = cur->content;
3955 tmp = cur;
3956 while (tmp != NULL) {
3957 tmp->parent = parent;
3958 tmp = tmp->next;
3959 }
3960 continue;
3961 }
3962 }
3963 if (cur == def)
3964 break;
3965 if (cur->next != NULL) {
3966 cur = cur->next;
3967 continue;
3968 }
3969 do {
3970 cur = cur->parent;
3971 if (cur == NULL)
3972 break;
3973 if (cur == def)
3974 return (ret);
3975 if (cur->next != NULL) {
3976 cur = cur->next;
3977 break;
3978 }
3979 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003980 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003981 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003982}
Daniel Veillard4c004142003-10-07 11:33:24 +00003983
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003984/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003985 * xmlRelaxNGCheckChoiceDeterminism:
3986 * @ctxt: a Relax-NG parser context
3987 * @def: the choice definition
3988 *
3989 * Also used to find indeterministic pattern in choice
3990 */
3991static void
3992xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003993 xmlRelaxNGDefinePtr def)
3994{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003995 xmlRelaxNGDefinePtr **list;
3996 xmlRelaxNGDefinePtr cur;
3997 int nbchild = 0, i, j, ret;
3998 int is_nullable = 0;
3999 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004000 xmlHashTablePtr triage = NULL;
4001 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004002
Daniel Veillard4c004142003-10-07 11:33:24 +00004003 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4004 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004005
Daniel Veillarde063f482003-03-21 16:53:17 +00004006 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004007 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004008
Daniel Veillardfd573f12003-03-16 17:52:32 +00004009 /*
4010 * Don't run that check in case of error. Infinite recursion
4011 * becomes possible.
4012 */
4013 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004014 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004015
4016 is_nullable = xmlRelaxNGIsNullable(def);
4017
4018 cur = def->content;
4019 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004020 nbchild++;
4021 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004022 }
4023
4024 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004025 sizeof(xmlRelaxNGDefinePtr
4026 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004027 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004028 xmlRngPErrMemory(ctxt, "building choice\n");
4029 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004030 }
4031 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004032 /*
4033 * a bit strong but safe
4034 */
4035 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004036 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004037 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004038 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004039 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004040 cur = def->content;
4041 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004042 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4043 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4044 is_triable = 0;
4045 } else if (is_triable == 1) {
4046 xmlRelaxNGDefinePtr *tmp;
4047 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004048
Daniel Veillard4c004142003-10-07 11:33:24 +00004049 tmp = list[i];
4050 while ((*tmp != NULL) && (is_triable == 1)) {
4051 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4052 res = xmlHashAddEntry2(triage,
4053 BAD_CAST "#text", NULL,
4054 (void *) cur);
4055 if (res != 0)
4056 is_triable = -1;
4057 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4058 ((*tmp)->name != NULL)) {
4059 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4060 res = xmlHashAddEntry2(triage,
4061 (*tmp)->name, NULL,
4062 (void *) cur);
4063 else
4064 res = xmlHashAddEntry2(triage,
4065 (*tmp)->name, (*tmp)->ns,
4066 (void *) cur);
4067 if (res != 0)
4068 is_triable = -1;
4069 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4070 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4071 res = xmlHashAddEntry2(triage,
4072 BAD_CAST "#any", NULL,
4073 (void *) cur);
4074 else
4075 res = xmlHashAddEntry2(triage,
4076 BAD_CAST "#any", (*tmp)->ns,
4077 (void *) cur);
4078 if (res != 0)
4079 is_triable = -1;
4080 } else {
4081 is_triable = -1;
4082 }
4083 tmp++;
4084 }
4085 }
4086 i++;
4087 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004088 }
4089
Daniel Veillard4c004142003-10-07 11:33:24 +00004090 for (i = 0; i < nbchild; i++) {
4091 if (list[i] == NULL)
4092 continue;
4093 for (j = 0; j < i; j++) {
4094 if (list[j] == NULL)
4095 continue;
4096 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4097 if (ret == 0) {
4098 is_indeterminist = 1;
4099 }
4100 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004101 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004102 for (i = 0; i < nbchild; i++) {
4103 if (list[i] != NULL)
4104 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004105 }
4106
4107 xmlFree(list);
4108 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004109 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004110 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004111 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004112 def->dflags |= IS_TRIABLE;
4113 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004114 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004115 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004116 }
4117 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004118}
4119
4120/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004121 * xmlRelaxNGCheckGroupAttrs:
4122 * @ctxt: a Relax-NG parser context
4123 * @def: the group definition
4124 *
4125 * Detects violations of rule 7.3
4126 */
4127static void
4128xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004129 xmlRelaxNGDefinePtr def)
4130{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004131 xmlRelaxNGDefinePtr **list;
4132 xmlRelaxNGDefinePtr cur;
4133 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004134
4135 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004136 ((def->type != XML_RELAXNG_GROUP) &&
4137 (def->type != XML_RELAXNG_ELEMENT)))
4138 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004139
Daniel Veillarde063f482003-03-21 16:53:17 +00004140 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004141 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004142
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004143 /*
4144 * Don't run that check in case of error. Infinite recursion
4145 * becomes possible.
4146 */
4147 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004148 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004149
Daniel Veillardfd573f12003-03-16 17:52:32 +00004150 cur = def->attrs;
4151 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004152 nbchild++;
4153 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004154 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004155 cur = def->content;
4156 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004157 nbchild++;
4158 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004159 }
4160
4161 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004162 sizeof(xmlRelaxNGDefinePtr
4163 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004164 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004165 xmlRngPErrMemory(ctxt, "building group\n");
4166 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004167 }
4168 i = 0;
4169 cur = def->attrs;
4170 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004171 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4172 i++;
4173 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004174 }
4175 cur = def->content;
4176 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004177 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4178 i++;
4179 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004180 }
4181
Daniel Veillard4c004142003-10-07 11:33:24 +00004182 for (i = 0; i < nbchild; i++) {
4183 if (list[i] == NULL)
4184 continue;
4185 for (j = 0; j < i; j++) {
4186 if (list[j] == NULL)
4187 continue;
4188 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4189 if (ret == 0) {
4190 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4191 "Attributes conflicts in group\n", NULL, NULL);
4192 }
4193 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004194 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004195 for (i = 0; i < nbchild; i++) {
4196 if (list[i] != NULL)
4197 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004198 }
4199
4200 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004201 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004202}
4203
4204/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004205 * xmlRelaxNGComputeInterleaves:
4206 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004207 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004208 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004209 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004210 * A lot of work for preprocessing interleave definitions
4211 * is potentially needed to get a decent execution speed at runtime
4212 * - trying to get a total order on the element nodes generated
4213 * by the interleaves, order the list of interleave definitions
4214 * following that order.
4215 * - if <text/> is used to handle mixed content, it is better to
4216 * flag this in the define and simplify the runtime checking
4217 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004218 */
4219static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004220xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004221 xmlRelaxNGParserCtxtPtr ctxt,
4222 xmlChar * name ATTRIBUTE_UNUSED)
4223{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004224 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004225
Daniel Veillardfd573f12003-03-16 17:52:32 +00004226 xmlRelaxNGPartitionPtr partitions = NULL;
4227 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4228 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004229 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004230 int nbgroups = 0;
4231 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004232 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004233 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004234
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004235 /*
4236 * Don't run that check in case of error. Infinite recursion
4237 * becomes possible.
4238 */
4239 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004240 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004241
Daniel Veillardfd573f12003-03-16 17:52:32 +00004242#ifdef DEBUG_INTERLEAVE
4243 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004244 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004245#endif
4246 cur = def->content;
4247 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004248 nbchild++;
4249 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004250 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004251
Daniel Veillardfd573f12003-03-16 17:52:32 +00004252#ifdef DEBUG_INTERLEAVE
4253 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4254#endif
4255 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004256 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004257 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004258 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004259 cur = def->content;
4260 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004261 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4262 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4263 if (groups[nbgroups] == NULL)
4264 goto error;
4265 if (cur->type == XML_RELAXNG_TEXT)
4266 is_mixed++;
4267 groups[nbgroups]->rule = cur;
4268 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4269 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4270 nbgroups++;
4271 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004272 }
4273#ifdef DEBUG_INTERLEAVE
4274 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4275#endif
4276
4277 /*
4278 * Let's check that all rules makes a partitions according to 7.4
4279 */
4280 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004281 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004282 if (partitions == NULL)
4283 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004284 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004285 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004286 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004287 for (i = 0; i < nbgroups; i++) {
4288 group = groups[i];
4289 for (j = i + 1; j < nbgroups; j++) {
4290 if (groups[j] == NULL)
4291 continue;
4292
4293 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4294 groups[j]->defs);
4295 if (ret == 0) {
4296 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4297 "Element or text conflicts in interleave\n",
4298 NULL, NULL);
4299 }
4300 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4301 groups[j]->attrs);
4302 if (ret == 0) {
4303 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4304 "Attributes conflicts in interleave\n", NULL,
4305 NULL);
4306 }
4307 }
4308 tmp = group->defs;
4309 if ((tmp != NULL) && (*tmp != NULL)) {
4310 while (*tmp != NULL) {
4311 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4312 res = xmlHashAddEntry2(partitions->triage,
4313 BAD_CAST "#text", NULL,
4314 (void *) (long) (i + 1));
4315 if (res != 0)
4316 is_determinist = -1;
4317 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4318 ((*tmp)->name != NULL)) {
4319 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4320 res = xmlHashAddEntry2(partitions->triage,
4321 (*tmp)->name, NULL,
4322 (void *) (long) (i + 1));
4323 else
4324 res = xmlHashAddEntry2(partitions->triage,
4325 (*tmp)->name, (*tmp)->ns,
4326 (void *) (long) (i + 1));
4327 if (res != 0)
4328 is_determinist = -1;
4329 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4330 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4331 res = xmlHashAddEntry2(partitions->triage,
4332 BAD_CAST "#any", NULL,
4333 (void *) (long) (i + 1));
4334 else
4335 res = xmlHashAddEntry2(partitions->triage,
4336 BAD_CAST "#any", (*tmp)->ns,
4337 (void *) (long) (i + 1));
4338 if ((*tmp)->nameClass != NULL)
4339 is_determinist = 2;
4340 if (res != 0)
4341 is_determinist = -1;
4342 } else {
4343 is_determinist = -1;
4344 }
4345 tmp++;
4346 }
4347 } else {
4348 is_determinist = 0;
4349 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004350 }
4351 partitions->groups = groups;
4352
4353 /*
4354 * and save the partition list back in the def
4355 */
4356 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004357 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004358 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004359 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004360 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004361 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004362 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004363 return;
4364
Daniel Veillard4c004142003-10-07 11:33:24 +00004365 error:
4366 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004367 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004368 for (i = 0; i < nbgroups; i++)
4369 if (groups[i] != NULL) {
4370 if (groups[i]->defs != NULL)
4371 xmlFree(groups[i]->defs);
4372 xmlFree(groups[i]);
4373 }
4374 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004375 }
4376 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004377}
4378
4379/**
4380 * xmlRelaxNGParseInterleave:
4381 * @ctxt: a Relax-NG parser context
4382 * @node: the data node.
4383 *
4384 * parse the content of a RelaxNG interleave node.
4385 *
4386 * Returns the definition pointer or NULL in case of error
4387 */
4388static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004389xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4390{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004391 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004392 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004393 xmlNodePtr child;
4394
Daniel Veillardfd573f12003-03-16 17:52:32 +00004395 def = xmlRelaxNGNewDefine(ctxt, node);
4396 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004397 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004398 }
4399 def->type = XML_RELAXNG_INTERLEAVE;
4400
4401 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004402 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004403 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004404 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004405 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004406 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004407
Daniel Veillard4c004142003-10-07 11:33:24 +00004408 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4409 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4410 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4411 "Failed to add %s to hash table\n",
4412 (const xmlChar *) name, NULL);
4413 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004414 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004415 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004416 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004417 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4418 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004419 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004420 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004421 if (IS_RELAXNG(child, "element")) {
4422 cur = xmlRelaxNGParseElement(ctxt, child);
4423 } else {
4424 cur = xmlRelaxNGParsePattern(ctxt, child);
4425 }
4426 if (cur != NULL) {
4427 cur->parent = def;
4428 if (last == NULL) {
4429 def->content = last = cur;
4430 } else {
4431 last->next = cur;
4432 last = cur;
4433 }
4434 }
4435 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004436 }
4437
Daniel Veillard4c004142003-10-07 11:33:24 +00004438 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004439}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004440
4441/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004442 * xmlRelaxNGParseInclude:
4443 * @ctxt: a Relax-NG parser context
4444 * @node: the include node
4445 *
4446 * Integrate the content of an include node in the current grammar
4447 *
4448 * Returns 0 in case of success or -1 in case of error
4449 */
4450static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004451xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4452{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004453 xmlRelaxNGIncludePtr incl;
4454 xmlNodePtr root;
4455 int ret = 0, tmp;
4456
4457 incl = node->_private;
4458 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004459 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4460 "Include node has no data\n", NULL, NULL);
4461 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004462 }
4463 root = xmlDocGetRootElement(incl->doc);
4464 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004465 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4466 NULL, NULL);
4467 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004468 }
4469 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004470 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4471 "Include document root is not a grammar\n", NULL, NULL);
4472 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004473 }
4474
4475 /*
4476 * Merge the definition from both the include and the internal list
4477 */
4478 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004479 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4480 if (tmp != 0)
4481 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004482 }
4483 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004484 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4485 if (tmp != 0)
4486 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004487 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004488 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004489}
4490
4491/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004492 * xmlRelaxNGParseDefine:
4493 * @ctxt: a Relax-NG parser context
4494 * @node: the define node
4495 *
4496 * parse the content of a RelaxNG define element node.
4497 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004498 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004499 */
4500static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004501xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4502{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004503 xmlChar *name;
4504 int ret = 0, tmp;
4505 xmlRelaxNGDefinePtr def;
4506 const xmlChar *olddefine;
4507
4508 name = xmlGetProp(node, BAD_CAST "name");
4509 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004510 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4511 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004512 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004513 xmlRelaxNGNormExtSpace(name);
4514 if (xmlValidateNCName(name, 0)) {
4515 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4516 "define name '%s' is not an NCName\n", name, NULL);
4517 }
4518 def = xmlRelaxNGNewDefine(ctxt, node);
4519 if (def == NULL) {
4520 xmlFree(name);
4521 return (-1);
4522 }
4523 def->type = XML_RELAXNG_DEF;
4524 def->name = name;
4525 if (node->children == NULL) {
4526 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4527 "define has no children\n", NULL, NULL);
4528 } else {
4529 olddefine = ctxt->define;
4530 ctxt->define = name;
4531 def->content =
4532 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4533 ctxt->define = olddefine;
4534 }
4535 if (ctxt->grammar->defs == NULL)
4536 ctxt->grammar->defs = xmlHashCreate(10);
4537 if (ctxt->grammar->defs == NULL) {
4538 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4539 "Could not create definition hash\n", NULL, NULL);
4540 ret = -1;
4541 } else {
4542 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4543 if (tmp < 0) {
4544 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004545
Daniel Veillard4c004142003-10-07 11:33:24 +00004546 prev = xmlHashLookup(ctxt->grammar->defs, name);
4547 if (prev == NULL) {
4548 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4549 "Internal error on define aggregation of %s\n",
4550 name, NULL);
4551 ret = -1;
4552 } else {
4553 while (prev->nextHash != NULL)
4554 prev = prev->nextHash;
4555 prev->nextHash = def;
4556 }
4557 }
4558 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004559 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004560 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004561}
4562
4563/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004564 * xmlRelaxNGProcessExternalRef:
4565 * @ctxt: the parser context
4566 * @node: the externlRef node
4567 *
4568 * Process and compile an externlRef node
4569 *
4570 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4571 */
4572static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004573xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4574{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004575 xmlRelaxNGDocumentPtr docu;
4576 xmlNodePtr root, tmp;
4577 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004578 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004579 xmlRelaxNGDefinePtr def;
4580
4581 docu = node->_private;
4582 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004583 def = xmlRelaxNGNewDefine(ctxt, node);
4584 if (def == NULL)
4585 return (NULL);
4586 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004587
Daniel Veillard4c004142003-10-07 11:33:24 +00004588 if (docu->content == NULL) {
4589 /*
4590 * Then do the parsing for good
4591 */
4592 root = xmlDocGetRootElement(docu->doc);
4593 if (root == NULL) {
4594 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4595 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4596 NULL);
4597 return (NULL);
4598 }
4599 /*
4600 * ns transmission rules
4601 */
4602 ns = xmlGetProp(root, BAD_CAST "ns");
4603 if (ns == NULL) {
4604 tmp = node;
4605 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4606 ns = xmlGetProp(tmp, BAD_CAST "ns");
4607 if (ns != NULL) {
4608 break;
4609 }
4610 tmp = tmp->parent;
4611 }
4612 if (ns != NULL) {
4613 xmlSetProp(root, BAD_CAST "ns", ns);
4614 newNs = 1;
4615 xmlFree(ns);
4616 }
4617 } else {
4618 xmlFree(ns);
4619 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004620
Daniel Veillard4c004142003-10-07 11:33:24 +00004621 /*
4622 * Parsing to get a precompiled schemas.
4623 */
4624 oldflags = ctxt->flags;
4625 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4626 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4627 ctxt->flags = oldflags;
4628 if ((docu->schema != NULL) &&
4629 (docu->schema->topgrammar != NULL)) {
4630 docu->content = docu->schema->topgrammar->start;
4631 }
4632
4633 /*
4634 * the externalRef may be reused in a different ns context
4635 */
4636 if (newNs == 1) {
4637 xmlUnsetProp(root, BAD_CAST "ns");
4638 }
4639 }
4640 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004641 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004642 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004643 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004644 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004645}
4646
4647/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004648 * xmlRelaxNGParsePattern:
4649 * @ctxt: a Relax-NG parser context
4650 * @node: the pattern node.
4651 *
4652 * parse the content of a RelaxNG pattern node.
4653 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004654 * Returns the definition pointer or NULL in case of error or if no
4655 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004656 */
4657static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004658xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4659{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004660 xmlRelaxNGDefinePtr def = NULL;
4661
Daniel Veillardd2298792003-02-14 16:54:11 +00004662 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004663 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004664 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004665 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004666 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004667 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004668 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004669 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004670 def = xmlRelaxNGNewDefine(ctxt, node);
4671 if (def == NULL)
4672 return (NULL);
4673 def->type = XML_RELAXNG_EMPTY;
4674 if (node->children != NULL) {
4675 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4676 "empty: had a child node\n", NULL, NULL);
4677 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004678 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004679 def = xmlRelaxNGNewDefine(ctxt, node);
4680 if (def == NULL)
4681 return (NULL);
4682 def->type = XML_RELAXNG_TEXT;
4683 if (node->children != NULL) {
4684 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4685 "text: had a child node\n", NULL, NULL);
4686 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004687 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004688 def = xmlRelaxNGNewDefine(ctxt, node);
4689 if (def == NULL)
4690 return (NULL);
4691 def->type = XML_RELAXNG_ZEROORMORE;
4692 if (node->children == NULL) {
4693 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4694 "Element %s is empty\n", node->name, NULL);
4695 } else {
4696 def->content =
4697 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4698 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004699 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004700 def = xmlRelaxNGNewDefine(ctxt, node);
4701 if (def == NULL)
4702 return (NULL);
4703 def->type = XML_RELAXNG_ONEORMORE;
4704 if (node->children == NULL) {
4705 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4706 "Element %s is empty\n", node->name, NULL);
4707 } else {
4708 def->content =
4709 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4710 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004711 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004712 def = xmlRelaxNGNewDefine(ctxt, node);
4713 if (def == NULL)
4714 return (NULL);
4715 def->type = XML_RELAXNG_OPTIONAL;
4716 if (node->children == NULL) {
4717 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4718 "Element %s is empty\n", node->name, NULL);
4719 } else {
4720 def->content =
4721 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4722 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004723 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004724 def = xmlRelaxNGNewDefine(ctxt, node);
4725 if (def == NULL)
4726 return (NULL);
4727 def->type = XML_RELAXNG_CHOICE;
4728 if (node->children == NULL) {
4729 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4730 "Element %s is empty\n", node->name, NULL);
4731 } else {
4732 def->content =
4733 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4734 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004735 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004736 def = xmlRelaxNGNewDefine(ctxt, node);
4737 if (def == NULL)
4738 return (NULL);
4739 def->type = XML_RELAXNG_GROUP;
4740 if (node->children == NULL) {
4741 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4742 "Element %s is empty\n", node->name, NULL);
4743 } else {
4744 def->content =
4745 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4746 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004747 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004748 def = xmlRelaxNGNewDefine(ctxt, node);
4749 if (def == NULL)
4750 return (NULL);
4751 def->type = XML_RELAXNG_REF;
4752 def->name = xmlGetProp(node, BAD_CAST "name");
4753 if (def->name == NULL) {
4754 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4755 NULL, NULL);
4756 } else {
4757 xmlRelaxNGNormExtSpace(def->name);
4758 if (xmlValidateNCName(def->name, 0)) {
4759 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4760 "ref name '%s' is not an NCName\n", def->name,
4761 NULL);
4762 }
4763 }
4764 if (node->children != NULL) {
4765 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4766 NULL, NULL);
4767 }
4768 if (ctxt->grammar->refs == NULL)
4769 ctxt->grammar->refs = xmlHashCreate(10);
4770 if (ctxt->grammar->refs == NULL) {
4771 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4772 "Could not create references hash\n", NULL, NULL);
4773 def = NULL;
4774 } else {
4775 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004776
Daniel Veillard4c004142003-10-07 11:33:24 +00004777 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4778 if (tmp < 0) {
4779 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004780
Daniel Veillard4c004142003-10-07 11:33:24 +00004781 prev = (xmlRelaxNGDefinePtr)
4782 xmlHashLookup(ctxt->grammar->refs, def->name);
4783 if (prev == NULL) {
4784 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004785 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4786 "Error refs definitions '%s'\n",
4787 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004788 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004789 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4790 "Error refs definitions\n",
4791 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004792 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004793 def = NULL;
4794 } else {
4795 def->nextHash = prev->nextHash;
4796 prev->nextHash = def;
4797 }
4798 }
4799 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004800 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004801 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004802 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004803 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004804 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004805 def = xmlRelaxNGNewDefine(ctxt, node);
4806 if (def == NULL)
4807 return (NULL);
4808 def->type = XML_RELAXNG_LIST;
4809 if (node->children == NULL) {
4810 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4811 "Element %s is empty\n", node->name, NULL);
4812 } else {
4813 def->content =
4814 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4815 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004816 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004817 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004818 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004819 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004820 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004821 def = xmlRelaxNGNewDefine(ctxt, node);
4822 if (def == NULL)
4823 return (NULL);
4824 def->type = XML_RELAXNG_NOT_ALLOWED;
4825 if (node->children != NULL) {
4826 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4827 "xmlRelaxNGParse: notAllowed element is not empty\n",
4828 NULL, NULL);
4829 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004830 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004831 xmlRelaxNGGrammarPtr grammar, old;
4832 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004833
Daniel Veillardc482e262003-02-26 14:48:48 +00004834#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004835 xmlGenericError(xmlGenericErrorContext,
4836 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004837#endif
4838
Daniel Veillard4c004142003-10-07 11:33:24 +00004839 oldparent = ctxt->parentgrammar;
4840 old = ctxt->grammar;
4841 ctxt->parentgrammar = old;
4842 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4843 if (old != NULL) {
4844 ctxt->grammar = old;
4845 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004846#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004847 if (grammar != NULL) {
4848 grammar->next = old->next;
4849 old->next = grammar;
4850 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004851#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004852 }
4853 if (grammar != NULL)
4854 def = grammar->start;
4855 else
4856 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004857 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004858 if (ctxt->parentgrammar == NULL) {
4859 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4860 "Use of parentRef without a parent grammar\n", NULL,
4861 NULL);
4862 return (NULL);
4863 }
4864 def = xmlRelaxNGNewDefine(ctxt, node);
4865 if (def == NULL)
4866 return (NULL);
4867 def->type = XML_RELAXNG_PARENTREF;
4868 def->name = xmlGetProp(node, BAD_CAST "name");
4869 if (def->name == NULL) {
4870 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4871 "parentRef has no name\n", NULL, NULL);
4872 } else {
4873 xmlRelaxNGNormExtSpace(def->name);
4874 if (xmlValidateNCName(def->name, 0)) {
4875 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4876 "parentRef name '%s' is not an NCName\n",
4877 def->name, NULL);
4878 }
4879 }
4880 if (node->children != NULL) {
4881 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4882 "parentRef is not empty\n", NULL, NULL);
4883 }
4884 if (ctxt->parentgrammar->refs == NULL)
4885 ctxt->parentgrammar->refs = xmlHashCreate(10);
4886 if (ctxt->parentgrammar->refs == NULL) {
4887 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4888 "Could not create references hash\n", NULL, NULL);
4889 def = NULL;
4890 } else if (def->name != NULL) {
4891 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00004892
Daniel Veillard4c004142003-10-07 11:33:24 +00004893 tmp =
4894 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4895 if (tmp < 0) {
4896 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00004897
Daniel Veillard4c004142003-10-07 11:33:24 +00004898 prev = (xmlRelaxNGDefinePtr)
4899 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4900 if (prev == NULL) {
4901 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4902 "Internal error parentRef definitions '%s'\n",
4903 def->name, NULL);
4904 def = NULL;
4905 } else {
4906 def->nextHash = prev->nextHash;
4907 prev->nextHash = def;
4908 }
4909 }
4910 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004911 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004912 if (node->children == NULL) {
4913 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4914 NULL, NULL);
4915 def = NULL;
4916 } else {
4917 def = xmlRelaxNGParseInterleave(ctxt, node);
4918 if (def != NULL) {
4919 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004920
Daniel Veillard4c004142003-10-07 11:33:24 +00004921 if ((def->content != NULL) && (def->content->next != NULL)) {
4922 tmp = xmlRelaxNGNewDefine(ctxt, node);
4923 if (tmp != NULL) {
4924 tmp->type = XML_RELAXNG_GROUP;
4925 tmp->content = def->content;
4926 def->content = tmp;
4927 }
4928 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004929
Daniel Veillard4c004142003-10-07 11:33:24 +00004930 tmp = xmlRelaxNGNewDefine(ctxt, node);
4931 if (tmp == NULL)
4932 return (def);
4933 tmp->type = XML_RELAXNG_TEXT;
4934 tmp->next = def->content;
4935 def->content = tmp;
4936 }
4937 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004938 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004939 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4940 "Unexpected node %s is not a pattern\n", node->name,
4941 NULL);
4942 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004943 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004944 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004945}
4946
4947/**
4948 * xmlRelaxNGParseAttribute:
4949 * @ctxt: a Relax-NG parser context
4950 * @node: the element node
4951 *
4952 * parse the content of a RelaxNG attribute node.
4953 *
4954 * Returns the definition pointer or NULL in case of error.
4955 */
4956static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004957xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4958{
Daniel Veillardd2298792003-02-14 16:54:11 +00004959 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004960 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004961 int old_flags;
4962
Daniel Veillardfd573f12003-03-16 17:52:32 +00004963 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004964 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004965 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004966 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004967 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004968 child = node->children;
4969 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004970 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
4971 "xmlRelaxNGParseattribute: attribute has no children\n",
4972 NULL, NULL);
4973 return (ret);
4974 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004975 old_flags = ctxt->flags;
4976 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004977 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4978 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004979 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004980
Daniel Veillardd2298792003-02-14 16:54:11 +00004981 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004982 cur = xmlRelaxNGParsePattern(ctxt, child);
4983 if (cur != NULL) {
4984 switch (cur->type) {
4985 case XML_RELAXNG_EMPTY:
4986 case XML_RELAXNG_NOT_ALLOWED:
4987 case XML_RELAXNG_TEXT:
4988 case XML_RELAXNG_ELEMENT:
4989 case XML_RELAXNG_DATATYPE:
4990 case XML_RELAXNG_VALUE:
4991 case XML_RELAXNG_LIST:
4992 case XML_RELAXNG_REF:
4993 case XML_RELAXNG_PARENTREF:
4994 case XML_RELAXNG_EXTERNALREF:
4995 case XML_RELAXNG_DEF:
4996 case XML_RELAXNG_ONEORMORE:
4997 case XML_RELAXNG_ZEROORMORE:
4998 case XML_RELAXNG_OPTIONAL:
4999 case XML_RELAXNG_CHOICE:
5000 case XML_RELAXNG_GROUP:
5001 case XML_RELAXNG_INTERLEAVE:
5002 case XML_RELAXNG_ATTRIBUTE:
5003 ret->content = cur;
5004 cur->parent = ret;
5005 break;
5006 case XML_RELAXNG_START:
5007 case XML_RELAXNG_PARAM:
5008 case XML_RELAXNG_EXCEPT:
5009 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5010 "attribute has invalid content\n", NULL,
5011 NULL);
5012 break;
5013 case XML_RELAXNG_NOOP:
5014 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5015 "RNG Internal error, noop found in attribute\n",
5016 NULL, NULL);
5017 break;
5018 }
5019 }
5020 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005021 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005022 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005023 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5024 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005025 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005026 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005027 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005028}
5029
5030/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005031 * xmlRelaxNGParseExceptNameClass:
5032 * @ctxt: a Relax-NG parser context
5033 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005034 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005035 *
5036 * parse the content of a RelaxNG nameClass node.
5037 *
5038 * Returns the definition pointer or NULL in case of error.
5039 */
5040static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005041xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005042 xmlNodePtr node, int attr)
5043{
Daniel Veillard144fae12003-02-03 13:17:57 +00005044 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5045 xmlNodePtr child;
5046
Daniel Veillardd2298792003-02-14 16:54:11 +00005047 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005048 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5049 "Expecting an except node\n", NULL, NULL);
5050 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005051 }
5052 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005053 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5054 "exceptNameClass allows only a single except node\n",
5055 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005056 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005057 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005058 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5059 NULL, NULL);
5060 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005061 }
5062
Daniel Veillardfd573f12003-03-16 17:52:32 +00005063 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005064 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005065 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005066 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005067 child = node->children;
5068 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005069 cur = xmlRelaxNGNewDefine(ctxt, child);
5070 if (cur == NULL)
5071 break;
5072 if (attr)
5073 cur->type = XML_RELAXNG_ATTRIBUTE;
5074 else
5075 cur->type = XML_RELAXNG_ELEMENT;
5076
Daniel Veillard419a7682003-02-03 23:22:49 +00005077 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005078 if (last == NULL) {
5079 ret->content = cur;
5080 } else {
5081 last->next = cur;
5082 }
5083 last = cur;
5084 }
5085 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005086 }
5087
Daniel Veillard4c004142003-10-07 11:33:24 +00005088 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005089}
5090
5091/**
5092 * xmlRelaxNGParseNameClass:
5093 * @ctxt: a Relax-NG parser context
5094 * @node: the nameClass node
5095 * @def: the current definition
5096 *
5097 * parse the content of a RelaxNG nameClass node.
5098 *
5099 * Returns the definition pointer or NULL in case of error.
5100 */
5101static xmlRelaxNGDefinePtr
5102xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005103 xmlRelaxNGDefinePtr def)
5104{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005105 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005106 xmlChar *val;
5107
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005108 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005109 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005110 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005111 if ((def->type != XML_RELAXNG_ELEMENT) &&
5112 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5113 ret = xmlRelaxNGNewDefine(ctxt, node);
5114 if (ret == NULL)
5115 return (NULL);
5116 ret->parent = def;
5117 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5118 ret->type = XML_RELAXNG_ATTRIBUTE;
5119 else
5120 ret->type = XML_RELAXNG_ELEMENT;
5121 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005122 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005123 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005124 val = xmlNodeGetContent(node);
5125 xmlRelaxNGNormExtSpace(val);
5126 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005127 if (node->parent != NULL)
5128 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5129 "Element %s name '%s' is not an NCName\n",
5130 node->parent->name, val);
5131 else
5132 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5133 "name '%s' is not an NCName\n",
5134 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005135 }
5136 ret->name = val;
5137 val = xmlGetProp(node, BAD_CAST "ns");
5138 ret->ns = val;
5139 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5140 (val != NULL) &&
5141 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005142 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005143 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005144 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005145 }
5146 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5147 (val != NULL) &&
5148 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005149 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5150 "Attribute with QName 'xmlns' is not allowed\n",
5151 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005152 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005153 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005154 ret->name = NULL;
5155 ret->ns = NULL;
5156 if (node->children != NULL) {
5157 ret->nameClass =
5158 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5159 (def->type ==
5160 XML_RELAXNG_ATTRIBUTE));
5161 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005162 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005163 ret->name = NULL;
5164 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5165 if (ret->ns == NULL) {
5166 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5167 "nsName has no ns attribute\n", NULL, NULL);
5168 }
5169 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5170 (ret->ns != NULL) &&
5171 (xmlStrEqual
5172 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5173 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5174 "Attribute with namespace '%s' is not allowed\n",
5175 ret->ns, NULL);
5176 }
5177 if (node->children != NULL) {
5178 ret->nameClass =
5179 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5180 (def->type ==
5181 XML_RELAXNG_ATTRIBUTE));
5182 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005183 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005184 xmlNodePtr child;
5185 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005186
Daniel Veillard4c004142003-10-07 11:33:24 +00005187 ret = xmlRelaxNGNewDefine(ctxt, node);
5188 if (ret == NULL)
5189 return (NULL);
5190 ret->parent = def;
5191 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005192
Daniel Veillard4c004142003-10-07 11:33:24 +00005193 if (node->children == NULL) {
5194 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5195 "Element choice is empty\n", NULL, NULL);
5196 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005197
Daniel Veillard4c004142003-10-07 11:33:24 +00005198 child = node->children;
5199 while (child != NULL) {
5200 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5201 if (tmp != NULL) {
5202 if (last == NULL) {
5203 last = ret->nameClass = tmp;
5204 } else {
5205 last->next = tmp;
5206 last = tmp;
5207 }
5208 }
5209 child = child->next;
5210 }
5211 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005212 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005213 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5214 "expecting name, anyName, nsName or choice : got %s\n",
5215 node->name, NULL);
5216 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005217 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005218 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005219 if (def->nameClass == NULL) {
5220 def->nameClass = ret;
5221 } else {
5222 tmp = def->nameClass;
5223 while (tmp->next != NULL) {
5224 tmp = tmp->next;
5225 }
5226 tmp->next = ret;
5227 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005228 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005229 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005230}
5231
5232/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005233 * xmlRelaxNGParseElement:
5234 * @ctxt: a Relax-NG parser context
5235 * @node: the element node
5236 *
5237 * parse the content of a RelaxNG element node.
5238 *
5239 * Returns the definition pointer or NULL in case of error.
5240 */
5241static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005242xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5243{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005244 xmlRelaxNGDefinePtr ret, cur, last;
5245 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005246 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005247
Daniel Veillardfd573f12003-03-16 17:52:32 +00005248 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005249 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005250 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005251 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005252 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005253 child = node->children;
5254 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005255 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5256 "xmlRelaxNGParseElement: element has no children\n",
5257 NULL, NULL);
5258 return (ret);
5259 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005260 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5261 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005262 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005263
Daniel Veillard6eadf632003-01-23 18:29:16 +00005264 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005265 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5266 "xmlRelaxNGParseElement: element has no content\n",
5267 NULL, NULL);
5268 return (ret);
5269 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005270 olddefine = ctxt->define;
5271 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005272 last = NULL;
5273 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005274 cur = xmlRelaxNGParsePattern(ctxt, child);
5275 if (cur != NULL) {
5276 cur->parent = ret;
5277 switch (cur->type) {
5278 case XML_RELAXNG_EMPTY:
5279 case XML_RELAXNG_NOT_ALLOWED:
5280 case XML_RELAXNG_TEXT:
5281 case XML_RELAXNG_ELEMENT:
5282 case XML_RELAXNG_DATATYPE:
5283 case XML_RELAXNG_VALUE:
5284 case XML_RELAXNG_LIST:
5285 case XML_RELAXNG_REF:
5286 case XML_RELAXNG_PARENTREF:
5287 case XML_RELAXNG_EXTERNALREF:
5288 case XML_RELAXNG_DEF:
5289 case XML_RELAXNG_ZEROORMORE:
5290 case XML_RELAXNG_ONEORMORE:
5291 case XML_RELAXNG_OPTIONAL:
5292 case XML_RELAXNG_CHOICE:
5293 case XML_RELAXNG_GROUP:
5294 case XML_RELAXNG_INTERLEAVE:
5295 if (last == NULL) {
5296 ret->content = last = cur;
5297 } else {
5298 if ((last->type == XML_RELAXNG_ELEMENT) &&
5299 (ret->content == last)) {
5300 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5301 if (ret->content != NULL) {
5302 ret->content->type = XML_RELAXNG_GROUP;
5303 ret->content->content = last;
5304 } else {
5305 ret->content = last;
5306 }
5307 }
5308 last->next = cur;
5309 last = cur;
5310 }
5311 break;
5312 case XML_RELAXNG_ATTRIBUTE:
5313 cur->next = ret->attrs;
5314 ret->attrs = cur;
5315 break;
5316 case XML_RELAXNG_START:
5317 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5318 "RNG Internal error, start found in element\n",
5319 NULL, NULL);
5320 break;
5321 case XML_RELAXNG_PARAM:
5322 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5323 "RNG Internal error, param found in element\n",
5324 NULL, NULL);
5325 break;
5326 case XML_RELAXNG_EXCEPT:
5327 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5328 "RNG Internal error, except found in element\n",
5329 NULL, NULL);
5330 break;
5331 case XML_RELAXNG_NOOP:
5332 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5333 "RNG Internal error, noop found in element\n",
5334 NULL, NULL);
5335 break;
5336 }
5337 }
5338 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005339 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005340 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005341 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005342}
5343
5344/**
5345 * xmlRelaxNGParsePatterns:
5346 * @ctxt: a Relax-NG parser context
5347 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005348 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005349 *
5350 * parse the content of a RelaxNG start node.
5351 *
5352 * Returns the definition pointer or NULL in case of error.
5353 */
5354static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005355xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005356 int group)
5357{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005358 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005359
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005360 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005361 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005362 if (IS_RELAXNG(nodes, "element")) {
5363 cur = xmlRelaxNGParseElement(ctxt, nodes);
5364 if (def == NULL) {
5365 def = last = cur;
5366 } else {
5367 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5368 (def == last)) {
5369 def = xmlRelaxNGNewDefine(ctxt, nodes);
5370 def->type = XML_RELAXNG_GROUP;
5371 def->content = last;
5372 }
5373 last->next = cur;
5374 last = cur;
5375 }
5376 cur->parent = parent;
5377 } else {
5378 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5379 if (cur != NULL) {
5380 if (def == NULL) {
5381 def = last = cur;
5382 } else {
5383 last->next = cur;
5384 last = cur;
5385 }
5386 }
5387 }
5388 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005389 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005390 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005391}
5392
5393/**
5394 * xmlRelaxNGParseStart:
5395 * @ctxt: a Relax-NG parser context
5396 * @nodes: start children nodes
5397 *
5398 * parse the content of a RelaxNG start node.
5399 *
5400 * Returns 0 in case of success, -1 in case of error
5401 */
5402static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005403xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5404{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005405 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005406 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005407
Daniel Veillardd2298792003-02-14 16:54:11 +00005408 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005409 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5410 NULL, NULL);
5411 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005412 }
5413 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005414 def = xmlRelaxNGNewDefine(ctxt, nodes);
5415 if (def == NULL)
5416 return (-1);
5417 def->type = XML_RELAXNG_EMPTY;
5418 if (nodes->children != NULL) {
5419 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5420 "element empty is not empty\n", NULL, NULL);
5421 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005422 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005423 def = xmlRelaxNGNewDefine(ctxt, nodes);
5424 if (def == NULL)
5425 return (-1);
5426 def->type = XML_RELAXNG_NOT_ALLOWED;
5427 if (nodes->children != NULL) {
5428 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5429 "element notAllowed is not empty\n", NULL, NULL);
5430 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005431 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005432 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005433 }
5434 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005435 last = ctxt->grammar->start;
5436 while (last->next != NULL)
5437 last = last->next;
5438 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005439 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005440 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005441 }
5442 nodes = nodes->next;
5443 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005444 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5445 "start more than one children\n", NULL, NULL);
5446 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005447 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005448 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005449}
5450
5451/**
5452 * xmlRelaxNGParseGrammarContent:
5453 * @ctxt: a Relax-NG parser context
5454 * @nodes: grammar children nodes
5455 *
5456 * parse the content of a RelaxNG grammar node.
5457 *
5458 * Returns 0 in case of success, -1 in case of error
5459 */
5460static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005461xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5462 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005463{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005464 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005465
5466 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005467 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5468 "grammar has no children\n", NULL, NULL);
5469 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005470 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005471 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005472 if (IS_RELAXNG(nodes, "start")) {
5473 if (nodes->children == NULL) {
5474 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5475 "start has no children\n", NULL, NULL);
5476 } else {
5477 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5478 if (tmp != 0)
5479 ret = -1;
5480 }
5481 } else if (IS_RELAXNG(nodes, "define")) {
5482 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5483 if (tmp != 0)
5484 ret = -1;
5485 } else if (IS_RELAXNG(nodes, "include")) {
5486 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5487 if (tmp != 0)
5488 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005489 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005490 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5491 "grammar has unexpected child %s\n", nodes->name,
5492 NULL);
5493 ret = -1;
5494 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005495 nodes = nodes->next;
5496 }
5497 return (ret);
5498}
5499
5500/**
5501 * xmlRelaxNGCheckReference:
5502 * @ref: the ref
5503 * @ctxt: a Relax-NG parser context
5504 * @name: the name associated to the defines
5505 *
5506 * Applies the 4.17. combine attribute rule for all the define
5507 * element of a given grammar using the same name.
5508 */
5509static void
5510xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005511 xmlRelaxNGParserCtxtPtr ctxt,
5512 const xmlChar * name)
5513{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005514 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005515 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005516
5517 grammar = ctxt->grammar;
5518 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005519 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5520 "Internal error: no grammar in CheckReference %s\n",
5521 name, NULL);
5522 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005523 }
5524 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005525 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5526 "Internal error: reference has content in CheckReference %s\n",
5527 name, NULL);
5528 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005529 }
5530 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005531 def = xmlHashLookup(grammar->defs, name);
5532 if (def != NULL) {
5533 cur = ref;
5534 while (cur != NULL) {
5535 cur->content = def;
5536 cur = cur->nextHash;
5537 }
5538 } else {
5539 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5540 "Reference %s has no matching definition\n", name,
5541 NULL);
5542 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005543 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005544 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5545 "Reference %s has no matching definition\n", name,
5546 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005547 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005548}
5549
5550/**
5551 * xmlRelaxNGCheckCombine:
5552 * @define: the define(s) list
5553 * @ctxt: a Relax-NG parser context
5554 * @name: the name associated to the defines
5555 *
5556 * Applies the 4.17. combine attribute rule for all the define
5557 * element of a given grammar using the same name.
5558 */
5559static void
5560xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005561 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5562{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005563 xmlChar *combine;
5564 int choiceOrInterleave = -1;
5565 int missing = 0;
5566 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5567
5568 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005569 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005570 cur = define;
5571 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005572 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5573 if (combine != NULL) {
5574 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5575 if (choiceOrInterleave == -1)
5576 choiceOrInterleave = 1;
5577 else if (choiceOrInterleave == 0) {
5578 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5579 "Defines for %s use both 'choice' and 'interleave'\n",
5580 name, NULL);
5581 }
5582 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5583 if (choiceOrInterleave == -1)
5584 choiceOrInterleave = 0;
5585 else if (choiceOrInterleave == 1) {
5586 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5587 "Defines for %s use both 'choice' and 'interleave'\n",
5588 name, NULL);
5589 }
5590 } else {
5591 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5592 "Defines for %s use unknown combine value '%s''\n",
5593 name, combine);
5594 }
5595 xmlFree(combine);
5596 } else {
5597 if (missing == 0)
5598 missing = 1;
5599 else {
5600 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5601 "Some defines for %s needs the combine attribute\n",
5602 name, NULL);
5603 }
5604 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005605
Daniel Veillard4c004142003-10-07 11:33:24 +00005606 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005607 }
5608#ifdef DEBUG
5609 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005610 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5611 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005612#endif
5613 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005614 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005615 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005616 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005617 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005618 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005619 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005620 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005621 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005622 tmp = define;
5623 last = NULL;
5624 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005625 if (tmp->content != NULL) {
5626 if (tmp->content->next != NULL) {
5627 /*
5628 * we need first to create a wrapper.
5629 */
5630 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5631 if (tmp2 == NULL)
5632 break;
5633 tmp2->type = XML_RELAXNG_GROUP;
5634 tmp2->content = tmp->content;
5635 } else {
5636 tmp2 = tmp->content;
5637 }
5638 if (last == NULL) {
5639 cur->content = tmp2;
5640 } else {
5641 last->next = tmp2;
5642 }
5643 last = tmp2;
5644 }
5645 tmp->content = cur;
5646 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005647 }
5648 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005649 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005650 if (ctxt->interleaves == NULL)
5651 ctxt->interleaves = xmlHashCreate(10);
5652 if (ctxt->interleaves == NULL) {
5653 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5654 "Failed to create interleaves hash table\n", NULL,
5655 NULL);
5656 } else {
5657 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005658
Daniel Veillard4c004142003-10-07 11:33:24 +00005659 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5660 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5661 0) {
5662 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5663 "Failed to add %s to hash table\n",
5664 (const xmlChar *) tmpname, NULL);
5665 }
5666 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005667 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005668}
5669
5670/**
5671 * xmlRelaxNGCombineStart:
5672 * @ctxt: a Relax-NG parser context
5673 * @grammar: the grammar
5674 *
5675 * Applies the 4.17. combine rule for all the start
5676 * element of a given grammar.
5677 */
5678static void
5679xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005680 xmlRelaxNGGrammarPtr grammar)
5681{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005682 xmlRelaxNGDefinePtr starts;
5683 xmlChar *combine;
5684 int choiceOrInterleave = -1;
5685 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005686 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005687
Daniel Veillard2df2de22003-02-17 23:34:33 +00005688 starts = grammar->start;
5689 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005690 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005691 cur = starts;
5692 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005693 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5694 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5695 combine = NULL;
5696 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5697 "Internal error: start element not found\n", NULL,
5698 NULL);
5699 } else {
5700 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5701 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005702
Daniel Veillard4c004142003-10-07 11:33:24 +00005703 if (combine != NULL) {
5704 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5705 if (choiceOrInterleave == -1)
5706 choiceOrInterleave = 1;
5707 else if (choiceOrInterleave == 0) {
5708 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5709 "<start> use both 'choice' and 'interleave'\n",
5710 NULL, NULL);
5711 }
5712 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5713 if (choiceOrInterleave == -1)
5714 choiceOrInterleave = 0;
5715 else if (choiceOrInterleave == 1) {
5716 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5717 "<start> use both 'choice' and 'interleave'\n",
5718 NULL, NULL);
5719 }
5720 } else {
5721 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5722 "<start> uses unknown combine value '%s''\n",
5723 combine, NULL);
5724 }
5725 xmlFree(combine);
5726 } else {
5727 if (missing == 0)
5728 missing = 1;
5729 else {
5730 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5731 "Some <start> element miss the combine attribute\n",
5732 NULL, NULL);
5733 }
5734 }
5735
5736 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005737 }
5738#ifdef DEBUG
5739 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005740 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5741 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005742#endif
5743 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005744 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005745 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005746 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005747 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005748 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005749 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005750 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005751 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005752 cur->content = grammar->start;
5753 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005754 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005755 if (ctxt->interleaves == NULL)
5756 ctxt->interleaves = xmlHashCreate(10);
5757 if (ctxt->interleaves == NULL) {
5758 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5759 "Failed to create interleaves hash table\n", NULL,
5760 NULL);
5761 } else {
5762 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005763
Daniel Veillard4c004142003-10-07 11:33:24 +00005764 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5765 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5766 0) {
5767 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5768 "Failed to add %s to hash table\n",
5769 (const xmlChar *) tmpname, NULL);
5770 }
5771 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005772 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005773}
5774
5775/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005776 * xmlRelaxNGCheckCycles:
5777 * @ctxt: a Relax-NG parser context
5778 * @nodes: grammar children nodes
5779 * @depth: the counter
5780 *
5781 * Check for cycles.
5782 *
5783 * Returns 0 if check passed, and -1 in case of error
5784 */
5785static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005786xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5787 xmlRelaxNGDefinePtr cur, int depth)
5788{
Daniel Veillardd4310742003-02-18 21:12:46 +00005789 int ret = 0;
5790
5791 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005792 if ((cur->type == XML_RELAXNG_REF) ||
5793 (cur->type == XML_RELAXNG_PARENTREF)) {
5794 if (cur->depth == -1) {
5795 cur->depth = depth;
5796 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5797 cur->depth = -2;
5798 } else if (depth == cur->depth) {
5799 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5800 "Detected a cycle in %s references\n",
5801 cur->name, NULL);
5802 return (-1);
5803 }
5804 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5805 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5806 } else {
5807 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5808 }
5809 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005810 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005811 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005812}
5813
5814/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005815 * xmlRelaxNGTryUnlink:
5816 * @ctxt: a Relax-NG parser context
5817 * @cur: the definition to unlink
5818 * @parent: the parent definition
5819 * @prev: the previous sibling definition
5820 *
5821 * Try to unlink a definition. If not possble make it a NOOP
5822 *
5823 * Returns the new prev definition
5824 */
5825static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005826xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5827 xmlRelaxNGDefinePtr cur,
5828 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5829{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005830 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005831 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005832 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005833 if (parent != NULL) {
5834 if (parent->content == cur)
5835 parent->content = cur->next;
5836 else if (parent->attrs == cur)
5837 parent->attrs = cur->next;
5838 else if (parent->nameClass == cur)
5839 parent->nameClass = cur->next;
5840 } else {
5841 cur->type = XML_RELAXNG_NOOP;
5842 prev = cur;
5843 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005844 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005845 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005846}
5847
5848/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005849 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005850 * @ctxt: a Relax-NG parser context
5851 * @nodes: grammar children nodes
5852 *
5853 * Check for simplification of empty and notAllowed
5854 */
5855static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005856xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5857 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5858{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005859 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005860
Daniel Veillardfd573f12003-03-16 17:52:32 +00005861 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005862 if ((cur->type == XML_RELAXNG_REF) ||
5863 (cur->type == XML_RELAXNG_PARENTREF)) {
5864 if (cur->depth != -3) {
5865 cur->depth = -3;
5866 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5867 }
5868 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5869 cur->parent = parent;
5870 if ((parent != NULL) &&
5871 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5872 (parent->type == XML_RELAXNG_LIST) ||
5873 (parent->type == XML_RELAXNG_GROUP) ||
5874 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5875 (parent->type == XML_RELAXNG_ONEORMORE) ||
5876 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5877 parent->type = XML_RELAXNG_NOT_ALLOWED;
5878 break;
5879 }
5880 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5881 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5882 } else
5883 prev = cur;
5884 } else if (cur->type == XML_RELAXNG_EMPTY) {
5885 cur->parent = parent;
5886 if ((parent != NULL) &&
5887 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5888 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5889 parent->type = XML_RELAXNG_EMPTY;
5890 break;
5891 }
5892 if ((parent != NULL) &&
5893 ((parent->type == XML_RELAXNG_GROUP) ||
5894 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5895 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5896 } else
5897 prev = cur;
5898 } else {
5899 cur->parent = parent;
5900 if (cur->content != NULL)
5901 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5902 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5903 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5904 if (cur->nameClass != NULL)
5905 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5906 /*
5907 * On Elements, try to move attribute only generating rules on
5908 * the attrs rules.
5909 */
5910 if (cur->type == XML_RELAXNG_ELEMENT) {
5911 int attronly;
5912 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00005913
Daniel Veillard4c004142003-10-07 11:33:24 +00005914 while (cur->content != NULL) {
5915 attronly =
5916 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5917 if (attronly == 1) {
5918 /*
5919 * migrate cur->content to attrs
5920 */
5921 tmp = cur->content;
5922 cur->content = tmp->next;
5923 tmp->next = cur->attrs;
5924 cur->attrs = tmp;
5925 } else {
5926 /*
5927 * cur->content can generate elements or text
5928 */
5929 break;
5930 }
5931 }
5932 pre = cur->content;
5933 while ((pre != NULL) && (pre->next != NULL)) {
5934 tmp = pre->next;
5935 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5936 if (attronly == 1) {
5937 /*
5938 * migrate tmp to attrs
5939 */
5940 pre->next = tmp->next;
5941 tmp->next = cur->attrs;
5942 cur->attrs = tmp;
5943 } else {
5944 pre = tmp;
5945 }
5946 }
5947 }
5948 /*
5949 * This may result in a simplification
5950 */
5951 if ((cur->type == XML_RELAXNG_GROUP) ||
5952 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5953 if (cur->content == NULL)
5954 cur->type = XML_RELAXNG_EMPTY;
5955 else if (cur->content->next == NULL) {
5956 if ((parent == NULL) && (prev == NULL)) {
5957 cur->type = XML_RELAXNG_NOOP;
5958 } else if (prev == NULL) {
5959 parent->content = cur->content;
5960 cur->content->next = cur->next;
5961 cur = cur->content;
5962 } else {
5963 cur->content->next = cur->next;
5964 prev->next = cur->content;
5965 cur = cur->content;
5966 }
5967 }
5968 }
5969 /*
5970 * the current node may have been transformed back
5971 */
5972 if ((cur->type == XML_RELAXNG_EXCEPT) &&
5973 (cur->content != NULL) &&
5974 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
5975 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5976 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5977 if ((parent != NULL) &&
5978 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5979 (parent->type == XML_RELAXNG_LIST) ||
5980 (parent->type == XML_RELAXNG_GROUP) ||
5981 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5982 (parent->type == XML_RELAXNG_ONEORMORE) ||
5983 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5984 parent->type = XML_RELAXNG_NOT_ALLOWED;
5985 break;
5986 }
5987 if ((parent != NULL) &&
5988 (parent->type == XML_RELAXNG_CHOICE)) {
5989 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5990 } else
5991 prev = cur;
5992 } else if (cur->type == XML_RELAXNG_EMPTY) {
5993 if ((parent != NULL) &&
5994 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5995 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5996 parent->type = XML_RELAXNG_EMPTY;
5997 break;
5998 }
5999 if ((parent != NULL) &&
6000 ((parent->type == XML_RELAXNG_GROUP) ||
6001 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6002 (parent->type == XML_RELAXNG_CHOICE))) {
6003 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6004 } else
6005 prev = cur;
6006 } else {
6007 prev = cur;
6008 }
6009 }
6010 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006011 }
6012}
6013
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006014/**
6015 * xmlRelaxNGGroupContentType:
6016 * @ct1: the first content type
6017 * @ct2: the second content type
6018 *
6019 * Try to group 2 content types
6020 *
6021 * Returns the content type
6022 */
6023static xmlRelaxNGContentType
6024xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006025 xmlRelaxNGContentType ct2)
6026{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006027 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006028 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6029 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006030 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006031 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006032 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006033 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006034 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006035 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6036 return (XML_RELAXNG_CONTENT_COMPLEX);
6037 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006038}
6039
6040/**
6041 * xmlRelaxNGMaxContentType:
6042 * @ct1: the first content type
6043 * @ct2: the second content type
6044 *
6045 * Compute the max content-type
6046 *
6047 * Returns the content type
6048 */
6049static xmlRelaxNGContentType
6050xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006051 xmlRelaxNGContentType ct2)
6052{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006053 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006054 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6055 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006056 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006057 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6058 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006059 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006060 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6061 return (XML_RELAXNG_CONTENT_COMPLEX);
6062 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006063}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006064
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006065/**
6066 * xmlRelaxNGCheckRules:
6067 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006068 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006069 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006070 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006071 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006072 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006073 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006074 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006075 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006076static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006077xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6078 xmlRelaxNGDefinePtr cur, int flags,
6079 xmlRelaxNGType ptype)
6080{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006081 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006082 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006083
Daniel Veillardfd573f12003-03-16 17:52:32 +00006084 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006085 ret = XML_RELAXNG_CONTENT_EMPTY;
6086 if ((cur->type == XML_RELAXNG_REF) ||
6087 (cur->type == XML_RELAXNG_PARENTREF)) {
6088 if (flags & XML_RELAXNG_IN_LIST) {
6089 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6090 "Found forbidden pattern list//ref\n", NULL,
6091 NULL);
6092 }
6093 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6094 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6095 "Found forbidden pattern data/except//ref\n",
6096 NULL, NULL);
6097 }
6098 if (cur->depth > -4) {
6099 cur->depth = -4;
6100 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6101 flags, cur->type);
6102 cur->depth = ret - 15;
6103 } else if (cur->depth == -4) {
6104 ret = XML_RELAXNG_CONTENT_COMPLEX;
6105 } else {
6106 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6107 }
6108 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6109 /*
6110 * The 7.3 Attribute derivation rule for groups is plugged there
6111 */
6112 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6113 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6114 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6115 "Found forbidden pattern data/except//element(ref)\n",
6116 NULL, NULL);
6117 }
6118 if (flags & XML_RELAXNG_IN_LIST) {
6119 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6120 "Found forbidden pattern list//element(ref)\n",
6121 NULL, NULL);
6122 }
6123 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6124 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6125 "Found forbidden pattern attribute//element(ref)\n",
6126 NULL, NULL);
6127 }
6128 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6129 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6130 "Found forbidden pattern attribute//element(ref)\n",
6131 NULL, NULL);
6132 }
6133 /*
6134 * reset since in the simple form elements are only child
6135 * of grammar/define
6136 */
6137 nflags = 0;
6138 ret =
6139 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6140 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6141 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6142 "Element %s attributes have a content type error\n",
6143 cur->name, NULL);
6144 }
6145 ret =
6146 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6147 cur->type);
6148 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6149 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6150 "Element %s has a content type error\n",
6151 cur->name, NULL);
6152 } else {
6153 ret = XML_RELAXNG_CONTENT_COMPLEX;
6154 }
6155 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6156 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6157 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6158 "Found forbidden pattern attribute//attribute\n",
6159 NULL, NULL);
6160 }
6161 if (flags & XML_RELAXNG_IN_LIST) {
6162 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6163 "Found forbidden pattern list//attribute\n",
6164 NULL, NULL);
6165 }
6166 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6167 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6168 "Found forbidden pattern oneOrMore//group//attribute\n",
6169 NULL, NULL);
6170 }
6171 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6172 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6173 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6174 NULL, NULL);
6175 }
6176 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6177 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6178 "Found forbidden pattern data/except//attribute\n",
6179 NULL, NULL);
6180 }
6181 if (flags & XML_RELAXNG_IN_START) {
6182 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6183 "Found forbidden pattern start//attribute\n",
6184 NULL, NULL);
6185 }
6186 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6187 && (cur->name == NULL)) {
6188 if (cur->ns == NULL) {
6189 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6190 "Found anyName attribute without oneOrMore ancestor\n",
6191 NULL, NULL);
6192 } else {
6193 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6194 "Found nsName attribute without oneOrMore ancestor\n",
6195 NULL, NULL);
6196 }
6197 }
6198 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6199 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6200 ret = XML_RELAXNG_CONTENT_EMPTY;
6201 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6202 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6203 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6204 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6205 "Found forbidden pattern data/except//oneOrMore\n",
6206 NULL, NULL);
6207 }
6208 if (flags & XML_RELAXNG_IN_START) {
6209 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6210 "Found forbidden pattern start//oneOrMore\n",
6211 NULL, NULL);
6212 }
6213 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6214 ret =
6215 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6216 cur->type);
6217 ret = xmlRelaxNGGroupContentType(ret, ret);
6218 } else if (cur->type == XML_RELAXNG_LIST) {
6219 if (flags & XML_RELAXNG_IN_LIST) {
6220 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6221 "Found forbidden pattern list//list\n", NULL,
6222 NULL);
6223 }
6224 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6225 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6226 "Found forbidden pattern data/except//list\n",
6227 NULL, NULL);
6228 }
6229 if (flags & XML_RELAXNG_IN_START) {
6230 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6231 "Found forbidden pattern start//list\n", NULL,
6232 NULL);
6233 }
6234 nflags = flags | XML_RELAXNG_IN_LIST;
6235 ret =
6236 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6237 cur->type);
6238 } else if (cur->type == XML_RELAXNG_GROUP) {
6239 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6240 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6241 "Found forbidden pattern data/except//group\n",
6242 NULL, NULL);
6243 }
6244 if (flags & XML_RELAXNG_IN_START) {
6245 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6246 "Found forbidden pattern start//group\n", NULL,
6247 NULL);
6248 }
6249 if (flags & XML_RELAXNG_IN_ONEORMORE)
6250 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6251 else
6252 nflags = flags;
6253 ret =
6254 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6255 cur->type);
6256 /*
6257 * The 7.3 Attribute derivation rule for groups is plugged there
6258 */
6259 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6260 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6261 if (flags & XML_RELAXNG_IN_LIST) {
6262 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6263 "Found forbidden pattern list//interleave\n",
6264 NULL, NULL);
6265 }
6266 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6267 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6268 "Found forbidden pattern data/except//interleave\n",
6269 NULL, NULL);
6270 }
6271 if (flags & XML_RELAXNG_IN_START) {
6272 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6273 "Found forbidden pattern start//interleave\n",
6274 NULL, NULL);
6275 }
6276 if (flags & XML_RELAXNG_IN_ONEORMORE)
6277 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6278 else
6279 nflags = flags;
6280 ret =
6281 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6282 cur->type);
6283 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6284 if ((cur->parent != NULL) &&
6285 (cur->parent->type == XML_RELAXNG_DATATYPE))
6286 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6287 else
6288 nflags = flags;
6289 ret =
6290 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6291 cur->type);
6292 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6293 if (flags & XML_RELAXNG_IN_START) {
6294 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6295 "Found forbidden pattern start//data\n", NULL,
6296 NULL);
6297 }
6298 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6299 ret = XML_RELAXNG_CONTENT_SIMPLE;
6300 } else if (cur->type == XML_RELAXNG_VALUE) {
6301 if (flags & XML_RELAXNG_IN_START) {
6302 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6303 "Found forbidden pattern start//value\n", NULL,
6304 NULL);
6305 }
6306 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6307 ret = XML_RELAXNG_CONTENT_SIMPLE;
6308 } else if (cur->type == XML_RELAXNG_TEXT) {
6309 if (flags & XML_RELAXNG_IN_LIST) {
6310 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6311 "Found forbidden pattern list//text\n", NULL,
6312 NULL);
6313 }
6314 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6315 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6316 "Found forbidden pattern data/except//text\n",
6317 NULL, NULL);
6318 }
6319 if (flags & XML_RELAXNG_IN_START) {
6320 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6321 "Found forbidden pattern start//text\n", NULL,
6322 NULL);
6323 }
6324 ret = XML_RELAXNG_CONTENT_COMPLEX;
6325 } else if (cur->type == XML_RELAXNG_EMPTY) {
6326 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6327 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6328 "Found forbidden pattern data/except//empty\n",
6329 NULL, NULL);
6330 }
6331 if (flags & XML_RELAXNG_IN_START) {
6332 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6333 "Found forbidden pattern start//empty\n", NULL,
6334 NULL);
6335 }
6336 ret = XML_RELAXNG_CONTENT_EMPTY;
6337 } else if (cur->type == XML_RELAXNG_CHOICE) {
6338 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6339 ret =
6340 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6341 } else {
6342 ret =
6343 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6344 }
6345 cur = cur->next;
6346 if (ptype == XML_RELAXNG_GROUP) {
6347 val = xmlRelaxNGGroupContentType(val, ret);
6348 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6349 tmp = xmlRelaxNGGroupContentType(val, ret);
6350 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6351 tmp = xmlRelaxNGMaxContentType(val, ret);
6352 } else if (ptype == XML_RELAXNG_CHOICE) {
6353 val = xmlRelaxNGMaxContentType(val, ret);
6354 } else if (ptype == XML_RELAXNG_LIST) {
6355 val = XML_RELAXNG_CONTENT_SIMPLE;
6356 } else if (ptype == XML_RELAXNG_EXCEPT) {
6357 if (ret == XML_RELAXNG_CONTENT_ERROR)
6358 val = XML_RELAXNG_CONTENT_ERROR;
6359 else
6360 val = XML_RELAXNG_CONTENT_SIMPLE;
6361 } else {
6362 val = xmlRelaxNGGroupContentType(val, ret);
6363 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006364
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006365 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006366 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006367}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006368
6369/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006370 * xmlRelaxNGParseGrammar:
6371 * @ctxt: a Relax-NG parser context
6372 * @nodes: grammar children nodes
6373 *
6374 * parse a Relax-NG <grammar> node
6375 *
6376 * Returns the internal xmlRelaxNGGrammarPtr built or
6377 * NULL in case of error
6378 */
6379static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006380xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6381{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006382 xmlRelaxNGGrammarPtr ret, tmp, old;
6383
Daniel Veillardc482e262003-02-26 14:48:48 +00006384#ifdef DEBUG_GRAMMAR
6385 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6386#endif
6387
Daniel Veillard6eadf632003-01-23 18:29:16 +00006388 ret = xmlRelaxNGNewGrammar(ctxt);
6389 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006390 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006391
6392 /*
6393 * Link the new grammar in the tree
6394 */
6395 ret->parent = ctxt->grammar;
6396 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006397 tmp = ctxt->grammar->children;
6398 if (tmp == NULL) {
6399 ctxt->grammar->children = ret;
6400 } else {
6401 while (tmp->next != NULL)
6402 tmp = tmp->next;
6403 tmp->next = ret;
6404 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006405 }
6406
6407 old = ctxt->grammar;
6408 ctxt->grammar = ret;
6409 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6410 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006411 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006412 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6413 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006414 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006415 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6416 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006417 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006418
6419 /*
6420 * Apply 4.17 mergingd rules to defines and starts
6421 */
6422 xmlRelaxNGCombineStart(ctxt, ret);
6423 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006424 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6425 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006426 }
6427
6428 /*
6429 * link together defines and refs in this grammar
6430 */
6431 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006432 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6433 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006434 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006435
Daniel Veillard6eadf632003-01-23 18:29:16 +00006436 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006437 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006438}
6439
6440/**
6441 * xmlRelaxNGParseDocument:
6442 * @ctxt: a Relax-NG parser context
6443 * @node: the root node of the RelaxNG schema
6444 *
6445 * parse a Relax-NG definition resource and build an internal
6446 * xmlRelaxNG struture which can be used to validate instances.
6447 *
6448 * Returns the internal XML RelaxNG structure built or
6449 * NULL in case of error
6450 */
6451static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006452xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6453{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006454 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006455 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006456 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006457
6458 if ((ctxt == NULL) || (node == NULL))
6459 return (NULL);
6460
6461 schema = xmlRelaxNGNewRelaxNG(ctxt);
6462 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006463 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006464
Daniel Veillard276be4a2003-01-24 01:03:34 +00006465 olddefine = ctxt->define;
6466 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006467 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006468 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006469 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006470 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006471
Daniel Veillard4c004142003-10-07 11:33:24 +00006472 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6473 if (schema->topgrammar == NULL) {
6474 return (schema);
6475 }
6476 /*
6477 * Link the new grammar in the tree
6478 */
6479 ret->parent = ctxt->grammar;
6480 if (ctxt->grammar != NULL) {
6481 tmp = ctxt->grammar->children;
6482 if (tmp == NULL) {
6483 ctxt->grammar->children = ret;
6484 } else {
6485 while (tmp->next != NULL)
6486 tmp = tmp->next;
6487 tmp->next = ret;
6488 }
6489 }
6490 old = ctxt->grammar;
6491 ctxt->grammar = ret;
6492 xmlRelaxNGParseStart(ctxt, node);
6493 if (old != NULL)
6494 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006495 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006496 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006497 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006498 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6499 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6500 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6501 while ((schema->topgrammar->start != NULL) &&
6502 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6503 (schema->topgrammar->start->next != NULL))
6504 schema->topgrammar->start =
6505 schema->topgrammar->start->content;
6506 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6507 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6508 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006509 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006510#ifdef DEBUG
6511 if (schema == NULL)
6512 xmlGenericError(xmlGenericErrorContext,
6513 "xmlRelaxNGParseDocument() failed\n");
6514#endif
6515
6516 return (schema);
6517}
6518
6519/************************************************************************
6520 * *
6521 * Reading RelaxNGs *
6522 * *
6523 ************************************************************************/
6524
6525/**
6526 * xmlRelaxNGNewParserCtxt:
6527 * @URL: the location of the schema
6528 *
6529 * Create an XML RelaxNGs parse context for that file/resource expected
6530 * to contain an XML RelaxNGs file.
6531 *
6532 * Returns the parser context or NULL in case of error
6533 */
6534xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006535xmlRelaxNGNewParserCtxt(const char *URL)
6536{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006537 xmlRelaxNGParserCtxtPtr ret;
6538
6539 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006540 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006541
Daniel Veillard4c004142003-10-07 11:33:24 +00006542 ret =
6543 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006544 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006545 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006546 return (NULL);
6547 }
6548 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006549 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006550 ret->error = xmlGenericError;
6551 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006552 return (ret);
6553}
6554
6555/**
6556 * xmlRelaxNGNewMemParserCtxt:
6557 * @buffer: a pointer to a char array containing the schemas
6558 * @size: the size of the array
6559 *
6560 * Create an XML RelaxNGs parse context for that memory buffer expected
6561 * to contain an XML RelaxNGs file.
6562 *
6563 * Returns the parser context or NULL in case of error
6564 */
6565xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006566xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6567{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006568 xmlRelaxNGParserCtxtPtr ret;
6569
6570 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006571 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006572
Daniel Veillard4c004142003-10-07 11:33:24 +00006573 ret =
6574 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006575 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006576 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006577 return (NULL);
6578 }
6579 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6580 ret->buffer = buffer;
6581 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006582 ret->error = xmlGenericError;
6583 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006584 return (ret);
6585}
6586
6587/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006588 * xmlRelaxNGNewDocParserCtxt:
6589 * @doc: a preparsed document tree
6590 *
6591 * Create an XML RelaxNGs parser context for that document.
6592 * Note: since the process of compiling a RelaxNG schemas modifies the
6593 * document, the @doc parameter is duplicated internally.
6594 *
6595 * Returns the parser context or NULL in case of error
6596 */
6597xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006598xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6599{
Daniel Veillard33300b42003-04-17 09:09:19 +00006600 xmlRelaxNGParserCtxtPtr ret;
6601 xmlDocPtr copy;
6602
6603 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006604 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006605 copy = xmlCopyDoc(doc, 1);
6606 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006607 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006608
Daniel Veillard4c004142003-10-07 11:33:24 +00006609 ret =
6610 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006611 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006612 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006613 return (NULL);
6614 }
6615 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6616 ret->document = copy;
6617 ret->userData = xmlGenericErrorContext;
6618 return (ret);
6619}
6620
6621/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006622 * xmlRelaxNGFreeParserCtxt:
6623 * @ctxt: the schema parser context
6624 *
6625 * Free the resources associated to the schema parser context
6626 */
6627void
Daniel Veillard4c004142003-10-07 11:33:24 +00006628xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6629{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006630 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006631 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006632 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006633 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006634 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006635 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006636 if (ctxt->interleaves != NULL)
6637 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006638 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006639 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006640 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006641 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006642 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006643 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006644 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006645 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006646 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006647 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006648
Daniel Veillard4c004142003-10-07 11:33:24 +00006649 for (i = 0; i < ctxt->defNr; i++)
6650 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6651 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006652 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006653 xmlFree(ctxt);
6654}
6655
Daniel Veillard6eadf632003-01-23 18:29:16 +00006656/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006657 * xmlRelaxNGNormExtSpace:
6658 * @value: a value
6659 *
6660 * Removes the leading and ending spaces of the value
6661 * The string is modified "in situ"
6662 */
6663static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006664xmlRelaxNGNormExtSpace(xmlChar * value)
6665{
Daniel Veillardd2298792003-02-14 16:54:11 +00006666 xmlChar *start = value;
6667 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006668
Daniel Veillard4c004142003-10-07 11:33:24 +00006669 if (value == NULL)
6670 return;
6671
6672 while (IS_BLANK(*cur))
6673 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006674 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006675 do {
6676 while ((*cur != 0) && (!IS_BLANK(*cur)))
6677 cur++;
6678 if (*cur == 0)
6679 return;
6680 start = cur;
6681 while (IS_BLANK(*cur))
6682 cur++;
6683 if (*cur == 0) {
6684 *start = 0;
6685 return;
6686 }
6687 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006688 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006689 do {
6690 while ((*cur != 0) && (!IS_BLANK(*cur)))
6691 *start++ = *cur++;
6692 if (*cur == 0) {
6693 *start = 0;
6694 return;
6695 }
6696 /* don't try to normalize the inner spaces */
6697 while (IS_BLANK(*cur))
6698 cur++;
6699 *start++ = *cur++;
6700 if (*cur == 0) {
6701 *start = 0;
6702 return;
6703 }
6704 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006705 }
6706}
6707
6708/**
6709 * xmlRelaxNGCheckAttributes:
6710 * @ctxt: a Relax-NG parser context
6711 * @node: a Relax-NG node
6712 *
6713 * Check all the attributes on the given node
6714 */
6715static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006716xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6717{
Daniel Veillardd2298792003-02-14 16:54:11 +00006718 xmlAttrPtr cur, next;
6719
6720 cur = node->properties;
6721 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006722 next = cur->next;
6723 if ((cur->ns == NULL) ||
6724 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6725 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6726 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6727 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6728 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6729 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6730 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6731 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6732 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6733 "Attribute %s is not allowed on %s\n",
6734 cur->name, node->name);
6735 }
6736 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6737 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6738 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6739 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6740 "Attribute %s is not allowed on %s\n",
6741 cur->name, node->name);
6742 }
6743 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6744 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6745 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6746 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6747 "Attribute %s is not allowed on %s\n",
6748 cur->name, node->name);
6749 }
6750 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6751 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6752 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6753 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6754 "Attribute %s is not allowed on %s\n",
6755 cur->name, node->name);
6756 }
6757 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6758 xmlChar *val;
6759 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006760
Daniel Veillard4c004142003-10-07 11:33:24 +00006761 val = xmlNodeListGetString(node->doc, cur->children, 1);
6762 if (val != NULL) {
6763 if (val[0] != 0) {
6764 uri = xmlParseURI((const char *) val);
6765 if (uri == NULL) {
6766 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6767 "Attribute %s contains invalid URI %s\n",
6768 cur->name, val);
6769 } else {
6770 if (uri->scheme == NULL) {
6771 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6772 "Attribute %s URI %s is not absolute\n",
6773 cur->name, val);
6774 }
6775 if (uri->fragment != NULL) {
6776 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6777 "Attribute %s URI %s has a fragment ID\n",
6778 cur->name, val);
6779 }
6780 xmlFreeURI(uri);
6781 }
6782 }
6783 xmlFree(val);
6784 }
6785 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6786 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6787 "Unknown attribute %s on %s\n", cur->name,
6788 node->name);
6789 }
6790 }
6791 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006792 }
6793}
6794
6795/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006796 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006797 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006798 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006799 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006800 * Cleanup the subtree from unwanted nodes for parsing, resolve
6801 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006802 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006803static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006804xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6805{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006806 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006807
Daniel Veillard6eadf632003-01-23 18:29:16 +00006808 delete = NULL;
6809 cur = root;
6810 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006811 if (delete != NULL) {
6812 xmlUnlinkNode(delete);
6813 xmlFreeNode(delete);
6814 delete = NULL;
6815 }
6816 if (cur->type == XML_ELEMENT_NODE) {
6817 /*
6818 * Simplification 4.1. Annotations
6819 */
6820 if ((cur->ns == NULL) ||
6821 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6822 if ((cur->parent != NULL) &&
6823 (cur->parent->type == XML_ELEMENT_NODE) &&
6824 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6825 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6826 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6827 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6828 "element %s doesn't allow foreign elements\n",
6829 cur->parent->name, NULL);
6830 }
6831 delete = cur;
6832 goto skip_children;
6833 } else {
6834 xmlRelaxNGCleanupAttributes(ctxt, cur);
6835 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6836 xmlChar *href, *ns, *base, *URL;
6837 xmlRelaxNGDocumentPtr docu;
6838 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006839
Daniel Veillard4c004142003-10-07 11:33:24 +00006840 ns = xmlGetProp(cur, BAD_CAST "ns");
6841 if (ns == NULL) {
6842 tmp = cur->parent;
6843 while ((tmp != NULL) &&
6844 (tmp->type == XML_ELEMENT_NODE)) {
6845 ns = xmlGetProp(tmp, BAD_CAST "ns");
6846 if (ns != NULL)
6847 break;
6848 tmp = tmp->parent;
6849 }
6850 }
6851 href = xmlGetProp(cur, BAD_CAST "href");
6852 if (href == NULL) {
6853 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6854 "xmlRelaxNGParse: externalRef has no href attribute\n",
6855 NULL, NULL);
6856 delete = cur;
6857 goto skip_children;
6858 }
6859 base = xmlNodeGetBase(cur->doc, cur);
6860 URL = xmlBuildURI(href, base);
6861 if (URL == NULL) {
6862 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6863 "Failed to compute URL for externalRef %s\n",
6864 href, NULL);
6865 if (href != NULL)
6866 xmlFree(href);
6867 if (base != NULL)
6868 xmlFree(base);
6869 delete = cur;
6870 goto skip_children;
6871 }
6872 if (href != NULL)
6873 xmlFree(href);
6874 if (base != NULL)
6875 xmlFree(base);
6876 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6877 if (docu == NULL) {
6878 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6879 "Failed to load externalRef %s\n", URL,
6880 NULL);
6881 xmlFree(URL);
6882 delete = cur;
6883 goto skip_children;
6884 }
6885 if (ns != NULL)
6886 xmlFree(ns);
6887 xmlFree(URL);
6888 cur->_private = docu;
6889 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6890 xmlChar *href, *ns, *base, *URL;
6891 xmlRelaxNGIncludePtr incl;
6892 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006893
Daniel Veillard4c004142003-10-07 11:33:24 +00006894 href = xmlGetProp(cur, BAD_CAST "href");
6895 if (href == NULL) {
6896 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6897 "xmlRelaxNGParse: include has no href attribute\n",
6898 NULL, NULL);
6899 delete = cur;
6900 goto skip_children;
6901 }
6902 base = xmlNodeGetBase(cur->doc, cur);
6903 URL = xmlBuildURI(href, base);
6904 if (URL == NULL) {
6905 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6906 "Failed to compute URL for include %s\n",
6907 href, NULL);
6908 if (href != NULL)
6909 xmlFree(href);
6910 if (base != NULL)
6911 xmlFree(base);
6912 delete = cur;
6913 goto skip_children;
6914 }
6915 if (href != NULL)
6916 xmlFree(href);
6917 if (base != NULL)
6918 xmlFree(base);
6919 ns = xmlGetProp(cur, BAD_CAST "ns");
6920 if (ns == NULL) {
6921 tmp = cur->parent;
6922 while ((tmp != NULL) &&
6923 (tmp->type == XML_ELEMENT_NODE)) {
6924 ns = xmlGetProp(tmp, BAD_CAST "ns");
6925 if (ns != NULL)
6926 break;
6927 tmp = tmp->parent;
6928 }
6929 }
6930 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6931 if (ns != NULL)
6932 xmlFree(ns);
6933 if (incl == NULL) {
6934 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
6935 "Failed to load include %s\n", URL,
6936 NULL);
6937 xmlFree(URL);
6938 delete = cur;
6939 goto skip_children;
6940 }
6941 xmlFree(URL);
6942 cur->_private = incl;
6943 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6944 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
6945 {
6946 xmlChar *name, *ns;
6947 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006948
Daniel Veillard4c004142003-10-07 11:33:24 +00006949 /*
6950 * Simplification 4.8. name attribute of element
6951 * and attribute elements
6952 */
6953 name = xmlGetProp(cur, BAD_CAST "name");
6954 if (name != NULL) {
6955 if (cur->children == NULL) {
6956 text =
6957 xmlNewChild(cur, cur->ns, BAD_CAST "name",
6958 name);
6959 } else {
6960 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006961
Daniel Veillard4c004142003-10-07 11:33:24 +00006962 node = xmlNewNode(cur->ns, BAD_CAST "name");
6963 if (node != NULL) {
6964 xmlAddPrevSibling(cur->children, node);
6965 text = xmlNewText(name);
6966 xmlAddChild(node, text);
6967 text = node;
6968 }
6969 }
6970 if (text == NULL) {
6971 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
6972 "Failed to create a name %s element\n",
6973 name, NULL);
6974 }
6975 xmlUnsetProp(cur, BAD_CAST "name");
6976 xmlFree(name);
6977 ns = xmlGetProp(cur, BAD_CAST "ns");
6978 if (ns != NULL) {
6979 if (text != NULL) {
6980 xmlSetProp(text, BAD_CAST "ns", ns);
6981 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
6982 }
6983 xmlFree(ns);
6984 } else if (xmlStrEqual(cur->name,
6985 BAD_CAST "attribute")) {
6986 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
6987 }
6988 }
6989 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
6990 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
6991 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
6992 /*
6993 * Simplification 4.8. name attribute of element
6994 * and attribute elements
6995 */
6996 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
6997 xmlNodePtr node;
6998 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006999
Daniel Veillard4c004142003-10-07 11:33:24 +00007000 node = cur->parent;
7001 while ((node != NULL) &&
7002 (node->type == XML_ELEMENT_NODE)) {
7003 ns = xmlGetProp(node, BAD_CAST "ns");
7004 if (ns != NULL) {
7005 break;
7006 }
7007 node = node->parent;
7008 }
7009 if (ns == NULL) {
7010 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7011 } else {
7012 xmlSetProp(cur, BAD_CAST "ns", ns);
7013 xmlFree(ns);
7014 }
7015 }
7016 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7017 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007018
Daniel Veillard4c004142003-10-07 11:33:24 +00007019 /*
7020 * Simplification: 4.10. QNames
7021 */
7022 name = xmlNodeGetContent(cur);
7023 if (name != NULL) {
7024 local = xmlSplitQName2(name, &prefix);
7025 if (local != NULL) {
7026 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007027
Daniel Veillard4c004142003-10-07 11:33:24 +00007028 ns = xmlSearchNs(cur->doc, cur, prefix);
7029 if (ns == NULL) {
7030 xmlRngPErr(ctxt, cur,
7031 XML_RNGP_PREFIX_UNDEFINED,
7032 "xmlRelaxNGParse: no namespace for prefix %s\n",
7033 prefix, NULL);
7034 } else {
7035 xmlSetProp(cur, BAD_CAST "ns",
7036 ns->href);
7037 xmlNodeSetContent(cur, local);
7038 }
7039 xmlFree(local);
7040 xmlFree(prefix);
7041 }
7042 xmlFree(name);
7043 }
7044 }
7045 /*
7046 * 4.16
7047 */
7048 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7049 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7050 xmlRngPErr(ctxt, cur,
7051 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7052 "Found nsName/except//nsName forbidden construct\n",
7053 NULL, NULL);
7054 }
7055 }
7056 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7057 (cur != root)) {
7058 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007059
Daniel Veillard4c004142003-10-07 11:33:24 +00007060 /*
7061 * 4.16
7062 */
7063 if ((cur->parent != NULL) &&
7064 (xmlStrEqual
7065 (cur->parent->name, BAD_CAST "anyName"))) {
7066 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7067 xmlRelaxNGCleanupTree(ctxt, cur);
7068 ctxt->flags = oldflags;
7069 goto skip_children;
7070 } else if ((cur->parent != NULL) &&
7071 (xmlStrEqual
7072 (cur->parent->name, BAD_CAST "nsName"))) {
7073 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7074 xmlRelaxNGCleanupTree(ctxt, cur);
7075 ctxt->flags = oldflags;
7076 goto skip_children;
7077 }
7078 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7079 /*
7080 * 4.16
7081 */
7082 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7083 xmlRngPErr(ctxt, cur,
7084 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7085 "Found anyName/except//anyName forbidden construct\n",
7086 NULL, NULL);
7087 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7088 xmlRngPErr(ctxt, cur,
7089 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7090 "Found nsName/except//anyName forbidden construct\n",
7091 NULL, NULL);
7092 }
7093 }
7094 /*
7095 * Thisd is not an else since "include" is transformed
7096 * into a div
7097 */
7098 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7099 xmlChar *ns;
7100 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007101
Daniel Veillard4c004142003-10-07 11:33:24 +00007102 /*
7103 * implements rule 4.11
7104 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007105
Daniel Veillard4c004142003-10-07 11:33:24 +00007106 ns = xmlGetProp(cur, BAD_CAST "ns");
7107
7108 child = cur->children;
7109 ins = cur;
7110 while (child != NULL) {
7111 if (ns != NULL) {
7112 if (!xmlHasProp(child, BAD_CAST "ns")) {
7113 xmlSetProp(child, BAD_CAST "ns", ns);
7114 }
7115 }
7116 tmp = child->next;
7117 xmlUnlinkNode(child);
7118 ins = xmlAddNextSibling(ins, child);
7119 child = tmp;
7120 }
7121 if (ns != NULL)
7122 xmlFree(ns);
7123 delete = cur;
7124 goto skip_children;
7125 }
7126 }
7127 }
7128 /*
7129 * Simplification 4.2 whitespaces
7130 */
7131 else if ((cur->type == XML_TEXT_NODE) ||
7132 (cur->type == XML_CDATA_SECTION_NODE)) {
7133 if (IS_BLANK_NODE(cur)) {
7134 if (cur->parent->type == XML_ELEMENT_NODE) {
7135 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7136 &&
7137 (!xmlStrEqual
7138 (cur->parent->name, BAD_CAST "param")))
7139 delete = cur;
7140 } else {
7141 delete = cur;
7142 goto skip_children;
7143 }
7144 }
7145 } else {
7146 delete = cur;
7147 goto skip_children;
7148 }
7149
7150 /*
7151 * Skip to next node
7152 */
7153 if (cur->children != NULL) {
7154 if ((cur->children->type != XML_ENTITY_DECL) &&
7155 (cur->children->type != XML_ENTITY_REF_NODE) &&
7156 (cur->children->type != XML_ENTITY_NODE)) {
7157 cur = cur->children;
7158 continue;
7159 }
7160 }
7161 skip_children:
7162 if (cur->next != NULL) {
7163 cur = cur->next;
7164 continue;
7165 }
7166
7167 do {
7168 cur = cur->parent;
7169 if (cur == NULL)
7170 break;
7171 if (cur == root) {
7172 cur = NULL;
7173 break;
7174 }
7175 if (cur->next != NULL) {
7176 cur = cur->next;
7177 break;
7178 }
7179 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007180 }
7181 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007182 xmlUnlinkNode(delete);
7183 xmlFreeNode(delete);
7184 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007185 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007186}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007187
Daniel Veillardc5312d72003-02-21 17:14:10 +00007188/**
7189 * xmlRelaxNGCleanupDoc:
7190 * @ctxt: a Relax-NG parser context
7191 * @doc: an xmldocPtr document pointer
7192 *
7193 * Cleanup the document from unwanted nodes for parsing, resolve
7194 * Include and externalRef lookups.
7195 *
7196 * Returns the cleaned up document or NULL in case of error
7197 */
7198static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007199xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7200{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007201 xmlNodePtr root;
7202
7203 /*
7204 * Extract the root
7205 */
7206 root = xmlDocGetRootElement(doc);
7207 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007208 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7209 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007210 return (NULL);
7211 }
7212 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007213 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007214}
7215
7216/**
7217 * xmlRelaxNGParse:
7218 * @ctxt: a Relax-NG parser context
7219 *
7220 * parse a schema definition resource and build an internal
7221 * XML Shema struture which can be used to validate instances.
7222 * *WARNING* this interface is highly subject to change
7223 *
7224 * Returns the internal XML RelaxNG structure built from the resource or
7225 * NULL in case of error
7226 */
7227xmlRelaxNGPtr
7228xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7229{
7230 xmlRelaxNGPtr ret = NULL;
7231 xmlDocPtr doc;
7232 xmlNodePtr root;
7233
7234 xmlRelaxNGInitTypes();
7235
7236 if (ctxt == NULL)
7237 return (NULL);
7238
7239 /*
7240 * First step is to parse the input document into an DOM/Infoset
7241 */
7242 if (ctxt->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007243 doc = xmlParseFile((const char *) ctxt->URL);
7244 if (doc == NULL) {
7245 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7246 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7247 NULL);
7248 return (NULL);
7249 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007250 } else if (ctxt->buffer != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007251 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
7252 if (doc == NULL) {
7253 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7254 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7255 NULL);
7256 return (NULL);
7257 }
7258 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7259 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007260 } else if (ctxt->document != NULL) {
7261 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007262 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007263 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7264 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7265 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007266 }
7267 ctxt->document = doc;
7268
7269 /*
7270 * Some preprocessing of the document content
7271 */
7272 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7273 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007274 xmlFreeDoc(ctxt->document);
7275 ctxt->document = NULL;
7276 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007277 }
7278
Daniel Veillard6eadf632003-01-23 18:29:16 +00007279 /*
7280 * Then do the parsing for good
7281 */
7282 root = xmlDocGetRootElement(doc);
7283 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007284 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7285 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7286 ctxt->URL, NULL);
7287 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007288 return (NULL);
7289 }
7290 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007291 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007292 xmlFreeDoc(doc);
7293 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007294 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007295
7296 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007297 * Check the ref/defines links
7298 */
7299 /*
7300 * try to preprocess interleaves
7301 */
7302 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007303 xmlHashScan(ctxt->interleaves,
7304 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007305 }
7306
7307 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007308 * if there was a parsing error return NULL
7309 */
7310 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007311 xmlRelaxNGFree(ret);
7312 ctxt->document = NULL;
7313 xmlFreeDoc(doc);
7314 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007315 }
7316
7317 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007318 * try to compile (parts of) the schemas
7319 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007320 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7321 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007322 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007323
Daniel Veillard4c004142003-10-07 11:33:24 +00007324 def = xmlRelaxNGNewDefine(ctxt, NULL);
7325 if (def != NULL) {
7326 def->type = XML_RELAXNG_START;
7327 def->content = ret->topgrammar->start;
7328 ret->topgrammar->start = def;
7329 }
7330 }
7331 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007332 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007333
7334 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007335 * Transfer the pointer for cleanup at the schema level.
7336 */
7337 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007338 ctxt->document = NULL;
7339 ret->documents = ctxt->documents;
7340 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007341
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007342 ret->includes = ctxt->includes;
7343 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007344 ret->defNr = ctxt->defNr;
7345 ret->defTab = ctxt->defTab;
7346 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007347 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007348 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007349
7350 return (ret);
7351}
Daniel Veillard4c004142003-10-07 11:33:24 +00007352
Daniel Veillard6eadf632003-01-23 18:29:16 +00007353/**
7354 * xmlRelaxNGSetParserErrors:
7355 * @ctxt: a Relax-NG validation context
7356 * @err: the error callback
7357 * @warn: the warning callback
7358 * @ctx: contextual data for the callbacks
7359 *
7360 * Set the callback functions used to handle errors for a validation context
7361 */
7362void
7363xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007364 xmlRelaxNGValidityErrorFunc err,
7365 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7366{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007367 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007368 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007369 ctxt->error = err;
7370 ctxt->warning = warn;
7371 ctxt->userData = ctx;
7372}
Daniel Veillard409a8142003-07-18 15:16:57 +00007373
7374/**
7375 * xmlRelaxNGGetParserErrors:
7376 * @ctxt: a Relax-NG validation context
7377 * @err: the error callback result
7378 * @warn: the warning callback result
7379 * @ctx: contextual data for the callbacks result
7380 *
7381 * Get the callback information used to handle errors for a validation context
7382 *
7383 * Returns -1 in case of failure, 0 otherwise.
7384 */
7385int
7386xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007387 xmlRelaxNGValidityErrorFunc * err,
7388 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7389{
Daniel Veillard409a8142003-07-18 15:16:57 +00007390 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007391 return (-1);
7392 if (err != NULL)
7393 *err = ctxt->error;
7394 if (warn != NULL)
7395 *warn = ctxt->warning;
7396 if (ctx != NULL)
7397 *ctx = ctxt->userData;
7398 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007399}
7400
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007401#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007402
Daniel Veillard6eadf632003-01-23 18:29:16 +00007403/************************************************************************
7404 * *
7405 * Dump back a compiled form *
7406 * *
7407 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007408static void xmlRelaxNGDumpDefine(FILE * output,
7409 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007410
7411/**
7412 * xmlRelaxNGDumpDefines:
7413 * @output: the file output
7414 * @defines: a list of define structures
7415 *
7416 * Dump a RelaxNG structure back
7417 */
7418static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007419xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7420{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007421 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007422 xmlRelaxNGDumpDefine(output, defines);
7423 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007424 }
7425}
7426
7427/**
7428 * xmlRelaxNGDumpDefine:
7429 * @output: the file output
7430 * @define: a define structure
7431 *
7432 * Dump a RelaxNG structure back
7433 */
7434static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007435xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7436{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007437 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007438 return;
7439 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007440 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007441 fprintf(output, "<empty/>\n");
7442 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007443 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007444 fprintf(output, "<notAllowed/>\n");
7445 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007446 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007447 fprintf(output, "<text/>\n");
7448 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007449 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007450 fprintf(output, "<element>\n");
7451 if (define->name != NULL) {
7452 fprintf(output, "<name");
7453 if (define->ns != NULL)
7454 fprintf(output, " ns=\"%s\"", define->ns);
7455 fprintf(output, ">%s</name>\n", define->name);
7456 }
7457 xmlRelaxNGDumpDefines(output, define->attrs);
7458 xmlRelaxNGDumpDefines(output, define->content);
7459 fprintf(output, "</element>\n");
7460 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007461 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007462 fprintf(output, "<list>\n");
7463 xmlRelaxNGDumpDefines(output, define->content);
7464 fprintf(output, "</list>\n");
7465 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007466 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007467 fprintf(output, "<oneOrMore>\n");
7468 xmlRelaxNGDumpDefines(output, define->content);
7469 fprintf(output, "</oneOrMore>\n");
7470 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007471 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007472 fprintf(output, "<zeroOrMore>\n");
7473 xmlRelaxNGDumpDefines(output, define->content);
7474 fprintf(output, "</zeroOrMore>\n");
7475 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007476 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007477 fprintf(output, "<choice>\n");
7478 xmlRelaxNGDumpDefines(output, define->content);
7479 fprintf(output, "</choice>\n");
7480 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007481 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007482 fprintf(output, "<group>\n");
7483 xmlRelaxNGDumpDefines(output, define->content);
7484 fprintf(output, "</group>\n");
7485 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007486 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007487 fprintf(output, "<interleave>\n");
7488 xmlRelaxNGDumpDefines(output, define->content);
7489 fprintf(output, "</interleave>\n");
7490 break;
7491 case XML_RELAXNG_OPTIONAL:
7492 fprintf(output, "<optional>\n");
7493 xmlRelaxNGDumpDefines(output, define->content);
7494 fprintf(output, "</optional>\n");
7495 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007496 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007497 fprintf(output, "<attribute>\n");
7498 xmlRelaxNGDumpDefines(output, define->content);
7499 fprintf(output, "</attribute>\n");
7500 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007501 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007502 fprintf(output, "<define");
7503 if (define->name != NULL)
7504 fprintf(output, " name=\"%s\"", define->name);
7505 fprintf(output, ">\n");
7506 xmlRelaxNGDumpDefines(output, define->content);
7507 fprintf(output, "</define>\n");
7508 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007509 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007510 fprintf(output, "<ref");
7511 if (define->name != NULL)
7512 fprintf(output, " name=\"%s\"", define->name);
7513 fprintf(output, ">\n");
7514 xmlRelaxNGDumpDefines(output, define->content);
7515 fprintf(output, "</ref>\n");
7516 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007517 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007518 fprintf(output, "<parentRef");
7519 if (define->name != NULL)
7520 fprintf(output, " name=\"%s\"", define->name);
7521 fprintf(output, ">\n");
7522 xmlRelaxNGDumpDefines(output, define->content);
7523 fprintf(output, "</parentRef>\n");
7524 break;
7525 case XML_RELAXNG_EXTERNALREF:
7526 fprintf(output, "<externalRef>");
7527 xmlRelaxNGDumpDefines(output, define->content);
7528 fprintf(output, "</externalRef>\n");
7529 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007530 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007531 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007532 TODO break;
7533 case XML_RELAXNG_START:
7534 case XML_RELAXNG_EXCEPT:
7535 case XML_RELAXNG_PARAM:
7536 TODO break;
7537 case XML_RELAXNG_NOOP:
7538 xmlRelaxNGDumpDefines(output, define->content);
7539 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007540 }
7541}
Daniel Veillard4c004142003-10-07 11:33:24 +00007542
Daniel Veillard6eadf632003-01-23 18:29:16 +00007543/**
7544 * xmlRelaxNGDumpGrammar:
7545 * @output: the file output
7546 * @grammar: a grammar structure
7547 * @top: is this a top grammar
7548 *
7549 * Dump a RelaxNG structure back
7550 */
7551static void
7552xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7553{
7554 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007555 return;
7556
Daniel Veillard6eadf632003-01-23 18:29:16 +00007557 fprintf(output, "<grammar");
7558 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007559 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7560 switch (grammar->combine) {
7561 case XML_RELAXNG_COMBINE_UNDEFINED:
7562 break;
7563 case XML_RELAXNG_COMBINE_CHOICE:
7564 fprintf(output, " combine=\"choice\"");
7565 break;
7566 case XML_RELAXNG_COMBINE_INTERLEAVE:
7567 fprintf(output, " combine=\"interleave\"");
7568 break;
7569 default:
7570 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007571 }
7572 fprintf(output, ">\n");
7573 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007574 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007575 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007576 fprintf(output, "<start>\n");
7577 xmlRelaxNGDumpDefine(output, grammar->start);
7578 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007579 }
7580 /* TODO ? Dump the defines ? */
7581 fprintf(output, "</grammar>\n");
7582}
7583
7584/**
7585 * xmlRelaxNGDump:
7586 * @output: the file output
7587 * @schema: a schema structure
7588 *
7589 * Dump a RelaxNG structure back
7590 */
7591void
7592xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7593{
7594 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007595 fprintf(output, "RelaxNG empty or failed to compile\n");
7596 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007597 }
7598 fprintf(output, "RelaxNG: ");
7599 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007600 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007601 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007602 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007603 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007604 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007605 }
7606 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007607 fprintf(output, "RelaxNG has no top grammar\n");
7608 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007609 }
7610 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7611}
7612
Daniel Veillardfebcca42003-02-16 15:44:18 +00007613/**
7614 * xmlRelaxNGDumpTree:
7615 * @output: the file output
7616 * @schema: a schema structure
7617 *
7618 * Dump the transformed RelaxNG tree.
7619 */
7620void
7621xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7622{
7623 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007624 fprintf(output, "RelaxNG empty or failed to compile\n");
7625 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007626 }
7627 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007628 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007629 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007630 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007631 }
7632}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007633#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007634
Daniel Veillard6eadf632003-01-23 18:29:16 +00007635/************************************************************************
7636 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007637 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007638 * *
7639 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007640static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7641 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007642
7643/**
7644 * xmlRelaxNGValidateCompiledCallback:
7645 * @exec: the regular expression instance
7646 * @token: the token which matched
7647 * @transdata: callback data, the define for the subelement if available
7648 @ @inputdata: callback data, the Relax NG validation context
7649 *
7650 * Handle the callback and if needed validate the element children.
7651 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007652static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007653xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007654 const xmlChar * token,
7655 void *transdata, void *inputdata)
7656{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007657 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7658 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7659 int ret;
7660
7661#ifdef DEBUG_COMPILE
7662 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007663 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007664#endif
7665 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007666 fprintf(stderr, "callback on %s missing context\n", token);
7667 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7668 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7669 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007670 }
7671 if (define == NULL) {
7672 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007673 return;
7674 fprintf(stderr, "callback on %s missing define\n", token);
7675 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7676 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7677 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007678 }
7679 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007680 fprintf(stderr, "callback on %s missing info\n", token);
7681 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7682 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7683 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007684 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007685 fprintf(stderr, "callback on %s define is not element\n", token);
7686 if (ctxt->errNo == XML_RELAXNG_OK)
7687 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7688 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007689 }
7690 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007691 if (ret != 0)
7692 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007693}
7694
7695/**
7696 * xmlRelaxNGValidateCompiledContent:
7697 * @ctxt: the RelaxNG validation context
7698 * @regexp: the regular expression as compiled
7699 * @content: list of children to test against the regexp
7700 *
7701 * Validate the content model of an element or start using the regexp
7702 *
7703 * Returns 0 in case of success, -1 in case of error.
7704 */
7705static int
7706xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007707 xmlRegexpPtr regexp, xmlNodePtr content)
7708{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007709 xmlRegExecCtxtPtr exec;
7710 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007711 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007712 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007713
7714 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007715 return (-1);
7716 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007717 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007718 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007719 cur = content;
7720 while (cur != NULL) {
7721 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007722 switch (cur->type) {
7723 case XML_TEXT_NODE:
7724 case XML_CDATA_SECTION_NODE:
7725 if (xmlIsBlankNode(cur))
7726 break;
7727 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7728 if (ret < 0) {
7729 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7730 cur->parent->name);
7731 }
7732 break;
7733 case XML_ELEMENT_NODE:
7734 if (cur->ns != NULL) {
7735 ret = xmlRegExecPushString2(exec, cur->name,
7736 cur->ns->href, ctxt);
7737 } else {
7738 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7739 }
7740 if (ret < 0) {
7741 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7742 }
7743 break;
7744 default:
7745 break;
7746 }
7747 if (ret < 0)
7748 break;
7749 /*
7750 * Switch to next element
7751 */
7752 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007753 }
7754 ret = xmlRegExecPushString(exec, NULL, NULL);
7755 if (ret == 1) {
7756 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007757 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007758 } else if (ret == 0) {
7759 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007760 * TODO: get some of the names needed to exit the current state of exec
7761 */
7762 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7763 ret = -1;
7764 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7765 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007766 } else {
7767 ret = -1;
7768 }
7769 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007770 /*
7771 * There might be content model errors outside of the pure
7772 * regexp validation, e.g. for attribute values.
7773 */
7774 if ((ret == 0) && (ctxt->perr != 0)) {
7775 ret = ctxt->perr;
7776 }
7777 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007778 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007779}
7780
7781/************************************************************************
7782 * *
7783 * Progressive validation of when possible *
7784 * *
7785 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007786static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7787 xmlRelaxNGDefinePtr defines);
7788static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
7789 int log);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007790static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007791
7792/**
7793 * xmlRelaxNGElemPush:
7794 * @ctxt: the validation context
7795 * @exec: the regexp runtime for the new content model
7796 *
7797 * Push a new regexp for the current node content model on the stack
7798 *
7799 * Returns 0 in case of success and -1 in case of error.
7800 */
7801static int
Daniel Veillard4c004142003-10-07 11:33:24 +00007802xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7803{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007804 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007805 ctxt->elemMax = 10;
7806 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7807 sizeof
7808 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007809 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007810 xmlRngVErrMemory(ctxt, "validating\n");
7811 return (-1);
7812 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007813 }
7814 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007815 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007816 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00007817 ctxt->elemMax *
7818 sizeof
7819 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00007820 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007821 xmlRngVErrMemory(ctxt, "validating\n");
7822 return (-1);
7823 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007824 }
7825 ctxt->elemTab[ctxt->elemNr++] = exec;
7826 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00007827 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007828}
7829
7830/**
7831 * xmlRelaxNGElemPop:
7832 * @ctxt: the validation context
7833 *
7834 * Pop the regexp of the current node content model from the stack
7835 *
7836 * Returns the exec or NULL if empty
7837 */
7838static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007839xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7840{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007841 xmlRegExecCtxtPtr ret;
7842
Daniel Veillard4c004142003-10-07 11:33:24 +00007843 if (ctxt->elemNr <= 0)
7844 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007845 ctxt->elemNr--;
7846 ret = ctxt->elemTab[ctxt->elemNr];
7847 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007848 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00007849 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7850 else
Daniel Veillard4c004142003-10-07 11:33:24 +00007851 ctxt->elem = NULL;
7852 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007853}
7854
7855/**
7856 * xmlRelaxNGValidateProgressiveCallback:
7857 * @exec: the regular expression instance
7858 * @token: the token which matched
7859 * @transdata: callback data, the define for the subelement if available
7860 @ @inputdata: callback data, the Relax NG validation context
7861 *
7862 * Handle the callback and if needed validate the element children.
7863 * some of the in/out informations are passed via the context in @inputdata.
7864 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007865static void
7866xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
7867 ATTRIBUTE_UNUSED,
7868 const xmlChar * token,
7869 void *transdata, void *inputdata)
7870{
Daniel Veillardf4e55762003-04-15 23:32:22 +00007871 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7872 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007873 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007874 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007875 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007876
7877#ifdef DEBUG_PROGRESSIVE
7878 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007879 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007880#endif
7881 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007882 fprintf(stderr, "callback on %s missing context\n", token);
7883 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007884 }
7885 ctxt->pstate = 1;
7886 if (define == NULL) {
7887 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007888 return;
7889 fprintf(stderr, "callback on %s missing define\n", token);
7890 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7891 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7892 ctxt->pstate = -1;
7893 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007894 }
7895 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007896 fprintf(stderr, "callback on %s missing info\n", token);
7897 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7898 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7899 ctxt->pstate = -1;
7900 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007901 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007902 fprintf(stderr, "callback on %s define is not element\n", token);
7903 if (ctxt->errNo == XML_RELAXNG_OK)
7904 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7905 ctxt->pstate = -1;
7906 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007907 }
7908 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007909 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7910 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7911 xmlRelaxNGDumpValidError(ctxt);
7912 ctxt->pstate = -1;
7913 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007914 }
7915 if (define->contModel == NULL) {
7916 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007917 * this node cannot be validated in a streamable fashion
7918 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00007919#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00007920 xmlGenericError(xmlGenericErrorContext,
7921 "Element '%s' validation is not streamable\n",
7922 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007923#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00007924 ctxt->pstate = 0;
7925 ctxt->pdef = define;
7926 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007927 }
7928 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00007929 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007930 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007931 ctxt->pstate = -1;
7932 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007933 }
7934 xmlRelaxNGElemPush(ctxt, exec);
7935
7936 /*
7937 * Validate the attributes part of the content.
7938 */
7939 state = xmlRelaxNGNewValidState(ctxt, node);
7940 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007941 ctxt->pstate = -1;
7942 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007943 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007944 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007945 ctxt->state = state;
7946 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007947 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
7948 if (ret != 0) {
7949 ctxt->pstate = -1;
7950 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
7951 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00007952 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007953 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007954 ctxt->state->seq = NULL;
7955 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
7956 if (ret != 0) {
7957 ctxt->pstate = -1;
7958 }
7959 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00007960 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007961 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007962
7963 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007964
Daniel Veillard4c004142003-10-07 11:33:24 +00007965 for (i = 0; i < ctxt->states->nbState; i++) {
7966 state = ctxt->states->tabState[i];
7967 ctxt->state = state;
7968 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007969
Daniel Veillard4c004142003-10-07 11:33:24 +00007970 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
7971 tmp = 0;
7972 break;
7973 }
7974 }
7975 if (tmp != 0) {
7976 /*
7977 * validation error, log the message for the "best" one
7978 */
7979 ctxt->flags |= FLAGS_IGNORABLE;
7980 xmlRelaxNGLogBestError(ctxt);
7981 }
7982 for (i = 0; i < ctxt->states->nbState; i++) {
7983 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
7984 }
7985 xmlRelaxNGFreeStates(ctxt, ctxt->states);
7986 ctxt->states = NULL;
7987 if ((ret == 0) && (tmp == -1))
7988 ctxt->pstate = -1;
7989 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007990 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007991 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007992 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
7993 xmlRelaxNGDumpValidError(ctxt);
7994 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007995 }
7996 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007997}
7998
7999/**
8000 * xmlRelaxNGValidatePushElement:
8001 * @ctxt: the validation context
8002 * @doc: a document instance
8003 * @elem: an element instance
8004 *
8005 * Push a new element start on the RelaxNG validation stack.
8006 *
8007 * returns 1 if no validation problem was found or 0 if validating the
8008 * element requires a full node, and -1 in case of error.
8009 */
8010int
Daniel Veillard33300b42003-04-17 09:09:19 +00008011xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8012 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008013 xmlNodePtr elem)
8014{
8015 int ret = 1;
8016
8017 if ((ctxt == NULL) || (elem == NULL))
8018 return (-1);
8019
8020#ifdef DEBUG_PROGRESSIVE
8021 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8022#endif
8023 if (ctxt->elem == 0) {
8024 xmlRelaxNGPtr schema;
8025 xmlRelaxNGGrammarPtr grammar;
8026 xmlRegExecCtxtPtr exec;
8027 xmlRelaxNGDefinePtr define;
8028
8029 schema = ctxt->schema;
8030 if (schema == NULL) {
8031 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8032 return (-1);
8033 }
8034 grammar = schema->topgrammar;
8035 if ((grammar == NULL) || (grammar->start == NULL)) {
8036 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8037 return (-1);
8038 }
8039 define = grammar->start;
8040 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008041 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008042 return (0);
8043 }
8044 exec = xmlRegNewExecCtxt(define->contModel,
8045 xmlRelaxNGValidateProgressiveCallback,
8046 ctxt);
8047 if (exec == NULL) {
8048 return (-1);
8049 }
8050 xmlRelaxNGElemPush(ctxt, exec);
8051 }
8052 ctxt->pnode = elem;
8053 ctxt->pstate = 0;
8054 if (elem->ns != NULL) {
8055 ret =
8056 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8057 ctxt);
8058 } else {
8059 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8060 }
8061 if (ret < 0) {
8062 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8063 } else {
8064 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008065 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008066 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008067 ret = -1;
8068 else
8069 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008070 }
8071#ifdef DEBUG_PROGRESSIVE
8072 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008073 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8074 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008075#endif
8076 return (ret);
8077}
8078
8079/**
8080 * xmlRelaxNGValidatePushCData:
8081 * @ctxt: the RelaxNG validation context
8082 * @data: some character data read
8083 * @len: the lenght of the data
8084 *
8085 * check the CData parsed for validation in the current stack
8086 *
8087 * returns 1 if no validation problem was found or -1 otherwise
8088 */
8089int
8090xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008091 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008092{
8093 int ret = 1;
8094
8095 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8096 return (-1);
8097
8098#ifdef DEBUG_PROGRESSIVE
8099 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8100#endif
8101
8102 while (*data != 0) {
8103 if (!IS_BLANK(*data))
8104 break;
8105 data++;
8106 }
8107 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008108 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008109
8110 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8111 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008112 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008113#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008114 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008115#endif
8116
Daniel Veillard4c004142003-10-07 11:33:24 +00008117 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008118 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008119 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008120}
8121
8122/**
8123 * xmlRelaxNGValidatePopElement:
8124 * @ctxt: the RelaxNG validation context
8125 * @doc: a document instance
8126 * @elem: an element instance
8127 *
8128 * Pop the element end from the RelaxNG validation stack.
8129 *
8130 * returns 1 if no validation problem was found or 0 otherwise
8131 */
8132int
8133xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8134 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008135 xmlNodePtr elem)
8136{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008137 int ret;
8138 xmlRegExecCtxtPtr exec;
8139
Daniel Veillard4c004142003-10-07 11:33:24 +00008140 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8141 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008142#ifdef DEBUG_PROGRESSIVE
8143 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8144#endif
8145 /*
8146 * verify that we reached a terminal state of the content model.
8147 */
8148 exec = xmlRelaxNGElemPop(ctxt);
8149 ret = xmlRegExecPushString(exec, NULL, NULL);
8150 if (ret == 0) {
8151 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008152 * TODO: get some of the names needed to exit the current state of exec
8153 */
8154 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8155 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008156 } else if (ret < 0) {
8157 ret = -1;
8158 } else {
8159 ret = 1;
8160 }
8161 xmlRegFreeExecCtxt(exec);
8162#ifdef DEBUG_PROGRESSIVE
8163 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008164 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8165 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008166#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008167 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008168}
8169
8170/**
8171 * xmlRelaxNGValidateFullElement:
8172 * @ctxt: the validation context
8173 * @doc: a document instance
8174 * @elem: an element instance
8175 *
8176 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8177 * 0 and the content of the node has been expanded.
8178 *
8179 * returns 1 if no validation problem was found or -1 in case of error.
8180 */
8181int
Daniel Veillard33300b42003-04-17 09:09:19 +00008182xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8183 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008184 xmlNodePtr elem)
8185{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008186 int ret;
8187 xmlRelaxNGValidStatePtr state;
8188
Daniel Veillard4c004142003-10-07 11:33:24 +00008189 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8190 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008191#ifdef DEBUG_PROGRESSIVE
8192 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8193#endif
8194 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8195 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008196 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008197 }
8198 state->seq = elem;
8199 ctxt->state = state;
8200 ctxt->errNo = XML_RELAXNG_OK;
8201 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008202 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8203 ret = -1;
8204 else
8205 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008206 xmlRelaxNGFreeValidState(ctxt, state);
8207 ctxt->state = NULL;
8208#ifdef DEBUG_PROGRESSIVE
8209 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008210 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8211 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008212#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008213 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008214}
8215
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008216/************************************************************************
8217 * *
8218 * Generic interpreted validation implementation *
8219 * *
8220 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008221static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8222 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008223
8224/**
8225 * xmlRelaxNGSkipIgnored:
8226 * @ctxt: a schema validation context
8227 * @node: the top node.
8228 *
8229 * Skip ignorable nodes in that context
8230 *
8231 * Returns the new sibling or NULL in case of error.
8232 */
8233static xmlNodePtr
8234xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008235 xmlNodePtr node)
8236{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008237 /*
8238 * TODO complete and handle entities
8239 */
8240 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008241 ((node->type == XML_COMMENT_NODE) ||
8242 (node->type == XML_PI_NODE) ||
8243 (((node->type == XML_TEXT_NODE) ||
8244 (node->type == XML_CDATA_SECTION_NODE)) &&
8245 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8246 (IS_BLANK_NODE(node)))))) {
8247 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008248 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008249 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008250}
8251
8252/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008253 * xmlRelaxNGNormalize:
8254 * @ctxt: a schema validation context
8255 * @str: the string to normalize
8256 *
8257 * Implements the normalizeWhiteSpace( s ) function from
8258 * section 6.2.9 of the spec
8259 *
8260 * Returns the new string or NULL in case of error.
8261 */
8262static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008263xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8264{
Daniel Veillardedc91922003-01-26 00:52:04 +00008265 xmlChar *ret, *p;
8266 const xmlChar *tmp;
8267 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008268
Daniel Veillardedc91922003-01-26 00:52:04 +00008269 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008270 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008271 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008272 while (*tmp != 0)
8273 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008274 len = tmp - str;
8275
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008276 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008277 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008278 xmlRngVErrMemory(ctxt, "validating\n");
8279 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008280 }
8281 p = ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00008282 while (IS_BLANK(*str))
8283 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008284 while (*str != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008285 if (IS_BLANK(*str)) {
8286 while (IS_BLANK(*str))
8287 str++;
8288 if (*str == 0)
8289 break;
8290 *p++ = ' ';
8291 } else
8292 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008293 }
8294 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008295 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008296}
8297
8298/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008299 * xmlRelaxNGValidateDatatype:
8300 * @ctxt: a Relax-NG validation context
8301 * @value: the string value
8302 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008303 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008304 *
8305 * Validate the given value against the dataype
8306 *
8307 * Returns 0 if the validation succeeded or an error code.
8308 */
8309static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008310xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8311 const xmlChar * value,
8312 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8313{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008314 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008315 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008316 void *result = NULL;
8317 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008318
8319 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008320 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008321 }
8322 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008323 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008324 if ((define->attrs != NULL) &&
8325 (define->attrs->type == XML_RELAXNG_PARAM)) {
8326 ret =
8327 lib->check(lib->data, define->name, value, &result, node);
8328 } else {
8329 ret = lib->check(lib->data, define->name, value, NULL, node);
8330 }
8331 } else
8332 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008333 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008334 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8335 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8336 lib->freef(lib->data, result);
8337 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008338 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008339 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008340 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008341 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008342 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008343 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8344 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008345 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008346 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008347 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008348 if (lib->facet != NULL) {
8349 tmp = lib->facet(lib->data, define->name, cur->name,
8350 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008351 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008352 ret = -1;
8353 }
8354 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008355 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008356 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008357 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008358
Daniel Veillard4c004142003-10-07 11:33:24 +00008359 oldvalue = ctxt->state->value;
8360 oldendvalue = ctxt->state->endvalue;
8361 ctxt->state->value = (xmlChar *) value;
8362 ctxt->state->endvalue = NULL;
8363 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8364 ctxt->state->value = (xmlChar *) oldvalue;
8365 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008366 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008367 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008368 lib->freef(lib->data, result);
8369 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008370}
8371
8372/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008373 * xmlRelaxNGNextValue:
8374 * @ctxt: a Relax-NG validation context
8375 *
8376 * Skip to the next value when validating within a list
8377 *
8378 * Returns 0 if the operation succeeded or an error code.
8379 */
8380static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008381xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8382{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008383 xmlChar *cur;
8384
8385 cur = ctxt->state->value;
8386 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008387 ctxt->state->value = NULL;
8388 ctxt->state->endvalue = NULL;
8389 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008390 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008391 while (*cur != 0)
8392 cur++;
8393 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8394 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008395 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008396 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008397 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008398 ctxt->state->value = cur;
8399 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008400}
8401
8402/**
8403 * xmlRelaxNGValidateValueList:
8404 * @ctxt: a Relax-NG validation context
8405 * @defines: the list of definitions to verify
8406 *
8407 * Validate the given set of definitions for the current value
8408 *
8409 * Returns 0 if the validation succeeded or an error code.
8410 */
8411static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008412xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8413 xmlRelaxNGDefinePtr defines)
8414{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008415 int ret = 0;
8416
8417 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008418 ret = xmlRelaxNGValidateValue(ctxt, defines);
8419 if (ret != 0)
8420 break;
8421 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008422 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008423 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008424}
8425
8426/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008427 * xmlRelaxNGValidateValue:
8428 * @ctxt: a Relax-NG validation context
8429 * @define: the definition to verify
8430 *
8431 * Validate the given definition for the current value
8432 *
8433 * Returns 0 if the validation succeeded or an error code.
8434 */
8435static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008436xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8437 xmlRelaxNGDefinePtr define)
8438{
Daniel Veillardedc91922003-01-26 00:52:04 +00008439 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008440 xmlChar *value;
8441
8442 value = ctxt->state->value;
8443 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008444 case XML_RELAXNG_EMPTY:{
8445 if ((value != NULL) && (value[0] != 0)) {
8446 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008447
Daniel Veillard4c004142003-10-07 11:33:24 +00008448 while (IS_BLANK(value[idx]))
8449 idx++;
8450 if (value[idx] != 0)
8451 ret = -1;
8452 }
8453 break;
8454 }
8455 case XML_RELAXNG_TEXT:
8456 break;
8457 case XML_RELAXNG_VALUE:{
8458 if (!xmlStrEqual(value, define->value)) {
8459 if (define->name != NULL) {
8460 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008461
Daniel Veillard4c004142003-10-07 11:33:24 +00008462 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8463 if ((lib != NULL) && (lib->comp != NULL)) {
8464 ret = lib->comp(lib->data, define->name,
8465 define->value, define->node,
8466 (void *) define->attrs,
8467 value, ctxt->state->node);
8468 } else
8469 ret = -1;
8470 if (ret < 0) {
8471 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8472 define->name);
8473 return (-1);
8474 } else if (ret == 1) {
8475 ret = 0;
8476 } else {
8477 ret = -1;
8478 }
8479 } else {
8480 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008481
Daniel Veillard4c004142003-10-07 11:33:24 +00008482 /*
8483 * TODO: trivial optimizations are possible by
8484 * computing at compile-time
8485 */
8486 nval = xmlRelaxNGNormalize(ctxt, define->value);
8487 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008488
Daniel Veillard4c004142003-10-07 11:33:24 +00008489 if ((nval == NULL) || (nvalue == NULL) ||
8490 (!xmlStrEqual(nval, nvalue)))
8491 ret = -1;
8492 if (nval != NULL)
8493 xmlFree(nval);
8494 if (nvalue != NULL)
8495 xmlFree(nvalue);
8496 }
8497 }
8498 if (ret == 0)
8499 xmlRelaxNGNextValue(ctxt);
8500 break;
8501 }
8502 case XML_RELAXNG_DATATYPE:{
8503 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8504 ctxt->state->seq);
8505 if (ret == 0)
8506 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008507
Daniel Veillard4c004142003-10-07 11:33:24 +00008508 break;
8509 }
8510 case XML_RELAXNG_CHOICE:{
8511 xmlRelaxNGDefinePtr list = define->content;
8512 xmlChar *oldvalue;
8513
8514 oldflags = ctxt->flags;
8515 ctxt->flags |= FLAGS_IGNORABLE;
8516
8517 oldvalue = ctxt->state->value;
8518 while (list != NULL) {
8519 ret = xmlRelaxNGValidateValue(ctxt, list);
8520 if (ret == 0) {
8521 break;
8522 }
8523 ctxt->state->value = oldvalue;
8524 list = list->next;
8525 }
8526 ctxt->flags = oldflags;
8527 if (ret != 0) {
8528 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8529 xmlRelaxNGDumpValidError(ctxt);
8530 } else {
8531 if (ctxt->errNr > 0)
8532 xmlRelaxNGPopErrors(ctxt, 0);
8533 }
8534 if (ret == 0)
8535 xmlRelaxNGNextValue(ctxt);
8536 break;
8537 }
8538 case XML_RELAXNG_LIST:{
8539 xmlRelaxNGDefinePtr list = define->content;
8540 xmlChar *oldvalue, *oldend, *val, *cur;
8541
Daniel Veillard416589a2003-02-17 17:25:42 +00008542#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008543 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008544#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008545
Daniel Veillard4c004142003-10-07 11:33:24 +00008546 oldvalue = ctxt->state->value;
8547 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008548
Daniel Veillard4c004142003-10-07 11:33:24 +00008549 val = xmlStrdup(oldvalue);
8550 if (val == NULL) {
8551 val = xmlStrdup(BAD_CAST "");
8552 }
8553 if (val == NULL) {
8554 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8555 return (-1);
8556 }
8557 cur = val;
8558 while (*cur != 0) {
8559 if (IS_BLANK(*cur)) {
8560 *cur = 0;
8561 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008562#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008563 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008564#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008565 while (IS_BLANK(*cur))
8566 *cur++ = 0;
8567 } else
8568 cur++;
8569 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008570#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008571 xmlGenericError(xmlGenericErrorContext,
8572 "list value: '%s' found %d items\n",
8573 oldvalue, nb_values);
8574 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008575#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008576 ctxt->state->endvalue = cur;
8577 cur = val;
8578 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8579 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008580
Daniel Veillard4c004142003-10-07 11:33:24 +00008581 ctxt->state->value = cur;
8582
8583 while (list != NULL) {
8584 if (ctxt->state->value == ctxt->state->endvalue)
8585 ctxt->state->value = NULL;
8586 ret = xmlRelaxNGValidateValue(ctxt, list);
8587 if (ret != 0) {
8588#ifdef DEBUG_LIST
8589 xmlGenericError(xmlGenericErrorContext,
8590 "Failed to validate value: '%s' with %d rule\n",
8591 ctxt->state->value, nb_values);
8592#endif
8593 break;
8594 }
8595#ifdef DEBUG_LIST
8596 nb_values++;
8597#endif
8598 list = list->next;
8599 }
8600
8601 if ((ret == 0) && (ctxt->state->value != NULL) &&
8602 (ctxt->state->value != ctxt->state->endvalue)) {
8603 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8604 ctxt->state->value);
8605 ret = -1;
8606 }
8607 xmlFree(val);
8608 ctxt->state->value = oldvalue;
8609 ctxt->state->endvalue = oldend;
8610 break;
8611 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008612 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008613 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8614 if (ret != 0) {
8615 break;
8616 }
8617 /* no break on purpose */
8618 case XML_RELAXNG_ZEROORMORE:{
8619 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008620
Daniel Veillard4c004142003-10-07 11:33:24 +00008621 oldflags = ctxt->flags;
8622 ctxt->flags |= FLAGS_IGNORABLE;
8623 cur = ctxt->state->value;
8624 temp = NULL;
8625 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8626 (temp != cur)) {
8627 temp = cur;
8628 ret =
8629 xmlRelaxNGValidateValueList(ctxt, define->content);
8630 if (ret != 0) {
8631 ctxt->state->value = temp;
8632 ret = 0;
8633 break;
8634 }
8635 cur = ctxt->state->value;
8636 }
8637 ctxt->flags = oldflags;
8638 if (ret != 0) {
8639 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8640 xmlRelaxNGDumpValidError(ctxt);
8641 } else {
8642 if (ctxt->errNr > 0)
8643 xmlRelaxNGPopErrors(ctxt, 0);
8644 }
8645 break;
8646 }
8647 case XML_RELAXNG_EXCEPT:{
8648 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008649
Daniel Veillard4c004142003-10-07 11:33:24 +00008650 list = define->content;
8651 while (list != NULL) {
8652 ret = xmlRelaxNGValidateValue(ctxt, list);
8653 if (ret == 0) {
8654 ret = -1;
8655 break;
8656 } else
8657 ret = 0;
8658 list = list->next;
8659 }
8660 break;
8661 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008662 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008663 case XML_RELAXNG_GROUP:{
8664 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008665
Daniel Veillard4c004142003-10-07 11:33:24 +00008666 list = define->content;
8667 while (list != NULL) {
8668 ret = xmlRelaxNGValidateValue(ctxt, list);
8669 if (ret != 0) {
8670 ret = -1;
8671 break;
8672 } else
8673 ret = 0;
8674 list = list->next;
8675 }
8676 break;
8677 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008678 case XML_RELAXNG_REF:
8679 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008680 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8681 break;
8682 default:
8683 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008684 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008685 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008686}
8687
8688/**
8689 * xmlRelaxNGValidateValueContent:
8690 * @ctxt: a Relax-NG validation context
8691 * @defines: the list of definitions to verify
8692 *
8693 * Validate the given definitions for the current value
8694 *
8695 * Returns 0 if the validation succeeded or an error code.
8696 */
8697static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008698xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8699 xmlRelaxNGDefinePtr defines)
8700{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008701 int ret = 0;
8702
8703 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008704 ret = xmlRelaxNGValidateValue(ctxt, defines);
8705 if (ret != 0)
8706 break;
8707 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008708 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008709 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008710}
8711
8712/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008713 * xmlRelaxNGAttributeMatch:
8714 * @ctxt: a Relax-NG validation context
8715 * @define: the definition to check
8716 * @prop: the attribute
8717 *
8718 * Check if the attribute matches the definition nameClass
8719 *
8720 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8721 */
8722static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008723xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8724 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8725{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008726 int ret;
8727
8728 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008729 if (!xmlStrEqual(define->name, prop->name))
8730 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008731 }
8732 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008733 if (define->ns[0] == 0) {
8734 if (prop->ns != NULL)
8735 return (0);
8736 } else {
8737 if ((prop->ns == NULL) ||
8738 (!xmlStrEqual(define->ns, prop->ns->href)))
8739 return (0);
8740 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008741 }
8742 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008743 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008744 define = define->nameClass;
8745 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008746 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008747
Daniel Veillard4c004142003-10-07 11:33:24 +00008748 list = define->content;
8749 while (list != NULL) {
8750 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8751 if (ret == 1)
8752 return (0);
8753 if (ret < 0)
8754 return (ret);
8755 list = list->next;
8756 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008757 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008758 TODO}
8759 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008760}
8761
8762/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008763 * xmlRelaxNGValidateAttribute:
8764 * @ctxt: a Relax-NG validation context
8765 * @define: the definition to verify
8766 *
8767 * Validate the given attribute definition for that node
8768 *
8769 * Returns 0 if the validation succeeded or an error code.
8770 */
8771static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008772xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8773 xmlRelaxNGDefinePtr define)
8774{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008775 int ret = 0, i;
8776 xmlChar *value, *oldvalue;
8777 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008778 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008779
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008780 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008781 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008782 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008783 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8784 tmp = ctxt->state->attrs[i];
8785 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8786 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8787 (tmp->ns == NULL)) ||
8788 ((tmp->ns != NULL) &&
8789 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8790 prop = tmp;
8791 break;
8792 }
8793 }
8794 }
8795 if (prop != NULL) {
8796 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8797 oldvalue = ctxt->state->value;
8798 oldseq = ctxt->state->seq;
8799 ctxt->state->seq = (xmlNodePtr) prop;
8800 ctxt->state->value = value;
8801 ctxt->state->endvalue = NULL;
8802 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8803 if (ctxt->state->value != NULL)
8804 value = ctxt->state->value;
8805 if (value != NULL)
8806 xmlFree(value);
8807 ctxt->state->value = oldvalue;
8808 ctxt->state->seq = oldseq;
8809 if (ret == 0) {
8810 /*
8811 * flag the attribute as processed
8812 */
8813 ctxt->state->attrs[i] = NULL;
8814 ctxt->state->nbAttrLeft--;
8815 }
8816 } else {
8817 ret = -1;
8818 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008819#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008820 xmlGenericError(xmlGenericErrorContext,
8821 "xmlRelaxNGValidateAttribute(%s): %d\n",
8822 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008823#endif
8824 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008825 for (i = 0; i < ctxt->state->nbAttrs; i++) {
8826 tmp = ctxt->state->attrs[i];
8827 if ((tmp != NULL) &&
8828 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8829 prop = tmp;
8830 break;
8831 }
8832 }
8833 if (prop != NULL) {
8834 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8835 oldvalue = ctxt->state->value;
8836 oldseq = ctxt->state->seq;
8837 ctxt->state->seq = (xmlNodePtr) prop;
8838 ctxt->state->value = value;
8839 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8840 if (ctxt->state->value != NULL)
8841 value = ctxt->state->value;
8842 if (value != NULL)
8843 xmlFree(value);
8844 ctxt->state->value = oldvalue;
8845 ctxt->state->seq = oldseq;
8846 if (ret == 0) {
8847 /*
8848 * flag the attribute as processed
8849 */
8850 ctxt->state->attrs[i] = NULL;
8851 ctxt->state->nbAttrLeft--;
8852 }
8853 } else {
8854 ret = -1;
8855 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008856#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00008857 if (define->ns != NULL) {
8858 xmlGenericError(xmlGenericErrorContext,
8859 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8860 define->ns, ret);
8861 } else {
8862 xmlGenericError(xmlGenericErrorContext,
8863 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8864 ret);
8865 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008866#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008867 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008868
8869 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008870}
8871
8872/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008873 * xmlRelaxNGValidateAttributeList:
8874 * @ctxt: a Relax-NG validation context
8875 * @define: the list of definition to verify
8876 *
8877 * Validate the given node against the list of attribute definitions
8878 *
8879 * Returns 0 if the validation succeeded or an error code.
8880 */
8881static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008882xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8883 xmlRelaxNGDefinePtr defines)
8884{
Daniel Veillardce192eb2003-04-16 15:58:05 +00008885 int ret = 0, res;
8886 int needmore = 0;
8887 xmlRelaxNGDefinePtr cur;
8888
8889 cur = defines;
8890 while (cur != NULL) {
8891 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008892 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8893 ret = -1;
8894 } else
8895 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008896 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008897 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008898 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00008899 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008900 cur = defines;
8901 while (cur != NULL) {
8902 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008903 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8904 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8905 if (res < 0)
8906 ret = -1;
8907 } else {
8908 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8909 return (-1);
8910 }
8911 if (res == -1) /* continues on -2 */
8912 break;
8913 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008914 cur = cur->next;
8915 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008916
8917 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008918}
8919
8920/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008921 * xmlRelaxNGNodeMatchesList:
8922 * @node: the node
8923 * @list: a NULL terminated array of definitions
8924 *
8925 * Check if a node can be matched by one of the definitions
8926 *
8927 * Returns 1 if matches 0 otherwise
8928 */
8929static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008930xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
8931{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008932 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008933 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008934
8935 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008936 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008937
8938 cur = list[i++];
8939 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008940 if ((node->type == XML_ELEMENT_NODE) &&
8941 (cur->type == XML_RELAXNG_ELEMENT)) {
8942 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8943 if (tmp == 1)
8944 return (1);
8945 } else if (((node->type == XML_TEXT_NODE) ||
8946 (node->type == XML_CDATA_SECTION_NODE)) &&
8947 (cur->type == XML_RELAXNG_TEXT)) {
8948 return (1);
8949 }
8950 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008951 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008952 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008953}
8954
8955/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008956 * xmlRelaxNGValidateInterleave:
8957 * @ctxt: a Relax-NG validation context
8958 * @define: the definition to verify
8959 *
8960 * Validate an interleave definition for a node.
8961 *
8962 * Returns 0 if the validation succeeded or an error code.
8963 */
8964static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008965xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
8966 xmlRelaxNGDefinePtr define)
8967{
William M. Brack779af002003-08-01 15:55:39 +00008968 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008969 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008970 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008971
8972 xmlRelaxNGValidStatePtr oldstate;
8973 xmlRelaxNGPartitionPtr partitions;
8974 xmlRelaxNGInterleaveGroupPtr group = NULL;
8975 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
8976 xmlNodePtr *list = NULL, *lasts = NULL;
8977
8978 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008979 partitions = (xmlRelaxNGPartitionPtr) define->data;
8980 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008981 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008982 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
8983 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008984 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008985 /*
8986 * Optimizations for MIXED
8987 */
8988 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00008989 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008990 ctxt->flags |= FLAGS_MIXED_CONTENT;
8991 if (nbgroups == 2) {
8992 /*
8993 * this is a pure <mixed> case
8994 */
8995 if (ctxt->state != NULL)
8996 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8997 ctxt->state->seq);
8998 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
8999 ret = xmlRelaxNGValidateDefinition(ctxt,
9000 partitions->groups[1]->
9001 rule);
9002 else
9003 ret = xmlRelaxNGValidateDefinition(ctxt,
9004 partitions->groups[0]->
9005 rule);
9006 if (ret == 0) {
9007 if (ctxt->state != NULL)
9008 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9009 ctxt->state->
9010 seq);
9011 }
9012 ctxt->flags = oldflags;
9013 return (ret);
9014 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009015 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009016
9017 /*
9018 * Build arrays to store the first and last node of the chain
9019 * pertaining to each group
9020 */
9021 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9022 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009023 xmlRngVErrMemory(ctxt, "validating\n");
9024 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009025 }
9026 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9027 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9028 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009029 xmlRngVErrMemory(ctxt, "validating\n");
9030 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009031 }
9032 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9033
9034 /*
9035 * Walk the sequence of children finding the right group and
9036 * sorting them in sequences.
9037 */
9038 cur = ctxt->state->seq;
9039 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9040 start = cur;
9041 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009042 ctxt->state->seq = cur;
9043 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009044 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009045 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009046
Daniel Veillard4c004142003-10-07 11:33:24 +00009047 if ((cur->type == XML_TEXT_NODE) ||
9048 (cur->type == XML_CDATA_SECTION_NODE)) {
9049 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9050 NULL);
9051 } else if (cur->type == XML_ELEMENT_NODE) {
9052 if (cur->ns != NULL) {
9053 tmp = xmlHashLookup2(partitions->triage, cur->name,
9054 cur->ns->href);
9055 if (tmp == NULL)
9056 tmp = xmlHashLookup2(partitions->triage,
9057 BAD_CAST "#any",
9058 cur->ns->href);
9059 } else
9060 tmp =
9061 xmlHashLookup2(partitions->triage, cur->name,
9062 NULL);
9063 if (tmp == NULL)
9064 tmp =
9065 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9066 NULL);
9067 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009068
Daniel Veillard4c004142003-10-07 11:33:24 +00009069 if (tmp == NULL) {
9070 i = nbgroups;
9071 } else {
9072 i = ((long) tmp) - 1;
9073 if (partitions->flags & IS_NEEDCHECK) {
9074 group = partitions->groups[i];
9075 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9076 i = nbgroups;
9077 }
9078 }
9079 } else {
9080 for (i = 0; i < nbgroups; i++) {
9081 group = partitions->groups[i];
9082 if (group == NULL)
9083 continue;
9084 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9085 break;
9086 }
9087 }
9088 /*
9089 * We break as soon as an element not matched is found
9090 */
9091 if (i >= nbgroups) {
9092 break;
9093 }
9094 if (lasts[i] != NULL) {
9095 lasts[i]->next = cur;
9096 lasts[i] = cur;
9097 } else {
9098 list[i] = cur;
9099 lasts[i] = cur;
9100 }
9101 if (cur->next != NULL)
9102 lastchg = cur->next;
9103 else
9104 lastchg = cur;
9105 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009106 }
9107 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009108 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9109 ret = -1;
9110 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009111 }
9112 lastelem = cur;
9113 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009114 for (i = 0; i < nbgroups; i++) {
9115 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9116 group = partitions->groups[i];
9117 if (lasts[i] != NULL) {
9118 last = lasts[i]->next;
9119 lasts[i]->next = NULL;
9120 }
9121 ctxt->state->seq = list[i];
9122 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9123 if (ret != 0)
9124 break;
9125 if (ctxt->state != NULL) {
9126 cur = ctxt->state->seq;
9127 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9128 xmlRelaxNGFreeValidState(ctxt, oldstate);
9129 oldstate = ctxt->state;
9130 ctxt->state = NULL;
9131 if (cur != NULL) {
9132 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9133 ret = -1;
9134 ctxt->state = oldstate;
9135 goto done;
9136 }
9137 } else if (ctxt->states != NULL) {
9138 int j;
9139 int found = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009140
Daniel Veillard4c004142003-10-07 11:33:24 +00009141 for (j = 0; j < ctxt->states->nbState; j++) {
9142 cur = ctxt->states->tabState[j]->seq;
9143 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9144 if (cur == NULL) {
9145 found = 1;
9146 break;
9147 }
9148 }
9149 if (ctxt->states->nbState > 0) {
9150 xmlRelaxNGFreeValidState(ctxt, oldstate);
9151 oldstate =
9152 ctxt->states->tabState[ctxt->states->nbState - 1];
9153 }
9154 for (j = 0; j < ctxt->states->nbState - 1; j++) {
9155 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9156 }
9157 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9158 ctxt->states = NULL;
9159 if (found == 0) {
9160 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9161 ret = -1;
9162 ctxt->state = oldstate;
9163 goto done;
9164 }
9165 } else {
9166 ret = -1;
9167 break;
9168 }
9169 if (lasts[i] != NULL) {
9170 lasts[i]->next = last;
9171 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009172 }
9173 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009174 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009175 ctxt->state = oldstate;
9176 ctxt->state->seq = lastelem;
9177 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009178 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9179 ret = -1;
9180 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009181 }
9182
Daniel Veillard4c004142003-10-07 11:33:24 +00009183 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009184 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009185 /*
9186 * builds the next links chain from the prev one
9187 */
9188 cur = lastchg;
9189 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009190 if ((cur == start) || (cur->prev == NULL))
9191 break;
9192 cur->prev->next = cur;
9193 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009194 }
9195 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009196 if (ctxt->errNr > errNr)
9197 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009198 }
9199
9200 xmlFree(list);
9201 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009202 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009203}
9204
9205/**
9206 * xmlRelaxNGValidateDefinitionList:
9207 * @ctxt: a Relax-NG validation context
9208 * @define: the list of definition to verify
9209 *
9210 * Validate the given node content against the (list) of definitions
9211 *
9212 * Returns 0 if the validation succeeded or an error code.
9213 */
9214static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009215xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9216 xmlRelaxNGDefinePtr defines)
9217{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009218 int ret = 0, res;
9219
9220
Daniel Veillard952379b2003-03-17 15:37:12 +00009221 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009222 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9223 BAD_CAST "NULL definition list");
9224 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009225 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009226 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009227 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9228 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9229 if (res < 0)
9230 ret = -1;
9231 } else {
9232 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9233 return (-1);
9234 }
9235 if (res == -1) /* continues on -2 */
9236 break;
9237 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009238 }
9239
Daniel Veillard4c004142003-10-07 11:33:24 +00009240 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009241}
9242
9243/**
9244 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009245 * @ctxt: a Relax-NG validation context
9246 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009247 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009248 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009249 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009250 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009251 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009252 */
9253static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009254xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9255 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9256{
Daniel Veillard580ced82003-03-21 21:22:48 +00009257 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009258
Daniel Veillardfd573f12003-03-16 17:52:32 +00009259 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009260 if (!xmlStrEqual(elem->name, define->name)) {
9261 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9262 return (0);
9263 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009264 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009265 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009266 if (elem->ns == NULL) {
9267 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9268 return (0);
9269 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9270 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9271 elem->name, define->ns);
9272 return (0);
9273 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009274 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009275 (define->name == NULL)) {
9276 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9277 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009278 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009279 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9280 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009281 }
9282
9283 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009284 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009285
9286 define = define->nameClass;
9287 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009288 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009289
Daniel Veillard4c004142003-10-07 11:33:24 +00009290 if (ctxt != NULL) {
9291 oldflags = ctxt->flags;
9292 ctxt->flags |= FLAGS_IGNORABLE;
9293 }
9294
9295 list = define->content;
9296 while (list != NULL) {
9297 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9298 if (ret == 1) {
9299 if (ctxt != NULL)
9300 ctxt->flags = oldflags;
9301 return (0);
9302 }
9303 if (ret < 0) {
9304 if (ctxt != NULL)
9305 ctxt->flags = oldflags;
9306 return (ret);
9307 }
9308 list = list->next;
9309 }
9310 ret = 1;
9311 if (ctxt != NULL) {
9312 ctxt->flags = oldflags;
9313 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009314 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009315 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009316
Daniel Veillard4c004142003-10-07 11:33:24 +00009317 if (ctxt != NULL) {
9318 oldflags = ctxt->flags;
9319 ctxt->flags |= FLAGS_IGNORABLE;
9320 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009321
Daniel Veillard4c004142003-10-07 11:33:24 +00009322 list = define->nameClass;
9323 while (list != NULL) {
9324 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9325 if (ret == 1) {
9326 if (ctxt != NULL)
9327 ctxt->flags = oldflags;
9328 return (1);
9329 }
9330 if (ret < 0) {
9331 if (ctxt != NULL)
9332 ctxt->flags = oldflags;
9333 return (ret);
9334 }
9335 list = list->next;
9336 }
9337 if (ctxt != NULL) {
9338 if (ret != 0) {
9339 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9340 xmlRelaxNGDumpValidError(ctxt);
9341 } else {
9342 if (ctxt->errNr > 0)
9343 xmlRelaxNGPopErrors(ctxt, 0);
9344 }
9345 }
9346 ret = 0;
9347 if (ctxt != NULL) {
9348 ctxt->flags = oldflags;
9349 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009350 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009351 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009352 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009353 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009354}
9355
9356/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009357 * xmlRelaxNGBestState:
9358 * @ctxt: a Relax-NG validation context
9359 *
9360 * Find the "best" state in the ctxt->states list of states to report
9361 * errors about. I.e. a state with no element left in the child list
9362 * or the one with the less attributes left.
9363 * This is called only if a falidation error was detected
9364 *
9365 * Returns the index of the "best" state or -1 in case of error
9366 */
9367static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009368xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9369{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009370 xmlRelaxNGValidStatePtr state;
9371 int i, tmp;
9372 int best = -1;
9373 int value = 1000000;
9374
9375 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9376 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009377 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009378
Daniel Veillard4c004142003-10-07 11:33:24 +00009379 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009380 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009381 if (state == NULL)
9382 continue;
9383 if (state->seq != NULL) {
9384 if ((best == -1) || (value > 100000)) {
9385 value = 100000;
9386 best = i;
9387 }
9388 } else {
9389 tmp = state->nbAttrLeft;
9390 if ((best == -1) || (value > tmp)) {
9391 value = tmp;
9392 best = i;
9393 }
9394 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009395 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009396 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009397}
9398
9399/**
9400 * xmlRelaxNGLogBestError:
9401 * @ctxt: a Relax-NG validation context
9402 *
9403 * Find the "best" state in the ctxt->states list of states to report
9404 * errors about and log it.
9405 */
9406static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009407xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9408{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009409 int best;
9410
9411 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9412 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009413 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009414
9415 best = xmlRelaxNGBestState(ctxt);
9416 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009417 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009418
Daniel Veillard4c004142003-10-07 11:33:24 +00009419 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009420 }
9421}
9422
9423/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009424 * xmlRelaxNGValidateElementEnd:
9425 * @ctxt: a Relax-NG validation context
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009426 * @log: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009427 *
9428 * Validate the end of the element, implements check that
9429 * there is nothing left not consumed in the element content
9430 * or in the attribute list.
9431 *
9432 * Returns 0 if the validation succeeded or an error code.
9433 */
9434static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009435xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log)
9436{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009437 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009438 xmlRelaxNGValidStatePtr state;
9439
9440 state = ctxt->state;
9441 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009442 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9443 if (state->seq != NULL) {
9444 if (log) {
9445 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9446 state->node->name, state->seq->name);
9447 }
9448 return (-1);
9449 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009450 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009451 for (i = 0; i < state->nbAttrs; i++) {
9452 if (state->attrs[i] != NULL) {
9453 if (log) {
9454 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9455 state->attrs[i]->name, state->node->name);
9456 }
9457 return (-1 - i);
9458 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009459 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009460 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009461}
9462
9463/**
9464 * xmlRelaxNGValidateState:
9465 * @ctxt: a Relax-NG validation context
9466 * @define: the definition to verify
9467 *
9468 * Validate the current state against the definition
9469 *
9470 * Returns 0 if the validation succeeded or an error code.
9471 */
9472static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009473xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9474 xmlRelaxNGDefinePtr define)
9475{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009476 xmlNodePtr node;
9477 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009478 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009479
9480 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009481 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9482 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009483 }
9484
9485 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009486 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009487 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009488 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009489 }
9490#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009491 for (i = 0; i < ctxt->depth; i++)
9492 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009493 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009494 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009495 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009496 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009497 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009498 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009499 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009500 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009501#endif
9502 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009503 switch (define->type) {
9504 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009505 node = xmlRelaxNGSkipIgnored(ctxt, node);
9506 ret = 0;
9507 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009508 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009509 ret = -1;
9510 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009511 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009512 while ((node != NULL) &&
9513 ((node->type == XML_TEXT_NODE) ||
9514 (node->type == XML_COMMENT_NODE) ||
9515 (node->type == XML_PI_NODE) ||
9516 (node->type == XML_CDATA_SECTION_NODE)))
9517 node = node->next;
9518 ctxt->state->seq = node;
9519 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009520 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009521 errNr = ctxt->errNr;
9522 node = xmlRelaxNGSkipIgnored(ctxt, node);
9523 if (node == NULL) {
9524 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9525 ret = -1;
9526 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9527 xmlRelaxNGDumpValidError(ctxt);
9528 break;
9529 }
9530 if (node->type != XML_ELEMENT_NODE) {
9531 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9532 ret = -1;
9533 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9534 xmlRelaxNGDumpValidError(ctxt);
9535 break;
9536 }
9537 /*
9538 * This node was already validated successfully against
9539 * this definition.
9540 */
9541 if (node->_private == define) {
9542 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9543 if (ctxt->errNr > errNr)
9544 xmlRelaxNGPopErrors(ctxt, errNr);
9545 if (ctxt->errNr != 0) {
9546 while ((ctxt->err != NULL) &&
9547 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9548 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9549 ||
9550 ((ctxt->err->err ==
9551 XML_RELAXNG_ERR_ELEMEXTRANS)
9552 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9553 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9554 || (ctxt->err->err ==
9555 XML_RELAXNG_ERR_NOTELEM)))
9556 xmlRelaxNGValidErrorPop(ctxt);
9557 }
9558 break;
9559 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009560
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009561 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9562 if (ret <= 0) {
9563 ret = -1;
9564 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9565 xmlRelaxNGDumpValidError(ctxt);
9566 break;
9567 }
9568 ret = 0;
9569 if (ctxt->errNr != 0) {
9570 if (ctxt->errNr > errNr)
9571 xmlRelaxNGPopErrors(ctxt, errNr);
9572 while ((ctxt->err != NULL) &&
9573 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9574 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9575 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9576 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9577 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9578 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9579 xmlRelaxNGValidErrorPop(ctxt);
9580 }
9581 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009582
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009583 oldflags = ctxt->flags;
9584 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9585 ctxt->flags -= FLAGS_MIXED_CONTENT;
9586 }
9587 state = xmlRelaxNGNewValidState(ctxt, node);
9588 if (state == NULL) {
9589 ret = -1;
9590 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9591 xmlRelaxNGDumpValidError(ctxt);
9592 break;
9593 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009594
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009595 oldstate = ctxt->state;
9596 ctxt->state = state;
9597 if (define->attrs != NULL) {
9598 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9599 if (tmp != 0) {
9600 ret = -1;
9601 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9602 }
9603 }
9604 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009605 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9606 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9607 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009608
Daniel Veillard4c004142003-10-07 11:33:24 +00009609 nstate = xmlRelaxNGNewValidState(ctxt, node);
9610 ctxt->state = nstate;
9611 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009612
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009613 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9614 define->contModel,
9615 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009616 nseq = ctxt->state->seq;
9617 ctxt->state = tmpstate;
9618 ctxt->states = tmpstates;
9619 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009620
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009621#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009622 xmlGenericError(xmlGenericErrorContext,
9623 "Validating content of '%s' : %d\n",
9624 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009625#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009626 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009627 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009628
9629 if (ctxt->states != NULL) {
9630 tmp = -1;
9631
Daniel Veillardce192eb2003-04-16 15:58:05 +00009632 for (i = 0; i < ctxt->states->nbState; i++) {
9633 state = ctxt->states->tabState[i];
9634 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009635 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009636
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009637 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009638 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009639 break;
9640 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009641 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009642 if (tmp != 0) {
9643 /*
9644 * validation error, log the message for the "best" one
9645 */
9646 ctxt->flags |= FLAGS_IGNORABLE;
9647 xmlRelaxNGLogBestError(ctxt);
9648 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009649 for (i = 0; i < ctxt->states->nbState; i++) {
9650 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009651 ctxt->states->
9652 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009653 }
9654 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9655 ctxt->flags = oldflags;
9656 ctxt->states = NULL;
9657 if ((ret == 0) && (tmp == -1))
9658 ret = -1;
9659 } else {
9660 state = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009661 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009662 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009663 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009664 xmlRelaxNGFreeValidState(ctxt, state);
9665 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009666 } else {
9667 if (define->content != NULL) {
9668 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009669 define->
9670 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009671 if (tmp != 0) {
9672 ret = -1;
9673 if (ctxt->state == NULL) {
9674 ctxt->state = oldstate;
9675 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9676 node->name);
9677 ctxt->state = NULL;
9678 } else {
9679 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9680 node->name);
9681 }
9682
9683 }
9684 }
9685 if (ctxt->states != NULL) {
9686 tmp = -1;
9687
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009688 for (i = 0; i < ctxt->states->nbState; i++) {
9689 state = ctxt->states->tabState[i];
9690 ctxt->state = state;
9691
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009692 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009693 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009694 break;
9695 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009696 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009697 if (tmp != 0) {
9698 /*
9699 * validation error, log the message for the "best" one
9700 */
9701 ctxt->flags |= FLAGS_IGNORABLE;
9702 xmlRelaxNGLogBestError(ctxt);
9703 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009704 for (i = 0; i < ctxt->states->nbState; i++) {
9705 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009706 ctxt->states->
9707 tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009708 }
9709 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9710 ctxt->flags = oldflags;
9711 ctxt->states = NULL;
9712 if ((ret == 0) && (tmp == -1))
9713 ret = -1;
9714 } else {
9715 state = ctxt->state;
9716 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009717 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009718 xmlRelaxNGFreeValidState(ctxt, state);
9719 }
9720 }
9721 if (ret == 0) {
9722 node->_private = define;
9723 }
9724 ctxt->flags = oldflags;
9725 ctxt->state = oldstate;
9726 if (oldstate != NULL)
9727 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9728 if (ret != 0) {
9729 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9730 xmlRelaxNGDumpValidError(ctxt);
9731 ret = 0;
9732 } else {
9733 ret = -2;
9734 }
9735 } else {
9736 if (ctxt->errNr > errNr)
9737 xmlRelaxNGPopErrors(ctxt, errNr);
9738 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009739
9740#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009741 xmlGenericError(xmlGenericErrorContext,
9742 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9743 node->name, ret);
9744 if (oldstate == NULL)
9745 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9746 else if (oldstate->seq == NULL)
9747 xmlGenericError(xmlGenericErrorContext, ": done\n");
9748 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9749 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9750 oldstate->seq->name);
9751 else
9752 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9753 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009754#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009755 break;
9756 case XML_RELAXNG_OPTIONAL:{
9757 errNr = ctxt->errNr;
9758 oldflags = ctxt->flags;
9759 ctxt->flags |= FLAGS_IGNORABLE;
9760 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9761 ret =
9762 xmlRelaxNGValidateDefinitionList(ctxt,
9763 define->content);
9764 if (ret != 0) {
9765 if (ctxt->state != NULL)
9766 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9767 ctxt->state = oldstate;
9768 ctxt->flags = oldflags;
9769 ret = 0;
9770 if (ctxt->errNr > errNr)
9771 xmlRelaxNGPopErrors(ctxt, errNr);
9772 break;
9773 }
9774 if (ctxt->states != NULL) {
9775 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9776 } else {
9777 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9778 if (ctxt->states == NULL) {
9779 xmlRelaxNGFreeValidState(ctxt, oldstate);
9780 ctxt->flags = oldflags;
9781 ret = -1;
9782 if (ctxt->errNr > errNr)
9783 xmlRelaxNGPopErrors(ctxt, errNr);
9784 break;
9785 }
9786 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9787 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9788 ctxt->state = NULL;
9789 }
9790 ctxt->flags = oldflags;
9791 ret = 0;
9792 if (ctxt->errNr > errNr)
9793 xmlRelaxNGPopErrors(ctxt, errNr);
9794 break;
9795 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009796 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009797 errNr = ctxt->errNr;
9798 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9799 if (ret != 0) {
9800 break;
9801 }
9802 if (ctxt->errNr > errNr)
9803 xmlRelaxNGPopErrors(ctxt, errNr);
9804 /* no break on purpose */
9805 case XML_RELAXNG_ZEROORMORE:{
9806 int progress;
9807 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9808 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009809
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009810 errNr = ctxt->errNr;
9811 res = xmlRelaxNGNewStates(ctxt, 1);
9812 if (res == NULL) {
9813 ret = -1;
9814 break;
9815 }
9816 /*
9817 * All the input states are also exit states
9818 */
9819 if (ctxt->state != NULL) {
9820 xmlRelaxNGAddStates(ctxt, res,
9821 xmlRelaxNGCopyValidState(ctxt,
9822 ctxt->
9823 state));
9824 } else {
9825 for (j = 0; j < ctxt->states->nbState; j++) {
9826 xmlRelaxNGAddStates(ctxt, res,
9827 xmlRelaxNGCopyValidState(ctxt,
9828 ctxt->
9829 states->
9830 tabState
9831 [j]));
9832 }
9833 }
9834 oldflags = ctxt->flags;
9835 ctxt->flags |= FLAGS_IGNORABLE;
9836 do {
9837 progress = 0;
9838 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009839
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009840 if (ctxt->states != NULL) {
9841 states = ctxt->states;
9842 for (i = 0; i < states->nbState; i++) {
9843 ctxt->state = states->tabState[i];
9844 ctxt->states = NULL;
9845 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9846 define->
9847 content);
9848 if (ret == 0) {
9849 if (ctxt->state != NULL) {
9850 tmp = xmlRelaxNGAddStates(ctxt, res,
9851 ctxt->state);
9852 ctxt->state = NULL;
9853 if (tmp == 1)
9854 progress = 1;
9855 } else if (ctxt->states != NULL) {
9856 for (j = 0; j < ctxt->states->nbState;
9857 j++) {
9858 tmp =
9859 xmlRelaxNGAddStates(ctxt, res,
9860 ctxt->
9861 states->
9862 tabState
9863 [j]);
9864 if (tmp == 1)
9865 progress = 1;
9866 }
9867 xmlRelaxNGFreeStates(ctxt,
9868 ctxt->states);
9869 ctxt->states = NULL;
9870 }
9871 } else {
9872 if (ctxt->state != NULL) {
9873 xmlRelaxNGFreeValidState(ctxt,
9874 ctxt->state);
9875 ctxt->state = NULL;
9876 }
9877 }
9878 }
9879 } else {
9880 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9881 define->
9882 content);
9883 if (ret != 0) {
9884 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9885 ctxt->state = NULL;
9886 } else {
9887 base = res->nbState;
9888 if (ctxt->state != NULL) {
9889 tmp = xmlRelaxNGAddStates(ctxt, res,
9890 ctxt->state);
9891 ctxt->state = NULL;
9892 if (tmp == 1)
9893 progress = 1;
9894 } else if (ctxt->states != NULL) {
9895 for (j = 0; j < ctxt->states->nbState; j++) {
9896 tmp = xmlRelaxNGAddStates(ctxt, res,
9897 ctxt->
9898 states->
9899 tabState[j]);
9900 if (tmp == 1)
9901 progress = 1;
9902 }
9903 if (states == NULL) {
9904 states = ctxt->states;
9905 } else {
9906 xmlRelaxNGFreeStates(ctxt,
9907 ctxt->states);
9908 }
9909 ctxt->states = NULL;
9910 }
9911 }
9912 }
9913 if (progress) {
9914 /*
9915 * Collect all the new nodes added at that step
9916 * and make them the new node set
9917 */
9918 if (res->nbState - base == 1) {
9919 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9920 res->
9921 tabState
9922 [base]);
9923 } else {
9924 if (states == NULL) {
9925 xmlRelaxNGNewStates(ctxt,
9926 res->nbState - base);
9927 }
9928 states->nbState = 0;
9929 for (i = base; i < res->nbState; i++)
9930 xmlRelaxNGAddStates(ctxt, states,
9931 xmlRelaxNGCopyValidState
9932 (ctxt,
9933 res->tabState[i]));
9934 ctxt->states = states;
9935 }
9936 }
9937 } while (progress == 1);
9938 if (states != NULL) {
9939 xmlRelaxNGFreeStates(ctxt, states);
9940 }
9941 ctxt->states = res;
9942 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009943#if 0
9944 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00009945 * errors may have to be propagated back...
9946 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009947 if (ctxt->errNr > errNr)
9948 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009949#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009950 ret = 0;
9951 break;
9952 }
9953 case XML_RELAXNG_CHOICE:{
9954 xmlRelaxNGDefinePtr list = NULL;
9955 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009956
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009957 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009958
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009959 errNr = ctxt->errNr;
9960 if ((define->dflags & IS_TRIABLE)
9961 && (define->data != NULL)) {
9962 xmlHashTablePtr triage =
9963 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +00009964
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009965 /*
9966 * Something we can optimize cleanly there is only one
9967 * possble branch out !
9968 */
9969 if (node == NULL) {
9970 ret = -1;
9971 break;
9972 }
9973 if ((node->type == XML_TEXT_NODE) ||
9974 (node->type == XML_CDATA_SECTION_NODE)) {
9975 list =
9976 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
9977 } else if (node->type == XML_ELEMENT_NODE) {
9978 if (node->ns != NULL) {
9979 list = xmlHashLookup2(triage, node->name,
9980 node->ns->href);
9981 if (list == NULL)
9982 list =
9983 xmlHashLookup2(triage, BAD_CAST "#any",
9984 node->ns->href);
9985 } else
9986 list =
9987 xmlHashLookup2(triage, node->name, NULL);
9988 if (list == NULL)
9989 list =
9990 xmlHashLookup2(triage, BAD_CAST "#any",
9991 NULL);
9992 }
9993 if (list == NULL) {
9994 ret = -1;
9995 break;
9996 }
9997 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9998 if (ret == 0) {
9999 }
10000 break;
10001 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010002
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010003 list = define->content;
10004 oldflags = ctxt->flags;
10005 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010006
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010007 while (list != NULL) {
10008 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10009 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10010 if (ret == 0) {
10011 if (states == NULL) {
10012 states = xmlRelaxNGNewStates(ctxt, 1);
10013 }
10014 if (ctxt->state != NULL) {
10015 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10016 } else if (ctxt->states != NULL) {
10017 for (i = 0; i < ctxt->states->nbState; i++) {
10018 xmlRelaxNGAddStates(ctxt, states,
10019 ctxt->states->
10020 tabState[i]);
10021 }
10022 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10023 ctxt->states = NULL;
10024 }
10025 } else {
10026 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10027 }
10028 ctxt->state = oldstate;
10029 list = list->next;
10030 }
10031 if (states != NULL) {
10032 xmlRelaxNGFreeValidState(ctxt, oldstate);
10033 ctxt->states = states;
10034 ctxt->state = NULL;
10035 ret = 0;
10036 } else {
10037 ctxt->states = NULL;
10038 }
10039 ctxt->flags = oldflags;
10040 if (ret != 0) {
10041 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10042 xmlRelaxNGDumpValidError(ctxt);
10043 }
10044 } else {
10045 if (ctxt->errNr > errNr)
10046 xmlRelaxNGPopErrors(ctxt, errNr);
10047 }
10048 break;
10049 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010050 case XML_RELAXNG_DEF:
10051 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010052 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10053 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010054 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010055 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10056 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010057 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010058 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10059 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010060 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010061 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010062 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010063 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010064 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010065 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10066 break;
10067 case XML_RELAXNG_DATATYPE:{
10068 xmlNodePtr child;
10069 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010070
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010071 child = node;
10072 while (child != NULL) {
10073 if (child->type == XML_ELEMENT_NODE) {
10074 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10075 node->parent->name);
10076 ret = -1;
10077 break;
10078 } else if ((child->type == XML_TEXT_NODE) ||
10079 (child->type == XML_CDATA_SECTION_NODE)) {
10080 content = xmlStrcat(content, child->content);
10081 }
10082 /* TODO: handle entities ... */
10083 child = child->next;
10084 }
10085 if (ret == -1) {
10086 if (content != NULL)
10087 xmlFree(content);
10088 break;
10089 }
10090 if (content == NULL) {
10091 content = xmlStrdup(BAD_CAST "");
10092 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010093 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010094 ret = -1;
10095 break;
10096 }
10097 }
10098 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10099 ctxt->state->seq);
10100 if (ret == -1) {
10101 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10102 } else if (ret == 0) {
10103 ctxt->state->seq = NULL;
10104 }
10105 if (content != NULL)
10106 xmlFree(content);
10107 break;
10108 }
10109 case XML_RELAXNG_VALUE:{
10110 xmlChar *content = NULL;
10111 xmlChar *oldvalue;
10112 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010113
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010114 child = node;
10115 while (child != NULL) {
10116 if (child->type == XML_ELEMENT_NODE) {
10117 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10118 node->parent->name);
10119 ret = -1;
10120 break;
10121 } else if ((child->type == XML_TEXT_NODE) ||
10122 (child->type == XML_CDATA_SECTION_NODE)) {
10123 content = xmlStrcat(content, child->content);
10124 }
10125 /* TODO: handle entities ... */
10126 child = child->next;
10127 }
10128 if (ret == -1) {
10129 if (content != NULL)
10130 xmlFree(content);
10131 break;
10132 }
10133 if (content == NULL) {
10134 content = xmlStrdup(BAD_CAST "");
10135 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010136 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010137 ret = -1;
10138 break;
10139 }
10140 }
10141 oldvalue = ctxt->state->value;
10142 ctxt->state->value = content;
10143 ret = xmlRelaxNGValidateValue(ctxt, define);
10144 ctxt->state->value = oldvalue;
10145 if (ret == -1) {
10146 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10147 } else if (ret == 0) {
10148 ctxt->state->seq = NULL;
10149 }
10150 if (content != NULL)
10151 xmlFree(content);
10152 break;
10153 }
10154 case XML_RELAXNG_LIST:{
10155 xmlChar *content;
10156 xmlNodePtr child;
10157 xmlChar *oldvalue, *oldendvalue;
10158 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010159
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010160 /*
10161 * Make sure it's only text nodes
10162 */
10163
10164 content = NULL;
10165 child = node;
10166 while (child != NULL) {
10167 if (child->type == XML_ELEMENT_NODE) {
10168 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10169 node->parent->name);
10170 ret = -1;
10171 break;
10172 } else if ((child->type == XML_TEXT_NODE) ||
10173 (child->type == XML_CDATA_SECTION_NODE)) {
10174 content = xmlStrcat(content, child->content);
10175 }
10176 /* TODO: handle entities ... */
10177 child = child->next;
10178 }
10179 if (ret == -1) {
10180 if (content != NULL)
10181 xmlFree(content);
10182 break;
10183 }
10184 if (content == NULL) {
10185 content = xmlStrdup(BAD_CAST "");
10186 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010187 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010188 ret = -1;
10189 break;
10190 }
10191 }
10192 len = xmlStrlen(content);
10193 oldvalue = ctxt->state->value;
10194 oldendvalue = ctxt->state->endvalue;
10195 ctxt->state->value = content;
10196 ctxt->state->endvalue = content + len;
10197 ret = xmlRelaxNGValidateValue(ctxt, define);
10198 ctxt->state->value = oldvalue;
10199 ctxt->state->endvalue = oldendvalue;
10200 if (ret == -1) {
10201 VALID_ERR(XML_RELAXNG_ERR_LIST);
10202 } else if ((ret == 0) && (node != NULL)) {
10203 ctxt->state->seq = node->next;
10204 }
10205 if (content != NULL)
10206 xmlFree(content);
10207 break;
10208 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010209 case XML_RELAXNG_EXCEPT:
10210 case XML_RELAXNG_PARAM:
10211 TODO ret = -1;
10212 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010213 }
10214 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010215#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010216 for (i = 0; i < ctxt->depth; i++)
10217 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010218 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010219 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010220 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010221 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010222 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010223 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010224 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010225 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010226#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010227 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010228}
10229
10230/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010231 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010232 * @ctxt: a Relax-NG validation context
10233 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010234 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010235 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010236 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010237 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010238 */
10239static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010240xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10241 xmlRelaxNGDefinePtr define)
10242{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010243 xmlRelaxNGStatesPtr states, res;
10244 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010245
Daniel Veillardfd573f12003-03-16 17:52:32 +000010246 /*
10247 * We should NOT have both ctxt->state and ctxt->states
10248 */
10249 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010250 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10251 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010252 }
10253
10254 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010255 if (ctxt->states != NULL) {
10256 ctxt->state = ctxt->states->tabState[0];
10257 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10258 ctxt->states = NULL;
10259 }
10260 ret = xmlRelaxNGValidateState(ctxt, define);
10261 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10262 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10263 ctxt->state = NULL;
10264 }
10265 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10266 ctxt->state = ctxt->states->tabState[0];
10267 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10268 ctxt->states = NULL;
10269 }
10270 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010271 }
10272
10273 states = ctxt->states;
10274 ctxt->states = NULL;
10275 res = NULL;
10276 j = 0;
10277 oldflags = ctxt->flags;
10278 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010279 for (i = 0; i < states->nbState; i++) {
10280 ctxt->state = states->tabState[i];
10281 ctxt->states = NULL;
10282 ret = xmlRelaxNGValidateState(ctxt, define);
10283 /*
10284 * We should NOT have both ctxt->state and ctxt->states
10285 */
10286 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10287 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10288 ctxt->state = NULL;
10289 }
10290 if (ret == 0) {
10291 if (ctxt->states == NULL) {
10292 if (res != NULL) {
10293 /* add the state to the container */
10294 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10295 ctxt->state = NULL;
10296 } else {
10297 /* add the state directly in states */
10298 states->tabState[j++] = ctxt->state;
10299 ctxt->state = NULL;
10300 }
10301 } else {
10302 if (res == NULL) {
10303 /* make it the new container and copy other results */
10304 res = ctxt->states;
10305 ctxt->states = NULL;
10306 for (k = 0; k < j; k++)
10307 xmlRelaxNGAddStates(ctxt, res,
10308 states->tabState[k]);
10309 } else {
10310 /* add all the new results to res and reff the container */
10311 for (k = 0; k < ctxt->states->nbState; k++)
10312 xmlRelaxNGAddStates(ctxt, res,
10313 ctxt->states->tabState[k]);
10314 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10315 ctxt->states = NULL;
10316 }
10317 }
10318 } else {
10319 if (ctxt->state != NULL) {
10320 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10321 ctxt->state = NULL;
10322 } else if (ctxt->states != NULL) {
10323 for (k = 0; k < ctxt->states->nbState; k++)
10324 xmlRelaxNGFreeValidState(ctxt,
10325 ctxt->states->tabState[k]);
10326 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10327 ctxt->states = NULL;
10328 }
10329 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010330 }
10331 ctxt->flags = oldflags;
10332 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010333 xmlRelaxNGFreeStates(ctxt, states);
10334 ctxt->states = res;
10335 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010336 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010337 states->nbState = j;
10338 ctxt->states = states;
10339 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010340 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010341 ctxt->state = states->tabState[0];
10342 xmlRelaxNGFreeStates(ctxt, states);
10343 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010344 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010345 ret = -1;
10346 xmlRelaxNGFreeStates(ctxt, states);
10347 if (ctxt->states != NULL) {
10348 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10349 ctxt->states = NULL;
10350 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010351 }
10352 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010353 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10354 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010355 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010356 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010357}
10358
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010359/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010360 * xmlRelaxNGValidateDocument:
10361 * @ctxt: a Relax-NG validation context
10362 * @doc: the document
10363 *
10364 * Validate the given document
10365 *
10366 * Returns 0 if the validation succeeded or an error code.
10367 */
10368static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010369xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10370{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010371 int ret;
10372 xmlRelaxNGPtr schema;
10373 xmlRelaxNGGrammarPtr grammar;
10374 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010375 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010376
10377 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010378 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010379
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010380 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010381 schema = ctxt->schema;
10382 grammar = schema->topgrammar;
10383 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010384 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10385 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010386 }
10387 state = xmlRelaxNGNewValidState(ctxt, NULL);
10388 ctxt->state = state;
10389 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010390 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010391 state = ctxt->state;
10392 node = state->seq;
10393 node = xmlRelaxNGSkipIgnored(ctxt, node);
10394 if (node != NULL) {
10395 if (ret != -1) {
10396 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10397 ret = -1;
10398 }
10399 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010400 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010401 int i;
10402 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010403
Daniel Veillard4c004142003-10-07 11:33:24 +000010404 for (i = 0; i < ctxt->states->nbState; i++) {
10405 state = ctxt->states->tabState[i];
10406 node = state->seq;
10407 node = xmlRelaxNGSkipIgnored(ctxt, node);
10408 if (node == NULL)
10409 tmp = 0;
10410 xmlRelaxNGFreeValidState(ctxt, state);
10411 }
10412 if (tmp == -1) {
10413 if (ret != -1) {
10414 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10415 ret = -1;
10416 }
10417 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010418 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010419 if (ctxt->state != NULL) {
10420 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010421 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010422 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010423 if (ret != 0)
10424 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010425#ifdef DEBUG
10426 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010427 ctxt->error(ctxt->userData,
10428 "%d Extra error messages left on stack !\n",
10429 ctxt->errNr);
10430 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010431 }
10432#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010433 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010434 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010435
Daniel Veillard4c004142003-10-07 11:33:24 +000010436 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10437 vctxt.valid = 1;
10438 vctxt.error = ctxt->error;
10439 vctxt.warning = ctxt->warning;
10440 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010441
Daniel Veillard4c004142003-10-07 11:33:24 +000010442 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10443 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010444 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010445 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010446 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010447
Daniel Veillard4c004142003-10-07 11:33:24 +000010448 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010449}
10450
Daniel Veillardfd573f12003-03-16 17:52:32 +000010451/************************************************************************
10452 * *
10453 * Validation interfaces *
10454 * *
10455 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010456
Daniel Veillard6eadf632003-01-23 18:29:16 +000010457/**
10458 * xmlRelaxNGNewValidCtxt:
10459 * @schema: a precompiled XML RelaxNGs
10460 *
10461 * Create an XML RelaxNGs validation context based on the given schema
10462 *
10463 * Returns the validation context or NULL in case of error
10464 */
10465xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010466xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10467{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010468 xmlRelaxNGValidCtxtPtr ret;
10469
10470 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10471 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010472 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010473 return (NULL);
10474 }
10475 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10476 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010477 ret->error = xmlGenericError;
10478 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010479 ret->errNr = 0;
10480 ret->errMax = 0;
10481 ret->err = NULL;
10482 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010483 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010484 ret->states = NULL;
10485 ret->freeState = NULL;
10486 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010487 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010488 return (ret);
10489}
10490
10491/**
10492 * xmlRelaxNGFreeValidCtxt:
10493 * @ctxt: the schema validation context
10494 *
10495 * Free the resources associated to the schema validation context
10496 */
10497void
Daniel Veillard4c004142003-10-07 11:33:24 +000010498xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10499{
Daniel Veillard798024a2003-03-19 10:36:09 +000010500 int k;
10501
Daniel Veillard6eadf632003-01-23 18:29:16 +000010502 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010503 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010504 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010505 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010506 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010507 for (k = 0; k < ctxt->freeState->nbState; k++) {
10508 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10509 }
10510 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010511 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010512 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010513 for (k = 0; k < ctxt->freeStatesNr; k++) {
10514 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10515 }
10516 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010517 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010518 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010519 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010520 if (ctxt->elemTab != NULL) {
10521 xmlRegExecCtxtPtr exec;
10522
Daniel Veillard4c004142003-10-07 11:33:24 +000010523 exec = xmlRelaxNGElemPop(ctxt);
10524 while (exec != NULL) {
10525 xmlRegFreeExecCtxt(exec);
10526 exec = xmlRelaxNGElemPop(ctxt);
10527 }
10528 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010529 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010530 xmlFree(ctxt);
10531}
10532
10533/**
10534 * xmlRelaxNGSetValidErrors:
10535 * @ctxt: a Relax-NG validation context
10536 * @err: the error function
10537 * @warn: the warning function
10538 * @ctx: the functions context
10539 *
10540 * Set the error and warning callback informations
10541 */
10542void
10543xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010544 xmlRelaxNGValidityErrorFunc err,
10545 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10546{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010547 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010548 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010549 ctxt->error = err;
10550 ctxt->warning = warn;
10551 ctxt->userData = ctx;
10552}
10553
10554/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010555 * xmlRelaxNGGetValidErrors:
10556 * @ctxt: a Relax-NG validation context
10557 * @err: the error function result
10558 * @warn: the warning function result
10559 * @ctx: the functions context result
10560 *
10561 * Get the error and warning callback informations
10562 *
10563 * Returns -1 in case of error and 0 otherwise
10564 */
10565int
10566xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010567 xmlRelaxNGValidityErrorFunc * err,
10568 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10569{
Daniel Veillard409a8142003-07-18 15:16:57 +000010570 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010571 return (-1);
10572 if (err != NULL)
10573 *err = ctxt->error;
10574 if (warn != NULL)
10575 *warn = ctxt->warning;
10576 if (ctx != NULL)
10577 *ctx = ctxt->userData;
10578 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010579}
10580
10581/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010582 * xmlRelaxNGValidateDoc:
10583 * @ctxt: a Relax-NG validation context
10584 * @doc: a parsed document tree
10585 *
10586 * Validate a document tree in memory.
10587 *
10588 * Returns 0 if the document is valid, a positive error code
10589 * number otherwise and -1 in case of internal or API error.
10590 */
10591int
Daniel Veillard4c004142003-10-07 11:33:24 +000010592xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10593{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010594 int ret;
10595
10596 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010597 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010598
10599 ctxt->doc = doc;
10600
10601 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010602 /*
10603 * TODO: build error codes
10604 */
10605 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010606 return (1);
10607 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010608}
10609
10610#endif /* LIBXML_SCHEMAS_ENABLED */