blob: a0ddcc6d036aff73b26d4be011b61f8dc237bca6 [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 Veillard23a47d62008-06-25 04:11:24 +000048#if 0
Daniel Veillard87254c82006-02-19 15:27:17 +000049#define DEBUG 1
Daniel Veillard4c004142003-10-07 11:33:24 +000050
Daniel Veillard87254c82006-02-19 15:27:17 +000051#define DEBUG_GRAMMAR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000052
Daniel Veillard87254c82006-02-19 15:27:17 +000053#define DEBUG_CONTENT 1
Daniel Veillard4c004142003-10-07 11:33:24 +000054
Daniel Veillard87254c82006-02-19 15:27:17 +000055#define DEBUG_TYPE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000056
Daniel Veillard87254c82006-02-19 15:27:17 +000057#define DEBUG_VALID 1
Daniel Veillard4c004142003-10-07 11:33:24 +000058
Daniel Veillard87254c82006-02-19 15:27:17 +000059#define DEBUG_INTERLEAVE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000060
Daniel Veillard87254c82006-02-19 15:27:17 +000061#define DEBUG_LIST 1
Daniel Veillard4c004142003-10-07 11:33:24 +000062
Daniel Veillard87254c82006-02-19 15:27:17 +000063#define DEBUG_INCLUDE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000064
Daniel Veillard87254c82006-02-19 15:27:17 +000065#define DEBUG_ERROR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000066
Daniel Veillard87254c82006-02-19 15:27:17 +000067#define DEBUG_COMPILE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000068
Daniel Veillard87254c82006-02-19 15:27:17 +000069#define DEBUG_PROGRESSIVE 1
70#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +000071
Daniel Veillard5f1946a2003-03-31 16:38:16 +000072#define MAX_ERROR 5
73
Daniel Veillard6eadf632003-01-23 18:29:16 +000074#define TODO \
75 xmlGenericError(xmlGenericErrorContext, \
76 "Unimplemented block at %s:%d\n", \
77 __FILE__, __LINE__);
78
79typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
80typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
81
82typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
83typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
84
Daniel Veillardd41f4f42003-01-29 21:07:52 +000085typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
86typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
87
Daniel Veillarda9d912d2003-02-01 17:43:10 +000088typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
89typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
90
Daniel Veillard6eadf632003-01-23 18:29:16 +000091typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000092 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
93 XML_RELAXNG_COMBINE_CHOICE, /* choice */
94 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000095} xmlRelaxNGCombine;
96
Daniel Veillard4c5cf702003-02-21 15:40:34 +000097typedef enum {
98 XML_RELAXNG_CONTENT_ERROR = -1,
99 XML_RELAXNG_CONTENT_EMPTY = 0,
100 XML_RELAXNG_CONTENT_SIMPLE,
101 XML_RELAXNG_CONTENT_COMPLEX
102} xmlRelaxNGContentType;
103
Daniel Veillard6eadf632003-01-23 18:29:16 +0000104typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
105typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
106
107struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000108 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
109 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
110 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
111 xmlRelaxNGDefinePtr start; /* <start> content */
112 xmlRelaxNGCombine combine; /* the default combine value */
113 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
114 xmlHashTablePtr defs; /* define* */
115 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000116};
117
118
Daniel Veillard6eadf632003-01-23 18:29:16 +0000119typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000120 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
121 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000122 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000123 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
124 XML_RELAXNG_TEXT, /* textual content */
125 XML_RELAXNG_ELEMENT, /* an element */
126 XML_RELAXNG_DATATYPE, /* extenal data type definition */
127 XML_RELAXNG_PARAM, /* extenal data type parameter */
128 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
129 XML_RELAXNG_LIST, /* a list of patterns */
130 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
131 XML_RELAXNG_DEF, /* a definition */
132 XML_RELAXNG_REF, /* reference to a definition */
133 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
134 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
135 XML_RELAXNG_OPTIONAL, /* optional patterns */
136 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
137 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
138 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
139 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
140 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
141 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000142} xmlRelaxNGType;
143
Daniel Veillard52b48c72003-04-13 19:53:42 +0000144#define IS_NULLABLE (1 << 0)
145#define IS_NOT_NULLABLE (1 << 1)
146#define IS_INDETERMINIST (1 << 2)
147#define IS_MIXED (1 << 3)
148#define IS_TRIABLE (1 << 4)
149#define IS_PROCESSED (1 << 5)
150#define IS_COMPILABLE (1 << 6)
151#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000152
Daniel Veillard6eadf632003-01-23 18:29:16 +0000153struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000154 xmlRelaxNGType type; /* the type of definition */
155 xmlNodePtr node; /* the node in the source */
156 xmlChar *name; /* the element local name if present */
157 xmlChar *ns; /* the namespace local name if present */
158 xmlChar *value; /* value when available */
159 void *data; /* data lib or specific pointer */
160 xmlRelaxNGDefinePtr content; /* the expected content */
161 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
162 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
163 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
164 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
165 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
166 short depth; /* used for the cycle detection */
167 short dflags; /* define related flags */
168 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000169};
170
171/**
172 * _xmlRelaxNG:
173 *
174 * A RelaxNGs definition
175 */
176struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000177 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000178 xmlRelaxNGGrammarPtr topgrammar;
179 xmlDocPtr doc;
180
Daniel Veillard4c004142003-10-07 11:33:24 +0000181 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000182
Daniel Veillard4c004142003-10-07 11:33:24 +0000183 xmlHashTablePtr defs; /* define */
184 xmlHashTablePtr refs; /* references */
185 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
186 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
187 int defNr; /* number of defines used */
188 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000189
Daniel Veillard6eadf632003-01-23 18:29:16 +0000190};
191
Daniel Veillard77648bb2003-02-20 15:03:22 +0000192#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
193#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
194#define XML_RELAXNG_IN_LIST (1 << 2)
195#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
196#define XML_RELAXNG_IN_START (1 << 4)
197#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
198#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
199#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000200#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
201#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000202
203struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000204 void *userData; /* user specific data block */
205 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
206 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000207 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000208 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000209
Daniel Veillard4c004142003-10-07 11:33:24 +0000210 xmlRelaxNGPtr schema; /* The schema in use */
211 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
212 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
213 int flags; /* parser flags */
214 int nbErrors; /* number of errors at parse time */
215 int nbWarnings; /* number of warnings at parse time */
216 const xmlChar *define; /* the current define scope */
217 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000218
Daniel Veillard4c004142003-10-07 11:33:24 +0000219 int nbInterleaves;
220 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000221
Daniel Veillard4c004142003-10-07 11:33:24 +0000222 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
223 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
224 xmlChar *URL;
225 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000226
Daniel Veillard4c004142003-10-07 11:33:24 +0000227 int defNr; /* number of defines used */
228 int defMax; /* number of defines aloocated */
229 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000230
Daniel Veillard4c004142003-10-07 11:33:24 +0000231 const char *buffer;
232 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000233
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000234 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000235 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
236 int docNr; /* Depth of the parsing stack */
237 int docMax; /* Max depth of the parsing stack */
238 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000239
240 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000241 xmlRelaxNGIncludePtr inc; /* Current parsed include */
242 int incNr; /* Depth of the include parsing stack */
243 int incMax; /* Max depth of the parsing stack */
244 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000245
Daniel Veillard4c004142003-10-07 11:33:24 +0000246 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000247
248 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000249 xmlAutomataPtr am; /* the automata */
250 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000251
252 int crng; /* compact syntax and other flags */
Daniel Veillard42595322004-11-08 10:52:06 +0000253 int freedoc; /* need to free the document */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000254};
255
256#define FLAGS_IGNORABLE 1
257#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000258#define FLAGS_MIXED_CONTENT 4
Daniel Veillardb30ca312005-09-04 13:50:03 +0000259#define FLAGS_NOERROR 8
Daniel Veillard6eadf632003-01-23 18:29:16 +0000260
261/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000262 * xmlRelaxNGInterleaveGroup:
263 *
264 * A RelaxNGs partition set associated to lists of definitions
265 */
266typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
267typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
268struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000269 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
270 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
271 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000272};
273
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000274#define IS_DETERMINIST 1
275#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000276
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000277/**
278 * xmlRelaxNGPartitions:
279 *
280 * A RelaxNGs partition associated to an interleave group
281 */
282typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
283typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
284struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000285 int nbgroups; /* number of groups in the partitions */
286 xmlHashTablePtr triage; /* hash table used to direct nodes to the
287 * right group when possible */
288 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000289 xmlRelaxNGInterleaveGroupPtr *groups;
290};
291
292/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000293 * xmlRelaxNGValidState:
294 *
295 * A RelaxNGs validation state
296 */
297#define MAX_ATTR 20
298typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
299typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
300struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000301 xmlNodePtr node; /* the current node */
302 xmlNodePtr seq; /* the sequence of children left to validate */
303 int nbAttrs; /* the number of attributes */
304 int maxAttrs; /* the size of attrs */
305 int nbAttrLeft; /* the number of attributes left to validate */
306 xmlChar *value; /* the value when operating on string */
307 xmlChar *endvalue; /* the end value when operating on string */
308 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000309};
310
311/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000312 * xmlRelaxNGStates:
313 *
314 * A RelaxNGs container for validation state
315 */
316typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
317typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
318struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000319 int nbState; /* the number of states */
320 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000321 xmlRelaxNGValidStatePtr *tabState;
322};
323
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000324#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000325
Daniel Veillardfd573f12003-03-16 17:52:32 +0000326/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000327 * xmlRelaxNGValidError:
328 *
329 * A RelaxNGs validation error
330 */
331typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
332typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
333struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000334 xmlRelaxNGValidErr err; /* the error number */
335 int flags; /* flags */
336 xmlNodePtr node; /* the current node */
337 xmlNodePtr seq; /* the current child */
338 const xmlChar *arg1; /* first arg */
339 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000340};
341
342/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000343 * xmlRelaxNGValidCtxt:
344 *
345 * A RelaxNGs validation context
346 */
347
348struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000349 void *userData; /* user specific data block */
350 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
351 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000352 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000353 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000354
Daniel Veillard4c004142003-10-07 11:33:24 +0000355 xmlRelaxNGPtr schema; /* The schema in use */
356 xmlDocPtr doc; /* the document being validated */
357 int flags; /* validation flags */
358 int depth; /* validation depth */
359 int idref; /* requires idref checking */
360 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000361
362 /*
363 * Errors accumulated in branches may have to be stacked to be
364 * provided back when it's sure they affect validation.
365 */
366 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000367 int errNr; /* Depth of the error stack */
368 int errMax; /* Max depth of the error stack */
369 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000370
Daniel Veillard4c004142003-10-07 11:33:24 +0000371 xmlRelaxNGValidStatePtr state; /* the current validation state */
372 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000373
Daniel Veillard4c004142003-10-07 11:33:24 +0000374 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
375 int freeStatesNr;
376 int freeStatesMax;
377 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000378
379 /*
380 * This is used for "progressive" validation
381 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000382 xmlRegExecCtxtPtr elem; /* the current element regexp */
383 int elemNr; /* the number of element validated */
384 int elemMax; /* the max depth of elements */
385 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
386 int pstate; /* progressive state */
387 xmlNodePtr pnode; /* the current node */
388 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
389 int perr; /* signal error in content model
390 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000391};
392
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000393/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000394 * xmlRelaxNGInclude:
395 *
396 * Structure associated to a RelaxNGs document element
397 */
398struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000399 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
400 xmlChar *href; /* the normalized href value */
401 xmlDocPtr doc; /* the associated XML document */
402 xmlRelaxNGDefinePtr content; /* the definitions */
403 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000404};
405
406/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000407 * xmlRelaxNGDocument:
408 *
409 * Structure associated to a RelaxNGs document element
410 */
411struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000412 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000413 xmlChar *href; /* the normalized href value */
414 xmlDocPtr doc; /* the associated XML document */
415 xmlRelaxNGDefinePtr content; /* the definitions */
416 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillard81c51e12009-08-14 18:52:10 +0200417 int externalRef; /* 1 if an external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000418};
419
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000420
Daniel Veillard6eadf632003-01-23 18:29:16 +0000421/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000422 * *
423 * Some factorized error routines *
424 * *
425 ************************************************************************/
426
427/**
428 * xmlRngPErrMemory:
429 * @ctxt: an Relax-NG parser context
430 * @extra: extra informations
431 *
432 * Handle a redefinition of attribute error
433 */
434static void
435xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
436{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000437 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000438 xmlGenericErrorFunc channel = NULL;
439 void *data = NULL;
440
441 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000442 if (ctxt->serror != NULL)
443 schannel = ctxt->serror;
444 else
445 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000446 data = ctxt->userData;
447 ctxt->nbErrors++;
448 }
449 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000450 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000451 NULL, NULL, XML_FROM_RELAXNGP,
452 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
453 NULL, NULL, 0, 0,
454 "Memory allocation failed : %s\n", extra);
455 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000456 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000457 NULL, NULL, XML_FROM_RELAXNGP,
458 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
459 NULL, NULL, 0, 0, "Memory allocation failed\n");
460}
461
462/**
463 * xmlRngVErrMemory:
464 * @ctxt: a Relax-NG validation context
465 * @extra: extra informations
466 *
467 * Handle a redefinition of attribute error
468 */
469static void
470xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
471{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000472 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000473 xmlGenericErrorFunc channel = NULL;
474 void *data = NULL;
475
476 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000477 if (ctxt->serror != NULL)
478 schannel = ctxt->serror;
479 else
480 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000481 data = ctxt->userData;
482 ctxt->nbErrors++;
483 }
484 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000485 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000486 NULL, NULL, XML_FROM_RELAXNGV,
487 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
488 NULL, NULL, 0, 0,
489 "Memory allocation failed : %s\n", extra);
490 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000491 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000492 NULL, NULL, XML_FROM_RELAXNGV,
493 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
494 NULL, NULL, 0, 0, "Memory allocation failed\n");
495}
496
497/**
498 * xmlRngPErr:
499 * @ctxt: a Relax-NG parser context
500 * @node: the node raising the error
501 * @error: the error code
502 * @msg: message
503 * @str1: extra info
504 * @str2: extra info
505 *
506 * Handle a Relax NG Parsing error
507 */
508static void
509xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
510 const char *msg, const xmlChar * str1, const xmlChar * str2)
511{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000512 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000513 xmlGenericErrorFunc channel = NULL;
514 void *data = NULL;
515
516 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000517 if (ctxt->serror != NULL)
518 schannel = ctxt->serror;
519 else
520 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000521 data = ctxt->userData;
522 ctxt->nbErrors++;
523 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000524 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000525 NULL, node, XML_FROM_RELAXNGP,
526 error, XML_ERR_ERROR, NULL, 0,
527 (const char *) str1, (const char *) str2, NULL, 0, 0,
528 msg, str1, str2);
529}
530
531/**
532 * xmlRngVErr:
533 * @ctxt: a Relax-NG validation context
534 * @node: the node raising the error
535 * @error: the error code
536 * @msg: message
537 * @str1: extra info
538 * @str2: extra info
539 *
540 * Handle a Relax NG Validation error
541 */
542static void
543xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
544 const char *msg, const xmlChar * str1, const xmlChar * str2)
545{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000546 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000547 xmlGenericErrorFunc channel = NULL;
548 void *data = NULL;
549
550 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000551 if (ctxt->serror != NULL)
552 schannel = ctxt->serror;
553 else
554 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000555 data = ctxt->userData;
556 ctxt->nbErrors++;
557 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000558 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000559 NULL, node, XML_FROM_RELAXNGV,
560 error, XML_ERR_ERROR, NULL, 0,
561 (const char *) str1, (const char *) str2, NULL, 0, 0,
562 msg, str1, str2);
563}
564
565/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000566 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000567 * Preliminary type checking interfaces *
568 * *
569 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000570
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000571/**
572 * xmlRelaxNGTypeHave:
573 * @data: data needed for the library
574 * @type: the type name
575 * @value: the value to check
576 *
577 * Function provided by a type library to check if a type is exported
578 *
579 * Returns 1 if yes, 0 if no and -1 in case of error.
580 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000581typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000582
583/**
584 * xmlRelaxNGTypeCheck:
585 * @data: data needed for the library
586 * @type: the type name
587 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000588 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000589 *
590 * Function provided by a type library to check if a value match a type
591 *
592 * Returns 1 if yes, 0 if no and -1 in case of error.
593 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000594typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
595 const xmlChar * value, void **result,
596 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000597
598/**
599 * xmlRelaxNGFacetCheck:
600 * @data: data needed for the library
601 * @type: the type name
602 * @facet: the facet name
603 * @val: the facet value
604 * @strval: the string value
605 * @value: the value to check
606 *
607 * Function provided by a type library to check a value facet
608 *
609 * Returns 1 if yes, 0 if no and -1 in case of error.
610 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000611typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
612 const xmlChar * facet,
613 const xmlChar * val,
614 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000615
616/**
617 * xmlRelaxNGTypeFree:
618 * @data: data needed for the library
619 * @result: the value to free
620 *
621 * Function provided by a type library to free a returned result
622 */
623typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000624
625/**
626 * xmlRelaxNGTypeCompare:
627 * @data: data needed for the library
628 * @type: the type name
629 * @value1: the first value
630 * @value2: the second value
631 *
632 * Function provided by a type library to compare two values accordingly
633 * to a type.
634 *
635 * Returns 1 if yes, 0 if no and -1 in case of error.
636 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000637typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
638 const xmlChar * value1,
639 xmlNodePtr ctxt1,
640 void *comp1,
641 const xmlChar * value2,
642 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000643typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
644typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
645struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000646 const xmlChar *namespace; /* the datatypeLibrary value */
647 void *data; /* data needed for the library */
648 xmlRelaxNGTypeHave have; /* the export function */
649 xmlRelaxNGTypeCheck check; /* the checking function */
650 xmlRelaxNGTypeCompare comp; /* the compare function */
651 xmlRelaxNGFacetCheck facet; /* the facet check function */
652 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000653};
654
655/************************************************************************
656 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000657 * Allocation functions *
658 * *
659 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000660static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
661static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000662static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000663static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000664static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
665 ATTRIBUTE_UNUSED,
666 xmlRelaxNGValidStatePtr state1,
667 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000668static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000669 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000670
671/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000672 * xmlRelaxNGFreeDocument:
673 * @docu: a document structure
674 *
675 * Deallocate a RelaxNG document structure.
676 */
677static void
678xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
679{
680 if (docu == NULL)
681 return;
682
683 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000684 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000685 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000686 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000687 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000688 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000689 xmlFree(docu);
690}
691
692/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000693 * xmlRelaxNGFreeDocumentList:
694 * @docu: a list of document structure
695 *
696 * Deallocate a RelaxNG document structures.
697 */
698static void
699xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
700{
701 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000702
Daniel Veillardc482e262003-02-26 14:48:48 +0000703 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000704 next = docu->next;
705 xmlRelaxNGFreeDocument(docu);
706 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000707 }
708}
709
710/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000711 * xmlRelaxNGFreeInclude:
712 * @incl: a include structure
713 *
714 * Deallocate a RelaxNG include structure.
715 */
716static void
717xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
718{
719 if (incl == NULL)
720 return;
721
722 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000723 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000724 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000725 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000726 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000727 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000728 xmlFree(incl);
729}
730
731/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000732 * xmlRelaxNGFreeIncludeList:
733 * @incl: a include structure list
734 *
735 * Deallocate a RelaxNG include structure.
736 */
737static void
738xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
739{
740 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000741
Daniel Veillardc482e262003-02-26 14:48:48 +0000742 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000743 next = incl->next;
744 xmlRelaxNGFreeInclude(incl);
745 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000746 }
747}
748
749/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000750 * xmlRelaxNGNewRelaxNG:
751 * @ctxt: a Relax-NG validation context (optional)
752 *
753 * Allocate a new RelaxNG structure.
754 *
755 * Returns the newly allocated structure or NULL in case or error
756 */
757static xmlRelaxNGPtr
758xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
759{
760 xmlRelaxNGPtr ret;
761
762 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
763 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000764 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000765 return (NULL);
766 }
767 memset(ret, 0, sizeof(xmlRelaxNG));
768
769 return (ret);
770}
771
772/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000773 * xmlRelaxNGFreeInnerSchema:
774 * @schema: a schema structure
775 *
776 * Deallocate a RelaxNG schema structure.
777 */
778static void
779xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
780{
781 if (schema == NULL)
782 return;
783
784 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000785 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000786 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000787 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000788
Daniel Veillard4c004142003-10-07 11:33:24 +0000789 for (i = 0; i < schema->defNr; i++)
790 xmlRelaxNGFreeDefine(schema->defTab[i]);
791 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000792 }
793
794 xmlFree(schema);
795}
796
797/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000798 * xmlRelaxNGFree:
799 * @schema: a schema structure
800 *
801 * Deallocate a RelaxNG structure.
802 */
803void
804xmlRelaxNGFree(xmlRelaxNGPtr schema)
805{
806 if (schema == NULL)
807 return;
808
Daniel Veillard6eadf632003-01-23 18:29:16 +0000809 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000810 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000811 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000812 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000813 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000814 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000815 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000816 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000817 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000818 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000819
Daniel Veillard4c004142003-10-07 11:33:24 +0000820 for (i = 0; i < schema->defNr; i++)
821 xmlRelaxNGFreeDefine(schema->defTab[i]);
822 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000823 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000824
825 xmlFree(schema);
826}
827
828/**
829 * xmlRelaxNGNewGrammar:
830 * @ctxt: a Relax-NG validation context (optional)
831 *
832 * Allocate a new RelaxNG grammar.
833 *
834 * Returns the newly allocated structure or NULL in case or error
835 */
836static xmlRelaxNGGrammarPtr
837xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
838{
839 xmlRelaxNGGrammarPtr ret;
840
841 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
842 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000843 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000844 return (NULL);
845 }
846 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
847
848 return (ret);
849}
850
851/**
852 * xmlRelaxNGFreeGrammar:
853 * @grammar: a grammar structure
854 *
855 * Deallocate a RelaxNG grammar structure.
856 */
857static void
858xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
859{
860 if (grammar == NULL)
861 return;
862
Daniel Veillardc482e262003-02-26 14:48:48 +0000863 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000864 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000865 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000866 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000867 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000868 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000869 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000870 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000871 }
872 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000873 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000874 }
875
876 xmlFree(grammar);
877}
878
879/**
880 * xmlRelaxNGNewDefine:
881 * @ctxt: a Relax-NG validation context
882 * @node: the node in the input document.
883 *
884 * Allocate a new RelaxNG define.
885 *
886 * Returns the newly allocated structure or NULL in case or error
887 */
888static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000889xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000890{
891 xmlRelaxNGDefinePtr ret;
892
Daniel Veillard419a7682003-02-03 23:22:49 +0000893 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000894 ctxt->defMax = 16;
895 ctxt->defNr = 0;
896 ctxt->defTab = (xmlRelaxNGDefinePtr *)
897 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
898 if (ctxt->defTab == NULL) {
899 xmlRngPErrMemory(ctxt, "allocating define\n");
900 return (NULL);
901 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000902 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000903 xmlRelaxNGDefinePtr *tmp;
904
905 ctxt->defMax *= 2;
906 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
907 ctxt->defMax *
908 sizeof
909 (xmlRelaxNGDefinePtr));
910 if (tmp == NULL) {
911 xmlRngPErrMemory(ctxt, "allocating define\n");
912 return (NULL);
913 }
914 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000915 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000916 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
917 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000918 xmlRngPErrMemory(ctxt, "allocating define\n");
919 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000920 }
921 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000922 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000923 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000924 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000925 return (ret);
926}
927
928/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000929 * xmlRelaxNGFreePartition:
930 * @partitions: a partition set structure
931 *
932 * Deallocate RelaxNG partition set structures.
933 */
934static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000935xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
936{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000937 xmlRelaxNGInterleaveGroupPtr group;
938 int j;
939
940 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000941 if (partitions->groups != NULL) {
942 for (j = 0; j < partitions->nbgroups; j++) {
943 group = partitions->groups[j];
944 if (group != NULL) {
945 if (group->defs != NULL)
946 xmlFree(group->defs);
947 if (group->attrs != NULL)
948 xmlFree(group->attrs);
949 xmlFree(group);
950 }
951 }
952 xmlFree(partitions->groups);
953 }
954 if (partitions->triage != NULL) {
955 xmlHashFree(partitions->triage, NULL);
956 }
957 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000958 }
959}
Daniel Veillard4c004142003-10-07 11:33:24 +0000960
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000961/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000962 * xmlRelaxNGFreeDefine:
963 * @define: a define structure
964 *
965 * Deallocate a RelaxNG define structure.
966 */
967static void
968xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
969{
970 if (define == NULL)
971 return;
972
Daniel Veillard4c004142003-10-07 11:33:24 +0000973 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
974 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000975
Daniel Veillard4c004142003-10-07 11:33:24 +0000976 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
977 if ((lib != NULL) && (lib->freef != NULL))
978 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000979 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000980 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
981 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
982 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
983 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000984 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000985 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000986 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000987 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000988 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000989 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000990 if (define->contModel != NULL)
991 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000992 xmlFree(define);
993}
994
995/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000996 * xmlRelaxNGNewStates:
997 * @ctxt: a Relax-NG validation context
998 * @size: the default size for the container
999 *
1000 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001001 *
1002 * Returns the newly allocated structure or NULL in case or error
1003 */
1004static xmlRelaxNGStatesPtr
1005xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1006{
1007 xmlRelaxNGStatesPtr ret;
1008
Daniel Veillard798024a2003-03-19 10:36:09 +00001009 if ((ctxt != NULL) &&
Daniel Veillard9fcd4622009-08-14 16:16:31 +02001010 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001011 ctxt->freeStatesNr--;
1012 ret = ctxt->freeStates[ctxt->freeStatesNr];
1013 ret->nbState = 0;
1014 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001015 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001016 if (size < 16)
1017 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001018
1019 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001020 (size -
1021 1) *
1022 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001023 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001024 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001025 return (NULL);
1026 }
1027 ret->nbState = 0;
1028 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001029 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1030 sizeof
1031 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001032 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001033 xmlRngVErrMemory(ctxt, "allocating states\n");
1034 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001035 return (NULL);
1036 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001037 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001038}
1039
1040/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001041 * xmlRelaxNGAddStateUniq:
1042 * @ctxt: a Relax-NG validation context
1043 * @states: the states container
1044 * @state: the validation state
1045 *
1046 * Add a RelaxNG validation state to the container without checking
1047 * for unicity.
1048 *
1049 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1050 */
1051static int
1052xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001053 xmlRelaxNGStatesPtr states,
1054 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001055{
1056 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001057 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001058 }
1059 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001060 xmlRelaxNGValidStatePtr *tmp;
1061 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001062
Daniel Veillard4c004142003-10-07 11:33:24 +00001063 size = states->maxState * 2;
1064 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1065 (size) *
1066 sizeof
1067 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001068 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001069 xmlRngVErrMemory(ctxt, "adding states\n");
1070 return (-1);
1071 }
1072 states->tabState = tmp;
1073 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001074 }
1075 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001076 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001077}
1078
1079/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001080 * xmlRelaxNGAddState:
1081 * @ctxt: a Relax-NG validation context
1082 * @states: the states container
1083 * @state: the validation state
1084 *
1085 * Add a RelaxNG validation state to the container
1086 *
1087 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1088 */
1089static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001090xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1091 xmlRelaxNGStatesPtr states,
1092 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001093{
1094 int i;
1095
1096 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001097 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001098 }
1099 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001100 xmlRelaxNGValidStatePtr *tmp;
1101 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001102
Daniel Veillard4c004142003-10-07 11:33:24 +00001103 size = states->maxState * 2;
1104 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1105 (size) *
1106 sizeof
1107 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001108 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001109 xmlRngVErrMemory(ctxt, "adding states\n");
1110 return (-1);
1111 }
1112 states->tabState = tmp;
1113 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001114 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001115 for (i = 0; i < states->nbState; i++) {
1116 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1117 xmlRelaxNGFreeValidState(ctxt, state);
1118 return (0);
1119 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001120 }
1121 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001122 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001123}
1124
1125/**
1126 * xmlRelaxNGFreeStates:
1127 * @ctxt: a Relax-NG validation context
1128 * @states: teh container
1129 *
1130 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001131 */
1132static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001133xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001134 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001135{
Daniel Veillard798024a2003-03-19 10:36:09 +00001136 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001137 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001138 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001139 ctxt->freeStatesMax = 40;
1140 ctxt->freeStatesNr = 0;
1141 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1142 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1143 if (ctxt->freeStates == NULL) {
1144 xmlRngVErrMemory(ctxt, "storing states\n");
1145 }
1146 } else if ((ctxt != NULL)
1147 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1148 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001149
Daniel Veillard4c004142003-10-07 11:33:24 +00001150 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1151 2 * ctxt->freeStatesMax *
1152 sizeof
1153 (xmlRelaxNGStatesPtr));
1154 if (tmp == NULL) {
1155 xmlRngVErrMemory(ctxt, "storing states\n");
1156 xmlFree(states->tabState);
1157 xmlFree(states);
1158 return;
1159 }
1160 ctxt->freeStates = tmp;
1161 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001162 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001163 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001164 xmlFree(states->tabState);
1165 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001166 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001167 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001168 }
1169}
1170
1171/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001172 * xmlRelaxNGNewValidState:
1173 * @ctxt: a Relax-NG validation context
1174 * @node: the current node or NULL for the document
1175 *
1176 * Allocate a new RelaxNG validation state
1177 *
1178 * Returns the newly allocated structure or NULL in case or error
1179 */
1180static xmlRelaxNGValidStatePtr
1181xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1182{
1183 xmlRelaxNGValidStatePtr ret;
1184 xmlAttrPtr attr;
1185 xmlAttrPtr attrs[MAX_ATTR];
1186 int nbAttrs = 0;
1187 xmlNodePtr root = NULL;
1188
1189 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001190 root = xmlDocGetRootElement(ctxt->doc);
1191 if (root == NULL)
1192 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001193 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001194 attr = node->properties;
1195 while (attr != NULL) {
1196 if (nbAttrs < MAX_ATTR)
1197 attrs[nbAttrs++] = attr;
1198 else
1199 nbAttrs++;
1200 attr = attr->next;
1201 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001202 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001203 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1204 ctxt->freeState->nbState--;
1205 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001206 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001207 ret =
1208 (xmlRelaxNGValidStatePtr)
1209 xmlMalloc(sizeof(xmlRelaxNGValidState));
1210 if (ret == NULL) {
1211 xmlRngVErrMemory(ctxt, "allocating states\n");
1212 return (NULL);
1213 }
1214 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001215 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001216 ret->value = NULL;
1217 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001218 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001219 ret->node = (xmlNodePtr) ctxt->doc;
1220 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001221 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001222 ret->node = node;
1223 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001224 }
1225 ret->nbAttrs = 0;
1226 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001227 if (ret->attrs == NULL) {
1228 if (nbAttrs < 4)
1229 ret->maxAttrs = 4;
1230 else
1231 ret->maxAttrs = nbAttrs;
1232 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1233 sizeof(xmlAttrPtr));
1234 if (ret->attrs == NULL) {
1235 xmlRngVErrMemory(ctxt, "allocating states\n");
1236 return (ret);
1237 }
1238 } else if (ret->maxAttrs < nbAttrs) {
1239 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001240
Daniel Veillard4c004142003-10-07 11:33:24 +00001241 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1242 sizeof(xmlAttrPtr));
1243 if (tmp == NULL) {
1244 xmlRngVErrMemory(ctxt, "allocating states\n");
1245 return (ret);
1246 }
1247 ret->attrs = tmp;
1248 ret->maxAttrs = nbAttrs;
1249 }
1250 ret->nbAttrs = nbAttrs;
1251 if (nbAttrs < MAX_ATTR) {
1252 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1253 } else {
1254 attr = node->properties;
1255 nbAttrs = 0;
1256 while (attr != NULL) {
1257 ret->attrs[nbAttrs++] = attr;
1258 attr = attr->next;
1259 }
1260 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001261 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001262 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001263 return (ret);
1264}
1265
1266/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001267 * xmlRelaxNGCopyValidState:
1268 * @ctxt: a Relax-NG validation context
1269 * @state: a validation state
1270 *
1271 * Copy the validation state
1272 *
1273 * Returns the newly allocated structure or NULL in case or error
1274 */
1275static xmlRelaxNGValidStatePtr
1276xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001277 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001278{
1279 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001280 unsigned int maxAttrs;
1281 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001282
1283 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001284 return (NULL);
1285 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1286 ctxt->freeState->nbState--;
1287 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001288 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001289 ret =
1290 (xmlRelaxNGValidStatePtr)
1291 xmlMalloc(sizeof(xmlRelaxNGValidState));
1292 if (ret == NULL) {
1293 xmlRngVErrMemory(ctxt, "allocating states\n");
1294 return (NULL);
1295 }
1296 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001297 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001298 attrs = ret->attrs;
1299 maxAttrs = ret->maxAttrs;
1300 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1301 ret->attrs = attrs;
1302 ret->maxAttrs = maxAttrs;
1303 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001304 if (ret->attrs == NULL) {
1305 ret->maxAttrs = state->maxAttrs;
1306 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1307 sizeof(xmlAttrPtr));
1308 if (ret->attrs == NULL) {
1309 xmlRngVErrMemory(ctxt, "allocating states\n");
1310 ret->nbAttrs = 0;
1311 return (ret);
1312 }
1313 } else if (ret->maxAttrs < state->nbAttrs) {
1314 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001315
Daniel Veillard4c004142003-10-07 11:33:24 +00001316 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1317 sizeof(xmlAttrPtr));
1318 if (tmp == NULL) {
1319 xmlRngVErrMemory(ctxt, "allocating states\n");
1320 ret->nbAttrs = 0;
1321 return (ret);
1322 }
1323 ret->maxAttrs = state->maxAttrs;
1324 ret->attrs = tmp;
1325 }
1326 memcpy(ret->attrs, state->attrs,
1327 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001328 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001329 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001330}
1331
1332/**
1333 * xmlRelaxNGEqualValidState:
1334 * @ctxt: a Relax-NG validation context
1335 * @state1: a validation state
1336 * @state2: a validation state
1337 *
1338 * Compare the validation states for equality
1339 *
1340 * Returns 1 if equald, 0 otherwise
1341 */
1342static int
1343xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001344 xmlRelaxNGValidStatePtr state1,
1345 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001346{
1347 int i;
1348
1349 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001350 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001351 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001352 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001353 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001354 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001355 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001356 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001357 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001358 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001359 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001360 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001361 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001362 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001363 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001364 (!xmlStrEqual(state1->value, state2->value)))
1365 return (0);
1366 for (i = 0; i < state1->nbAttrs; i++) {
1367 if (state1->attrs[i] != state2->attrs[i])
1368 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001369 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001370 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001371}
1372
1373/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001374 * xmlRelaxNGFreeValidState:
1375 * @state: a validation state structure
1376 *
1377 * Deallocate a RelaxNG validation state structure.
1378 */
1379static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001380xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001381 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001382{
1383 if (state == NULL)
1384 return;
1385
Daniel Veillard798024a2003-03-19 10:36:09 +00001386 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001387 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001388 }
1389 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001390 if (state->attrs != NULL)
1391 xmlFree(state->attrs);
1392 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001393 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001394 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001395 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001396}
1397
1398/************************************************************************
1399 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001400 * Semi internal functions *
1401 * *
1402 ************************************************************************/
1403
1404/**
1405 * xmlRelaxParserSetFlag:
1406 * @ctxt: a RelaxNG parser context
1407 * @flags: a set of flags values
1408 *
1409 * Semi private function used to pass informations to a parser context
1410 * which are a combination of xmlRelaxNGParserFlag .
1411 *
1412 * Returns 0 if success and -1 in case of error
1413 */
1414int
1415xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1416{
1417 if (ctxt == NULL) return(-1);
1418 if (flags & XML_RELAXNGP_FREE_DOC) {
1419 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1420 flags -= XML_RELAXNGP_FREE_DOC;
1421 }
1422 if (flags & XML_RELAXNGP_CRNG) {
1423 ctxt->crng |= XML_RELAXNGP_CRNG;
1424 flags -= XML_RELAXNGP_CRNG;
1425 }
1426 if (flags != 0) return(-1);
1427 return(0);
1428}
1429
1430/************************************************************************
1431 * *
1432 * Document functions *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001433 * *
1434 ************************************************************************/
1435static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001436 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001437
1438/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001439 * xmlRelaxNGIncludePush:
1440 * @ctxt: the parser context
1441 * @value: the element doc
1442 *
1443 * Pushes a new include on top of the include stack
1444 *
1445 * Returns 0 in case of error, the index in the stack otherwise
1446 */
1447static int
1448xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001449 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001450{
1451 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001452 ctxt->incMax = 4;
1453 ctxt->incNr = 0;
1454 ctxt->incTab =
1455 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1456 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001457 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001458 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001459 return (0);
1460 }
1461 }
1462 if (ctxt->incNr >= ctxt->incMax) {
1463 ctxt->incMax *= 2;
1464 ctxt->incTab =
1465 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001466 ctxt->incMax *
1467 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001468 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001469 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001470 return (0);
1471 }
1472 }
1473 ctxt->incTab[ctxt->incNr] = value;
1474 ctxt->inc = value;
1475 return (ctxt->incNr++);
1476}
1477
1478/**
1479 * xmlRelaxNGIncludePop:
1480 * @ctxt: the parser context
1481 *
1482 * Pops the top include from the include stack
1483 *
1484 * Returns the include just removed
1485 */
1486static xmlRelaxNGIncludePtr
1487xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1488{
1489 xmlRelaxNGIncludePtr ret;
1490
1491 if (ctxt->incNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001492 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001493 ctxt->incNr--;
1494 if (ctxt->incNr > 0)
1495 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1496 else
1497 ctxt->inc = NULL;
1498 ret = ctxt->incTab[ctxt->incNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001499 ctxt->incTab[ctxt->incNr] = NULL;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001500 return (ret);
1501}
1502
1503/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001504 * xmlRelaxNGRemoveRedefine:
1505 * @ctxt: the parser context
1506 * @URL: the normalized URL
1507 * @target: the included target
1508 * @name: the define name to eliminate
1509 *
1510 * Applies the elimination algorithm of 4.7
1511 *
1512 * Returns 0 in case of error, 1 in case of success.
1513 */
1514static int
1515xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001516 const xmlChar * URL ATTRIBUTE_UNUSED,
1517 xmlNodePtr target, const xmlChar * name)
1518{
Daniel Veillard5add8682003-03-10 13:13:58 +00001519 int found = 0;
1520 xmlNodePtr tmp, tmp2;
1521 xmlChar *name2;
1522
1523#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001524 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001525 xmlGenericError(xmlGenericErrorContext,
1526 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001527 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001528 xmlGenericError(xmlGenericErrorContext,
1529 "Elimination of <include> define %s from %s\n",
1530 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001531#endif
1532 tmp = target;
1533 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001534 tmp2 = tmp->next;
1535 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1536 found = 1;
1537 xmlUnlinkNode(tmp);
1538 xmlFreeNode(tmp);
1539 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1540 name2 = xmlGetProp(tmp, BAD_CAST "name");
1541 xmlRelaxNGNormExtSpace(name2);
1542 if (name2 != NULL) {
1543 if (xmlStrEqual(name, name2)) {
1544 found = 1;
1545 xmlUnlinkNode(tmp);
1546 xmlFreeNode(tmp);
1547 }
1548 xmlFree(name2);
1549 }
1550 } else if (IS_RELAXNG(tmp, "include")) {
1551 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001552 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001553
Daniel Veillard4c004142003-10-07 11:33:24 +00001554 if ((inc != NULL) && (inc->doc != NULL) &&
1555 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001556
Daniel Veillard4c004142003-10-07 11:33:24 +00001557 if (xmlStrEqual
1558 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001559#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001560 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001561#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001562 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1563 inc->doc->children->
1564 children, name) == 1) {
1565 found = 1;
1566 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001567#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001568 if (href != NULL)
1569 xmlFree(href);
Daniel Veillard14b56432006-03-09 18:41:40 +00001570#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001571 }
1572 }
1573 }
1574 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001575 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001576 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001577}
1578
1579/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001580 * xmlRelaxNGLoadInclude:
1581 * @ctxt: the parser context
1582 * @URL: the normalized URL
1583 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001584 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001585 *
1586 * First lookup if the document is already loaded into the parser context,
1587 * check against recursion. If not found the resource is loaded and
1588 * the content is preprocessed before being returned back to the caller.
1589 *
1590 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1591 */
1592static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001593xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1594 xmlNodePtr node, const xmlChar * ns)
1595{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001596 xmlRelaxNGIncludePtr ret = NULL;
1597 xmlDocPtr doc;
1598 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001599 xmlNodePtr root, cur;
1600
1601#ifdef DEBUG_INCLUDE
1602 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001603 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001604#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001605
1606 /*
1607 * check against recursion in the stack
1608 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001609 for (i = 0; i < ctxt->incNr; i++) {
1610 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1611 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1612 "Detected an Include recursion for %s\n", URL,
1613 NULL);
1614 return (NULL);
1615 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001616 }
1617
1618 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001619 * load the document
1620 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001621 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001622 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001623 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1624 "xmlRelaxNG: could not load %s\n", URL, NULL);
1625 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001626 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001627#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001628 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001629#endif
1630
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001631 /*
1632 * Allocate the document structures and register it first.
1633 */
1634 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1635 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001636 xmlRngPErrMemory(ctxt, "allocating include\n");
1637 xmlFreeDoc(doc);
1638 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001639 }
1640 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1641 ret->doc = doc;
1642 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001643 ret->next = ctxt->includes;
1644 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001645
1646 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001647 * transmit the ns if needed
1648 */
1649 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001650 root = xmlDocGetRootElement(doc);
1651 if (root != NULL) {
1652 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1653 xmlSetProp(root, BAD_CAST "ns", ns);
1654 }
1655 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001656 }
1657
1658 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001659 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001660 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001661 xmlRelaxNGIncludePush(ctxt, ret);
1662
1663 /*
1664 * Some preprocessing of the document content, this include recursing
1665 * in the include stack.
1666 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001667#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001668 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001669#endif
1670
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001671 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1672 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001673 ctxt->inc = NULL;
1674 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001675 }
1676
1677 /*
1678 * Pop up the include from the stack
1679 */
1680 xmlRelaxNGIncludePop(ctxt);
1681
Daniel Veillard5add8682003-03-10 13:13:58 +00001682#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001683 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001684#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001685 /*
1686 * Check that the top element is a grammar
1687 */
1688 root = xmlDocGetRootElement(doc);
1689 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001690 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1691 "xmlRelaxNG: included document is empty %s\n", URL,
1692 NULL);
1693 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001694 }
1695 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001696 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1697 "xmlRelaxNG: included document %s root is not a grammar\n",
1698 URL, NULL);
1699 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001700 }
1701
1702 /*
1703 * Elimination of redefined rules in the include.
1704 */
1705 cur = node->children;
1706 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001707 if (IS_RELAXNG(cur, "start")) {
1708 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001709
Daniel Veillard4c004142003-10-07 11:33:24 +00001710 found =
1711 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1712 if (!found) {
1713 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1714 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1715 URL, NULL);
1716 }
1717 } else if (IS_RELAXNG(cur, "define")) {
1718 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001719
Daniel Veillard4c004142003-10-07 11:33:24 +00001720 name = xmlGetProp(cur, BAD_CAST "name");
1721 if (name == NULL) {
1722 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1723 "xmlRelaxNG: include %s has define without name\n",
1724 URL, NULL);
1725 } else {
1726 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001727
Daniel Veillard4c004142003-10-07 11:33:24 +00001728 xmlRelaxNGNormExtSpace(name);
1729 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1730 root->children, name);
1731 if (!found) {
1732 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1733 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1734 URL, name);
1735 }
1736 xmlFree(name);
1737 }
1738 }
1739 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001740 }
1741
1742
Daniel Veillard4c004142003-10-07 11:33:24 +00001743 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001744}
1745
1746/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001747 * xmlRelaxNGValidErrorPush:
1748 * @ctxt: the validation context
1749 * @err: the error code
1750 * @arg1: the first string argument
1751 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001752 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001753 *
1754 * Pushes a new error on top of the error stack
1755 *
1756 * Returns 0 in case of error, the index in the stack otherwise
1757 */
1758static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001759xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1760 xmlRelaxNGValidErr err, const xmlChar * arg1,
1761 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001762{
1763 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001764
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001765#ifdef DEBUG_ERROR
1766 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001767 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001768#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001769 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001770 ctxt->errMax = 8;
1771 ctxt->errNr = 0;
1772 ctxt->errTab =
1773 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1774 sizeof
1775 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001776 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001777 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001778 return (0);
1779 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001780 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001781 }
1782 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001783 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001784 ctxt->errTab =
1785 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001786 ctxt->errMax *
1787 sizeof
1788 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001789 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001790 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001791 return (0);
1792 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001793 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001794 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001795 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001796 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1797 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001798 cur = &ctxt->errTab[ctxt->errNr];
1799 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001800 if (dup) {
1801 cur->arg1 = xmlStrdup(arg1);
1802 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001803 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001804 } else {
1805 cur->arg1 = arg1;
1806 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001807 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001808 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001809 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001810 cur->node = ctxt->state->node;
1811 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001812 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001813 cur->node = NULL;
1814 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001815 }
1816 ctxt->err = cur;
1817 return (ctxt->errNr++);
1818}
1819
1820/**
1821 * xmlRelaxNGValidErrorPop:
1822 * @ctxt: the validation context
1823 *
1824 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001825 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001826static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001827xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1828{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001829 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001830
Daniel Veillard580ced82003-03-21 21:22:48 +00001831 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001832 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001833 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001834 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001835 ctxt->errNr--;
1836 if (ctxt->errNr > 0)
1837 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1838 else
1839 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001840 cur = &ctxt->errTab[ctxt->errNr];
1841 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001842 if (cur->arg1 != NULL)
1843 xmlFree((xmlChar *) cur->arg1);
1844 cur->arg1 = NULL;
1845 if (cur->arg2 != NULL)
1846 xmlFree((xmlChar *) cur->arg2);
1847 cur->arg2 = NULL;
1848 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001849 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001850}
1851
Daniel Veillard42f12e92003-03-07 18:32:59 +00001852/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001853 * xmlRelaxNGDocumentPush:
1854 * @ctxt: the parser context
1855 * @value: the element doc
1856 *
1857 * Pushes a new doc on top of the doc stack
1858 *
1859 * Returns 0 in case of error, the index in the stack otherwise
1860 */
1861static int
1862xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001863 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001864{
1865 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001866 ctxt->docMax = 4;
1867 ctxt->docNr = 0;
1868 ctxt->docTab =
1869 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1870 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001871 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001872 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001873 return (0);
1874 }
1875 }
1876 if (ctxt->docNr >= ctxt->docMax) {
1877 ctxt->docMax *= 2;
1878 ctxt->docTab =
1879 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001880 ctxt->docMax *
1881 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001882 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001883 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001884 return (0);
1885 }
1886 }
1887 ctxt->docTab[ctxt->docNr] = value;
1888 ctxt->doc = value;
1889 return (ctxt->docNr++);
1890}
1891
1892/**
1893 * xmlRelaxNGDocumentPop:
1894 * @ctxt: the parser context
1895 *
1896 * Pops the top doc from the doc stack
1897 *
1898 * Returns the doc just removed
1899 */
1900static xmlRelaxNGDocumentPtr
1901xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1902{
1903 xmlRelaxNGDocumentPtr ret;
1904
1905 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001906 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001907 ctxt->docNr--;
1908 if (ctxt->docNr > 0)
1909 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1910 else
1911 ctxt->doc = NULL;
1912 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001913 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001914 return (ret);
1915}
1916
1917/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001918 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001919 * @ctxt: the parser context
1920 * @URL: the normalized URL
1921 * @ns: the inherited ns if any
1922 *
1923 * First lookup if the document is already loaded into the parser context,
1924 * check against recursion. If not found the resource is loaded and
1925 * the content is preprocessed before being returned back to the caller.
1926 *
1927 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1928 */
1929static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001930xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1931 const xmlChar * URL, const xmlChar * ns)
1932{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001933 xmlRelaxNGDocumentPtr ret = NULL;
1934 xmlDocPtr doc;
1935 xmlNodePtr root;
1936 int i;
1937
1938 /*
1939 * check against recursion in the stack
1940 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001941 for (i = 0; i < ctxt->docNr; i++) {
1942 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1943 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1944 "Detected an externalRef recursion for %s\n", URL,
1945 NULL);
1946 return (NULL);
1947 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001948 }
1949
1950 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001951 * load the document
1952 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001953 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001954 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001955 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1956 "xmlRelaxNG: could not load %s\n", URL, NULL);
1957 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001958 }
1959
1960 /*
1961 * Allocate the document structures and register it first.
1962 */
1963 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1964 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001965 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1966 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1967 xmlFreeDoc(doc);
1968 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001969 }
1970 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1971 ret->doc = doc;
1972 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001973 ret->next = ctxt->documents;
Daniel Veillard81c51e12009-08-14 18:52:10 +02001974 ret->externalRef = 1;
Daniel Veillardc482e262003-02-26 14:48:48 +00001975 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001976
1977 /*
1978 * transmit the ns if needed
1979 */
1980 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001981 root = xmlDocGetRootElement(doc);
1982 if (root != NULL) {
1983 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1984 xmlSetProp(root, BAD_CAST "ns", ns);
1985 }
1986 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001987 }
1988
1989 /*
1990 * push it on the stack and register it in the hash table
1991 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001992 xmlRelaxNGDocumentPush(ctxt, ret);
1993
1994 /*
1995 * Some preprocessing of the document content
1996 */
1997 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1998 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001999 ctxt->doc = NULL;
2000 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002001 }
2002
2003 xmlRelaxNGDocumentPop(ctxt);
2004
Daniel Veillard4c004142003-10-07 11:33:24 +00002005 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002006}
2007
2008/************************************************************************
2009 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002010 * Error functions *
2011 * *
2012 ************************************************************************/
2013
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002014#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2015#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2016#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2017#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2018#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002019
Daniel Veillard231d7912003-02-09 14:22:17 +00002020static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002021xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2022{
Daniel Veillard231d7912003-02-09 14:22:17 +00002023 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002024 return ("none");
2025 switch (def->type) {
2026 case XML_RELAXNG_EMPTY:
2027 return ("empty");
2028 case XML_RELAXNG_NOT_ALLOWED:
2029 return ("notAllowed");
2030 case XML_RELAXNG_EXCEPT:
2031 return ("except");
2032 case XML_RELAXNG_TEXT:
2033 return ("text");
2034 case XML_RELAXNG_ELEMENT:
2035 return ("element");
2036 case XML_RELAXNG_DATATYPE:
2037 return ("datatype");
2038 case XML_RELAXNG_VALUE:
2039 return ("value");
2040 case XML_RELAXNG_LIST:
2041 return ("list");
2042 case XML_RELAXNG_ATTRIBUTE:
2043 return ("attribute");
2044 case XML_RELAXNG_DEF:
2045 return ("def");
2046 case XML_RELAXNG_REF:
2047 return ("ref");
2048 case XML_RELAXNG_EXTERNALREF:
2049 return ("externalRef");
2050 case XML_RELAXNG_PARENTREF:
2051 return ("parentRef");
2052 case XML_RELAXNG_OPTIONAL:
2053 return ("optional");
2054 case XML_RELAXNG_ZEROORMORE:
2055 return ("zeroOrMore");
2056 case XML_RELAXNG_ONEORMORE:
2057 return ("oneOrMore");
2058 case XML_RELAXNG_CHOICE:
2059 return ("choice");
2060 case XML_RELAXNG_GROUP:
2061 return ("group");
2062 case XML_RELAXNG_INTERLEAVE:
2063 return ("interleave");
2064 case XML_RELAXNG_START:
2065 return ("start");
2066 case XML_RELAXNG_NOOP:
2067 return ("noop");
2068 case XML_RELAXNG_PARAM:
2069 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002070 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002071 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002072}
Daniel Veillardd2298792003-02-14 16:54:11 +00002073
Daniel Veillard6eadf632003-01-23 18:29:16 +00002074/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002075 * xmlRelaxNGGetErrorString:
2076 * @err: the error code
2077 * @arg1: the first string argument
2078 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002079 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002080 * computes a formatted error string for the given error code and args
2081 *
2082 * Returns the error string, it must be deallocated by the caller
2083 */
2084static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002085xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2086 const xmlChar * arg2)
2087{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002088 char msg[1000];
2089
2090 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002091 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002092 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002093 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002094
2095 msg[0] = 0;
2096 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002097 case XML_RELAXNG_OK:
2098 return (NULL);
2099 case XML_RELAXNG_ERR_MEMORY:
2100 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002101 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002102 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2103 break;
2104 case XML_RELAXNG_ERR_TYPEVAL:
2105 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2106 arg2);
2107 break;
2108 case XML_RELAXNG_ERR_DUPID:
2109 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2110 break;
2111 case XML_RELAXNG_ERR_TYPECMP:
2112 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2113 break;
2114 case XML_RELAXNG_ERR_NOSTATE:
2115 return (xmlCharStrdup("Internal error: no state\n"));
2116 case XML_RELAXNG_ERR_NODEFINE:
2117 return (xmlCharStrdup("Internal error: no define\n"));
2118 case XML_RELAXNG_ERR_INTERNAL:
2119 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2120 break;
2121 case XML_RELAXNG_ERR_LISTEXTRA:
2122 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2123 break;
2124 case XML_RELAXNG_ERR_INTERNODATA:
2125 return (xmlCharStrdup
2126 ("Internal: interleave block has no data\n"));
2127 case XML_RELAXNG_ERR_INTERSEQ:
2128 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2129 case XML_RELAXNG_ERR_INTEREXTRA:
2130 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2131 break;
2132 case XML_RELAXNG_ERR_ELEMNAME:
2133 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2134 arg2);
2135 break;
2136 case XML_RELAXNG_ERR_ELEMNONS:
2137 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2138 arg1);
2139 break;
2140 case XML_RELAXNG_ERR_ELEMWRONGNS:
2141 snprintf(msg, 1000,
2142 "Element %s has wrong namespace: expecting %s\n", arg1,
2143 arg2);
2144 break;
2145 case XML_RELAXNG_ERR_ELEMWRONG:
2146 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2147 break;
2148 case XML_RELAXNG_ERR_TEXTWRONG:
2149 snprintf(msg, 1000,
2150 "Did not expect text in element %s content\n", arg1);
2151 break;
2152 case XML_RELAXNG_ERR_ELEMEXTRANS:
2153 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2154 arg1);
2155 break;
2156 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2157 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2158 break;
2159 case XML_RELAXNG_ERR_NOELEM:
2160 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2161 arg1);
2162 break;
2163 case XML_RELAXNG_ERR_NOTELEM:
2164 return (xmlCharStrdup("Expecting an element got text\n"));
2165 case XML_RELAXNG_ERR_ATTRVALID:
2166 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2167 arg1);
2168 break;
2169 case XML_RELAXNG_ERR_CONTENTVALID:
2170 snprintf(msg, 1000, "Element %s failed to validate content\n",
2171 arg1);
2172 break;
2173 case XML_RELAXNG_ERR_EXTRACONTENT:
2174 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2175 arg1, arg2);
2176 break;
2177 case XML_RELAXNG_ERR_INVALIDATTR:
2178 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2179 arg1, arg2);
2180 break;
2181 case XML_RELAXNG_ERR_LACKDATA:
2182 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2183 arg1);
2184 break;
2185 case XML_RELAXNG_ERR_DATAELEM:
2186 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2187 arg1);
2188 break;
2189 case XML_RELAXNG_ERR_VALELEM:
2190 snprintf(msg, 1000, "Value element %s has child elements\n",
2191 arg1);
2192 break;
2193 case XML_RELAXNG_ERR_LISTELEM:
2194 snprintf(msg, 1000, "List element %s has child elements\n",
2195 arg1);
2196 break;
2197 case XML_RELAXNG_ERR_DATATYPE:
2198 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2199 break;
2200 case XML_RELAXNG_ERR_VALUE:
2201 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2202 break;
2203 case XML_RELAXNG_ERR_LIST:
2204 return (xmlCharStrdup("Error validating list\n"));
2205 case XML_RELAXNG_ERR_NOGRAMMAR:
2206 return (xmlCharStrdup("No top grammar defined\n"));
2207 case XML_RELAXNG_ERR_EXTRADATA:
2208 return (xmlCharStrdup("Extra data in the document\n"));
2209 default:
2210 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002211 }
2212 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002213 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002214 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002215 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002216 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002217}
2218
2219/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002220 * xmlRelaxNGShowValidError:
2221 * @ctxt: the validation context
2222 * @err: the error number
2223 * @node: the node
2224 * @child: the node child generating the problem.
2225 * @arg1: the first argument
2226 * @arg2: the second argument
2227 *
2228 * Show a validation error.
2229 */
2230static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002231xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2232 xmlRelaxNGValidErr err, xmlNodePtr node,
2233 xmlNodePtr child, const xmlChar * arg1,
2234 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002235{
2236 xmlChar *msg;
2237
Daniel Veillardb30ca312005-09-04 13:50:03 +00002238 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002239 return;
2240
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002241#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002242 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002243#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002244 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2245 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002246 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002247
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002248 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002249 ctxt->errNo = err;
2250 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2251 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002252 xmlFree(msg);
2253}
2254
2255/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002256 * xmlRelaxNGPopErrors:
2257 * @ctxt: the validation context
2258 * @level: the error level in the stack
2259 *
2260 * pop and discard all errors until the given level is reached
2261 */
2262static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002263xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2264{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002265 int i;
2266 xmlRelaxNGValidErrorPtr err;
2267
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002268#ifdef DEBUG_ERROR
2269 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002270 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002271#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002272 for (i = level; i < ctxt->errNr; i++) {
2273 err = &ctxt->errTab[i];
2274 if (err->flags & ERROR_IS_DUP) {
2275 if (err->arg1 != NULL)
2276 xmlFree((xmlChar *) err->arg1);
2277 err->arg1 = NULL;
2278 if (err->arg2 != NULL)
2279 xmlFree((xmlChar *) err->arg2);
2280 err->arg2 = NULL;
2281 err->flags = 0;
2282 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002283 }
2284 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002285 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002286 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002287}
Daniel Veillard4c004142003-10-07 11:33:24 +00002288
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002289/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002290 * xmlRelaxNGDumpValidError:
2291 * @ctxt: the validation context
2292 *
2293 * Show all validation error over a given index.
2294 */
2295static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002296xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2297{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002298 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002299 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002300
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002301#ifdef DEBUG_ERROR
2302 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002303 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002304#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002305 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2306 err = &ctxt->errTab[i];
2307 if (k < MAX_ERROR) {
2308 for (j = 0; j < i; j++) {
2309 dup = &ctxt->errTab[j];
2310 if ((err->err == dup->err) && (err->node == dup->node) &&
2311 (xmlStrEqual(err->arg1, dup->arg1)) &&
2312 (xmlStrEqual(err->arg2, dup->arg2))) {
2313 goto skip;
2314 }
2315 }
2316 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2317 err->arg1, err->arg2);
2318 k++;
2319 }
2320 skip:
2321 if (err->flags & ERROR_IS_DUP) {
2322 if (err->arg1 != NULL)
2323 xmlFree((xmlChar *) err->arg1);
2324 err->arg1 = NULL;
2325 if (err->arg2 != NULL)
2326 xmlFree((xmlChar *) err->arg2);
2327 err->arg2 = NULL;
2328 err->flags = 0;
2329 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002330 }
2331 ctxt->errNr = 0;
2332}
Daniel Veillard4c004142003-10-07 11:33:24 +00002333
Daniel Veillard42f12e92003-03-07 18:32:59 +00002334/**
2335 * xmlRelaxNGAddValidError:
2336 * @ctxt: the validation context
2337 * @err: the error number
2338 * @arg1: the first argument
2339 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002340 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002341 *
2342 * Register a validation error, either generating it if it's sure
2343 * or stacking it for later handling if unsure.
2344 */
2345static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002346xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2347 xmlRelaxNGValidErr err, const xmlChar * arg1,
2348 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002349{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002350 if (ctxt == NULL)
2351 return;
2352 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002353 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002354
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002355#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002356 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002357#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002358 /*
2359 * generate the error directly
2360 */
William M. Brack60929622004-03-27 17:54:18 +00002361 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2362 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002363 xmlNodePtr node, seq;
2364
2365 /*
2366 * Flush first any stacked error which might be the
2367 * real cause of the problem.
2368 */
2369 if (ctxt->errNr != 0)
2370 xmlRelaxNGDumpValidError(ctxt);
2371 if (ctxt->state != NULL) {
2372 node = ctxt->state->node;
2373 seq = ctxt->state->seq;
2374 } else {
2375 node = seq = NULL;
2376 }
2377 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002378 }
2379 /*
2380 * Stack the error for later processing if needed
2381 */
2382 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002383 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002384 }
2385}
2386
Daniel Veillard6eadf632003-01-23 18:29:16 +00002387
2388/************************************************************************
2389 * *
2390 * Type library hooks *
2391 * *
2392 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002393static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002394 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002395
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002396/**
2397 * xmlRelaxNGSchemaTypeHave:
2398 * @data: data needed for the library
2399 * @type: the type name
2400 *
2401 * Check if the given type is provided by
2402 * the W3C XMLSchema Datatype library.
2403 *
2404 * Returns 1 if yes, 0 if no and -1 in case of error.
2405 */
2406static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002407xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2408{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002409 xmlSchemaTypePtr typ;
2410
2411 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002412 return (-1);
2413 typ = xmlSchemaGetPredefinedType(type,
2414 BAD_CAST
2415 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002416 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002417 return (0);
2418 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002419}
2420
2421/**
2422 * xmlRelaxNGSchemaTypeCheck:
2423 * @data: data needed for the library
2424 * @type: the type name
2425 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002426 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002427 *
2428 * Check if the given type and value are validated by
2429 * the W3C XMLSchema Datatype library.
2430 *
2431 * Returns 1 if yes, 0 if no and -1 in case of error.
2432 */
2433static int
2434xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002435 const xmlChar * type,
2436 const xmlChar * value,
2437 void **result, xmlNodePtr node)
2438{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002439 xmlSchemaTypePtr typ;
2440 int ret;
2441
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002442 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002443 return (-1);
2444 typ = xmlSchemaGetPredefinedType(type,
2445 BAD_CAST
2446 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002447 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002448 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002449 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002450 (xmlSchemaValPtr *) result, node);
2451 if (ret == 2) /* special ID error code */
2452 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002453 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002454 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002455 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002456 return (0);
2457 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002458}
2459
2460/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002461 * xmlRelaxNGSchemaFacetCheck:
2462 * @data: data needed for the library
2463 * @type: the type name
2464 * @facet: the facet name
2465 * @val: the facet value
2466 * @strval: the string value
2467 * @value: the value to check
2468 *
2469 * Function provided by a type library to check a value facet
2470 *
2471 * Returns 1 if yes, 0 if no and -1 in case of error.
2472 */
2473static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002474xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2475 const xmlChar * type, const xmlChar * facetname,
2476 const xmlChar * val, const xmlChar * strval,
2477 void *value)
2478{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002479 xmlSchemaFacetPtr facet;
2480 xmlSchemaTypePtr typ;
2481 int ret;
2482
2483 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002484 return (-1);
2485 typ = xmlSchemaGetPredefinedType(type,
2486 BAD_CAST
2487 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002488 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002489 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002490
2491 facet = xmlSchemaNewFacet();
2492 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002493 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002494
Daniel Veillard4c004142003-10-07 11:33:24 +00002495 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002496 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002497 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002498 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002499 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002500 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002501 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002502 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002503 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002504 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002505 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002506 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002507 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002508 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002509 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002510 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002511 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002512 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002513 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002514 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002515 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002516 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2517 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2518 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2519 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002520 xmlSchemaFreeFacet(facet);
2521 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002522 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002523 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002524 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2525 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002526 xmlSchemaFreeFacet(facet);
2527 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002528 }
2529 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2530 xmlSchemaFreeFacet(facet);
2531 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002532 return (-1);
2533 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002534}
2535
2536/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002537 * xmlRelaxNGSchemaFreeValue:
2538 * @data: data needed for the library
2539 * @value: the value to free
2540 *
2541 * Function provided by a type library to free a Schemas value
2542 *
2543 * Returns 1 if yes, 0 if no and -1 in case of error.
2544 */
2545static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002546xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2547{
Daniel Veillard80b19092003-03-28 13:29:53 +00002548 xmlSchemaFreeValue(value);
2549}
2550
2551/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002552 * xmlRelaxNGSchemaTypeCompare:
2553 * @data: data needed for the library
2554 * @type: the type name
2555 * @value1: the first value
2556 * @value2: the second value
2557 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002558 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002559 * Datatype library.
2560 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002561 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002562 */
2563static int
2564xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002565 const xmlChar * type,
2566 const xmlChar * value1,
2567 xmlNodePtr ctxt1,
2568 void *comp1,
2569 const xmlChar * value2, xmlNodePtr ctxt2)
2570{
Daniel Veillard80b19092003-03-28 13:29:53 +00002571 int ret;
2572 xmlSchemaTypePtr typ;
2573 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2574
2575 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002576 return (-1);
2577 typ = xmlSchemaGetPredefinedType(type,
2578 BAD_CAST
2579 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002580 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002581 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002582 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002583 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2584 if (ret != 0)
2585 return (-1);
2586 if (res1 == NULL)
2587 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002588 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002589 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002590 }
2591 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002592 if (ret != 0) {
Daniel Veillardf4644032005-06-13 11:41:31 +00002593 if ((comp1 == NULL) && (res1 != NULL))
2594 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002595 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002596 }
2597 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002598 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002599 }
2600 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002601 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002602 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002603 xmlSchemaFreeValue(res2);
2604 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002605 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002606 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002607 return (1);
2608 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002609}
Daniel Veillard4c004142003-10-07 11:33:24 +00002610
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002611/**
2612 * xmlRelaxNGDefaultTypeHave:
2613 * @data: data needed for the library
2614 * @type: the type name
2615 *
2616 * Check if the given type is provided by
2617 * the default datatype library.
2618 *
2619 * Returns 1 if yes, 0 if no and -1 in case of error.
2620 */
2621static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002622xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2623 const xmlChar * type)
2624{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002625 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002626 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002627 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002628 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002629 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002630 return (1);
2631 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002632}
2633
2634/**
2635 * xmlRelaxNGDefaultTypeCheck:
2636 * @data: data needed for the library
2637 * @type: the type name
2638 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002639 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002640 *
2641 * Check if the given type and value are validated by
2642 * the default datatype library.
2643 *
2644 * Returns 1 if yes, 0 if no and -1 in case of error.
2645 */
2646static int
2647xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002648 const xmlChar * type ATTRIBUTE_UNUSED,
2649 const xmlChar * value ATTRIBUTE_UNUSED,
2650 void **result ATTRIBUTE_UNUSED,
2651 xmlNodePtr node ATTRIBUTE_UNUSED)
2652{
Daniel Veillardd4310742003-02-18 21:12:46 +00002653 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002654 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002655 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002656 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002657 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002658 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002659 }
2660
Daniel Veillard4c004142003-10-07 11:33:24 +00002661 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002662}
2663
2664/**
2665 * xmlRelaxNGDefaultTypeCompare:
2666 * @data: data needed for the library
2667 * @type: the type name
2668 * @value1: the first value
2669 * @value2: the second value
2670 *
2671 * Compare two values accordingly a type from the default
2672 * datatype library.
2673 *
2674 * Returns 1 if yes, 0 if no and -1 in case of error.
2675 */
2676static int
2677xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002678 const xmlChar * type,
2679 const xmlChar * value1,
2680 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2681 void *comp1 ATTRIBUTE_UNUSED,
2682 const xmlChar * value2,
2683 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2684{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002685 int ret = -1;
2686
2687 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002688 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002689 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002690 if (!xmlStrEqual(value1, value2)) {
2691 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002692
Daniel Veillard4c004142003-10-07 11:33:24 +00002693 /*
2694 * TODO: trivial optimizations are possible by
2695 * computing at compile-time
2696 */
2697 nval = xmlRelaxNGNormalize(NULL, value1);
2698 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002699
Daniel Veillard4c004142003-10-07 11:33:24 +00002700 if ((nval == NULL) || (nvalue == NULL))
2701 ret = -1;
2702 else if (xmlStrEqual(nval, nvalue))
2703 ret = 1;
2704 else
2705 ret = 0;
2706 if (nval != NULL)
2707 xmlFree(nval);
2708 if (nvalue != NULL)
2709 xmlFree(nvalue);
2710 } else
2711 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002712 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002713 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002714}
Daniel Veillard4c004142003-10-07 11:33:24 +00002715
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002716static int xmlRelaxNGTypeInitialized = 0;
2717static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2718
2719/**
2720 * xmlRelaxNGFreeTypeLibrary:
2721 * @lib: the type library structure
2722 * @namespace: the URI bound to the library
2723 *
2724 * Free the structure associated to the type library
2725 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002726static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002727xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002728 const xmlChar * namespace ATTRIBUTE_UNUSED)
2729{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002730 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002731 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002732 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002733 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002734 xmlFree(lib);
2735}
2736
2737/**
2738 * xmlRelaxNGRegisterTypeLibrary:
2739 * @namespace: the URI bound to the library
2740 * @data: data associated to the library
2741 * @have: the provide function
2742 * @check: the checking function
2743 * @comp: the comparison function
2744 *
2745 * Register a new type library
2746 *
2747 * Returns 0 in case of success and -1 in case of error.
2748 */
2749static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002750xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2751 xmlRelaxNGTypeHave have,
2752 xmlRelaxNGTypeCheck check,
2753 xmlRelaxNGTypeCompare comp,
2754 xmlRelaxNGFacetCheck facet,
2755 xmlRelaxNGTypeFree freef)
2756{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002757 xmlRelaxNGTypeLibraryPtr lib;
2758 int ret;
2759
2760 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002761 (check == NULL) || (comp == NULL))
2762 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002763 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002764 xmlGenericError(xmlGenericErrorContext,
2765 "Relax-NG types library '%s' already registered\n",
2766 namespace);
2767 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002768 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002769 lib =
2770 (xmlRelaxNGTypeLibraryPtr)
2771 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002772 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002773 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002774 return (-1);
2775 }
2776 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2777 lib->namespace = xmlStrdup(namespace);
2778 lib->data = data;
2779 lib->have = have;
2780 lib->comp = comp;
2781 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002782 lib->facet = facet;
2783 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002784 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2785 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002786 xmlGenericError(xmlGenericErrorContext,
2787 "Relax-NG types library failed to register '%s'\n",
2788 namespace);
2789 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2790 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002791 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002792 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002793}
2794
2795/**
2796 * xmlRelaxNGInitTypes:
2797 *
2798 * Initilize the default type libraries.
2799 *
2800 * Returns 0 in case of success and -1 in case of error.
2801 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002802int
Daniel Veillard4c004142003-10-07 11:33:24 +00002803xmlRelaxNGInitTypes(void)
2804{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002805 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002806 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002807 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2808 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002809 xmlGenericError(xmlGenericErrorContext,
2810 "Failed to allocate sh table for Relax-NG types\n");
2811 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002812 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002813 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2814 "http://www.w3.org/2001/XMLSchema-datatypes",
2815 NULL, xmlRelaxNGSchemaTypeHave,
2816 xmlRelaxNGSchemaTypeCheck,
2817 xmlRelaxNGSchemaTypeCompare,
2818 xmlRelaxNGSchemaFacetCheck,
2819 xmlRelaxNGSchemaFreeValue);
2820 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2821 xmlRelaxNGDefaultTypeHave,
2822 xmlRelaxNGDefaultTypeCheck,
2823 xmlRelaxNGDefaultTypeCompare, NULL,
2824 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002825 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002826 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002827}
2828
2829/**
2830 * xmlRelaxNGCleanupTypes:
2831 *
2832 * Cleanup the default Schemas type library associated to RelaxNG
2833 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002834void
2835xmlRelaxNGCleanupTypes(void)
2836{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002837 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002838 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002839 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002840 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002841 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002842 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002843}
2844
2845/************************************************************************
2846 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002847 * Compiling element content into regexp *
2848 * *
2849 * Sometime the element content can be compiled into a pure regexp, *
2850 * This allows a faster execution and streamability at that level *
2851 * *
2852 ************************************************************************/
2853
Daniel Veillard52b48c72003-04-13 19:53:42 +00002854static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2855 xmlRelaxNGDefinePtr def);
2856
Daniel Veillard952379b2003-03-17 15:37:12 +00002857/**
2858 * xmlRelaxNGIsCompileable:
2859 * @define: the definition to check
2860 *
2861 * Check if a definition is nullable.
2862 *
2863 * Returns 1 if yes, 0 if no and -1 in case of error
2864 */
2865static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002866xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2867{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002868 int ret = -1;
2869
Daniel Veillard952379b2003-03-17 15:37:12 +00002870 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002871 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002872 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002873 if ((def->type != XML_RELAXNG_ELEMENT) &&
2874 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002875 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002876 if ((def->type != XML_RELAXNG_ELEMENT) &&
2877 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002878 return (0);
2879 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002880 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002881 ret = xmlRelaxNGIsCompileable(def->content);
2882 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002883 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002884 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002885 ret = 1;
2886 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002887 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002888 /*
2889 * Check if the element content is compileable
2890 */
2891 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2892 ((def->dflags & IS_COMPILABLE) == 0)) {
2893 xmlRelaxNGDefinePtr list;
2894
2895 list = def->content;
2896 while (list != NULL) {
2897 ret = xmlRelaxNGIsCompileable(list);
2898 if (ret != 1)
2899 break;
2900 list = list->next;
2901 }
William M. Brack60929622004-03-27 17:54:18 +00002902 /*
2903 * Because the routine is recursive, we must guard against
2904 * discovering both COMPILABLE and NOT_COMPILABLE
2905 */
2906 if (ret == 0) {
2907 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002908 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002909 }
2910 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002911 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002912#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002913 if (ret == 1) {
2914 xmlGenericError(xmlGenericErrorContext,
2915 "element content for %s is compilable\n",
2916 def->name);
2917 } else if (ret == 0) {
2918 xmlGenericError(xmlGenericErrorContext,
2919 "element content for %s is not compilable\n",
2920 def->name);
2921 } else {
2922 xmlGenericError(xmlGenericErrorContext,
2923 "Problem in RelaxNGIsCompileable for element %s\n",
2924 def->name);
2925 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002926#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002927 }
2928 /*
2929 * All elements return a compileable status unless they
2930 * are generic like anyName
2931 */
2932 if ((def->nameClass != NULL) || (def->name == NULL))
2933 ret = 0;
2934 else
2935 ret = 1;
2936 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002937 case XML_RELAXNG_REF:
2938 case XML_RELAXNG_EXTERNALREF:
2939 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002940 if (def->depth == -20) {
2941 return (1);
2942 } else {
2943 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002944
Daniel Veillard4c004142003-10-07 11:33:24 +00002945 def->depth = -20;
2946 list = def->content;
2947 while (list != NULL) {
2948 ret = xmlRelaxNGIsCompileable(list);
2949 if (ret != 1)
2950 break;
2951 list = list->next;
2952 }
2953 }
2954 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002955 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002956 case XML_RELAXNG_OPTIONAL:
2957 case XML_RELAXNG_ZEROORMORE:
2958 case XML_RELAXNG_ONEORMORE:
2959 case XML_RELAXNG_CHOICE:
2960 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002961 case XML_RELAXNG_DEF:{
2962 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002963
Daniel Veillard4c004142003-10-07 11:33:24 +00002964 list = def->content;
2965 while (list != NULL) {
2966 ret = xmlRelaxNGIsCompileable(list);
2967 if (ret != 1)
2968 break;
2969 list = list->next;
2970 }
2971 break;
2972 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002973 case XML_RELAXNG_EXCEPT:
2974 case XML_RELAXNG_ATTRIBUTE:
2975 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002976 case XML_RELAXNG_DATATYPE:
2977 case XML_RELAXNG_LIST:
2978 case XML_RELAXNG_PARAM:
2979 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002980 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002981 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002982 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002983 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002984 if (ret == 0)
2985 def->dflags |= IS_NOT_COMPILABLE;
2986 if (ret == 1)
2987 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002988#ifdef DEBUG_COMPILE
2989 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002990 xmlGenericError(xmlGenericErrorContext,
2991 "RelaxNGIsCompileable %s : true\n",
2992 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002993 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002994 xmlGenericError(xmlGenericErrorContext,
2995 "RelaxNGIsCompileable %s : false\n",
2996 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002997 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002998 xmlGenericError(xmlGenericErrorContext,
2999 "Problem in RelaxNGIsCompileable %s\n",
3000 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003001 }
3002#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003003 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003004}
3005
3006/**
3007 * xmlRelaxNGCompile:
3008 * ctxt: the RelaxNG parser context
3009 * @define: the definition tree to compile
3010 *
3011 * Compile the set of definitions, it works recursively, till the
3012 * element boundaries, where it tries to compile the content if possible
3013 *
3014 * Returns 0 if success and -1 in case of error
3015 */
3016static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003017xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3018{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003019 int ret = 0;
3020 xmlRelaxNGDefinePtr list;
3021
Daniel Veillard4c004142003-10-07 11:33:24 +00003022 if ((ctxt == NULL) || (def == NULL))
3023 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003024
Daniel Veillard4c004142003-10-07 11:33:24 +00003025 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003026 case XML_RELAXNG_START:
3027 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003028 xmlAutomataPtr oldam = ctxt->am;
3029 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003030
3031 def->depth = -25;
3032
Daniel Veillard4c004142003-10-07 11:33:24 +00003033 list = def->content;
3034 ctxt->am = xmlNewAutomata();
3035 if (ctxt->am == NULL)
3036 return (-1);
3037 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3038 while (list != NULL) {
3039 xmlRelaxNGCompile(ctxt, list);
3040 list = list->next;
3041 }
3042 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3043 def->contModel = xmlAutomataCompile(ctxt->am);
3044 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003045
Daniel Veillard4c004142003-10-07 11:33:24 +00003046 xmlFreeAutomata(ctxt->am);
3047 ctxt->state = oldstate;
3048 ctxt->am = oldam;
3049 }
3050 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003051 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003052 if ((ctxt->am != NULL) && (def->name != NULL)) {
3053 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3054 ctxt->state, NULL,
3055 def->name, def->ns,
3056 def);
3057 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003058 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003059 xmlAutomataPtr oldam = ctxt->am;
3060 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003061
3062 def->depth = -25;
3063
Daniel Veillard4c004142003-10-07 11:33:24 +00003064 list = def->content;
3065 ctxt->am = xmlNewAutomata();
3066 if (ctxt->am == NULL)
3067 return (-1);
3068 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3069 while (list != NULL) {
3070 xmlRelaxNGCompile(ctxt, list);
3071 list = list->next;
3072 }
3073 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3074 def->contModel = xmlAutomataCompile(ctxt->am);
3075 if (!xmlRegexpIsDeterminist(def->contModel)) {
3076 /*
3077 * we can only use the automata if it is determinist
3078 */
3079 xmlRegFreeRegexp(def->contModel);
3080 def->contModel = NULL;
3081 }
3082 xmlFreeAutomata(ctxt->am);
3083 ctxt->state = oldstate;
3084 ctxt->am = oldam;
3085 } else {
3086 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003087
Daniel Veillard4c004142003-10-07 11:33:24 +00003088 /*
3089 * we can't build the content model for this element content
3090 * but it still might be possible to build it for some of its
3091 * children, recurse.
3092 */
3093 ret = xmlRelaxNGTryCompile(ctxt, def);
3094 ctxt->am = oldam;
3095 }
3096 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003097 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003098 ret = xmlRelaxNGCompile(ctxt, def->content);
3099 break;
3100 case XML_RELAXNG_OPTIONAL:{
3101 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003102
Daniel Veillardfd780772009-08-26 18:35:29 +02003103 list = def->content;
3104 while (list != NULL) {
3105 xmlRelaxNGCompile(ctxt, list);
3106 list = list->next;
3107 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003108 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3109 break;
3110 }
3111 case XML_RELAXNG_ZEROORMORE:{
3112 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003113
Daniel Veillard4c004142003-10-07 11:33:24 +00003114 ctxt->state =
3115 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3116 oldstate = ctxt->state;
3117 list = def->content;
3118 while (list != NULL) {
3119 xmlRelaxNGCompile(ctxt, list);
3120 list = list->next;
3121 }
3122 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3123 ctxt->state =
3124 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3125 break;
3126 }
3127 case XML_RELAXNG_ONEORMORE:{
3128 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003129
Daniel Veillard4c004142003-10-07 11:33:24 +00003130 list = def->content;
3131 while (list != NULL) {
3132 xmlRelaxNGCompile(ctxt, list);
3133 list = list->next;
3134 }
3135 oldstate = ctxt->state;
3136 list = def->content;
3137 while (list != NULL) {
3138 xmlRelaxNGCompile(ctxt, list);
3139 list = list->next;
3140 }
3141 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3142 ctxt->state =
3143 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3144 break;
3145 }
3146 case XML_RELAXNG_CHOICE:{
3147 xmlAutomataStatePtr target = NULL;
3148 xmlAutomataStatePtr oldstate = ctxt->state;
3149
3150 list = def->content;
3151 while (list != NULL) {
3152 ctxt->state = oldstate;
3153 ret = xmlRelaxNGCompile(ctxt, list);
3154 if (ret != 0)
3155 break;
3156 if (target == NULL)
3157 target = ctxt->state;
3158 else {
3159 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3160 target);
3161 }
3162 list = list->next;
3163 }
3164 ctxt->state = target;
3165
3166 break;
3167 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003168 case XML_RELAXNG_REF:
3169 case XML_RELAXNG_EXTERNALREF:
3170 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003171 case XML_RELAXNG_GROUP:
3172 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003173 list = def->content;
3174 while (list != NULL) {
3175 ret = xmlRelaxNGCompile(ctxt, list);
3176 if (ret != 0)
3177 break;
3178 list = list->next;
3179 }
3180 break;
3181 case XML_RELAXNG_TEXT:{
3182 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003183
Daniel Veillard4c004142003-10-07 11:33:24 +00003184 ctxt->state =
3185 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3186 oldstate = ctxt->state;
3187 xmlRelaxNGCompile(ctxt, def->content);
3188 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3189 ctxt->state, BAD_CAST "#text",
3190 NULL);
3191 ctxt->state =
3192 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3193 break;
3194 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003195 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003196 ctxt->state =
3197 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3198 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003199 case XML_RELAXNG_EXCEPT:
3200 case XML_RELAXNG_ATTRIBUTE:
3201 case XML_RELAXNG_INTERLEAVE:
3202 case XML_RELAXNG_NOT_ALLOWED:
3203 case XML_RELAXNG_DATATYPE:
3204 case XML_RELAXNG_LIST:
3205 case XML_RELAXNG_PARAM:
3206 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003207 /* This should not happen and generate an internal error */
3208 fprintf(stderr, "RNG internal error trying to compile %s\n",
3209 xmlRelaxNGDefName(def));
3210 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003211 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003212 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003213}
3214
3215/**
3216 * xmlRelaxNGTryCompile:
3217 * ctxt: the RelaxNG parser context
3218 * @define: the definition tree to compile
3219 *
3220 * Try to compile the set of definitions, it works recursively,
3221 * possibly ignoring parts which cannot be compiled.
3222 *
3223 * Returns 0 if success and -1 in case of error
3224 */
3225static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003226xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3227{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003228 int ret = 0;
3229 xmlRelaxNGDefinePtr list;
3230
Daniel Veillard4c004142003-10-07 11:33:24 +00003231 if ((ctxt == NULL) || (def == NULL))
3232 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003233
3234 if ((def->type == XML_RELAXNG_START) ||
3235 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003236 ret = xmlRelaxNGIsCompileable(def);
3237 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3238 ctxt->am = NULL;
3239 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003240#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003241 if (ret == 0) {
3242 if (def->type == XML_RELAXNG_START)
3243 xmlGenericError(xmlGenericErrorContext,
3244 "compiled the start\n");
3245 else
3246 xmlGenericError(xmlGenericErrorContext,
3247 "compiled element %s\n", def->name);
3248 } else {
3249 if (def->type == XML_RELAXNG_START)
3250 xmlGenericError(xmlGenericErrorContext,
3251 "failed to compile the start\n");
3252 else
3253 xmlGenericError(xmlGenericErrorContext,
3254 "failed to compile element %s\n",
3255 def->name);
3256 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003257#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003258 return (ret);
3259 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003260 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003261 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003262 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003263 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3264 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003265 case XML_RELAXNG_TEXT:
3266 case XML_RELAXNG_DATATYPE:
3267 case XML_RELAXNG_LIST:
3268 case XML_RELAXNG_PARAM:
3269 case XML_RELAXNG_VALUE:
3270 case XML_RELAXNG_EMPTY:
3271 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003272 ret = 0;
3273 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003274 case XML_RELAXNG_OPTIONAL:
3275 case XML_RELAXNG_ZEROORMORE:
3276 case XML_RELAXNG_ONEORMORE:
3277 case XML_RELAXNG_CHOICE:
3278 case XML_RELAXNG_GROUP:
3279 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003280 case XML_RELAXNG_START:
3281 case XML_RELAXNG_REF:
3282 case XML_RELAXNG_EXTERNALREF:
3283 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003284 list = def->content;
3285 while (list != NULL) {
3286 ret = xmlRelaxNGTryCompile(ctxt, list);
3287 if (ret != 0)
3288 break;
3289 list = list->next;
3290 }
3291 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003292 case XML_RELAXNG_EXCEPT:
3293 case XML_RELAXNG_ATTRIBUTE:
3294 case XML_RELAXNG_INTERLEAVE:
3295 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003296 ret = 0;
3297 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003298 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003299 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003300}
3301
3302/************************************************************************
3303 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003304 * Parsing functions *
3305 * *
3306 ************************************************************************/
3307
Daniel Veillard4c004142003-10-07 11:33:24 +00003308static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3309 ctxt, xmlNodePtr node);
3310static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3311 ctxt, xmlNodePtr node);
3312static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3313 ctxt, xmlNodePtr nodes,
3314 int group);
3315static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3316 ctxt, xmlNodePtr node);
3317static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3318 xmlNodePtr node);
3319static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3320 xmlNodePtr nodes);
3321static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3322 ctxt, xmlNodePtr node,
3323 xmlRelaxNGDefinePtr
3324 def);
3325static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3326 ctxt, xmlNodePtr nodes);
3327static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3328 xmlRelaxNGDefinePtr define,
3329 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003330
3331
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003332#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003333
3334/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003335 * xmlRelaxNGIsNullable:
3336 * @define: the definition to verify
3337 *
3338 * Check if a definition is nullable.
3339 *
3340 * Returns 1 if yes, 0 if no and -1 in case of error
3341 */
3342static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003343xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3344{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003345 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003346
Daniel Veillardfd573f12003-03-16 17:52:32 +00003347 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003348 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003349
Daniel Veillarde063f482003-03-21 16:53:17 +00003350 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003351 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003352 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003353 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003354 switch (define->type) {
3355 case XML_RELAXNG_EMPTY:
3356 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003357 ret = 1;
3358 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003359 case XML_RELAXNG_NOOP:
3360 case XML_RELAXNG_DEF:
3361 case XML_RELAXNG_REF:
3362 case XML_RELAXNG_EXTERNALREF:
3363 case XML_RELAXNG_PARENTREF:
3364 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003365 ret = xmlRelaxNGIsNullable(define->content);
3366 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003367 case XML_RELAXNG_EXCEPT:
3368 case XML_RELAXNG_NOT_ALLOWED:
3369 case XML_RELAXNG_ELEMENT:
3370 case XML_RELAXNG_DATATYPE:
3371 case XML_RELAXNG_PARAM:
3372 case XML_RELAXNG_VALUE:
3373 case XML_RELAXNG_LIST:
3374 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003375 ret = 0;
3376 break;
3377 case XML_RELAXNG_CHOICE:{
3378 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003379
Daniel Veillard4c004142003-10-07 11:33:24 +00003380 while (list != NULL) {
3381 ret = xmlRelaxNGIsNullable(list);
3382 if (ret != 0)
3383 goto done;
3384 list = list->next;
3385 }
3386 ret = 0;
3387 break;
3388 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003389 case XML_RELAXNG_START:
3390 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003391 case XML_RELAXNG_GROUP:{
3392 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003393
Daniel Veillard4c004142003-10-07 11:33:24 +00003394 while (list != NULL) {
3395 ret = xmlRelaxNGIsNullable(list);
3396 if (ret != 1)
3397 goto done;
3398 list = list->next;
3399 }
3400 return (1);
3401 }
3402 default:
3403 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003404 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003405 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003406 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003407 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003408 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003409 define->dflags |= IS_NULLABLE;
3410 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003411}
3412
3413/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003414 * xmlRelaxNGIsBlank:
3415 * @str: a string
3416 *
3417 * Check if a string is ignorable c.f. 4.2. Whitespace
3418 *
3419 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3420 */
3421static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003422xmlRelaxNGIsBlank(xmlChar * str)
3423{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003424 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003425 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003426 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003427 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003428 return (0);
3429 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003430 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003431 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003432}
3433
Daniel Veillard6eadf632003-01-23 18:29:16 +00003434/**
3435 * xmlRelaxNGGetDataTypeLibrary:
3436 * @ctxt: a Relax-NG parser context
3437 * @node: the current data or value element
3438 *
3439 * Applies algorithm from 4.3. datatypeLibrary attribute
3440 *
3441 * Returns the datatypeLibary value or NULL if not found
3442 */
3443static xmlChar *
3444xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003445 xmlNodePtr node)
3446{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003447 xmlChar *ret, *escape;
3448
Daniel Veillard6eadf632003-01-23 18:29:16 +00003449 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003450 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3451 if (ret != NULL) {
3452 if (ret[0] == 0) {
3453 xmlFree(ret);
3454 return (NULL);
3455 }
3456 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3457 if (escape == NULL) {
3458 return (ret);
3459 }
3460 xmlFree(ret);
3461 return (escape);
3462 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003463 }
3464 node = node->parent;
3465 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003466 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3467 if (ret != NULL) {
3468 if (ret[0] == 0) {
3469 xmlFree(ret);
3470 return (NULL);
3471 }
3472 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3473 if (escape == NULL) {
3474 return (ret);
3475 }
3476 xmlFree(ret);
3477 return (escape);
3478 }
3479 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003480 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003481 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003482}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003483
3484/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003485 * xmlRelaxNGParseValue:
3486 * @ctxt: a Relax-NG parser context
3487 * @node: the data node.
3488 *
3489 * parse the content of a RelaxNG value node.
3490 *
3491 * Returns the definition pointer or NULL in case of error
3492 */
3493static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003494xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3495{
Daniel Veillardedc91922003-01-26 00:52:04 +00003496 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003497 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003498 xmlChar *type;
3499 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003500 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003501
Daniel Veillardfd573f12003-03-16 17:52:32 +00003502 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003503 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003504 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003505 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003506
3507 type = xmlGetProp(node, BAD_CAST "type");
3508 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003509 xmlRelaxNGNormExtSpace(type);
3510 if (xmlValidateNCName(type, 0)) {
3511 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3512 "value type '%s' is not an NCName\n", type, NULL);
3513 }
3514 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3515 if (library == NULL)
3516 library =
3517 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003518
Daniel Veillard4c004142003-10-07 11:33:24 +00003519 def->name = type;
3520 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003521
Daniel Veillard4c004142003-10-07 11:33:24 +00003522 lib = (xmlRelaxNGTypeLibraryPtr)
3523 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3524 if (lib == NULL) {
3525 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3526 "Use of unregistered type library '%s'\n", library,
3527 NULL);
3528 def->data = NULL;
3529 } else {
3530 def->data = lib;
3531 if (lib->have == NULL) {
3532 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3533 "Internal error with type library '%s': no 'have'\n",
3534 library, NULL);
3535 } else {
3536 success = lib->have(lib->data, def->name);
3537 if (success != 1) {
3538 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3539 "Error type '%s' is not exported by type library '%s'\n",
3540 def->name, library);
3541 }
3542 }
3543 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003544 }
3545 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003546 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003547 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003548 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3549 (node->children->next != NULL)) {
3550 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3551 "Expecting a single text value for <value>content\n",
3552 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003553 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003554 def->value = xmlNodeGetContent(node);
3555 if (def->value == NULL) {
3556 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3557 "Element <value> has no content\n", NULL, NULL);
3558 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3559 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003560
Daniel Veillard4c004142003-10-07 11:33:24 +00003561 success =
3562 lib->check(lib->data, def->name, def->value, &val, node);
3563 if (success != 1) {
3564 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3565 "Value '%s' is not acceptable for type '%s'\n",
3566 def->value, def->name);
3567 } else {
3568 if (val != NULL)
3569 def->attrs = val;
3570 }
3571 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003572 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003573 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003574}
3575
3576/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003577 * xmlRelaxNGParseData:
3578 * @ctxt: a Relax-NG parser context
3579 * @node: the data node.
3580 *
3581 * parse the content of a RelaxNG data node.
3582 *
3583 * Returns the definition pointer or NULL in case of error
3584 */
3585static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003586xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3587{
Daniel Veillard14b56432006-03-09 18:41:40 +00003588 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003589 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003590 xmlRelaxNGTypeLibraryPtr lib;
3591 xmlChar *type;
3592 xmlChar *library;
3593 xmlNodePtr content;
3594 int tmp;
3595
3596 type = xmlGetProp(node, BAD_CAST "type");
3597 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003598 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3599 NULL);
3600 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003601 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003602 xmlRelaxNGNormExtSpace(type);
3603 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003604 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3605 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003606 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003607 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3608 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003609 library =
3610 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003611
Daniel Veillardfd573f12003-03-16 17:52:32 +00003612 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003613 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003614 xmlFree(type);
3615 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003616 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003617 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003618 def->name = type;
3619 def->ns = library;
3620
3621 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003622 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003623 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003624 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3625 "Use of unregistered type library '%s'\n", library,
3626 NULL);
3627 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003628 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003629 def->data = lib;
3630 if (lib->have == NULL) {
3631 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3632 "Internal error with type library '%s': no 'have'\n",
3633 library, NULL);
3634 } else {
3635 tmp = lib->have(lib->data, def->name);
3636 if (tmp != 1) {
3637 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3638 "Error type '%s' is not exported by type library '%s'\n",
3639 def->name, library);
3640 } else
3641 if ((xmlStrEqual
3642 (library,
3643 BAD_CAST
3644 "http://www.w3.org/2001/XMLSchema-datatypes"))
3645 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3646 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3647 ctxt->idref = 1;
3648 }
3649 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003650 }
3651 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003652
3653 /*
3654 * Handle optional params
3655 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003656 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003657 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3658 break;
3659 if (xmlStrEqual(library,
3660 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3661 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3662 "Type library '%s' does not allow type parameters\n",
3663 library, NULL);
3664 content = content->next;
3665 while ((content != NULL) &&
3666 (xmlStrEqual(content->name, BAD_CAST "param")))
3667 content = content->next;
3668 } else {
3669 param = xmlRelaxNGNewDefine(ctxt, node);
3670 if (param != NULL) {
3671 param->type = XML_RELAXNG_PARAM;
3672 param->name = xmlGetProp(content, BAD_CAST "name");
3673 if (param->name == NULL) {
3674 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3675 "param has no name\n", NULL, NULL);
3676 }
3677 param->value = xmlNodeGetContent(content);
3678 if (lastparam == NULL) {
3679 def->attrs = lastparam = param;
3680 } else {
3681 lastparam->next = param;
3682 lastparam = param;
3683 }
3684 if (lib != NULL) {
3685 }
3686 }
3687 content = content->next;
3688 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003689 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003690 /*
3691 * Handle optional except
3692 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003693 if ((content != NULL)
3694 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3695 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003696 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003697
Daniel Veillard4c004142003-10-07 11:33:24 +00003698 except = xmlRelaxNGNewDefine(ctxt, node);
3699 if (except == NULL) {
3700 return (def);
3701 }
3702 except->type = XML_RELAXNG_EXCEPT;
3703 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003704 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003705 if (child == NULL) {
3706 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3707 "except has no content\n", NULL, NULL);
3708 }
3709 while (child != NULL) {
3710 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3711 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003712 if (last == NULL) {
3713 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003714 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003715 last->next = tmp2;
3716 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003717 }
3718 }
3719 child = child->next;
3720 }
3721 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003722 }
3723 /*
3724 * Check there is no unhandled data
3725 */
3726 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003727 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3728 "Element data has unexpected content %s\n",
3729 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003730 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003731
Daniel Veillard4c004142003-10-07 11:33:24 +00003732 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003733}
3734
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003735static const xmlChar *invalidName = BAD_CAST "\1";
3736
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003737/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003738 * xmlRelaxNGCompareNameClasses:
3739 * @defs1: the first element/attribute defs
3740 * @defs2: the second element/attribute defs
3741 * @name: the restriction on the name
3742 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003743 *
3744 * Compare the 2 lists of element definitions. The comparison is
3745 * that if both lists do not accept the same QNames, it returns 1
3746 * If the 2 lists can accept the same QName the comparison returns 0
3747 *
3748 * Returns 1 disttinct, 0 if equal
3749 */
3750static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003751xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003752 xmlRelaxNGDefinePtr def2)
3753{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003754 int ret = 1;
3755 xmlNode node;
3756 xmlNs ns;
3757 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003758
Daniel Veillard42f12e92003-03-07 18:32:59 +00003759 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3760
Daniel Veillardb30ca312005-09-04 13:50:03 +00003761 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3762
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003763 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003764 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3765 if (def2->type == XML_RELAXNG_TEXT)
3766 return (1);
3767 if (def1->name != NULL) {
3768 node.name = def1->name;
3769 } else {
3770 node.name = invalidName;
3771 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003772 if (def1->ns != NULL) {
3773 if (def1->ns[0] == 0) {
3774 node.ns = NULL;
3775 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003776 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003777 ns.href = def1->ns;
3778 }
3779 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003780 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003781 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003782 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003783 if (def1->nameClass != NULL) {
3784 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3785 } else {
3786 ret = 0;
3787 }
3788 } else {
3789 ret = 1;
3790 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003791 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003792 if (def2->type == XML_RELAXNG_TEXT)
3793 return (0);
3794 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003795 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003796 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003797 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003798 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003799 }
3800 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003801 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003802 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003803 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3804 if (def2->name != NULL) {
3805 node.name = def2->name;
3806 } else {
3807 node.name = invalidName;
3808 }
3809 node.ns = &ns;
3810 if (def2->ns != NULL) {
3811 if (def2->ns[0] == 0) {
3812 node.ns = NULL;
3813 } else {
3814 ns.href = def2->ns;
3815 }
3816 } else {
3817 ns.href = invalidName;
3818 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003819 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003820 if (def2->nameClass != NULL) {
3821 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3822 } else {
3823 ret = 0;
3824 }
3825 } else {
3826 ret = 1;
3827 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003828 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003829 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003830 }
3831
Daniel Veillard4c004142003-10-07 11:33:24 +00003832 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003833}
3834
3835/**
3836 * xmlRelaxNGCompareElemDefLists:
3837 * @ctxt: a Relax-NG parser context
3838 * @defs1: the first list of element/attribute defs
3839 * @defs2: the second list of element/attribute defs
3840 *
3841 * Compare the 2 lists of element or attribute definitions. The comparison
3842 * is that if both lists do not accept the same QNames, it returns 1
3843 * If the 2 lists can accept the same QName the comparison returns 0
3844 *
3845 * Returns 1 disttinct, 0 if equal
3846 */
3847static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003848xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3849 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3850 xmlRelaxNGDefinePtr * def2)
3851{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003852 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003853
Daniel Veillard154877e2003-01-30 12:17:05 +00003854 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003855 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003856 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003857 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003858 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003859 while ((*def2) != NULL) {
3860 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3861 return (0);
3862 def2++;
3863 }
3864 def2 = basedef2;
3865 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003866 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003867 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003868}
3869
3870/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003871 * xmlRelaxNGGenerateAttributes:
3872 * @ctxt: a Relax-NG parser context
3873 * @def: the definition definition
3874 *
3875 * Check if the definition can only generate attributes
3876 *
3877 * Returns 1 if yes, 0 if no and -1 in case of error.
3878 */
3879static int
3880xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003881 xmlRelaxNGDefinePtr def)
3882{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003883 xmlRelaxNGDefinePtr parent, cur, tmp;
3884
3885 /*
3886 * Don't run that check in case of error. Infinite recursion
3887 * becomes possible.
3888 */
3889 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003890 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003891
3892 parent = NULL;
3893 cur = def;
3894 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003895 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3896 (cur->type == XML_RELAXNG_TEXT) ||
3897 (cur->type == XML_RELAXNG_DATATYPE) ||
3898 (cur->type == XML_RELAXNG_PARAM) ||
3899 (cur->type == XML_RELAXNG_LIST) ||
3900 (cur->type == XML_RELAXNG_VALUE) ||
3901 (cur->type == XML_RELAXNG_EMPTY))
3902 return (0);
3903 if ((cur->type == XML_RELAXNG_CHOICE) ||
3904 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3905 (cur->type == XML_RELAXNG_GROUP) ||
3906 (cur->type == XML_RELAXNG_ONEORMORE) ||
3907 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3908 (cur->type == XML_RELAXNG_OPTIONAL) ||
3909 (cur->type == XML_RELAXNG_PARENTREF) ||
3910 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3911 (cur->type == XML_RELAXNG_REF) ||
3912 (cur->type == XML_RELAXNG_DEF)) {
3913 if (cur->content != NULL) {
3914 parent = cur;
3915 cur = cur->content;
3916 tmp = cur;
3917 while (tmp != NULL) {
3918 tmp->parent = parent;
3919 tmp = tmp->next;
3920 }
3921 continue;
3922 }
3923 }
3924 if (cur == def)
3925 break;
3926 if (cur->next != NULL) {
3927 cur = cur->next;
3928 continue;
3929 }
3930 do {
3931 cur = cur->parent;
3932 if (cur == NULL)
3933 break;
3934 if (cur == def)
3935 return (1);
3936 if (cur->next != NULL) {
3937 cur = cur->next;
3938 break;
3939 }
3940 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003941 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003942 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003943}
Daniel Veillard4c004142003-10-07 11:33:24 +00003944
Daniel Veillardce192eb2003-04-16 15:58:05 +00003945/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003946 * xmlRelaxNGGetElements:
3947 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003948 * @def: the definition definition
3949 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003950 *
3951 * Compute the list of top elements a definition can generate
3952 *
3953 * Returns a list of elements or NULL if none was found.
3954 */
3955static xmlRelaxNGDefinePtr *
3956xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003957 xmlRelaxNGDefinePtr def, int eora)
3958{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003959 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003960 int len = 0;
3961 int max = 0;
3962
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003963 /*
3964 * Don't run that check in case of error. Infinite recursion
3965 * becomes possible.
3966 */
3967 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003968 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003969
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003970 parent = NULL;
3971 cur = def;
3972 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003973 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3974 (cur->type == XML_RELAXNG_TEXT))) ||
3975 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3976 if (ret == NULL) {
3977 max = 10;
3978 ret = (xmlRelaxNGDefinePtr *)
3979 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3980 if (ret == NULL) {
3981 xmlRngPErrMemory(ctxt, "getting element list\n");
3982 return (NULL);
3983 }
3984 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00003985 xmlRelaxNGDefinePtr *temp;
3986
Daniel Veillard4c004142003-10-07 11:33:24 +00003987 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00003988 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00003989 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00003990 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003991 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00003992 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00003993 return (NULL);
3994 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00003995 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00003996 }
3997 ret[len++] = cur;
3998 ret[len] = NULL;
3999 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4000 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4001 (cur->type == XML_RELAXNG_GROUP) ||
4002 (cur->type == XML_RELAXNG_ONEORMORE) ||
4003 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4004 (cur->type == XML_RELAXNG_OPTIONAL) ||
4005 (cur->type == XML_RELAXNG_PARENTREF) ||
4006 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004007 (cur->type == XML_RELAXNG_DEF) ||
4008 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004009 /*
4010 * Don't go within elements or attributes or string values.
4011 * Just gather the element top list
4012 */
4013 if (cur->content != NULL) {
4014 parent = cur;
4015 cur = cur->content;
4016 tmp = cur;
4017 while (tmp != NULL) {
4018 tmp->parent = parent;
4019 tmp = tmp->next;
4020 }
4021 continue;
4022 }
4023 }
4024 if (cur == def)
4025 break;
4026 if (cur->next != NULL) {
4027 cur = cur->next;
4028 continue;
4029 }
4030 do {
4031 cur = cur->parent;
4032 if (cur == NULL)
4033 break;
4034 if (cur == def)
4035 return (ret);
4036 if (cur->next != NULL) {
4037 cur = cur->next;
4038 break;
4039 }
4040 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004041 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004042 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004043}
Daniel Veillard4c004142003-10-07 11:33:24 +00004044
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004045/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004046 * xmlRelaxNGCheckChoiceDeterminism:
4047 * @ctxt: a Relax-NG parser context
4048 * @def: the choice definition
4049 *
4050 * Also used to find indeterministic pattern in choice
4051 */
4052static void
4053xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004054 xmlRelaxNGDefinePtr def)
4055{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004056 xmlRelaxNGDefinePtr **list;
4057 xmlRelaxNGDefinePtr cur;
4058 int nbchild = 0, i, j, ret;
4059 int is_nullable = 0;
4060 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004061 xmlHashTablePtr triage = NULL;
4062 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004063
Daniel Veillard4c004142003-10-07 11:33:24 +00004064 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4065 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004066
Daniel Veillarde063f482003-03-21 16:53:17 +00004067 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004068 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004069
Daniel Veillardfd573f12003-03-16 17:52:32 +00004070 /*
4071 * Don't run that check in case of error. Infinite recursion
4072 * becomes possible.
4073 */
4074 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004075 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004076
4077 is_nullable = xmlRelaxNGIsNullable(def);
4078
4079 cur = def->content;
4080 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004081 nbchild++;
4082 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004083 }
4084
4085 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004086 sizeof(xmlRelaxNGDefinePtr
4087 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004088 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004089 xmlRngPErrMemory(ctxt, "building choice\n");
4090 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004091 }
4092 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004093 /*
4094 * a bit strong but safe
4095 */
4096 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004097 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004098 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004099 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004100 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004101 cur = def->content;
4102 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004103 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4104 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4105 is_triable = 0;
4106 } else if (is_triable == 1) {
4107 xmlRelaxNGDefinePtr *tmp;
4108 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004109
Daniel Veillard4c004142003-10-07 11:33:24 +00004110 tmp = list[i];
4111 while ((*tmp != NULL) && (is_triable == 1)) {
4112 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4113 res = xmlHashAddEntry2(triage,
4114 BAD_CAST "#text", NULL,
4115 (void *) cur);
4116 if (res != 0)
4117 is_triable = -1;
4118 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4119 ((*tmp)->name != NULL)) {
4120 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4121 res = xmlHashAddEntry2(triage,
4122 (*tmp)->name, NULL,
4123 (void *) cur);
4124 else
4125 res = xmlHashAddEntry2(triage,
4126 (*tmp)->name, (*tmp)->ns,
4127 (void *) cur);
4128 if (res != 0)
4129 is_triable = -1;
4130 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4131 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4132 res = xmlHashAddEntry2(triage,
4133 BAD_CAST "#any", NULL,
4134 (void *) cur);
4135 else
4136 res = xmlHashAddEntry2(triage,
4137 BAD_CAST "#any", (*tmp)->ns,
4138 (void *) cur);
4139 if (res != 0)
4140 is_triable = -1;
4141 } else {
4142 is_triable = -1;
4143 }
4144 tmp++;
4145 }
4146 }
4147 i++;
4148 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004149 }
4150
Daniel Veillard4c004142003-10-07 11:33:24 +00004151 for (i = 0; i < nbchild; i++) {
4152 if (list[i] == NULL)
4153 continue;
4154 for (j = 0; j < i; j++) {
4155 if (list[j] == NULL)
4156 continue;
4157 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4158 if (ret == 0) {
4159 is_indeterminist = 1;
4160 }
4161 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004162 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004163 for (i = 0; i < nbchild; i++) {
4164 if (list[i] != NULL)
4165 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004166 }
4167
4168 xmlFree(list);
4169 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004170 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004171 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004172 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004173 def->dflags |= IS_TRIABLE;
4174 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004175 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004176 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004177 }
4178 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004179}
4180
4181/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004182 * xmlRelaxNGCheckGroupAttrs:
4183 * @ctxt: a Relax-NG parser context
4184 * @def: the group definition
4185 *
4186 * Detects violations of rule 7.3
4187 */
4188static void
4189xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004190 xmlRelaxNGDefinePtr def)
4191{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004192 xmlRelaxNGDefinePtr **list;
4193 xmlRelaxNGDefinePtr cur;
4194 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004195
4196 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004197 ((def->type != XML_RELAXNG_GROUP) &&
4198 (def->type != XML_RELAXNG_ELEMENT)))
4199 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004200
Daniel Veillarde063f482003-03-21 16:53:17 +00004201 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004202 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004203
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004204 /*
4205 * Don't run that check in case of error. Infinite recursion
4206 * becomes possible.
4207 */
4208 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004209 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004210
Daniel Veillardfd573f12003-03-16 17:52:32 +00004211 cur = def->attrs;
4212 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004213 nbchild++;
4214 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004215 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004216 cur = def->content;
4217 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004218 nbchild++;
4219 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004220 }
4221
4222 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004223 sizeof(xmlRelaxNGDefinePtr
4224 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004225 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004226 xmlRngPErrMemory(ctxt, "building group\n");
4227 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004228 }
4229 i = 0;
4230 cur = def->attrs;
4231 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004232 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4233 i++;
4234 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004235 }
4236 cur = def->content;
4237 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004238 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4239 i++;
4240 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004241 }
4242
Daniel Veillard4c004142003-10-07 11:33:24 +00004243 for (i = 0; i < nbchild; i++) {
4244 if (list[i] == NULL)
4245 continue;
4246 for (j = 0; j < i; j++) {
4247 if (list[j] == NULL)
4248 continue;
4249 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4250 if (ret == 0) {
4251 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4252 "Attributes conflicts in group\n", NULL, NULL);
4253 }
4254 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004255 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004256 for (i = 0; i < nbchild; i++) {
4257 if (list[i] != NULL)
4258 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004259 }
4260
4261 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004262 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004263}
4264
4265/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004266 * xmlRelaxNGComputeInterleaves:
4267 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004268 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004269 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004270 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004271 * A lot of work for preprocessing interleave definitions
4272 * is potentially needed to get a decent execution speed at runtime
4273 * - trying to get a total order on the element nodes generated
4274 * by the interleaves, order the list of interleave definitions
4275 * following that order.
4276 * - if <text/> is used to handle mixed content, it is better to
4277 * flag this in the define and simplify the runtime checking
4278 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004279 */
4280static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004281xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004282 xmlRelaxNGParserCtxtPtr ctxt,
4283 xmlChar * name ATTRIBUTE_UNUSED)
4284{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004285 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004286
Daniel Veillardfd573f12003-03-16 17:52:32 +00004287 xmlRelaxNGPartitionPtr partitions = NULL;
4288 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4289 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004290 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004291 int nbgroups = 0;
4292 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004293 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004294 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004295
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004296 /*
4297 * Don't run that check in case of error. Infinite recursion
4298 * becomes possible.
4299 */
4300 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004301 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004302
Daniel Veillardfd573f12003-03-16 17:52:32 +00004303#ifdef DEBUG_INTERLEAVE
4304 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004305 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004306#endif
4307 cur = def->content;
4308 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004309 nbchild++;
4310 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004311 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004312
Daniel Veillardfd573f12003-03-16 17:52:32 +00004313#ifdef DEBUG_INTERLEAVE
4314 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4315#endif
4316 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004317 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004318 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004319 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004320 cur = def->content;
4321 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004322 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4323 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4324 if (groups[nbgroups] == NULL)
4325 goto error;
4326 if (cur->type == XML_RELAXNG_TEXT)
4327 is_mixed++;
4328 groups[nbgroups]->rule = cur;
4329 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4330 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4331 nbgroups++;
4332 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004333 }
4334#ifdef DEBUG_INTERLEAVE
4335 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4336#endif
4337
4338 /*
4339 * Let's check that all rules makes a partitions according to 7.4
4340 */
4341 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004342 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004343 if (partitions == NULL)
4344 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004345 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004346 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004347 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004348 for (i = 0; i < nbgroups; i++) {
4349 group = groups[i];
4350 for (j = i + 1; j < nbgroups; j++) {
4351 if (groups[j] == NULL)
4352 continue;
4353
4354 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4355 groups[j]->defs);
4356 if (ret == 0) {
4357 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4358 "Element or text conflicts in interleave\n",
4359 NULL, NULL);
4360 }
4361 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4362 groups[j]->attrs);
4363 if (ret == 0) {
4364 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4365 "Attributes conflicts in interleave\n", NULL,
4366 NULL);
4367 }
4368 }
4369 tmp = group->defs;
4370 if ((tmp != NULL) && (*tmp != NULL)) {
4371 while (*tmp != NULL) {
4372 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4373 res = xmlHashAddEntry2(partitions->triage,
4374 BAD_CAST "#text", NULL,
4375 (void *) (long) (i + 1));
4376 if (res != 0)
4377 is_determinist = -1;
4378 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4379 ((*tmp)->name != NULL)) {
4380 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4381 res = xmlHashAddEntry2(partitions->triage,
4382 (*tmp)->name, NULL,
4383 (void *) (long) (i + 1));
4384 else
4385 res = xmlHashAddEntry2(partitions->triage,
4386 (*tmp)->name, (*tmp)->ns,
4387 (void *) (long) (i + 1));
4388 if (res != 0)
4389 is_determinist = -1;
4390 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4391 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4392 res = xmlHashAddEntry2(partitions->triage,
4393 BAD_CAST "#any", NULL,
4394 (void *) (long) (i + 1));
4395 else
4396 res = xmlHashAddEntry2(partitions->triage,
4397 BAD_CAST "#any", (*tmp)->ns,
4398 (void *) (long) (i + 1));
4399 if ((*tmp)->nameClass != NULL)
4400 is_determinist = 2;
4401 if (res != 0)
4402 is_determinist = -1;
4403 } else {
4404 is_determinist = -1;
4405 }
4406 tmp++;
4407 }
4408 } else {
4409 is_determinist = 0;
4410 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004411 }
4412 partitions->groups = groups;
4413
4414 /*
4415 * and save the partition list back in the def
4416 */
4417 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004418 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004419 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004420 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004421 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004422 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004423 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004424 return;
4425
Daniel Veillard4c004142003-10-07 11:33:24 +00004426 error:
4427 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004428 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004429 for (i = 0; i < nbgroups; i++)
4430 if (groups[i] != NULL) {
4431 if (groups[i]->defs != NULL)
4432 xmlFree(groups[i]->defs);
4433 xmlFree(groups[i]);
4434 }
4435 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004436 }
4437 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004438}
4439
4440/**
4441 * xmlRelaxNGParseInterleave:
4442 * @ctxt: a Relax-NG parser context
4443 * @node: the data node.
4444 *
4445 * parse the content of a RelaxNG interleave node.
4446 *
4447 * Returns the definition pointer or NULL in case of error
4448 */
4449static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004450xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4451{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004452 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004453 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004454 xmlNodePtr child;
4455
Daniel Veillardfd573f12003-03-16 17:52:32 +00004456 def = xmlRelaxNGNewDefine(ctxt, node);
4457 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004458 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004459 }
4460 def->type = XML_RELAXNG_INTERLEAVE;
4461
4462 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004463 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004464 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004465 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004466 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004467 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004468
Daniel Veillard4c004142003-10-07 11:33:24 +00004469 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4470 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4471 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4472 "Failed to add %s to hash table\n",
4473 (const xmlChar *) name, NULL);
4474 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004475 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004476 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004477 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004478 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4479 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004480 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004481 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004482 if (IS_RELAXNG(child, "element")) {
4483 cur = xmlRelaxNGParseElement(ctxt, child);
4484 } else {
4485 cur = xmlRelaxNGParsePattern(ctxt, child);
4486 }
4487 if (cur != NULL) {
4488 cur->parent = def;
4489 if (last == NULL) {
4490 def->content = last = cur;
4491 } else {
4492 last->next = cur;
4493 last = cur;
4494 }
4495 }
4496 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004497 }
4498
Daniel Veillard4c004142003-10-07 11:33:24 +00004499 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004500}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004501
4502/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004503 * xmlRelaxNGParseInclude:
4504 * @ctxt: a Relax-NG parser context
4505 * @node: the include node
4506 *
4507 * Integrate the content of an include node in the current grammar
4508 *
4509 * Returns 0 in case of success or -1 in case of error
4510 */
4511static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004512xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4513{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004514 xmlRelaxNGIncludePtr incl;
4515 xmlNodePtr root;
4516 int ret = 0, tmp;
4517
Daniel Veillard807daf82004-02-22 22:13:27 +00004518 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004519 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004520 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4521 "Include node has no data\n", NULL, NULL);
4522 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004523 }
4524 root = xmlDocGetRootElement(incl->doc);
4525 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004526 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4527 NULL, NULL);
4528 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004529 }
4530 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004531 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4532 "Include document root is not a grammar\n", NULL, NULL);
4533 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004534 }
4535
4536 /*
4537 * Merge the definition from both the include and the internal list
4538 */
4539 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004540 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4541 if (tmp != 0)
4542 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004543 }
4544 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004545 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4546 if (tmp != 0)
4547 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004548 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004549 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004550}
4551
4552/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004553 * xmlRelaxNGParseDefine:
4554 * @ctxt: a Relax-NG parser context
4555 * @node: the define node
4556 *
4557 * parse the content of a RelaxNG define element node.
4558 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004559 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004560 */
4561static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004562xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4563{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004564 xmlChar *name;
4565 int ret = 0, tmp;
4566 xmlRelaxNGDefinePtr def;
4567 const xmlChar *olddefine;
4568
4569 name = xmlGetProp(node, BAD_CAST "name");
4570 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004571 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4572 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004573 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004574 xmlRelaxNGNormExtSpace(name);
4575 if (xmlValidateNCName(name, 0)) {
4576 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4577 "define name '%s' is not an NCName\n", name, NULL);
4578 }
4579 def = xmlRelaxNGNewDefine(ctxt, node);
4580 if (def == NULL) {
4581 xmlFree(name);
4582 return (-1);
4583 }
4584 def->type = XML_RELAXNG_DEF;
4585 def->name = name;
4586 if (node->children == NULL) {
4587 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4588 "define has no children\n", NULL, NULL);
4589 } else {
4590 olddefine = ctxt->define;
4591 ctxt->define = name;
4592 def->content =
4593 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4594 ctxt->define = olddefine;
4595 }
4596 if (ctxt->grammar->defs == NULL)
4597 ctxt->grammar->defs = xmlHashCreate(10);
4598 if (ctxt->grammar->defs == NULL) {
4599 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4600 "Could not create definition hash\n", NULL, NULL);
4601 ret = -1;
4602 } else {
4603 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4604 if (tmp < 0) {
4605 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004606
Daniel Veillard4c004142003-10-07 11:33:24 +00004607 prev = xmlHashLookup(ctxt->grammar->defs, name);
4608 if (prev == NULL) {
4609 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4610 "Internal error on define aggregation of %s\n",
4611 name, NULL);
4612 ret = -1;
4613 } else {
4614 while (prev->nextHash != NULL)
4615 prev = prev->nextHash;
4616 prev->nextHash = def;
4617 }
4618 }
4619 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004620 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004621 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004622}
4623
4624/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004625 * xmlRelaxNGParseImportRef:
4626 * @payload: the parser context
4627 * @data: the current grammar
4628 * @name: the reference name
4629 *
4630 * Import import one references into the current grammar
4631 */
4632static void
4633xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4634 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4635 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4636 int tmp;
4637
4638 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4639 if (tmp < 0) {
4640 xmlRelaxNGDefinePtr prev;
4641
4642 prev = (xmlRelaxNGDefinePtr)
4643 xmlHashLookup(ctxt->grammar->refs, def->name);
4644 if (prev == NULL) {
4645 if (def->name != NULL) {
4646 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4647 "Error refs definitions '%s'\n",
4648 def->name, NULL);
4649 } else {
4650 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4651 "Error refs definitions\n",
4652 NULL, NULL);
4653 }
4654 } else {
4655 def->nextHash = prev->nextHash;
4656 prev->nextHash = def;
4657 }
4658 }
4659}
4660
4661/**
4662 * xmlRelaxNGParseImportRefs:
4663 * @ctxt: the parser context
4664 * @grammar: the sub grammar
4665 *
4666 * Import references from the subgrammar into the current grammar
4667 *
4668 * Returns 0 in case of success, -1 in case of failure
4669 */
4670static int
4671xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4672 xmlRelaxNGGrammarPtr grammar) {
4673 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4674 return(-1);
4675 if (grammar->refs == NULL)
4676 return(0);
4677 if (ctxt->grammar->refs == NULL)
4678 ctxt->grammar->refs = xmlHashCreate(10);
4679 if (ctxt->grammar->refs == NULL) {
4680 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4681 "Could not create references hash\n", NULL, NULL);
4682 return(-1);
4683 }
4684 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
4685}
4686
4687/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004688 * xmlRelaxNGProcessExternalRef:
4689 * @ctxt: the parser context
4690 * @node: the externlRef node
4691 *
4692 * Process and compile an externlRef node
4693 *
4694 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4695 */
4696static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004697xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4698{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004699 xmlRelaxNGDocumentPtr docu;
4700 xmlNodePtr root, tmp;
4701 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004702 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004703 xmlRelaxNGDefinePtr def;
4704
Daniel Veillard807daf82004-02-22 22:13:27 +00004705 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004706 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004707 def = xmlRelaxNGNewDefine(ctxt, node);
4708 if (def == NULL)
4709 return (NULL);
4710 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004711
Daniel Veillard4c004142003-10-07 11:33:24 +00004712 if (docu->content == NULL) {
4713 /*
4714 * Then do the parsing for good
4715 */
4716 root = xmlDocGetRootElement(docu->doc);
4717 if (root == NULL) {
4718 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4719 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4720 NULL);
4721 return (NULL);
4722 }
4723 /*
4724 * ns transmission rules
4725 */
4726 ns = xmlGetProp(root, BAD_CAST "ns");
4727 if (ns == NULL) {
4728 tmp = node;
4729 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4730 ns = xmlGetProp(tmp, BAD_CAST "ns");
4731 if (ns != NULL) {
4732 break;
4733 }
4734 tmp = tmp->parent;
4735 }
4736 if (ns != NULL) {
4737 xmlSetProp(root, BAD_CAST "ns", ns);
4738 newNs = 1;
4739 xmlFree(ns);
4740 }
4741 } else {
4742 xmlFree(ns);
4743 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004744
Daniel Veillard4c004142003-10-07 11:33:24 +00004745 /*
4746 * Parsing to get a precompiled schemas.
4747 */
4748 oldflags = ctxt->flags;
4749 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4750 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4751 ctxt->flags = oldflags;
4752 if ((docu->schema != NULL) &&
4753 (docu->schema->topgrammar != NULL)) {
4754 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004755 if (docu->schema->topgrammar->refs)
4756 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004757 }
4758
4759 /*
4760 * the externalRef may be reused in a different ns context
4761 */
4762 if (newNs == 1) {
4763 xmlUnsetProp(root, BAD_CAST "ns");
4764 }
4765 }
4766 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004767 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004768 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004769 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004770 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004771}
4772
4773/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004774 * xmlRelaxNGParsePattern:
4775 * @ctxt: a Relax-NG parser context
4776 * @node: the pattern node.
4777 *
4778 * parse the content of a RelaxNG pattern node.
4779 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004780 * Returns the definition pointer or NULL in case of error or if no
4781 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004782 */
4783static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004784xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4785{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004786 xmlRelaxNGDefinePtr def = NULL;
4787
Daniel Veillardd2298792003-02-14 16:54:11 +00004788 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004789 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004790 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004791 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004792 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004793 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004794 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004795 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004796 def = xmlRelaxNGNewDefine(ctxt, node);
4797 if (def == NULL)
4798 return (NULL);
4799 def->type = XML_RELAXNG_EMPTY;
4800 if (node->children != NULL) {
4801 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4802 "empty: had a child node\n", NULL, NULL);
4803 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004804 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004805 def = xmlRelaxNGNewDefine(ctxt, node);
4806 if (def == NULL)
4807 return (NULL);
4808 def->type = XML_RELAXNG_TEXT;
4809 if (node->children != NULL) {
4810 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4811 "text: had a child node\n", NULL, NULL);
4812 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004813 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004814 def = xmlRelaxNGNewDefine(ctxt, node);
4815 if (def == NULL)
4816 return (NULL);
4817 def->type = XML_RELAXNG_ZEROORMORE;
4818 if (node->children == NULL) {
4819 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4820 "Element %s is empty\n", node->name, NULL);
4821 } else {
4822 def->content =
4823 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4824 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004825 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004826 def = xmlRelaxNGNewDefine(ctxt, node);
4827 if (def == NULL)
4828 return (NULL);
4829 def->type = XML_RELAXNG_ONEORMORE;
4830 if (node->children == NULL) {
4831 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4832 "Element %s is empty\n", node->name, NULL);
4833 } else {
4834 def->content =
4835 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4836 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004837 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004838 def = xmlRelaxNGNewDefine(ctxt, node);
4839 if (def == NULL)
4840 return (NULL);
4841 def->type = XML_RELAXNG_OPTIONAL;
4842 if (node->children == NULL) {
4843 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4844 "Element %s is empty\n", node->name, NULL);
4845 } else {
4846 def->content =
4847 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4848 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004849 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004850 def = xmlRelaxNGNewDefine(ctxt, node);
4851 if (def == NULL)
4852 return (NULL);
4853 def->type = XML_RELAXNG_CHOICE;
4854 if (node->children == NULL) {
4855 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4856 "Element %s is empty\n", node->name, NULL);
4857 } else {
4858 def->content =
4859 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4860 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004861 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004862 def = xmlRelaxNGNewDefine(ctxt, node);
4863 if (def == NULL)
4864 return (NULL);
4865 def->type = XML_RELAXNG_GROUP;
4866 if (node->children == NULL) {
4867 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4868 "Element %s is empty\n", node->name, NULL);
4869 } else {
4870 def->content =
4871 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4872 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004873 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004874 def = xmlRelaxNGNewDefine(ctxt, node);
4875 if (def == NULL)
4876 return (NULL);
4877 def->type = XML_RELAXNG_REF;
4878 def->name = xmlGetProp(node, BAD_CAST "name");
4879 if (def->name == NULL) {
4880 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4881 NULL, NULL);
4882 } else {
4883 xmlRelaxNGNormExtSpace(def->name);
4884 if (xmlValidateNCName(def->name, 0)) {
4885 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4886 "ref name '%s' is not an NCName\n", def->name,
4887 NULL);
4888 }
4889 }
4890 if (node->children != NULL) {
4891 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4892 NULL, NULL);
4893 }
4894 if (ctxt->grammar->refs == NULL)
4895 ctxt->grammar->refs = xmlHashCreate(10);
4896 if (ctxt->grammar->refs == NULL) {
4897 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4898 "Could not create references hash\n", NULL, NULL);
4899 def = NULL;
4900 } else {
4901 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004902
Daniel Veillard4c004142003-10-07 11:33:24 +00004903 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4904 if (tmp < 0) {
4905 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004906
Daniel Veillard4c004142003-10-07 11:33:24 +00004907 prev = (xmlRelaxNGDefinePtr)
4908 xmlHashLookup(ctxt->grammar->refs, def->name);
4909 if (prev == NULL) {
4910 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004911 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4912 "Error refs definitions '%s'\n",
4913 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004914 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004915 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4916 "Error refs definitions\n",
4917 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004918 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004919 def = NULL;
4920 } else {
4921 def->nextHash = prev->nextHash;
4922 prev->nextHash = def;
4923 }
4924 }
4925 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004926 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004927 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004928 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004929 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004930 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004931 def = xmlRelaxNGNewDefine(ctxt, node);
4932 if (def == NULL)
4933 return (NULL);
4934 def->type = XML_RELAXNG_LIST;
4935 if (node->children == NULL) {
4936 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4937 "Element %s is empty\n", node->name, NULL);
4938 } else {
4939 def->content =
4940 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4941 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004942 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004943 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004944 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004945 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004946 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004947 def = xmlRelaxNGNewDefine(ctxt, node);
4948 if (def == NULL)
4949 return (NULL);
4950 def->type = XML_RELAXNG_NOT_ALLOWED;
4951 if (node->children != NULL) {
4952 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4953 "xmlRelaxNGParse: notAllowed element is not empty\n",
4954 NULL, NULL);
4955 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004956 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004957 xmlRelaxNGGrammarPtr grammar, old;
4958 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004959
Daniel Veillardc482e262003-02-26 14:48:48 +00004960#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004961 xmlGenericError(xmlGenericErrorContext,
4962 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004963#endif
4964
Daniel Veillard4c004142003-10-07 11:33:24 +00004965 oldparent = ctxt->parentgrammar;
4966 old = ctxt->grammar;
4967 ctxt->parentgrammar = old;
4968 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4969 if (old != NULL) {
4970 ctxt->grammar = old;
4971 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004972#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004973 if (grammar != NULL) {
4974 grammar->next = old->next;
4975 old->next = grammar;
4976 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004977#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004978 }
4979 if (grammar != NULL)
4980 def = grammar->start;
4981 else
4982 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004983 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004984 if (ctxt->parentgrammar == NULL) {
4985 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4986 "Use of parentRef without a parent grammar\n", NULL,
4987 NULL);
4988 return (NULL);
4989 }
4990 def = xmlRelaxNGNewDefine(ctxt, node);
4991 if (def == NULL)
4992 return (NULL);
4993 def->type = XML_RELAXNG_PARENTREF;
4994 def->name = xmlGetProp(node, BAD_CAST "name");
4995 if (def->name == NULL) {
4996 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4997 "parentRef has no name\n", NULL, NULL);
4998 } else {
4999 xmlRelaxNGNormExtSpace(def->name);
5000 if (xmlValidateNCName(def->name, 0)) {
5001 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5002 "parentRef name '%s' is not an NCName\n",
5003 def->name, NULL);
5004 }
5005 }
5006 if (node->children != NULL) {
5007 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5008 "parentRef is not empty\n", NULL, NULL);
5009 }
5010 if (ctxt->parentgrammar->refs == NULL)
5011 ctxt->parentgrammar->refs = xmlHashCreate(10);
5012 if (ctxt->parentgrammar->refs == NULL) {
5013 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5014 "Could not create references hash\n", NULL, NULL);
5015 def = NULL;
5016 } else if (def->name != NULL) {
5017 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005018
Daniel Veillard4c004142003-10-07 11:33:24 +00005019 tmp =
5020 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5021 if (tmp < 0) {
5022 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005023
Daniel Veillard4c004142003-10-07 11:33:24 +00005024 prev = (xmlRelaxNGDefinePtr)
5025 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5026 if (prev == NULL) {
5027 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5028 "Internal error parentRef definitions '%s'\n",
5029 def->name, NULL);
5030 def = NULL;
5031 } else {
5032 def->nextHash = prev->nextHash;
5033 prev->nextHash = def;
5034 }
5035 }
5036 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005037 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005038 if (node->children == NULL) {
5039 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5040 NULL, NULL);
5041 def = NULL;
5042 } else {
5043 def = xmlRelaxNGParseInterleave(ctxt, node);
5044 if (def != NULL) {
5045 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005046
Daniel Veillard4c004142003-10-07 11:33:24 +00005047 if ((def->content != NULL) && (def->content->next != NULL)) {
5048 tmp = xmlRelaxNGNewDefine(ctxt, node);
5049 if (tmp != NULL) {
5050 tmp->type = XML_RELAXNG_GROUP;
5051 tmp->content = def->content;
5052 def->content = tmp;
5053 }
5054 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005055
Daniel Veillard4c004142003-10-07 11:33:24 +00005056 tmp = xmlRelaxNGNewDefine(ctxt, node);
5057 if (tmp == NULL)
5058 return (def);
5059 tmp->type = XML_RELAXNG_TEXT;
5060 tmp->next = def->content;
5061 def->content = tmp;
5062 }
5063 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005064 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005065 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5066 "Unexpected node %s is not a pattern\n", node->name,
5067 NULL);
5068 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005069 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005070 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005071}
5072
5073/**
5074 * xmlRelaxNGParseAttribute:
5075 * @ctxt: a Relax-NG parser context
5076 * @node: the element node
5077 *
5078 * parse the content of a RelaxNG attribute node.
5079 *
5080 * Returns the definition pointer or NULL in case of error.
5081 */
5082static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005083xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5084{
Daniel Veillardd2298792003-02-14 16:54:11 +00005085 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005086 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005087 int old_flags;
5088
Daniel Veillardfd573f12003-03-16 17:52:32 +00005089 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005090 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005091 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005092 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005093 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005094 child = node->children;
5095 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005096 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5097 "xmlRelaxNGParseattribute: attribute has no children\n",
5098 NULL, NULL);
5099 return (ret);
5100 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005101 old_flags = ctxt->flags;
5102 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005103 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5104 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005105 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005106
Daniel Veillardd2298792003-02-14 16:54:11 +00005107 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005108 cur = xmlRelaxNGParsePattern(ctxt, child);
5109 if (cur != NULL) {
5110 switch (cur->type) {
5111 case XML_RELAXNG_EMPTY:
5112 case XML_RELAXNG_NOT_ALLOWED:
5113 case XML_RELAXNG_TEXT:
5114 case XML_RELAXNG_ELEMENT:
5115 case XML_RELAXNG_DATATYPE:
5116 case XML_RELAXNG_VALUE:
5117 case XML_RELAXNG_LIST:
5118 case XML_RELAXNG_REF:
5119 case XML_RELAXNG_PARENTREF:
5120 case XML_RELAXNG_EXTERNALREF:
5121 case XML_RELAXNG_DEF:
5122 case XML_RELAXNG_ONEORMORE:
5123 case XML_RELAXNG_ZEROORMORE:
5124 case XML_RELAXNG_OPTIONAL:
5125 case XML_RELAXNG_CHOICE:
5126 case XML_RELAXNG_GROUP:
5127 case XML_RELAXNG_INTERLEAVE:
5128 case XML_RELAXNG_ATTRIBUTE:
5129 ret->content = cur;
5130 cur->parent = ret;
5131 break;
5132 case XML_RELAXNG_START:
5133 case XML_RELAXNG_PARAM:
5134 case XML_RELAXNG_EXCEPT:
5135 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5136 "attribute has invalid content\n", NULL,
5137 NULL);
5138 break;
5139 case XML_RELAXNG_NOOP:
5140 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5141 "RNG Internal error, noop found in attribute\n",
5142 NULL, NULL);
5143 break;
5144 }
5145 }
5146 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005147 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005148 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005149 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5150 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005151 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005152 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005153 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005154}
5155
5156/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005157 * xmlRelaxNGParseExceptNameClass:
5158 * @ctxt: a Relax-NG parser context
5159 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005160 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005161 *
5162 * parse the content of a RelaxNG nameClass node.
5163 *
5164 * Returns the definition pointer or NULL in case of error.
5165 */
5166static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005167xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005168 xmlNodePtr node, int attr)
5169{
Daniel Veillard144fae12003-02-03 13:17:57 +00005170 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5171 xmlNodePtr child;
5172
Daniel Veillardd2298792003-02-14 16:54:11 +00005173 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005174 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5175 "Expecting an except node\n", NULL, NULL);
5176 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005177 }
5178 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005179 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5180 "exceptNameClass allows only a single except node\n",
5181 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005182 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005183 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005184 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5185 NULL, NULL);
5186 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005187 }
5188
Daniel Veillardfd573f12003-03-16 17:52:32 +00005189 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005190 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005191 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005192 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005193 child = node->children;
5194 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005195 cur = xmlRelaxNGNewDefine(ctxt, child);
5196 if (cur == NULL)
5197 break;
5198 if (attr)
5199 cur->type = XML_RELAXNG_ATTRIBUTE;
5200 else
5201 cur->type = XML_RELAXNG_ELEMENT;
5202
Daniel Veillard419a7682003-02-03 23:22:49 +00005203 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005204 if (last == NULL) {
5205 ret->content = cur;
5206 } else {
5207 last->next = cur;
5208 }
5209 last = cur;
5210 }
5211 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005212 }
5213
Daniel Veillard4c004142003-10-07 11:33:24 +00005214 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005215}
5216
5217/**
5218 * xmlRelaxNGParseNameClass:
5219 * @ctxt: a Relax-NG parser context
5220 * @node: the nameClass node
5221 * @def: the current definition
5222 *
5223 * parse the content of a RelaxNG nameClass node.
5224 *
5225 * Returns the definition pointer or NULL in case of error.
5226 */
5227static xmlRelaxNGDefinePtr
5228xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005229 xmlRelaxNGDefinePtr def)
5230{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005231 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005232 xmlChar *val;
5233
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005234 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005235 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005236 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005237 if ((def->type != XML_RELAXNG_ELEMENT) &&
5238 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5239 ret = xmlRelaxNGNewDefine(ctxt, node);
5240 if (ret == NULL)
5241 return (NULL);
5242 ret->parent = def;
5243 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5244 ret->type = XML_RELAXNG_ATTRIBUTE;
5245 else
5246 ret->type = XML_RELAXNG_ELEMENT;
5247 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005248 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005249 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005250 val = xmlNodeGetContent(node);
5251 xmlRelaxNGNormExtSpace(val);
5252 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005253 if (node->parent != NULL)
5254 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5255 "Element %s name '%s' is not an NCName\n",
5256 node->parent->name, val);
5257 else
5258 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5259 "name '%s' is not an NCName\n",
5260 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005261 }
5262 ret->name = val;
5263 val = xmlGetProp(node, BAD_CAST "ns");
5264 ret->ns = val;
5265 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5266 (val != NULL) &&
5267 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005268 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005269 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005270 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005271 }
5272 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5273 (val != NULL) &&
5274 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005275 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5276 "Attribute with QName 'xmlns' is not allowed\n",
5277 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005278 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005279 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005280 ret->name = NULL;
5281 ret->ns = NULL;
5282 if (node->children != NULL) {
5283 ret->nameClass =
5284 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5285 (def->type ==
5286 XML_RELAXNG_ATTRIBUTE));
5287 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005288 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005289 ret->name = NULL;
5290 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5291 if (ret->ns == NULL) {
5292 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5293 "nsName has no ns attribute\n", NULL, NULL);
5294 }
5295 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5296 (ret->ns != NULL) &&
5297 (xmlStrEqual
5298 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5299 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5300 "Attribute with namespace '%s' is not allowed\n",
5301 ret->ns, NULL);
5302 }
5303 if (node->children != NULL) {
5304 ret->nameClass =
5305 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5306 (def->type ==
5307 XML_RELAXNG_ATTRIBUTE));
5308 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005309 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005310 xmlNodePtr child;
5311 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005312
Daniel Veillard4c004142003-10-07 11:33:24 +00005313 ret = xmlRelaxNGNewDefine(ctxt, node);
5314 if (ret == NULL)
5315 return (NULL);
5316 ret->parent = def;
5317 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005318
Daniel Veillard4c004142003-10-07 11:33:24 +00005319 if (node->children == NULL) {
5320 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5321 "Element choice is empty\n", NULL, NULL);
5322 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005323
Daniel Veillard4c004142003-10-07 11:33:24 +00005324 child = node->children;
5325 while (child != NULL) {
5326 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5327 if (tmp != NULL) {
5328 if (last == NULL) {
5329 last = ret->nameClass = tmp;
5330 } else {
5331 last->next = tmp;
5332 last = tmp;
5333 }
5334 }
5335 child = child->next;
5336 }
5337 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005338 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005339 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5340 "expecting name, anyName, nsName or choice : got %s\n",
5341 node->name, NULL);
5342 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005343 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005344 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005345 if (def->nameClass == NULL) {
5346 def->nameClass = ret;
5347 } else {
5348 tmp = def->nameClass;
5349 while (tmp->next != NULL) {
5350 tmp = tmp->next;
5351 }
5352 tmp->next = ret;
5353 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005354 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005355 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005356}
5357
5358/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005359 * xmlRelaxNGParseElement:
5360 * @ctxt: a Relax-NG parser context
5361 * @node: the element node
5362 *
5363 * parse the content of a RelaxNG element node.
5364 *
5365 * Returns the definition pointer or NULL in case of error.
5366 */
5367static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005368xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5369{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005370 xmlRelaxNGDefinePtr ret, cur, last;
5371 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005372 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005373
Daniel Veillardfd573f12003-03-16 17:52:32 +00005374 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005375 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005376 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005377 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005378 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005379 child = node->children;
5380 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005381 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5382 "xmlRelaxNGParseElement: element has no children\n",
5383 NULL, NULL);
5384 return (ret);
5385 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005386 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5387 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005388 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005389
Daniel Veillard6eadf632003-01-23 18:29:16 +00005390 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005391 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5392 "xmlRelaxNGParseElement: element has no content\n",
5393 NULL, NULL);
5394 return (ret);
5395 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005396 olddefine = ctxt->define;
5397 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005398 last = NULL;
5399 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005400 cur = xmlRelaxNGParsePattern(ctxt, child);
5401 if (cur != NULL) {
5402 cur->parent = ret;
5403 switch (cur->type) {
5404 case XML_RELAXNG_EMPTY:
5405 case XML_RELAXNG_NOT_ALLOWED:
5406 case XML_RELAXNG_TEXT:
5407 case XML_RELAXNG_ELEMENT:
5408 case XML_RELAXNG_DATATYPE:
5409 case XML_RELAXNG_VALUE:
5410 case XML_RELAXNG_LIST:
5411 case XML_RELAXNG_REF:
5412 case XML_RELAXNG_PARENTREF:
5413 case XML_RELAXNG_EXTERNALREF:
5414 case XML_RELAXNG_DEF:
5415 case XML_RELAXNG_ZEROORMORE:
5416 case XML_RELAXNG_ONEORMORE:
5417 case XML_RELAXNG_OPTIONAL:
5418 case XML_RELAXNG_CHOICE:
5419 case XML_RELAXNG_GROUP:
5420 case XML_RELAXNG_INTERLEAVE:
5421 if (last == NULL) {
5422 ret->content = last = cur;
5423 } else {
5424 if ((last->type == XML_RELAXNG_ELEMENT) &&
5425 (ret->content == last)) {
5426 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5427 if (ret->content != NULL) {
5428 ret->content->type = XML_RELAXNG_GROUP;
5429 ret->content->content = last;
5430 } else {
5431 ret->content = last;
5432 }
5433 }
5434 last->next = cur;
5435 last = cur;
5436 }
5437 break;
5438 case XML_RELAXNG_ATTRIBUTE:
5439 cur->next = ret->attrs;
5440 ret->attrs = cur;
5441 break;
5442 case XML_RELAXNG_START:
5443 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5444 "RNG Internal error, start found in element\n",
5445 NULL, NULL);
5446 break;
5447 case XML_RELAXNG_PARAM:
5448 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5449 "RNG Internal error, param found in element\n",
5450 NULL, NULL);
5451 break;
5452 case XML_RELAXNG_EXCEPT:
5453 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5454 "RNG Internal error, except found in element\n",
5455 NULL, NULL);
5456 break;
5457 case XML_RELAXNG_NOOP:
5458 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5459 "RNG Internal error, noop found in element\n",
5460 NULL, NULL);
5461 break;
5462 }
5463 }
5464 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005465 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005466 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005467 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005468}
5469
5470/**
5471 * xmlRelaxNGParsePatterns:
5472 * @ctxt: a Relax-NG parser context
5473 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005474 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005475 *
5476 * parse the content of a RelaxNG start node.
5477 *
5478 * Returns the definition pointer or NULL in case of error.
5479 */
5480static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005481xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005482 int group)
5483{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005484 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005485
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005486 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005487 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005488 if (IS_RELAXNG(nodes, "element")) {
5489 cur = xmlRelaxNGParseElement(ctxt, nodes);
5490 if (def == NULL) {
5491 def = last = cur;
5492 } else {
5493 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5494 (def == last)) {
5495 def = xmlRelaxNGNewDefine(ctxt, nodes);
5496 def->type = XML_RELAXNG_GROUP;
5497 def->content = last;
5498 }
5499 last->next = cur;
5500 last = cur;
5501 }
5502 cur->parent = parent;
5503 } else {
5504 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5505 if (cur != NULL) {
5506 if (def == NULL) {
5507 def = last = cur;
5508 } else {
5509 last->next = cur;
5510 last = cur;
5511 }
5512 }
5513 }
5514 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005515 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005516 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005517}
5518
5519/**
5520 * xmlRelaxNGParseStart:
5521 * @ctxt: a Relax-NG parser context
5522 * @nodes: start children nodes
5523 *
5524 * parse the content of a RelaxNG start node.
5525 *
5526 * Returns 0 in case of success, -1 in case of error
5527 */
5528static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005529xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5530{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005531 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005532 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005533
Daniel Veillardd2298792003-02-14 16:54:11 +00005534 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005535 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5536 NULL, NULL);
5537 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005538 }
5539 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005540 def = xmlRelaxNGNewDefine(ctxt, nodes);
5541 if (def == NULL)
5542 return (-1);
5543 def->type = XML_RELAXNG_EMPTY;
5544 if (nodes->children != NULL) {
5545 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5546 "element empty is not empty\n", NULL, NULL);
5547 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005548 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005549 def = xmlRelaxNGNewDefine(ctxt, nodes);
5550 if (def == NULL)
5551 return (-1);
5552 def->type = XML_RELAXNG_NOT_ALLOWED;
5553 if (nodes->children != NULL) {
5554 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5555 "element notAllowed is not empty\n", NULL, NULL);
5556 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005557 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005558 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005559 }
5560 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005561 last = ctxt->grammar->start;
5562 while (last->next != NULL)
5563 last = last->next;
5564 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005565 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005566 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005567 }
5568 nodes = nodes->next;
5569 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005570 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5571 "start more than one children\n", NULL, NULL);
5572 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005573 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005574 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005575}
5576
5577/**
5578 * xmlRelaxNGParseGrammarContent:
5579 * @ctxt: a Relax-NG parser context
5580 * @nodes: grammar children nodes
5581 *
5582 * parse the content of a RelaxNG grammar node.
5583 *
5584 * Returns 0 in case of success, -1 in case of error
5585 */
5586static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005587xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5588 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005589{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005590 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005591
5592 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005593 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5594 "grammar has no children\n", NULL, NULL);
5595 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005596 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005597 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005598 if (IS_RELAXNG(nodes, "start")) {
5599 if (nodes->children == NULL) {
5600 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5601 "start has no children\n", NULL, NULL);
5602 } else {
5603 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5604 if (tmp != 0)
5605 ret = -1;
5606 }
5607 } else if (IS_RELAXNG(nodes, "define")) {
5608 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5609 if (tmp != 0)
5610 ret = -1;
5611 } else if (IS_RELAXNG(nodes, "include")) {
5612 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5613 if (tmp != 0)
5614 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005615 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005616 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5617 "grammar has unexpected child %s\n", nodes->name,
5618 NULL);
5619 ret = -1;
5620 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005621 nodes = nodes->next;
5622 }
5623 return (ret);
5624}
5625
5626/**
5627 * xmlRelaxNGCheckReference:
5628 * @ref: the ref
5629 * @ctxt: a Relax-NG parser context
5630 * @name: the name associated to the defines
5631 *
5632 * Applies the 4.17. combine attribute rule for all the define
5633 * element of a given grammar using the same name.
5634 */
5635static void
5636xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005637 xmlRelaxNGParserCtxtPtr ctxt,
5638 const xmlChar * name)
5639{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005640 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005641 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005642
5643 grammar = ctxt->grammar;
5644 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005645 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5646 "Internal error: no grammar in CheckReference %s\n",
5647 name, NULL);
5648 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005649 }
5650 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005651 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5652 "Internal error: reference has content in CheckReference %s\n",
5653 name, NULL);
5654 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005655 }
5656 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005657 def = xmlHashLookup(grammar->defs, name);
5658 if (def != NULL) {
5659 cur = ref;
5660 while (cur != NULL) {
5661 cur->content = def;
5662 cur = cur->nextHash;
5663 }
5664 } else {
5665 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5666 "Reference %s has no matching definition\n", name,
5667 NULL);
5668 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005669 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005670 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5671 "Reference %s has no matching definition\n", name,
5672 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005673 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005674}
5675
5676/**
5677 * xmlRelaxNGCheckCombine:
5678 * @define: the define(s) list
5679 * @ctxt: a Relax-NG parser context
5680 * @name: the name associated to the defines
5681 *
5682 * Applies the 4.17. combine attribute rule for all the define
5683 * element of a given grammar using the same name.
5684 */
5685static void
5686xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005687 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5688{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005689 xmlChar *combine;
5690 int choiceOrInterleave = -1;
5691 int missing = 0;
5692 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5693
5694 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005695 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005696 cur = define;
5697 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005698 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5699 if (combine != NULL) {
5700 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5701 if (choiceOrInterleave == -1)
5702 choiceOrInterleave = 1;
5703 else if (choiceOrInterleave == 0) {
5704 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5705 "Defines for %s use both 'choice' and 'interleave'\n",
5706 name, NULL);
5707 }
5708 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5709 if (choiceOrInterleave == -1)
5710 choiceOrInterleave = 0;
5711 else if (choiceOrInterleave == 1) {
5712 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5713 "Defines for %s use both 'choice' and 'interleave'\n",
5714 name, NULL);
5715 }
5716 } else {
5717 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5718 "Defines for %s use unknown combine value '%s''\n",
5719 name, combine);
5720 }
5721 xmlFree(combine);
5722 } else {
5723 if (missing == 0)
5724 missing = 1;
5725 else {
5726 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5727 "Some defines for %s needs the combine attribute\n",
5728 name, NULL);
5729 }
5730 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005731
Daniel Veillard4c004142003-10-07 11:33:24 +00005732 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005733 }
5734#ifdef DEBUG
5735 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005736 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5737 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005738#endif
5739 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005740 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005741 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005742 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005743 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005744 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005745 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005746 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005747 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005748 tmp = define;
5749 last = NULL;
5750 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005751 if (tmp->content != NULL) {
5752 if (tmp->content->next != NULL) {
5753 /*
5754 * we need first to create a wrapper.
5755 */
5756 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5757 if (tmp2 == NULL)
5758 break;
5759 tmp2->type = XML_RELAXNG_GROUP;
5760 tmp2->content = tmp->content;
5761 } else {
5762 tmp2 = tmp->content;
5763 }
5764 if (last == NULL) {
5765 cur->content = tmp2;
5766 } else {
5767 last->next = tmp2;
5768 }
5769 last = tmp2;
5770 }
5771 tmp->content = cur;
5772 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005773 }
5774 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005775 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005776 if (ctxt->interleaves == NULL)
5777 ctxt->interleaves = xmlHashCreate(10);
5778 if (ctxt->interleaves == NULL) {
5779 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5780 "Failed to create interleaves hash table\n", NULL,
5781 NULL);
5782 } else {
5783 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005784
Daniel Veillard4c004142003-10-07 11:33:24 +00005785 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5786 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5787 0) {
5788 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5789 "Failed to add %s to hash table\n",
5790 (const xmlChar *) tmpname, NULL);
5791 }
5792 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005793 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005794}
5795
5796/**
5797 * xmlRelaxNGCombineStart:
5798 * @ctxt: a Relax-NG parser context
5799 * @grammar: the grammar
5800 *
5801 * Applies the 4.17. combine rule for all the start
5802 * element of a given grammar.
5803 */
5804static void
5805xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005806 xmlRelaxNGGrammarPtr grammar)
5807{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005808 xmlRelaxNGDefinePtr starts;
5809 xmlChar *combine;
5810 int choiceOrInterleave = -1;
5811 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005812 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005813
Daniel Veillard2df2de22003-02-17 23:34:33 +00005814 starts = grammar->start;
5815 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005816 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005817 cur = starts;
5818 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005819 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5820 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5821 combine = NULL;
5822 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5823 "Internal error: start element not found\n", NULL,
5824 NULL);
5825 } else {
5826 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5827 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005828
Daniel Veillard4c004142003-10-07 11:33:24 +00005829 if (combine != NULL) {
5830 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5831 if (choiceOrInterleave == -1)
5832 choiceOrInterleave = 1;
5833 else if (choiceOrInterleave == 0) {
5834 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5835 "<start> use both 'choice' and 'interleave'\n",
5836 NULL, NULL);
5837 }
5838 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5839 if (choiceOrInterleave == -1)
5840 choiceOrInterleave = 0;
5841 else if (choiceOrInterleave == 1) {
5842 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5843 "<start> use both 'choice' and 'interleave'\n",
5844 NULL, NULL);
5845 }
5846 } else {
5847 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5848 "<start> uses unknown combine value '%s''\n",
5849 combine, NULL);
5850 }
5851 xmlFree(combine);
5852 } else {
5853 if (missing == 0)
5854 missing = 1;
5855 else {
5856 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5857 "Some <start> element miss the combine attribute\n",
5858 NULL, NULL);
5859 }
5860 }
5861
5862 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005863 }
5864#ifdef DEBUG
5865 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005866 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5867 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005868#endif
5869 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005870 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005871 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005872 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005873 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005874 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005875 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005876 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005877 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005878 cur->content = grammar->start;
5879 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005880 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005881 if (ctxt->interleaves == NULL)
5882 ctxt->interleaves = xmlHashCreate(10);
5883 if (ctxt->interleaves == NULL) {
5884 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5885 "Failed to create interleaves hash table\n", NULL,
5886 NULL);
5887 } else {
5888 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005889
Daniel Veillard4c004142003-10-07 11:33:24 +00005890 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5891 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5892 0) {
5893 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5894 "Failed to add %s to hash table\n",
5895 (const xmlChar *) tmpname, NULL);
5896 }
5897 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005898 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005899}
5900
5901/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005902 * xmlRelaxNGCheckCycles:
5903 * @ctxt: a Relax-NG parser context
5904 * @nodes: grammar children nodes
5905 * @depth: the counter
5906 *
5907 * Check for cycles.
5908 *
5909 * Returns 0 if check passed, and -1 in case of error
5910 */
5911static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005912xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5913 xmlRelaxNGDefinePtr cur, int depth)
5914{
Daniel Veillardd4310742003-02-18 21:12:46 +00005915 int ret = 0;
5916
5917 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005918 if ((cur->type == XML_RELAXNG_REF) ||
5919 (cur->type == XML_RELAXNG_PARENTREF)) {
5920 if (cur->depth == -1) {
5921 cur->depth = depth;
5922 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5923 cur->depth = -2;
5924 } else if (depth == cur->depth) {
5925 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5926 "Detected a cycle in %s references\n",
5927 cur->name, NULL);
5928 return (-1);
5929 }
5930 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5931 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5932 } else {
5933 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5934 }
5935 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005936 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005937 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005938}
5939
5940/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005941 * xmlRelaxNGTryUnlink:
5942 * @ctxt: a Relax-NG parser context
5943 * @cur: the definition to unlink
5944 * @parent: the parent definition
5945 * @prev: the previous sibling definition
5946 *
5947 * Try to unlink a definition. If not possble make it a NOOP
5948 *
5949 * Returns the new prev definition
5950 */
5951static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005952xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5953 xmlRelaxNGDefinePtr cur,
5954 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5955{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005956 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005957 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005958 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005959 if (parent != NULL) {
5960 if (parent->content == cur)
5961 parent->content = cur->next;
5962 else if (parent->attrs == cur)
5963 parent->attrs = cur->next;
5964 else if (parent->nameClass == cur)
5965 parent->nameClass = cur->next;
5966 } else {
5967 cur->type = XML_RELAXNG_NOOP;
5968 prev = cur;
5969 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005970 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005971 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005972}
5973
5974/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005975 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005976 * @ctxt: a Relax-NG parser context
5977 * @nodes: grammar children nodes
5978 *
5979 * Check for simplification of empty and notAllowed
5980 */
5981static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005982xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5983 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5984{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005985 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005986
Daniel Veillardfd573f12003-03-16 17:52:32 +00005987 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005988 if ((cur->type == XML_RELAXNG_REF) ||
5989 (cur->type == XML_RELAXNG_PARENTREF)) {
5990 if (cur->depth != -3) {
5991 cur->depth = -3;
5992 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5993 }
5994 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5995 cur->parent = parent;
5996 if ((parent != NULL) &&
5997 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5998 (parent->type == XML_RELAXNG_LIST) ||
5999 (parent->type == XML_RELAXNG_GROUP) ||
6000 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6001 (parent->type == XML_RELAXNG_ONEORMORE) ||
6002 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6003 parent->type = XML_RELAXNG_NOT_ALLOWED;
6004 break;
6005 }
6006 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6007 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6008 } else
6009 prev = cur;
6010 } else if (cur->type == XML_RELAXNG_EMPTY) {
6011 cur->parent = parent;
6012 if ((parent != NULL) &&
6013 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6014 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6015 parent->type = XML_RELAXNG_EMPTY;
6016 break;
6017 }
6018 if ((parent != NULL) &&
6019 ((parent->type == XML_RELAXNG_GROUP) ||
6020 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6021 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6022 } else
6023 prev = cur;
6024 } else {
6025 cur->parent = parent;
6026 if (cur->content != NULL)
6027 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6028 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6029 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6030 if (cur->nameClass != NULL)
6031 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6032 /*
6033 * On Elements, try to move attribute only generating rules on
6034 * the attrs rules.
6035 */
6036 if (cur->type == XML_RELAXNG_ELEMENT) {
6037 int attronly;
6038 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006039
Daniel Veillard4c004142003-10-07 11:33:24 +00006040 while (cur->content != NULL) {
6041 attronly =
6042 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6043 if (attronly == 1) {
6044 /*
6045 * migrate cur->content to attrs
6046 */
6047 tmp = cur->content;
6048 cur->content = tmp->next;
6049 tmp->next = cur->attrs;
6050 cur->attrs = tmp;
6051 } else {
6052 /*
6053 * cur->content can generate elements or text
6054 */
6055 break;
6056 }
6057 }
6058 pre = cur->content;
6059 while ((pre != NULL) && (pre->next != NULL)) {
6060 tmp = pre->next;
6061 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6062 if (attronly == 1) {
6063 /*
6064 * migrate tmp to attrs
6065 */
6066 pre->next = tmp->next;
6067 tmp->next = cur->attrs;
6068 cur->attrs = tmp;
6069 } else {
6070 pre = tmp;
6071 }
6072 }
6073 }
6074 /*
6075 * This may result in a simplification
6076 */
6077 if ((cur->type == XML_RELAXNG_GROUP) ||
6078 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6079 if (cur->content == NULL)
6080 cur->type = XML_RELAXNG_EMPTY;
6081 else if (cur->content->next == NULL) {
6082 if ((parent == NULL) && (prev == NULL)) {
6083 cur->type = XML_RELAXNG_NOOP;
6084 } else if (prev == NULL) {
6085 parent->content = cur->content;
6086 cur->content->next = cur->next;
6087 cur = cur->content;
6088 } else {
6089 cur->content->next = cur->next;
6090 prev->next = cur->content;
6091 cur = cur->content;
6092 }
6093 }
6094 }
6095 /*
6096 * the current node may have been transformed back
6097 */
6098 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6099 (cur->content != NULL) &&
6100 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6101 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6102 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6103 if ((parent != NULL) &&
6104 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6105 (parent->type == XML_RELAXNG_LIST) ||
6106 (parent->type == XML_RELAXNG_GROUP) ||
6107 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6108 (parent->type == XML_RELAXNG_ONEORMORE) ||
6109 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6110 parent->type = XML_RELAXNG_NOT_ALLOWED;
6111 break;
6112 }
6113 if ((parent != NULL) &&
6114 (parent->type == XML_RELAXNG_CHOICE)) {
6115 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6116 } else
6117 prev = cur;
6118 } else if (cur->type == XML_RELAXNG_EMPTY) {
6119 if ((parent != NULL) &&
6120 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6121 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6122 parent->type = XML_RELAXNG_EMPTY;
6123 break;
6124 }
6125 if ((parent != NULL) &&
6126 ((parent->type == XML_RELAXNG_GROUP) ||
6127 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6128 (parent->type == XML_RELAXNG_CHOICE))) {
6129 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6130 } else
6131 prev = cur;
6132 } else {
6133 prev = cur;
6134 }
6135 }
6136 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006137 }
6138}
6139
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006140/**
6141 * xmlRelaxNGGroupContentType:
6142 * @ct1: the first content type
6143 * @ct2: the second content type
6144 *
6145 * Try to group 2 content types
6146 *
6147 * Returns the content type
6148 */
6149static xmlRelaxNGContentType
6150xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006151 xmlRelaxNGContentType ct2)
6152{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006153 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006154 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6155 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006156 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006157 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006158 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006159 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006160 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006161 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6162 return (XML_RELAXNG_CONTENT_COMPLEX);
6163 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006164}
6165
6166/**
6167 * xmlRelaxNGMaxContentType:
6168 * @ct1: the first content type
6169 * @ct2: the second content type
6170 *
6171 * Compute the max content-type
6172 *
6173 * Returns the content type
6174 */
6175static xmlRelaxNGContentType
6176xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006177 xmlRelaxNGContentType ct2)
6178{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006179 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006180 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6181 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006182 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006183 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6184 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006185 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006186 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6187 return (XML_RELAXNG_CONTENT_COMPLEX);
6188 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006189}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006190
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006191/**
6192 * xmlRelaxNGCheckRules:
6193 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006194 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006195 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006196 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006197 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006198 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006199 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006200 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006201 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006202static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006203xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6204 xmlRelaxNGDefinePtr cur, int flags,
6205 xmlRelaxNGType ptype)
6206{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006207 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006208 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006209
Daniel Veillardfd573f12003-03-16 17:52:32 +00006210 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006211 ret = XML_RELAXNG_CONTENT_EMPTY;
6212 if ((cur->type == XML_RELAXNG_REF) ||
6213 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006214 /*
6215 * This should actually be caught by list//element(ref) at the
6216 * element boundaries, c.f. Bug #159968 local refs are dropped
6217 * in step 4.19.
6218 */
6219#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006220 if (flags & XML_RELAXNG_IN_LIST) {
6221 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6222 "Found forbidden pattern list//ref\n", NULL,
6223 NULL);
6224 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006225#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006226 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6227 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6228 "Found forbidden pattern data/except//ref\n",
6229 NULL, NULL);
6230 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006231 if (cur->content == NULL) {
6232 if (cur->type == XML_RELAXNG_PARENTREF)
6233 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6234 "Internal found no define for parent refs\n",
6235 NULL, NULL);
6236 else
6237 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6238 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006239 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006240 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006241 if (cur->depth > -4) {
6242 cur->depth = -4;
6243 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6244 flags, cur->type);
6245 cur->depth = ret - 15;
6246 } else if (cur->depth == -4) {
6247 ret = XML_RELAXNG_CONTENT_COMPLEX;
6248 } else {
6249 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6250 }
6251 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6252 /*
6253 * The 7.3 Attribute derivation rule for groups is plugged there
6254 */
6255 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6256 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6257 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6258 "Found forbidden pattern data/except//element(ref)\n",
6259 NULL, NULL);
6260 }
6261 if (flags & XML_RELAXNG_IN_LIST) {
6262 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6263 "Found forbidden pattern list//element(ref)\n",
6264 NULL, NULL);
6265 }
6266 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6267 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6268 "Found forbidden pattern attribute//element(ref)\n",
6269 NULL, NULL);
6270 }
6271 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6272 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6273 "Found forbidden pattern attribute//element(ref)\n",
6274 NULL, NULL);
6275 }
6276 /*
6277 * reset since in the simple form elements are only child
6278 * of grammar/define
6279 */
6280 nflags = 0;
6281 ret =
6282 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6283 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6284 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6285 "Element %s attributes have a content type error\n",
6286 cur->name, NULL);
6287 }
6288 ret =
6289 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6290 cur->type);
6291 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6292 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6293 "Element %s has a content type error\n",
6294 cur->name, NULL);
6295 } else {
6296 ret = XML_RELAXNG_CONTENT_COMPLEX;
6297 }
6298 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6299 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6300 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6301 "Found forbidden pattern attribute//attribute\n",
6302 NULL, NULL);
6303 }
6304 if (flags & XML_RELAXNG_IN_LIST) {
6305 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6306 "Found forbidden pattern list//attribute\n",
6307 NULL, NULL);
6308 }
6309 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6310 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6311 "Found forbidden pattern oneOrMore//group//attribute\n",
6312 NULL, NULL);
6313 }
6314 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6315 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6316 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6317 NULL, NULL);
6318 }
6319 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6320 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6321 "Found forbidden pattern data/except//attribute\n",
6322 NULL, NULL);
6323 }
6324 if (flags & XML_RELAXNG_IN_START) {
6325 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6326 "Found forbidden pattern start//attribute\n",
6327 NULL, NULL);
6328 }
6329 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6330 && (cur->name == NULL)) {
6331 if (cur->ns == NULL) {
6332 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6333 "Found anyName attribute without oneOrMore ancestor\n",
6334 NULL, NULL);
6335 } else {
6336 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6337 "Found nsName attribute without oneOrMore ancestor\n",
6338 NULL, NULL);
6339 }
6340 }
6341 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6342 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6343 ret = XML_RELAXNG_CONTENT_EMPTY;
6344 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6345 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6346 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6347 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6348 "Found forbidden pattern data/except//oneOrMore\n",
6349 NULL, NULL);
6350 }
6351 if (flags & XML_RELAXNG_IN_START) {
6352 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6353 "Found forbidden pattern start//oneOrMore\n",
6354 NULL, NULL);
6355 }
6356 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6357 ret =
6358 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6359 cur->type);
6360 ret = xmlRelaxNGGroupContentType(ret, ret);
6361 } else if (cur->type == XML_RELAXNG_LIST) {
6362 if (flags & XML_RELAXNG_IN_LIST) {
6363 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6364 "Found forbidden pattern list//list\n", NULL,
6365 NULL);
6366 }
6367 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6368 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6369 "Found forbidden pattern data/except//list\n",
6370 NULL, NULL);
6371 }
6372 if (flags & XML_RELAXNG_IN_START) {
6373 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6374 "Found forbidden pattern start//list\n", NULL,
6375 NULL);
6376 }
6377 nflags = flags | XML_RELAXNG_IN_LIST;
6378 ret =
6379 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6380 cur->type);
6381 } else if (cur->type == XML_RELAXNG_GROUP) {
6382 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6383 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6384 "Found forbidden pattern data/except//group\n",
6385 NULL, NULL);
6386 }
6387 if (flags & XML_RELAXNG_IN_START) {
6388 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6389 "Found forbidden pattern start//group\n", NULL,
6390 NULL);
6391 }
6392 if (flags & XML_RELAXNG_IN_ONEORMORE)
6393 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6394 else
6395 nflags = flags;
6396 ret =
6397 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6398 cur->type);
6399 /*
6400 * The 7.3 Attribute derivation rule for groups is plugged there
6401 */
6402 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6403 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6404 if (flags & XML_RELAXNG_IN_LIST) {
6405 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6406 "Found forbidden pattern list//interleave\n",
6407 NULL, NULL);
6408 }
6409 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6410 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6411 "Found forbidden pattern data/except//interleave\n",
6412 NULL, NULL);
6413 }
6414 if (flags & XML_RELAXNG_IN_START) {
6415 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6416 "Found forbidden pattern start//interleave\n",
6417 NULL, NULL);
6418 }
6419 if (flags & XML_RELAXNG_IN_ONEORMORE)
6420 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6421 else
6422 nflags = flags;
6423 ret =
6424 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6425 cur->type);
6426 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6427 if ((cur->parent != NULL) &&
6428 (cur->parent->type == XML_RELAXNG_DATATYPE))
6429 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6430 else
6431 nflags = flags;
6432 ret =
6433 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6434 cur->type);
6435 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6436 if (flags & XML_RELAXNG_IN_START) {
6437 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6438 "Found forbidden pattern start//data\n", NULL,
6439 NULL);
6440 }
6441 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6442 ret = XML_RELAXNG_CONTENT_SIMPLE;
6443 } else if (cur->type == XML_RELAXNG_VALUE) {
6444 if (flags & XML_RELAXNG_IN_START) {
6445 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6446 "Found forbidden pattern start//value\n", NULL,
6447 NULL);
6448 }
6449 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6450 ret = XML_RELAXNG_CONTENT_SIMPLE;
6451 } else if (cur->type == XML_RELAXNG_TEXT) {
6452 if (flags & XML_RELAXNG_IN_LIST) {
6453 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6454 "Found forbidden pattern list//text\n", NULL,
6455 NULL);
6456 }
6457 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6458 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6459 "Found forbidden pattern data/except//text\n",
6460 NULL, NULL);
6461 }
6462 if (flags & XML_RELAXNG_IN_START) {
6463 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6464 "Found forbidden pattern start//text\n", NULL,
6465 NULL);
6466 }
6467 ret = XML_RELAXNG_CONTENT_COMPLEX;
6468 } else if (cur->type == XML_RELAXNG_EMPTY) {
6469 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6470 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6471 "Found forbidden pattern data/except//empty\n",
6472 NULL, NULL);
6473 }
6474 if (flags & XML_RELAXNG_IN_START) {
6475 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6476 "Found forbidden pattern start//empty\n", NULL,
6477 NULL);
6478 }
6479 ret = XML_RELAXNG_CONTENT_EMPTY;
6480 } else if (cur->type == XML_RELAXNG_CHOICE) {
6481 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6482 ret =
6483 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6484 } else {
6485 ret =
6486 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6487 }
6488 cur = cur->next;
6489 if (ptype == XML_RELAXNG_GROUP) {
6490 val = xmlRelaxNGGroupContentType(val, ret);
6491 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6492 tmp = xmlRelaxNGGroupContentType(val, ret);
6493 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6494 tmp = xmlRelaxNGMaxContentType(val, ret);
6495 } else if (ptype == XML_RELAXNG_CHOICE) {
6496 val = xmlRelaxNGMaxContentType(val, ret);
6497 } else if (ptype == XML_RELAXNG_LIST) {
6498 val = XML_RELAXNG_CONTENT_SIMPLE;
6499 } else if (ptype == XML_RELAXNG_EXCEPT) {
6500 if (ret == XML_RELAXNG_CONTENT_ERROR)
6501 val = XML_RELAXNG_CONTENT_ERROR;
6502 else
6503 val = XML_RELAXNG_CONTENT_SIMPLE;
6504 } else {
6505 val = xmlRelaxNGGroupContentType(val, ret);
6506 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006507
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006508 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006509 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006510}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006511
6512/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006513 * xmlRelaxNGParseGrammar:
6514 * @ctxt: a Relax-NG parser context
6515 * @nodes: grammar children nodes
6516 *
6517 * parse a Relax-NG <grammar> node
6518 *
6519 * Returns the internal xmlRelaxNGGrammarPtr built or
6520 * NULL in case of error
6521 */
6522static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006523xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6524{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006525 xmlRelaxNGGrammarPtr ret, tmp, old;
6526
Daniel Veillardc482e262003-02-26 14:48:48 +00006527#ifdef DEBUG_GRAMMAR
6528 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6529#endif
6530
Daniel Veillard6eadf632003-01-23 18:29:16 +00006531 ret = xmlRelaxNGNewGrammar(ctxt);
6532 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006533 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006534
6535 /*
6536 * Link the new grammar in the tree
6537 */
6538 ret->parent = ctxt->grammar;
6539 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006540 tmp = ctxt->grammar->children;
6541 if (tmp == NULL) {
6542 ctxt->grammar->children = ret;
6543 } else {
6544 while (tmp->next != NULL)
6545 tmp = tmp->next;
6546 tmp->next = ret;
6547 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006548 }
6549
6550 old = ctxt->grammar;
6551 ctxt->grammar = ret;
6552 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6553 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006554 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006555 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6556 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006557 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006558 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6559 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006560 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006561
6562 /*
6563 * Apply 4.17 mergingd rules to defines and starts
6564 */
6565 xmlRelaxNGCombineStart(ctxt, ret);
6566 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006567 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6568 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006569 }
6570
6571 /*
6572 * link together defines and refs in this grammar
6573 */
6574 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006575 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6576 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006577 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006578
Daniel Veillard81c51e12009-08-14 18:52:10 +02006579
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006580 /* @@@@ */
6581
Daniel Veillard6eadf632003-01-23 18:29:16 +00006582 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006583 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006584}
6585
6586/**
6587 * xmlRelaxNGParseDocument:
6588 * @ctxt: a Relax-NG parser context
6589 * @node: the root node of the RelaxNG schema
6590 *
6591 * parse a Relax-NG definition resource and build an internal
6592 * xmlRelaxNG struture which can be used to validate instances.
6593 *
6594 * Returns the internal XML RelaxNG structure built or
6595 * NULL in case of error
6596 */
6597static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006598xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6599{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006600 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006601 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006602 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006603
6604 if ((ctxt == NULL) || (node == NULL))
6605 return (NULL);
6606
6607 schema = xmlRelaxNGNewRelaxNG(ctxt);
6608 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006609 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006610
Daniel Veillard276be4a2003-01-24 01:03:34 +00006611 olddefine = ctxt->define;
6612 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006613 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006614 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006615 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006616 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006617
Daniel Veillard4c004142003-10-07 11:33:24 +00006618 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6619 if (schema->topgrammar == NULL) {
6620 return (schema);
6621 }
6622 /*
6623 * Link the new grammar in the tree
6624 */
6625 ret->parent = ctxt->grammar;
6626 if (ctxt->grammar != NULL) {
6627 tmp = ctxt->grammar->children;
6628 if (tmp == NULL) {
6629 ctxt->grammar->children = ret;
6630 } else {
6631 while (tmp->next != NULL)
6632 tmp = tmp->next;
6633 tmp->next = ret;
6634 }
6635 }
6636 old = ctxt->grammar;
6637 ctxt->grammar = ret;
6638 xmlRelaxNGParseStart(ctxt, node);
6639 if (old != NULL)
6640 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006641 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006642 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006643 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006644 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6645 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6646 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6647 while ((schema->topgrammar->start != NULL) &&
6648 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6649 (schema->topgrammar->start->next != NULL))
6650 schema->topgrammar->start =
6651 schema->topgrammar->start->content;
6652 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6653 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6654 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006655 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006656#ifdef DEBUG
6657 if (schema == NULL)
6658 xmlGenericError(xmlGenericErrorContext,
6659 "xmlRelaxNGParseDocument() failed\n");
6660#endif
6661
6662 return (schema);
6663}
6664
6665/************************************************************************
6666 * *
6667 * Reading RelaxNGs *
6668 * *
6669 ************************************************************************/
6670
6671/**
6672 * xmlRelaxNGNewParserCtxt:
6673 * @URL: the location of the schema
6674 *
6675 * Create an XML RelaxNGs parse context for that file/resource expected
6676 * to contain an XML RelaxNGs file.
6677 *
6678 * Returns the parser context or NULL in case of error
6679 */
6680xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006681xmlRelaxNGNewParserCtxt(const char *URL)
6682{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006683 xmlRelaxNGParserCtxtPtr ret;
6684
6685 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006686 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006687
Daniel Veillard4c004142003-10-07 11:33:24 +00006688 ret =
6689 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006690 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006691 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006692 return (NULL);
6693 }
6694 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006695 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006696 ret->error = xmlGenericError;
6697 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006698 return (ret);
6699}
6700
6701/**
6702 * xmlRelaxNGNewMemParserCtxt:
6703 * @buffer: a pointer to a char array containing the schemas
6704 * @size: the size of the array
6705 *
6706 * Create an XML RelaxNGs parse context for that memory buffer expected
6707 * to contain an XML RelaxNGs file.
6708 *
6709 * Returns the parser context or NULL in case of error
6710 */
6711xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006712xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6713{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006714 xmlRelaxNGParserCtxtPtr ret;
6715
6716 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006717 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006718
Daniel Veillard4c004142003-10-07 11:33:24 +00006719 ret =
6720 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006721 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006722 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006723 return (NULL);
6724 }
6725 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6726 ret->buffer = buffer;
6727 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006728 ret->error = xmlGenericError;
6729 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006730 return (ret);
6731}
6732
6733/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006734 * xmlRelaxNGNewDocParserCtxt:
6735 * @doc: a preparsed document tree
6736 *
6737 * Create an XML RelaxNGs parser context for that document.
6738 * Note: since the process of compiling a RelaxNG schemas modifies the
6739 * document, the @doc parameter is duplicated internally.
6740 *
6741 * Returns the parser context or NULL in case of error
6742 */
6743xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006744xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6745{
Daniel Veillard33300b42003-04-17 09:09:19 +00006746 xmlRelaxNGParserCtxtPtr ret;
6747 xmlDocPtr copy;
6748
6749 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006750 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006751 copy = xmlCopyDoc(doc, 1);
6752 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006753 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006754
Daniel Veillard4c004142003-10-07 11:33:24 +00006755 ret =
6756 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006757 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006758 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006759 return (NULL);
6760 }
6761 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6762 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006763 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006764 ret->userData = xmlGenericErrorContext;
6765 return (ret);
6766}
6767
6768/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006769 * xmlRelaxNGFreeParserCtxt:
6770 * @ctxt: the schema parser context
6771 *
6772 * Free the resources associated to the schema parser context
6773 */
6774void
Daniel Veillard4c004142003-10-07 11:33:24 +00006775xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6776{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006777 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006778 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006779 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006780 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006781 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006782 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006783 if (ctxt->interleaves != NULL)
6784 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006785 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006786 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006787 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006788 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006789 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006790 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006791 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006792 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006793 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006794 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006795
Daniel Veillard4c004142003-10-07 11:33:24 +00006796 for (i = 0; i < ctxt->defNr; i++)
6797 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6798 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006799 }
Daniel Veillard42595322004-11-08 10:52:06 +00006800 if ((ctxt->document != NULL) && (ctxt->freedoc))
6801 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006802 xmlFree(ctxt);
6803}
6804
Daniel Veillard6eadf632003-01-23 18:29:16 +00006805/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006806 * xmlRelaxNGNormExtSpace:
6807 * @value: a value
6808 *
6809 * Removes the leading and ending spaces of the value
6810 * The string is modified "in situ"
6811 */
6812static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006813xmlRelaxNGNormExtSpace(xmlChar * value)
6814{
Daniel Veillardd2298792003-02-14 16:54:11 +00006815 xmlChar *start = value;
6816 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006817
Daniel Veillard4c004142003-10-07 11:33:24 +00006818 if (value == NULL)
6819 return;
6820
William M. Brack76e95df2003-10-18 16:20:14 +00006821 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006822 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006823 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006824 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006825 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006826 cur++;
6827 if (*cur == 0)
6828 return;
6829 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006830 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006831 cur++;
6832 if (*cur == 0) {
6833 *start = 0;
6834 return;
6835 }
6836 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006837 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006838 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006839 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006840 *start++ = *cur++;
6841 if (*cur == 0) {
6842 *start = 0;
6843 return;
6844 }
6845 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006846 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006847 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006848 if (*cur == 0) {
6849 *start = 0;
6850 return;
6851 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006852 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006853 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006854 }
6855}
6856
6857/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006858 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006859 * @ctxt: a Relax-NG parser context
6860 * @node: a Relax-NG node
6861 *
6862 * Check all the attributes on the given node
6863 */
6864static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006865xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6866{
Daniel Veillardd2298792003-02-14 16:54:11 +00006867 xmlAttrPtr cur, next;
6868
6869 cur = node->properties;
6870 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006871 next = cur->next;
6872 if ((cur->ns == NULL) ||
6873 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6874 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6875 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6876 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6877 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6878 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6879 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6880 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6881 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6882 "Attribute %s is not allowed on %s\n",
6883 cur->name, node->name);
6884 }
6885 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6886 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6887 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6888 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6889 "Attribute %s is not allowed on %s\n",
6890 cur->name, node->name);
6891 }
6892 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6893 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6894 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6895 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6896 "Attribute %s is not allowed on %s\n",
6897 cur->name, node->name);
6898 }
6899 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6900 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6901 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6902 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6903 "Attribute %s is not allowed on %s\n",
6904 cur->name, node->name);
6905 }
6906 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6907 xmlChar *val;
6908 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006909
Daniel Veillard4c004142003-10-07 11:33:24 +00006910 val = xmlNodeListGetString(node->doc, cur->children, 1);
6911 if (val != NULL) {
6912 if (val[0] != 0) {
6913 uri = xmlParseURI((const char *) val);
6914 if (uri == NULL) {
6915 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6916 "Attribute %s contains invalid URI %s\n",
6917 cur->name, val);
6918 } else {
6919 if (uri->scheme == NULL) {
6920 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6921 "Attribute %s URI %s is not absolute\n",
6922 cur->name, val);
6923 }
6924 if (uri->fragment != NULL) {
6925 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6926 "Attribute %s URI %s has a fragment ID\n",
6927 cur->name, val);
6928 }
6929 xmlFreeURI(uri);
6930 }
6931 }
6932 xmlFree(val);
6933 }
6934 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6935 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6936 "Unknown attribute %s on %s\n", cur->name,
6937 node->name);
6938 }
6939 }
6940 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006941 }
6942}
6943
6944/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006945 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006946 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006947 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006948 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006949 * Cleanup the subtree from unwanted nodes for parsing, resolve
6950 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006951 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006952static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006953xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6954{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006955 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006956
Daniel Veillard6eadf632003-01-23 18:29:16 +00006957 delete = NULL;
6958 cur = root;
6959 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006960 if (delete != NULL) {
6961 xmlUnlinkNode(delete);
6962 xmlFreeNode(delete);
6963 delete = NULL;
6964 }
6965 if (cur->type == XML_ELEMENT_NODE) {
6966 /*
6967 * Simplification 4.1. Annotations
6968 */
6969 if ((cur->ns == NULL) ||
6970 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6971 if ((cur->parent != NULL) &&
6972 (cur->parent->type == XML_ELEMENT_NODE) &&
6973 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6974 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6975 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6976 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6977 "element %s doesn't allow foreign elements\n",
6978 cur->parent->name, NULL);
6979 }
6980 delete = cur;
6981 goto skip_children;
6982 } else {
6983 xmlRelaxNGCleanupAttributes(ctxt, cur);
6984 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6985 xmlChar *href, *ns, *base, *URL;
6986 xmlRelaxNGDocumentPtr docu;
6987 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00006988 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006989
Daniel Veillard4c004142003-10-07 11:33:24 +00006990 ns = xmlGetProp(cur, BAD_CAST "ns");
6991 if (ns == NULL) {
6992 tmp = cur->parent;
6993 while ((tmp != NULL) &&
6994 (tmp->type == XML_ELEMENT_NODE)) {
6995 ns = xmlGetProp(tmp, BAD_CAST "ns");
6996 if (ns != NULL)
6997 break;
6998 tmp = tmp->parent;
6999 }
7000 }
7001 href = xmlGetProp(cur, BAD_CAST "href");
7002 if (href == NULL) {
7003 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7004 "xmlRelaxNGParse: externalRef has no href attribute\n",
7005 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007006 if (ns != NULL)
7007 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007008 delete = cur;
7009 goto skip_children;
7010 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007011 uri = xmlParseURI((const char *) href);
7012 if (uri == NULL) {
7013 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7014 "Incorrect URI for externalRef %s\n",
7015 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007016 if (ns != NULL)
7017 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007018 if (href != NULL)
7019 xmlFree(href);
7020 delete = cur;
7021 goto skip_children;
7022 }
7023 if (uri->fragment != NULL) {
7024 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7025 "Fragment forbidden in URI for externalRef %s\n",
7026 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007027 if (ns != NULL)
7028 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007029 xmlFreeURI(uri);
7030 if (href != NULL)
7031 xmlFree(href);
7032 delete = cur;
7033 goto skip_children;
7034 }
7035 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007036 base = xmlNodeGetBase(cur->doc, cur);
7037 URL = xmlBuildURI(href, base);
7038 if (URL == NULL) {
7039 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7040 "Failed to compute URL for externalRef %s\n",
7041 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007042 if (ns != NULL)
7043 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007044 if (href != NULL)
7045 xmlFree(href);
7046 if (base != NULL)
7047 xmlFree(base);
7048 delete = cur;
7049 goto skip_children;
7050 }
7051 if (href != NULL)
7052 xmlFree(href);
7053 if (base != NULL)
7054 xmlFree(base);
7055 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7056 if (docu == NULL) {
7057 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7058 "Failed to load externalRef %s\n", URL,
7059 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007060 if (ns != NULL)
7061 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007062 xmlFree(URL);
7063 delete = cur;
7064 goto skip_children;
7065 }
7066 if (ns != NULL)
7067 xmlFree(ns);
7068 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007069 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007070 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7071 xmlChar *href, *ns, *base, *URL;
7072 xmlRelaxNGIncludePtr incl;
7073 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007074
Daniel Veillard4c004142003-10-07 11:33:24 +00007075 href = xmlGetProp(cur, BAD_CAST "href");
7076 if (href == NULL) {
7077 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7078 "xmlRelaxNGParse: include has no href attribute\n",
7079 NULL, NULL);
7080 delete = cur;
7081 goto skip_children;
7082 }
7083 base = xmlNodeGetBase(cur->doc, cur);
7084 URL = xmlBuildURI(href, base);
7085 if (URL == NULL) {
7086 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7087 "Failed to compute URL for include %s\n",
7088 href, NULL);
7089 if (href != NULL)
7090 xmlFree(href);
7091 if (base != NULL)
7092 xmlFree(base);
7093 delete = cur;
7094 goto skip_children;
7095 }
7096 if (href != NULL)
7097 xmlFree(href);
7098 if (base != NULL)
7099 xmlFree(base);
7100 ns = xmlGetProp(cur, BAD_CAST "ns");
7101 if (ns == NULL) {
7102 tmp = cur->parent;
7103 while ((tmp != NULL) &&
7104 (tmp->type == XML_ELEMENT_NODE)) {
7105 ns = xmlGetProp(tmp, BAD_CAST "ns");
7106 if (ns != NULL)
7107 break;
7108 tmp = tmp->parent;
7109 }
7110 }
7111 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7112 if (ns != NULL)
7113 xmlFree(ns);
7114 if (incl == NULL) {
7115 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7116 "Failed to load include %s\n", URL,
7117 NULL);
7118 xmlFree(URL);
7119 delete = cur;
7120 goto skip_children;
7121 }
7122 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007123 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007124 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7125 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7126 {
7127 xmlChar *name, *ns;
7128 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007129
Daniel Veillard4c004142003-10-07 11:33:24 +00007130 /*
7131 * Simplification 4.8. name attribute of element
7132 * and attribute elements
7133 */
7134 name = xmlGetProp(cur, BAD_CAST "name");
7135 if (name != NULL) {
7136 if (cur->children == NULL) {
7137 text =
7138 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7139 name);
7140 } else {
7141 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007142
Daniel Veillard03a53c32004-10-26 16:06:51 +00007143 node = xmlNewDocNode(cur->doc, cur->ns,
7144 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007145 if (node != NULL) {
7146 xmlAddPrevSibling(cur->children, node);
7147 text = xmlNewText(name);
7148 xmlAddChild(node, text);
7149 text = node;
7150 }
7151 }
7152 if (text == NULL) {
7153 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7154 "Failed to create a name %s element\n",
7155 name, NULL);
7156 }
7157 xmlUnsetProp(cur, BAD_CAST "name");
7158 xmlFree(name);
7159 ns = xmlGetProp(cur, BAD_CAST "ns");
7160 if (ns != NULL) {
7161 if (text != NULL) {
7162 xmlSetProp(text, BAD_CAST "ns", ns);
7163 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7164 }
7165 xmlFree(ns);
7166 } else if (xmlStrEqual(cur->name,
7167 BAD_CAST "attribute")) {
7168 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7169 }
7170 }
7171 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7172 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7173 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7174 /*
7175 * Simplification 4.8. name attribute of element
7176 * and attribute elements
7177 */
7178 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7179 xmlNodePtr node;
7180 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007181
Daniel Veillard4c004142003-10-07 11:33:24 +00007182 node = cur->parent;
7183 while ((node != NULL) &&
7184 (node->type == XML_ELEMENT_NODE)) {
7185 ns = xmlGetProp(node, BAD_CAST "ns");
7186 if (ns != NULL) {
7187 break;
7188 }
7189 node = node->parent;
7190 }
7191 if (ns == NULL) {
7192 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7193 } else {
7194 xmlSetProp(cur, BAD_CAST "ns", ns);
7195 xmlFree(ns);
7196 }
7197 }
7198 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7199 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007200
Daniel Veillard4c004142003-10-07 11:33:24 +00007201 /*
7202 * Simplification: 4.10. QNames
7203 */
7204 name = xmlNodeGetContent(cur);
7205 if (name != NULL) {
7206 local = xmlSplitQName2(name, &prefix);
7207 if (local != NULL) {
7208 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007209
Daniel Veillard4c004142003-10-07 11:33:24 +00007210 ns = xmlSearchNs(cur->doc, cur, prefix);
7211 if (ns == NULL) {
7212 xmlRngPErr(ctxt, cur,
7213 XML_RNGP_PREFIX_UNDEFINED,
7214 "xmlRelaxNGParse: no namespace for prefix %s\n",
7215 prefix, NULL);
7216 } else {
7217 xmlSetProp(cur, BAD_CAST "ns",
7218 ns->href);
7219 xmlNodeSetContent(cur, local);
7220 }
7221 xmlFree(local);
7222 xmlFree(prefix);
7223 }
7224 xmlFree(name);
7225 }
7226 }
7227 /*
7228 * 4.16
7229 */
7230 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7231 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7232 xmlRngPErr(ctxt, cur,
7233 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7234 "Found nsName/except//nsName forbidden construct\n",
7235 NULL, NULL);
7236 }
7237 }
7238 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7239 (cur != root)) {
7240 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007241
Daniel Veillard4c004142003-10-07 11:33:24 +00007242 /*
7243 * 4.16
7244 */
7245 if ((cur->parent != NULL) &&
7246 (xmlStrEqual
7247 (cur->parent->name, BAD_CAST "anyName"))) {
7248 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7249 xmlRelaxNGCleanupTree(ctxt, cur);
7250 ctxt->flags = oldflags;
7251 goto skip_children;
7252 } else if ((cur->parent != NULL) &&
7253 (xmlStrEqual
7254 (cur->parent->name, BAD_CAST "nsName"))) {
7255 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7256 xmlRelaxNGCleanupTree(ctxt, cur);
7257 ctxt->flags = oldflags;
7258 goto skip_children;
7259 }
7260 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7261 /*
7262 * 4.16
7263 */
7264 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7265 xmlRngPErr(ctxt, cur,
7266 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7267 "Found anyName/except//anyName forbidden construct\n",
7268 NULL, NULL);
7269 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7270 xmlRngPErr(ctxt, cur,
7271 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7272 "Found nsName/except//anyName forbidden construct\n",
7273 NULL, NULL);
7274 }
7275 }
7276 /*
7277 * Thisd is not an else since "include" is transformed
7278 * into a div
7279 */
7280 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7281 xmlChar *ns;
7282 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007283
Daniel Veillard4c004142003-10-07 11:33:24 +00007284 /*
7285 * implements rule 4.11
7286 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007287
Daniel Veillard4c004142003-10-07 11:33:24 +00007288 ns = xmlGetProp(cur, BAD_CAST "ns");
7289
7290 child = cur->children;
7291 ins = cur;
7292 while (child != NULL) {
7293 if (ns != NULL) {
7294 if (!xmlHasProp(child, BAD_CAST "ns")) {
7295 xmlSetProp(child, BAD_CAST "ns", ns);
7296 }
7297 }
7298 tmp = child->next;
7299 xmlUnlinkNode(child);
7300 ins = xmlAddNextSibling(ins, child);
7301 child = tmp;
7302 }
7303 if (ns != NULL)
7304 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007305 /*
7306 * Since we are about to delete cur, if it's nsDef is non-NULL we
7307 * need to preserve it (it contains the ns definitions for the
7308 * children we just moved). We'll just stick it on to the end
7309 * of cur->parent's list, since it's never going to be re-serialized
7310 * (bug 143738).
7311 */
7312 if (cur->nsDef != NULL) {
7313 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7314 while (parDef->next != NULL)
7315 parDef = parDef->next;
7316 parDef->next = cur->nsDef;
7317 cur->nsDef = NULL;
7318 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007319 delete = cur;
7320 goto skip_children;
7321 }
7322 }
7323 }
7324 /*
7325 * Simplification 4.2 whitespaces
7326 */
7327 else if ((cur->type == XML_TEXT_NODE) ||
7328 (cur->type == XML_CDATA_SECTION_NODE)) {
7329 if (IS_BLANK_NODE(cur)) {
7330 if (cur->parent->type == XML_ELEMENT_NODE) {
7331 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7332 &&
7333 (!xmlStrEqual
7334 (cur->parent->name, BAD_CAST "param")))
7335 delete = cur;
7336 } else {
7337 delete = cur;
7338 goto skip_children;
7339 }
7340 }
7341 } else {
7342 delete = cur;
7343 goto skip_children;
7344 }
7345
7346 /*
7347 * Skip to next node
7348 */
7349 if (cur->children != NULL) {
7350 if ((cur->children->type != XML_ENTITY_DECL) &&
7351 (cur->children->type != XML_ENTITY_REF_NODE) &&
7352 (cur->children->type != XML_ENTITY_NODE)) {
7353 cur = cur->children;
7354 continue;
7355 }
7356 }
7357 skip_children:
7358 if (cur->next != NULL) {
7359 cur = cur->next;
7360 continue;
7361 }
7362
7363 do {
7364 cur = cur->parent;
7365 if (cur == NULL)
7366 break;
7367 if (cur == root) {
7368 cur = NULL;
7369 break;
7370 }
7371 if (cur->next != NULL) {
7372 cur = cur->next;
7373 break;
7374 }
7375 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007376 }
7377 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007378 xmlUnlinkNode(delete);
7379 xmlFreeNode(delete);
7380 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007381 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007382}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007383
Daniel Veillardc5312d72003-02-21 17:14:10 +00007384/**
7385 * xmlRelaxNGCleanupDoc:
7386 * @ctxt: a Relax-NG parser context
7387 * @doc: an xmldocPtr document pointer
7388 *
7389 * Cleanup the document from unwanted nodes for parsing, resolve
7390 * Include and externalRef lookups.
7391 *
7392 * Returns the cleaned up document or NULL in case of error
7393 */
7394static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007395xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7396{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007397 xmlNodePtr root;
7398
7399 /*
7400 * Extract the root
7401 */
7402 root = xmlDocGetRootElement(doc);
7403 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007404 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7405 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007406 return (NULL);
7407 }
7408 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007409 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007410}
7411
7412/**
7413 * xmlRelaxNGParse:
7414 * @ctxt: a Relax-NG parser context
7415 *
7416 * parse a schema definition resource and build an internal
7417 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007418 *
7419 * Returns the internal XML RelaxNG structure built from the resource or
7420 * NULL in case of error
7421 */
7422xmlRelaxNGPtr
7423xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7424{
7425 xmlRelaxNGPtr ret = NULL;
7426 xmlDocPtr doc;
7427 xmlNodePtr root;
7428
7429 xmlRelaxNGInitTypes();
7430
7431 if (ctxt == NULL)
7432 return (NULL);
7433
7434 /*
7435 * First step is to parse the input document into an DOM/Infoset
7436 */
7437 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007438 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007439 if (doc == NULL) {
7440 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7441 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7442 NULL);
7443 return (NULL);
7444 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007445 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007446 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007447 if (doc == NULL) {
7448 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7449 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7450 NULL);
7451 return (NULL);
7452 }
7453 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7454 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007455 } else if (ctxt->document != NULL) {
7456 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007457 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007458 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7459 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7460 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007461 }
7462 ctxt->document = doc;
7463
7464 /*
7465 * Some preprocessing of the document content
7466 */
7467 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7468 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007469 xmlFreeDoc(ctxt->document);
7470 ctxt->document = NULL;
7471 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007472 }
7473
Daniel Veillard6eadf632003-01-23 18:29:16 +00007474 /*
7475 * Then do the parsing for good
7476 */
7477 root = xmlDocGetRootElement(doc);
7478 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007479 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7480 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007481 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillard3f845a92006-04-13 07:33:44 +00007482
7483 xmlFreeDoc(ctxt->document);
7484 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007485 return (NULL);
7486 }
7487 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007488 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007489 xmlFreeDoc(ctxt->document);
7490 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007491 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007492 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007493
7494 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007495 * Check the ref/defines links
7496 */
7497 /*
7498 * try to preprocess interleaves
7499 */
7500 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007501 xmlHashScan(ctxt->interleaves,
7502 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007503 }
7504
7505 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007506 * if there was a parsing error return NULL
7507 */
7508 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007509 xmlRelaxNGFree(ret);
7510 ctxt->document = NULL;
7511 xmlFreeDoc(doc);
7512 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007513 }
7514
7515 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007516 * try to compile (parts of) the schemas
7517 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007518 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7519 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007520 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007521
Daniel Veillard4c004142003-10-07 11:33:24 +00007522 def = xmlRelaxNGNewDefine(ctxt, NULL);
7523 if (def != NULL) {
7524 def->type = XML_RELAXNG_START;
7525 def->content = ret->topgrammar->start;
7526 ret->topgrammar->start = def;
7527 }
7528 }
7529 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007530 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007531
7532 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007533 * Transfer the pointer for cleanup at the schema level.
7534 */
7535 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007536 ctxt->document = NULL;
7537 ret->documents = ctxt->documents;
7538 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007539
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007540 ret->includes = ctxt->includes;
7541 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007542 ret->defNr = ctxt->defNr;
7543 ret->defTab = ctxt->defTab;
7544 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007545 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007546 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007547
7548 return (ret);
7549}
Daniel Veillard4c004142003-10-07 11:33:24 +00007550
Daniel Veillard6eadf632003-01-23 18:29:16 +00007551/**
7552 * xmlRelaxNGSetParserErrors:
7553 * @ctxt: a Relax-NG validation context
7554 * @err: the error callback
7555 * @warn: the warning callback
7556 * @ctx: contextual data for the callbacks
7557 *
7558 * Set the callback functions used to handle errors for a validation context
7559 */
7560void
7561xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007562 xmlRelaxNGValidityErrorFunc err,
7563 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7564{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007565 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007566 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007567 ctxt->error = err;
7568 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007569 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007570 ctxt->userData = ctx;
7571}
Daniel Veillard409a8142003-07-18 15:16:57 +00007572
7573/**
7574 * xmlRelaxNGGetParserErrors:
7575 * @ctxt: a Relax-NG validation context
7576 * @err: the error callback result
7577 * @warn: the warning callback result
7578 * @ctx: contextual data for the callbacks result
7579 *
7580 * Get the callback information used to handle errors for a validation context
7581 *
7582 * Returns -1 in case of failure, 0 otherwise.
7583 */
7584int
7585xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007586 xmlRelaxNGValidityErrorFunc * err,
7587 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7588{
Daniel Veillard409a8142003-07-18 15:16:57 +00007589 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007590 return (-1);
7591 if (err != NULL)
7592 *err = ctxt->error;
7593 if (warn != NULL)
7594 *warn = ctxt->warning;
7595 if (ctx != NULL)
7596 *ctx = ctxt->userData;
7597 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007598}
7599
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007600/**
7601 * xmlRelaxNGSetParserStructuredErrors:
7602 * @ctxt: a Relax-NG parser context
7603 * @serror: the error callback
7604 * @ctx: contextual data for the callbacks
7605 *
7606 * Set the callback functions used to handle errors for a parsing context
7607 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007608void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007609xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007610 xmlStructuredErrorFunc serror,
7611 void *ctx)
7612{
7613 if (ctxt == NULL)
7614 return;
7615 ctxt->serror = serror;
7616 ctxt->error = NULL;
7617 ctxt->warning = NULL;
7618 ctxt->userData = ctx;
7619}
7620
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007621#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007622
Daniel Veillard6eadf632003-01-23 18:29:16 +00007623/************************************************************************
7624 * *
7625 * Dump back a compiled form *
7626 * *
7627 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007628static void xmlRelaxNGDumpDefine(FILE * output,
7629 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007630
7631/**
7632 * xmlRelaxNGDumpDefines:
7633 * @output: the file output
7634 * @defines: a list of define structures
7635 *
7636 * Dump a RelaxNG structure back
7637 */
7638static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007639xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7640{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007641 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007642 xmlRelaxNGDumpDefine(output, defines);
7643 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007644 }
7645}
7646
7647/**
7648 * xmlRelaxNGDumpDefine:
7649 * @output: the file output
7650 * @define: a define structure
7651 *
7652 * Dump a RelaxNG structure back
7653 */
7654static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007655xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7656{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007657 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007658 return;
7659 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007660 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007661 fprintf(output, "<empty/>\n");
7662 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007663 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007664 fprintf(output, "<notAllowed/>\n");
7665 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007666 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007667 fprintf(output, "<text/>\n");
7668 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007669 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007670 fprintf(output, "<element>\n");
7671 if (define->name != NULL) {
7672 fprintf(output, "<name");
7673 if (define->ns != NULL)
7674 fprintf(output, " ns=\"%s\"", define->ns);
7675 fprintf(output, ">%s</name>\n", define->name);
7676 }
7677 xmlRelaxNGDumpDefines(output, define->attrs);
7678 xmlRelaxNGDumpDefines(output, define->content);
7679 fprintf(output, "</element>\n");
7680 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007681 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007682 fprintf(output, "<list>\n");
7683 xmlRelaxNGDumpDefines(output, define->content);
7684 fprintf(output, "</list>\n");
7685 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007686 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007687 fprintf(output, "<oneOrMore>\n");
7688 xmlRelaxNGDumpDefines(output, define->content);
7689 fprintf(output, "</oneOrMore>\n");
7690 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007691 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007692 fprintf(output, "<zeroOrMore>\n");
7693 xmlRelaxNGDumpDefines(output, define->content);
7694 fprintf(output, "</zeroOrMore>\n");
7695 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007696 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007697 fprintf(output, "<choice>\n");
7698 xmlRelaxNGDumpDefines(output, define->content);
7699 fprintf(output, "</choice>\n");
7700 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007701 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007702 fprintf(output, "<group>\n");
7703 xmlRelaxNGDumpDefines(output, define->content);
7704 fprintf(output, "</group>\n");
7705 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007706 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007707 fprintf(output, "<interleave>\n");
7708 xmlRelaxNGDumpDefines(output, define->content);
7709 fprintf(output, "</interleave>\n");
7710 break;
7711 case XML_RELAXNG_OPTIONAL:
7712 fprintf(output, "<optional>\n");
7713 xmlRelaxNGDumpDefines(output, define->content);
7714 fprintf(output, "</optional>\n");
7715 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007716 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007717 fprintf(output, "<attribute>\n");
7718 xmlRelaxNGDumpDefines(output, define->content);
7719 fprintf(output, "</attribute>\n");
7720 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007721 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007722 fprintf(output, "<define");
7723 if (define->name != NULL)
7724 fprintf(output, " name=\"%s\"", define->name);
7725 fprintf(output, ">\n");
7726 xmlRelaxNGDumpDefines(output, define->content);
7727 fprintf(output, "</define>\n");
7728 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007729 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007730 fprintf(output, "<ref");
7731 if (define->name != NULL)
7732 fprintf(output, " name=\"%s\"", define->name);
7733 fprintf(output, ">\n");
7734 xmlRelaxNGDumpDefines(output, define->content);
7735 fprintf(output, "</ref>\n");
7736 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007737 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007738 fprintf(output, "<parentRef");
7739 if (define->name != NULL)
7740 fprintf(output, " name=\"%s\"", define->name);
7741 fprintf(output, ">\n");
7742 xmlRelaxNGDumpDefines(output, define->content);
7743 fprintf(output, "</parentRef>\n");
7744 break;
7745 case XML_RELAXNG_EXTERNALREF:
7746 fprintf(output, "<externalRef>");
7747 xmlRelaxNGDumpDefines(output, define->content);
7748 fprintf(output, "</externalRef>\n");
7749 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007750 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007751 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007752 TODO break;
7753 case XML_RELAXNG_START:
7754 case XML_RELAXNG_EXCEPT:
7755 case XML_RELAXNG_PARAM:
7756 TODO break;
7757 case XML_RELAXNG_NOOP:
7758 xmlRelaxNGDumpDefines(output, define->content);
7759 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007760 }
7761}
Daniel Veillard4c004142003-10-07 11:33:24 +00007762
Daniel Veillard6eadf632003-01-23 18:29:16 +00007763/**
7764 * xmlRelaxNGDumpGrammar:
7765 * @output: the file output
7766 * @grammar: a grammar structure
7767 * @top: is this a top grammar
7768 *
7769 * Dump a RelaxNG structure back
7770 */
7771static void
7772xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7773{
7774 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007775 return;
7776
Daniel Veillard6eadf632003-01-23 18:29:16 +00007777 fprintf(output, "<grammar");
7778 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007779 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7780 switch (grammar->combine) {
7781 case XML_RELAXNG_COMBINE_UNDEFINED:
7782 break;
7783 case XML_RELAXNG_COMBINE_CHOICE:
7784 fprintf(output, " combine=\"choice\"");
7785 break;
7786 case XML_RELAXNG_COMBINE_INTERLEAVE:
7787 fprintf(output, " combine=\"interleave\"");
7788 break;
7789 default:
7790 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007791 }
7792 fprintf(output, ">\n");
7793 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007794 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007795 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007796 fprintf(output, "<start>\n");
7797 xmlRelaxNGDumpDefine(output, grammar->start);
7798 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007799 }
7800 /* TODO ? Dump the defines ? */
7801 fprintf(output, "</grammar>\n");
7802}
7803
7804/**
7805 * xmlRelaxNGDump:
7806 * @output: the file output
7807 * @schema: a schema structure
7808 *
7809 * Dump a RelaxNG structure back
7810 */
7811void
7812xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7813{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007814 if (output == NULL)
7815 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007816 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007817 fprintf(output, "RelaxNG empty or failed to compile\n");
7818 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007819 }
7820 fprintf(output, "RelaxNG: ");
7821 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007822 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007823 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007824 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007825 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007826 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007827 }
7828 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007829 fprintf(output, "RelaxNG has no top grammar\n");
7830 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007831 }
7832 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7833}
7834
Daniel Veillardfebcca42003-02-16 15:44:18 +00007835/**
7836 * xmlRelaxNGDumpTree:
7837 * @output: the file output
7838 * @schema: a schema structure
7839 *
7840 * Dump the transformed RelaxNG tree.
7841 */
7842void
7843xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7844{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007845 if (output == NULL)
7846 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007847 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007848 fprintf(output, "RelaxNG empty or failed to compile\n");
7849 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007850 }
7851 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007852 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007853 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007854 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007855 }
7856}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007857#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007858
Daniel Veillard6eadf632003-01-23 18:29:16 +00007859/************************************************************************
7860 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007861 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007862 * *
7863 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007864static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7865 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007866
7867/**
7868 * xmlRelaxNGValidateCompiledCallback:
7869 * @exec: the regular expression instance
7870 * @token: the token which matched
7871 * @transdata: callback data, the define for the subelement if available
7872 @ @inputdata: callback data, the Relax NG validation context
7873 *
7874 * Handle the callback and if needed validate the element children.
7875 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007876static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007877xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007878 const xmlChar * token,
7879 void *transdata, void *inputdata)
7880{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007881 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7882 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7883 int ret;
7884
7885#ifdef DEBUG_COMPILE
7886 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007887 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007888#endif
7889 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007890 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007891 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007892 }
7893 if (define == NULL) {
7894 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007895 return;
7896 fprintf(stderr, "callback on %s missing define\n", token);
7897 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7898 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7899 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007900 }
7901 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007902 fprintf(stderr, "callback on %s missing info\n", token);
7903 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7904 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7905 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007906 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007907 fprintf(stderr, "callback on %s define is not element\n", token);
7908 if (ctxt->errNo == XML_RELAXNG_OK)
7909 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7910 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007911 }
7912 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007913 if (ret != 0)
7914 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007915}
7916
7917/**
7918 * xmlRelaxNGValidateCompiledContent:
7919 * @ctxt: the RelaxNG validation context
7920 * @regexp: the regular expression as compiled
7921 * @content: list of children to test against the regexp
7922 *
7923 * Validate the content model of an element or start using the regexp
7924 *
7925 * Returns 0 in case of success, -1 in case of error.
7926 */
7927static int
7928xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007929 xmlRegexpPtr regexp, xmlNodePtr content)
7930{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007931 xmlRegExecCtxtPtr exec;
7932 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007933 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007934 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007935
7936 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007937 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007938 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007939 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007940 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007941 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007942 cur = content;
7943 while (cur != NULL) {
7944 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007945 switch (cur->type) {
7946 case XML_TEXT_NODE:
7947 case XML_CDATA_SECTION_NODE:
7948 if (xmlIsBlankNode(cur))
7949 break;
7950 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7951 if (ret < 0) {
7952 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7953 cur->parent->name);
7954 }
7955 break;
7956 case XML_ELEMENT_NODE:
7957 if (cur->ns != NULL) {
7958 ret = xmlRegExecPushString2(exec, cur->name,
7959 cur->ns->href, ctxt);
7960 } else {
7961 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7962 }
7963 if (ret < 0) {
7964 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7965 }
7966 break;
7967 default:
7968 break;
7969 }
7970 if (ret < 0)
7971 break;
7972 /*
7973 * Switch to next element
7974 */
7975 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007976 }
7977 ret = xmlRegExecPushString(exec, NULL, NULL);
7978 if (ret == 1) {
7979 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007980 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007981 } else if (ret == 0) {
7982 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007983 * TODO: get some of the names needed to exit the current state of exec
7984 */
7985 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7986 ret = -1;
7987 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7988 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007989 } else {
7990 ret = -1;
7991 }
7992 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007993 /*
7994 * There might be content model errors outside of the pure
7995 * regexp validation, e.g. for attribute values.
7996 */
7997 if ((ret == 0) && (ctxt->perr != 0)) {
7998 ret = ctxt->perr;
7999 }
8000 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008001 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008002}
8003
8004/************************************************************************
8005 * *
8006 * Progressive validation of when possible *
8007 * *
8008 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008009static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8010 xmlRelaxNGDefinePtr defines);
8011static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008012 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008013static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008014
8015/**
8016 * xmlRelaxNGElemPush:
8017 * @ctxt: the validation context
8018 * @exec: the regexp runtime for the new content model
8019 *
8020 * Push a new regexp for the current node content model on the stack
8021 *
8022 * Returns 0 in case of success and -1 in case of error.
8023 */
8024static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008025xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8026{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008027 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008028 ctxt->elemMax = 10;
8029 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8030 sizeof
8031 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008032 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008033 xmlRngVErrMemory(ctxt, "validating\n");
8034 return (-1);
8035 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008036 }
8037 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008038 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008039 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008040 ctxt->elemMax *
8041 sizeof
8042 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008043 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008044 xmlRngVErrMemory(ctxt, "validating\n");
8045 return (-1);
8046 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008047 }
8048 ctxt->elemTab[ctxt->elemNr++] = exec;
8049 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008050 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008051}
8052
8053/**
8054 * xmlRelaxNGElemPop:
8055 * @ctxt: the validation context
8056 *
8057 * Pop the regexp of the current node content model from the stack
8058 *
8059 * Returns the exec or NULL if empty
8060 */
8061static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008062xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8063{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008064 xmlRegExecCtxtPtr ret;
8065
Daniel Veillard4c004142003-10-07 11:33:24 +00008066 if (ctxt->elemNr <= 0)
8067 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008068 ctxt->elemNr--;
8069 ret = ctxt->elemTab[ctxt->elemNr];
8070 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008071 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008072 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8073 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008074 ctxt->elem = NULL;
8075 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008076}
8077
8078/**
8079 * xmlRelaxNGValidateProgressiveCallback:
8080 * @exec: the regular expression instance
8081 * @token: the token which matched
8082 * @transdata: callback data, the define for the subelement if available
8083 @ @inputdata: callback data, the Relax NG validation context
8084 *
8085 * Handle the callback and if needed validate the element children.
8086 * some of the in/out informations are passed via the context in @inputdata.
8087 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008088static void
8089xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8090 ATTRIBUTE_UNUSED,
8091 const xmlChar * token,
8092 void *transdata, void *inputdata)
8093{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008094 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8095 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008096 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008097 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008098 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008099
8100#ifdef DEBUG_PROGRESSIVE
8101 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008102 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008103#endif
8104 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008105 fprintf(stderr, "callback on %s missing context\n", token);
8106 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008107 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008108 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008109 ctxt->pstate = 1;
8110 if (define == NULL) {
8111 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008112 return;
8113 fprintf(stderr, "callback on %s missing define\n", token);
8114 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8115 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8116 ctxt->pstate = -1;
8117 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008118 }
8119 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008120 fprintf(stderr, "callback on %s missing info\n", token);
8121 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8122 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8123 ctxt->pstate = -1;
8124 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008125 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008126 fprintf(stderr, "callback on %s define is not element\n", token);
8127 if (ctxt->errNo == XML_RELAXNG_OK)
8128 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8129 ctxt->pstate = -1;
8130 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008131 }
8132 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008133 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8134 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8135 xmlRelaxNGDumpValidError(ctxt);
8136 ctxt->pstate = -1;
8137 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008138 }
8139 if (define->contModel == NULL) {
8140 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008141 * this node cannot be validated in a streamable fashion
8142 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008143#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008144 xmlGenericError(xmlGenericErrorContext,
8145 "Element '%s' validation is not streamable\n",
8146 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008147#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008148 ctxt->pstate = 0;
8149 ctxt->pdef = define;
8150 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008151 }
8152 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008153 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008154 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008155 ctxt->pstate = -1;
8156 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008157 }
8158 xmlRelaxNGElemPush(ctxt, exec);
8159
8160 /*
8161 * Validate the attributes part of the content.
8162 */
8163 state = xmlRelaxNGNewValidState(ctxt, node);
8164 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008165 ctxt->pstate = -1;
8166 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008167 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008168 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008169 ctxt->state = state;
8170 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008171 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8172 if (ret != 0) {
8173 ctxt->pstate = -1;
8174 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8175 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008176 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008177 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008178 ctxt->state->seq = NULL;
8179 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8180 if (ret != 0) {
8181 ctxt->pstate = -1;
8182 }
8183 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008184 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008185 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008186
8187 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008188
Daniel Veillard4c004142003-10-07 11:33:24 +00008189 for (i = 0; i < ctxt->states->nbState; i++) {
8190 state = ctxt->states->tabState[i];
8191 ctxt->state = state;
8192 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008193
Daniel Veillard4c004142003-10-07 11:33:24 +00008194 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8195 tmp = 0;
8196 break;
8197 }
8198 }
8199 if (tmp != 0) {
8200 /*
8201 * validation error, log the message for the "best" one
8202 */
8203 ctxt->flags |= FLAGS_IGNORABLE;
8204 xmlRelaxNGLogBestError(ctxt);
8205 }
8206 for (i = 0; i < ctxt->states->nbState; i++) {
8207 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8208 }
8209 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8210 ctxt->states = NULL;
8211 if ((ret == 0) && (tmp == -1))
8212 ctxt->pstate = -1;
8213 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008214 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008215 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008216 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8217 xmlRelaxNGDumpValidError(ctxt);
8218 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008219 }
8220 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008221}
8222
8223/**
8224 * xmlRelaxNGValidatePushElement:
8225 * @ctxt: the validation context
8226 * @doc: a document instance
8227 * @elem: an element instance
8228 *
8229 * Push a new element start on the RelaxNG validation stack.
8230 *
8231 * returns 1 if no validation problem was found or 0 if validating the
8232 * element requires a full node, and -1 in case of error.
8233 */
8234int
Daniel Veillard33300b42003-04-17 09:09:19 +00008235xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8236 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008237 xmlNodePtr elem)
8238{
8239 int ret = 1;
8240
8241 if ((ctxt == NULL) || (elem == NULL))
8242 return (-1);
8243
8244#ifdef DEBUG_PROGRESSIVE
8245 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8246#endif
8247 if (ctxt->elem == 0) {
8248 xmlRelaxNGPtr schema;
8249 xmlRelaxNGGrammarPtr grammar;
8250 xmlRegExecCtxtPtr exec;
8251 xmlRelaxNGDefinePtr define;
8252
8253 schema = ctxt->schema;
8254 if (schema == NULL) {
8255 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8256 return (-1);
8257 }
8258 grammar = schema->topgrammar;
8259 if ((grammar == NULL) || (grammar->start == NULL)) {
8260 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8261 return (-1);
8262 }
8263 define = grammar->start;
8264 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008265 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008266 return (0);
8267 }
8268 exec = xmlRegNewExecCtxt(define->contModel,
8269 xmlRelaxNGValidateProgressiveCallback,
8270 ctxt);
8271 if (exec == NULL) {
8272 return (-1);
8273 }
8274 xmlRelaxNGElemPush(ctxt, exec);
8275 }
8276 ctxt->pnode = elem;
8277 ctxt->pstate = 0;
8278 if (elem->ns != NULL) {
8279 ret =
8280 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8281 ctxt);
8282 } else {
8283 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8284 }
8285 if (ret < 0) {
8286 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8287 } else {
8288 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008289 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008290 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008291 ret = -1;
8292 else
8293 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008294 }
8295#ifdef DEBUG_PROGRESSIVE
8296 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008297 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8298 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008299#endif
8300 return (ret);
8301}
8302
8303/**
8304 * xmlRelaxNGValidatePushCData:
8305 * @ctxt: the RelaxNG validation context
8306 * @data: some character data read
8307 * @len: the lenght of the data
8308 *
8309 * check the CData parsed for validation in the current stack
8310 *
8311 * returns 1 if no validation problem was found or -1 otherwise
8312 */
8313int
8314xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008315 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008316{
8317 int ret = 1;
8318
8319 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8320 return (-1);
8321
8322#ifdef DEBUG_PROGRESSIVE
8323 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8324#endif
8325
8326 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008327 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008328 break;
8329 data++;
8330 }
8331 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008332 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008333
8334 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8335 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008336 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008337#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008338 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008339#endif
8340
Daniel Veillard4c004142003-10-07 11:33:24 +00008341 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008342 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008343 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008344}
8345
8346/**
8347 * xmlRelaxNGValidatePopElement:
8348 * @ctxt: the RelaxNG validation context
8349 * @doc: a document instance
8350 * @elem: an element instance
8351 *
8352 * Pop the element end from the RelaxNG validation stack.
8353 *
8354 * returns 1 if no validation problem was found or 0 otherwise
8355 */
8356int
8357xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8358 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008359 xmlNodePtr elem)
8360{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008361 int ret;
8362 xmlRegExecCtxtPtr exec;
8363
Daniel Veillard4c004142003-10-07 11:33:24 +00008364 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8365 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008366#ifdef DEBUG_PROGRESSIVE
8367 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8368#endif
8369 /*
8370 * verify that we reached a terminal state of the content model.
8371 */
8372 exec = xmlRelaxNGElemPop(ctxt);
8373 ret = xmlRegExecPushString(exec, NULL, NULL);
8374 if (ret == 0) {
8375 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008376 * TODO: get some of the names needed to exit the current state of exec
8377 */
8378 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8379 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008380 } else if (ret < 0) {
8381 ret = -1;
8382 } else {
8383 ret = 1;
8384 }
8385 xmlRegFreeExecCtxt(exec);
8386#ifdef DEBUG_PROGRESSIVE
8387 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008388 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8389 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008390#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008391 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008392}
8393
8394/**
8395 * xmlRelaxNGValidateFullElement:
8396 * @ctxt: the validation context
8397 * @doc: a document instance
8398 * @elem: an element instance
8399 *
8400 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8401 * 0 and the content of the node has been expanded.
8402 *
8403 * returns 1 if no validation problem was found or -1 in case of error.
8404 */
8405int
Daniel Veillard33300b42003-04-17 09:09:19 +00008406xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8407 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008408 xmlNodePtr elem)
8409{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008410 int ret;
8411 xmlRelaxNGValidStatePtr state;
8412
Daniel Veillard4c004142003-10-07 11:33:24 +00008413 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8414 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008415#ifdef DEBUG_PROGRESSIVE
8416 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8417#endif
8418 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8419 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008420 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008421 }
8422 state->seq = elem;
8423 ctxt->state = state;
8424 ctxt->errNo = XML_RELAXNG_OK;
8425 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008426 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8427 ret = -1;
8428 else
8429 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008430 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008431 ctxt->state = NULL;
8432#ifdef DEBUG_PROGRESSIVE
8433 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008434 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8435 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008436#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008437 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008438}
8439
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008440/************************************************************************
8441 * *
8442 * Generic interpreted validation implementation *
8443 * *
8444 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008445static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8446 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008447
8448/**
8449 * xmlRelaxNGSkipIgnored:
8450 * @ctxt: a schema validation context
8451 * @node: the top node.
8452 *
8453 * Skip ignorable nodes in that context
8454 *
8455 * Returns the new sibling or NULL in case of error.
8456 */
8457static xmlNodePtr
8458xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008459 xmlNodePtr node)
8460{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008461 /*
8462 * TODO complete and handle entities
8463 */
8464 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008465 ((node->type == XML_COMMENT_NODE) ||
8466 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008467 (node->type == XML_XINCLUDE_START) ||
8468 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008469 (((node->type == XML_TEXT_NODE) ||
8470 (node->type == XML_CDATA_SECTION_NODE)) &&
8471 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8472 (IS_BLANK_NODE(node)))))) {
8473 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008474 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008475 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008476}
8477
8478/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008479 * xmlRelaxNGNormalize:
8480 * @ctxt: a schema validation context
8481 * @str: the string to normalize
8482 *
8483 * Implements the normalizeWhiteSpace( s ) function from
8484 * section 6.2.9 of the spec
8485 *
8486 * Returns the new string or NULL in case of error.
8487 */
8488static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008489xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8490{
Daniel Veillardedc91922003-01-26 00:52:04 +00008491 xmlChar *ret, *p;
8492 const xmlChar *tmp;
8493 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008494
Daniel Veillardedc91922003-01-26 00:52:04 +00008495 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008496 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008497 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008498 while (*tmp != 0)
8499 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008500 len = tmp - str;
8501
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008502 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008503 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008504 xmlRngVErrMemory(ctxt, "validating\n");
8505 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008506 }
8507 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008508 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008509 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008510 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008511 if (IS_BLANK_CH(*str)) {
8512 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008513 str++;
8514 if (*str == 0)
8515 break;
8516 *p++ = ' ';
8517 } else
8518 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008519 }
8520 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008521 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008522}
8523
8524/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008525 * xmlRelaxNGValidateDatatype:
8526 * @ctxt: a Relax-NG validation context
8527 * @value: the string value
8528 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008529 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008530 *
8531 * Validate the given value against the dataype
8532 *
8533 * Returns 0 if the validation succeeded or an error code.
8534 */
8535static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008536xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8537 const xmlChar * value,
8538 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8539{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008540 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008541 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008542 void *result = NULL;
8543 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008544
8545 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008546 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008547 }
8548 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008549 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008550 if ((define->attrs != NULL) &&
8551 (define->attrs->type == XML_RELAXNG_PARAM)) {
8552 ret =
8553 lib->check(lib->data, define->name, value, &result, node);
8554 } else {
8555 ret = lib->check(lib->data, define->name, value, NULL, node);
8556 }
8557 } else
8558 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008559 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008560 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8561 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8562 lib->freef(lib->data, result);
8563 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008564 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008565 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008566 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008567 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008568 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008569 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8570 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008571 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008572 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008573 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008574 if (lib->facet != NULL) {
8575 tmp = lib->facet(lib->data, define->name, cur->name,
8576 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008577 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008578 ret = -1;
8579 }
8580 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008581 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008582 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008583 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008584
Daniel Veillard4c004142003-10-07 11:33:24 +00008585 oldvalue = ctxt->state->value;
8586 oldendvalue = ctxt->state->endvalue;
8587 ctxt->state->value = (xmlChar *) value;
8588 ctxt->state->endvalue = NULL;
8589 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8590 ctxt->state->value = (xmlChar *) oldvalue;
8591 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008592 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008593 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008594 lib->freef(lib->data, result);
8595 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008596}
8597
8598/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008599 * xmlRelaxNGNextValue:
8600 * @ctxt: a Relax-NG validation context
8601 *
8602 * Skip to the next value when validating within a list
8603 *
8604 * Returns 0 if the operation succeeded or an error code.
8605 */
8606static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008607xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8608{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008609 xmlChar *cur;
8610
8611 cur = ctxt->state->value;
8612 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008613 ctxt->state->value = NULL;
8614 ctxt->state->endvalue = NULL;
8615 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008616 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008617 while (*cur != 0)
8618 cur++;
8619 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8620 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008621 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008622 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008623 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008624 ctxt->state->value = cur;
8625 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008626}
8627
8628/**
8629 * xmlRelaxNGValidateValueList:
8630 * @ctxt: a Relax-NG validation context
8631 * @defines: the list of definitions to verify
8632 *
8633 * Validate the given set of definitions for the current value
8634 *
8635 * Returns 0 if the validation succeeded or an error code.
8636 */
8637static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008638xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8639 xmlRelaxNGDefinePtr defines)
8640{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008641 int ret = 0;
8642
8643 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008644 ret = xmlRelaxNGValidateValue(ctxt, defines);
8645 if (ret != 0)
8646 break;
8647 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008648 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008649 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008650}
8651
8652/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008653 * xmlRelaxNGValidateValue:
8654 * @ctxt: a Relax-NG validation context
8655 * @define: the definition to verify
8656 *
8657 * Validate the given definition for the current value
8658 *
8659 * Returns 0 if the validation succeeded or an error code.
8660 */
8661static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008662xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8663 xmlRelaxNGDefinePtr define)
8664{
Daniel Veillardedc91922003-01-26 00:52:04 +00008665 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008666 xmlChar *value;
8667
8668 value = ctxt->state->value;
8669 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008670 case XML_RELAXNG_EMPTY:{
8671 if ((value != NULL) && (value[0] != 0)) {
8672 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008673
William M. Brack76e95df2003-10-18 16:20:14 +00008674 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008675 idx++;
8676 if (value[idx] != 0)
8677 ret = -1;
8678 }
8679 break;
8680 }
8681 case XML_RELAXNG_TEXT:
8682 break;
8683 case XML_RELAXNG_VALUE:{
8684 if (!xmlStrEqual(value, define->value)) {
8685 if (define->name != NULL) {
8686 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008687
Daniel Veillard4c004142003-10-07 11:33:24 +00008688 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8689 if ((lib != NULL) && (lib->comp != NULL)) {
8690 ret = lib->comp(lib->data, define->name,
8691 define->value, define->node,
8692 (void *) define->attrs,
8693 value, ctxt->state->node);
8694 } else
8695 ret = -1;
8696 if (ret < 0) {
8697 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8698 define->name);
8699 return (-1);
8700 } else if (ret == 1) {
8701 ret = 0;
8702 } else {
8703 ret = -1;
8704 }
8705 } else {
8706 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008707
Daniel Veillard4c004142003-10-07 11:33:24 +00008708 /*
8709 * TODO: trivial optimizations are possible by
8710 * computing at compile-time
8711 */
8712 nval = xmlRelaxNGNormalize(ctxt, define->value);
8713 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008714
Daniel Veillard4c004142003-10-07 11:33:24 +00008715 if ((nval == NULL) || (nvalue == NULL) ||
8716 (!xmlStrEqual(nval, nvalue)))
8717 ret = -1;
8718 if (nval != NULL)
8719 xmlFree(nval);
8720 if (nvalue != NULL)
8721 xmlFree(nvalue);
8722 }
8723 }
8724 if (ret == 0)
8725 xmlRelaxNGNextValue(ctxt);
8726 break;
8727 }
8728 case XML_RELAXNG_DATATYPE:{
8729 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8730 ctxt->state->seq);
8731 if (ret == 0)
8732 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008733
Daniel Veillard4c004142003-10-07 11:33:24 +00008734 break;
8735 }
8736 case XML_RELAXNG_CHOICE:{
8737 xmlRelaxNGDefinePtr list = define->content;
8738 xmlChar *oldvalue;
8739
8740 oldflags = ctxt->flags;
8741 ctxt->flags |= FLAGS_IGNORABLE;
8742
8743 oldvalue = ctxt->state->value;
8744 while (list != NULL) {
8745 ret = xmlRelaxNGValidateValue(ctxt, list);
8746 if (ret == 0) {
8747 break;
8748 }
8749 ctxt->state->value = oldvalue;
8750 list = list->next;
8751 }
8752 ctxt->flags = oldflags;
8753 if (ret != 0) {
8754 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8755 xmlRelaxNGDumpValidError(ctxt);
8756 } else {
8757 if (ctxt->errNr > 0)
8758 xmlRelaxNGPopErrors(ctxt, 0);
8759 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008760 break;
8761 }
8762 case XML_RELAXNG_LIST:{
8763 xmlRelaxNGDefinePtr list = define->content;
8764 xmlChar *oldvalue, *oldend, *val, *cur;
8765
Daniel Veillard416589a2003-02-17 17:25:42 +00008766#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008767 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008768#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008769
Daniel Veillard4c004142003-10-07 11:33:24 +00008770 oldvalue = ctxt->state->value;
8771 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008772
Daniel Veillard4c004142003-10-07 11:33:24 +00008773 val = xmlStrdup(oldvalue);
8774 if (val == NULL) {
8775 val = xmlStrdup(BAD_CAST "");
8776 }
8777 if (val == NULL) {
8778 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8779 return (-1);
8780 }
8781 cur = val;
8782 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008783 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008784 *cur = 0;
8785 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008786#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008787 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008788#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008789 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008790 *cur++ = 0;
8791 } else
8792 cur++;
8793 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008794#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008795 xmlGenericError(xmlGenericErrorContext,
8796 "list value: '%s' found %d items\n",
8797 oldvalue, nb_values);
8798 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008799#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008800 ctxt->state->endvalue = cur;
8801 cur = val;
8802 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8803 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008804
Daniel Veillard4c004142003-10-07 11:33:24 +00008805 ctxt->state->value = cur;
8806
8807 while (list != NULL) {
8808 if (ctxt->state->value == ctxt->state->endvalue)
8809 ctxt->state->value = NULL;
8810 ret = xmlRelaxNGValidateValue(ctxt, list);
8811 if (ret != 0) {
8812#ifdef DEBUG_LIST
8813 xmlGenericError(xmlGenericErrorContext,
8814 "Failed to validate value: '%s' with %d rule\n",
8815 ctxt->state->value, nb_values);
8816#endif
8817 break;
8818 }
8819#ifdef DEBUG_LIST
8820 nb_values++;
8821#endif
8822 list = list->next;
8823 }
8824
8825 if ((ret == 0) && (ctxt->state->value != NULL) &&
8826 (ctxt->state->value != ctxt->state->endvalue)) {
8827 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8828 ctxt->state->value);
8829 ret = -1;
8830 }
8831 xmlFree(val);
8832 ctxt->state->value = oldvalue;
8833 ctxt->state->endvalue = oldend;
8834 break;
8835 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008836 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008837 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8838 if (ret != 0) {
8839 break;
8840 }
8841 /* no break on purpose */
8842 case XML_RELAXNG_ZEROORMORE:{
8843 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008844
Daniel Veillard4c004142003-10-07 11:33:24 +00008845 oldflags = ctxt->flags;
8846 ctxt->flags |= FLAGS_IGNORABLE;
8847 cur = ctxt->state->value;
8848 temp = NULL;
8849 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8850 (temp != cur)) {
8851 temp = cur;
8852 ret =
8853 xmlRelaxNGValidateValueList(ctxt, define->content);
8854 if (ret != 0) {
8855 ctxt->state->value = temp;
8856 ret = 0;
8857 break;
8858 }
8859 cur = ctxt->state->value;
8860 }
8861 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008862 if (ctxt->errNr > 0)
8863 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008864 break;
8865 }
8866 case XML_RELAXNG_EXCEPT:{
8867 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008868
Daniel Veillard4c004142003-10-07 11:33:24 +00008869 list = define->content;
8870 while (list != NULL) {
8871 ret = xmlRelaxNGValidateValue(ctxt, list);
8872 if (ret == 0) {
8873 ret = -1;
8874 break;
8875 } else
8876 ret = 0;
8877 list = list->next;
8878 }
8879 break;
8880 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008881 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008882 case XML_RELAXNG_GROUP:{
8883 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008884
Daniel Veillard4c004142003-10-07 11:33:24 +00008885 list = define->content;
8886 while (list != NULL) {
8887 ret = xmlRelaxNGValidateValue(ctxt, list);
8888 if (ret != 0) {
8889 ret = -1;
8890 break;
8891 } else
8892 ret = 0;
8893 list = list->next;
8894 }
8895 break;
8896 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008897 case XML_RELAXNG_REF:
8898 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008899 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008900 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8901 ret = -1;
8902 } else {
8903 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8904 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008905 break;
8906 default:
8907 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008908 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008909 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008910}
8911
8912/**
8913 * xmlRelaxNGValidateValueContent:
8914 * @ctxt: a Relax-NG validation context
8915 * @defines: the list of definitions to verify
8916 *
8917 * Validate the given definitions for the current value
8918 *
8919 * Returns 0 if the validation succeeded or an error code.
8920 */
8921static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008922xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8923 xmlRelaxNGDefinePtr defines)
8924{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008925 int ret = 0;
8926
8927 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008928 ret = xmlRelaxNGValidateValue(ctxt, defines);
8929 if (ret != 0)
8930 break;
8931 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008932 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008933 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008934}
8935
8936/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008937 * xmlRelaxNGAttributeMatch:
8938 * @ctxt: a Relax-NG validation context
8939 * @define: the definition to check
8940 * @prop: the attribute
8941 *
8942 * Check if the attribute matches the definition nameClass
8943 *
8944 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8945 */
8946static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008947xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8948 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8949{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008950 int ret;
8951
8952 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008953 if (!xmlStrEqual(define->name, prop->name))
8954 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008955 }
8956 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008957 if (define->ns[0] == 0) {
8958 if (prop->ns != NULL)
8959 return (0);
8960 } else {
8961 if ((prop->ns == NULL) ||
8962 (!xmlStrEqual(define->ns, prop->ns->href)))
8963 return (0);
8964 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008965 }
8966 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008967 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008968 define = define->nameClass;
8969 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008970 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008971
Daniel Veillard4c004142003-10-07 11:33:24 +00008972 list = define->content;
8973 while (list != NULL) {
8974 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8975 if (ret == 1)
8976 return (0);
8977 if (ret < 0)
8978 return (ret);
8979 list = list->next;
8980 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008981 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008982 TODO}
8983 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008984}
8985
8986/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008987 * xmlRelaxNGValidateAttribute:
8988 * @ctxt: a Relax-NG validation context
8989 * @define: the definition to verify
8990 *
8991 * Validate the given attribute definition for that node
8992 *
8993 * Returns 0 if the validation succeeded or an error code.
8994 */
8995static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008996xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8997 xmlRelaxNGDefinePtr define)
8998{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008999 int ret = 0, i;
9000 xmlChar *value, *oldvalue;
9001 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009002 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009003
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009004 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009005 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009006 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009007 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9008 tmp = ctxt->state->attrs[i];
9009 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9010 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9011 (tmp->ns == NULL)) ||
9012 ((tmp->ns != NULL) &&
9013 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9014 prop = tmp;
9015 break;
9016 }
9017 }
9018 }
9019 if (prop != NULL) {
9020 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9021 oldvalue = ctxt->state->value;
9022 oldseq = ctxt->state->seq;
9023 ctxt->state->seq = (xmlNodePtr) prop;
9024 ctxt->state->value = value;
9025 ctxt->state->endvalue = NULL;
9026 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9027 if (ctxt->state->value != NULL)
9028 value = ctxt->state->value;
9029 if (value != NULL)
9030 xmlFree(value);
9031 ctxt->state->value = oldvalue;
9032 ctxt->state->seq = oldseq;
9033 if (ret == 0) {
9034 /*
9035 * flag the attribute as processed
9036 */
9037 ctxt->state->attrs[i] = NULL;
9038 ctxt->state->nbAttrLeft--;
9039 }
9040 } else {
9041 ret = -1;
9042 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009043#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009044 xmlGenericError(xmlGenericErrorContext,
9045 "xmlRelaxNGValidateAttribute(%s): %d\n",
9046 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009047#endif
9048 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009049 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9050 tmp = ctxt->state->attrs[i];
9051 if ((tmp != NULL) &&
9052 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9053 prop = tmp;
9054 break;
9055 }
9056 }
9057 if (prop != NULL) {
9058 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9059 oldvalue = ctxt->state->value;
9060 oldseq = ctxt->state->seq;
9061 ctxt->state->seq = (xmlNodePtr) prop;
9062 ctxt->state->value = value;
9063 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9064 if (ctxt->state->value != NULL)
9065 value = ctxt->state->value;
9066 if (value != NULL)
9067 xmlFree(value);
9068 ctxt->state->value = oldvalue;
9069 ctxt->state->seq = oldseq;
9070 if (ret == 0) {
9071 /*
9072 * flag the attribute as processed
9073 */
9074 ctxt->state->attrs[i] = NULL;
9075 ctxt->state->nbAttrLeft--;
9076 }
9077 } else {
9078 ret = -1;
9079 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009080#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009081 if (define->ns != NULL) {
9082 xmlGenericError(xmlGenericErrorContext,
9083 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9084 define->ns, ret);
9085 } else {
9086 xmlGenericError(xmlGenericErrorContext,
9087 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9088 ret);
9089 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009090#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009091 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009092
9093 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009094}
9095
9096/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009097 * xmlRelaxNGValidateAttributeList:
9098 * @ctxt: a Relax-NG validation context
9099 * @define: the list of definition to verify
9100 *
9101 * Validate the given node against the list of attribute definitions
9102 *
9103 * Returns 0 if the validation succeeded or an error code.
9104 */
9105static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009106xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9107 xmlRelaxNGDefinePtr defines)
9108{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009109 int ret = 0, res;
9110 int needmore = 0;
9111 xmlRelaxNGDefinePtr cur;
9112
9113 cur = defines;
9114 while (cur != NULL) {
9115 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009116 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9117 ret = -1;
9118 } else
9119 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009120 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009121 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009122 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009123 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009124 cur = defines;
9125 while (cur != NULL) {
9126 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009127 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9128 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9129 if (res < 0)
9130 ret = -1;
9131 } else {
9132 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9133 return (-1);
9134 }
9135 if (res == -1) /* continues on -2 */
9136 break;
9137 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009138 cur = cur->next;
9139 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009140
9141 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009142}
9143
9144/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009145 * xmlRelaxNGNodeMatchesList:
9146 * @node: the node
9147 * @list: a NULL terminated array of definitions
9148 *
9149 * Check if a node can be matched by one of the definitions
9150 *
9151 * Returns 1 if matches 0 otherwise
9152 */
9153static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009154xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9155{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009156 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009157 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009158
9159 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009160 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009161
9162 cur = list[i++];
9163 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009164 if ((node->type == XML_ELEMENT_NODE) &&
9165 (cur->type == XML_RELAXNG_ELEMENT)) {
9166 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9167 if (tmp == 1)
9168 return (1);
9169 } else if (((node->type == XML_TEXT_NODE) ||
9170 (node->type == XML_CDATA_SECTION_NODE)) &&
9171 (cur->type == XML_RELAXNG_TEXT)) {
9172 return (1);
9173 }
9174 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009175 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009176 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009177}
9178
9179/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009180 * xmlRelaxNGValidateInterleave:
9181 * @ctxt: a Relax-NG validation context
9182 * @define: the definition to verify
9183 *
9184 * Validate an interleave definition for a node.
9185 *
9186 * Returns 0 if the validation succeeded or an error code.
9187 */
9188static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009189xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9190 xmlRelaxNGDefinePtr define)
9191{
William M. Brack779af002003-08-01 15:55:39 +00009192 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009193 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009194 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009195
9196 xmlRelaxNGValidStatePtr oldstate;
9197 xmlRelaxNGPartitionPtr partitions;
9198 xmlRelaxNGInterleaveGroupPtr group = NULL;
9199 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9200 xmlNodePtr *list = NULL, *lasts = NULL;
9201
9202 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009203 partitions = (xmlRelaxNGPartitionPtr) define->data;
9204 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009205 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009206 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9207 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009208 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009209 /*
9210 * Optimizations for MIXED
9211 */
9212 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009213 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009214 ctxt->flags |= FLAGS_MIXED_CONTENT;
9215 if (nbgroups == 2) {
9216 /*
9217 * this is a pure <mixed> case
9218 */
9219 if (ctxt->state != NULL)
9220 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9221 ctxt->state->seq);
9222 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9223 ret = xmlRelaxNGValidateDefinition(ctxt,
9224 partitions->groups[1]->
9225 rule);
9226 else
9227 ret = xmlRelaxNGValidateDefinition(ctxt,
9228 partitions->groups[0]->
9229 rule);
9230 if (ret == 0) {
9231 if (ctxt->state != NULL)
9232 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9233 ctxt->state->
9234 seq);
9235 }
9236 ctxt->flags = oldflags;
9237 return (ret);
9238 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009239 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009240
9241 /*
9242 * Build arrays to store the first and last node of the chain
9243 * pertaining to each group
9244 */
9245 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9246 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009247 xmlRngVErrMemory(ctxt, "validating\n");
9248 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009249 }
9250 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9251 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9252 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009253 xmlRngVErrMemory(ctxt, "validating\n");
9254 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009255 }
9256 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9257
9258 /*
9259 * Walk the sequence of children finding the right group and
9260 * sorting them in sequences.
9261 */
9262 cur = ctxt->state->seq;
9263 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9264 start = cur;
9265 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009266 ctxt->state->seq = cur;
9267 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009268 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009269 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009270
Daniel Veillard4c004142003-10-07 11:33:24 +00009271 if ((cur->type == XML_TEXT_NODE) ||
9272 (cur->type == XML_CDATA_SECTION_NODE)) {
9273 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9274 NULL);
9275 } else if (cur->type == XML_ELEMENT_NODE) {
9276 if (cur->ns != NULL) {
9277 tmp = xmlHashLookup2(partitions->triage, cur->name,
9278 cur->ns->href);
9279 if (tmp == NULL)
9280 tmp = xmlHashLookup2(partitions->triage,
9281 BAD_CAST "#any",
9282 cur->ns->href);
9283 } else
9284 tmp =
9285 xmlHashLookup2(partitions->triage, cur->name,
9286 NULL);
9287 if (tmp == NULL)
9288 tmp =
9289 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9290 NULL);
9291 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009292
Daniel Veillard4c004142003-10-07 11:33:24 +00009293 if (tmp == NULL) {
9294 i = nbgroups;
9295 } else {
9296 i = ((long) tmp) - 1;
9297 if (partitions->flags & IS_NEEDCHECK) {
9298 group = partitions->groups[i];
9299 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9300 i = nbgroups;
9301 }
9302 }
9303 } else {
9304 for (i = 0; i < nbgroups; i++) {
9305 group = partitions->groups[i];
9306 if (group == NULL)
9307 continue;
9308 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9309 break;
9310 }
9311 }
9312 /*
9313 * We break as soon as an element not matched is found
9314 */
9315 if (i >= nbgroups) {
9316 break;
9317 }
9318 if (lasts[i] != NULL) {
9319 lasts[i]->next = cur;
9320 lasts[i] = cur;
9321 } else {
9322 list[i] = cur;
9323 lasts[i] = cur;
9324 }
9325 if (cur->next != NULL)
9326 lastchg = cur->next;
9327 else
9328 lastchg = cur;
9329 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009330 }
9331 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009332 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9333 ret = -1;
9334 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009335 }
9336 lastelem = cur;
9337 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009338 for (i = 0; i < nbgroups; i++) {
9339 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9340 group = partitions->groups[i];
9341 if (lasts[i] != NULL) {
9342 last = lasts[i]->next;
9343 lasts[i]->next = NULL;
9344 }
9345 ctxt->state->seq = list[i];
9346 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9347 if (ret != 0)
9348 break;
9349 if (ctxt->state != NULL) {
9350 cur = ctxt->state->seq;
9351 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9352 xmlRelaxNGFreeValidState(ctxt, oldstate);
9353 oldstate = ctxt->state;
9354 ctxt->state = NULL;
9355 if (cur != NULL) {
9356 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9357 ret = -1;
9358 ctxt->state = oldstate;
9359 goto done;
9360 }
9361 } else if (ctxt->states != NULL) {
9362 int j;
9363 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009364 int best = -1;
9365 int lowattr = -1;
9366
9367 /*
9368 * PBM: what happen if there is attributes checks in the interleaves
9369 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009370
Daniel Veillard4c004142003-10-07 11:33:24 +00009371 for (j = 0; j < ctxt->states->nbState; j++) {
9372 cur = ctxt->states->tabState[j]->seq;
9373 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9374 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009375 if (found == 0) {
9376 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9377 best = j;
9378 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009379 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009380 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9381 /* try to keep the latest one to mach old heuristic */
9382 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9383 best = j;
9384 }
9385 if (lowattr == 0)
9386 break;
9387 } else if (found == 0) {
9388 if (lowattr == -1) {
9389 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9390 best = j;
9391 } else
9392 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9393 /* try to keep the latest one to mach old heuristic */
9394 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9395 best = j;
9396 }
9397 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009398 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009399 /*
9400 * BIG PBM: here we pick only one restarting point :-(
9401 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009402 if (ctxt->states->nbState > 0) {
9403 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009404 if (best != -1) {
9405 oldstate = ctxt->states->tabState[best];
9406 ctxt->states->tabState[best] = NULL;
9407 } else {
9408 oldstate =
9409 ctxt->states->tabState[ctxt->states->nbState - 1];
9410 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009411 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009412 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009413 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009414 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009415 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9416 }
9417 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9418 ctxt->states = NULL;
9419 if (found == 0) {
9420 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9421 ret = -1;
9422 ctxt->state = oldstate;
9423 goto done;
9424 }
9425 } else {
9426 ret = -1;
9427 break;
9428 }
9429 if (lasts[i] != NULL) {
9430 lasts[i]->next = last;
9431 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009432 }
9433 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009434 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009435 ctxt->state = oldstate;
9436 ctxt->state->seq = lastelem;
9437 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009438 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9439 ret = -1;
9440 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009441 }
9442
Daniel Veillard4c004142003-10-07 11:33:24 +00009443 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009444 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009445 /*
9446 * builds the next links chain from the prev one
9447 */
9448 cur = lastchg;
9449 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009450 if ((cur == start) || (cur->prev == NULL))
9451 break;
9452 cur->prev->next = cur;
9453 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009454 }
9455 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009456 if (ctxt->errNr > errNr)
9457 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009458 }
9459
9460 xmlFree(list);
9461 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009462 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009463}
9464
9465/**
9466 * xmlRelaxNGValidateDefinitionList:
9467 * @ctxt: a Relax-NG validation context
9468 * @define: the list of definition to verify
9469 *
9470 * Validate the given node content against the (list) of definitions
9471 *
9472 * Returns 0 if the validation succeeded or an error code.
9473 */
9474static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009475xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9476 xmlRelaxNGDefinePtr defines)
9477{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009478 int ret = 0, res;
9479
9480
Daniel Veillard952379b2003-03-17 15:37:12 +00009481 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009482 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9483 BAD_CAST "NULL definition list");
9484 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009485 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009486 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009487 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9488 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9489 if (res < 0)
9490 ret = -1;
9491 } else {
9492 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9493 return (-1);
9494 }
9495 if (res == -1) /* continues on -2 */
9496 break;
9497 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009498 }
9499
Daniel Veillard4c004142003-10-07 11:33:24 +00009500 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009501}
9502
9503/**
9504 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009505 * @ctxt: a Relax-NG validation context
9506 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009507 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009508 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009509 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009510 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009511 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009512 */
9513static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009514xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9515 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9516{
Daniel Veillard580ced82003-03-21 21:22:48 +00009517 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009518
Daniel Veillardfd573f12003-03-16 17:52:32 +00009519 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009520 if (!xmlStrEqual(elem->name, define->name)) {
9521 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9522 return (0);
9523 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009524 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009525 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009526 if (elem->ns == NULL) {
9527 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9528 return (0);
9529 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9530 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9531 elem->name, define->ns);
9532 return (0);
9533 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009534 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009535 (define->name == NULL)) {
9536 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9537 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009538 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009539 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9540 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009541 }
9542
9543 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009544 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009545
9546 define = define->nameClass;
9547 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009548 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009549
Daniel Veillard4c004142003-10-07 11:33:24 +00009550 if (ctxt != NULL) {
9551 oldflags = ctxt->flags;
9552 ctxt->flags |= FLAGS_IGNORABLE;
9553 }
9554
9555 list = define->content;
9556 while (list != NULL) {
9557 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9558 if (ret == 1) {
9559 if (ctxt != NULL)
9560 ctxt->flags = oldflags;
9561 return (0);
9562 }
9563 if (ret < 0) {
9564 if (ctxt != NULL)
9565 ctxt->flags = oldflags;
9566 return (ret);
9567 }
9568 list = list->next;
9569 }
9570 ret = 1;
9571 if (ctxt != NULL) {
9572 ctxt->flags = oldflags;
9573 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009574 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009575 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009576
Daniel Veillard4c004142003-10-07 11:33:24 +00009577 if (ctxt != NULL) {
9578 oldflags = ctxt->flags;
9579 ctxt->flags |= FLAGS_IGNORABLE;
9580 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009581
Daniel Veillard4c004142003-10-07 11:33:24 +00009582 list = define->nameClass;
9583 while (list != NULL) {
9584 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9585 if (ret == 1) {
9586 if (ctxt != NULL)
9587 ctxt->flags = oldflags;
9588 return (1);
9589 }
9590 if (ret < 0) {
9591 if (ctxt != NULL)
9592 ctxt->flags = oldflags;
9593 return (ret);
9594 }
9595 list = list->next;
9596 }
9597 if (ctxt != NULL) {
9598 if (ret != 0) {
9599 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9600 xmlRelaxNGDumpValidError(ctxt);
9601 } else {
9602 if (ctxt->errNr > 0)
9603 xmlRelaxNGPopErrors(ctxt, 0);
9604 }
9605 }
9606 ret = 0;
9607 if (ctxt != NULL) {
9608 ctxt->flags = oldflags;
9609 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009610 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009611 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009612 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009613 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009614}
9615
9616/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009617 * xmlRelaxNGBestState:
9618 * @ctxt: a Relax-NG validation context
9619 *
9620 * Find the "best" state in the ctxt->states list of states to report
9621 * errors about. I.e. a state with no element left in the child list
9622 * or the one with the less attributes left.
9623 * This is called only if a falidation error was detected
9624 *
9625 * Returns the index of the "best" state or -1 in case of error
9626 */
9627static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009628xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9629{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009630 xmlRelaxNGValidStatePtr state;
9631 int i, tmp;
9632 int best = -1;
9633 int value = 1000000;
9634
9635 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9636 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009637 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009638
Daniel Veillard4c004142003-10-07 11:33:24 +00009639 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009640 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009641 if (state == NULL)
9642 continue;
9643 if (state->seq != NULL) {
9644 if ((best == -1) || (value > 100000)) {
9645 value = 100000;
9646 best = i;
9647 }
9648 } else {
9649 tmp = state->nbAttrLeft;
9650 if ((best == -1) || (value > tmp)) {
9651 value = tmp;
9652 best = i;
9653 }
9654 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009655 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009656 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009657}
9658
9659/**
9660 * xmlRelaxNGLogBestError:
9661 * @ctxt: a Relax-NG validation context
9662 *
9663 * Find the "best" state in the ctxt->states list of states to report
9664 * errors about and log it.
9665 */
9666static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009667xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9668{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009669 int best;
9670
9671 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9672 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009673 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009674
9675 best = xmlRelaxNGBestState(ctxt);
9676 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009677 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009678
Daniel Veillard4c004142003-10-07 11:33:24 +00009679 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009680 }
9681}
9682
9683/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009684 * xmlRelaxNGValidateElementEnd:
9685 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009686 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009687 *
9688 * Validate the end of the element, implements check that
9689 * there is nothing left not consumed in the element content
9690 * or in the attribute list.
9691 *
9692 * Returns 0 if the validation succeeded or an error code.
9693 */
9694static int
William M. Brack272693c2003-11-14 16:20:34 +00009695xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009696{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009697 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009698 xmlRelaxNGValidStatePtr state;
9699
9700 state = ctxt->state;
9701 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009702 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9703 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009704 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009705 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9706 state->node->name, state->seq->name);
9707 }
9708 return (-1);
9709 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009710 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009711 for (i = 0; i < state->nbAttrs; i++) {
9712 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009713 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009714 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9715 state->attrs[i]->name, state->node->name);
9716 }
9717 return (-1 - i);
9718 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009719 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009720 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009721}
9722
9723/**
9724 * xmlRelaxNGValidateState:
9725 * @ctxt: a Relax-NG validation context
9726 * @define: the definition to verify
9727 *
9728 * Validate the current state against the definition
9729 *
9730 * Returns 0 if the validation succeeded or an error code.
9731 */
9732static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009733xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9734 xmlRelaxNGDefinePtr define)
9735{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009736 xmlNodePtr node;
9737 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009738 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009739
9740 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009741 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9742 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009743 }
9744
9745 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009746 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009747 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009748 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009749 }
9750#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009751 for (i = 0; i < ctxt->depth; i++)
9752 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009753 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009754 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009755 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009756 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009757 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009758 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009759 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009760 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009761#endif
9762 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009763 switch (define->type) {
9764 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009765 node = xmlRelaxNGSkipIgnored(ctxt, node);
9766 ret = 0;
9767 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009768 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009769 ret = -1;
9770 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009771 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009772 while ((node != NULL) &&
9773 ((node->type == XML_TEXT_NODE) ||
9774 (node->type == XML_COMMENT_NODE) ||
9775 (node->type == XML_PI_NODE) ||
9776 (node->type == XML_CDATA_SECTION_NODE)))
9777 node = node->next;
9778 ctxt->state->seq = node;
9779 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009780 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009781 errNr = ctxt->errNr;
9782 node = xmlRelaxNGSkipIgnored(ctxt, node);
9783 if (node == NULL) {
9784 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9785 ret = -1;
9786 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9787 xmlRelaxNGDumpValidError(ctxt);
9788 break;
9789 }
9790 if (node->type != XML_ELEMENT_NODE) {
9791 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9792 ret = -1;
9793 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9794 xmlRelaxNGDumpValidError(ctxt);
9795 break;
9796 }
9797 /*
9798 * This node was already validated successfully against
9799 * this definition.
9800 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009801 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009802 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9803 if (ctxt->errNr > errNr)
9804 xmlRelaxNGPopErrors(ctxt, errNr);
9805 if (ctxt->errNr != 0) {
9806 while ((ctxt->err != NULL) &&
9807 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9808 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9809 ||
9810 ((ctxt->err->err ==
9811 XML_RELAXNG_ERR_ELEMEXTRANS)
9812 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9813 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9814 || (ctxt->err->err ==
9815 XML_RELAXNG_ERR_NOTELEM)))
9816 xmlRelaxNGValidErrorPop(ctxt);
9817 }
9818 break;
9819 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009820
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009821 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9822 if (ret <= 0) {
9823 ret = -1;
9824 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9825 xmlRelaxNGDumpValidError(ctxt);
9826 break;
9827 }
9828 ret = 0;
9829 if (ctxt->errNr != 0) {
9830 if (ctxt->errNr > errNr)
9831 xmlRelaxNGPopErrors(ctxt, errNr);
9832 while ((ctxt->err != NULL) &&
9833 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9834 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9835 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9836 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9837 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9838 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9839 xmlRelaxNGValidErrorPop(ctxt);
9840 }
9841 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009842
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009843 oldflags = ctxt->flags;
9844 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9845 ctxt->flags -= FLAGS_MIXED_CONTENT;
9846 }
9847 state = xmlRelaxNGNewValidState(ctxt, node);
9848 if (state == NULL) {
9849 ret = -1;
9850 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9851 xmlRelaxNGDumpValidError(ctxt);
9852 break;
9853 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009854
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009855 oldstate = ctxt->state;
9856 ctxt->state = state;
9857 if (define->attrs != NULL) {
9858 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9859 if (tmp != 0) {
9860 ret = -1;
9861 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9862 }
9863 }
9864 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009865 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9866 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9867 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009868
Daniel Veillard4c004142003-10-07 11:33:24 +00009869 nstate = xmlRelaxNGNewValidState(ctxt, node);
9870 ctxt->state = nstate;
9871 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009872
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009873 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9874 define->contModel,
9875 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009876 nseq = ctxt->state->seq;
9877 ctxt->state = tmpstate;
9878 ctxt->states = tmpstates;
9879 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009880
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009881#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009882 xmlGenericError(xmlGenericErrorContext,
9883 "Validating content of '%s' : %d\n",
9884 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009885#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009886 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009887 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009888
9889 if (ctxt->states != NULL) {
9890 tmp = -1;
9891
Daniel Veillardce192eb2003-04-16 15:58:05 +00009892 for (i = 0; i < ctxt->states->nbState; i++) {
9893 state = ctxt->states->tabState[i];
9894 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009895 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009896
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009897 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009898 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009899 break;
9900 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009901 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009902 if (tmp != 0) {
9903 /*
9904 * validation error, log the message for the "best" one
9905 */
9906 ctxt->flags |= FLAGS_IGNORABLE;
9907 xmlRelaxNGLogBestError(ctxt);
9908 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009909 for (i = 0; i < ctxt->states->nbState; i++) {
9910 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009911 ctxt->states->
9912 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009913 }
9914 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9915 ctxt->flags = oldflags;
9916 ctxt->states = NULL;
9917 if ((ret == 0) && (tmp == -1))
9918 ret = -1;
9919 } else {
9920 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +00009921 if (ctxt->state != NULL)
9922 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009923 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009924 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009925 xmlRelaxNGFreeValidState(ctxt, state);
9926 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009927 } else {
9928 if (define->content != NULL) {
9929 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009930 define->
9931 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009932 if (tmp != 0) {
9933 ret = -1;
9934 if (ctxt->state == NULL) {
9935 ctxt->state = oldstate;
9936 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9937 node->name);
9938 ctxt->state = NULL;
9939 } else {
9940 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9941 node->name);
9942 }
9943
9944 }
9945 }
9946 if (ctxt->states != NULL) {
9947 tmp = -1;
9948
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009949 for (i = 0; i < ctxt->states->nbState; i++) {
9950 state = ctxt->states->tabState[i];
9951 ctxt->state = state;
9952
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009953 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009954 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009955 break;
9956 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009957 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009958 if (tmp != 0) {
9959 /*
9960 * validation error, log the message for the "best" one
9961 */
9962 ctxt->flags |= FLAGS_IGNORABLE;
9963 xmlRelaxNGLogBestError(ctxt);
9964 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009965 for (i = 0; i < ctxt->states->nbState; i++) {
9966 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009967 ctxt->states->tabState[i]);
9968 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009969 }
9970 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9971 ctxt->flags = oldflags;
9972 ctxt->states = NULL;
9973 if ((ret == 0) && (tmp == -1))
9974 ret = -1;
9975 } else {
9976 state = ctxt->state;
9977 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009978 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009979 xmlRelaxNGFreeValidState(ctxt, state);
9980 }
9981 }
9982 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +00009983 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009984 }
9985 ctxt->flags = oldflags;
9986 ctxt->state = oldstate;
9987 if (oldstate != NULL)
9988 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9989 if (ret != 0) {
9990 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9991 xmlRelaxNGDumpValidError(ctxt);
9992 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +00009993#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009994 } else {
9995 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +00009996#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009997 }
9998 } else {
9999 if (ctxt->errNr > errNr)
10000 xmlRelaxNGPopErrors(ctxt, errNr);
10001 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010002
10003#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010004 xmlGenericError(xmlGenericErrorContext,
10005 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10006 node->name, ret);
10007 if (oldstate == NULL)
10008 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10009 else if (oldstate->seq == NULL)
10010 xmlGenericError(xmlGenericErrorContext, ": done\n");
10011 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10012 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10013 oldstate->seq->name);
10014 else
10015 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10016 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010017#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010018 break;
10019 case XML_RELAXNG_OPTIONAL:{
10020 errNr = ctxt->errNr;
10021 oldflags = ctxt->flags;
10022 ctxt->flags |= FLAGS_IGNORABLE;
10023 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10024 ret =
10025 xmlRelaxNGValidateDefinitionList(ctxt,
10026 define->content);
10027 if (ret != 0) {
10028 if (ctxt->state != NULL)
10029 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10030 ctxt->state = oldstate;
10031 ctxt->flags = oldflags;
10032 ret = 0;
10033 if (ctxt->errNr > errNr)
10034 xmlRelaxNGPopErrors(ctxt, errNr);
10035 break;
10036 }
10037 if (ctxt->states != NULL) {
10038 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10039 } else {
10040 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10041 if (ctxt->states == NULL) {
10042 xmlRelaxNGFreeValidState(ctxt, oldstate);
10043 ctxt->flags = oldflags;
10044 ret = -1;
10045 if (ctxt->errNr > errNr)
10046 xmlRelaxNGPopErrors(ctxt, errNr);
10047 break;
10048 }
10049 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10050 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10051 ctxt->state = NULL;
10052 }
10053 ctxt->flags = oldflags;
10054 ret = 0;
10055 if (ctxt->errNr > errNr)
10056 xmlRelaxNGPopErrors(ctxt, errNr);
10057 break;
10058 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010059 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010060 errNr = ctxt->errNr;
10061 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10062 if (ret != 0) {
10063 break;
10064 }
10065 if (ctxt->errNr > errNr)
10066 xmlRelaxNGPopErrors(ctxt, errNr);
10067 /* no break on purpose */
10068 case XML_RELAXNG_ZEROORMORE:{
10069 int progress;
10070 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10071 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010072
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010073 errNr = ctxt->errNr;
10074 res = xmlRelaxNGNewStates(ctxt, 1);
10075 if (res == NULL) {
10076 ret = -1;
10077 break;
10078 }
10079 /*
10080 * All the input states are also exit states
10081 */
10082 if (ctxt->state != NULL) {
10083 xmlRelaxNGAddStates(ctxt, res,
10084 xmlRelaxNGCopyValidState(ctxt,
10085 ctxt->
10086 state));
10087 } else {
10088 for (j = 0; j < ctxt->states->nbState; j++) {
10089 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010090 xmlRelaxNGCopyValidState(ctxt,
10091 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010092 }
10093 }
10094 oldflags = ctxt->flags;
10095 ctxt->flags |= FLAGS_IGNORABLE;
10096 do {
10097 progress = 0;
10098 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010099
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010100 if (ctxt->states != NULL) {
10101 states = ctxt->states;
10102 for (i = 0; i < states->nbState; i++) {
10103 ctxt->state = states->tabState[i];
10104 ctxt->states = NULL;
10105 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10106 define->
10107 content);
10108 if (ret == 0) {
10109 if (ctxt->state != NULL) {
10110 tmp = xmlRelaxNGAddStates(ctxt, res,
10111 ctxt->state);
10112 ctxt->state = NULL;
10113 if (tmp == 1)
10114 progress = 1;
10115 } else if (ctxt->states != NULL) {
10116 for (j = 0; j < ctxt->states->nbState;
10117 j++) {
10118 tmp =
10119 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010120 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010121 if (tmp == 1)
10122 progress = 1;
10123 }
10124 xmlRelaxNGFreeStates(ctxt,
10125 ctxt->states);
10126 ctxt->states = NULL;
10127 }
10128 } else {
10129 if (ctxt->state != NULL) {
10130 xmlRelaxNGFreeValidState(ctxt,
10131 ctxt->state);
10132 ctxt->state = NULL;
10133 }
10134 }
10135 }
10136 } else {
10137 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10138 define->
10139 content);
10140 if (ret != 0) {
10141 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10142 ctxt->state = NULL;
10143 } else {
10144 base = res->nbState;
10145 if (ctxt->state != NULL) {
10146 tmp = xmlRelaxNGAddStates(ctxt, res,
10147 ctxt->state);
10148 ctxt->state = NULL;
10149 if (tmp == 1)
10150 progress = 1;
10151 } else if (ctxt->states != NULL) {
10152 for (j = 0; j < ctxt->states->nbState; j++) {
10153 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010154 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010155 if (tmp == 1)
10156 progress = 1;
10157 }
10158 if (states == NULL) {
10159 states = ctxt->states;
10160 } else {
10161 xmlRelaxNGFreeStates(ctxt,
10162 ctxt->states);
10163 }
10164 ctxt->states = NULL;
10165 }
10166 }
10167 }
10168 if (progress) {
10169 /*
10170 * Collect all the new nodes added at that step
10171 * and make them the new node set
10172 */
10173 if (res->nbState - base == 1) {
10174 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10175 res->
10176 tabState
10177 [base]);
10178 } else {
10179 if (states == NULL) {
10180 xmlRelaxNGNewStates(ctxt,
10181 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010182 states = ctxt->states;
10183 if (states == NULL) {
10184 progress = 0;
10185 break;
10186 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010187 }
10188 states->nbState = 0;
10189 for (i = base; i < res->nbState; i++)
10190 xmlRelaxNGAddStates(ctxt, states,
10191 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010192 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010193 ctxt->states = states;
10194 }
10195 }
10196 } while (progress == 1);
10197 if (states != NULL) {
10198 xmlRelaxNGFreeStates(ctxt, states);
10199 }
10200 ctxt->states = res;
10201 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010202#if 0
10203 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010204 * errors may have to be propagated back...
10205 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010206 if (ctxt->errNr > errNr)
10207 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010208#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010209 ret = 0;
10210 break;
10211 }
10212 case XML_RELAXNG_CHOICE:{
10213 xmlRelaxNGDefinePtr list = NULL;
10214 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010215
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010216 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010217
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010218 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010219 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10220 (node != NULL)) {
10221 /*
10222 * node == NULL can't be optimized since IS_TRIABLE
10223 * doesn't account for choice which may lead to
10224 * only attributes.
10225 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010226 xmlHashTablePtr triage =
10227 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010228
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010229 /*
10230 * Something we can optimize cleanly there is only one
10231 * possble branch out !
10232 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010233 if ((node->type == XML_TEXT_NODE) ||
10234 (node->type == XML_CDATA_SECTION_NODE)) {
10235 list =
10236 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10237 } else if (node->type == XML_ELEMENT_NODE) {
10238 if (node->ns != NULL) {
10239 list = xmlHashLookup2(triage, node->name,
10240 node->ns->href);
10241 if (list == NULL)
10242 list =
10243 xmlHashLookup2(triage, BAD_CAST "#any",
10244 node->ns->href);
10245 } else
10246 list =
10247 xmlHashLookup2(triage, node->name, NULL);
10248 if (list == NULL)
10249 list =
10250 xmlHashLookup2(triage, BAD_CAST "#any",
10251 NULL);
10252 }
10253 if (list == NULL) {
10254 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010255 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010256 break;
10257 }
10258 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10259 if (ret == 0) {
10260 }
10261 break;
10262 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010263
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010264 list = define->content;
10265 oldflags = ctxt->flags;
10266 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010267
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010268 while (list != NULL) {
10269 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10270 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10271 if (ret == 0) {
10272 if (states == NULL) {
10273 states = xmlRelaxNGNewStates(ctxt, 1);
10274 }
10275 if (ctxt->state != NULL) {
10276 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10277 } else if (ctxt->states != NULL) {
10278 for (i = 0; i < ctxt->states->nbState; i++) {
10279 xmlRelaxNGAddStates(ctxt, states,
10280 ctxt->states->
10281 tabState[i]);
10282 }
10283 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10284 ctxt->states = NULL;
10285 }
10286 } else {
10287 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10288 }
10289 ctxt->state = oldstate;
10290 list = list->next;
10291 }
10292 if (states != NULL) {
10293 xmlRelaxNGFreeValidState(ctxt, oldstate);
10294 ctxt->states = states;
10295 ctxt->state = NULL;
10296 ret = 0;
10297 } else {
10298 ctxt->states = NULL;
10299 }
10300 ctxt->flags = oldflags;
10301 if (ret != 0) {
10302 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10303 xmlRelaxNGDumpValidError(ctxt);
10304 }
10305 } else {
10306 if (ctxt->errNr > errNr)
10307 xmlRelaxNGPopErrors(ctxt, errNr);
10308 }
10309 break;
10310 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010311 case XML_RELAXNG_DEF:
10312 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010313 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10314 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010315 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010316 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10317 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010318 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010319 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10320 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010321 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010322 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010323 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010324 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010325 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010326 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10327 break;
10328 case XML_RELAXNG_DATATYPE:{
10329 xmlNodePtr child;
10330 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010331
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010332 child = node;
10333 while (child != NULL) {
10334 if (child->type == XML_ELEMENT_NODE) {
10335 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10336 node->parent->name);
10337 ret = -1;
10338 break;
10339 } else if ((child->type == XML_TEXT_NODE) ||
10340 (child->type == XML_CDATA_SECTION_NODE)) {
10341 content = xmlStrcat(content, child->content);
10342 }
10343 /* TODO: handle entities ... */
10344 child = child->next;
10345 }
10346 if (ret == -1) {
10347 if (content != NULL)
10348 xmlFree(content);
10349 break;
10350 }
10351 if (content == NULL) {
10352 content = xmlStrdup(BAD_CAST "");
10353 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010354 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010355 ret = -1;
10356 break;
10357 }
10358 }
10359 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10360 ctxt->state->seq);
10361 if (ret == -1) {
10362 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10363 } else if (ret == 0) {
10364 ctxt->state->seq = NULL;
10365 }
10366 if (content != NULL)
10367 xmlFree(content);
10368 break;
10369 }
10370 case XML_RELAXNG_VALUE:{
10371 xmlChar *content = NULL;
10372 xmlChar *oldvalue;
10373 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010374
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010375 child = node;
10376 while (child != NULL) {
10377 if (child->type == XML_ELEMENT_NODE) {
10378 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10379 node->parent->name);
10380 ret = -1;
10381 break;
10382 } else if ((child->type == XML_TEXT_NODE) ||
10383 (child->type == XML_CDATA_SECTION_NODE)) {
10384 content = xmlStrcat(content, child->content);
10385 }
10386 /* TODO: handle entities ... */
10387 child = child->next;
10388 }
10389 if (ret == -1) {
10390 if (content != NULL)
10391 xmlFree(content);
10392 break;
10393 }
10394 if (content == NULL) {
10395 content = xmlStrdup(BAD_CAST "");
10396 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010397 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010398 ret = -1;
10399 break;
10400 }
10401 }
10402 oldvalue = ctxt->state->value;
10403 ctxt->state->value = content;
10404 ret = xmlRelaxNGValidateValue(ctxt, define);
10405 ctxt->state->value = oldvalue;
10406 if (ret == -1) {
10407 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10408 } else if (ret == 0) {
10409 ctxt->state->seq = NULL;
10410 }
10411 if (content != NULL)
10412 xmlFree(content);
10413 break;
10414 }
10415 case XML_RELAXNG_LIST:{
10416 xmlChar *content;
10417 xmlNodePtr child;
10418 xmlChar *oldvalue, *oldendvalue;
10419 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010420
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010421 /*
10422 * Make sure it's only text nodes
10423 */
10424
10425 content = NULL;
10426 child = node;
10427 while (child != NULL) {
10428 if (child->type == XML_ELEMENT_NODE) {
10429 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10430 node->parent->name);
10431 ret = -1;
10432 break;
10433 } else if ((child->type == XML_TEXT_NODE) ||
10434 (child->type == XML_CDATA_SECTION_NODE)) {
10435 content = xmlStrcat(content, child->content);
10436 }
10437 /* TODO: handle entities ... */
10438 child = child->next;
10439 }
10440 if (ret == -1) {
10441 if (content != NULL)
10442 xmlFree(content);
10443 break;
10444 }
10445 if (content == NULL) {
10446 content = xmlStrdup(BAD_CAST "");
10447 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010448 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010449 ret = -1;
10450 break;
10451 }
10452 }
10453 len = xmlStrlen(content);
10454 oldvalue = ctxt->state->value;
10455 oldendvalue = ctxt->state->endvalue;
10456 ctxt->state->value = content;
10457 ctxt->state->endvalue = content + len;
10458 ret = xmlRelaxNGValidateValue(ctxt, define);
10459 ctxt->state->value = oldvalue;
10460 ctxt->state->endvalue = oldendvalue;
10461 if (ret == -1) {
10462 VALID_ERR(XML_RELAXNG_ERR_LIST);
10463 } else if ((ret == 0) && (node != NULL)) {
10464 ctxt->state->seq = node->next;
10465 }
10466 if (content != NULL)
10467 xmlFree(content);
10468 break;
10469 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010470 case XML_RELAXNG_EXCEPT:
10471 case XML_RELAXNG_PARAM:
10472 TODO ret = -1;
10473 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010474 }
10475 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010476#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010477 for (i = 0; i < ctxt->depth; i++)
10478 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010479 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010480 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010481 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010482 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010483 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010484 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010485 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010486 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010487#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010488 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010489}
10490
10491/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010492 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010493 * @ctxt: a Relax-NG validation context
10494 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010495 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010496 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010497 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010498 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010499 */
10500static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010501xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10502 xmlRelaxNGDefinePtr define)
10503{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010504 xmlRelaxNGStatesPtr states, res;
10505 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010506
Daniel Veillardfd573f12003-03-16 17:52:32 +000010507 /*
10508 * We should NOT have both ctxt->state and ctxt->states
10509 */
10510 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010511 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10512 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010513 }
10514
10515 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010516 if (ctxt->states != NULL) {
10517 ctxt->state = ctxt->states->tabState[0];
10518 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10519 ctxt->states = NULL;
10520 }
10521 ret = xmlRelaxNGValidateState(ctxt, define);
10522 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10523 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10524 ctxt->state = NULL;
10525 }
10526 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10527 ctxt->state = ctxt->states->tabState[0];
10528 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10529 ctxt->states = NULL;
10530 }
10531 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010532 }
10533
10534 states = ctxt->states;
10535 ctxt->states = NULL;
10536 res = NULL;
10537 j = 0;
10538 oldflags = ctxt->flags;
10539 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010540 for (i = 0; i < states->nbState; i++) {
10541 ctxt->state = states->tabState[i];
10542 ctxt->states = NULL;
10543 ret = xmlRelaxNGValidateState(ctxt, define);
10544 /*
10545 * We should NOT have both ctxt->state and ctxt->states
10546 */
10547 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10548 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10549 ctxt->state = NULL;
10550 }
10551 if (ret == 0) {
10552 if (ctxt->states == NULL) {
10553 if (res != NULL) {
10554 /* add the state to the container */
10555 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10556 ctxt->state = NULL;
10557 } else {
10558 /* add the state directly in states */
10559 states->tabState[j++] = ctxt->state;
10560 ctxt->state = NULL;
10561 }
10562 } else {
10563 if (res == NULL) {
10564 /* make it the new container and copy other results */
10565 res = ctxt->states;
10566 ctxt->states = NULL;
10567 for (k = 0; k < j; k++)
10568 xmlRelaxNGAddStates(ctxt, res,
10569 states->tabState[k]);
10570 } else {
10571 /* add all the new results to res and reff the container */
10572 for (k = 0; k < ctxt->states->nbState; k++)
10573 xmlRelaxNGAddStates(ctxt, res,
10574 ctxt->states->tabState[k]);
10575 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10576 ctxt->states = NULL;
10577 }
10578 }
10579 } else {
10580 if (ctxt->state != NULL) {
10581 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10582 ctxt->state = NULL;
10583 } else if (ctxt->states != NULL) {
10584 for (k = 0; k < ctxt->states->nbState; k++)
10585 xmlRelaxNGFreeValidState(ctxt,
10586 ctxt->states->tabState[k]);
10587 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10588 ctxt->states = NULL;
10589 }
10590 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010591 }
10592 ctxt->flags = oldflags;
10593 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010594 xmlRelaxNGFreeStates(ctxt, states);
10595 ctxt->states = res;
10596 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010597 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010598 states->nbState = j;
10599 ctxt->states = states;
10600 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010601 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010602 ctxt->state = states->tabState[0];
10603 xmlRelaxNGFreeStates(ctxt, states);
10604 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010605 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010606 ret = -1;
10607 xmlRelaxNGFreeStates(ctxt, states);
10608 if (ctxt->states != NULL) {
10609 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10610 ctxt->states = NULL;
10611 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010612 }
10613 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010614 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10615 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010616 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010617 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010618}
10619
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010620/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010621 * xmlRelaxNGValidateDocument:
10622 * @ctxt: a Relax-NG validation context
10623 * @doc: the document
10624 *
10625 * Validate the given document
10626 *
10627 * Returns 0 if the validation succeeded or an error code.
10628 */
10629static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010630xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10631{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010632 int ret;
10633 xmlRelaxNGPtr schema;
10634 xmlRelaxNGGrammarPtr grammar;
10635 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010636 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010637
10638 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010639 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010640
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010641 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010642 schema = ctxt->schema;
10643 grammar = schema->topgrammar;
10644 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010645 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10646 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010647 }
10648 state = xmlRelaxNGNewValidState(ctxt, NULL);
10649 ctxt->state = state;
10650 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010651 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010652 state = ctxt->state;
10653 node = state->seq;
10654 node = xmlRelaxNGSkipIgnored(ctxt, node);
10655 if (node != NULL) {
10656 if (ret != -1) {
10657 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10658 ret = -1;
10659 }
10660 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010661 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010662 int i;
10663 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010664
Daniel Veillard4c004142003-10-07 11:33:24 +000010665 for (i = 0; i < ctxt->states->nbState; i++) {
10666 state = ctxt->states->tabState[i];
10667 node = state->seq;
10668 node = xmlRelaxNGSkipIgnored(ctxt, node);
10669 if (node == NULL)
10670 tmp = 0;
10671 xmlRelaxNGFreeValidState(ctxt, state);
10672 }
10673 if (tmp == -1) {
10674 if (ret != -1) {
10675 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10676 ret = -1;
10677 }
10678 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010679 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010680 if (ctxt->state != NULL) {
10681 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010682 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010683 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010684 if (ret != 0)
10685 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010686#ifdef DEBUG
10687 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010688 ctxt->error(ctxt->userData,
10689 "%d Extra error messages left on stack !\n",
10690 ctxt->errNr);
10691 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010692 }
10693#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010694#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010695 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010696 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010697
Daniel Veillard4c004142003-10-07 11:33:24 +000010698 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10699 vctxt.valid = 1;
10700 vctxt.error = ctxt->error;
10701 vctxt.warning = ctxt->warning;
10702 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010703
Daniel Veillard4c004142003-10-07 11:33:24 +000010704 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10705 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010706 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010707#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010708 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010709 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010710
Daniel Veillard4c004142003-10-07 11:33:24 +000010711 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010712}
10713
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010714/**
10715 * xmlRelaxNGCleanPSVI:
10716 * @node: an input element or document
10717 *
10718 * Call this routine to speed up XPath computation on static documents.
10719 * This stamps all the element nodes with the document order
10720 * Like for line information, the order is kept in the element->content
10721 * field, the value stored is actually - the node number (starting at -1)
10722 * to be able to differentiate from line numbers.
10723 *
10724 * Returns the number of elements found in the document or -1 in case
10725 * of error.
10726 */
10727static void
10728xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10729 xmlNodePtr cur;
10730
10731 if ((node == NULL) ||
10732 ((node->type != XML_ELEMENT_NODE) &&
10733 (node->type != XML_DOCUMENT_NODE) &&
10734 (node->type != XML_HTML_DOCUMENT_NODE)))
10735 return;
10736 if (node->type == XML_ELEMENT_NODE)
10737 node->psvi = NULL;
10738
10739 cur = node->children;
10740 while (cur != NULL) {
10741 if (cur->type == XML_ELEMENT_NODE) {
10742 cur->psvi = NULL;
10743 if (cur->children != NULL) {
10744 cur = cur->children;
10745 continue;
10746 }
10747 }
10748 if (cur->next != NULL) {
10749 cur = cur->next;
10750 continue;
10751 }
10752 do {
10753 cur = cur->parent;
10754 if (cur == NULL)
10755 break;
10756 if (cur == node) {
10757 cur = NULL;
10758 break;
10759 }
10760 if (cur->next != NULL) {
10761 cur = cur->next;
10762 break;
10763 }
10764 } while (cur != NULL);
10765 }
10766 return;
10767}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010768/************************************************************************
10769 * *
10770 * Validation interfaces *
10771 * *
10772 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010773
Daniel Veillard6eadf632003-01-23 18:29:16 +000010774/**
10775 * xmlRelaxNGNewValidCtxt:
10776 * @schema: a precompiled XML RelaxNGs
10777 *
10778 * Create an XML RelaxNGs validation context based on the given schema
10779 *
10780 * Returns the validation context or NULL in case of error
10781 */
10782xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010783xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10784{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010785 xmlRelaxNGValidCtxtPtr ret;
10786
10787 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10788 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010789 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010790 return (NULL);
10791 }
10792 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10793 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010794 ret->error = xmlGenericError;
10795 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010796 ret->errNr = 0;
10797 ret->errMax = 0;
10798 ret->err = NULL;
10799 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010800 if (schema != NULL)
10801 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010802 ret->states = NULL;
10803 ret->freeState = NULL;
10804 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010805 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010806 return (ret);
10807}
10808
10809/**
10810 * xmlRelaxNGFreeValidCtxt:
10811 * @ctxt: the schema validation context
10812 *
10813 * Free the resources associated to the schema validation context
10814 */
10815void
Daniel Veillard4c004142003-10-07 11:33:24 +000010816xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10817{
Daniel Veillard798024a2003-03-19 10:36:09 +000010818 int k;
10819
Daniel Veillard6eadf632003-01-23 18:29:16 +000010820 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010821 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010822 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010823 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010824 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010825 for (k = 0; k < ctxt->freeState->nbState; k++) {
10826 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10827 }
10828 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010829 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010830 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010831 for (k = 0; k < ctxt->freeStatesNr; k++) {
10832 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10833 }
10834 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010835 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010836 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010837 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010838 if (ctxt->elemTab != NULL) {
10839 xmlRegExecCtxtPtr exec;
10840
Daniel Veillard4c004142003-10-07 11:33:24 +000010841 exec = xmlRelaxNGElemPop(ctxt);
10842 while (exec != NULL) {
10843 xmlRegFreeExecCtxt(exec);
10844 exec = xmlRelaxNGElemPop(ctxt);
10845 }
10846 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010847 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010848 xmlFree(ctxt);
10849}
10850
10851/**
10852 * xmlRelaxNGSetValidErrors:
10853 * @ctxt: a Relax-NG validation context
10854 * @err: the error function
10855 * @warn: the warning function
10856 * @ctx: the functions context
10857 *
10858 * Set the error and warning callback informations
10859 */
10860void
10861xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010862 xmlRelaxNGValidityErrorFunc err,
10863 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10864{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010865 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010866 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010867 ctxt->error = err;
10868 ctxt->warning = warn;
10869 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010870 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010871}
10872
10873/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010874 * xmlRelaxNGSetValidStructuredErrors:
10875 * @ctxt: a Relax-NG validation context
10876 * @serror: the structured error function
10877 * @ctx: the functions context
10878 *
10879 * Set the structured error callback
10880 */
10881void
10882xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010883 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010884{
10885 if (ctxt == NULL)
10886 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010887 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010888 ctxt->error = NULL;
10889 ctxt->warning = NULL;
10890 ctxt->userData = ctx;
10891}
10892
10893/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010894 * xmlRelaxNGGetValidErrors:
10895 * @ctxt: a Relax-NG validation context
10896 * @err: the error function result
10897 * @warn: the warning function result
10898 * @ctx: the functions context result
10899 *
10900 * Get the error and warning callback informations
10901 *
10902 * Returns -1 in case of error and 0 otherwise
10903 */
10904int
10905xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010906 xmlRelaxNGValidityErrorFunc * err,
10907 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10908{
Daniel Veillard409a8142003-07-18 15:16:57 +000010909 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010910 return (-1);
10911 if (err != NULL)
10912 *err = ctxt->error;
10913 if (warn != NULL)
10914 *warn = ctxt->warning;
10915 if (ctx != NULL)
10916 *ctx = ctxt->userData;
10917 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010918}
10919
10920/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010921 * xmlRelaxNGValidateDoc:
10922 * @ctxt: a Relax-NG validation context
10923 * @doc: a parsed document tree
10924 *
10925 * Validate a document tree in memory.
10926 *
10927 * Returns 0 if the document is valid, a positive error code
10928 * number otherwise and -1 in case of internal or API error.
10929 */
10930int
Daniel Veillard4c004142003-10-07 11:33:24 +000010931xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10932{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010933 int ret;
10934
10935 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010936 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010937
10938 ctxt->doc = doc;
10939
10940 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010941 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010942 * Remove all left PSVI
10943 */
10944 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
10945
10946 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000010947 * TODO: build error codes
10948 */
10949 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010950 return (1);
10951 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010952}
10953
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010954#define bottom_relaxng
10955#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000010956#endif /* LIBXML_SCHEMAS_ENABLED */