blob: 5146d9dcaec31bfebab32b61c95f8ee716e9aefe [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 }
Daniel Veillardec18c962009-08-26 18:37:43 +02002377 if ((node == NULL) && (seq == NULL)) {
2378 node = ctxt->pnode;
2379 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002380 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002381 }
2382 /*
2383 * Stack the error for later processing if needed
2384 */
2385 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002386 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002387 }
2388}
2389
Daniel Veillard6eadf632003-01-23 18:29:16 +00002390
2391/************************************************************************
2392 * *
2393 * Type library hooks *
2394 * *
2395 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002396static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002397 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002398
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002399/**
2400 * xmlRelaxNGSchemaTypeHave:
2401 * @data: data needed for the library
2402 * @type: the type name
2403 *
2404 * Check if the given type is provided by
2405 * the W3C XMLSchema Datatype library.
2406 *
2407 * Returns 1 if yes, 0 if no and -1 in case of error.
2408 */
2409static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002410xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2411{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002412 xmlSchemaTypePtr typ;
2413
2414 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002415 return (-1);
2416 typ = xmlSchemaGetPredefinedType(type,
2417 BAD_CAST
2418 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002419 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002420 return (0);
2421 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002422}
2423
2424/**
2425 * xmlRelaxNGSchemaTypeCheck:
2426 * @data: data needed for the library
2427 * @type: the type name
2428 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002429 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002430 *
2431 * Check if the given type and value are validated by
2432 * the W3C XMLSchema Datatype library.
2433 *
2434 * Returns 1 if yes, 0 if no and -1 in case of error.
2435 */
2436static int
2437xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002438 const xmlChar * type,
2439 const xmlChar * value,
2440 void **result, xmlNodePtr node)
2441{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002442 xmlSchemaTypePtr typ;
2443 int ret;
2444
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002445 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002446 return (-1);
2447 typ = xmlSchemaGetPredefinedType(type,
2448 BAD_CAST
2449 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002450 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002451 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002452 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002453 (xmlSchemaValPtr *) result, node);
2454 if (ret == 2) /* special ID error code */
2455 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002456 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002457 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002458 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002459 return (0);
2460 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002461}
2462
2463/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002464 * xmlRelaxNGSchemaFacetCheck:
2465 * @data: data needed for the library
2466 * @type: the type name
2467 * @facet: the facet name
2468 * @val: the facet value
2469 * @strval: the string value
2470 * @value: the value to check
2471 *
2472 * Function provided by a type library to check a value facet
2473 *
2474 * Returns 1 if yes, 0 if no and -1 in case of error.
2475 */
2476static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002477xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2478 const xmlChar * type, const xmlChar * facetname,
2479 const xmlChar * val, const xmlChar * strval,
2480 void *value)
2481{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002482 xmlSchemaFacetPtr facet;
2483 xmlSchemaTypePtr typ;
2484 int ret;
2485
2486 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002487 return (-1);
2488 typ = xmlSchemaGetPredefinedType(type,
2489 BAD_CAST
2490 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002491 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002492 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002493
2494 facet = xmlSchemaNewFacet();
2495 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002496 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002497
Daniel Veillard4c004142003-10-07 11:33:24 +00002498 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002499 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002500 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002501 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002502 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002503 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002504 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002505 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002506 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002507 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002508 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002509 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002510 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002511 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002512 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002513 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002514 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002515 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002516 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002517 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002518 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002519 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2520 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2521 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2522 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002523 xmlSchemaFreeFacet(facet);
2524 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002525 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002526 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002527 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2528 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002529 xmlSchemaFreeFacet(facet);
2530 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002531 }
2532 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2533 xmlSchemaFreeFacet(facet);
2534 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002535 return (-1);
2536 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002537}
2538
2539/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002540 * xmlRelaxNGSchemaFreeValue:
2541 * @data: data needed for the library
2542 * @value: the value to free
2543 *
2544 * Function provided by a type library to free a Schemas value
2545 *
2546 * Returns 1 if yes, 0 if no and -1 in case of error.
2547 */
2548static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002549xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2550{
Daniel Veillard80b19092003-03-28 13:29:53 +00002551 xmlSchemaFreeValue(value);
2552}
2553
2554/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002555 * xmlRelaxNGSchemaTypeCompare:
2556 * @data: data needed for the library
2557 * @type: the type name
2558 * @value1: the first value
2559 * @value2: the second value
2560 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002561 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002562 * Datatype library.
2563 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002564 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002565 */
2566static int
2567xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002568 const xmlChar * type,
2569 const xmlChar * value1,
2570 xmlNodePtr ctxt1,
2571 void *comp1,
2572 const xmlChar * value2, xmlNodePtr ctxt2)
2573{
Daniel Veillard80b19092003-03-28 13:29:53 +00002574 int ret;
2575 xmlSchemaTypePtr typ;
2576 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2577
2578 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002579 return (-1);
2580 typ = xmlSchemaGetPredefinedType(type,
2581 BAD_CAST
2582 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002583 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002584 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002585 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002586 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2587 if (ret != 0)
2588 return (-1);
2589 if (res1 == NULL)
2590 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002591 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002592 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002593 }
2594 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002595 if (ret != 0) {
Daniel Veillardf4644032005-06-13 11:41:31 +00002596 if ((comp1 == NULL) && (res1 != NULL))
2597 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002598 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002599 }
2600 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002601 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002602 }
2603 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002604 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002605 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002606 xmlSchemaFreeValue(res2);
2607 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002608 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002609 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002610 return (1);
2611 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002612}
Daniel Veillard4c004142003-10-07 11:33:24 +00002613
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002614/**
2615 * xmlRelaxNGDefaultTypeHave:
2616 * @data: data needed for the library
2617 * @type: the type name
2618 *
2619 * Check if the given type is provided by
2620 * the default datatype library.
2621 *
2622 * Returns 1 if yes, 0 if no and -1 in case of error.
2623 */
2624static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002625xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2626 const xmlChar * type)
2627{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002628 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002629 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002630 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002631 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002632 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002633 return (1);
2634 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002635}
2636
2637/**
2638 * xmlRelaxNGDefaultTypeCheck:
2639 * @data: data needed for the library
2640 * @type: the type name
2641 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002642 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002643 *
2644 * Check if the given type and value are validated by
2645 * the default datatype library.
2646 *
2647 * Returns 1 if yes, 0 if no and -1 in case of error.
2648 */
2649static int
2650xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002651 const xmlChar * type ATTRIBUTE_UNUSED,
2652 const xmlChar * value ATTRIBUTE_UNUSED,
2653 void **result ATTRIBUTE_UNUSED,
2654 xmlNodePtr node ATTRIBUTE_UNUSED)
2655{
Daniel Veillardd4310742003-02-18 21:12:46 +00002656 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002657 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002658 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002659 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002660 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002661 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002662 }
2663
Daniel Veillard4c004142003-10-07 11:33:24 +00002664 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002665}
2666
2667/**
2668 * xmlRelaxNGDefaultTypeCompare:
2669 * @data: data needed for the library
2670 * @type: the type name
2671 * @value1: the first value
2672 * @value2: the second value
2673 *
2674 * Compare two values accordingly a type from the default
2675 * datatype library.
2676 *
2677 * Returns 1 if yes, 0 if no and -1 in case of error.
2678 */
2679static int
2680xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002681 const xmlChar * type,
2682 const xmlChar * value1,
2683 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2684 void *comp1 ATTRIBUTE_UNUSED,
2685 const xmlChar * value2,
2686 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2687{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002688 int ret = -1;
2689
2690 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002691 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002692 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002693 if (!xmlStrEqual(value1, value2)) {
2694 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002695
Daniel Veillard4c004142003-10-07 11:33:24 +00002696 /*
2697 * TODO: trivial optimizations are possible by
2698 * computing at compile-time
2699 */
2700 nval = xmlRelaxNGNormalize(NULL, value1);
2701 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002702
Daniel Veillard4c004142003-10-07 11:33:24 +00002703 if ((nval == NULL) || (nvalue == NULL))
2704 ret = -1;
2705 else if (xmlStrEqual(nval, nvalue))
2706 ret = 1;
2707 else
2708 ret = 0;
2709 if (nval != NULL)
2710 xmlFree(nval);
2711 if (nvalue != NULL)
2712 xmlFree(nvalue);
2713 } else
2714 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002715 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002716 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002717}
Daniel Veillard4c004142003-10-07 11:33:24 +00002718
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002719static int xmlRelaxNGTypeInitialized = 0;
2720static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2721
2722/**
2723 * xmlRelaxNGFreeTypeLibrary:
2724 * @lib: the type library structure
2725 * @namespace: the URI bound to the library
2726 *
2727 * Free the structure associated to the type library
2728 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002729static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002730xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002731 const xmlChar * namespace ATTRIBUTE_UNUSED)
2732{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002733 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002734 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002735 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002736 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002737 xmlFree(lib);
2738}
2739
2740/**
2741 * xmlRelaxNGRegisterTypeLibrary:
2742 * @namespace: the URI bound to the library
2743 * @data: data associated to the library
2744 * @have: the provide function
2745 * @check: the checking function
2746 * @comp: the comparison function
2747 *
2748 * Register a new type library
2749 *
2750 * Returns 0 in case of success and -1 in case of error.
2751 */
2752static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002753xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2754 xmlRelaxNGTypeHave have,
2755 xmlRelaxNGTypeCheck check,
2756 xmlRelaxNGTypeCompare comp,
2757 xmlRelaxNGFacetCheck facet,
2758 xmlRelaxNGTypeFree freef)
2759{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002760 xmlRelaxNGTypeLibraryPtr lib;
2761 int ret;
2762
2763 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002764 (check == NULL) || (comp == NULL))
2765 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002766 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002767 xmlGenericError(xmlGenericErrorContext,
2768 "Relax-NG types library '%s' already registered\n",
2769 namespace);
2770 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002771 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002772 lib =
2773 (xmlRelaxNGTypeLibraryPtr)
2774 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002775 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002776 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002777 return (-1);
2778 }
2779 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2780 lib->namespace = xmlStrdup(namespace);
2781 lib->data = data;
2782 lib->have = have;
2783 lib->comp = comp;
2784 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002785 lib->facet = facet;
2786 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002787 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2788 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002789 xmlGenericError(xmlGenericErrorContext,
2790 "Relax-NG types library failed to register '%s'\n",
2791 namespace);
2792 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2793 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002794 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002795 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002796}
2797
2798/**
2799 * xmlRelaxNGInitTypes:
2800 *
2801 * Initilize the default type libraries.
2802 *
2803 * Returns 0 in case of success and -1 in case of error.
2804 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002805int
Daniel Veillard4c004142003-10-07 11:33:24 +00002806xmlRelaxNGInitTypes(void)
2807{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002808 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002809 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002810 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2811 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002812 xmlGenericError(xmlGenericErrorContext,
2813 "Failed to allocate sh table for Relax-NG types\n");
2814 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002815 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002816 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2817 "http://www.w3.org/2001/XMLSchema-datatypes",
2818 NULL, xmlRelaxNGSchemaTypeHave,
2819 xmlRelaxNGSchemaTypeCheck,
2820 xmlRelaxNGSchemaTypeCompare,
2821 xmlRelaxNGSchemaFacetCheck,
2822 xmlRelaxNGSchemaFreeValue);
2823 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2824 xmlRelaxNGDefaultTypeHave,
2825 xmlRelaxNGDefaultTypeCheck,
2826 xmlRelaxNGDefaultTypeCompare, NULL,
2827 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002828 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002829 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002830}
2831
2832/**
2833 * xmlRelaxNGCleanupTypes:
2834 *
2835 * Cleanup the default Schemas type library associated to RelaxNG
2836 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002837void
2838xmlRelaxNGCleanupTypes(void)
2839{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002840 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002841 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002842 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002843 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002844 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002845 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002846}
2847
2848/************************************************************************
2849 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002850 * Compiling element content into regexp *
2851 * *
2852 * Sometime the element content can be compiled into a pure regexp, *
2853 * This allows a faster execution and streamability at that level *
2854 * *
2855 ************************************************************************/
2856
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002857/* from automata.c but not exported */
2858void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2859
2860
Daniel Veillard52b48c72003-04-13 19:53:42 +00002861static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2862 xmlRelaxNGDefinePtr def);
2863
Daniel Veillard952379b2003-03-17 15:37:12 +00002864/**
2865 * xmlRelaxNGIsCompileable:
2866 * @define: the definition to check
2867 *
2868 * Check if a definition is nullable.
2869 *
2870 * Returns 1 if yes, 0 if no and -1 in case of error
2871 */
2872static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002873xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2874{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002875 int ret = -1;
2876
Daniel Veillard952379b2003-03-17 15:37:12 +00002877 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002878 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002879 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002880 if ((def->type != XML_RELAXNG_ELEMENT) &&
2881 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002882 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002883 if ((def->type != XML_RELAXNG_ELEMENT) &&
2884 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002885 return (0);
2886 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002887 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002888 ret = xmlRelaxNGIsCompileable(def->content);
2889 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002890 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002891 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002892 ret = 1;
2893 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002894 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002895 /*
2896 * Check if the element content is compileable
2897 */
2898 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2899 ((def->dflags & IS_COMPILABLE) == 0)) {
2900 xmlRelaxNGDefinePtr list;
2901
2902 list = def->content;
2903 while (list != NULL) {
2904 ret = xmlRelaxNGIsCompileable(list);
2905 if (ret != 1)
2906 break;
2907 list = list->next;
2908 }
William M. Brack60929622004-03-27 17:54:18 +00002909 /*
2910 * Because the routine is recursive, we must guard against
2911 * discovering both COMPILABLE and NOT_COMPILABLE
2912 */
2913 if (ret == 0) {
2914 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002915 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002916 }
2917 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002918 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002919#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002920 if (ret == 1) {
2921 xmlGenericError(xmlGenericErrorContext,
2922 "element content for %s is compilable\n",
2923 def->name);
2924 } else if (ret == 0) {
2925 xmlGenericError(xmlGenericErrorContext,
2926 "element content for %s is not compilable\n",
2927 def->name);
2928 } else {
2929 xmlGenericError(xmlGenericErrorContext,
2930 "Problem in RelaxNGIsCompileable for element %s\n",
2931 def->name);
2932 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002933#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002934 }
2935 /*
2936 * All elements return a compileable status unless they
2937 * are generic like anyName
2938 */
2939 if ((def->nameClass != NULL) || (def->name == NULL))
2940 ret = 0;
2941 else
2942 ret = 1;
2943 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002944 case XML_RELAXNG_REF:
2945 case XML_RELAXNG_EXTERNALREF:
2946 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002947 if (def->depth == -20) {
2948 return (1);
2949 } else {
2950 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002951
Daniel Veillard4c004142003-10-07 11:33:24 +00002952 def->depth = -20;
2953 list = def->content;
2954 while (list != NULL) {
2955 ret = xmlRelaxNGIsCompileable(list);
2956 if (ret != 1)
2957 break;
2958 list = list->next;
2959 }
2960 }
2961 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002962 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002963 case XML_RELAXNG_OPTIONAL:
2964 case XML_RELAXNG_ZEROORMORE:
2965 case XML_RELAXNG_ONEORMORE:
2966 case XML_RELAXNG_CHOICE:
2967 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002968 case XML_RELAXNG_DEF:{
2969 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002970
Daniel Veillard4c004142003-10-07 11:33:24 +00002971 list = def->content;
2972 while (list != NULL) {
2973 ret = xmlRelaxNGIsCompileable(list);
2974 if (ret != 1)
2975 break;
2976 list = list->next;
2977 }
2978 break;
2979 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002980 case XML_RELAXNG_EXCEPT:
2981 case XML_RELAXNG_ATTRIBUTE:
2982 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002983 case XML_RELAXNG_DATATYPE:
2984 case XML_RELAXNG_LIST:
2985 case XML_RELAXNG_PARAM:
2986 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002987 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002988 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002989 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002990 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002991 if (ret == 0)
2992 def->dflags |= IS_NOT_COMPILABLE;
2993 if (ret == 1)
2994 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002995#ifdef DEBUG_COMPILE
2996 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002997 xmlGenericError(xmlGenericErrorContext,
2998 "RelaxNGIsCompileable %s : true\n",
2999 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003000 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003001 xmlGenericError(xmlGenericErrorContext,
3002 "RelaxNGIsCompileable %s : false\n",
3003 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003004 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003005 xmlGenericError(xmlGenericErrorContext,
3006 "Problem in RelaxNGIsCompileable %s\n",
3007 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003008 }
3009#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003010 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003011}
3012
3013/**
3014 * xmlRelaxNGCompile:
3015 * ctxt: the RelaxNG parser context
3016 * @define: the definition tree to compile
3017 *
3018 * Compile the set of definitions, it works recursively, till the
3019 * element boundaries, where it tries to compile the content if possible
3020 *
3021 * Returns 0 if success and -1 in case of error
3022 */
3023static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003024xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3025{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003026 int ret = 0;
3027 xmlRelaxNGDefinePtr list;
3028
Daniel Veillard4c004142003-10-07 11:33:24 +00003029 if ((ctxt == NULL) || (def == NULL))
3030 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003031
Daniel Veillard4c004142003-10-07 11:33:24 +00003032 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003033 case XML_RELAXNG_START:
3034 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003035 xmlAutomataPtr oldam = ctxt->am;
3036 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003037
3038 def->depth = -25;
3039
Daniel Veillard4c004142003-10-07 11:33:24 +00003040 list = def->content;
3041 ctxt->am = xmlNewAutomata();
3042 if (ctxt->am == NULL)
3043 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003044
3045 /*
3046 * assume identical strings but not same pointer are different
3047 * atoms, needed for non-determinism detection
3048 * That way if 2 elements with the same name are in a choice
3049 * branch the automata is found non-deterministic and
3050 * we fallback to the normal validation which does the right
3051 * thing of exploring both choices.
3052 */
3053 xmlAutomataSetFlags(ctxt->am, 1);
3054
Daniel Veillard4c004142003-10-07 11:33:24 +00003055 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3056 while (list != NULL) {
3057 xmlRelaxNGCompile(ctxt, list);
3058 list = list->next;
3059 }
3060 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3061 def->contModel = xmlAutomataCompile(ctxt->am);
3062 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003063
Daniel Veillard4c004142003-10-07 11:33:24 +00003064 xmlFreeAutomata(ctxt->am);
3065 ctxt->state = oldstate;
3066 ctxt->am = oldam;
3067 }
3068 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003069 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003070 if ((ctxt->am != NULL) && (def->name != NULL)) {
3071 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3072 ctxt->state, NULL,
3073 def->name, def->ns,
3074 def);
3075 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003076 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003077 xmlAutomataPtr oldam = ctxt->am;
3078 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003079
3080 def->depth = -25;
3081
Daniel Veillard4c004142003-10-07 11:33:24 +00003082 list = def->content;
3083 ctxt->am = xmlNewAutomata();
3084 if (ctxt->am == NULL)
3085 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003086 xmlAutomataSetFlags(ctxt->am, 1);
Daniel Veillard4c004142003-10-07 11:33:24 +00003087 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3088 while (list != NULL) {
3089 xmlRelaxNGCompile(ctxt, list);
3090 list = list->next;
3091 }
3092 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3093 def->contModel = xmlAutomataCompile(ctxt->am);
3094 if (!xmlRegexpIsDeterminist(def->contModel)) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003095#ifdef DEBUG_COMPILE
3096 xmlGenericError(xmlGenericErrorContext,
3097 "Content model not determinist %s\n",
3098 def->name);
3099#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003100 /*
3101 * we can only use the automata if it is determinist
3102 */
3103 xmlRegFreeRegexp(def->contModel);
3104 def->contModel = NULL;
3105 }
3106 xmlFreeAutomata(ctxt->am);
3107 ctxt->state = oldstate;
3108 ctxt->am = oldam;
3109 } else {
3110 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003111
Daniel Veillard4c004142003-10-07 11:33:24 +00003112 /*
3113 * we can't build the content model for this element content
3114 * but it still might be possible to build it for some of its
3115 * children, recurse.
3116 */
3117 ret = xmlRelaxNGTryCompile(ctxt, def);
3118 ctxt->am = oldam;
3119 }
3120 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003121 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003122 ret = xmlRelaxNGCompile(ctxt, def->content);
3123 break;
3124 case XML_RELAXNG_OPTIONAL:{
3125 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003126
Daniel Veillardfd780772009-08-26 18:35:29 +02003127 list = def->content;
3128 while (list != NULL) {
3129 xmlRelaxNGCompile(ctxt, list);
3130 list = list->next;
3131 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003132 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3133 break;
3134 }
3135 case XML_RELAXNG_ZEROORMORE:{
3136 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003137
Daniel Veillard4c004142003-10-07 11:33:24 +00003138 ctxt->state =
3139 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3140 oldstate = ctxt->state;
3141 list = def->content;
3142 while (list != NULL) {
3143 xmlRelaxNGCompile(ctxt, list);
3144 list = list->next;
3145 }
3146 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3147 ctxt->state =
3148 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3149 break;
3150 }
3151 case XML_RELAXNG_ONEORMORE:{
3152 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003153
Daniel Veillard4c004142003-10-07 11:33:24 +00003154 list = def->content;
3155 while (list != NULL) {
3156 xmlRelaxNGCompile(ctxt, list);
3157 list = list->next;
3158 }
3159 oldstate = ctxt->state;
3160 list = def->content;
3161 while (list != NULL) {
3162 xmlRelaxNGCompile(ctxt, list);
3163 list = list->next;
3164 }
3165 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3166 ctxt->state =
3167 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3168 break;
3169 }
3170 case XML_RELAXNG_CHOICE:{
3171 xmlAutomataStatePtr target = NULL;
3172 xmlAutomataStatePtr oldstate = ctxt->state;
3173
3174 list = def->content;
3175 while (list != NULL) {
3176 ctxt->state = oldstate;
3177 ret = xmlRelaxNGCompile(ctxt, list);
3178 if (ret != 0)
3179 break;
3180 if (target == NULL)
3181 target = ctxt->state;
3182 else {
3183 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3184 target);
3185 }
3186 list = list->next;
3187 }
3188 ctxt->state = target;
3189
3190 break;
3191 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003192 case XML_RELAXNG_REF:
3193 case XML_RELAXNG_EXTERNALREF:
3194 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003195 case XML_RELAXNG_GROUP:
3196 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003197 list = def->content;
3198 while (list != NULL) {
3199 ret = xmlRelaxNGCompile(ctxt, list);
3200 if (ret != 0)
3201 break;
3202 list = list->next;
3203 }
3204 break;
3205 case XML_RELAXNG_TEXT:{
3206 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003207
Daniel Veillard4c004142003-10-07 11:33:24 +00003208 ctxt->state =
3209 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3210 oldstate = ctxt->state;
3211 xmlRelaxNGCompile(ctxt, def->content);
3212 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3213 ctxt->state, BAD_CAST "#text",
3214 NULL);
3215 ctxt->state =
3216 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3217 break;
3218 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003219 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003220 ctxt->state =
3221 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3222 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003223 case XML_RELAXNG_EXCEPT:
3224 case XML_RELAXNG_ATTRIBUTE:
3225 case XML_RELAXNG_INTERLEAVE:
3226 case XML_RELAXNG_NOT_ALLOWED:
3227 case XML_RELAXNG_DATATYPE:
3228 case XML_RELAXNG_LIST:
3229 case XML_RELAXNG_PARAM:
3230 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003231 /* This should not happen and generate an internal error */
3232 fprintf(stderr, "RNG internal error trying to compile %s\n",
3233 xmlRelaxNGDefName(def));
3234 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003235 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003236 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003237}
3238
3239/**
3240 * xmlRelaxNGTryCompile:
3241 * ctxt: the RelaxNG parser context
3242 * @define: the definition tree to compile
3243 *
3244 * Try to compile the set of definitions, it works recursively,
3245 * possibly ignoring parts which cannot be compiled.
3246 *
3247 * Returns 0 if success and -1 in case of error
3248 */
3249static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003250xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3251{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003252 int ret = 0;
3253 xmlRelaxNGDefinePtr list;
3254
Daniel Veillard4c004142003-10-07 11:33:24 +00003255 if ((ctxt == NULL) || (def == NULL))
3256 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003257
3258 if ((def->type == XML_RELAXNG_START) ||
3259 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003260 ret = xmlRelaxNGIsCompileable(def);
3261 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3262 ctxt->am = NULL;
3263 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003264#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003265 if (ret == 0) {
3266 if (def->type == XML_RELAXNG_START)
3267 xmlGenericError(xmlGenericErrorContext,
3268 "compiled the start\n");
3269 else
3270 xmlGenericError(xmlGenericErrorContext,
3271 "compiled element %s\n", def->name);
3272 } else {
3273 if (def->type == XML_RELAXNG_START)
3274 xmlGenericError(xmlGenericErrorContext,
3275 "failed to compile the start\n");
3276 else
3277 xmlGenericError(xmlGenericErrorContext,
3278 "failed to compile element %s\n",
3279 def->name);
3280 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003281#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003282 return (ret);
3283 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003284 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003285 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003286 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003287 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3288 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003289 case XML_RELAXNG_TEXT:
3290 case XML_RELAXNG_DATATYPE:
3291 case XML_RELAXNG_LIST:
3292 case XML_RELAXNG_PARAM:
3293 case XML_RELAXNG_VALUE:
3294 case XML_RELAXNG_EMPTY:
3295 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003296 ret = 0;
3297 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003298 case XML_RELAXNG_OPTIONAL:
3299 case XML_RELAXNG_ZEROORMORE:
3300 case XML_RELAXNG_ONEORMORE:
3301 case XML_RELAXNG_CHOICE:
3302 case XML_RELAXNG_GROUP:
3303 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003304 case XML_RELAXNG_START:
3305 case XML_RELAXNG_REF:
3306 case XML_RELAXNG_EXTERNALREF:
3307 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003308 list = def->content;
3309 while (list != NULL) {
3310 ret = xmlRelaxNGTryCompile(ctxt, list);
3311 if (ret != 0)
3312 break;
3313 list = list->next;
3314 }
3315 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003316 case XML_RELAXNG_EXCEPT:
3317 case XML_RELAXNG_ATTRIBUTE:
3318 case XML_RELAXNG_INTERLEAVE:
3319 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003320 ret = 0;
3321 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003322 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003323 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003324}
3325
3326/************************************************************************
3327 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003328 * Parsing functions *
3329 * *
3330 ************************************************************************/
3331
Daniel Veillard4c004142003-10-07 11:33:24 +00003332static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3333 ctxt, xmlNodePtr node);
3334static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3335 ctxt, xmlNodePtr node);
3336static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3337 ctxt, xmlNodePtr nodes,
3338 int group);
3339static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3340 ctxt, xmlNodePtr node);
3341static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3342 xmlNodePtr node);
3343static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3344 xmlNodePtr nodes);
3345static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3346 ctxt, xmlNodePtr node,
3347 xmlRelaxNGDefinePtr
3348 def);
3349static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3350 ctxt, xmlNodePtr nodes);
3351static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3352 xmlRelaxNGDefinePtr define,
3353 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003354
3355
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003356#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003357
3358/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003359 * xmlRelaxNGIsNullable:
3360 * @define: the definition to verify
3361 *
3362 * Check if a definition is nullable.
3363 *
3364 * Returns 1 if yes, 0 if no and -1 in case of error
3365 */
3366static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003367xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3368{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003369 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003370
Daniel Veillardfd573f12003-03-16 17:52:32 +00003371 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003372 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003373
Daniel Veillarde063f482003-03-21 16:53:17 +00003374 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003375 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003376 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003377 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003378 switch (define->type) {
3379 case XML_RELAXNG_EMPTY:
3380 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003381 ret = 1;
3382 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003383 case XML_RELAXNG_NOOP:
3384 case XML_RELAXNG_DEF:
3385 case XML_RELAXNG_REF:
3386 case XML_RELAXNG_EXTERNALREF:
3387 case XML_RELAXNG_PARENTREF:
3388 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003389 ret = xmlRelaxNGIsNullable(define->content);
3390 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003391 case XML_RELAXNG_EXCEPT:
3392 case XML_RELAXNG_NOT_ALLOWED:
3393 case XML_RELAXNG_ELEMENT:
3394 case XML_RELAXNG_DATATYPE:
3395 case XML_RELAXNG_PARAM:
3396 case XML_RELAXNG_VALUE:
3397 case XML_RELAXNG_LIST:
3398 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003399 ret = 0;
3400 break;
3401 case XML_RELAXNG_CHOICE:{
3402 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003403
Daniel Veillard4c004142003-10-07 11:33:24 +00003404 while (list != NULL) {
3405 ret = xmlRelaxNGIsNullable(list);
3406 if (ret != 0)
3407 goto done;
3408 list = list->next;
3409 }
3410 ret = 0;
3411 break;
3412 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003413 case XML_RELAXNG_START:
3414 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003415 case XML_RELAXNG_GROUP:{
3416 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003417
Daniel Veillard4c004142003-10-07 11:33:24 +00003418 while (list != NULL) {
3419 ret = xmlRelaxNGIsNullable(list);
3420 if (ret != 1)
3421 goto done;
3422 list = list->next;
3423 }
3424 return (1);
3425 }
3426 default:
3427 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003428 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003429 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003430 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003431 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003432 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003433 define->dflags |= IS_NULLABLE;
3434 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003435}
3436
3437/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003438 * xmlRelaxNGIsBlank:
3439 * @str: a string
3440 *
3441 * Check if a string is ignorable c.f. 4.2. Whitespace
3442 *
3443 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3444 */
3445static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003446xmlRelaxNGIsBlank(xmlChar * str)
3447{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003448 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003449 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003450 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003451 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003452 return (0);
3453 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003454 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003455 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003456}
3457
Daniel Veillard6eadf632003-01-23 18:29:16 +00003458/**
3459 * xmlRelaxNGGetDataTypeLibrary:
3460 * @ctxt: a Relax-NG parser context
3461 * @node: the current data or value element
3462 *
3463 * Applies algorithm from 4.3. datatypeLibrary attribute
3464 *
3465 * Returns the datatypeLibary value or NULL if not found
3466 */
3467static xmlChar *
3468xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003469 xmlNodePtr node)
3470{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003471 xmlChar *ret, *escape;
3472
Daniel Veillardd44b9362009-09-07 12:15:08 +02003473 if (node == NULL)
3474 return(NULL);
3475
Daniel Veillard6eadf632003-01-23 18:29:16 +00003476 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003477 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3478 if (ret != NULL) {
3479 if (ret[0] == 0) {
3480 xmlFree(ret);
3481 return (NULL);
3482 }
3483 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3484 if (escape == NULL) {
3485 return (ret);
3486 }
3487 xmlFree(ret);
3488 return (escape);
3489 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003490 }
3491 node = node->parent;
3492 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003493 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3494 if (ret != NULL) {
3495 if (ret[0] == 0) {
3496 xmlFree(ret);
3497 return (NULL);
3498 }
3499 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3500 if (escape == NULL) {
3501 return (ret);
3502 }
3503 xmlFree(ret);
3504 return (escape);
3505 }
3506 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003507 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003508 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003509}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003510
3511/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003512 * xmlRelaxNGParseValue:
3513 * @ctxt: a Relax-NG parser context
3514 * @node: the data node.
3515 *
3516 * parse the content of a RelaxNG value node.
3517 *
3518 * Returns the definition pointer or NULL in case of error
3519 */
3520static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003521xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3522{
Daniel Veillardedc91922003-01-26 00:52:04 +00003523 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003524 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003525 xmlChar *type;
3526 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003527 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003528
Daniel Veillardfd573f12003-03-16 17:52:32 +00003529 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003530 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003531 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003532 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003533
3534 type = xmlGetProp(node, BAD_CAST "type");
3535 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003536 xmlRelaxNGNormExtSpace(type);
3537 if (xmlValidateNCName(type, 0)) {
3538 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3539 "value type '%s' is not an NCName\n", type, NULL);
3540 }
3541 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3542 if (library == NULL)
3543 library =
3544 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003545
Daniel Veillard4c004142003-10-07 11:33:24 +00003546 def->name = type;
3547 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003548
Daniel Veillard4c004142003-10-07 11:33:24 +00003549 lib = (xmlRelaxNGTypeLibraryPtr)
3550 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3551 if (lib == NULL) {
3552 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3553 "Use of unregistered type library '%s'\n", library,
3554 NULL);
3555 def->data = NULL;
3556 } else {
3557 def->data = lib;
3558 if (lib->have == NULL) {
3559 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3560 "Internal error with type library '%s': no 'have'\n",
3561 library, NULL);
3562 } else {
3563 success = lib->have(lib->data, def->name);
3564 if (success != 1) {
3565 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3566 "Error type '%s' is not exported by type library '%s'\n",
3567 def->name, library);
3568 }
3569 }
3570 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003571 }
3572 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003573 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003574 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003575 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3576 (node->children->next != NULL)) {
3577 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3578 "Expecting a single text value for <value>content\n",
3579 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003580 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003581 def->value = xmlNodeGetContent(node);
3582 if (def->value == NULL) {
3583 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3584 "Element <value> has no content\n", NULL, NULL);
3585 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3586 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003587
Daniel Veillard4c004142003-10-07 11:33:24 +00003588 success =
3589 lib->check(lib->data, def->name, def->value, &val, node);
3590 if (success != 1) {
3591 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3592 "Value '%s' is not acceptable for type '%s'\n",
3593 def->value, def->name);
3594 } else {
3595 if (val != NULL)
3596 def->attrs = val;
3597 }
3598 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003599 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003600 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003601}
3602
3603/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003604 * xmlRelaxNGParseData:
3605 * @ctxt: a Relax-NG parser context
3606 * @node: the data node.
3607 *
3608 * parse the content of a RelaxNG data node.
3609 *
3610 * Returns the definition pointer or NULL in case of error
3611 */
3612static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003613xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3614{
Daniel Veillard14b56432006-03-09 18:41:40 +00003615 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003616 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003617 xmlRelaxNGTypeLibraryPtr lib;
3618 xmlChar *type;
3619 xmlChar *library;
3620 xmlNodePtr content;
3621 int tmp;
3622
3623 type = xmlGetProp(node, BAD_CAST "type");
3624 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003625 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3626 NULL);
3627 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003628 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003629 xmlRelaxNGNormExtSpace(type);
3630 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003631 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3632 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003633 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003634 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3635 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003636 library =
3637 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003638
Daniel Veillardfd573f12003-03-16 17:52:32 +00003639 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003640 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003641 xmlFree(type);
3642 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003643 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003644 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003645 def->name = type;
3646 def->ns = library;
3647
3648 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003649 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003650 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003651 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3652 "Use of unregistered type library '%s'\n", library,
3653 NULL);
3654 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003655 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003656 def->data = lib;
3657 if (lib->have == NULL) {
3658 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3659 "Internal error with type library '%s': no 'have'\n",
3660 library, NULL);
3661 } else {
3662 tmp = lib->have(lib->data, def->name);
3663 if (tmp != 1) {
3664 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3665 "Error type '%s' is not exported by type library '%s'\n",
3666 def->name, library);
3667 } else
3668 if ((xmlStrEqual
3669 (library,
3670 BAD_CAST
3671 "http://www.w3.org/2001/XMLSchema-datatypes"))
3672 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3673 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3674 ctxt->idref = 1;
3675 }
3676 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003677 }
3678 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003679
3680 /*
3681 * Handle optional params
3682 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003683 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003684 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3685 break;
3686 if (xmlStrEqual(library,
3687 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3688 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3689 "Type library '%s' does not allow type parameters\n",
3690 library, NULL);
3691 content = content->next;
3692 while ((content != NULL) &&
3693 (xmlStrEqual(content->name, BAD_CAST "param")))
3694 content = content->next;
3695 } else {
3696 param = xmlRelaxNGNewDefine(ctxt, node);
3697 if (param != NULL) {
3698 param->type = XML_RELAXNG_PARAM;
3699 param->name = xmlGetProp(content, BAD_CAST "name");
3700 if (param->name == NULL) {
3701 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3702 "param has no name\n", NULL, NULL);
3703 }
3704 param->value = xmlNodeGetContent(content);
3705 if (lastparam == NULL) {
3706 def->attrs = lastparam = param;
3707 } else {
3708 lastparam->next = param;
3709 lastparam = param;
3710 }
3711 if (lib != NULL) {
3712 }
3713 }
3714 content = content->next;
3715 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003716 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003717 /*
3718 * Handle optional except
3719 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003720 if ((content != NULL)
3721 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3722 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003723 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003724
Daniel Veillard4c004142003-10-07 11:33:24 +00003725 except = xmlRelaxNGNewDefine(ctxt, node);
3726 if (except == NULL) {
3727 return (def);
3728 }
3729 except->type = XML_RELAXNG_EXCEPT;
3730 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003731 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003732 if (child == NULL) {
3733 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3734 "except has no content\n", NULL, NULL);
3735 }
3736 while (child != NULL) {
3737 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3738 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003739 if (last == NULL) {
3740 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003741 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003742 last->next = tmp2;
3743 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003744 }
3745 }
3746 child = child->next;
3747 }
3748 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003749 }
3750 /*
3751 * Check there is no unhandled data
3752 */
3753 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003754 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3755 "Element data has unexpected content %s\n",
3756 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003757 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003758
Daniel Veillard4c004142003-10-07 11:33:24 +00003759 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003760}
3761
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003762static const xmlChar *invalidName = BAD_CAST "\1";
3763
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003764/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003765 * xmlRelaxNGCompareNameClasses:
3766 * @defs1: the first element/attribute defs
3767 * @defs2: the second element/attribute defs
3768 * @name: the restriction on the name
3769 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003770 *
3771 * Compare the 2 lists of element definitions. The comparison is
3772 * that if both lists do not accept the same QNames, it returns 1
3773 * If the 2 lists can accept the same QName the comparison returns 0
3774 *
3775 * Returns 1 disttinct, 0 if equal
3776 */
3777static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003778xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003779 xmlRelaxNGDefinePtr def2)
3780{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003781 int ret = 1;
3782 xmlNode node;
3783 xmlNs ns;
3784 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003785
Daniel Veillard42f12e92003-03-07 18:32:59 +00003786 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3787
Daniel Veillardb30ca312005-09-04 13:50:03 +00003788 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3789
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003790 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003791 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3792 if (def2->type == XML_RELAXNG_TEXT)
3793 return (1);
3794 if (def1->name != NULL) {
3795 node.name = def1->name;
3796 } else {
3797 node.name = invalidName;
3798 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003799 if (def1->ns != NULL) {
3800 if (def1->ns[0] == 0) {
3801 node.ns = NULL;
3802 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003803 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003804 ns.href = def1->ns;
3805 }
3806 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003807 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003808 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003809 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003810 if (def1->nameClass != NULL) {
3811 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3812 } else {
3813 ret = 0;
3814 }
3815 } else {
3816 ret = 1;
3817 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003818 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003819 if (def2->type == XML_RELAXNG_TEXT)
3820 return (0);
3821 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003822 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003823 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003824 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003825 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003826 }
3827 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003828 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003829 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003830 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3831 if (def2->name != NULL) {
3832 node.name = def2->name;
3833 } else {
3834 node.name = invalidName;
3835 }
3836 node.ns = &ns;
3837 if (def2->ns != NULL) {
3838 if (def2->ns[0] == 0) {
3839 node.ns = NULL;
3840 } else {
3841 ns.href = def2->ns;
3842 }
3843 } else {
3844 ns.href = invalidName;
3845 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003846 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003847 if (def2->nameClass != NULL) {
3848 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3849 } else {
3850 ret = 0;
3851 }
3852 } else {
3853 ret = 1;
3854 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003855 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003856 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003857 }
3858
Daniel Veillard4c004142003-10-07 11:33:24 +00003859 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003860}
3861
3862/**
3863 * xmlRelaxNGCompareElemDefLists:
3864 * @ctxt: a Relax-NG parser context
3865 * @defs1: the first list of element/attribute defs
3866 * @defs2: the second list of element/attribute defs
3867 *
3868 * Compare the 2 lists of element or attribute definitions. The comparison
3869 * is that if both lists do not accept the same QNames, it returns 1
3870 * If the 2 lists can accept the same QName the comparison returns 0
3871 *
3872 * Returns 1 disttinct, 0 if equal
3873 */
3874static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003875xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3876 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3877 xmlRelaxNGDefinePtr * def2)
3878{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003879 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003880
Daniel Veillard154877e2003-01-30 12:17:05 +00003881 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003882 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003883 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003884 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003885 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003886 while ((*def2) != NULL) {
3887 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3888 return (0);
3889 def2++;
3890 }
3891 def2 = basedef2;
3892 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003893 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003894 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003895}
3896
3897/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003898 * xmlRelaxNGGenerateAttributes:
3899 * @ctxt: a Relax-NG parser context
3900 * @def: the definition definition
3901 *
3902 * Check if the definition can only generate attributes
3903 *
3904 * Returns 1 if yes, 0 if no and -1 in case of error.
3905 */
3906static int
3907xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003908 xmlRelaxNGDefinePtr def)
3909{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003910 xmlRelaxNGDefinePtr parent, cur, tmp;
3911
3912 /*
3913 * Don't run that check in case of error. Infinite recursion
3914 * becomes possible.
3915 */
3916 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003917 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003918
3919 parent = NULL;
3920 cur = def;
3921 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003922 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3923 (cur->type == XML_RELAXNG_TEXT) ||
3924 (cur->type == XML_RELAXNG_DATATYPE) ||
3925 (cur->type == XML_RELAXNG_PARAM) ||
3926 (cur->type == XML_RELAXNG_LIST) ||
3927 (cur->type == XML_RELAXNG_VALUE) ||
3928 (cur->type == XML_RELAXNG_EMPTY))
3929 return (0);
3930 if ((cur->type == XML_RELAXNG_CHOICE) ||
3931 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3932 (cur->type == XML_RELAXNG_GROUP) ||
3933 (cur->type == XML_RELAXNG_ONEORMORE) ||
3934 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3935 (cur->type == XML_RELAXNG_OPTIONAL) ||
3936 (cur->type == XML_RELAXNG_PARENTREF) ||
3937 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3938 (cur->type == XML_RELAXNG_REF) ||
3939 (cur->type == XML_RELAXNG_DEF)) {
3940 if (cur->content != NULL) {
3941 parent = cur;
3942 cur = cur->content;
3943 tmp = cur;
3944 while (tmp != NULL) {
3945 tmp->parent = parent;
3946 tmp = tmp->next;
3947 }
3948 continue;
3949 }
3950 }
3951 if (cur == def)
3952 break;
3953 if (cur->next != NULL) {
3954 cur = cur->next;
3955 continue;
3956 }
3957 do {
3958 cur = cur->parent;
3959 if (cur == NULL)
3960 break;
3961 if (cur == def)
3962 return (1);
3963 if (cur->next != NULL) {
3964 cur = cur->next;
3965 break;
3966 }
3967 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003968 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003969 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003970}
Daniel Veillard4c004142003-10-07 11:33:24 +00003971
Daniel Veillardce192eb2003-04-16 15:58:05 +00003972/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003973 * xmlRelaxNGGetElements:
3974 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003975 * @def: the definition definition
3976 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003977 *
3978 * Compute the list of top elements a definition can generate
3979 *
3980 * Returns a list of elements or NULL if none was found.
3981 */
3982static xmlRelaxNGDefinePtr *
3983xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003984 xmlRelaxNGDefinePtr def, int eora)
3985{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003986 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003987 int len = 0;
3988 int max = 0;
3989
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003990 /*
3991 * Don't run that check in case of error. Infinite recursion
3992 * becomes possible.
3993 */
3994 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003995 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003996
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003997 parent = NULL;
3998 cur = def;
3999 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004000 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4001 (cur->type == XML_RELAXNG_TEXT))) ||
4002 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
4003 if (ret == NULL) {
4004 max = 10;
4005 ret = (xmlRelaxNGDefinePtr *)
4006 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4007 if (ret == NULL) {
4008 xmlRngPErrMemory(ctxt, "getting element list\n");
4009 return (NULL);
4010 }
4011 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00004012 xmlRelaxNGDefinePtr *temp;
4013
Daniel Veillard4c004142003-10-07 11:33:24 +00004014 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00004015 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00004016 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00004017 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004018 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00004019 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00004020 return (NULL);
4021 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00004022 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00004023 }
4024 ret[len++] = cur;
4025 ret[len] = NULL;
4026 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4027 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4028 (cur->type == XML_RELAXNG_GROUP) ||
4029 (cur->type == XML_RELAXNG_ONEORMORE) ||
4030 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4031 (cur->type == XML_RELAXNG_OPTIONAL) ||
4032 (cur->type == XML_RELAXNG_PARENTREF) ||
4033 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004034 (cur->type == XML_RELAXNG_DEF) ||
4035 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004036 /*
4037 * Don't go within elements or attributes or string values.
4038 * Just gather the element top list
4039 */
4040 if (cur->content != NULL) {
4041 parent = cur;
4042 cur = cur->content;
4043 tmp = cur;
4044 while (tmp != NULL) {
4045 tmp->parent = parent;
4046 tmp = tmp->next;
4047 }
4048 continue;
4049 }
4050 }
4051 if (cur == def)
4052 break;
4053 if (cur->next != NULL) {
4054 cur = cur->next;
4055 continue;
4056 }
4057 do {
4058 cur = cur->parent;
4059 if (cur == NULL)
4060 break;
4061 if (cur == def)
4062 return (ret);
4063 if (cur->next != NULL) {
4064 cur = cur->next;
4065 break;
4066 }
4067 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004068 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004069 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004070}
Daniel Veillard4c004142003-10-07 11:33:24 +00004071
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004072/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004073 * xmlRelaxNGCheckChoiceDeterminism:
4074 * @ctxt: a Relax-NG parser context
4075 * @def: the choice definition
4076 *
4077 * Also used to find indeterministic pattern in choice
4078 */
4079static void
4080xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004081 xmlRelaxNGDefinePtr def)
4082{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004083 xmlRelaxNGDefinePtr **list;
4084 xmlRelaxNGDefinePtr cur;
4085 int nbchild = 0, i, j, ret;
4086 int is_nullable = 0;
4087 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004088 xmlHashTablePtr triage = NULL;
4089 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004090
Daniel Veillard4c004142003-10-07 11:33:24 +00004091 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4092 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004093
Daniel Veillarde063f482003-03-21 16:53:17 +00004094 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004095 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004096
Daniel Veillardfd573f12003-03-16 17:52:32 +00004097 /*
4098 * Don't run that check in case of error. Infinite recursion
4099 * becomes possible.
4100 */
4101 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004102 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004103
4104 is_nullable = xmlRelaxNGIsNullable(def);
4105
4106 cur = def->content;
4107 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004108 nbchild++;
4109 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004110 }
4111
4112 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004113 sizeof(xmlRelaxNGDefinePtr
4114 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004115 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004116 xmlRngPErrMemory(ctxt, "building choice\n");
4117 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004118 }
4119 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004120 /*
4121 * a bit strong but safe
4122 */
4123 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004124 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004125 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004126 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004127 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004128 cur = def->content;
4129 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004130 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4131 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4132 is_triable = 0;
4133 } else if (is_triable == 1) {
4134 xmlRelaxNGDefinePtr *tmp;
4135 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004136
Daniel Veillard4c004142003-10-07 11:33:24 +00004137 tmp = list[i];
4138 while ((*tmp != NULL) && (is_triable == 1)) {
4139 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4140 res = xmlHashAddEntry2(triage,
4141 BAD_CAST "#text", NULL,
4142 (void *) cur);
4143 if (res != 0)
4144 is_triable = -1;
4145 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4146 ((*tmp)->name != NULL)) {
4147 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4148 res = xmlHashAddEntry2(triage,
4149 (*tmp)->name, NULL,
4150 (void *) cur);
4151 else
4152 res = xmlHashAddEntry2(triage,
4153 (*tmp)->name, (*tmp)->ns,
4154 (void *) cur);
4155 if (res != 0)
4156 is_triable = -1;
4157 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4158 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4159 res = xmlHashAddEntry2(triage,
4160 BAD_CAST "#any", NULL,
4161 (void *) cur);
4162 else
4163 res = xmlHashAddEntry2(triage,
4164 BAD_CAST "#any", (*tmp)->ns,
4165 (void *) cur);
4166 if (res != 0)
4167 is_triable = -1;
4168 } else {
4169 is_triable = -1;
4170 }
4171 tmp++;
4172 }
4173 }
4174 i++;
4175 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004176 }
4177
Daniel Veillard4c004142003-10-07 11:33:24 +00004178 for (i = 0; i < nbchild; i++) {
4179 if (list[i] == NULL)
4180 continue;
4181 for (j = 0; j < i; j++) {
4182 if (list[j] == NULL)
4183 continue;
4184 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4185 if (ret == 0) {
4186 is_indeterminist = 1;
4187 }
4188 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004189 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004190 for (i = 0; i < nbchild; i++) {
4191 if (list[i] != NULL)
4192 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004193 }
4194
4195 xmlFree(list);
4196 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004197 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004198 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004199 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004200 def->dflags |= IS_TRIABLE;
4201 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004202 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004203 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004204 }
4205 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004206}
4207
4208/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004209 * xmlRelaxNGCheckGroupAttrs:
4210 * @ctxt: a Relax-NG parser context
4211 * @def: the group definition
4212 *
4213 * Detects violations of rule 7.3
4214 */
4215static void
4216xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004217 xmlRelaxNGDefinePtr def)
4218{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004219 xmlRelaxNGDefinePtr **list;
4220 xmlRelaxNGDefinePtr cur;
4221 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004222
4223 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004224 ((def->type != XML_RELAXNG_GROUP) &&
4225 (def->type != XML_RELAXNG_ELEMENT)))
4226 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004227
Daniel Veillarde063f482003-03-21 16:53:17 +00004228 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004229 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004230
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004231 /*
4232 * Don't run that check in case of error. Infinite recursion
4233 * becomes possible.
4234 */
4235 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004236 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004237
Daniel Veillardfd573f12003-03-16 17:52:32 +00004238 cur = def->attrs;
4239 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004240 nbchild++;
4241 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004242 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004243 cur = def->content;
4244 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004245 nbchild++;
4246 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004247 }
4248
4249 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004250 sizeof(xmlRelaxNGDefinePtr
4251 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004252 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004253 xmlRngPErrMemory(ctxt, "building group\n");
4254 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004255 }
4256 i = 0;
4257 cur = def->attrs;
4258 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004259 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4260 i++;
4261 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004262 }
4263 cur = def->content;
4264 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004265 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4266 i++;
4267 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004268 }
4269
Daniel Veillard4c004142003-10-07 11:33:24 +00004270 for (i = 0; i < nbchild; i++) {
4271 if (list[i] == NULL)
4272 continue;
4273 for (j = 0; j < i; j++) {
4274 if (list[j] == NULL)
4275 continue;
4276 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4277 if (ret == 0) {
4278 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4279 "Attributes conflicts in group\n", NULL, NULL);
4280 }
4281 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004282 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004283 for (i = 0; i < nbchild; i++) {
4284 if (list[i] != NULL)
4285 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004286 }
4287
4288 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004289 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004290}
4291
4292/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004293 * xmlRelaxNGComputeInterleaves:
4294 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004295 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004296 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004297 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004298 * A lot of work for preprocessing interleave definitions
4299 * is potentially needed to get a decent execution speed at runtime
4300 * - trying to get a total order on the element nodes generated
4301 * by the interleaves, order the list of interleave definitions
4302 * following that order.
4303 * - if <text/> is used to handle mixed content, it is better to
4304 * flag this in the define and simplify the runtime checking
4305 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004306 */
4307static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004308xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004309 xmlRelaxNGParserCtxtPtr ctxt,
4310 xmlChar * name ATTRIBUTE_UNUSED)
4311{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004312 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004313
Daniel Veillardfd573f12003-03-16 17:52:32 +00004314 xmlRelaxNGPartitionPtr partitions = NULL;
4315 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4316 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004317 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004318 int nbgroups = 0;
4319 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004320 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004321 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004322
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004323 /*
4324 * Don't run that check in case of error. Infinite recursion
4325 * becomes possible.
4326 */
4327 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004328 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004329
Daniel Veillardfd573f12003-03-16 17:52:32 +00004330#ifdef DEBUG_INTERLEAVE
4331 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004332 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004333#endif
4334 cur = def->content;
4335 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004336 nbchild++;
4337 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004338 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004339
Daniel Veillardfd573f12003-03-16 17:52:32 +00004340#ifdef DEBUG_INTERLEAVE
4341 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4342#endif
4343 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004344 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004345 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004346 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004347 cur = def->content;
4348 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004349 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4350 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4351 if (groups[nbgroups] == NULL)
4352 goto error;
4353 if (cur->type == XML_RELAXNG_TEXT)
4354 is_mixed++;
4355 groups[nbgroups]->rule = cur;
4356 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4357 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4358 nbgroups++;
4359 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004360 }
4361#ifdef DEBUG_INTERLEAVE
4362 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4363#endif
4364
4365 /*
4366 * Let's check that all rules makes a partitions according to 7.4
4367 */
4368 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004369 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004370 if (partitions == NULL)
4371 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004372 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004373 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004374 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004375 for (i = 0; i < nbgroups; i++) {
4376 group = groups[i];
4377 for (j = i + 1; j < nbgroups; j++) {
4378 if (groups[j] == NULL)
4379 continue;
4380
4381 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4382 groups[j]->defs);
4383 if (ret == 0) {
4384 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4385 "Element or text conflicts in interleave\n",
4386 NULL, NULL);
4387 }
4388 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4389 groups[j]->attrs);
4390 if (ret == 0) {
4391 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4392 "Attributes conflicts in interleave\n", NULL,
4393 NULL);
4394 }
4395 }
4396 tmp = group->defs;
4397 if ((tmp != NULL) && (*tmp != NULL)) {
4398 while (*tmp != NULL) {
4399 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4400 res = xmlHashAddEntry2(partitions->triage,
4401 BAD_CAST "#text", NULL,
4402 (void *) (long) (i + 1));
4403 if (res != 0)
4404 is_determinist = -1;
4405 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4406 ((*tmp)->name != NULL)) {
4407 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4408 res = xmlHashAddEntry2(partitions->triage,
4409 (*tmp)->name, NULL,
4410 (void *) (long) (i + 1));
4411 else
4412 res = xmlHashAddEntry2(partitions->triage,
4413 (*tmp)->name, (*tmp)->ns,
4414 (void *) (long) (i + 1));
4415 if (res != 0)
4416 is_determinist = -1;
4417 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4418 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4419 res = xmlHashAddEntry2(partitions->triage,
4420 BAD_CAST "#any", NULL,
4421 (void *) (long) (i + 1));
4422 else
4423 res = xmlHashAddEntry2(partitions->triage,
4424 BAD_CAST "#any", (*tmp)->ns,
4425 (void *) (long) (i + 1));
4426 if ((*tmp)->nameClass != NULL)
4427 is_determinist = 2;
4428 if (res != 0)
4429 is_determinist = -1;
4430 } else {
4431 is_determinist = -1;
4432 }
4433 tmp++;
4434 }
4435 } else {
4436 is_determinist = 0;
4437 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004438 }
4439 partitions->groups = groups;
4440
4441 /*
4442 * and save the partition list back in the def
4443 */
4444 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004445 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004446 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004447 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004448 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004449 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004450 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004451 return;
4452
Daniel Veillard4c004142003-10-07 11:33:24 +00004453 error:
4454 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004455 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004456 for (i = 0; i < nbgroups; i++)
4457 if (groups[i] != NULL) {
4458 if (groups[i]->defs != NULL)
4459 xmlFree(groups[i]->defs);
4460 xmlFree(groups[i]);
4461 }
4462 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004463 }
4464 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004465}
4466
4467/**
4468 * xmlRelaxNGParseInterleave:
4469 * @ctxt: a Relax-NG parser context
4470 * @node: the data node.
4471 *
4472 * parse the content of a RelaxNG interleave node.
4473 *
4474 * Returns the definition pointer or NULL in case of error
4475 */
4476static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004477xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4478{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004479 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004480 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004481 xmlNodePtr child;
4482
Daniel Veillardfd573f12003-03-16 17:52:32 +00004483 def = xmlRelaxNGNewDefine(ctxt, node);
4484 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004485 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004486 }
4487 def->type = XML_RELAXNG_INTERLEAVE;
4488
4489 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004490 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004491 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004492 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004493 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004494 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004495
Daniel Veillard4c004142003-10-07 11:33:24 +00004496 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4497 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4498 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4499 "Failed to add %s to hash table\n",
4500 (const xmlChar *) name, NULL);
4501 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004502 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004503 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004504 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004505 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4506 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004507 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004508 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004509 if (IS_RELAXNG(child, "element")) {
4510 cur = xmlRelaxNGParseElement(ctxt, child);
4511 } else {
4512 cur = xmlRelaxNGParsePattern(ctxt, child);
4513 }
4514 if (cur != NULL) {
4515 cur->parent = def;
4516 if (last == NULL) {
4517 def->content = last = cur;
4518 } else {
4519 last->next = cur;
4520 last = cur;
4521 }
4522 }
4523 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004524 }
4525
Daniel Veillard4c004142003-10-07 11:33:24 +00004526 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004527}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004528
4529/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004530 * xmlRelaxNGParseInclude:
4531 * @ctxt: a Relax-NG parser context
4532 * @node: the include node
4533 *
4534 * Integrate the content of an include node in the current grammar
4535 *
4536 * Returns 0 in case of success or -1 in case of error
4537 */
4538static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004539xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4540{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004541 xmlRelaxNGIncludePtr incl;
4542 xmlNodePtr root;
4543 int ret = 0, tmp;
4544
Daniel Veillard807daf82004-02-22 22:13:27 +00004545 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004546 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004547 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4548 "Include node has no data\n", NULL, NULL);
4549 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004550 }
4551 root = xmlDocGetRootElement(incl->doc);
4552 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004553 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4554 NULL, NULL);
4555 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004556 }
4557 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004558 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4559 "Include document root is not a grammar\n", NULL, NULL);
4560 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004561 }
4562
4563 /*
4564 * Merge the definition from both the include and the internal list
4565 */
4566 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004567 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4568 if (tmp != 0)
4569 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004570 }
4571 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004572 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4573 if (tmp != 0)
4574 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004575 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004576 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004577}
4578
4579/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004580 * xmlRelaxNGParseDefine:
4581 * @ctxt: a Relax-NG parser context
4582 * @node: the define node
4583 *
4584 * parse the content of a RelaxNG define element node.
4585 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004586 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004587 */
4588static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004589xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4590{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004591 xmlChar *name;
4592 int ret = 0, tmp;
4593 xmlRelaxNGDefinePtr def;
4594 const xmlChar *olddefine;
4595
4596 name = xmlGetProp(node, BAD_CAST "name");
4597 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004598 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4599 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004600 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004601 xmlRelaxNGNormExtSpace(name);
4602 if (xmlValidateNCName(name, 0)) {
4603 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4604 "define name '%s' is not an NCName\n", name, NULL);
4605 }
4606 def = xmlRelaxNGNewDefine(ctxt, node);
4607 if (def == NULL) {
4608 xmlFree(name);
4609 return (-1);
4610 }
4611 def->type = XML_RELAXNG_DEF;
4612 def->name = name;
4613 if (node->children == NULL) {
4614 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4615 "define has no children\n", NULL, NULL);
4616 } else {
4617 olddefine = ctxt->define;
4618 ctxt->define = name;
4619 def->content =
4620 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4621 ctxt->define = olddefine;
4622 }
4623 if (ctxt->grammar->defs == NULL)
4624 ctxt->grammar->defs = xmlHashCreate(10);
4625 if (ctxt->grammar->defs == NULL) {
4626 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4627 "Could not create definition hash\n", NULL, NULL);
4628 ret = -1;
4629 } else {
4630 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4631 if (tmp < 0) {
4632 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004633
Daniel Veillard4c004142003-10-07 11:33:24 +00004634 prev = xmlHashLookup(ctxt->grammar->defs, name);
4635 if (prev == NULL) {
4636 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4637 "Internal error on define aggregation of %s\n",
4638 name, NULL);
4639 ret = -1;
4640 } else {
4641 while (prev->nextHash != NULL)
4642 prev = prev->nextHash;
4643 prev->nextHash = def;
4644 }
4645 }
4646 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004647 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004648 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004649}
4650
4651/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004652 * xmlRelaxNGParseImportRef:
4653 * @payload: the parser context
4654 * @data: the current grammar
4655 * @name: the reference name
4656 *
4657 * Import import one references into the current grammar
4658 */
4659static void
4660xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4661 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4662 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4663 int tmp;
4664
4665 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4666 if (tmp < 0) {
4667 xmlRelaxNGDefinePtr prev;
4668
4669 prev = (xmlRelaxNGDefinePtr)
4670 xmlHashLookup(ctxt->grammar->refs, def->name);
4671 if (prev == NULL) {
4672 if (def->name != NULL) {
4673 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4674 "Error refs definitions '%s'\n",
4675 def->name, NULL);
4676 } else {
4677 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4678 "Error refs definitions\n",
4679 NULL, NULL);
4680 }
4681 } else {
4682 def->nextHash = prev->nextHash;
4683 prev->nextHash = def;
4684 }
4685 }
4686}
4687
4688/**
4689 * xmlRelaxNGParseImportRefs:
4690 * @ctxt: the parser context
4691 * @grammar: the sub grammar
4692 *
4693 * Import references from the subgrammar into the current grammar
4694 *
4695 * Returns 0 in case of success, -1 in case of failure
4696 */
4697static int
4698xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4699 xmlRelaxNGGrammarPtr grammar) {
4700 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4701 return(-1);
4702 if (grammar->refs == NULL)
4703 return(0);
4704 if (ctxt->grammar->refs == NULL)
4705 ctxt->grammar->refs = xmlHashCreate(10);
4706 if (ctxt->grammar->refs == NULL) {
4707 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4708 "Could not create references hash\n", NULL, NULL);
4709 return(-1);
4710 }
4711 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004712 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004713}
4714
4715/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004716 * xmlRelaxNGProcessExternalRef:
4717 * @ctxt: the parser context
4718 * @node: the externlRef node
4719 *
4720 * Process and compile an externlRef node
4721 *
4722 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4723 */
4724static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004725xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4726{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004727 xmlRelaxNGDocumentPtr docu;
4728 xmlNodePtr root, tmp;
4729 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004730 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004731 xmlRelaxNGDefinePtr def;
4732
Daniel Veillard807daf82004-02-22 22:13:27 +00004733 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004734 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004735 def = xmlRelaxNGNewDefine(ctxt, node);
4736 if (def == NULL)
4737 return (NULL);
4738 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004739
Daniel Veillard4c004142003-10-07 11:33:24 +00004740 if (docu->content == NULL) {
4741 /*
4742 * Then do the parsing for good
4743 */
4744 root = xmlDocGetRootElement(docu->doc);
4745 if (root == NULL) {
4746 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4747 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4748 NULL);
4749 return (NULL);
4750 }
4751 /*
4752 * ns transmission rules
4753 */
4754 ns = xmlGetProp(root, BAD_CAST "ns");
4755 if (ns == NULL) {
4756 tmp = node;
4757 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4758 ns = xmlGetProp(tmp, BAD_CAST "ns");
4759 if (ns != NULL) {
4760 break;
4761 }
4762 tmp = tmp->parent;
4763 }
4764 if (ns != NULL) {
4765 xmlSetProp(root, BAD_CAST "ns", ns);
4766 newNs = 1;
4767 xmlFree(ns);
4768 }
4769 } else {
4770 xmlFree(ns);
4771 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004772
Daniel Veillard4c004142003-10-07 11:33:24 +00004773 /*
4774 * Parsing to get a precompiled schemas.
4775 */
4776 oldflags = ctxt->flags;
4777 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4778 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4779 ctxt->flags = oldflags;
4780 if ((docu->schema != NULL) &&
4781 (docu->schema->topgrammar != NULL)) {
4782 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004783 if (docu->schema->topgrammar->refs)
4784 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004785 }
4786
4787 /*
4788 * the externalRef may be reused in a different ns context
4789 */
4790 if (newNs == 1) {
4791 xmlUnsetProp(root, BAD_CAST "ns");
4792 }
4793 }
4794 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004795 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004796 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004797 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004798 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004799}
4800
4801/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004802 * xmlRelaxNGParsePattern:
4803 * @ctxt: a Relax-NG parser context
4804 * @node: the pattern node.
4805 *
4806 * parse the content of a RelaxNG pattern node.
4807 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004808 * Returns the definition pointer or NULL in case of error or if no
4809 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004810 */
4811static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004812xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4813{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004814 xmlRelaxNGDefinePtr def = NULL;
4815
Daniel Veillardd2298792003-02-14 16:54:11 +00004816 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004817 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004818 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004819 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004820 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004821 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004822 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004823 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004824 def = xmlRelaxNGNewDefine(ctxt, node);
4825 if (def == NULL)
4826 return (NULL);
4827 def->type = XML_RELAXNG_EMPTY;
4828 if (node->children != NULL) {
4829 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4830 "empty: had a child node\n", NULL, NULL);
4831 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004832 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004833 def = xmlRelaxNGNewDefine(ctxt, node);
4834 if (def == NULL)
4835 return (NULL);
4836 def->type = XML_RELAXNG_TEXT;
4837 if (node->children != NULL) {
4838 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4839 "text: had a child node\n", NULL, NULL);
4840 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004841 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004842 def = xmlRelaxNGNewDefine(ctxt, node);
4843 if (def == NULL)
4844 return (NULL);
4845 def->type = XML_RELAXNG_ZEROORMORE;
4846 if (node->children == NULL) {
4847 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4848 "Element %s is empty\n", node->name, NULL);
4849 } else {
4850 def->content =
4851 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4852 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004853 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004854 def = xmlRelaxNGNewDefine(ctxt, node);
4855 if (def == NULL)
4856 return (NULL);
4857 def->type = XML_RELAXNG_ONEORMORE;
4858 if (node->children == NULL) {
4859 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4860 "Element %s is empty\n", node->name, NULL);
4861 } else {
4862 def->content =
4863 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4864 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004865 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004866 def = xmlRelaxNGNewDefine(ctxt, node);
4867 if (def == NULL)
4868 return (NULL);
4869 def->type = XML_RELAXNG_OPTIONAL;
4870 if (node->children == NULL) {
4871 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4872 "Element %s is empty\n", node->name, NULL);
4873 } else {
4874 def->content =
4875 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4876 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004877 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004878 def = xmlRelaxNGNewDefine(ctxt, node);
4879 if (def == NULL)
4880 return (NULL);
4881 def->type = XML_RELAXNG_CHOICE;
4882 if (node->children == NULL) {
4883 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4884 "Element %s is empty\n", node->name, NULL);
4885 } else {
4886 def->content =
4887 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4888 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004889 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004890 def = xmlRelaxNGNewDefine(ctxt, node);
4891 if (def == NULL)
4892 return (NULL);
4893 def->type = XML_RELAXNG_GROUP;
4894 if (node->children == NULL) {
4895 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4896 "Element %s is empty\n", node->name, NULL);
4897 } else {
4898 def->content =
4899 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4900 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004901 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004902 def = xmlRelaxNGNewDefine(ctxt, node);
4903 if (def == NULL)
4904 return (NULL);
4905 def->type = XML_RELAXNG_REF;
4906 def->name = xmlGetProp(node, BAD_CAST "name");
4907 if (def->name == NULL) {
4908 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4909 NULL, NULL);
4910 } else {
4911 xmlRelaxNGNormExtSpace(def->name);
4912 if (xmlValidateNCName(def->name, 0)) {
4913 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4914 "ref name '%s' is not an NCName\n", def->name,
4915 NULL);
4916 }
4917 }
4918 if (node->children != NULL) {
4919 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4920 NULL, NULL);
4921 }
4922 if (ctxt->grammar->refs == NULL)
4923 ctxt->grammar->refs = xmlHashCreate(10);
4924 if (ctxt->grammar->refs == NULL) {
4925 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4926 "Could not create references hash\n", NULL, NULL);
4927 def = NULL;
4928 } else {
4929 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004930
Daniel Veillard4c004142003-10-07 11:33:24 +00004931 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4932 if (tmp < 0) {
4933 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004934
Daniel Veillard4c004142003-10-07 11:33:24 +00004935 prev = (xmlRelaxNGDefinePtr)
4936 xmlHashLookup(ctxt->grammar->refs, def->name);
4937 if (prev == NULL) {
4938 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004939 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4940 "Error refs definitions '%s'\n",
4941 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004942 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004943 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4944 "Error refs definitions\n",
4945 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004946 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004947 def = NULL;
4948 } else {
4949 def->nextHash = prev->nextHash;
4950 prev->nextHash = def;
4951 }
4952 }
4953 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004954 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004955 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004956 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004957 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004958 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004959 def = xmlRelaxNGNewDefine(ctxt, node);
4960 if (def == NULL)
4961 return (NULL);
4962 def->type = XML_RELAXNG_LIST;
4963 if (node->children == NULL) {
4964 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4965 "Element %s is empty\n", node->name, NULL);
4966 } else {
4967 def->content =
4968 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4969 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004970 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004971 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004972 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004973 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004974 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004975 def = xmlRelaxNGNewDefine(ctxt, node);
4976 if (def == NULL)
4977 return (NULL);
4978 def->type = XML_RELAXNG_NOT_ALLOWED;
4979 if (node->children != NULL) {
4980 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4981 "xmlRelaxNGParse: notAllowed element is not empty\n",
4982 NULL, NULL);
4983 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004984 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004985 xmlRelaxNGGrammarPtr grammar, old;
4986 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004987
Daniel Veillardc482e262003-02-26 14:48:48 +00004988#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004989 xmlGenericError(xmlGenericErrorContext,
4990 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004991#endif
4992
Daniel Veillard4c004142003-10-07 11:33:24 +00004993 oldparent = ctxt->parentgrammar;
4994 old = ctxt->grammar;
4995 ctxt->parentgrammar = old;
4996 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4997 if (old != NULL) {
4998 ctxt->grammar = old;
4999 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00005000#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00005001 if (grammar != NULL) {
5002 grammar->next = old->next;
5003 old->next = grammar;
5004 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005005#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00005006 }
5007 if (grammar != NULL)
5008 def = grammar->start;
5009 else
5010 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00005011 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005012 if (ctxt->parentgrammar == NULL) {
5013 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5014 "Use of parentRef without a parent grammar\n", NULL,
5015 NULL);
5016 return (NULL);
5017 }
5018 def = xmlRelaxNGNewDefine(ctxt, node);
5019 if (def == NULL)
5020 return (NULL);
5021 def->type = XML_RELAXNG_PARENTREF;
5022 def->name = xmlGetProp(node, BAD_CAST "name");
5023 if (def->name == NULL) {
5024 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5025 "parentRef has no name\n", NULL, NULL);
5026 } else {
5027 xmlRelaxNGNormExtSpace(def->name);
5028 if (xmlValidateNCName(def->name, 0)) {
5029 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5030 "parentRef name '%s' is not an NCName\n",
5031 def->name, NULL);
5032 }
5033 }
5034 if (node->children != NULL) {
5035 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5036 "parentRef is not empty\n", NULL, NULL);
5037 }
5038 if (ctxt->parentgrammar->refs == NULL)
5039 ctxt->parentgrammar->refs = xmlHashCreate(10);
5040 if (ctxt->parentgrammar->refs == NULL) {
5041 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5042 "Could not create references hash\n", NULL, NULL);
5043 def = NULL;
5044 } else if (def->name != NULL) {
5045 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005046
Daniel Veillard4c004142003-10-07 11:33:24 +00005047 tmp =
5048 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5049 if (tmp < 0) {
5050 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005051
Daniel Veillard4c004142003-10-07 11:33:24 +00005052 prev = (xmlRelaxNGDefinePtr)
5053 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5054 if (prev == NULL) {
5055 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5056 "Internal error parentRef definitions '%s'\n",
5057 def->name, NULL);
5058 def = NULL;
5059 } else {
5060 def->nextHash = prev->nextHash;
5061 prev->nextHash = def;
5062 }
5063 }
5064 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005065 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005066 if (node->children == NULL) {
5067 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5068 NULL, NULL);
5069 def = NULL;
5070 } else {
5071 def = xmlRelaxNGParseInterleave(ctxt, node);
5072 if (def != NULL) {
5073 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005074
Daniel Veillard4c004142003-10-07 11:33:24 +00005075 if ((def->content != NULL) && (def->content->next != NULL)) {
5076 tmp = xmlRelaxNGNewDefine(ctxt, node);
5077 if (tmp != NULL) {
5078 tmp->type = XML_RELAXNG_GROUP;
5079 tmp->content = def->content;
5080 def->content = tmp;
5081 }
5082 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005083
Daniel Veillard4c004142003-10-07 11:33:24 +00005084 tmp = xmlRelaxNGNewDefine(ctxt, node);
5085 if (tmp == NULL)
5086 return (def);
5087 tmp->type = XML_RELAXNG_TEXT;
5088 tmp->next = def->content;
5089 def->content = tmp;
5090 }
5091 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005092 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005093 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5094 "Unexpected node %s is not a pattern\n", node->name,
5095 NULL);
5096 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005097 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005098 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005099}
5100
5101/**
5102 * xmlRelaxNGParseAttribute:
5103 * @ctxt: a Relax-NG parser context
5104 * @node: the element node
5105 *
5106 * parse the content of a RelaxNG attribute node.
5107 *
5108 * Returns the definition pointer or NULL in case of error.
5109 */
5110static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005111xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5112{
Daniel Veillardd2298792003-02-14 16:54:11 +00005113 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005114 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005115 int old_flags;
5116
Daniel Veillardfd573f12003-03-16 17:52:32 +00005117 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005118 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005119 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005120 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005121 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005122 child = node->children;
5123 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005124 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5125 "xmlRelaxNGParseattribute: attribute has no children\n",
5126 NULL, NULL);
5127 return (ret);
5128 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005129 old_flags = ctxt->flags;
5130 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005131 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5132 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005133 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005134
Daniel Veillardd2298792003-02-14 16:54:11 +00005135 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005136 cur = xmlRelaxNGParsePattern(ctxt, child);
5137 if (cur != NULL) {
5138 switch (cur->type) {
5139 case XML_RELAXNG_EMPTY:
5140 case XML_RELAXNG_NOT_ALLOWED:
5141 case XML_RELAXNG_TEXT:
5142 case XML_RELAXNG_ELEMENT:
5143 case XML_RELAXNG_DATATYPE:
5144 case XML_RELAXNG_VALUE:
5145 case XML_RELAXNG_LIST:
5146 case XML_RELAXNG_REF:
5147 case XML_RELAXNG_PARENTREF:
5148 case XML_RELAXNG_EXTERNALREF:
5149 case XML_RELAXNG_DEF:
5150 case XML_RELAXNG_ONEORMORE:
5151 case XML_RELAXNG_ZEROORMORE:
5152 case XML_RELAXNG_OPTIONAL:
5153 case XML_RELAXNG_CHOICE:
5154 case XML_RELAXNG_GROUP:
5155 case XML_RELAXNG_INTERLEAVE:
5156 case XML_RELAXNG_ATTRIBUTE:
5157 ret->content = cur;
5158 cur->parent = ret;
5159 break;
5160 case XML_RELAXNG_START:
5161 case XML_RELAXNG_PARAM:
5162 case XML_RELAXNG_EXCEPT:
5163 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5164 "attribute has invalid content\n", NULL,
5165 NULL);
5166 break;
5167 case XML_RELAXNG_NOOP:
5168 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5169 "RNG Internal error, noop found in attribute\n",
5170 NULL, NULL);
5171 break;
5172 }
5173 }
5174 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005175 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005176 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005177 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5178 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005179 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005180 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005181 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005182}
5183
5184/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005185 * xmlRelaxNGParseExceptNameClass:
5186 * @ctxt: a Relax-NG parser context
5187 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005188 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005189 *
5190 * parse the content of a RelaxNG nameClass node.
5191 *
5192 * Returns the definition pointer or NULL in case of error.
5193 */
5194static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005195xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005196 xmlNodePtr node, int attr)
5197{
Daniel Veillard144fae12003-02-03 13:17:57 +00005198 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5199 xmlNodePtr child;
5200
Daniel Veillardd2298792003-02-14 16:54:11 +00005201 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005202 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5203 "Expecting an except node\n", NULL, NULL);
5204 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005205 }
5206 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005207 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5208 "exceptNameClass allows only a single except node\n",
5209 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005210 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005211 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005212 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5213 NULL, NULL);
5214 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005215 }
5216
Daniel Veillardfd573f12003-03-16 17:52:32 +00005217 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005218 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005219 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005220 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005221 child = node->children;
5222 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005223 cur = xmlRelaxNGNewDefine(ctxt, child);
5224 if (cur == NULL)
5225 break;
5226 if (attr)
5227 cur->type = XML_RELAXNG_ATTRIBUTE;
5228 else
5229 cur->type = XML_RELAXNG_ELEMENT;
5230
Daniel Veillard419a7682003-02-03 23:22:49 +00005231 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005232 if (last == NULL) {
5233 ret->content = cur;
5234 } else {
5235 last->next = cur;
5236 }
5237 last = cur;
5238 }
5239 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005240 }
5241
Daniel Veillard4c004142003-10-07 11:33:24 +00005242 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005243}
5244
5245/**
5246 * xmlRelaxNGParseNameClass:
5247 * @ctxt: a Relax-NG parser context
5248 * @node: the nameClass node
5249 * @def: the current definition
5250 *
5251 * parse the content of a RelaxNG nameClass node.
5252 *
5253 * Returns the definition pointer or NULL in case of error.
5254 */
5255static xmlRelaxNGDefinePtr
5256xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005257 xmlRelaxNGDefinePtr def)
5258{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005259 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005260 xmlChar *val;
5261
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005262 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005263 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005264 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005265 if ((def->type != XML_RELAXNG_ELEMENT) &&
5266 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5267 ret = xmlRelaxNGNewDefine(ctxt, node);
5268 if (ret == NULL)
5269 return (NULL);
5270 ret->parent = def;
5271 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5272 ret->type = XML_RELAXNG_ATTRIBUTE;
5273 else
5274 ret->type = XML_RELAXNG_ELEMENT;
5275 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005276 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005277 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005278 val = xmlNodeGetContent(node);
5279 xmlRelaxNGNormExtSpace(val);
5280 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005281 if (node->parent != NULL)
5282 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5283 "Element %s name '%s' is not an NCName\n",
5284 node->parent->name, val);
5285 else
5286 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5287 "name '%s' is not an NCName\n",
5288 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005289 }
5290 ret->name = val;
5291 val = xmlGetProp(node, BAD_CAST "ns");
5292 ret->ns = val;
5293 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5294 (val != NULL) &&
5295 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005296 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005297 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005298 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005299 }
5300 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5301 (val != NULL) &&
5302 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005303 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5304 "Attribute with QName 'xmlns' is not allowed\n",
5305 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005306 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005307 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005308 ret->name = NULL;
5309 ret->ns = NULL;
5310 if (node->children != NULL) {
5311 ret->nameClass =
5312 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5313 (def->type ==
5314 XML_RELAXNG_ATTRIBUTE));
5315 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005316 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005317 ret->name = NULL;
5318 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5319 if (ret->ns == NULL) {
5320 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5321 "nsName has no ns attribute\n", NULL, NULL);
5322 }
5323 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5324 (ret->ns != NULL) &&
5325 (xmlStrEqual
5326 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5327 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5328 "Attribute with namespace '%s' is not allowed\n",
5329 ret->ns, NULL);
5330 }
5331 if (node->children != NULL) {
5332 ret->nameClass =
5333 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5334 (def->type ==
5335 XML_RELAXNG_ATTRIBUTE));
5336 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005337 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005338 xmlNodePtr child;
5339 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005340
Daniel Veillard4c004142003-10-07 11:33:24 +00005341 ret = xmlRelaxNGNewDefine(ctxt, node);
5342 if (ret == NULL)
5343 return (NULL);
5344 ret->parent = def;
5345 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005346
Daniel Veillard4c004142003-10-07 11:33:24 +00005347 if (node->children == NULL) {
5348 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5349 "Element choice is empty\n", NULL, NULL);
5350 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005351
Daniel Veillard4c004142003-10-07 11:33:24 +00005352 child = node->children;
5353 while (child != NULL) {
5354 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5355 if (tmp != NULL) {
5356 if (last == NULL) {
5357 last = ret->nameClass = tmp;
5358 } else {
5359 last->next = tmp;
5360 last = tmp;
5361 }
5362 }
5363 child = child->next;
5364 }
5365 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005366 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005367 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5368 "expecting name, anyName, nsName or choice : got %s\n",
Daniel Veillard76d36452009-09-07 11:19:33 +02005369 (node == NULL ? 'nothing' : node->name), NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005370 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005371 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005372 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005373 if (def->nameClass == NULL) {
5374 def->nameClass = ret;
5375 } else {
5376 tmp = def->nameClass;
5377 while (tmp->next != NULL) {
5378 tmp = tmp->next;
5379 }
5380 tmp->next = ret;
5381 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005382 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005383 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005384}
5385
5386/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005387 * xmlRelaxNGParseElement:
5388 * @ctxt: a Relax-NG parser context
5389 * @node: the element node
5390 *
5391 * parse the content of a RelaxNG element node.
5392 *
5393 * Returns the definition pointer or NULL in case of error.
5394 */
5395static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005396xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5397{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005398 xmlRelaxNGDefinePtr ret, cur, last;
5399 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005400 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005401
Daniel Veillardfd573f12003-03-16 17:52:32 +00005402 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005403 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005404 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005405 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005406 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005407 child = node->children;
5408 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005409 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5410 "xmlRelaxNGParseElement: element has no children\n",
5411 NULL, NULL);
5412 return (ret);
5413 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005414 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5415 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005416 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005417
Daniel Veillard6eadf632003-01-23 18:29:16 +00005418 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005419 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5420 "xmlRelaxNGParseElement: element has no content\n",
5421 NULL, NULL);
5422 return (ret);
5423 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005424 olddefine = ctxt->define;
5425 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005426 last = NULL;
5427 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005428 cur = xmlRelaxNGParsePattern(ctxt, child);
5429 if (cur != NULL) {
5430 cur->parent = ret;
5431 switch (cur->type) {
5432 case XML_RELAXNG_EMPTY:
5433 case XML_RELAXNG_NOT_ALLOWED:
5434 case XML_RELAXNG_TEXT:
5435 case XML_RELAXNG_ELEMENT:
5436 case XML_RELAXNG_DATATYPE:
5437 case XML_RELAXNG_VALUE:
5438 case XML_RELAXNG_LIST:
5439 case XML_RELAXNG_REF:
5440 case XML_RELAXNG_PARENTREF:
5441 case XML_RELAXNG_EXTERNALREF:
5442 case XML_RELAXNG_DEF:
5443 case XML_RELAXNG_ZEROORMORE:
5444 case XML_RELAXNG_ONEORMORE:
5445 case XML_RELAXNG_OPTIONAL:
5446 case XML_RELAXNG_CHOICE:
5447 case XML_RELAXNG_GROUP:
5448 case XML_RELAXNG_INTERLEAVE:
5449 if (last == NULL) {
5450 ret->content = last = cur;
5451 } else {
5452 if ((last->type == XML_RELAXNG_ELEMENT) &&
5453 (ret->content == last)) {
5454 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5455 if (ret->content != NULL) {
5456 ret->content->type = XML_RELAXNG_GROUP;
5457 ret->content->content = last;
5458 } else {
5459 ret->content = last;
5460 }
5461 }
5462 last->next = cur;
5463 last = cur;
5464 }
5465 break;
5466 case XML_RELAXNG_ATTRIBUTE:
5467 cur->next = ret->attrs;
5468 ret->attrs = cur;
5469 break;
5470 case XML_RELAXNG_START:
5471 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5472 "RNG Internal error, start found in element\n",
5473 NULL, NULL);
5474 break;
5475 case XML_RELAXNG_PARAM:
5476 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5477 "RNG Internal error, param found in element\n",
5478 NULL, NULL);
5479 break;
5480 case XML_RELAXNG_EXCEPT:
5481 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5482 "RNG Internal error, except found in element\n",
5483 NULL, NULL);
5484 break;
5485 case XML_RELAXNG_NOOP:
5486 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5487 "RNG Internal error, noop found in element\n",
5488 NULL, NULL);
5489 break;
5490 }
5491 }
5492 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005493 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005494 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005495 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005496}
5497
5498/**
5499 * xmlRelaxNGParsePatterns:
5500 * @ctxt: a Relax-NG parser context
5501 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005502 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005503 *
5504 * parse the content of a RelaxNG start node.
5505 *
5506 * Returns the definition pointer or NULL in case of error.
5507 */
5508static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005509xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005510 int group)
5511{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005512 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005513
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005514 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005515 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005516 if (IS_RELAXNG(nodes, "element")) {
5517 cur = xmlRelaxNGParseElement(ctxt, nodes);
5518 if (def == NULL) {
5519 def = last = cur;
5520 } else {
5521 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5522 (def == last)) {
5523 def = xmlRelaxNGNewDefine(ctxt, nodes);
5524 def->type = XML_RELAXNG_GROUP;
5525 def->content = last;
5526 }
5527 last->next = cur;
5528 last = cur;
5529 }
5530 cur->parent = parent;
5531 } else {
5532 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5533 if (cur != NULL) {
5534 if (def == NULL) {
5535 def = last = cur;
5536 } else {
5537 last->next = cur;
5538 last = cur;
5539 }
5540 }
5541 }
5542 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005543 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005544 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005545}
5546
5547/**
5548 * xmlRelaxNGParseStart:
5549 * @ctxt: a Relax-NG parser context
5550 * @nodes: start children nodes
5551 *
5552 * parse the content of a RelaxNG start node.
5553 *
5554 * Returns 0 in case of success, -1 in case of error
5555 */
5556static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005557xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5558{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005559 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005560 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005561
Daniel Veillardd2298792003-02-14 16:54:11 +00005562 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005563 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5564 NULL, NULL);
5565 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005566 }
5567 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005568 def = xmlRelaxNGNewDefine(ctxt, nodes);
5569 if (def == NULL)
5570 return (-1);
5571 def->type = XML_RELAXNG_EMPTY;
5572 if (nodes->children != NULL) {
5573 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5574 "element empty is not empty\n", NULL, NULL);
5575 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005576 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005577 def = xmlRelaxNGNewDefine(ctxt, nodes);
5578 if (def == NULL)
5579 return (-1);
5580 def->type = XML_RELAXNG_NOT_ALLOWED;
5581 if (nodes->children != NULL) {
5582 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5583 "element notAllowed is not empty\n", NULL, NULL);
5584 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005585 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005586 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005587 }
5588 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005589 last = ctxt->grammar->start;
5590 while (last->next != NULL)
5591 last = last->next;
5592 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005593 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005594 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005595 }
5596 nodes = nodes->next;
5597 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005598 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5599 "start more than one children\n", NULL, NULL);
5600 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005601 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005602 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005603}
5604
5605/**
5606 * xmlRelaxNGParseGrammarContent:
5607 * @ctxt: a Relax-NG parser context
5608 * @nodes: grammar children nodes
5609 *
5610 * parse the content of a RelaxNG grammar node.
5611 *
5612 * Returns 0 in case of success, -1 in case of error
5613 */
5614static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005615xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5616 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005617{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005618 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005619
5620 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005621 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5622 "grammar has no children\n", NULL, NULL);
5623 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005624 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005625 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005626 if (IS_RELAXNG(nodes, "start")) {
5627 if (nodes->children == NULL) {
5628 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5629 "start has no children\n", NULL, NULL);
5630 } else {
5631 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5632 if (tmp != 0)
5633 ret = -1;
5634 }
5635 } else if (IS_RELAXNG(nodes, "define")) {
5636 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5637 if (tmp != 0)
5638 ret = -1;
5639 } else if (IS_RELAXNG(nodes, "include")) {
5640 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5641 if (tmp != 0)
5642 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005643 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005644 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5645 "grammar has unexpected child %s\n", nodes->name,
5646 NULL);
5647 ret = -1;
5648 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005649 nodes = nodes->next;
5650 }
5651 return (ret);
5652}
5653
5654/**
5655 * xmlRelaxNGCheckReference:
5656 * @ref: the ref
5657 * @ctxt: a Relax-NG parser context
5658 * @name: the name associated to the defines
5659 *
5660 * Applies the 4.17. combine attribute rule for all the define
5661 * element of a given grammar using the same name.
5662 */
5663static void
5664xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005665 xmlRelaxNGParserCtxtPtr ctxt,
5666 const xmlChar * name)
5667{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005668 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005669 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005670
5671 grammar = ctxt->grammar;
5672 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005673 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5674 "Internal error: no grammar in CheckReference %s\n",
5675 name, NULL);
5676 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005677 }
5678 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005679 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5680 "Internal error: reference has content in CheckReference %s\n",
5681 name, NULL);
5682 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005683 }
5684 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005685 def = xmlHashLookup(grammar->defs, name);
5686 if (def != NULL) {
5687 cur = ref;
5688 while (cur != NULL) {
5689 cur->content = def;
5690 cur = cur->nextHash;
5691 }
5692 } else {
5693 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5694 "Reference %s has no matching definition\n", name,
5695 NULL);
5696 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005697 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005698 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5699 "Reference %s has no matching definition\n", name,
5700 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005701 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005702}
5703
5704/**
5705 * xmlRelaxNGCheckCombine:
5706 * @define: the define(s) list
5707 * @ctxt: a Relax-NG parser context
5708 * @name: the name associated to the defines
5709 *
5710 * Applies the 4.17. combine attribute rule for all the define
5711 * element of a given grammar using the same name.
5712 */
5713static void
5714xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005715 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5716{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005717 xmlChar *combine;
5718 int choiceOrInterleave = -1;
5719 int missing = 0;
5720 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5721
5722 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005723 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005724 cur = define;
5725 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005726 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5727 if (combine != NULL) {
5728 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5729 if (choiceOrInterleave == -1)
5730 choiceOrInterleave = 1;
5731 else if (choiceOrInterleave == 0) {
5732 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5733 "Defines for %s use both 'choice' and 'interleave'\n",
5734 name, NULL);
5735 }
5736 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5737 if (choiceOrInterleave == -1)
5738 choiceOrInterleave = 0;
5739 else if (choiceOrInterleave == 1) {
5740 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5741 "Defines for %s use both 'choice' and 'interleave'\n",
5742 name, NULL);
5743 }
5744 } else {
5745 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5746 "Defines for %s use unknown combine value '%s''\n",
5747 name, combine);
5748 }
5749 xmlFree(combine);
5750 } else {
5751 if (missing == 0)
5752 missing = 1;
5753 else {
5754 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5755 "Some defines for %s needs the combine attribute\n",
5756 name, NULL);
5757 }
5758 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005759
Daniel Veillard4c004142003-10-07 11:33:24 +00005760 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005761 }
5762#ifdef DEBUG
5763 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005764 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5765 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005766#endif
5767 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005768 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005769 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005770 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005771 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005772 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005773 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005774 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005775 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005776 tmp = define;
5777 last = NULL;
5778 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005779 if (tmp->content != NULL) {
5780 if (tmp->content->next != NULL) {
5781 /*
5782 * we need first to create a wrapper.
5783 */
5784 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5785 if (tmp2 == NULL)
5786 break;
5787 tmp2->type = XML_RELAXNG_GROUP;
5788 tmp2->content = tmp->content;
5789 } else {
5790 tmp2 = tmp->content;
5791 }
5792 if (last == NULL) {
5793 cur->content = tmp2;
5794 } else {
5795 last->next = tmp2;
5796 }
5797 last = tmp2;
5798 }
5799 tmp->content = cur;
5800 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005801 }
5802 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005803 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005804 if (ctxt->interleaves == NULL)
5805 ctxt->interleaves = xmlHashCreate(10);
5806 if (ctxt->interleaves == NULL) {
5807 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5808 "Failed to create interleaves hash table\n", NULL,
5809 NULL);
5810 } else {
5811 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005812
Daniel Veillard4c004142003-10-07 11:33:24 +00005813 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5814 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5815 0) {
5816 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5817 "Failed to add %s to hash table\n",
5818 (const xmlChar *) tmpname, NULL);
5819 }
5820 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005821 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005822}
5823
5824/**
5825 * xmlRelaxNGCombineStart:
5826 * @ctxt: a Relax-NG parser context
5827 * @grammar: the grammar
5828 *
5829 * Applies the 4.17. combine rule for all the start
5830 * element of a given grammar.
5831 */
5832static void
5833xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005834 xmlRelaxNGGrammarPtr grammar)
5835{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005836 xmlRelaxNGDefinePtr starts;
5837 xmlChar *combine;
5838 int choiceOrInterleave = -1;
5839 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005840 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005841
Daniel Veillard2df2de22003-02-17 23:34:33 +00005842 starts = grammar->start;
5843 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005844 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005845 cur = starts;
5846 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005847 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5848 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5849 combine = NULL;
5850 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5851 "Internal error: start element not found\n", NULL,
5852 NULL);
5853 } else {
5854 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5855 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005856
Daniel Veillard4c004142003-10-07 11:33:24 +00005857 if (combine != NULL) {
5858 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5859 if (choiceOrInterleave == -1)
5860 choiceOrInterleave = 1;
5861 else if (choiceOrInterleave == 0) {
5862 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5863 "<start> use both 'choice' and 'interleave'\n",
5864 NULL, NULL);
5865 }
5866 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5867 if (choiceOrInterleave == -1)
5868 choiceOrInterleave = 0;
5869 else if (choiceOrInterleave == 1) {
5870 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5871 "<start> use both 'choice' and 'interleave'\n",
5872 NULL, NULL);
5873 }
5874 } else {
5875 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5876 "<start> uses unknown combine value '%s''\n",
5877 combine, NULL);
5878 }
5879 xmlFree(combine);
5880 } else {
5881 if (missing == 0)
5882 missing = 1;
5883 else {
5884 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5885 "Some <start> element miss the combine attribute\n",
5886 NULL, NULL);
5887 }
5888 }
5889
5890 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005891 }
5892#ifdef DEBUG
5893 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005894 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5895 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005896#endif
5897 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005898 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005899 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005900 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005901 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005902 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005903 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005904 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005905 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005906 cur->content = grammar->start;
5907 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005908 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005909 if (ctxt->interleaves == NULL)
5910 ctxt->interleaves = xmlHashCreate(10);
5911 if (ctxt->interleaves == NULL) {
5912 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5913 "Failed to create interleaves hash table\n", NULL,
5914 NULL);
5915 } else {
5916 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005917
Daniel Veillard4c004142003-10-07 11:33:24 +00005918 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5919 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5920 0) {
5921 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5922 "Failed to add %s to hash table\n",
5923 (const xmlChar *) tmpname, NULL);
5924 }
5925 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005926 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005927}
5928
5929/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005930 * xmlRelaxNGCheckCycles:
5931 * @ctxt: a Relax-NG parser context
5932 * @nodes: grammar children nodes
5933 * @depth: the counter
5934 *
5935 * Check for cycles.
5936 *
5937 * Returns 0 if check passed, and -1 in case of error
5938 */
5939static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005940xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5941 xmlRelaxNGDefinePtr cur, int depth)
5942{
Daniel Veillardd4310742003-02-18 21:12:46 +00005943 int ret = 0;
5944
5945 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005946 if ((cur->type == XML_RELAXNG_REF) ||
5947 (cur->type == XML_RELAXNG_PARENTREF)) {
5948 if (cur->depth == -1) {
5949 cur->depth = depth;
5950 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5951 cur->depth = -2;
5952 } else if (depth == cur->depth) {
5953 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5954 "Detected a cycle in %s references\n",
5955 cur->name, NULL);
5956 return (-1);
5957 }
5958 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5959 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5960 } else {
5961 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5962 }
5963 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005964 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005965 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005966}
5967
5968/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005969 * xmlRelaxNGTryUnlink:
5970 * @ctxt: a Relax-NG parser context
5971 * @cur: the definition to unlink
5972 * @parent: the parent definition
5973 * @prev: the previous sibling definition
5974 *
5975 * Try to unlink a definition. If not possble make it a NOOP
5976 *
5977 * Returns the new prev definition
5978 */
5979static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005980xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5981 xmlRelaxNGDefinePtr cur,
5982 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5983{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005984 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005985 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005986 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005987 if (parent != NULL) {
5988 if (parent->content == cur)
5989 parent->content = cur->next;
5990 else if (parent->attrs == cur)
5991 parent->attrs = cur->next;
5992 else if (parent->nameClass == cur)
5993 parent->nameClass = cur->next;
5994 } else {
5995 cur->type = XML_RELAXNG_NOOP;
5996 prev = cur;
5997 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005998 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005999 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006000}
6001
6002/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006003 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006004 * @ctxt: a Relax-NG parser context
6005 * @nodes: grammar children nodes
6006 *
6007 * Check for simplification of empty and notAllowed
6008 */
6009static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006010xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6011 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6012{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006013 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006014
Daniel Veillardfd573f12003-03-16 17:52:32 +00006015 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006016 if ((cur->type == XML_RELAXNG_REF) ||
6017 (cur->type == XML_RELAXNG_PARENTREF)) {
6018 if (cur->depth != -3) {
6019 cur->depth = -3;
6020 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6021 }
6022 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6023 cur->parent = parent;
6024 if ((parent != NULL) &&
6025 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6026 (parent->type == XML_RELAXNG_LIST) ||
6027 (parent->type == XML_RELAXNG_GROUP) ||
6028 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6029 (parent->type == XML_RELAXNG_ONEORMORE) ||
6030 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6031 parent->type = XML_RELAXNG_NOT_ALLOWED;
6032 break;
6033 }
6034 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6035 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6036 } else
6037 prev = cur;
6038 } else if (cur->type == XML_RELAXNG_EMPTY) {
6039 cur->parent = parent;
6040 if ((parent != NULL) &&
6041 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6042 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6043 parent->type = XML_RELAXNG_EMPTY;
6044 break;
6045 }
6046 if ((parent != NULL) &&
6047 ((parent->type == XML_RELAXNG_GROUP) ||
6048 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6049 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6050 } else
6051 prev = cur;
6052 } else {
6053 cur->parent = parent;
6054 if (cur->content != NULL)
6055 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6056 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6057 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6058 if (cur->nameClass != NULL)
6059 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6060 /*
6061 * On Elements, try to move attribute only generating rules on
6062 * the attrs rules.
6063 */
6064 if (cur->type == XML_RELAXNG_ELEMENT) {
6065 int attronly;
6066 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006067
Daniel Veillard4c004142003-10-07 11:33:24 +00006068 while (cur->content != NULL) {
6069 attronly =
6070 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6071 if (attronly == 1) {
6072 /*
6073 * migrate cur->content to attrs
6074 */
6075 tmp = cur->content;
6076 cur->content = tmp->next;
6077 tmp->next = cur->attrs;
6078 cur->attrs = tmp;
6079 } else {
6080 /*
6081 * cur->content can generate elements or text
6082 */
6083 break;
6084 }
6085 }
6086 pre = cur->content;
6087 while ((pre != NULL) && (pre->next != NULL)) {
6088 tmp = pre->next;
6089 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6090 if (attronly == 1) {
6091 /*
6092 * migrate tmp to attrs
6093 */
6094 pre->next = tmp->next;
6095 tmp->next = cur->attrs;
6096 cur->attrs = tmp;
6097 } else {
6098 pre = tmp;
6099 }
6100 }
6101 }
6102 /*
6103 * This may result in a simplification
6104 */
6105 if ((cur->type == XML_RELAXNG_GROUP) ||
6106 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6107 if (cur->content == NULL)
6108 cur->type = XML_RELAXNG_EMPTY;
6109 else if (cur->content->next == NULL) {
6110 if ((parent == NULL) && (prev == NULL)) {
6111 cur->type = XML_RELAXNG_NOOP;
6112 } else if (prev == NULL) {
6113 parent->content = cur->content;
6114 cur->content->next = cur->next;
6115 cur = cur->content;
6116 } else {
6117 cur->content->next = cur->next;
6118 prev->next = cur->content;
6119 cur = cur->content;
6120 }
6121 }
6122 }
6123 /*
6124 * the current node may have been transformed back
6125 */
6126 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6127 (cur->content != NULL) &&
6128 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6129 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6130 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6131 if ((parent != NULL) &&
6132 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6133 (parent->type == XML_RELAXNG_LIST) ||
6134 (parent->type == XML_RELAXNG_GROUP) ||
6135 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6136 (parent->type == XML_RELAXNG_ONEORMORE) ||
6137 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6138 parent->type = XML_RELAXNG_NOT_ALLOWED;
6139 break;
6140 }
6141 if ((parent != NULL) &&
6142 (parent->type == XML_RELAXNG_CHOICE)) {
6143 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6144 } else
6145 prev = cur;
6146 } else if (cur->type == XML_RELAXNG_EMPTY) {
6147 if ((parent != NULL) &&
6148 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6149 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6150 parent->type = XML_RELAXNG_EMPTY;
6151 break;
6152 }
6153 if ((parent != NULL) &&
6154 ((parent->type == XML_RELAXNG_GROUP) ||
6155 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6156 (parent->type == XML_RELAXNG_CHOICE))) {
6157 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6158 } else
6159 prev = cur;
6160 } else {
6161 prev = cur;
6162 }
6163 }
6164 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006165 }
6166}
6167
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006168/**
6169 * xmlRelaxNGGroupContentType:
6170 * @ct1: the first content type
6171 * @ct2: the second content type
6172 *
6173 * Try to group 2 content types
6174 *
6175 * Returns the content type
6176 */
6177static xmlRelaxNGContentType
6178xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006179 xmlRelaxNGContentType ct2)
6180{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006181 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006182 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6183 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006184 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006185 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006186 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006187 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006188 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006189 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6190 return (XML_RELAXNG_CONTENT_COMPLEX);
6191 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006192}
6193
6194/**
6195 * xmlRelaxNGMaxContentType:
6196 * @ct1: the first content type
6197 * @ct2: the second content type
6198 *
6199 * Compute the max content-type
6200 *
6201 * Returns the content type
6202 */
6203static xmlRelaxNGContentType
6204xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006205 xmlRelaxNGContentType ct2)
6206{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006207 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006208 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6209 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006210 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006211 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6212 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006213 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006214 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6215 return (XML_RELAXNG_CONTENT_COMPLEX);
6216 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006217}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006218
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006219/**
6220 * xmlRelaxNGCheckRules:
6221 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006222 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006223 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006224 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006225 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006226 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006227 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006228 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006229 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006230static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006231xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6232 xmlRelaxNGDefinePtr cur, int flags,
6233 xmlRelaxNGType ptype)
6234{
Daniel Veillardd44b9362009-09-07 12:15:08 +02006235 int nflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006236 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006237
Daniel Veillardfd573f12003-03-16 17:52:32 +00006238 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006239 ret = XML_RELAXNG_CONTENT_EMPTY;
6240 if ((cur->type == XML_RELAXNG_REF) ||
6241 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006242 /*
6243 * This should actually be caught by list//element(ref) at the
6244 * element boundaries, c.f. Bug #159968 local refs are dropped
6245 * in step 4.19.
6246 */
6247#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006248 if (flags & XML_RELAXNG_IN_LIST) {
6249 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6250 "Found forbidden pattern list//ref\n", NULL,
6251 NULL);
6252 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006253#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006254 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6255 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6256 "Found forbidden pattern data/except//ref\n",
6257 NULL, NULL);
6258 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006259 if (cur->content == NULL) {
6260 if (cur->type == XML_RELAXNG_PARENTREF)
6261 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6262 "Internal found no define for parent refs\n",
6263 NULL, NULL);
6264 else
6265 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6266 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006267 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006268 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006269 if (cur->depth > -4) {
6270 cur->depth = -4;
6271 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6272 flags, cur->type);
6273 cur->depth = ret - 15;
6274 } else if (cur->depth == -4) {
6275 ret = XML_RELAXNG_CONTENT_COMPLEX;
6276 } else {
6277 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6278 }
6279 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6280 /*
6281 * The 7.3 Attribute derivation rule for groups is plugged there
6282 */
6283 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6284 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6285 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6286 "Found forbidden pattern data/except//element(ref)\n",
6287 NULL, NULL);
6288 }
6289 if (flags & XML_RELAXNG_IN_LIST) {
6290 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6291 "Found forbidden pattern list//element(ref)\n",
6292 NULL, NULL);
6293 }
6294 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6295 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6296 "Found forbidden pattern attribute//element(ref)\n",
6297 NULL, NULL);
6298 }
6299 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6300 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6301 "Found forbidden pattern attribute//element(ref)\n",
6302 NULL, NULL);
6303 }
6304 /*
6305 * reset since in the simple form elements are only child
6306 * of grammar/define
6307 */
6308 nflags = 0;
6309 ret =
6310 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6311 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6312 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6313 "Element %s attributes have a content type error\n",
6314 cur->name, NULL);
6315 }
6316 ret =
6317 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6318 cur->type);
6319 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6320 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6321 "Element %s has a content type error\n",
6322 cur->name, NULL);
6323 } else {
6324 ret = XML_RELAXNG_CONTENT_COMPLEX;
6325 }
6326 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6327 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6328 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6329 "Found forbidden pattern attribute//attribute\n",
6330 NULL, NULL);
6331 }
6332 if (flags & XML_RELAXNG_IN_LIST) {
6333 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6334 "Found forbidden pattern list//attribute\n",
6335 NULL, NULL);
6336 }
6337 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6338 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6339 "Found forbidden pattern oneOrMore//group//attribute\n",
6340 NULL, NULL);
6341 }
6342 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6343 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6344 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6345 NULL, NULL);
6346 }
6347 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6348 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6349 "Found forbidden pattern data/except//attribute\n",
6350 NULL, NULL);
6351 }
6352 if (flags & XML_RELAXNG_IN_START) {
6353 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6354 "Found forbidden pattern start//attribute\n",
6355 NULL, NULL);
6356 }
6357 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6358 && (cur->name == NULL)) {
6359 if (cur->ns == NULL) {
6360 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6361 "Found anyName attribute without oneOrMore ancestor\n",
6362 NULL, NULL);
6363 } else {
6364 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6365 "Found nsName attribute without oneOrMore ancestor\n",
6366 NULL, NULL);
6367 }
6368 }
6369 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6370 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6371 ret = XML_RELAXNG_CONTENT_EMPTY;
6372 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6373 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6374 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6375 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6376 "Found forbidden pattern data/except//oneOrMore\n",
6377 NULL, NULL);
6378 }
6379 if (flags & XML_RELAXNG_IN_START) {
6380 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6381 "Found forbidden pattern start//oneOrMore\n",
6382 NULL, NULL);
6383 }
6384 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6385 ret =
6386 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6387 cur->type);
6388 ret = xmlRelaxNGGroupContentType(ret, ret);
6389 } else if (cur->type == XML_RELAXNG_LIST) {
6390 if (flags & XML_RELAXNG_IN_LIST) {
6391 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6392 "Found forbidden pattern list//list\n", NULL,
6393 NULL);
6394 }
6395 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6396 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6397 "Found forbidden pattern data/except//list\n",
6398 NULL, NULL);
6399 }
6400 if (flags & XML_RELAXNG_IN_START) {
6401 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6402 "Found forbidden pattern start//list\n", NULL,
6403 NULL);
6404 }
6405 nflags = flags | XML_RELAXNG_IN_LIST;
6406 ret =
6407 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6408 cur->type);
6409 } else if (cur->type == XML_RELAXNG_GROUP) {
6410 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6411 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6412 "Found forbidden pattern data/except//group\n",
6413 NULL, NULL);
6414 }
6415 if (flags & XML_RELAXNG_IN_START) {
6416 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6417 "Found forbidden pattern start//group\n", NULL,
6418 NULL);
6419 }
6420 if (flags & XML_RELAXNG_IN_ONEORMORE)
6421 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6422 else
6423 nflags = flags;
6424 ret =
6425 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6426 cur->type);
6427 /*
6428 * The 7.3 Attribute derivation rule for groups is plugged there
6429 */
6430 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6431 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6432 if (flags & XML_RELAXNG_IN_LIST) {
6433 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6434 "Found forbidden pattern list//interleave\n",
6435 NULL, NULL);
6436 }
6437 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6438 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6439 "Found forbidden pattern data/except//interleave\n",
6440 NULL, NULL);
6441 }
6442 if (flags & XML_RELAXNG_IN_START) {
6443 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6444 "Found forbidden pattern start//interleave\n",
6445 NULL, NULL);
6446 }
6447 if (flags & XML_RELAXNG_IN_ONEORMORE)
6448 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6449 else
6450 nflags = flags;
6451 ret =
6452 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6453 cur->type);
6454 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6455 if ((cur->parent != NULL) &&
6456 (cur->parent->type == XML_RELAXNG_DATATYPE))
6457 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6458 else
6459 nflags = flags;
6460 ret =
6461 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6462 cur->type);
6463 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6464 if (flags & XML_RELAXNG_IN_START) {
6465 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6466 "Found forbidden pattern start//data\n", NULL,
6467 NULL);
6468 }
6469 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6470 ret = XML_RELAXNG_CONTENT_SIMPLE;
6471 } else if (cur->type == XML_RELAXNG_VALUE) {
6472 if (flags & XML_RELAXNG_IN_START) {
6473 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6474 "Found forbidden pattern start//value\n", NULL,
6475 NULL);
6476 }
6477 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6478 ret = XML_RELAXNG_CONTENT_SIMPLE;
6479 } else if (cur->type == XML_RELAXNG_TEXT) {
6480 if (flags & XML_RELAXNG_IN_LIST) {
6481 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6482 "Found forbidden pattern list//text\n", NULL,
6483 NULL);
6484 }
6485 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6486 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6487 "Found forbidden pattern data/except//text\n",
6488 NULL, NULL);
6489 }
6490 if (flags & XML_RELAXNG_IN_START) {
6491 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6492 "Found forbidden pattern start//text\n", NULL,
6493 NULL);
6494 }
6495 ret = XML_RELAXNG_CONTENT_COMPLEX;
6496 } else if (cur->type == XML_RELAXNG_EMPTY) {
6497 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6498 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6499 "Found forbidden pattern data/except//empty\n",
6500 NULL, NULL);
6501 }
6502 if (flags & XML_RELAXNG_IN_START) {
6503 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6504 "Found forbidden pattern start//empty\n", NULL,
6505 NULL);
6506 }
6507 ret = XML_RELAXNG_CONTENT_EMPTY;
6508 } else if (cur->type == XML_RELAXNG_CHOICE) {
6509 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6510 ret =
6511 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6512 } else {
6513 ret =
6514 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6515 }
6516 cur = cur->next;
6517 if (ptype == XML_RELAXNG_GROUP) {
6518 val = xmlRelaxNGGroupContentType(val, ret);
6519 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6520 tmp = xmlRelaxNGGroupContentType(val, ret);
6521 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6522 tmp = xmlRelaxNGMaxContentType(val, ret);
6523 } else if (ptype == XML_RELAXNG_CHOICE) {
6524 val = xmlRelaxNGMaxContentType(val, ret);
6525 } else if (ptype == XML_RELAXNG_LIST) {
6526 val = XML_RELAXNG_CONTENT_SIMPLE;
6527 } else if (ptype == XML_RELAXNG_EXCEPT) {
6528 if (ret == XML_RELAXNG_CONTENT_ERROR)
6529 val = XML_RELAXNG_CONTENT_ERROR;
6530 else
6531 val = XML_RELAXNG_CONTENT_SIMPLE;
6532 } else {
6533 val = xmlRelaxNGGroupContentType(val, ret);
6534 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006535
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006536 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006537 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006538}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006539
6540/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006541 * xmlRelaxNGParseGrammar:
6542 * @ctxt: a Relax-NG parser context
6543 * @nodes: grammar children nodes
6544 *
6545 * parse a Relax-NG <grammar> node
6546 *
6547 * Returns the internal xmlRelaxNGGrammarPtr built or
6548 * NULL in case of error
6549 */
6550static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006551xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6552{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006553 xmlRelaxNGGrammarPtr ret, tmp, old;
6554
Daniel Veillardc482e262003-02-26 14:48:48 +00006555#ifdef DEBUG_GRAMMAR
6556 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6557#endif
6558
Daniel Veillard6eadf632003-01-23 18:29:16 +00006559 ret = xmlRelaxNGNewGrammar(ctxt);
6560 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006561 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006562
6563 /*
6564 * Link the new grammar in the tree
6565 */
6566 ret->parent = ctxt->grammar;
6567 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006568 tmp = ctxt->grammar->children;
6569 if (tmp == NULL) {
6570 ctxt->grammar->children = ret;
6571 } else {
6572 while (tmp->next != NULL)
6573 tmp = tmp->next;
6574 tmp->next = ret;
6575 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006576 }
6577
6578 old = ctxt->grammar;
6579 ctxt->grammar = ret;
6580 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6581 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006582 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006583 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6584 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006585 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006586 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6587 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006588 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006589
6590 /*
6591 * Apply 4.17 mergingd rules to defines and starts
6592 */
6593 xmlRelaxNGCombineStart(ctxt, ret);
6594 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006595 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6596 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006597 }
6598
6599 /*
6600 * link together defines and refs in this grammar
6601 */
6602 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006603 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6604 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006605 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006606
Daniel Veillard81c51e12009-08-14 18:52:10 +02006607
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006608 /* @@@@ */
6609
Daniel Veillard6eadf632003-01-23 18:29:16 +00006610 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006611 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006612}
6613
6614/**
6615 * xmlRelaxNGParseDocument:
6616 * @ctxt: a Relax-NG parser context
6617 * @node: the root node of the RelaxNG schema
6618 *
6619 * parse a Relax-NG definition resource and build an internal
6620 * xmlRelaxNG struture which can be used to validate instances.
6621 *
6622 * Returns the internal XML RelaxNG structure built or
6623 * NULL in case of error
6624 */
6625static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006626xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6627{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006628 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006629 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006630 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006631
6632 if ((ctxt == NULL) || (node == NULL))
6633 return (NULL);
6634
6635 schema = xmlRelaxNGNewRelaxNG(ctxt);
6636 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006637 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006638
Daniel Veillard276be4a2003-01-24 01:03:34 +00006639 olddefine = ctxt->define;
6640 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006641 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006642 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006643 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006644 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006645
Daniel Veillard4c004142003-10-07 11:33:24 +00006646 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6647 if (schema->topgrammar == NULL) {
6648 return (schema);
6649 }
6650 /*
6651 * Link the new grammar in the tree
6652 */
6653 ret->parent = ctxt->grammar;
6654 if (ctxt->grammar != NULL) {
6655 tmp = ctxt->grammar->children;
6656 if (tmp == NULL) {
6657 ctxt->grammar->children = ret;
6658 } else {
6659 while (tmp->next != NULL)
6660 tmp = tmp->next;
6661 tmp->next = ret;
6662 }
6663 }
6664 old = ctxt->grammar;
6665 ctxt->grammar = ret;
6666 xmlRelaxNGParseStart(ctxt, node);
6667 if (old != NULL)
6668 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006669 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006670 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006671 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006672 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6673 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6674 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6675 while ((schema->topgrammar->start != NULL) &&
6676 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6677 (schema->topgrammar->start->next != NULL))
6678 schema->topgrammar->start =
6679 schema->topgrammar->start->content;
6680 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6681 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6682 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006683 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006684#ifdef DEBUG
6685 if (schema == NULL)
6686 xmlGenericError(xmlGenericErrorContext,
6687 "xmlRelaxNGParseDocument() failed\n");
6688#endif
6689
6690 return (schema);
6691}
6692
6693/************************************************************************
6694 * *
6695 * Reading RelaxNGs *
6696 * *
6697 ************************************************************************/
6698
6699/**
6700 * xmlRelaxNGNewParserCtxt:
6701 * @URL: the location of the schema
6702 *
6703 * Create an XML RelaxNGs parse context for that file/resource expected
6704 * to contain an XML RelaxNGs file.
6705 *
6706 * Returns the parser context or NULL in case of error
6707 */
6708xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006709xmlRelaxNGNewParserCtxt(const char *URL)
6710{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006711 xmlRelaxNGParserCtxtPtr ret;
6712
6713 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006714 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006715
Daniel Veillard4c004142003-10-07 11:33:24 +00006716 ret =
6717 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006718 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006719 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006720 return (NULL);
6721 }
6722 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006723 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006724 ret->error = xmlGenericError;
6725 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006726 return (ret);
6727}
6728
6729/**
6730 * xmlRelaxNGNewMemParserCtxt:
6731 * @buffer: a pointer to a char array containing the schemas
6732 * @size: the size of the array
6733 *
6734 * Create an XML RelaxNGs parse context for that memory buffer expected
6735 * to contain an XML RelaxNGs file.
6736 *
6737 * Returns the parser context or NULL in case of error
6738 */
6739xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006740xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6741{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006742 xmlRelaxNGParserCtxtPtr ret;
6743
6744 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006745 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006746
Daniel Veillard4c004142003-10-07 11:33:24 +00006747 ret =
6748 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006749 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006750 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006751 return (NULL);
6752 }
6753 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6754 ret->buffer = buffer;
6755 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006756 ret->error = xmlGenericError;
6757 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006758 return (ret);
6759}
6760
6761/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006762 * xmlRelaxNGNewDocParserCtxt:
6763 * @doc: a preparsed document tree
6764 *
6765 * Create an XML RelaxNGs parser context for that document.
6766 * Note: since the process of compiling a RelaxNG schemas modifies the
6767 * document, the @doc parameter is duplicated internally.
6768 *
6769 * Returns the parser context or NULL in case of error
6770 */
6771xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006772xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6773{
Daniel Veillard33300b42003-04-17 09:09:19 +00006774 xmlRelaxNGParserCtxtPtr ret;
6775 xmlDocPtr copy;
6776
6777 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006778 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006779 copy = xmlCopyDoc(doc, 1);
6780 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006781 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006782
Daniel Veillard4c004142003-10-07 11:33:24 +00006783 ret =
6784 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006785 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006786 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006787 return (NULL);
6788 }
6789 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6790 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006791 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006792 ret->userData = xmlGenericErrorContext;
6793 return (ret);
6794}
6795
6796/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006797 * xmlRelaxNGFreeParserCtxt:
6798 * @ctxt: the schema parser context
6799 *
6800 * Free the resources associated to the schema parser context
6801 */
6802void
Daniel Veillard4c004142003-10-07 11:33:24 +00006803xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6804{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006805 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006806 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006807 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006808 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006809 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006810 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006811 if (ctxt->interleaves != NULL)
6812 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006813 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006814 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006815 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006816 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006817 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006818 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006819 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006820 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006821 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006822 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006823
Daniel Veillard4c004142003-10-07 11:33:24 +00006824 for (i = 0; i < ctxt->defNr; i++)
6825 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6826 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006827 }
Daniel Veillard42595322004-11-08 10:52:06 +00006828 if ((ctxt->document != NULL) && (ctxt->freedoc))
6829 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006830 xmlFree(ctxt);
6831}
6832
Daniel Veillard6eadf632003-01-23 18:29:16 +00006833/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006834 * xmlRelaxNGNormExtSpace:
6835 * @value: a value
6836 *
6837 * Removes the leading and ending spaces of the value
6838 * The string is modified "in situ"
6839 */
6840static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006841xmlRelaxNGNormExtSpace(xmlChar * value)
6842{
Daniel Veillardd2298792003-02-14 16:54:11 +00006843 xmlChar *start = value;
6844 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006845
Daniel Veillard4c004142003-10-07 11:33:24 +00006846 if (value == NULL)
6847 return;
6848
William M. Brack76e95df2003-10-18 16:20:14 +00006849 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006850 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006851 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006852 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006853 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006854 cur++;
6855 if (*cur == 0)
6856 return;
6857 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006858 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006859 cur++;
6860 if (*cur == 0) {
6861 *start = 0;
6862 return;
6863 }
6864 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006865 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006866 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006867 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006868 *start++ = *cur++;
6869 if (*cur == 0) {
6870 *start = 0;
6871 return;
6872 }
6873 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006874 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006875 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006876 if (*cur == 0) {
6877 *start = 0;
6878 return;
6879 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006880 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006881 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006882 }
6883}
6884
6885/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006886 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006887 * @ctxt: a Relax-NG parser context
6888 * @node: a Relax-NG node
6889 *
6890 * Check all the attributes on the given node
6891 */
6892static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006893xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6894{
Daniel Veillardd2298792003-02-14 16:54:11 +00006895 xmlAttrPtr cur, next;
6896
6897 cur = node->properties;
6898 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006899 next = cur->next;
6900 if ((cur->ns == NULL) ||
6901 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6902 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6903 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6904 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6905 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6906 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6907 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6908 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6909 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6910 "Attribute %s is not allowed on %s\n",
6911 cur->name, node->name);
6912 }
6913 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6914 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6915 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6916 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6917 "Attribute %s is not allowed on %s\n",
6918 cur->name, node->name);
6919 }
6920 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6921 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6922 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6923 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6924 "Attribute %s is not allowed on %s\n",
6925 cur->name, node->name);
6926 }
6927 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6928 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6929 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6930 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6931 "Attribute %s is not allowed on %s\n",
6932 cur->name, node->name);
6933 }
6934 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6935 xmlChar *val;
6936 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006937
Daniel Veillard4c004142003-10-07 11:33:24 +00006938 val = xmlNodeListGetString(node->doc, cur->children, 1);
6939 if (val != NULL) {
6940 if (val[0] != 0) {
6941 uri = xmlParseURI((const char *) val);
6942 if (uri == NULL) {
6943 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6944 "Attribute %s contains invalid URI %s\n",
6945 cur->name, val);
6946 } else {
6947 if (uri->scheme == NULL) {
6948 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6949 "Attribute %s URI %s is not absolute\n",
6950 cur->name, val);
6951 }
6952 if (uri->fragment != NULL) {
6953 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6954 "Attribute %s URI %s has a fragment ID\n",
6955 cur->name, val);
6956 }
6957 xmlFreeURI(uri);
6958 }
6959 }
6960 xmlFree(val);
6961 }
6962 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6963 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6964 "Unknown attribute %s on %s\n", cur->name,
6965 node->name);
6966 }
6967 }
6968 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006969 }
6970}
6971
6972/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006973 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006974 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006975 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006976 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006977 * Cleanup the subtree from unwanted nodes for parsing, resolve
6978 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006979 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006980static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006981xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6982{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006983 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006984
Daniel Veillard6eadf632003-01-23 18:29:16 +00006985 delete = NULL;
6986 cur = root;
6987 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006988 if (delete != NULL) {
6989 xmlUnlinkNode(delete);
6990 xmlFreeNode(delete);
6991 delete = NULL;
6992 }
6993 if (cur->type == XML_ELEMENT_NODE) {
6994 /*
6995 * Simplification 4.1. Annotations
6996 */
6997 if ((cur->ns == NULL) ||
6998 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6999 if ((cur->parent != NULL) &&
7000 (cur->parent->type == XML_ELEMENT_NODE) &&
7001 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7002 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7003 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7004 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7005 "element %s doesn't allow foreign elements\n",
7006 cur->parent->name, NULL);
7007 }
7008 delete = cur;
7009 goto skip_children;
7010 } else {
7011 xmlRelaxNGCleanupAttributes(ctxt, cur);
7012 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7013 xmlChar *href, *ns, *base, *URL;
7014 xmlRelaxNGDocumentPtr docu;
7015 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00007016 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007017
Daniel Veillard4c004142003-10-07 11:33:24 +00007018 ns = xmlGetProp(cur, BAD_CAST "ns");
7019 if (ns == NULL) {
7020 tmp = cur->parent;
7021 while ((tmp != NULL) &&
7022 (tmp->type == XML_ELEMENT_NODE)) {
7023 ns = xmlGetProp(tmp, BAD_CAST "ns");
7024 if (ns != NULL)
7025 break;
7026 tmp = tmp->parent;
7027 }
7028 }
7029 href = xmlGetProp(cur, BAD_CAST "href");
7030 if (href == NULL) {
7031 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7032 "xmlRelaxNGParse: externalRef has no href attribute\n",
7033 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007034 if (ns != NULL)
7035 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007036 delete = cur;
7037 goto skip_children;
7038 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007039 uri = xmlParseURI((const char *) href);
7040 if (uri == NULL) {
7041 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7042 "Incorrect URI for externalRef %s\n",
7043 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007044 if (ns != NULL)
7045 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007046 if (href != NULL)
7047 xmlFree(href);
7048 delete = cur;
7049 goto skip_children;
7050 }
7051 if (uri->fragment != NULL) {
7052 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7053 "Fragment forbidden in URI for externalRef %s\n",
7054 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007055 if (ns != NULL)
7056 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007057 xmlFreeURI(uri);
7058 if (href != NULL)
7059 xmlFree(href);
7060 delete = cur;
7061 goto skip_children;
7062 }
7063 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007064 base = xmlNodeGetBase(cur->doc, cur);
7065 URL = xmlBuildURI(href, base);
7066 if (URL == NULL) {
7067 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7068 "Failed to compute URL for externalRef %s\n",
7069 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007070 if (ns != NULL)
7071 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007072 if (href != NULL)
7073 xmlFree(href);
7074 if (base != NULL)
7075 xmlFree(base);
7076 delete = cur;
7077 goto skip_children;
7078 }
7079 if (href != NULL)
7080 xmlFree(href);
7081 if (base != NULL)
7082 xmlFree(base);
7083 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7084 if (docu == NULL) {
7085 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7086 "Failed to load externalRef %s\n", URL,
7087 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007088 if (ns != NULL)
7089 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007090 xmlFree(URL);
7091 delete = cur;
7092 goto skip_children;
7093 }
7094 if (ns != NULL)
7095 xmlFree(ns);
7096 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007097 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007098 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7099 xmlChar *href, *ns, *base, *URL;
7100 xmlRelaxNGIncludePtr incl;
7101 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007102
Daniel Veillard4c004142003-10-07 11:33:24 +00007103 href = xmlGetProp(cur, BAD_CAST "href");
7104 if (href == NULL) {
7105 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7106 "xmlRelaxNGParse: include has no href attribute\n",
7107 NULL, NULL);
7108 delete = cur;
7109 goto skip_children;
7110 }
7111 base = xmlNodeGetBase(cur->doc, cur);
7112 URL = xmlBuildURI(href, base);
7113 if (URL == NULL) {
7114 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7115 "Failed to compute URL for include %s\n",
7116 href, NULL);
7117 if (href != NULL)
7118 xmlFree(href);
7119 if (base != NULL)
7120 xmlFree(base);
7121 delete = cur;
7122 goto skip_children;
7123 }
7124 if (href != NULL)
7125 xmlFree(href);
7126 if (base != NULL)
7127 xmlFree(base);
7128 ns = xmlGetProp(cur, BAD_CAST "ns");
7129 if (ns == NULL) {
7130 tmp = cur->parent;
7131 while ((tmp != NULL) &&
7132 (tmp->type == XML_ELEMENT_NODE)) {
7133 ns = xmlGetProp(tmp, BAD_CAST "ns");
7134 if (ns != NULL)
7135 break;
7136 tmp = tmp->parent;
7137 }
7138 }
7139 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7140 if (ns != NULL)
7141 xmlFree(ns);
7142 if (incl == NULL) {
7143 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7144 "Failed to load include %s\n", URL,
7145 NULL);
7146 xmlFree(URL);
7147 delete = cur;
7148 goto skip_children;
7149 }
7150 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007151 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007152 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7153 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7154 {
7155 xmlChar *name, *ns;
7156 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007157
Daniel Veillard4c004142003-10-07 11:33:24 +00007158 /*
7159 * Simplification 4.8. name attribute of element
7160 * and attribute elements
7161 */
7162 name = xmlGetProp(cur, BAD_CAST "name");
7163 if (name != NULL) {
7164 if (cur->children == NULL) {
7165 text =
7166 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7167 name);
7168 } else {
7169 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007170
Daniel Veillard03a53c32004-10-26 16:06:51 +00007171 node = xmlNewDocNode(cur->doc, cur->ns,
7172 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007173 if (node != NULL) {
7174 xmlAddPrevSibling(cur->children, node);
7175 text = xmlNewText(name);
7176 xmlAddChild(node, text);
7177 text = node;
7178 }
7179 }
7180 if (text == NULL) {
7181 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7182 "Failed to create a name %s element\n",
7183 name, NULL);
7184 }
7185 xmlUnsetProp(cur, BAD_CAST "name");
7186 xmlFree(name);
7187 ns = xmlGetProp(cur, BAD_CAST "ns");
7188 if (ns != NULL) {
7189 if (text != NULL) {
7190 xmlSetProp(text, BAD_CAST "ns", ns);
7191 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7192 }
7193 xmlFree(ns);
7194 } else if (xmlStrEqual(cur->name,
7195 BAD_CAST "attribute")) {
7196 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7197 }
7198 }
7199 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7200 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7201 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7202 /*
7203 * Simplification 4.8. name attribute of element
7204 * and attribute elements
7205 */
7206 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7207 xmlNodePtr node;
7208 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007209
Daniel Veillard4c004142003-10-07 11:33:24 +00007210 node = cur->parent;
7211 while ((node != NULL) &&
7212 (node->type == XML_ELEMENT_NODE)) {
7213 ns = xmlGetProp(node, BAD_CAST "ns");
7214 if (ns != NULL) {
7215 break;
7216 }
7217 node = node->parent;
7218 }
7219 if (ns == NULL) {
7220 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7221 } else {
7222 xmlSetProp(cur, BAD_CAST "ns", ns);
7223 xmlFree(ns);
7224 }
7225 }
7226 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7227 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007228
Daniel Veillard4c004142003-10-07 11:33:24 +00007229 /*
7230 * Simplification: 4.10. QNames
7231 */
7232 name = xmlNodeGetContent(cur);
7233 if (name != NULL) {
7234 local = xmlSplitQName2(name, &prefix);
7235 if (local != NULL) {
7236 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007237
Daniel Veillard4c004142003-10-07 11:33:24 +00007238 ns = xmlSearchNs(cur->doc, cur, prefix);
7239 if (ns == NULL) {
7240 xmlRngPErr(ctxt, cur,
7241 XML_RNGP_PREFIX_UNDEFINED,
7242 "xmlRelaxNGParse: no namespace for prefix %s\n",
7243 prefix, NULL);
7244 } else {
7245 xmlSetProp(cur, BAD_CAST "ns",
7246 ns->href);
7247 xmlNodeSetContent(cur, local);
7248 }
7249 xmlFree(local);
7250 xmlFree(prefix);
7251 }
7252 xmlFree(name);
7253 }
7254 }
7255 /*
7256 * 4.16
7257 */
7258 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7259 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7260 xmlRngPErr(ctxt, cur,
7261 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7262 "Found nsName/except//nsName forbidden construct\n",
7263 NULL, NULL);
7264 }
7265 }
7266 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7267 (cur != root)) {
7268 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007269
Daniel Veillard4c004142003-10-07 11:33:24 +00007270 /*
7271 * 4.16
7272 */
7273 if ((cur->parent != NULL) &&
7274 (xmlStrEqual
7275 (cur->parent->name, BAD_CAST "anyName"))) {
7276 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7277 xmlRelaxNGCleanupTree(ctxt, cur);
7278 ctxt->flags = oldflags;
7279 goto skip_children;
7280 } else if ((cur->parent != NULL) &&
7281 (xmlStrEqual
7282 (cur->parent->name, BAD_CAST "nsName"))) {
7283 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7284 xmlRelaxNGCleanupTree(ctxt, cur);
7285 ctxt->flags = oldflags;
7286 goto skip_children;
7287 }
7288 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7289 /*
7290 * 4.16
7291 */
7292 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7293 xmlRngPErr(ctxt, cur,
7294 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7295 "Found anyName/except//anyName forbidden construct\n",
7296 NULL, NULL);
7297 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7298 xmlRngPErr(ctxt, cur,
7299 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7300 "Found nsName/except//anyName forbidden construct\n",
7301 NULL, NULL);
7302 }
7303 }
7304 /*
7305 * Thisd is not an else since "include" is transformed
7306 * into a div
7307 */
7308 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7309 xmlChar *ns;
7310 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007311
Daniel Veillard4c004142003-10-07 11:33:24 +00007312 /*
7313 * implements rule 4.11
7314 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007315
Daniel Veillard4c004142003-10-07 11:33:24 +00007316 ns = xmlGetProp(cur, BAD_CAST "ns");
7317
7318 child = cur->children;
7319 ins = cur;
7320 while (child != NULL) {
7321 if (ns != NULL) {
7322 if (!xmlHasProp(child, BAD_CAST "ns")) {
7323 xmlSetProp(child, BAD_CAST "ns", ns);
7324 }
7325 }
7326 tmp = child->next;
7327 xmlUnlinkNode(child);
7328 ins = xmlAddNextSibling(ins, child);
7329 child = tmp;
7330 }
7331 if (ns != NULL)
7332 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007333 /*
7334 * Since we are about to delete cur, if it's nsDef is non-NULL we
7335 * need to preserve it (it contains the ns definitions for the
7336 * children we just moved). We'll just stick it on to the end
7337 * of cur->parent's list, since it's never going to be re-serialized
7338 * (bug 143738).
7339 */
7340 if (cur->nsDef != NULL) {
7341 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7342 while (parDef->next != NULL)
7343 parDef = parDef->next;
7344 parDef->next = cur->nsDef;
7345 cur->nsDef = NULL;
7346 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007347 delete = cur;
7348 goto skip_children;
7349 }
7350 }
7351 }
7352 /*
7353 * Simplification 4.2 whitespaces
7354 */
7355 else if ((cur->type == XML_TEXT_NODE) ||
7356 (cur->type == XML_CDATA_SECTION_NODE)) {
7357 if (IS_BLANK_NODE(cur)) {
7358 if (cur->parent->type == XML_ELEMENT_NODE) {
7359 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7360 &&
7361 (!xmlStrEqual
7362 (cur->parent->name, BAD_CAST "param")))
7363 delete = cur;
7364 } else {
7365 delete = cur;
7366 goto skip_children;
7367 }
7368 }
7369 } else {
7370 delete = cur;
7371 goto skip_children;
7372 }
7373
7374 /*
7375 * Skip to next node
7376 */
7377 if (cur->children != NULL) {
7378 if ((cur->children->type != XML_ENTITY_DECL) &&
7379 (cur->children->type != XML_ENTITY_REF_NODE) &&
7380 (cur->children->type != XML_ENTITY_NODE)) {
7381 cur = cur->children;
7382 continue;
7383 }
7384 }
7385 skip_children:
7386 if (cur->next != NULL) {
7387 cur = cur->next;
7388 continue;
7389 }
7390
7391 do {
7392 cur = cur->parent;
7393 if (cur == NULL)
7394 break;
7395 if (cur == root) {
7396 cur = NULL;
7397 break;
7398 }
7399 if (cur->next != NULL) {
7400 cur = cur->next;
7401 break;
7402 }
7403 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007404 }
7405 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007406 xmlUnlinkNode(delete);
7407 xmlFreeNode(delete);
7408 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007409 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007410}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007411
Daniel Veillardc5312d72003-02-21 17:14:10 +00007412/**
7413 * xmlRelaxNGCleanupDoc:
7414 * @ctxt: a Relax-NG parser context
7415 * @doc: an xmldocPtr document pointer
7416 *
7417 * Cleanup the document from unwanted nodes for parsing, resolve
7418 * Include and externalRef lookups.
7419 *
7420 * Returns the cleaned up document or NULL in case of error
7421 */
7422static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007423xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7424{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007425 xmlNodePtr root;
7426
7427 /*
7428 * Extract the root
7429 */
7430 root = xmlDocGetRootElement(doc);
7431 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007432 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7433 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007434 return (NULL);
7435 }
7436 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007437 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007438}
7439
7440/**
7441 * xmlRelaxNGParse:
7442 * @ctxt: a Relax-NG parser context
7443 *
7444 * parse a schema definition resource and build an internal
7445 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007446 *
7447 * Returns the internal XML RelaxNG structure built from the resource or
7448 * NULL in case of error
7449 */
7450xmlRelaxNGPtr
7451xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7452{
7453 xmlRelaxNGPtr ret = NULL;
7454 xmlDocPtr doc;
7455 xmlNodePtr root;
7456
7457 xmlRelaxNGInitTypes();
7458
7459 if (ctxt == NULL)
7460 return (NULL);
7461
7462 /*
7463 * First step is to parse the input document into an DOM/Infoset
7464 */
7465 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007466 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007467 if (doc == NULL) {
7468 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7469 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7470 NULL);
7471 return (NULL);
7472 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007473 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007474 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007475 if (doc == NULL) {
7476 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7477 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7478 NULL);
7479 return (NULL);
7480 }
7481 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7482 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007483 } else if (ctxt->document != NULL) {
7484 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007485 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007486 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7487 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7488 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007489 }
7490 ctxt->document = doc;
7491
7492 /*
7493 * Some preprocessing of the document content
7494 */
7495 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7496 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007497 xmlFreeDoc(ctxt->document);
7498 ctxt->document = NULL;
7499 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007500 }
7501
Daniel Veillard6eadf632003-01-23 18:29:16 +00007502 /*
7503 * Then do the parsing for good
7504 */
7505 root = xmlDocGetRootElement(doc);
7506 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007507 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7508 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007509 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillard3f845a92006-04-13 07:33:44 +00007510
7511 xmlFreeDoc(ctxt->document);
7512 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007513 return (NULL);
7514 }
7515 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007516 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007517 xmlFreeDoc(ctxt->document);
7518 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007519 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007520 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007521
7522 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007523 * Check the ref/defines links
7524 */
7525 /*
7526 * try to preprocess interleaves
7527 */
7528 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007529 xmlHashScan(ctxt->interleaves,
7530 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007531 }
7532
7533 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007534 * if there was a parsing error return NULL
7535 */
7536 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007537 xmlRelaxNGFree(ret);
7538 ctxt->document = NULL;
7539 xmlFreeDoc(doc);
7540 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007541 }
7542
7543 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007544 * try to compile (parts of) the schemas
7545 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007546 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7547 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007548 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007549
Daniel Veillard4c004142003-10-07 11:33:24 +00007550 def = xmlRelaxNGNewDefine(ctxt, NULL);
7551 if (def != NULL) {
7552 def->type = XML_RELAXNG_START;
7553 def->content = ret->topgrammar->start;
7554 ret->topgrammar->start = def;
7555 }
7556 }
7557 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007558 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007559
7560 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007561 * Transfer the pointer for cleanup at the schema level.
7562 */
7563 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007564 ctxt->document = NULL;
7565 ret->documents = ctxt->documents;
7566 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007567
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007568 ret->includes = ctxt->includes;
7569 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007570 ret->defNr = ctxt->defNr;
7571 ret->defTab = ctxt->defTab;
7572 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007573 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007574 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007575
7576 return (ret);
7577}
Daniel Veillard4c004142003-10-07 11:33:24 +00007578
Daniel Veillard6eadf632003-01-23 18:29:16 +00007579/**
7580 * xmlRelaxNGSetParserErrors:
7581 * @ctxt: a Relax-NG validation context
7582 * @err: the error callback
7583 * @warn: the warning callback
7584 * @ctx: contextual data for the callbacks
7585 *
7586 * Set the callback functions used to handle errors for a validation context
7587 */
7588void
7589xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007590 xmlRelaxNGValidityErrorFunc err,
7591 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7592{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007593 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007595 ctxt->error = err;
7596 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007597 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007598 ctxt->userData = ctx;
7599}
Daniel Veillard409a8142003-07-18 15:16:57 +00007600
7601/**
7602 * xmlRelaxNGGetParserErrors:
7603 * @ctxt: a Relax-NG validation context
7604 * @err: the error callback result
7605 * @warn: the warning callback result
7606 * @ctx: contextual data for the callbacks result
7607 *
7608 * Get the callback information used to handle errors for a validation context
7609 *
7610 * Returns -1 in case of failure, 0 otherwise.
7611 */
7612int
7613xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007614 xmlRelaxNGValidityErrorFunc * err,
7615 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7616{
Daniel Veillard409a8142003-07-18 15:16:57 +00007617 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007618 return (-1);
7619 if (err != NULL)
7620 *err = ctxt->error;
7621 if (warn != NULL)
7622 *warn = ctxt->warning;
7623 if (ctx != NULL)
7624 *ctx = ctxt->userData;
7625 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007626}
7627
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007628/**
7629 * xmlRelaxNGSetParserStructuredErrors:
7630 * @ctxt: a Relax-NG parser context
7631 * @serror: the error callback
7632 * @ctx: contextual data for the callbacks
7633 *
7634 * Set the callback functions used to handle errors for a parsing context
7635 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007636void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007637xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007638 xmlStructuredErrorFunc serror,
7639 void *ctx)
7640{
7641 if (ctxt == NULL)
7642 return;
7643 ctxt->serror = serror;
7644 ctxt->error = NULL;
7645 ctxt->warning = NULL;
7646 ctxt->userData = ctx;
7647}
7648
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007649#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007650
Daniel Veillard6eadf632003-01-23 18:29:16 +00007651/************************************************************************
7652 * *
7653 * Dump back a compiled form *
7654 * *
7655 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007656static void xmlRelaxNGDumpDefine(FILE * output,
7657 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007658
7659/**
7660 * xmlRelaxNGDumpDefines:
7661 * @output: the file output
7662 * @defines: a list of define structures
7663 *
7664 * Dump a RelaxNG structure back
7665 */
7666static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007667xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7668{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007669 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007670 xmlRelaxNGDumpDefine(output, defines);
7671 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007672 }
7673}
7674
7675/**
7676 * xmlRelaxNGDumpDefine:
7677 * @output: the file output
7678 * @define: a define structure
7679 *
7680 * Dump a RelaxNG structure back
7681 */
7682static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007683xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7684{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007685 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007686 return;
7687 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007688 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007689 fprintf(output, "<empty/>\n");
7690 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007691 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007692 fprintf(output, "<notAllowed/>\n");
7693 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007694 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007695 fprintf(output, "<text/>\n");
7696 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007697 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007698 fprintf(output, "<element>\n");
7699 if (define->name != NULL) {
7700 fprintf(output, "<name");
7701 if (define->ns != NULL)
7702 fprintf(output, " ns=\"%s\"", define->ns);
7703 fprintf(output, ">%s</name>\n", define->name);
7704 }
7705 xmlRelaxNGDumpDefines(output, define->attrs);
7706 xmlRelaxNGDumpDefines(output, define->content);
7707 fprintf(output, "</element>\n");
7708 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007709 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007710 fprintf(output, "<list>\n");
7711 xmlRelaxNGDumpDefines(output, define->content);
7712 fprintf(output, "</list>\n");
7713 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007714 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007715 fprintf(output, "<oneOrMore>\n");
7716 xmlRelaxNGDumpDefines(output, define->content);
7717 fprintf(output, "</oneOrMore>\n");
7718 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007719 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007720 fprintf(output, "<zeroOrMore>\n");
7721 xmlRelaxNGDumpDefines(output, define->content);
7722 fprintf(output, "</zeroOrMore>\n");
7723 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007724 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007725 fprintf(output, "<choice>\n");
7726 xmlRelaxNGDumpDefines(output, define->content);
7727 fprintf(output, "</choice>\n");
7728 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007729 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007730 fprintf(output, "<group>\n");
7731 xmlRelaxNGDumpDefines(output, define->content);
7732 fprintf(output, "</group>\n");
7733 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007734 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007735 fprintf(output, "<interleave>\n");
7736 xmlRelaxNGDumpDefines(output, define->content);
7737 fprintf(output, "</interleave>\n");
7738 break;
7739 case XML_RELAXNG_OPTIONAL:
7740 fprintf(output, "<optional>\n");
7741 xmlRelaxNGDumpDefines(output, define->content);
7742 fprintf(output, "</optional>\n");
7743 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007744 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007745 fprintf(output, "<attribute>\n");
7746 xmlRelaxNGDumpDefines(output, define->content);
7747 fprintf(output, "</attribute>\n");
7748 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007749 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007750 fprintf(output, "<define");
7751 if (define->name != NULL)
7752 fprintf(output, " name=\"%s\"", define->name);
7753 fprintf(output, ">\n");
7754 xmlRelaxNGDumpDefines(output, define->content);
7755 fprintf(output, "</define>\n");
7756 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007757 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007758 fprintf(output, "<ref");
7759 if (define->name != NULL)
7760 fprintf(output, " name=\"%s\"", define->name);
7761 fprintf(output, ">\n");
7762 xmlRelaxNGDumpDefines(output, define->content);
7763 fprintf(output, "</ref>\n");
7764 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007765 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007766 fprintf(output, "<parentRef");
7767 if (define->name != NULL)
7768 fprintf(output, " name=\"%s\"", define->name);
7769 fprintf(output, ">\n");
7770 xmlRelaxNGDumpDefines(output, define->content);
7771 fprintf(output, "</parentRef>\n");
7772 break;
7773 case XML_RELAXNG_EXTERNALREF:
7774 fprintf(output, "<externalRef>");
7775 xmlRelaxNGDumpDefines(output, define->content);
7776 fprintf(output, "</externalRef>\n");
7777 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007778 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007779 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007780 TODO break;
7781 case XML_RELAXNG_START:
7782 case XML_RELAXNG_EXCEPT:
7783 case XML_RELAXNG_PARAM:
7784 TODO break;
7785 case XML_RELAXNG_NOOP:
7786 xmlRelaxNGDumpDefines(output, define->content);
7787 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007788 }
7789}
Daniel Veillard4c004142003-10-07 11:33:24 +00007790
Daniel Veillard6eadf632003-01-23 18:29:16 +00007791/**
7792 * xmlRelaxNGDumpGrammar:
7793 * @output: the file output
7794 * @grammar: a grammar structure
7795 * @top: is this a top grammar
7796 *
7797 * Dump a RelaxNG structure back
7798 */
7799static void
7800xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7801{
7802 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007803 return;
7804
Daniel Veillard6eadf632003-01-23 18:29:16 +00007805 fprintf(output, "<grammar");
7806 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007807 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7808 switch (grammar->combine) {
7809 case XML_RELAXNG_COMBINE_UNDEFINED:
7810 break;
7811 case XML_RELAXNG_COMBINE_CHOICE:
7812 fprintf(output, " combine=\"choice\"");
7813 break;
7814 case XML_RELAXNG_COMBINE_INTERLEAVE:
7815 fprintf(output, " combine=\"interleave\"");
7816 break;
7817 default:
7818 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007819 }
7820 fprintf(output, ">\n");
7821 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007822 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007823 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007824 fprintf(output, "<start>\n");
7825 xmlRelaxNGDumpDefine(output, grammar->start);
7826 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007827 }
7828 /* TODO ? Dump the defines ? */
7829 fprintf(output, "</grammar>\n");
7830}
7831
7832/**
7833 * xmlRelaxNGDump:
7834 * @output: the file output
7835 * @schema: a schema structure
7836 *
7837 * Dump a RelaxNG structure back
7838 */
7839void
7840xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7841{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007842 if (output == NULL)
7843 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007844 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007845 fprintf(output, "RelaxNG empty or failed to compile\n");
7846 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007847 }
7848 fprintf(output, "RelaxNG: ");
7849 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007850 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007851 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007852 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007853 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007854 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007855 }
7856 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007857 fprintf(output, "RelaxNG has no top grammar\n");
7858 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007859 }
7860 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7861}
7862
Daniel Veillardfebcca42003-02-16 15:44:18 +00007863/**
7864 * xmlRelaxNGDumpTree:
7865 * @output: the file output
7866 * @schema: a schema structure
7867 *
7868 * Dump the transformed RelaxNG tree.
7869 */
7870void
7871xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7872{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007873 if (output == NULL)
7874 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007875 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007876 fprintf(output, "RelaxNG empty or failed to compile\n");
7877 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007878 }
7879 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007880 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007881 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007882 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007883 }
7884}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007885#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007886
Daniel Veillard6eadf632003-01-23 18:29:16 +00007887/************************************************************************
7888 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007889 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007890 * *
7891 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007892static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7893 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007894
7895/**
7896 * xmlRelaxNGValidateCompiledCallback:
7897 * @exec: the regular expression instance
7898 * @token: the token which matched
7899 * @transdata: callback data, the define for the subelement if available
7900 @ @inputdata: callback data, the Relax NG validation context
7901 *
7902 * Handle the callback and if needed validate the element children.
7903 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007904static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007905xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007906 const xmlChar * token,
7907 void *transdata, void *inputdata)
7908{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007909 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7910 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7911 int ret;
7912
7913#ifdef DEBUG_COMPILE
7914 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007915 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007916#endif
7917 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007918 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007919 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007920 }
7921 if (define == NULL) {
7922 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007923 return;
7924 fprintf(stderr, "callback on %s missing define\n", token);
7925 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7926 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7927 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007928 }
7929 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007930 fprintf(stderr, "callback on %s missing info\n", token);
7931 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7932 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7933 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007934 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007935 fprintf(stderr, "callback on %s define is not element\n", token);
7936 if (ctxt->errNo == XML_RELAXNG_OK)
7937 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7938 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007939 }
7940 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007941 if (ret != 0)
7942 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007943}
7944
7945/**
7946 * xmlRelaxNGValidateCompiledContent:
7947 * @ctxt: the RelaxNG validation context
7948 * @regexp: the regular expression as compiled
7949 * @content: list of children to test against the regexp
7950 *
7951 * Validate the content model of an element or start using the regexp
7952 *
7953 * Returns 0 in case of success, -1 in case of error.
7954 */
7955static int
7956xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007957 xmlRegexpPtr regexp, xmlNodePtr content)
7958{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007959 xmlRegExecCtxtPtr exec;
7960 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007961 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007962 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007963
7964 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007965 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007966 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007967 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007968 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007969 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007970 cur = content;
7971 while (cur != NULL) {
7972 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007973 switch (cur->type) {
7974 case XML_TEXT_NODE:
7975 case XML_CDATA_SECTION_NODE:
7976 if (xmlIsBlankNode(cur))
7977 break;
7978 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7979 if (ret < 0) {
7980 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7981 cur->parent->name);
7982 }
7983 break;
7984 case XML_ELEMENT_NODE:
7985 if (cur->ns != NULL) {
7986 ret = xmlRegExecPushString2(exec, cur->name,
7987 cur->ns->href, ctxt);
7988 } else {
7989 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7990 }
7991 if (ret < 0) {
7992 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7993 }
7994 break;
7995 default:
7996 break;
7997 }
7998 if (ret < 0)
7999 break;
8000 /*
8001 * Switch to next element
8002 */
8003 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008004 }
8005 ret = xmlRegExecPushString(exec, NULL, NULL);
8006 if (ret == 1) {
8007 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008008 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008009 } else if (ret == 0) {
8010 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008011 * TODO: get some of the names needed to exit the current state of exec
8012 */
8013 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8014 ret = -1;
8015 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8016 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008017 } else {
8018 ret = -1;
8019 }
8020 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008021 /*
8022 * There might be content model errors outside of the pure
8023 * regexp validation, e.g. for attribute values.
8024 */
8025 if ((ret == 0) && (ctxt->perr != 0)) {
8026 ret = ctxt->perr;
8027 }
8028 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008029 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008030}
8031
8032/************************************************************************
8033 * *
8034 * Progressive validation of when possible *
8035 * *
8036 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008037static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8038 xmlRelaxNGDefinePtr defines);
8039static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008040 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008041static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008042
8043/**
8044 * xmlRelaxNGElemPush:
8045 * @ctxt: the validation context
8046 * @exec: the regexp runtime for the new content model
8047 *
8048 * Push a new regexp for the current node content model on the stack
8049 *
8050 * Returns 0 in case of success and -1 in case of error.
8051 */
8052static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008053xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8054{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008055 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008056 ctxt->elemMax = 10;
8057 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8058 sizeof
8059 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008060 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008061 xmlRngVErrMemory(ctxt, "validating\n");
8062 return (-1);
8063 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008064 }
8065 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008066 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008067 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008068 ctxt->elemMax *
8069 sizeof
8070 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008071 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008072 xmlRngVErrMemory(ctxt, "validating\n");
8073 return (-1);
8074 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008075 }
8076 ctxt->elemTab[ctxt->elemNr++] = exec;
8077 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008078 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008079}
8080
8081/**
8082 * xmlRelaxNGElemPop:
8083 * @ctxt: the validation context
8084 *
8085 * Pop the regexp of the current node content model from the stack
8086 *
8087 * Returns the exec or NULL if empty
8088 */
8089static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008090xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8091{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008092 xmlRegExecCtxtPtr ret;
8093
Daniel Veillard4c004142003-10-07 11:33:24 +00008094 if (ctxt->elemNr <= 0)
8095 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008096 ctxt->elemNr--;
8097 ret = ctxt->elemTab[ctxt->elemNr];
8098 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008099 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008100 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8101 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008102 ctxt->elem = NULL;
8103 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008104}
8105
8106/**
8107 * xmlRelaxNGValidateProgressiveCallback:
8108 * @exec: the regular expression instance
8109 * @token: the token which matched
8110 * @transdata: callback data, the define for the subelement if available
8111 @ @inputdata: callback data, the Relax NG validation context
8112 *
8113 * Handle the callback and if needed validate the element children.
8114 * some of the in/out informations are passed via the context in @inputdata.
8115 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008116static void
8117xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8118 ATTRIBUTE_UNUSED,
8119 const xmlChar * token,
8120 void *transdata, void *inputdata)
8121{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008122 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8123 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008124 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008125 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008126 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008127
8128#ifdef DEBUG_PROGRESSIVE
8129 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008130 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008131#endif
8132 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008133 fprintf(stderr, "callback on %s missing context\n", token);
8134 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008135 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008136 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008137 ctxt->pstate = 1;
8138 if (define == NULL) {
8139 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008140 return;
8141 fprintf(stderr, "callback on %s missing define\n", token);
8142 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8143 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8144 ctxt->pstate = -1;
8145 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008146 }
8147 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008148 fprintf(stderr, "callback on %s missing info\n", token);
8149 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8150 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8151 ctxt->pstate = -1;
8152 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008153 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008154 fprintf(stderr, "callback on %s define is not element\n", token);
8155 if (ctxt->errNo == XML_RELAXNG_OK)
8156 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8157 ctxt->pstate = -1;
8158 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008159 }
8160 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008161 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8162 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8163 xmlRelaxNGDumpValidError(ctxt);
8164 ctxt->pstate = -1;
8165 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008166 }
8167 if (define->contModel == NULL) {
8168 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008169 * this node cannot be validated in a streamable fashion
8170 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008171#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008172 xmlGenericError(xmlGenericErrorContext,
8173 "Element '%s' validation is not streamable\n",
8174 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008175#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008176 ctxt->pstate = 0;
8177 ctxt->pdef = define;
8178 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008179 }
8180 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008181 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008182 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008183 ctxt->pstate = -1;
8184 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008185 }
8186 xmlRelaxNGElemPush(ctxt, exec);
8187
8188 /*
8189 * Validate the attributes part of the content.
8190 */
8191 state = xmlRelaxNGNewValidState(ctxt, node);
8192 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008193 ctxt->pstate = -1;
8194 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008195 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008196 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008197 ctxt->state = state;
8198 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008199 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8200 if (ret != 0) {
8201 ctxt->pstate = -1;
8202 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8203 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008204 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008205 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008206 ctxt->state->seq = NULL;
8207 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8208 if (ret != 0) {
8209 ctxt->pstate = -1;
8210 }
8211 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008212 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008213 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008214
8215 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008216
Daniel Veillard4c004142003-10-07 11:33:24 +00008217 for (i = 0; i < ctxt->states->nbState; i++) {
8218 state = ctxt->states->tabState[i];
8219 ctxt->state = state;
8220 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008221
Daniel Veillard4c004142003-10-07 11:33:24 +00008222 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8223 tmp = 0;
8224 break;
8225 }
8226 }
8227 if (tmp != 0) {
8228 /*
8229 * validation error, log the message for the "best" one
8230 */
8231 ctxt->flags |= FLAGS_IGNORABLE;
8232 xmlRelaxNGLogBestError(ctxt);
8233 }
8234 for (i = 0; i < ctxt->states->nbState; i++) {
8235 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8236 }
8237 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8238 ctxt->states = NULL;
8239 if ((ret == 0) && (tmp == -1))
8240 ctxt->pstate = -1;
8241 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008242 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008243 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008244 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8245 xmlRelaxNGDumpValidError(ctxt);
8246 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008247 }
8248 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008249}
8250
8251/**
8252 * xmlRelaxNGValidatePushElement:
8253 * @ctxt: the validation context
8254 * @doc: a document instance
8255 * @elem: an element instance
8256 *
8257 * Push a new element start on the RelaxNG validation stack.
8258 *
8259 * returns 1 if no validation problem was found or 0 if validating the
8260 * element requires a full node, and -1 in case of error.
8261 */
8262int
Daniel Veillard33300b42003-04-17 09:09:19 +00008263xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8264 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008265 xmlNodePtr elem)
8266{
8267 int ret = 1;
8268
8269 if ((ctxt == NULL) || (elem == NULL))
8270 return (-1);
8271
8272#ifdef DEBUG_PROGRESSIVE
8273 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8274#endif
8275 if (ctxt->elem == 0) {
8276 xmlRelaxNGPtr schema;
8277 xmlRelaxNGGrammarPtr grammar;
8278 xmlRegExecCtxtPtr exec;
8279 xmlRelaxNGDefinePtr define;
8280
8281 schema = ctxt->schema;
8282 if (schema == NULL) {
8283 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8284 return (-1);
8285 }
8286 grammar = schema->topgrammar;
8287 if ((grammar == NULL) || (grammar->start == NULL)) {
8288 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8289 return (-1);
8290 }
8291 define = grammar->start;
8292 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008293 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008294 return (0);
8295 }
8296 exec = xmlRegNewExecCtxt(define->contModel,
8297 xmlRelaxNGValidateProgressiveCallback,
8298 ctxt);
8299 if (exec == NULL) {
8300 return (-1);
8301 }
8302 xmlRelaxNGElemPush(ctxt, exec);
8303 }
8304 ctxt->pnode = elem;
8305 ctxt->pstate = 0;
8306 if (elem->ns != NULL) {
8307 ret =
8308 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8309 ctxt);
8310 } else {
8311 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8312 }
8313 if (ret < 0) {
8314 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8315 } else {
8316 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008317 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008318 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008319 ret = -1;
8320 else
8321 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008322 }
8323#ifdef DEBUG_PROGRESSIVE
8324 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008325 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8326 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008327#endif
8328 return (ret);
8329}
8330
8331/**
8332 * xmlRelaxNGValidatePushCData:
8333 * @ctxt: the RelaxNG validation context
8334 * @data: some character data read
8335 * @len: the lenght of the data
8336 *
8337 * check the CData parsed for validation in the current stack
8338 *
8339 * returns 1 if no validation problem was found or -1 otherwise
8340 */
8341int
8342xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008343 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008344{
8345 int ret = 1;
8346
8347 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8348 return (-1);
8349
8350#ifdef DEBUG_PROGRESSIVE
8351 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8352#endif
8353
8354 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008355 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008356 break;
8357 data++;
8358 }
8359 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008360 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008361
8362 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8363 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008364 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008365#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008366 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008367#endif
8368
Daniel Veillard4c004142003-10-07 11:33:24 +00008369 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008370 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008371 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008372}
8373
8374/**
8375 * xmlRelaxNGValidatePopElement:
8376 * @ctxt: the RelaxNG validation context
8377 * @doc: a document instance
8378 * @elem: an element instance
8379 *
8380 * Pop the element end from the RelaxNG validation stack.
8381 *
8382 * returns 1 if no validation problem was found or 0 otherwise
8383 */
8384int
8385xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8386 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008387 xmlNodePtr elem)
8388{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008389 int ret;
8390 xmlRegExecCtxtPtr exec;
8391
Daniel Veillard4c004142003-10-07 11:33:24 +00008392 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8393 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008394#ifdef DEBUG_PROGRESSIVE
8395 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8396#endif
8397 /*
8398 * verify that we reached a terminal state of the content model.
8399 */
8400 exec = xmlRelaxNGElemPop(ctxt);
8401 ret = xmlRegExecPushString(exec, NULL, NULL);
8402 if (ret == 0) {
8403 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008404 * TODO: get some of the names needed to exit the current state of exec
8405 */
8406 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8407 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008408 } else if (ret < 0) {
8409 ret = -1;
8410 } else {
8411 ret = 1;
8412 }
8413 xmlRegFreeExecCtxt(exec);
8414#ifdef DEBUG_PROGRESSIVE
8415 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008416 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8417 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008418#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008419 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008420}
8421
8422/**
8423 * xmlRelaxNGValidateFullElement:
8424 * @ctxt: the validation context
8425 * @doc: a document instance
8426 * @elem: an element instance
8427 *
8428 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8429 * 0 and the content of the node has been expanded.
8430 *
8431 * returns 1 if no validation problem was found or -1 in case of error.
8432 */
8433int
Daniel Veillard33300b42003-04-17 09:09:19 +00008434xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8435 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008436 xmlNodePtr elem)
8437{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008438 int ret;
8439 xmlRelaxNGValidStatePtr state;
8440
Daniel Veillard4c004142003-10-07 11:33:24 +00008441 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8442 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008443#ifdef DEBUG_PROGRESSIVE
8444 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8445#endif
8446 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8447 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008448 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008449 }
8450 state->seq = elem;
8451 ctxt->state = state;
8452 ctxt->errNo = XML_RELAXNG_OK;
8453 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008454 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8455 ret = -1;
8456 else
8457 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008458 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008459 ctxt->state = NULL;
8460#ifdef DEBUG_PROGRESSIVE
8461 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008462 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8463 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008464#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008465 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008466}
8467
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008468/************************************************************************
8469 * *
8470 * Generic interpreted validation implementation *
8471 * *
8472 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008473static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8474 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008475
8476/**
8477 * xmlRelaxNGSkipIgnored:
8478 * @ctxt: a schema validation context
8479 * @node: the top node.
8480 *
8481 * Skip ignorable nodes in that context
8482 *
8483 * Returns the new sibling or NULL in case of error.
8484 */
8485static xmlNodePtr
8486xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008487 xmlNodePtr node)
8488{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008489 /*
8490 * TODO complete and handle entities
8491 */
8492 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008493 ((node->type == XML_COMMENT_NODE) ||
8494 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008495 (node->type == XML_XINCLUDE_START) ||
8496 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008497 (((node->type == XML_TEXT_NODE) ||
8498 (node->type == XML_CDATA_SECTION_NODE)) &&
8499 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8500 (IS_BLANK_NODE(node)))))) {
8501 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008502 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008503 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008504}
8505
8506/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008507 * xmlRelaxNGNormalize:
8508 * @ctxt: a schema validation context
8509 * @str: the string to normalize
8510 *
8511 * Implements the normalizeWhiteSpace( s ) function from
8512 * section 6.2.9 of the spec
8513 *
8514 * Returns the new string or NULL in case of error.
8515 */
8516static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008517xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8518{
Daniel Veillardedc91922003-01-26 00:52:04 +00008519 xmlChar *ret, *p;
8520 const xmlChar *tmp;
8521 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008522
Daniel Veillardedc91922003-01-26 00:52:04 +00008523 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008524 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008525 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008526 while (*tmp != 0)
8527 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008528 len = tmp - str;
8529
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008530 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008531 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008532 xmlRngVErrMemory(ctxt, "validating\n");
8533 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008534 }
8535 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008536 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008537 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008538 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008539 if (IS_BLANK_CH(*str)) {
8540 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008541 str++;
8542 if (*str == 0)
8543 break;
8544 *p++ = ' ';
8545 } else
8546 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008547 }
8548 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008549 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008550}
8551
8552/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008553 * xmlRelaxNGValidateDatatype:
8554 * @ctxt: a Relax-NG validation context
8555 * @value: the string value
8556 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008557 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008558 *
8559 * Validate the given value against the dataype
8560 *
8561 * Returns 0 if the validation succeeded or an error code.
8562 */
8563static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008564xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8565 const xmlChar * value,
8566 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8567{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008568 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008569 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008570 void *result = NULL;
8571 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008572
8573 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008574 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008575 }
8576 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008577 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008578 if ((define->attrs != NULL) &&
8579 (define->attrs->type == XML_RELAXNG_PARAM)) {
8580 ret =
8581 lib->check(lib->data, define->name, value, &result, node);
8582 } else {
8583 ret = lib->check(lib->data, define->name, value, NULL, node);
8584 }
8585 } else
8586 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008587 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008588 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8589 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8590 lib->freef(lib->data, result);
8591 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008592 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008593 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008594 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008595 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008596 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008597 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8598 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008599 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008600 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008601 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008602 if (lib->facet != NULL) {
8603 tmp = lib->facet(lib->data, define->name, cur->name,
8604 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008605 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008606 ret = -1;
8607 }
8608 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008609 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008610 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008611 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008612
Daniel Veillard4c004142003-10-07 11:33:24 +00008613 oldvalue = ctxt->state->value;
8614 oldendvalue = ctxt->state->endvalue;
8615 ctxt->state->value = (xmlChar *) value;
8616 ctxt->state->endvalue = NULL;
8617 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8618 ctxt->state->value = (xmlChar *) oldvalue;
8619 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008620 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008621 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008622 lib->freef(lib->data, result);
8623 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008624}
8625
8626/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008627 * xmlRelaxNGNextValue:
8628 * @ctxt: a Relax-NG validation context
8629 *
8630 * Skip to the next value when validating within a list
8631 *
8632 * Returns 0 if the operation succeeded or an error code.
8633 */
8634static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008635xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8636{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008637 xmlChar *cur;
8638
8639 cur = ctxt->state->value;
8640 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008641 ctxt->state->value = NULL;
8642 ctxt->state->endvalue = NULL;
8643 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008644 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008645 while (*cur != 0)
8646 cur++;
8647 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8648 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008649 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008650 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008651 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008652 ctxt->state->value = cur;
8653 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008654}
8655
8656/**
8657 * xmlRelaxNGValidateValueList:
8658 * @ctxt: a Relax-NG validation context
8659 * @defines: the list of definitions to verify
8660 *
8661 * Validate the given set of definitions for the current value
8662 *
8663 * Returns 0 if the validation succeeded or an error code.
8664 */
8665static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008666xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8667 xmlRelaxNGDefinePtr defines)
8668{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008669 int ret = 0;
8670
8671 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008672 ret = xmlRelaxNGValidateValue(ctxt, defines);
8673 if (ret != 0)
8674 break;
8675 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008676 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008677 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008678}
8679
8680/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008681 * xmlRelaxNGValidateValue:
8682 * @ctxt: a Relax-NG validation context
8683 * @define: the definition to verify
8684 *
8685 * Validate the given definition for the current value
8686 *
8687 * Returns 0 if the validation succeeded or an error code.
8688 */
8689static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008690xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8691 xmlRelaxNGDefinePtr define)
8692{
Daniel Veillardedc91922003-01-26 00:52:04 +00008693 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008694 xmlChar *value;
8695
8696 value = ctxt->state->value;
8697 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008698 case XML_RELAXNG_EMPTY:{
8699 if ((value != NULL) && (value[0] != 0)) {
8700 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008701
William M. Brack76e95df2003-10-18 16:20:14 +00008702 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008703 idx++;
8704 if (value[idx] != 0)
8705 ret = -1;
8706 }
8707 break;
8708 }
8709 case XML_RELAXNG_TEXT:
8710 break;
8711 case XML_RELAXNG_VALUE:{
8712 if (!xmlStrEqual(value, define->value)) {
8713 if (define->name != NULL) {
8714 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008715
Daniel Veillard4c004142003-10-07 11:33:24 +00008716 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8717 if ((lib != NULL) && (lib->comp != NULL)) {
8718 ret = lib->comp(lib->data, define->name,
8719 define->value, define->node,
8720 (void *) define->attrs,
8721 value, ctxt->state->node);
8722 } else
8723 ret = -1;
8724 if (ret < 0) {
8725 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8726 define->name);
8727 return (-1);
8728 } else if (ret == 1) {
8729 ret = 0;
8730 } else {
8731 ret = -1;
8732 }
8733 } else {
8734 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008735
Daniel Veillard4c004142003-10-07 11:33:24 +00008736 /*
8737 * TODO: trivial optimizations are possible by
8738 * computing at compile-time
8739 */
8740 nval = xmlRelaxNGNormalize(ctxt, define->value);
8741 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008742
Daniel Veillard4c004142003-10-07 11:33:24 +00008743 if ((nval == NULL) || (nvalue == NULL) ||
8744 (!xmlStrEqual(nval, nvalue)))
8745 ret = -1;
8746 if (nval != NULL)
8747 xmlFree(nval);
8748 if (nvalue != NULL)
8749 xmlFree(nvalue);
8750 }
8751 }
8752 if (ret == 0)
8753 xmlRelaxNGNextValue(ctxt);
8754 break;
8755 }
8756 case XML_RELAXNG_DATATYPE:{
8757 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8758 ctxt->state->seq);
8759 if (ret == 0)
8760 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008761
Daniel Veillard4c004142003-10-07 11:33:24 +00008762 break;
8763 }
8764 case XML_RELAXNG_CHOICE:{
8765 xmlRelaxNGDefinePtr list = define->content;
8766 xmlChar *oldvalue;
8767
8768 oldflags = ctxt->flags;
8769 ctxt->flags |= FLAGS_IGNORABLE;
8770
8771 oldvalue = ctxt->state->value;
8772 while (list != NULL) {
8773 ret = xmlRelaxNGValidateValue(ctxt, list);
8774 if (ret == 0) {
8775 break;
8776 }
8777 ctxt->state->value = oldvalue;
8778 list = list->next;
8779 }
8780 ctxt->flags = oldflags;
8781 if (ret != 0) {
8782 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8783 xmlRelaxNGDumpValidError(ctxt);
8784 } else {
8785 if (ctxt->errNr > 0)
8786 xmlRelaxNGPopErrors(ctxt, 0);
8787 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008788 break;
8789 }
8790 case XML_RELAXNG_LIST:{
8791 xmlRelaxNGDefinePtr list = define->content;
8792 xmlChar *oldvalue, *oldend, *val, *cur;
8793
Daniel Veillard416589a2003-02-17 17:25:42 +00008794#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008795 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008796#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008797
Daniel Veillard4c004142003-10-07 11:33:24 +00008798 oldvalue = ctxt->state->value;
8799 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008800
Daniel Veillard4c004142003-10-07 11:33:24 +00008801 val = xmlStrdup(oldvalue);
8802 if (val == NULL) {
8803 val = xmlStrdup(BAD_CAST "");
8804 }
8805 if (val == NULL) {
8806 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8807 return (-1);
8808 }
8809 cur = val;
8810 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008811 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008812 *cur = 0;
8813 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008814#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008815 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008816#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008817 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008818 *cur++ = 0;
8819 } else
8820 cur++;
8821 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008822#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008823 xmlGenericError(xmlGenericErrorContext,
8824 "list value: '%s' found %d items\n",
8825 oldvalue, nb_values);
8826 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008827#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008828 ctxt->state->endvalue = cur;
8829 cur = val;
8830 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8831 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008832
Daniel Veillard4c004142003-10-07 11:33:24 +00008833 ctxt->state->value = cur;
8834
8835 while (list != NULL) {
8836 if (ctxt->state->value == ctxt->state->endvalue)
8837 ctxt->state->value = NULL;
8838 ret = xmlRelaxNGValidateValue(ctxt, list);
8839 if (ret != 0) {
8840#ifdef DEBUG_LIST
8841 xmlGenericError(xmlGenericErrorContext,
8842 "Failed to validate value: '%s' with %d rule\n",
8843 ctxt->state->value, nb_values);
8844#endif
8845 break;
8846 }
8847#ifdef DEBUG_LIST
8848 nb_values++;
8849#endif
8850 list = list->next;
8851 }
8852
8853 if ((ret == 0) && (ctxt->state->value != NULL) &&
8854 (ctxt->state->value != ctxt->state->endvalue)) {
8855 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8856 ctxt->state->value);
8857 ret = -1;
8858 }
8859 xmlFree(val);
8860 ctxt->state->value = oldvalue;
8861 ctxt->state->endvalue = oldend;
8862 break;
8863 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008864 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008865 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8866 if (ret != 0) {
8867 break;
8868 }
8869 /* no break on purpose */
8870 case XML_RELAXNG_ZEROORMORE:{
8871 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008872
Daniel Veillard4c004142003-10-07 11:33:24 +00008873 oldflags = ctxt->flags;
8874 ctxt->flags |= FLAGS_IGNORABLE;
8875 cur = ctxt->state->value;
8876 temp = NULL;
8877 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8878 (temp != cur)) {
8879 temp = cur;
8880 ret =
8881 xmlRelaxNGValidateValueList(ctxt, define->content);
8882 if (ret != 0) {
8883 ctxt->state->value = temp;
8884 ret = 0;
8885 break;
8886 }
8887 cur = ctxt->state->value;
8888 }
8889 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008890 if (ctxt->errNr > 0)
8891 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008892 break;
8893 }
8894 case XML_RELAXNG_EXCEPT:{
8895 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008896
Daniel Veillard4c004142003-10-07 11:33:24 +00008897 list = define->content;
8898 while (list != NULL) {
8899 ret = xmlRelaxNGValidateValue(ctxt, list);
8900 if (ret == 0) {
8901 ret = -1;
8902 break;
8903 } else
8904 ret = 0;
8905 list = list->next;
8906 }
8907 break;
8908 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008909 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008910 case XML_RELAXNG_GROUP:{
8911 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008912
Daniel Veillard4c004142003-10-07 11:33:24 +00008913 list = define->content;
8914 while (list != NULL) {
8915 ret = xmlRelaxNGValidateValue(ctxt, list);
8916 if (ret != 0) {
8917 ret = -1;
8918 break;
8919 } else
8920 ret = 0;
8921 list = list->next;
8922 }
8923 break;
8924 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008925 case XML_RELAXNG_REF:
8926 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008927 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008928 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8929 ret = -1;
8930 } else {
8931 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8932 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008933 break;
8934 default:
8935 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008936 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008937 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008938}
8939
8940/**
8941 * xmlRelaxNGValidateValueContent:
8942 * @ctxt: a Relax-NG validation context
8943 * @defines: the list of definitions to verify
8944 *
8945 * Validate the given definitions for the current value
8946 *
8947 * Returns 0 if the validation succeeded or an error code.
8948 */
8949static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008950xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8951 xmlRelaxNGDefinePtr defines)
8952{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008953 int ret = 0;
8954
8955 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008956 ret = xmlRelaxNGValidateValue(ctxt, defines);
8957 if (ret != 0)
8958 break;
8959 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008960 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008961 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008962}
8963
8964/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008965 * xmlRelaxNGAttributeMatch:
8966 * @ctxt: a Relax-NG validation context
8967 * @define: the definition to check
8968 * @prop: the attribute
8969 *
8970 * Check if the attribute matches the definition nameClass
8971 *
8972 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8973 */
8974static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008975xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8976 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8977{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008978 int ret;
8979
8980 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008981 if (!xmlStrEqual(define->name, prop->name))
8982 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008983 }
8984 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008985 if (define->ns[0] == 0) {
8986 if (prop->ns != NULL)
8987 return (0);
8988 } else {
8989 if ((prop->ns == NULL) ||
8990 (!xmlStrEqual(define->ns, prop->ns->href)))
8991 return (0);
8992 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008993 }
8994 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008995 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008996 define = define->nameClass;
8997 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008998 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008999
Daniel Veillard4c004142003-10-07 11:33:24 +00009000 list = define->content;
9001 while (list != NULL) {
9002 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9003 if (ret == 1)
9004 return (0);
9005 if (ret < 0)
9006 return (ret);
9007 list = list->next;
9008 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009009 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009010 TODO}
9011 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009012}
9013
9014/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009015 * xmlRelaxNGValidateAttribute:
9016 * @ctxt: a Relax-NG validation context
9017 * @define: the definition to verify
9018 *
9019 * Validate the given attribute definition for that node
9020 *
9021 * Returns 0 if the validation succeeded or an error code.
9022 */
9023static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009024xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9025 xmlRelaxNGDefinePtr define)
9026{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009027 int ret = 0, i;
9028 xmlChar *value, *oldvalue;
9029 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009030 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009031
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009032 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009033 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009034 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009035 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9036 tmp = ctxt->state->attrs[i];
9037 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9038 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9039 (tmp->ns == NULL)) ||
9040 ((tmp->ns != NULL) &&
9041 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9042 prop = tmp;
9043 break;
9044 }
9045 }
9046 }
9047 if (prop != NULL) {
9048 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9049 oldvalue = ctxt->state->value;
9050 oldseq = ctxt->state->seq;
9051 ctxt->state->seq = (xmlNodePtr) prop;
9052 ctxt->state->value = value;
9053 ctxt->state->endvalue = NULL;
9054 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9055 if (ctxt->state->value != NULL)
9056 value = ctxt->state->value;
9057 if (value != NULL)
9058 xmlFree(value);
9059 ctxt->state->value = oldvalue;
9060 ctxt->state->seq = oldseq;
9061 if (ret == 0) {
9062 /*
9063 * flag the attribute as processed
9064 */
9065 ctxt->state->attrs[i] = NULL;
9066 ctxt->state->nbAttrLeft--;
9067 }
9068 } else {
9069 ret = -1;
9070 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009071#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009072 xmlGenericError(xmlGenericErrorContext,
9073 "xmlRelaxNGValidateAttribute(%s): %d\n",
9074 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009075#endif
9076 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009077 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9078 tmp = ctxt->state->attrs[i];
9079 if ((tmp != NULL) &&
9080 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9081 prop = tmp;
9082 break;
9083 }
9084 }
9085 if (prop != NULL) {
9086 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9087 oldvalue = ctxt->state->value;
9088 oldseq = ctxt->state->seq;
9089 ctxt->state->seq = (xmlNodePtr) prop;
9090 ctxt->state->value = value;
9091 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9092 if (ctxt->state->value != NULL)
9093 value = ctxt->state->value;
9094 if (value != NULL)
9095 xmlFree(value);
9096 ctxt->state->value = oldvalue;
9097 ctxt->state->seq = oldseq;
9098 if (ret == 0) {
9099 /*
9100 * flag the attribute as processed
9101 */
9102 ctxt->state->attrs[i] = NULL;
9103 ctxt->state->nbAttrLeft--;
9104 }
9105 } else {
9106 ret = -1;
9107 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009108#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009109 if (define->ns != NULL) {
9110 xmlGenericError(xmlGenericErrorContext,
9111 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9112 define->ns, ret);
9113 } else {
9114 xmlGenericError(xmlGenericErrorContext,
9115 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9116 ret);
9117 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009118#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009119 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009120
9121 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009122}
9123
9124/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009125 * xmlRelaxNGValidateAttributeList:
9126 * @ctxt: a Relax-NG validation context
9127 * @define: the list of definition to verify
9128 *
9129 * Validate the given node against the list of attribute definitions
9130 *
9131 * Returns 0 if the validation succeeded or an error code.
9132 */
9133static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009134xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9135 xmlRelaxNGDefinePtr defines)
9136{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009137 int ret = 0, res;
9138 int needmore = 0;
9139 xmlRelaxNGDefinePtr cur;
9140
9141 cur = defines;
9142 while (cur != NULL) {
9143 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009144 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9145 ret = -1;
9146 } else
9147 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009148 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009149 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009150 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009151 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009152 cur = defines;
9153 while (cur != NULL) {
9154 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009155 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9156 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9157 if (res < 0)
9158 ret = -1;
9159 } else {
9160 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9161 return (-1);
9162 }
9163 if (res == -1) /* continues on -2 */
9164 break;
9165 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009166 cur = cur->next;
9167 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009168
9169 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009170}
9171
9172/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009173 * xmlRelaxNGNodeMatchesList:
9174 * @node: the node
9175 * @list: a NULL terminated array of definitions
9176 *
9177 * Check if a node can be matched by one of the definitions
9178 *
9179 * Returns 1 if matches 0 otherwise
9180 */
9181static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009182xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9183{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009184 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009185 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009186
9187 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009188 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009189
9190 cur = list[i++];
9191 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009192 if ((node->type == XML_ELEMENT_NODE) &&
9193 (cur->type == XML_RELAXNG_ELEMENT)) {
9194 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9195 if (tmp == 1)
9196 return (1);
9197 } else if (((node->type == XML_TEXT_NODE) ||
9198 (node->type == XML_CDATA_SECTION_NODE)) &&
9199 (cur->type == XML_RELAXNG_TEXT)) {
9200 return (1);
9201 }
9202 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009203 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009204 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009205}
9206
9207/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009208 * xmlRelaxNGValidateInterleave:
9209 * @ctxt: a Relax-NG validation context
9210 * @define: the definition to verify
9211 *
9212 * Validate an interleave definition for a node.
9213 *
9214 * Returns 0 if the validation succeeded or an error code.
9215 */
9216static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009217xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9218 xmlRelaxNGDefinePtr define)
9219{
William M. Brack779af002003-08-01 15:55:39 +00009220 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009221 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009222 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009223
9224 xmlRelaxNGValidStatePtr oldstate;
9225 xmlRelaxNGPartitionPtr partitions;
9226 xmlRelaxNGInterleaveGroupPtr group = NULL;
9227 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9228 xmlNodePtr *list = NULL, *lasts = NULL;
9229
9230 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009231 partitions = (xmlRelaxNGPartitionPtr) define->data;
9232 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009233 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009234 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9235 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009236 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009237 /*
9238 * Optimizations for MIXED
9239 */
9240 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009241 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009242 ctxt->flags |= FLAGS_MIXED_CONTENT;
9243 if (nbgroups == 2) {
9244 /*
9245 * this is a pure <mixed> case
9246 */
9247 if (ctxt->state != NULL)
9248 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9249 ctxt->state->seq);
9250 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9251 ret = xmlRelaxNGValidateDefinition(ctxt,
9252 partitions->groups[1]->
9253 rule);
9254 else
9255 ret = xmlRelaxNGValidateDefinition(ctxt,
9256 partitions->groups[0]->
9257 rule);
9258 if (ret == 0) {
9259 if (ctxt->state != NULL)
9260 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9261 ctxt->state->
9262 seq);
9263 }
9264 ctxt->flags = oldflags;
9265 return (ret);
9266 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009267 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009268
9269 /*
9270 * Build arrays to store the first and last node of the chain
9271 * pertaining to each group
9272 */
9273 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9274 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009275 xmlRngVErrMemory(ctxt, "validating\n");
9276 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009277 }
9278 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9279 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9280 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009281 xmlRngVErrMemory(ctxt, "validating\n");
9282 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009283 }
9284 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9285
9286 /*
9287 * Walk the sequence of children finding the right group and
9288 * sorting them in sequences.
9289 */
9290 cur = ctxt->state->seq;
9291 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9292 start = cur;
9293 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009294 ctxt->state->seq = cur;
9295 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009296 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009297 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009298
Daniel Veillard4c004142003-10-07 11:33:24 +00009299 if ((cur->type == XML_TEXT_NODE) ||
9300 (cur->type == XML_CDATA_SECTION_NODE)) {
9301 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9302 NULL);
9303 } else if (cur->type == XML_ELEMENT_NODE) {
9304 if (cur->ns != NULL) {
9305 tmp = xmlHashLookup2(partitions->triage, cur->name,
9306 cur->ns->href);
9307 if (tmp == NULL)
9308 tmp = xmlHashLookup2(partitions->triage,
9309 BAD_CAST "#any",
9310 cur->ns->href);
9311 } else
9312 tmp =
9313 xmlHashLookup2(partitions->triage, cur->name,
9314 NULL);
9315 if (tmp == NULL)
9316 tmp =
9317 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9318 NULL);
9319 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009320
Daniel Veillard4c004142003-10-07 11:33:24 +00009321 if (tmp == NULL) {
9322 i = nbgroups;
9323 } else {
9324 i = ((long) tmp) - 1;
9325 if (partitions->flags & IS_NEEDCHECK) {
9326 group = partitions->groups[i];
9327 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9328 i = nbgroups;
9329 }
9330 }
9331 } else {
9332 for (i = 0; i < nbgroups; i++) {
9333 group = partitions->groups[i];
9334 if (group == NULL)
9335 continue;
9336 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9337 break;
9338 }
9339 }
9340 /*
9341 * We break as soon as an element not matched is found
9342 */
9343 if (i >= nbgroups) {
9344 break;
9345 }
9346 if (lasts[i] != NULL) {
9347 lasts[i]->next = cur;
9348 lasts[i] = cur;
9349 } else {
9350 list[i] = cur;
9351 lasts[i] = cur;
9352 }
9353 if (cur->next != NULL)
9354 lastchg = cur->next;
9355 else
9356 lastchg = cur;
9357 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009358 }
9359 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009360 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9361 ret = -1;
9362 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009363 }
9364 lastelem = cur;
9365 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009366 for (i = 0; i < nbgroups; i++) {
9367 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9368 group = partitions->groups[i];
9369 if (lasts[i] != NULL) {
9370 last = lasts[i]->next;
9371 lasts[i]->next = NULL;
9372 }
9373 ctxt->state->seq = list[i];
9374 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9375 if (ret != 0)
9376 break;
9377 if (ctxt->state != NULL) {
9378 cur = ctxt->state->seq;
9379 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9380 xmlRelaxNGFreeValidState(ctxt, oldstate);
9381 oldstate = ctxt->state;
9382 ctxt->state = NULL;
9383 if (cur != NULL) {
9384 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9385 ret = -1;
9386 ctxt->state = oldstate;
9387 goto done;
9388 }
9389 } else if (ctxt->states != NULL) {
9390 int j;
9391 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009392 int best = -1;
9393 int lowattr = -1;
9394
9395 /*
9396 * PBM: what happen if there is attributes checks in the interleaves
9397 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009398
Daniel Veillard4c004142003-10-07 11:33:24 +00009399 for (j = 0; j < ctxt->states->nbState; j++) {
9400 cur = ctxt->states->tabState[j]->seq;
9401 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9402 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009403 if (found == 0) {
9404 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9405 best = j;
9406 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009407 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009408 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9409 /* try to keep the latest one to mach old heuristic */
9410 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9411 best = j;
9412 }
9413 if (lowattr == 0)
9414 break;
9415 } else if (found == 0) {
9416 if (lowattr == -1) {
9417 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9418 best = j;
9419 } else
9420 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9421 /* try to keep the latest one to mach old heuristic */
9422 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9423 best = j;
9424 }
9425 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009426 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009427 /*
9428 * BIG PBM: here we pick only one restarting point :-(
9429 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009430 if (ctxt->states->nbState > 0) {
9431 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009432 if (best != -1) {
9433 oldstate = ctxt->states->tabState[best];
9434 ctxt->states->tabState[best] = NULL;
9435 } else {
9436 oldstate =
9437 ctxt->states->tabState[ctxt->states->nbState - 1];
9438 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009439 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009440 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009441 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009442 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009443 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9444 }
9445 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9446 ctxt->states = NULL;
9447 if (found == 0) {
Daniel Veillard76d36452009-09-07 11:19:33 +02009448 if (cur == NULL) {
9449 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, 'noname');
9450 } else {
9451 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9452 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009453 ret = -1;
9454 ctxt->state = oldstate;
9455 goto done;
9456 }
9457 } else {
9458 ret = -1;
9459 break;
9460 }
9461 if (lasts[i] != NULL) {
9462 lasts[i]->next = last;
9463 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009464 }
9465 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009466 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009467 ctxt->state = oldstate;
9468 ctxt->state->seq = lastelem;
9469 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009470 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9471 ret = -1;
9472 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009473 }
9474
Daniel Veillard4c004142003-10-07 11:33:24 +00009475 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009476 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009477 /*
9478 * builds the next links chain from the prev one
9479 */
9480 cur = lastchg;
9481 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009482 if ((cur == start) || (cur->prev == NULL))
9483 break;
9484 cur->prev->next = cur;
9485 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009486 }
9487 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009488 if (ctxt->errNr > errNr)
9489 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009490 }
9491
9492 xmlFree(list);
9493 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009494 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009495}
9496
9497/**
9498 * xmlRelaxNGValidateDefinitionList:
9499 * @ctxt: a Relax-NG validation context
9500 * @define: the list of definition to verify
9501 *
9502 * Validate the given node content against the (list) of definitions
9503 *
9504 * Returns 0 if the validation succeeded or an error code.
9505 */
9506static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009507xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9508 xmlRelaxNGDefinePtr defines)
9509{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009510 int ret = 0, res;
9511
9512
Daniel Veillard952379b2003-03-17 15:37:12 +00009513 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009514 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9515 BAD_CAST "NULL definition list");
9516 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009517 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009518 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009519 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9520 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9521 if (res < 0)
9522 ret = -1;
9523 } else {
9524 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9525 return (-1);
9526 }
9527 if (res == -1) /* continues on -2 */
9528 break;
9529 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009530 }
9531
Daniel Veillard4c004142003-10-07 11:33:24 +00009532 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009533}
9534
9535/**
9536 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009537 * @ctxt: a Relax-NG validation context
9538 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009539 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009540 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009541 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009542 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009543 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009544 */
9545static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009546xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9547 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9548{
Daniel Veillard580ced82003-03-21 21:22:48 +00009549 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009550
Daniel Veillardfd573f12003-03-16 17:52:32 +00009551 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009552 if (!xmlStrEqual(elem->name, define->name)) {
9553 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9554 return (0);
9555 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009556 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009557 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009558 if (elem->ns == NULL) {
9559 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9560 return (0);
9561 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9562 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9563 elem->name, define->ns);
9564 return (0);
9565 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009566 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009567 (define->name == NULL)) {
9568 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9569 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009570 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009571 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9572 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009573 }
9574
9575 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009576 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009577
9578 define = define->nameClass;
9579 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009580 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009581
Daniel Veillard4c004142003-10-07 11:33:24 +00009582 if (ctxt != NULL) {
9583 oldflags = ctxt->flags;
9584 ctxt->flags |= FLAGS_IGNORABLE;
9585 }
9586
9587 list = define->content;
9588 while (list != NULL) {
9589 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9590 if (ret == 1) {
9591 if (ctxt != NULL)
9592 ctxt->flags = oldflags;
9593 return (0);
9594 }
9595 if (ret < 0) {
9596 if (ctxt != NULL)
9597 ctxt->flags = oldflags;
9598 return (ret);
9599 }
9600 list = list->next;
9601 }
9602 ret = 1;
9603 if (ctxt != NULL) {
9604 ctxt->flags = oldflags;
9605 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009606 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009607 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009608
Daniel Veillard4c004142003-10-07 11:33:24 +00009609 if (ctxt != NULL) {
9610 oldflags = ctxt->flags;
9611 ctxt->flags |= FLAGS_IGNORABLE;
9612 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009613
Daniel Veillard4c004142003-10-07 11:33:24 +00009614 list = define->nameClass;
9615 while (list != NULL) {
9616 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9617 if (ret == 1) {
9618 if (ctxt != NULL)
9619 ctxt->flags = oldflags;
9620 return (1);
9621 }
9622 if (ret < 0) {
9623 if (ctxt != NULL)
9624 ctxt->flags = oldflags;
9625 return (ret);
9626 }
9627 list = list->next;
9628 }
9629 if (ctxt != NULL) {
9630 if (ret != 0) {
9631 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9632 xmlRelaxNGDumpValidError(ctxt);
9633 } else {
9634 if (ctxt->errNr > 0)
9635 xmlRelaxNGPopErrors(ctxt, 0);
9636 }
9637 }
9638 ret = 0;
9639 if (ctxt != NULL) {
9640 ctxt->flags = oldflags;
9641 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009642 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009643 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009644 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009645 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009646}
9647
9648/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009649 * xmlRelaxNGBestState:
9650 * @ctxt: a Relax-NG validation context
9651 *
9652 * Find the "best" state in the ctxt->states list of states to report
9653 * errors about. I.e. a state with no element left in the child list
9654 * or the one with the less attributes left.
9655 * This is called only if a falidation error was detected
9656 *
9657 * Returns the index of the "best" state or -1 in case of error
9658 */
9659static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009660xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9661{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009662 xmlRelaxNGValidStatePtr state;
9663 int i, tmp;
9664 int best = -1;
9665 int value = 1000000;
9666
9667 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9668 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009669 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009670
Daniel Veillard4c004142003-10-07 11:33:24 +00009671 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009672 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009673 if (state == NULL)
9674 continue;
9675 if (state->seq != NULL) {
9676 if ((best == -1) || (value > 100000)) {
9677 value = 100000;
9678 best = i;
9679 }
9680 } else {
9681 tmp = state->nbAttrLeft;
9682 if ((best == -1) || (value > tmp)) {
9683 value = tmp;
9684 best = i;
9685 }
9686 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009687 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009688 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009689}
9690
9691/**
9692 * xmlRelaxNGLogBestError:
9693 * @ctxt: a Relax-NG validation context
9694 *
9695 * Find the "best" state in the ctxt->states list of states to report
9696 * errors about and log it.
9697 */
9698static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009699xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9700{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009701 int best;
9702
9703 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9704 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009705 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009706
9707 best = xmlRelaxNGBestState(ctxt);
9708 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009709 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009710
Daniel Veillard4c004142003-10-07 11:33:24 +00009711 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009712 }
9713}
9714
9715/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009716 * xmlRelaxNGValidateElementEnd:
9717 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009718 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009719 *
9720 * Validate the end of the element, implements check that
9721 * there is nothing left not consumed in the element content
9722 * or in the attribute list.
9723 *
9724 * Returns 0 if the validation succeeded or an error code.
9725 */
9726static int
William M. Brack272693c2003-11-14 16:20:34 +00009727xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009728{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009729 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009730 xmlRelaxNGValidStatePtr state;
9731
9732 state = ctxt->state;
9733 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009734 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9735 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009736 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009737 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9738 state->node->name, state->seq->name);
9739 }
9740 return (-1);
9741 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009742 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009743 for (i = 0; i < state->nbAttrs; i++) {
9744 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009745 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009746 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9747 state->attrs[i]->name, state->node->name);
9748 }
9749 return (-1 - i);
9750 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009751 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009752 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009753}
9754
9755/**
9756 * xmlRelaxNGValidateState:
9757 * @ctxt: a Relax-NG validation context
9758 * @define: the definition to verify
9759 *
9760 * Validate the current state against the definition
9761 *
9762 * Returns 0 if the validation succeeded or an error code.
9763 */
9764static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009765xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9766 xmlRelaxNGDefinePtr define)
9767{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009768 xmlNodePtr node;
9769 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009770 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009771
9772 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009773 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9774 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009775 }
9776
9777 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009778 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009779 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009780 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009781 }
9782#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009783 for (i = 0; i < ctxt->depth; i++)
9784 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009785 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009786 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009787 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009788 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009789 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009790 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009791 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009792 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009793#endif
9794 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009795 switch (define->type) {
9796 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009797 node = xmlRelaxNGSkipIgnored(ctxt, node);
9798 ret = 0;
9799 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009800 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009801 ret = -1;
9802 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009803 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009804 while ((node != NULL) &&
9805 ((node->type == XML_TEXT_NODE) ||
9806 (node->type == XML_COMMENT_NODE) ||
9807 (node->type == XML_PI_NODE) ||
9808 (node->type == XML_CDATA_SECTION_NODE)))
9809 node = node->next;
9810 ctxt->state->seq = node;
9811 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009812 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009813 errNr = ctxt->errNr;
9814 node = xmlRelaxNGSkipIgnored(ctxt, node);
9815 if (node == NULL) {
9816 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9817 ret = -1;
9818 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9819 xmlRelaxNGDumpValidError(ctxt);
9820 break;
9821 }
9822 if (node->type != XML_ELEMENT_NODE) {
9823 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9824 ret = -1;
9825 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9826 xmlRelaxNGDumpValidError(ctxt);
9827 break;
9828 }
9829 /*
9830 * This node was already validated successfully against
9831 * this definition.
9832 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009833 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009834 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9835 if (ctxt->errNr > errNr)
9836 xmlRelaxNGPopErrors(ctxt, errNr);
9837 if (ctxt->errNr != 0) {
9838 while ((ctxt->err != NULL) &&
9839 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9840 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9841 ||
9842 ((ctxt->err->err ==
9843 XML_RELAXNG_ERR_ELEMEXTRANS)
9844 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9845 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9846 || (ctxt->err->err ==
9847 XML_RELAXNG_ERR_NOTELEM)))
9848 xmlRelaxNGValidErrorPop(ctxt);
9849 }
9850 break;
9851 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009852
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009853 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9854 if (ret <= 0) {
9855 ret = -1;
9856 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9857 xmlRelaxNGDumpValidError(ctxt);
9858 break;
9859 }
9860 ret = 0;
9861 if (ctxt->errNr != 0) {
9862 if (ctxt->errNr > errNr)
9863 xmlRelaxNGPopErrors(ctxt, errNr);
9864 while ((ctxt->err != NULL) &&
9865 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9866 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9867 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9868 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9869 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9870 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9871 xmlRelaxNGValidErrorPop(ctxt);
9872 }
9873 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009874
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009875 oldflags = ctxt->flags;
9876 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9877 ctxt->flags -= FLAGS_MIXED_CONTENT;
9878 }
9879 state = xmlRelaxNGNewValidState(ctxt, node);
9880 if (state == NULL) {
9881 ret = -1;
9882 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9883 xmlRelaxNGDumpValidError(ctxt);
9884 break;
9885 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009886
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009887 oldstate = ctxt->state;
9888 ctxt->state = state;
9889 if (define->attrs != NULL) {
9890 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9891 if (tmp != 0) {
9892 ret = -1;
9893 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9894 }
9895 }
9896 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009897 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9898 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9899 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009900
Daniel Veillard4c004142003-10-07 11:33:24 +00009901 nstate = xmlRelaxNGNewValidState(ctxt, node);
9902 ctxt->state = nstate;
9903 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009904
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009905 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9906 define->contModel,
9907 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009908 nseq = ctxt->state->seq;
9909 ctxt->state = tmpstate;
9910 ctxt->states = tmpstates;
9911 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009912
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009913#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009914 xmlGenericError(xmlGenericErrorContext,
9915 "Validating content of '%s' : %d\n",
9916 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009917#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009918 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009919 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009920
9921 if (ctxt->states != NULL) {
9922 tmp = -1;
9923
Daniel Veillardce192eb2003-04-16 15:58:05 +00009924 for (i = 0; i < ctxt->states->nbState; i++) {
9925 state = ctxt->states->tabState[i];
9926 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009927 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009928
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009929 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009930 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009931 break;
9932 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009933 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009934 if (tmp != 0) {
9935 /*
9936 * validation error, log the message for the "best" one
9937 */
9938 ctxt->flags |= FLAGS_IGNORABLE;
9939 xmlRelaxNGLogBestError(ctxt);
9940 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009941 for (i = 0; i < ctxt->states->nbState; i++) {
9942 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009943 ctxt->states->
9944 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009945 }
9946 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9947 ctxt->flags = oldflags;
9948 ctxt->states = NULL;
9949 if ((ret == 0) && (tmp == -1))
9950 ret = -1;
9951 } else {
9952 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +00009953 if (ctxt->state != NULL)
9954 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009955 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009956 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009957 xmlRelaxNGFreeValidState(ctxt, state);
9958 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009959 } else {
9960 if (define->content != NULL) {
9961 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009962 define->
9963 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009964 if (tmp != 0) {
9965 ret = -1;
9966 if (ctxt->state == NULL) {
9967 ctxt->state = oldstate;
9968 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9969 node->name);
9970 ctxt->state = NULL;
9971 } else {
9972 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9973 node->name);
9974 }
9975
9976 }
9977 }
9978 if (ctxt->states != NULL) {
9979 tmp = -1;
9980
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009981 for (i = 0; i < ctxt->states->nbState; i++) {
9982 state = ctxt->states->tabState[i];
9983 ctxt->state = state;
9984
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009985 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009986 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009987 break;
9988 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009989 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009990 if (tmp != 0) {
9991 /*
9992 * validation error, log the message for the "best" one
9993 */
9994 ctxt->flags |= FLAGS_IGNORABLE;
9995 xmlRelaxNGLogBestError(ctxt);
9996 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009997 for (i = 0; i < ctxt->states->nbState; i++) {
9998 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009999 ctxt->states->tabState[i]);
10000 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010001 }
10002 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10003 ctxt->flags = oldflags;
10004 ctxt->states = NULL;
10005 if ((ret == 0) && (tmp == -1))
10006 ret = -1;
10007 } else {
10008 state = ctxt->state;
10009 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010010 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010011 xmlRelaxNGFreeValidState(ctxt, state);
10012 }
10013 }
10014 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +000010015 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010016 }
10017 ctxt->flags = oldflags;
10018 ctxt->state = oldstate;
10019 if (oldstate != NULL)
10020 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10021 if (ret != 0) {
10022 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10023 xmlRelaxNGDumpValidError(ctxt);
10024 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010025#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010026 } else {
10027 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010028#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010029 }
10030 } else {
10031 if (ctxt->errNr > errNr)
10032 xmlRelaxNGPopErrors(ctxt, errNr);
10033 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010034
10035#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010036 xmlGenericError(xmlGenericErrorContext,
10037 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10038 node->name, ret);
10039 if (oldstate == NULL)
10040 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10041 else if (oldstate->seq == NULL)
10042 xmlGenericError(xmlGenericErrorContext, ": done\n");
10043 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10044 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10045 oldstate->seq->name);
10046 else
10047 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10048 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010049#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010050 break;
10051 case XML_RELAXNG_OPTIONAL:{
10052 errNr = ctxt->errNr;
10053 oldflags = ctxt->flags;
10054 ctxt->flags |= FLAGS_IGNORABLE;
10055 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10056 ret =
10057 xmlRelaxNGValidateDefinitionList(ctxt,
10058 define->content);
10059 if (ret != 0) {
10060 if (ctxt->state != NULL)
10061 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10062 ctxt->state = oldstate;
10063 ctxt->flags = oldflags;
10064 ret = 0;
10065 if (ctxt->errNr > errNr)
10066 xmlRelaxNGPopErrors(ctxt, errNr);
10067 break;
10068 }
10069 if (ctxt->states != NULL) {
10070 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10071 } else {
10072 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10073 if (ctxt->states == NULL) {
10074 xmlRelaxNGFreeValidState(ctxt, oldstate);
10075 ctxt->flags = oldflags;
10076 ret = -1;
10077 if (ctxt->errNr > errNr)
10078 xmlRelaxNGPopErrors(ctxt, errNr);
10079 break;
10080 }
10081 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10082 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10083 ctxt->state = NULL;
10084 }
10085 ctxt->flags = oldflags;
10086 ret = 0;
10087 if (ctxt->errNr > errNr)
10088 xmlRelaxNGPopErrors(ctxt, errNr);
10089 break;
10090 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010091 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010092 errNr = ctxt->errNr;
10093 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10094 if (ret != 0) {
10095 break;
10096 }
10097 if (ctxt->errNr > errNr)
10098 xmlRelaxNGPopErrors(ctxt, errNr);
10099 /* no break on purpose */
10100 case XML_RELAXNG_ZEROORMORE:{
10101 int progress;
10102 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10103 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010104
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010105 errNr = ctxt->errNr;
10106 res = xmlRelaxNGNewStates(ctxt, 1);
10107 if (res == NULL) {
10108 ret = -1;
10109 break;
10110 }
10111 /*
10112 * All the input states are also exit states
10113 */
10114 if (ctxt->state != NULL) {
10115 xmlRelaxNGAddStates(ctxt, res,
10116 xmlRelaxNGCopyValidState(ctxt,
10117 ctxt->
10118 state));
10119 } else {
10120 for (j = 0; j < ctxt->states->nbState; j++) {
10121 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010122 xmlRelaxNGCopyValidState(ctxt,
10123 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010124 }
10125 }
10126 oldflags = ctxt->flags;
10127 ctxt->flags |= FLAGS_IGNORABLE;
10128 do {
10129 progress = 0;
10130 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010131
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010132 if (ctxt->states != NULL) {
10133 states = ctxt->states;
10134 for (i = 0; i < states->nbState; i++) {
10135 ctxt->state = states->tabState[i];
10136 ctxt->states = NULL;
10137 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10138 define->
10139 content);
10140 if (ret == 0) {
10141 if (ctxt->state != NULL) {
10142 tmp = xmlRelaxNGAddStates(ctxt, res,
10143 ctxt->state);
10144 ctxt->state = NULL;
10145 if (tmp == 1)
10146 progress = 1;
10147 } else if (ctxt->states != NULL) {
10148 for (j = 0; j < ctxt->states->nbState;
10149 j++) {
10150 tmp =
10151 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010152 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010153 if (tmp == 1)
10154 progress = 1;
10155 }
10156 xmlRelaxNGFreeStates(ctxt,
10157 ctxt->states);
10158 ctxt->states = NULL;
10159 }
10160 } else {
10161 if (ctxt->state != NULL) {
10162 xmlRelaxNGFreeValidState(ctxt,
10163 ctxt->state);
10164 ctxt->state = NULL;
10165 }
10166 }
10167 }
10168 } else {
10169 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10170 define->
10171 content);
10172 if (ret != 0) {
10173 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10174 ctxt->state = NULL;
10175 } else {
10176 base = res->nbState;
10177 if (ctxt->state != NULL) {
10178 tmp = xmlRelaxNGAddStates(ctxt, res,
10179 ctxt->state);
10180 ctxt->state = NULL;
10181 if (tmp == 1)
10182 progress = 1;
10183 } else if (ctxt->states != NULL) {
10184 for (j = 0; j < ctxt->states->nbState; j++) {
10185 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010186 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010187 if (tmp == 1)
10188 progress = 1;
10189 }
10190 if (states == NULL) {
10191 states = ctxt->states;
10192 } else {
10193 xmlRelaxNGFreeStates(ctxt,
10194 ctxt->states);
10195 }
10196 ctxt->states = NULL;
10197 }
10198 }
10199 }
10200 if (progress) {
10201 /*
10202 * Collect all the new nodes added at that step
10203 * and make them the new node set
10204 */
10205 if (res->nbState - base == 1) {
10206 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10207 res->
10208 tabState
10209 [base]);
10210 } else {
10211 if (states == NULL) {
10212 xmlRelaxNGNewStates(ctxt,
10213 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010214 states = ctxt->states;
10215 if (states == NULL) {
10216 progress = 0;
10217 break;
10218 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010219 }
10220 states->nbState = 0;
10221 for (i = base; i < res->nbState; i++)
10222 xmlRelaxNGAddStates(ctxt, states,
10223 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010224 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010225 ctxt->states = states;
10226 }
10227 }
10228 } while (progress == 1);
10229 if (states != NULL) {
10230 xmlRelaxNGFreeStates(ctxt, states);
10231 }
10232 ctxt->states = res;
10233 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010234#if 0
10235 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010236 * errors may have to be propagated back...
10237 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010238 if (ctxt->errNr > errNr)
10239 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010240#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010241 ret = 0;
10242 break;
10243 }
10244 case XML_RELAXNG_CHOICE:{
10245 xmlRelaxNGDefinePtr list = NULL;
10246 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010247
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010248 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010249
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010250 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010251 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10252 (node != NULL)) {
10253 /*
10254 * node == NULL can't be optimized since IS_TRIABLE
10255 * doesn't account for choice which may lead to
10256 * only attributes.
10257 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010258 xmlHashTablePtr triage =
10259 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010260
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010261 /*
10262 * Something we can optimize cleanly there is only one
10263 * possble branch out !
10264 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010265 if ((node->type == XML_TEXT_NODE) ||
10266 (node->type == XML_CDATA_SECTION_NODE)) {
10267 list =
10268 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10269 } else if (node->type == XML_ELEMENT_NODE) {
10270 if (node->ns != NULL) {
10271 list = xmlHashLookup2(triage, node->name,
10272 node->ns->href);
10273 if (list == NULL)
10274 list =
10275 xmlHashLookup2(triage, BAD_CAST "#any",
10276 node->ns->href);
10277 } else
10278 list =
10279 xmlHashLookup2(triage, node->name, NULL);
10280 if (list == NULL)
10281 list =
10282 xmlHashLookup2(triage, BAD_CAST "#any",
10283 NULL);
10284 }
10285 if (list == NULL) {
10286 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010287 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010288 break;
10289 }
10290 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10291 if (ret == 0) {
10292 }
10293 break;
10294 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010295
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010296 list = define->content;
10297 oldflags = ctxt->flags;
10298 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010299
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010300 while (list != NULL) {
10301 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10302 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10303 if (ret == 0) {
10304 if (states == NULL) {
10305 states = xmlRelaxNGNewStates(ctxt, 1);
10306 }
10307 if (ctxt->state != NULL) {
10308 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10309 } else if (ctxt->states != NULL) {
10310 for (i = 0; i < ctxt->states->nbState; i++) {
10311 xmlRelaxNGAddStates(ctxt, states,
10312 ctxt->states->
10313 tabState[i]);
10314 }
10315 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10316 ctxt->states = NULL;
10317 }
10318 } else {
10319 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10320 }
10321 ctxt->state = oldstate;
10322 list = list->next;
10323 }
10324 if (states != NULL) {
10325 xmlRelaxNGFreeValidState(ctxt, oldstate);
10326 ctxt->states = states;
10327 ctxt->state = NULL;
10328 ret = 0;
10329 } else {
10330 ctxt->states = NULL;
10331 }
10332 ctxt->flags = oldflags;
10333 if (ret != 0) {
10334 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10335 xmlRelaxNGDumpValidError(ctxt);
10336 }
10337 } else {
10338 if (ctxt->errNr > errNr)
10339 xmlRelaxNGPopErrors(ctxt, errNr);
10340 }
10341 break;
10342 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010343 case XML_RELAXNG_DEF:
10344 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010345 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10346 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010347 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010348 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10349 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010350 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010351 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10352 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010353 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010354 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010355 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010356 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010357 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010358 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10359 break;
10360 case XML_RELAXNG_DATATYPE:{
10361 xmlNodePtr child;
10362 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010363
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010364 child = node;
10365 while (child != NULL) {
10366 if (child->type == XML_ELEMENT_NODE) {
10367 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10368 node->parent->name);
10369 ret = -1;
10370 break;
10371 } else if ((child->type == XML_TEXT_NODE) ||
10372 (child->type == XML_CDATA_SECTION_NODE)) {
10373 content = xmlStrcat(content, child->content);
10374 }
10375 /* TODO: handle entities ... */
10376 child = child->next;
10377 }
10378 if (ret == -1) {
10379 if (content != NULL)
10380 xmlFree(content);
10381 break;
10382 }
10383 if (content == NULL) {
10384 content = xmlStrdup(BAD_CAST "");
10385 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010386 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010387 ret = -1;
10388 break;
10389 }
10390 }
10391 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10392 ctxt->state->seq);
10393 if (ret == -1) {
10394 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10395 } else if (ret == 0) {
10396 ctxt->state->seq = NULL;
10397 }
10398 if (content != NULL)
10399 xmlFree(content);
10400 break;
10401 }
10402 case XML_RELAXNG_VALUE:{
10403 xmlChar *content = NULL;
10404 xmlChar *oldvalue;
10405 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010406
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010407 child = node;
10408 while (child != NULL) {
10409 if (child->type == XML_ELEMENT_NODE) {
10410 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10411 node->parent->name);
10412 ret = -1;
10413 break;
10414 } else if ((child->type == XML_TEXT_NODE) ||
10415 (child->type == XML_CDATA_SECTION_NODE)) {
10416 content = xmlStrcat(content, child->content);
10417 }
10418 /* TODO: handle entities ... */
10419 child = child->next;
10420 }
10421 if (ret == -1) {
10422 if (content != NULL)
10423 xmlFree(content);
10424 break;
10425 }
10426 if (content == NULL) {
10427 content = xmlStrdup(BAD_CAST "");
10428 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010429 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010430 ret = -1;
10431 break;
10432 }
10433 }
10434 oldvalue = ctxt->state->value;
10435 ctxt->state->value = content;
10436 ret = xmlRelaxNGValidateValue(ctxt, define);
10437 ctxt->state->value = oldvalue;
10438 if (ret == -1) {
10439 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10440 } else if (ret == 0) {
10441 ctxt->state->seq = NULL;
10442 }
10443 if (content != NULL)
10444 xmlFree(content);
10445 break;
10446 }
10447 case XML_RELAXNG_LIST:{
10448 xmlChar *content;
10449 xmlNodePtr child;
10450 xmlChar *oldvalue, *oldendvalue;
10451 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010452
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010453 /*
10454 * Make sure it's only text nodes
10455 */
10456
10457 content = NULL;
10458 child = node;
10459 while (child != NULL) {
10460 if (child->type == XML_ELEMENT_NODE) {
10461 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10462 node->parent->name);
10463 ret = -1;
10464 break;
10465 } else if ((child->type == XML_TEXT_NODE) ||
10466 (child->type == XML_CDATA_SECTION_NODE)) {
10467 content = xmlStrcat(content, child->content);
10468 }
10469 /* TODO: handle entities ... */
10470 child = child->next;
10471 }
10472 if (ret == -1) {
10473 if (content != NULL)
10474 xmlFree(content);
10475 break;
10476 }
10477 if (content == NULL) {
10478 content = xmlStrdup(BAD_CAST "");
10479 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010480 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010481 ret = -1;
10482 break;
10483 }
10484 }
10485 len = xmlStrlen(content);
10486 oldvalue = ctxt->state->value;
10487 oldendvalue = ctxt->state->endvalue;
10488 ctxt->state->value = content;
10489 ctxt->state->endvalue = content + len;
10490 ret = xmlRelaxNGValidateValue(ctxt, define);
10491 ctxt->state->value = oldvalue;
10492 ctxt->state->endvalue = oldendvalue;
10493 if (ret == -1) {
10494 VALID_ERR(XML_RELAXNG_ERR_LIST);
10495 } else if ((ret == 0) && (node != NULL)) {
10496 ctxt->state->seq = node->next;
10497 }
10498 if (content != NULL)
10499 xmlFree(content);
10500 break;
10501 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010502 case XML_RELAXNG_EXCEPT:
10503 case XML_RELAXNG_PARAM:
10504 TODO ret = -1;
10505 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010506 }
10507 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010508#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010509 for (i = 0; i < ctxt->depth; i++)
10510 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010511 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010512 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010513 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010514 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010515 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010516 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010517 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010518 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010519#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010520 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010521}
10522
10523/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010524 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010525 * @ctxt: a Relax-NG validation context
10526 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010527 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010528 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010529 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010530 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010531 */
10532static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010533xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10534 xmlRelaxNGDefinePtr define)
10535{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010536 xmlRelaxNGStatesPtr states, res;
10537 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010538
Daniel Veillardfd573f12003-03-16 17:52:32 +000010539 /*
10540 * We should NOT have both ctxt->state and ctxt->states
10541 */
10542 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010543 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10544 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010545 }
10546
10547 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010548 if (ctxt->states != NULL) {
10549 ctxt->state = ctxt->states->tabState[0];
10550 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10551 ctxt->states = NULL;
10552 }
10553 ret = xmlRelaxNGValidateState(ctxt, define);
10554 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10555 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10556 ctxt->state = NULL;
10557 }
10558 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10559 ctxt->state = ctxt->states->tabState[0];
10560 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10561 ctxt->states = NULL;
10562 }
10563 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010564 }
10565
10566 states = ctxt->states;
10567 ctxt->states = NULL;
10568 res = NULL;
10569 j = 0;
10570 oldflags = ctxt->flags;
10571 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010572 for (i = 0; i < states->nbState; i++) {
10573 ctxt->state = states->tabState[i];
10574 ctxt->states = NULL;
10575 ret = xmlRelaxNGValidateState(ctxt, define);
10576 /*
10577 * We should NOT have both ctxt->state and ctxt->states
10578 */
10579 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10580 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10581 ctxt->state = NULL;
10582 }
10583 if (ret == 0) {
10584 if (ctxt->states == NULL) {
10585 if (res != NULL) {
10586 /* add the state to the container */
10587 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10588 ctxt->state = NULL;
10589 } else {
10590 /* add the state directly in states */
10591 states->tabState[j++] = ctxt->state;
10592 ctxt->state = NULL;
10593 }
10594 } else {
10595 if (res == NULL) {
10596 /* make it the new container and copy other results */
10597 res = ctxt->states;
10598 ctxt->states = NULL;
10599 for (k = 0; k < j; k++)
10600 xmlRelaxNGAddStates(ctxt, res,
10601 states->tabState[k]);
10602 } else {
10603 /* add all the new results to res and reff the container */
10604 for (k = 0; k < ctxt->states->nbState; k++)
10605 xmlRelaxNGAddStates(ctxt, res,
10606 ctxt->states->tabState[k]);
10607 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10608 ctxt->states = NULL;
10609 }
10610 }
10611 } else {
10612 if (ctxt->state != NULL) {
10613 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10614 ctxt->state = NULL;
10615 } else if (ctxt->states != NULL) {
10616 for (k = 0; k < ctxt->states->nbState; k++)
10617 xmlRelaxNGFreeValidState(ctxt,
10618 ctxt->states->tabState[k]);
10619 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10620 ctxt->states = NULL;
10621 }
10622 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010623 }
10624 ctxt->flags = oldflags;
10625 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010626 xmlRelaxNGFreeStates(ctxt, states);
10627 ctxt->states = res;
10628 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010629 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010630 states->nbState = j;
10631 ctxt->states = states;
10632 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010633 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010634 ctxt->state = states->tabState[0];
10635 xmlRelaxNGFreeStates(ctxt, states);
10636 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010637 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010638 ret = -1;
10639 xmlRelaxNGFreeStates(ctxt, states);
10640 if (ctxt->states != NULL) {
10641 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10642 ctxt->states = NULL;
10643 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010644 }
10645 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010646 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10647 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010648 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010649 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010650}
10651
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010652/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010653 * xmlRelaxNGValidateDocument:
10654 * @ctxt: a Relax-NG validation context
10655 * @doc: the document
10656 *
10657 * Validate the given document
10658 *
10659 * Returns 0 if the validation succeeded or an error code.
10660 */
10661static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010662xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10663{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010664 int ret;
10665 xmlRelaxNGPtr schema;
10666 xmlRelaxNGGrammarPtr grammar;
10667 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010668 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010669
10670 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010671 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010672
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010673 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010674 schema = ctxt->schema;
10675 grammar = schema->topgrammar;
10676 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010677 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10678 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010679 }
10680 state = xmlRelaxNGNewValidState(ctxt, NULL);
10681 ctxt->state = state;
10682 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010683 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010684 state = ctxt->state;
10685 node = state->seq;
10686 node = xmlRelaxNGSkipIgnored(ctxt, node);
10687 if (node != NULL) {
10688 if (ret != -1) {
10689 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10690 ret = -1;
10691 }
10692 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010693 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010694 int i;
10695 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010696
Daniel Veillard4c004142003-10-07 11:33:24 +000010697 for (i = 0; i < ctxt->states->nbState; i++) {
10698 state = ctxt->states->tabState[i];
10699 node = state->seq;
10700 node = xmlRelaxNGSkipIgnored(ctxt, node);
10701 if (node == NULL)
10702 tmp = 0;
10703 xmlRelaxNGFreeValidState(ctxt, state);
10704 }
10705 if (tmp == -1) {
10706 if (ret != -1) {
10707 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10708 ret = -1;
10709 }
10710 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010711 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010712 if (ctxt->state != NULL) {
10713 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010714 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010715 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010716 if (ret != 0)
10717 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010718#ifdef DEBUG
10719 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010720 ctxt->error(ctxt->userData,
10721 "%d Extra error messages left on stack !\n",
10722 ctxt->errNr);
10723 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010724 }
10725#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010726#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010727 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010728 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010729
Daniel Veillard4c004142003-10-07 11:33:24 +000010730 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10731 vctxt.valid = 1;
10732 vctxt.error = ctxt->error;
10733 vctxt.warning = ctxt->warning;
10734 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010735
Daniel Veillard4c004142003-10-07 11:33:24 +000010736 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10737 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010738 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010739#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010740 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010741 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010742
Daniel Veillard4c004142003-10-07 11:33:24 +000010743 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010744}
10745
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010746/**
10747 * xmlRelaxNGCleanPSVI:
10748 * @node: an input element or document
10749 *
10750 * Call this routine to speed up XPath computation on static documents.
10751 * This stamps all the element nodes with the document order
10752 * Like for line information, the order is kept in the element->content
10753 * field, the value stored is actually - the node number (starting at -1)
10754 * to be able to differentiate from line numbers.
10755 *
10756 * Returns the number of elements found in the document or -1 in case
10757 * of error.
10758 */
10759static void
10760xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10761 xmlNodePtr cur;
10762
10763 if ((node == NULL) ||
10764 ((node->type != XML_ELEMENT_NODE) &&
10765 (node->type != XML_DOCUMENT_NODE) &&
10766 (node->type != XML_HTML_DOCUMENT_NODE)))
10767 return;
10768 if (node->type == XML_ELEMENT_NODE)
10769 node->psvi = NULL;
10770
10771 cur = node->children;
10772 while (cur != NULL) {
10773 if (cur->type == XML_ELEMENT_NODE) {
10774 cur->psvi = NULL;
10775 if (cur->children != NULL) {
10776 cur = cur->children;
10777 continue;
10778 }
10779 }
10780 if (cur->next != NULL) {
10781 cur = cur->next;
10782 continue;
10783 }
10784 do {
10785 cur = cur->parent;
10786 if (cur == NULL)
10787 break;
10788 if (cur == node) {
10789 cur = NULL;
10790 break;
10791 }
10792 if (cur->next != NULL) {
10793 cur = cur->next;
10794 break;
10795 }
10796 } while (cur != NULL);
10797 }
10798 return;
10799}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010800/************************************************************************
10801 * *
10802 * Validation interfaces *
10803 * *
10804 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010805
Daniel Veillard6eadf632003-01-23 18:29:16 +000010806/**
10807 * xmlRelaxNGNewValidCtxt:
10808 * @schema: a precompiled XML RelaxNGs
10809 *
10810 * Create an XML RelaxNGs validation context based on the given schema
10811 *
10812 * Returns the validation context or NULL in case of error
10813 */
10814xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010815xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10816{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010817 xmlRelaxNGValidCtxtPtr ret;
10818
10819 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10820 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010821 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010822 return (NULL);
10823 }
10824 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10825 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010826 ret->error = xmlGenericError;
10827 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010828 ret->errNr = 0;
10829 ret->errMax = 0;
10830 ret->err = NULL;
10831 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010832 if (schema != NULL)
10833 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010834 ret->states = NULL;
10835 ret->freeState = NULL;
10836 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010837 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010838 return (ret);
10839}
10840
10841/**
10842 * xmlRelaxNGFreeValidCtxt:
10843 * @ctxt: the schema validation context
10844 *
10845 * Free the resources associated to the schema validation context
10846 */
10847void
Daniel Veillard4c004142003-10-07 11:33:24 +000010848xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10849{
Daniel Veillard798024a2003-03-19 10:36:09 +000010850 int k;
10851
Daniel Veillard6eadf632003-01-23 18:29:16 +000010852 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010853 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010854 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010855 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010856 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010857 for (k = 0; k < ctxt->freeState->nbState; k++) {
10858 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10859 }
10860 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010861 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010862 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010863 for (k = 0; k < ctxt->freeStatesNr; k++) {
10864 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10865 }
10866 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010867 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010868 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010869 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010870 if (ctxt->elemTab != NULL) {
10871 xmlRegExecCtxtPtr exec;
10872
Daniel Veillard4c004142003-10-07 11:33:24 +000010873 exec = xmlRelaxNGElemPop(ctxt);
10874 while (exec != NULL) {
10875 xmlRegFreeExecCtxt(exec);
10876 exec = xmlRelaxNGElemPop(ctxt);
10877 }
10878 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010879 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010880 xmlFree(ctxt);
10881}
10882
10883/**
10884 * xmlRelaxNGSetValidErrors:
10885 * @ctxt: a Relax-NG validation context
10886 * @err: the error function
10887 * @warn: the warning function
10888 * @ctx: the functions context
10889 *
10890 * Set the error and warning callback informations
10891 */
10892void
10893xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010894 xmlRelaxNGValidityErrorFunc err,
10895 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10896{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010897 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010898 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010899 ctxt->error = err;
10900 ctxt->warning = warn;
10901 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010902 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010903}
10904
10905/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010906 * xmlRelaxNGSetValidStructuredErrors:
10907 * @ctxt: a Relax-NG validation context
10908 * @serror: the structured error function
10909 * @ctx: the functions context
10910 *
10911 * Set the structured error callback
10912 */
10913void
10914xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010915 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010916{
10917 if (ctxt == NULL)
10918 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010919 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010920 ctxt->error = NULL;
10921 ctxt->warning = NULL;
10922 ctxt->userData = ctx;
10923}
10924
10925/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010926 * xmlRelaxNGGetValidErrors:
10927 * @ctxt: a Relax-NG validation context
10928 * @err: the error function result
10929 * @warn: the warning function result
10930 * @ctx: the functions context result
10931 *
10932 * Get the error and warning callback informations
10933 *
10934 * Returns -1 in case of error and 0 otherwise
10935 */
10936int
10937xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010938 xmlRelaxNGValidityErrorFunc * err,
10939 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10940{
Daniel Veillard409a8142003-07-18 15:16:57 +000010941 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010942 return (-1);
10943 if (err != NULL)
10944 *err = ctxt->error;
10945 if (warn != NULL)
10946 *warn = ctxt->warning;
10947 if (ctx != NULL)
10948 *ctx = ctxt->userData;
10949 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010950}
10951
10952/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010953 * xmlRelaxNGValidateDoc:
10954 * @ctxt: a Relax-NG validation context
10955 * @doc: a parsed document tree
10956 *
10957 * Validate a document tree in memory.
10958 *
10959 * Returns 0 if the document is valid, a positive error code
10960 * number otherwise and -1 in case of internal or API error.
10961 */
10962int
Daniel Veillard4c004142003-10-07 11:33:24 +000010963xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10964{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010965 int ret;
10966
10967 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010968 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010969
10970 ctxt->doc = doc;
10971
10972 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010973 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010974 * Remove all left PSVI
10975 */
10976 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
10977
10978 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000010979 * TODO: build error codes
10980 */
10981 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010982 return (1);
10983 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010984}
10985
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010986#define bottom_relaxng
10987#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000010988#endif /* LIBXML_SCHEMAS_ENABLED */