blob: 6b83cfd35d5efa286ed0f53b580e08456243baaf [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 Veillard52b48c72003-04-13 19:53:42 +00002857static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2858 xmlRelaxNGDefinePtr def);
2859
Daniel Veillard952379b2003-03-17 15:37:12 +00002860/**
2861 * xmlRelaxNGIsCompileable:
2862 * @define: the definition to check
2863 *
2864 * Check if a definition is nullable.
2865 *
2866 * Returns 1 if yes, 0 if no and -1 in case of error
2867 */
2868static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002869xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2870{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002871 int ret = -1;
2872
Daniel Veillard952379b2003-03-17 15:37:12 +00002873 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002874 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002875 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002876 if ((def->type != XML_RELAXNG_ELEMENT) &&
2877 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002878 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002879 if ((def->type != XML_RELAXNG_ELEMENT) &&
2880 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002881 return (0);
2882 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002883 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002884 ret = xmlRelaxNGIsCompileable(def->content);
2885 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002886 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002887 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002888 ret = 1;
2889 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002890 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002891 /*
2892 * Check if the element content is compileable
2893 */
2894 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2895 ((def->dflags & IS_COMPILABLE) == 0)) {
2896 xmlRelaxNGDefinePtr list;
2897
2898 list = def->content;
2899 while (list != NULL) {
2900 ret = xmlRelaxNGIsCompileable(list);
2901 if (ret != 1)
2902 break;
2903 list = list->next;
2904 }
William M. Brack60929622004-03-27 17:54:18 +00002905 /*
2906 * Because the routine is recursive, we must guard against
2907 * discovering both COMPILABLE and NOT_COMPILABLE
2908 */
2909 if (ret == 0) {
2910 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002911 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002912 }
2913 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002914 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002915#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002916 if (ret == 1) {
2917 xmlGenericError(xmlGenericErrorContext,
2918 "element content for %s is compilable\n",
2919 def->name);
2920 } else if (ret == 0) {
2921 xmlGenericError(xmlGenericErrorContext,
2922 "element content for %s is not compilable\n",
2923 def->name);
2924 } else {
2925 xmlGenericError(xmlGenericErrorContext,
2926 "Problem in RelaxNGIsCompileable for element %s\n",
2927 def->name);
2928 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002929#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002930 }
2931 /*
2932 * All elements return a compileable status unless they
2933 * are generic like anyName
2934 */
2935 if ((def->nameClass != NULL) || (def->name == NULL))
2936 ret = 0;
2937 else
2938 ret = 1;
2939 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002940 case XML_RELAXNG_REF:
2941 case XML_RELAXNG_EXTERNALREF:
2942 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002943 if (def->depth == -20) {
2944 return (1);
2945 } else {
2946 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002947
Daniel Veillard4c004142003-10-07 11:33:24 +00002948 def->depth = -20;
2949 list = def->content;
2950 while (list != NULL) {
2951 ret = xmlRelaxNGIsCompileable(list);
2952 if (ret != 1)
2953 break;
2954 list = list->next;
2955 }
2956 }
2957 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002958 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002959 case XML_RELAXNG_OPTIONAL:
2960 case XML_RELAXNG_ZEROORMORE:
2961 case XML_RELAXNG_ONEORMORE:
2962 case XML_RELAXNG_CHOICE:
2963 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002964 case XML_RELAXNG_DEF:{
2965 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002966
Daniel Veillard4c004142003-10-07 11:33:24 +00002967 list = def->content;
2968 while (list != NULL) {
2969 ret = xmlRelaxNGIsCompileable(list);
2970 if (ret != 1)
2971 break;
2972 list = list->next;
2973 }
2974 break;
2975 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002976 case XML_RELAXNG_EXCEPT:
2977 case XML_RELAXNG_ATTRIBUTE:
2978 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002979 case XML_RELAXNG_DATATYPE:
2980 case XML_RELAXNG_LIST:
2981 case XML_RELAXNG_PARAM:
2982 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002983 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002984 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002985 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002986 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002987 if (ret == 0)
2988 def->dflags |= IS_NOT_COMPILABLE;
2989 if (ret == 1)
2990 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002991#ifdef DEBUG_COMPILE
2992 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002993 xmlGenericError(xmlGenericErrorContext,
2994 "RelaxNGIsCompileable %s : true\n",
2995 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002996 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002997 xmlGenericError(xmlGenericErrorContext,
2998 "RelaxNGIsCompileable %s : false\n",
2999 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003000 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003001 xmlGenericError(xmlGenericErrorContext,
3002 "Problem in RelaxNGIsCompileable %s\n",
3003 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003004 }
3005#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003006 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003007}
3008
3009/**
3010 * xmlRelaxNGCompile:
3011 * ctxt: the RelaxNG parser context
3012 * @define: the definition tree to compile
3013 *
3014 * Compile the set of definitions, it works recursively, till the
3015 * element boundaries, where it tries to compile the content if possible
3016 *
3017 * Returns 0 if success and -1 in case of error
3018 */
3019static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003020xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3021{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003022 int ret = 0;
3023 xmlRelaxNGDefinePtr list;
3024
Daniel Veillard4c004142003-10-07 11:33:24 +00003025 if ((ctxt == NULL) || (def == NULL))
3026 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003027
Daniel Veillard4c004142003-10-07 11:33:24 +00003028 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003029 case XML_RELAXNG_START:
3030 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003031 xmlAutomataPtr oldam = ctxt->am;
3032 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003033
3034 def->depth = -25;
3035
Daniel Veillard4c004142003-10-07 11:33:24 +00003036 list = def->content;
3037 ctxt->am = xmlNewAutomata();
3038 if (ctxt->am == NULL)
3039 return (-1);
3040 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3041 while (list != NULL) {
3042 xmlRelaxNGCompile(ctxt, list);
3043 list = list->next;
3044 }
3045 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3046 def->contModel = xmlAutomataCompile(ctxt->am);
3047 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003048
Daniel Veillard4c004142003-10-07 11:33:24 +00003049 xmlFreeAutomata(ctxt->am);
3050 ctxt->state = oldstate;
3051 ctxt->am = oldam;
3052 }
3053 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003054 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003055 if ((ctxt->am != NULL) && (def->name != NULL)) {
3056 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3057 ctxt->state, NULL,
3058 def->name, def->ns,
3059 def);
3060 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003061 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003062 xmlAutomataPtr oldam = ctxt->am;
3063 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003064
3065 def->depth = -25;
3066
Daniel Veillard4c004142003-10-07 11:33:24 +00003067 list = def->content;
3068 ctxt->am = xmlNewAutomata();
3069 if (ctxt->am == NULL)
3070 return (-1);
3071 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3072 while (list != NULL) {
3073 xmlRelaxNGCompile(ctxt, list);
3074 list = list->next;
3075 }
3076 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3077 def->contModel = xmlAutomataCompile(ctxt->am);
3078 if (!xmlRegexpIsDeterminist(def->contModel)) {
3079 /*
3080 * we can only use the automata if it is determinist
3081 */
3082 xmlRegFreeRegexp(def->contModel);
3083 def->contModel = NULL;
3084 }
3085 xmlFreeAutomata(ctxt->am);
3086 ctxt->state = oldstate;
3087 ctxt->am = oldam;
3088 } else {
3089 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003090
Daniel Veillard4c004142003-10-07 11:33:24 +00003091 /*
3092 * we can't build the content model for this element content
3093 * but it still might be possible to build it for some of its
3094 * children, recurse.
3095 */
3096 ret = xmlRelaxNGTryCompile(ctxt, def);
3097 ctxt->am = oldam;
3098 }
3099 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003100 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003101 ret = xmlRelaxNGCompile(ctxt, def->content);
3102 break;
3103 case XML_RELAXNG_OPTIONAL:{
3104 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003105
Daniel Veillardfd780772009-08-26 18:35:29 +02003106 list = def->content;
3107 while (list != NULL) {
3108 xmlRelaxNGCompile(ctxt, list);
3109 list = list->next;
3110 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003111 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3112 break;
3113 }
3114 case XML_RELAXNG_ZEROORMORE:{
3115 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003116
Daniel Veillard4c004142003-10-07 11:33:24 +00003117 ctxt->state =
3118 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3119 oldstate = ctxt->state;
3120 list = def->content;
3121 while (list != NULL) {
3122 xmlRelaxNGCompile(ctxt, list);
3123 list = list->next;
3124 }
3125 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3126 ctxt->state =
3127 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3128 break;
3129 }
3130 case XML_RELAXNG_ONEORMORE:{
3131 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003132
Daniel Veillard4c004142003-10-07 11:33:24 +00003133 list = def->content;
3134 while (list != NULL) {
3135 xmlRelaxNGCompile(ctxt, list);
3136 list = list->next;
3137 }
3138 oldstate = ctxt->state;
3139 list = def->content;
3140 while (list != NULL) {
3141 xmlRelaxNGCompile(ctxt, list);
3142 list = list->next;
3143 }
3144 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3145 ctxt->state =
3146 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3147 break;
3148 }
3149 case XML_RELAXNG_CHOICE:{
3150 xmlAutomataStatePtr target = NULL;
3151 xmlAutomataStatePtr oldstate = ctxt->state;
3152
3153 list = def->content;
3154 while (list != NULL) {
3155 ctxt->state = oldstate;
3156 ret = xmlRelaxNGCompile(ctxt, list);
3157 if (ret != 0)
3158 break;
3159 if (target == NULL)
3160 target = ctxt->state;
3161 else {
3162 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3163 target);
3164 }
3165 list = list->next;
3166 }
3167 ctxt->state = target;
3168
3169 break;
3170 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003171 case XML_RELAXNG_REF:
3172 case XML_RELAXNG_EXTERNALREF:
3173 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003174 case XML_RELAXNG_GROUP:
3175 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003176 list = def->content;
3177 while (list != NULL) {
3178 ret = xmlRelaxNGCompile(ctxt, list);
3179 if (ret != 0)
3180 break;
3181 list = list->next;
3182 }
3183 break;
3184 case XML_RELAXNG_TEXT:{
3185 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003186
Daniel Veillard4c004142003-10-07 11:33:24 +00003187 ctxt->state =
3188 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3189 oldstate = ctxt->state;
3190 xmlRelaxNGCompile(ctxt, def->content);
3191 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3192 ctxt->state, BAD_CAST "#text",
3193 NULL);
3194 ctxt->state =
3195 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3196 break;
3197 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003198 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003199 ctxt->state =
3200 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3201 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003202 case XML_RELAXNG_EXCEPT:
3203 case XML_RELAXNG_ATTRIBUTE:
3204 case XML_RELAXNG_INTERLEAVE:
3205 case XML_RELAXNG_NOT_ALLOWED:
3206 case XML_RELAXNG_DATATYPE:
3207 case XML_RELAXNG_LIST:
3208 case XML_RELAXNG_PARAM:
3209 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003210 /* This should not happen and generate an internal error */
3211 fprintf(stderr, "RNG internal error trying to compile %s\n",
3212 xmlRelaxNGDefName(def));
3213 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003214 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003215 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003216}
3217
3218/**
3219 * xmlRelaxNGTryCompile:
3220 * ctxt: the RelaxNG parser context
3221 * @define: the definition tree to compile
3222 *
3223 * Try to compile the set of definitions, it works recursively,
3224 * possibly ignoring parts which cannot be compiled.
3225 *
3226 * Returns 0 if success and -1 in case of error
3227 */
3228static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003229xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3230{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003231 int ret = 0;
3232 xmlRelaxNGDefinePtr list;
3233
Daniel Veillard4c004142003-10-07 11:33:24 +00003234 if ((ctxt == NULL) || (def == NULL))
3235 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003236
3237 if ((def->type == XML_RELAXNG_START) ||
3238 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003239 ret = xmlRelaxNGIsCompileable(def);
3240 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3241 ctxt->am = NULL;
3242 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003243#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003244 if (ret == 0) {
3245 if (def->type == XML_RELAXNG_START)
3246 xmlGenericError(xmlGenericErrorContext,
3247 "compiled the start\n");
3248 else
3249 xmlGenericError(xmlGenericErrorContext,
3250 "compiled element %s\n", def->name);
3251 } else {
3252 if (def->type == XML_RELAXNG_START)
3253 xmlGenericError(xmlGenericErrorContext,
3254 "failed to compile the start\n");
3255 else
3256 xmlGenericError(xmlGenericErrorContext,
3257 "failed to compile element %s\n",
3258 def->name);
3259 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003260#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003261 return (ret);
3262 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003263 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003264 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003265 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003266 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3267 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003268 case XML_RELAXNG_TEXT:
3269 case XML_RELAXNG_DATATYPE:
3270 case XML_RELAXNG_LIST:
3271 case XML_RELAXNG_PARAM:
3272 case XML_RELAXNG_VALUE:
3273 case XML_RELAXNG_EMPTY:
3274 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003275 ret = 0;
3276 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003277 case XML_RELAXNG_OPTIONAL:
3278 case XML_RELAXNG_ZEROORMORE:
3279 case XML_RELAXNG_ONEORMORE:
3280 case XML_RELAXNG_CHOICE:
3281 case XML_RELAXNG_GROUP:
3282 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003283 case XML_RELAXNG_START:
3284 case XML_RELAXNG_REF:
3285 case XML_RELAXNG_EXTERNALREF:
3286 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003287 list = def->content;
3288 while (list != NULL) {
3289 ret = xmlRelaxNGTryCompile(ctxt, list);
3290 if (ret != 0)
3291 break;
3292 list = list->next;
3293 }
3294 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003295 case XML_RELAXNG_EXCEPT:
3296 case XML_RELAXNG_ATTRIBUTE:
3297 case XML_RELAXNG_INTERLEAVE:
3298 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003299 ret = 0;
3300 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003301 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003302 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003303}
3304
3305/************************************************************************
3306 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003307 * Parsing functions *
3308 * *
3309 ************************************************************************/
3310
Daniel Veillard4c004142003-10-07 11:33:24 +00003311static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3312 ctxt, xmlNodePtr node);
3313static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3314 ctxt, xmlNodePtr node);
3315static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3316 ctxt, xmlNodePtr nodes,
3317 int group);
3318static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3319 ctxt, xmlNodePtr node);
3320static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3321 xmlNodePtr node);
3322static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3323 xmlNodePtr nodes);
3324static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3325 ctxt, xmlNodePtr node,
3326 xmlRelaxNGDefinePtr
3327 def);
3328static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3329 ctxt, xmlNodePtr nodes);
3330static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3331 xmlRelaxNGDefinePtr define,
3332 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003333
3334
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003335#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003336
3337/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003338 * xmlRelaxNGIsNullable:
3339 * @define: the definition to verify
3340 *
3341 * Check if a definition is nullable.
3342 *
3343 * Returns 1 if yes, 0 if no and -1 in case of error
3344 */
3345static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003346xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3347{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003348 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003349
Daniel Veillardfd573f12003-03-16 17:52:32 +00003350 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003351 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003352
Daniel Veillarde063f482003-03-21 16:53:17 +00003353 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003354 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003355 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003356 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003357 switch (define->type) {
3358 case XML_RELAXNG_EMPTY:
3359 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003360 ret = 1;
3361 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003362 case XML_RELAXNG_NOOP:
3363 case XML_RELAXNG_DEF:
3364 case XML_RELAXNG_REF:
3365 case XML_RELAXNG_EXTERNALREF:
3366 case XML_RELAXNG_PARENTREF:
3367 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003368 ret = xmlRelaxNGIsNullable(define->content);
3369 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003370 case XML_RELAXNG_EXCEPT:
3371 case XML_RELAXNG_NOT_ALLOWED:
3372 case XML_RELAXNG_ELEMENT:
3373 case XML_RELAXNG_DATATYPE:
3374 case XML_RELAXNG_PARAM:
3375 case XML_RELAXNG_VALUE:
3376 case XML_RELAXNG_LIST:
3377 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003378 ret = 0;
3379 break;
3380 case XML_RELAXNG_CHOICE:{
3381 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003382
Daniel Veillard4c004142003-10-07 11:33:24 +00003383 while (list != NULL) {
3384 ret = xmlRelaxNGIsNullable(list);
3385 if (ret != 0)
3386 goto done;
3387 list = list->next;
3388 }
3389 ret = 0;
3390 break;
3391 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003392 case XML_RELAXNG_START:
3393 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003394 case XML_RELAXNG_GROUP:{
3395 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003396
Daniel Veillard4c004142003-10-07 11:33:24 +00003397 while (list != NULL) {
3398 ret = xmlRelaxNGIsNullable(list);
3399 if (ret != 1)
3400 goto done;
3401 list = list->next;
3402 }
3403 return (1);
3404 }
3405 default:
3406 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003407 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003408 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003409 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003410 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003411 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003412 define->dflags |= IS_NULLABLE;
3413 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003414}
3415
3416/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003417 * xmlRelaxNGIsBlank:
3418 * @str: a string
3419 *
3420 * Check if a string is ignorable c.f. 4.2. Whitespace
3421 *
3422 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3423 */
3424static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003425xmlRelaxNGIsBlank(xmlChar * str)
3426{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003427 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003428 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003429 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003430 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003431 return (0);
3432 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003433 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003434 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003435}
3436
Daniel Veillard6eadf632003-01-23 18:29:16 +00003437/**
3438 * xmlRelaxNGGetDataTypeLibrary:
3439 * @ctxt: a Relax-NG parser context
3440 * @node: the current data or value element
3441 *
3442 * Applies algorithm from 4.3. datatypeLibrary attribute
3443 *
3444 * Returns the datatypeLibary value or NULL if not found
3445 */
3446static xmlChar *
3447xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003448 xmlNodePtr node)
3449{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003450 xmlChar *ret, *escape;
3451
Daniel Veillard6eadf632003-01-23 18:29:16 +00003452 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003453 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3454 if (ret != NULL) {
3455 if (ret[0] == 0) {
3456 xmlFree(ret);
3457 return (NULL);
3458 }
3459 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3460 if (escape == NULL) {
3461 return (ret);
3462 }
3463 xmlFree(ret);
3464 return (escape);
3465 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003466 }
3467 node = node->parent;
3468 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003469 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3470 if (ret != NULL) {
3471 if (ret[0] == 0) {
3472 xmlFree(ret);
3473 return (NULL);
3474 }
3475 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3476 if (escape == NULL) {
3477 return (ret);
3478 }
3479 xmlFree(ret);
3480 return (escape);
3481 }
3482 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003483 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003484 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003485}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003486
3487/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003488 * xmlRelaxNGParseValue:
3489 * @ctxt: a Relax-NG parser context
3490 * @node: the data node.
3491 *
3492 * parse the content of a RelaxNG value node.
3493 *
3494 * Returns the definition pointer or NULL in case of error
3495 */
3496static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003497xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3498{
Daniel Veillardedc91922003-01-26 00:52:04 +00003499 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003500 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003501 xmlChar *type;
3502 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003503 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003504
Daniel Veillardfd573f12003-03-16 17:52:32 +00003505 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003506 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003507 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003508 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003509
3510 type = xmlGetProp(node, BAD_CAST "type");
3511 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003512 xmlRelaxNGNormExtSpace(type);
3513 if (xmlValidateNCName(type, 0)) {
3514 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3515 "value type '%s' is not an NCName\n", type, NULL);
3516 }
3517 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3518 if (library == NULL)
3519 library =
3520 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003521
Daniel Veillard4c004142003-10-07 11:33:24 +00003522 def->name = type;
3523 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003524
Daniel Veillard4c004142003-10-07 11:33:24 +00003525 lib = (xmlRelaxNGTypeLibraryPtr)
3526 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3527 if (lib == NULL) {
3528 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3529 "Use of unregistered type library '%s'\n", library,
3530 NULL);
3531 def->data = NULL;
3532 } else {
3533 def->data = lib;
3534 if (lib->have == NULL) {
3535 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3536 "Internal error with type library '%s': no 'have'\n",
3537 library, NULL);
3538 } else {
3539 success = lib->have(lib->data, def->name);
3540 if (success != 1) {
3541 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3542 "Error type '%s' is not exported by type library '%s'\n",
3543 def->name, library);
3544 }
3545 }
3546 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003547 }
3548 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003549 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003550 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003551 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3552 (node->children->next != NULL)) {
3553 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3554 "Expecting a single text value for <value>content\n",
3555 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003556 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003557 def->value = xmlNodeGetContent(node);
3558 if (def->value == NULL) {
3559 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3560 "Element <value> has no content\n", NULL, NULL);
3561 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3562 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003563
Daniel Veillard4c004142003-10-07 11:33:24 +00003564 success =
3565 lib->check(lib->data, def->name, def->value, &val, node);
3566 if (success != 1) {
3567 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3568 "Value '%s' is not acceptable for type '%s'\n",
3569 def->value, def->name);
3570 } else {
3571 if (val != NULL)
3572 def->attrs = val;
3573 }
3574 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003575 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003576 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003577}
3578
3579/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003580 * xmlRelaxNGParseData:
3581 * @ctxt: a Relax-NG parser context
3582 * @node: the data node.
3583 *
3584 * parse the content of a RelaxNG data node.
3585 *
3586 * Returns the definition pointer or NULL in case of error
3587 */
3588static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003589xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3590{
Daniel Veillard14b56432006-03-09 18:41:40 +00003591 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003592 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003593 xmlRelaxNGTypeLibraryPtr lib;
3594 xmlChar *type;
3595 xmlChar *library;
3596 xmlNodePtr content;
3597 int tmp;
3598
3599 type = xmlGetProp(node, BAD_CAST "type");
3600 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003601 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3602 NULL);
3603 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003604 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003605 xmlRelaxNGNormExtSpace(type);
3606 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003607 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3608 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003609 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003610 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3611 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003612 library =
3613 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003614
Daniel Veillardfd573f12003-03-16 17:52:32 +00003615 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003616 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003617 xmlFree(type);
3618 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003619 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003620 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003621 def->name = type;
3622 def->ns = library;
3623
3624 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003625 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003626 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003627 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3628 "Use of unregistered type library '%s'\n", library,
3629 NULL);
3630 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003631 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003632 def->data = lib;
3633 if (lib->have == NULL) {
3634 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3635 "Internal error with type library '%s': no 'have'\n",
3636 library, NULL);
3637 } else {
3638 tmp = lib->have(lib->data, def->name);
3639 if (tmp != 1) {
3640 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3641 "Error type '%s' is not exported by type library '%s'\n",
3642 def->name, library);
3643 } else
3644 if ((xmlStrEqual
3645 (library,
3646 BAD_CAST
3647 "http://www.w3.org/2001/XMLSchema-datatypes"))
3648 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3649 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3650 ctxt->idref = 1;
3651 }
3652 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003653 }
3654 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003655
3656 /*
3657 * Handle optional params
3658 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003659 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003660 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3661 break;
3662 if (xmlStrEqual(library,
3663 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3664 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3665 "Type library '%s' does not allow type parameters\n",
3666 library, NULL);
3667 content = content->next;
3668 while ((content != NULL) &&
3669 (xmlStrEqual(content->name, BAD_CAST "param")))
3670 content = content->next;
3671 } else {
3672 param = xmlRelaxNGNewDefine(ctxt, node);
3673 if (param != NULL) {
3674 param->type = XML_RELAXNG_PARAM;
3675 param->name = xmlGetProp(content, BAD_CAST "name");
3676 if (param->name == NULL) {
3677 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3678 "param has no name\n", NULL, NULL);
3679 }
3680 param->value = xmlNodeGetContent(content);
3681 if (lastparam == NULL) {
3682 def->attrs = lastparam = param;
3683 } else {
3684 lastparam->next = param;
3685 lastparam = param;
3686 }
3687 if (lib != NULL) {
3688 }
3689 }
3690 content = content->next;
3691 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003692 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003693 /*
3694 * Handle optional except
3695 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003696 if ((content != NULL)
3697 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3698 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003699 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003700
Daniel Veillard4c004142003-10-07 11:33:24 +00003701 except = xmlRelaxNGNewDefine(ctxt, node);
3702 if (except == NULL) {
3703 return (def);
3704 }
3705 except->type = XML_RELAXNG_EXCEPT;
3706 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003707 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003708 if (child == NULL) {
3709 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3710 "except has no content\n", NULL, NULL);
3711 }
3712 while (child != NULL) {
3713 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3714 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003715 if (last == NULL) {
3716 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003717 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003718 last->next = tmp2;
3719 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003720 }
3721 }
3722 child = child->next;
3723 }
3724 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003725 }
3726 /*
3727 * Check there is no unhandled data
3728 */
3729 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003730 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3731 "Element data has unexpected content %s\n",
3732 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003733 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003734
Daniel Veillard4c004142003-10-07 11:33:24 +00003735 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003736}
3737
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003738static const xmlChar *invalidName = BAD_CAST "\1";
3739
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003740/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003741 * xmlRelaxNGCompareNameClasses:
3742 * @defs1: the first element/attribute defs
3743 * @defs2: the second element/attribute defs
3744 * @name: the restriction on the name
3745 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003746 *
3747 * Compare the 2 lists of element definitions. The comparison is
3748 * that if both lists do not accept the same QNames, it returns 1
3749 * If the 2 lists can accept the same QName the comparison returns 0
3750 *
3751 * Returns 1 disttinct, 0 if equal
3752 */
3753static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003754xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003755 xmlRelaxNGDefinePtr def2)
3756{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003757 int ret = 1;
3758 xmlNode node;
3759 xmlNs ns;
3760 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003761
Daniel Veillard42f12e92003-03-07 18:32:59 +00003762 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3763
Daniel Veillardb30ca312005-09-04 13:50:03 +00003764 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3765
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003766 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003767 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3768 if (def2->type == XML_RELAXNG_TEXT)
3769 return (1);
3770 if (def1->name != NULL) {
3771 node.name = def1->name;
3772 } else {
3773 node.name = invalidName;
3774 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003775 if (def1->ns != NULL) {
3776 if (def1->ns[0] == 0) {
3777 node.ns = NULL;
3778 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003779 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003780 ns.href = def1->ns;
3781 }
3782 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003783 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003784 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003785 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003786 if (def1->nameClass != NULL) {
3787 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3788 } else {
3789 ret = 0;
3790 }
3791 } else {
3792 ret = 1;
3793 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003794 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003795 if (def2->type == XML_RELAXNG_TEXT)
3796 return (0);
3797 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003798 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003799 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003800 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003801 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003802 }
3803 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003804 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003805 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003806 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3807 if (def2->name != NULL) {
3808 node.name = def2->name;
3809 } else {
3810 node.name = invalidName;
3811 }
3812 node.ns = &ns;
3813 if (def2->ns != NULL) {
3814 if (def2->ns[0] == 0) {
3815 node.ns = NULL;
3816 } else {
3817 ns.href = def2->ns;
3818 }
3819 } else {
3820 ns.href = invalidName;
3821 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003822 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003823 if (def2->nameClass != NULL) {
3824 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3825 } else {
3826 ret = 0;
3827 }
3828 } else {
3829 ret = 1;
3830 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003831 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003832 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003833 }
3834
Daniel Veillard4c004142003-10-07 11:33:24 +00003835 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003836}
3837
3838/**
3839 * xmlRelaxNGCompareElemDefLists:
3840 * @ctxt: a Relax-NG parser context
3841 * @defs1: the first list of element/attribute defs
3842 * @defs2: the second list of element/attribute defs
3843 *
3844 * Compare the 2 lists of element or attribute definitions. The comparison
3845 * is that if both lists do not accept the same QNames, it returns 1
3846 * If the 2 lists can accept the same QName the comparison returns 0
3847 *
3848 * Returns 1 disttinct, 0 if equal
3849 */
3850static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003851xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3852 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3853 xmlRelaxNGDefinePtr * def2)
3854{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003855 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003856
Daniel Veillard154877e2003-01-30 12:17:05 +00003857 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003858 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003859 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003860 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003861 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003862 while ((*def2) != NULL) {
3863 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3864 return (0);
3865 def2++;
3866 }
3867 def2 = basedef2;
3868 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003869 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003870 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003871}
3872
3873/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003874 * xmlRelaxNGGenerateAttributes:
3875 * @ctxt: a Relax-NG parser context
3876 * @def: the definition definition
3877 *
3878 * Check if the definition can only generate attributes
3879 *
3880 * Returns 1 if yes, 0 if no and -1 in case of error.
3881 */
3882static int
3883xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003884 xmlRelaxNGDefinePtr def)
3885{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003886 xmlRelaxNGDefinePtr parent, cur, tmp;
3887
3888 /*
3889 * Don't run that check in case of error. Infinite recursion
3890 * becomes possible.
3891 */
3892 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003893 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003894
3895 parent = NULL;
3896 cur = def;
3897 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003898 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3899 (cur->type == XML_RELAXNG_TEXT) ||
3900 (cur->type == XML_RELAXNG_DATATYPE) ||
3901 (cur->type == XML_RELAXNG_PARAM) ||
3902 (cur->type == XML_RELAXNG_LIST) ||
3903 (cur->type == XML_RELAXNG_VALUE) ||
3904 (cur->type == XML_RELAXNG_EMPTY))
3905 return (0);
3906 if ((cur->type == XML_RELAXNG_CHOICE) ||
3907 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3908 (cur->type == XML_RELAXNG_GROUP) ||
3909 (cur->type == XML_RELAXNG_ONEORMORE) ||
3910 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3911 (cur->type == XML_RELAXNG_OPTIONAL) ||
3912 (cur->type == XML_RELAXNG_PARENTREF) ||
3913 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3914 (cur->type == XML_RELAXNG_REF) ||
3915 (cur->type == XML_RELAXNG_DEF)) {
3916 if (cur->content != NULL) {
3917 parent = cur;
3918 cur = cur->content;
3919 tmp = cur;
3920 while (tmp != NULL) {
3921 tmp->parent = parent;
3922 tmp = tmp->next;
3923 }
3924 continue;
3925 }
3926 }
3927 if (cur == def)
3928 break;
3929 if (cur->next != NULL) {
3930 cur = cur->next;
3931 continue;
3932 }
3933 do {
3934 cur = cur->parent;
3935 if (cur == NULL)
3936 break;
3937 if (cur == def)
3938 return (1);
3939 if (cur->next != NULL) {
3940 cur = cur->next;
3941 break;
3942 }
3943 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003944 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003945 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003946}
Daniel Veillard4c004142003-10-07 11:33:24 +00003947
Daniel Veillardce192eb2003-04-16 15:58:05 +00003948/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003949 * xmlRelaxNGGetElements:
3950 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003951 * @def: the definition definition
3952 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003953 *
3954 * Compute the list of top elements a definition can generate
3955 *
3956 * Returns a list of elements or NULL if none was found.
3957 */
3958static xmlRelaxNGDefinePtr *
3959xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003960 xmlRelaxNGDefinePtr def, int eora)
3961{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003962 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003963 int len = 0;
3964 int max = 0;
3965
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003966 /*
3967 * Don't run that check in case of error. Infinite recursion
3968 * becomes possible.
3969 */
3970 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003971 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003972
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003973 parent = NULL;
3974 cur = def;
3975 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003976 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3977 (cur->type == XML_RELAXNG_TEXT))) ||
3978 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3979 if (ret == NULL) {
3980 max = 10;
3981 ret = (xmlRelaxNGDefinePtr *)
3982 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3983 if (ret == NULL) {
3984 xmlRngPErrMemory(ctxt, "getting element list\n");
3985 return (NULL);
3986 }
3987 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00003988 xmlRelaxNGDefinePtr *temp;
3989
Daniel Veillard4c004142003-10-07 11:33:24 +00003990 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00003991 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00003992 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00003993 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003994 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00003995 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00003996 return (NULL);
3997 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00003998 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00003999 }
4000 ret[len++] = cur;
4001 ret[len] = NULL;
4002 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4003 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4004 (cur->type == XML_RELAXNG_GROUP) ||
4005 (cur->type == XML_RELAXNG_ONEORMORE) ||
4006 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4007 (cur->type == XML_RELAXNG_OPTIONAL) ||
4008 (cur->type == XML_RELAXNG_PARENTREF) ||
4009 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004010 (cur->type == XML_RELAXNG_DEF) ||
4011 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004012 /*
4013 * Don't go within elements or attributes or string values.
4014 * Just gather the element top list
4015 */
4016 if (cur->content != NULL) {
4017 parent = cur;
4018 cur = cur->content;
4019 tmp = cur;
4020 while (tmp != NULL) {
4021 tmp->parent = parent;
4022 tmp = tmp->next;
4023 }
4024 continue;
4025 }
4026 }
4027 if (cur == def)
4028 break;
4029 if (cur->next != NULL) {
4030 cur = cur->next;
4031 continue;
4032 }
4033 do {
4034 cur = cur->parent;
4035 if (cur == NULL)
4036 break;
4037 if (cur == def)
4038 return (ret);
4039 if (cur->next != NULL) {
4040 cur = cur->next;
4041 break;
4042 }
4043 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004044 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004045 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004046}
Daniel Veillard4c004142003-10-07 11:33:24 +00004047
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004048/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004049 * xmlRelaxNGCheckChoiceDeterminism:
4050 * @ctxt: a Relax-NG parser context
4051 * @def: the choice definition
4052 *
4053 * Also used to find indeterministic pattern in choice
4054 */
4055static void
4056xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004057 xmlRelaxNGDefinePtr def)
4058{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004059 xmlRelaxNGDefinePtr **list;
4060 xmlRelaxNGDefinePtr cur;
4061 int nbchild = 0, i, j, ret;
4062 int is_nullable = 0;
4063 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004064 xmlHashTablePtr triage = NULL;
4065 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004066
Daniel Veillard4c004142003-10-07 11:33:24 +00004067 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4068 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004069
Daniel Veillarde063f482003-03-21 16:53:17 +00004070 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004071 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004072
Daniel Veillardfd573f12003-03-16 17:52:32 +00004073 /*
4074 * Don't run that check in case of error. Infinite recursion
4075 * becomes possible.
4076 */
4077 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004078 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004079
4080 is_nullable = xmlRelaxNGIsNullable(def);
4081
4082 cur = def->content;
4083 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004084 nbchild++;
4085 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004086 }
4087
4088 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004089 sizeof(xmlRelaxNGDefinePtr
4090 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004091 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004092 xmlRngPErrMemory(ctxt, "building choice\n");
4093 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004094 }
4095 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004096 /*
4097 * a bit strong but safe
4098 */
4099 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004100 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004101 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004102 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004103 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004104 cur = def->content;
4105 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004106 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4107 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4108 is_triable = 0;
4109 } else if (is_triable == 1) {
4110 xmlRelaxNGDefinePtr *tmp;
4111 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004112
Daniel Veillard4c004142003-10-07 11:33:24 +00004113 tmp = list[i];
4114 while ((*tmp != NULL) && (is_triable == 1)) {
4115 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4116 res = xmlHashAddEntry2(triage,
4117 BAD_CAST "#text", NULL,
4118 (void *) cur);
4119 if (res != 0)
4120 is_triable = -1;
4121 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4122 ((*tmp)->name != NULL)) {
4123 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4124 res = xmlHashAddEntry2(triage,
4125 (*tmp)->name, NULL,
4126 (void *) cur);
4127 else
4128 res = xmlHashAddEntry2(triage,
4129 (*tmp)->name, (*tmp)->ns,
4130 (void *) cur);
4131 if (res != 0)
4132 is_triable = -1;
4133 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4134 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4135 res = xmlHashAddEntry2(triage,
4136 BAD_CAST "#any", NULL,
4137 (void *) cur);
4138 else
4139 res = xmlHashAddEntry2(triage,
4140 BAD_CAST "#any", (*tmp)->ns,
4141 (void *) cur);
4142 if (res != 0)
4143 is_triable = -1;
4144 } else {
4145 is_triable = -1;
4146 }
4147 tmp++;
4148 }
4149 }
4150 i++;
4151 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004152 }
4153
Daniel Veillard4c004142003-10-07 11:33:24 +00004154 for (i = 0; i < nbchild; i++) {
4155 if (list[i] == NULL)
4156 continue;
4157 for (j = 0; j < i; j++) {
4158 if (list[j] == NULL)
4159 continue;
4160 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4161 if (ret == 0) {
4162 is_indeterminist = 1;
4163 }
4164 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004165 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004166 for (i = 0; i < nbchild; i++) {
4167 if (list[i] != NULL)
4168 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004169 }
4170
4171 xmlFree(list);
4172 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004173 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004174 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004175 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004176 def->dflags |= IS_TRIABLE;
4177 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004178 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004179 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004180 }
4181 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004182}
4183
4184/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004185 * xmlRelaxNGCheckGroupAttrs:
4186 * @ctxt: a Relax-NG parser context
4187 * @def: the group definition
4188 *
4189 * Detects violations of rule 7.3
4190 */
4191static void
4192xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004193 xmlRelaxNGDefinePtr def)
4194{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004195 xmlRelaxNGDefinePtr **list;
4196 xmlRelaxNGDefinePtr cur;
4197 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004198
4199 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004200 ((def->type != XML_RELAXNG_GROUP) &&
4201 (def->type != XML_RELAXNG_ELEMENT)))
4202 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004203
Daniel Veillarde063f482003-03-21 16:53:17 +00004204 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004205 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004206
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004207 /*
4208 * Don't run that check in case of error. Infinite recursion
4209 * becomes possible.
4210 */
4211 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004212 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004213
Daniel Veillardfd573f12003-03-16 17:52:32 +00004214 cur = def->attrs;
4215 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004216 nbchild++;
4217 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004218 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004219 cur = def->content;
4220 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004221 nbchild++;
4222 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004223 }
4224
4225 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004226 sizeof(xmlRelaxNGDefinePtr
4227 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004228 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004229 xmlRngPErrMemory(ctxt, "building group\n");
4230 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004231 }
4232 i = 0;
4233 cur = def->attrs;
4234 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004235 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4236 i++;
4237 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004238 }
4239 cur = def->content;
4240 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004241 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4242 i++;
4243 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004244 }
4245
Daniel Veillard4c004142003-10-07 11:33:24 +00004246 for (i = 0; i < nbchild; i++) {
4247 if (list[i] == NULL)
4248 continue;
4249 for (j = 0; j < i; j++) {
4250 if (list[j] == NULL)
4251 continue;
4252 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4253 if (ret == 0) {
4254 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4255 "Attributes conflicts in group\n", NULL, NULL);
4256 }
4257 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004258 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004259 for (i = 0; i < nbchild; i++) {
4260 if (list[i] != NULL)
4261 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004262 }
4263
4264 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004265 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004266}
4267
4268/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004269 * xmlRelaxNGComputeInterleaves:
4270 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004271 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004272 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004273 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004274 * A lot of work for preprocessing interleave definitions
4275 * is potentially needed to get a decent execution speed at runtime
4276 * - trying to get a total order on the element nodes generated
4277 * by the interleaves, order the list of interleave definitions
4278 * following that order.
4279 * - if <text/> is used to handle mixed content, it is better to
4280 * flag this in the define and simplify the runtime checking
4281 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004282 */
4283static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004284xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004285 xmlRelaxNGParserCtxtPtr ctxt,
4286 xmlChar * name ATTRIBUTE_UNUSED)
4287{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004288 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004289
Daniel Veillardfd573f12003-03-16 17:52:32 +00004290 xmlRelaxNGPartitionPtr partitions = NULL;
4291 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4292 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004293 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004294 int nbgroups = 0;
4295 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004296 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004297 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004298
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004299 /*
4300 * Don't run that check in case of error. Infinite recursion
4301 * becomes possible.
4302 */
4303 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004304 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004305
Daniel Veillardfd573f12003-03-16 17:52:32 +00004306#ifdef DEBUG_INTERLEAVE
4307 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004308 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004309#endif
4310 cur = def->content;
4311 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004312 nbchild++;
4313 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004314 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004315
Daniel Veillardfd573f12003-03-16 17:52:32 +00004316#ifdef DEBUG_INTERLEAVE
4317 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4318#endif
4319 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004320 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004321 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004322 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004323 cur = def->content;
4324 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004325 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4326 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4327 if (groups[nbgroups] == NULL)
4328 goto error;
4329 if (cur->type == XML_RELAXNG_TEXT)
4330 is_mixed++;
4331 groups[nbgroups]->rule = cur;
4332 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4333 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4334 nbgroups++;
4335 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004336 }
4337#ifdef DEBUG_INTERLEAVE
4338 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4339#endif
4340
4341 /*
4342 * Let's check that all rules makes a partitions according to 7.4
4343 */
4344 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004345 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004346 if (partitions == NULL)
4347 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004348 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004349 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004350 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004351 for (i = 0; i < nbgroups; i++) {
4352 group = groups[i];
4353 for (j = i + 1; j < nbgroups; j++) {
4354 if (groups[j] == NULL)
4355 continue;
4356
4357 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4358 groups[j]->defs);
4359 if (ret == 0) {
4360 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4361 "Element or text conflicts in interleave\n",
4362 NULL, NULL);
4363 }
4364 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4365 groups[j]->attrs);
4366 if (ret == 0) {
4367 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4368 "Attributes conflicts in interleave\n", NULL,
4369 NULL);
4370 }
4371 }
4372 tmp = group->defs;
4373 if ((tmp != NULL) && (*tmp != NULL)) {
4374 while (*tmp != NULL) {
4375 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4376 res = xmlHashAddEntry2(partitions->triage,
4377 BAD_CAST "#text", NULL,
4378 (void *) (long) (i + 1));
4379 if (res != 0)
4380 is_determinist = -1;
4381 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4382 ((*tmp)->name != NULL)) {
4383 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4384 res = xmlHashAddEntry2(partitions->triage,
4385 (*tmp)->name, NULL,
4386 (void *) (long) (i + 1));
4387 else
4388 res = xmlHashAddEntry2(partitions->triage,
4389 (*tmp)->name, (*tmp)->ns,
4390 (void *) (long) (i + 1));
4391 if (res != 0)
4392 is_determinist = -1;
4393 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4394 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4395 res = xmlHashAddEntry2(partitions->triage,
4396 BAD_CAST "#any", NULL,
4397 (void *) (long) (i + 1));
4398 else
4399 res = xmlHashAddEntry2(partitions->triage,
4400 BAD_CAST "#any", (*tmp)->ns,
4401 (void *) (long) (i + 1));
4402 if ((*tmp)->nameClass != NULL)
4403 is_determinist = 2;
4404 if (res != 0)
4405 is_determinist = -1;
4406 } else {
4407 is_determinist = -1;
4408 }
4409 tmp++;
4410 }
4411 } else {
4412 is_determinist = 0;
4413 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004414 }
4415 partitions->groups = groups;
4416
4417 /*
4418 * and save the partition list back in the def
4419 */
4420 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004421 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004422 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004423 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004424 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004425 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004426 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004427 return;
4428
Daniel Veillard4c004142003-10-07 11:33:24 +00004429 error:
4430 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004431 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004432 for (i = 0; i < nbgroups; i++)
4433 if (groups[i] != NULL) {
4434 if (groups[i]->defs != NULL)
4435 xmlFree(groups[i]->defs);
4436 xmlFree(groups[i]);
4437 }
4438 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004439 }
4440 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004441}
4442
4443/**
4444 * xmlRelaxNGParseInterleave:
4445 * @ctxt: a Relax-NG parser context
4446 * @node: the data node.
4447 *
4448 * parse the content of a RelaxNG interleave node.
4449 *
4450 * Returns the definition pointer or NULL in case of error
4451 */
4452static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004453xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4454{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004455 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004456 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004457 xmlNodePtr child;
4458
Daniel Veillardfd573f12003-03-16 17:52:32 +00004459 def = xmlRelaxNGNewDefine(ctxt, node);
4460 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004461 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004462 }
4463 def->type = XML_RELAXNG_INTERLEAVE;
4464
4465 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004466 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004467 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004468 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004469 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004470 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004471
Daniel Veillard4c004142003-10-07 11:33:24 +00004472 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4473 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4474 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4475 "Failed to add %s to hash table\n",
4476 (const xmlChar *) name, NULL);
4477 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004478 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004479 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004480 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004481 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4482 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004483 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004484 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004485 if (IS_RELAXNG(child, "element")) {
4486 cur = xmlRelaxNGParseElement(ctxt, child);
4487 } else {
4488 cur = xmlRelaxNGParsePattern(ctxt, child);
4489 }
4490 if (cur != NULL) {
4491 cur->parent = def;
4492 if (last == NULL) {
4493 def->content = last = cur;
4494 } else {
4495 last->next = cur;
4496 last = cur;
4497 }
4498 }
4499 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004500 }
4501
Daniel Veillard4c004142003-10-07 11:33:24 +00004502 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004503}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004504
4505/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004506 * xmlRelaxNGParseInclude:
4507 * @ctxt: a Relax-NG parser context
4508 * @node: the include node
4509 *
4510 * Integrate the content of an include node in the current grammar
4511 *
4512 * Returns 0 in case of success or -1 in case of error
4513 */
4514static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004515xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4516{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004517 xmlRelaxNGIncludePtr incl;
4518 xmlNodePtr root;
4519 int ret = 0, tmp;
4520
Daniel Veillard807daf82004-02-22 22:13:27 +00004521 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004522 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004523 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4524 "Include node has no data\n", NULL, NULL);
4525 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004526 }
4527 root = xmlDocGetRootElement(incl->doc);
4528 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004529 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4530 NULL, NULL);
4531 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004532 }
4533 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004534 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4535 "Include document root is not a grammar\n", NULL, NULL);
4536 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004537 }
4538
4539 /*
4540 * Merge the definition from both the include and the internal list
4541 */
4542 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004543 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4544 if (tmp != 0)
4545 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004546 }
4547 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004548 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4549 if (tmp != 0)
4550 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004551 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004552 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004553}
4554
4555/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004556 * xmlRelaxNGParseDefine:
4557 * @ctxt: a Relax-NG parser context
4558 * @node: the define node
4559 *
4560 * parse the content of a RelaxNG define element node.
4561 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004562 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004563 */
4564static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004565xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4566{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004567 xmlChar *name;
4568 int ret = 0, tmp;
4569 xmlRelaxNGDefinePtr def;
4570 const xmlChar *olddefine;
4571
4572 name = xmlGetProp(node, BAD_CAST "name");
4573 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004574 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4575 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004576 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004577 xmlRelaxNGNormExtSpace(name);
4578 if (xmlValidateNCName(name, 0)) {
4579 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4580 "define name '%s' is not an NCName\n", name, NULL);
4581 }
4582 def = xmlRelaxNGNewDefine(ctxt, node);
4583 if (def == NULL) {
4584 xmlFree(name);
4585 return (-1);
4586 }
4587 def->type = XML_RELAXNG_DEF;
4588 def->name = name;
4589 if (node->children == NULL) {
4590 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4591 "define has no children\n", NULL, NULL);
4592 } else {
4593 olddefine = ctxt->define;
4594 ctxt->define = name;
4595 def->content =
4596 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4597 ctxt->define = olddefine;
4598 }
4599 if (ctxt->grammar->defs == NULL)
4600 ctxt->grammar->defs = xmlHashCreate(10);
4601 if (ctxt->grammar->defs == NULL) {
4602 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4603 "Could not create definition hash\n", NULL, NULL);
4604 ret = -1;
4605 } else {
4606 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4607 if (tmp < 0) {
4608 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004609
Daniel Veillard4c004142003-10-07 11:33:24 +00004610 prev = xmlHashLookup(ctxt->grammar->defs, name);
4611 if (prev == NULL) {
4612 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4613 "Internal error on define aggregation of %s\n",
4614 name, NULL);
4615 ret = -1;
4616 } else {
4617 while (prev->nextHash != NULL)
4618 prev = prev->nextHash;
4619 prev->nextHash = def;
4620 }
4621 }
4622 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004623 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004624 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004625}
4626
4627/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004628 * xmlRelaxNGParseImportRef:
4629 * @payload: the parser context
4630 * @data: the current grammar
4631 * @name: the reference name
4632 *
4633 * Import import one references into the current grammar
4634 */
4635static void
4636xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4637 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4638 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4639 int tmp;
4640
4641 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4642 if (tmp < 0) {
4643 xmlRelaxNGDefinePtr prev;
4644
4645 prev = (xmlRelaxNGDefinePtr)
4646 xmlHashLookup(ctxt->grammar->refs, def->name);
4647 if (prev == NULL) {
4648 if (def->name != NULL) {
4649 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4650 "Error refs definitions '%s'\n",
4651 def->name, NULL);
4652 } else {
4653 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4654 "Error refs definitions\n",
4655 NULL, NULL);
4656 }
4657 } else {
4658 def->nextHash = prev->nextHash;
4659 prev->nextHash = def;
4660 }
4661 }
4662}
4663
4664/**
4665 * xmlRelaxNGParseImportRefs:
4666 * @ctxt: the parser context
4667 * @grammar: the sub grammar
4668 *
4669 * Import references from the subgrammar into the current grammar
4670 *
4671 * Returns 0 in case of success, -1 in case of failure
4672 */
4673static int
4674xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4675 xmlRelaxNGGrammarPtr grammar) {
4676 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4677 return(-1);
4678 if (grammar->refs == NULL)
4679 return(0);
4680 if (ctxt->grammar->refs == NULL)
4681 ctxt->grammar->refs = xmlHashCreate(10);
4682 if (ctxt->grammar->refs == NULL) {
4683 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4684 "Could not create references hash\n", NULL, NULL);
4685 return(-1);
4686 }
4687 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004688 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004689}
4690
4691/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004692 * xmlRelaxNGProcessExternalRef:
4693 * @ctxt: the parser context
4694 * @node: the externlRef node
4695 *
4696 * Process and compile an externlRef node
4697 *
4698 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4699 */
4700static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004701xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4702{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004703 xmlRelaxNGDocumentPtr docu;
4704 xmlNodePtr root, tmp;
4705 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004706 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004707 xmlRelaxNGDefinePtr def;
4708
Daniel Veillard807daf82004-02-22 22:13:27 +00004709 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004710 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004711 def = xmlRelaxNGNewDefine(ctxt, node);
4712 if (def == NULL)
4713 return (NULL);
4714 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004715
Daniel Veillard4c004142003-10-07 11:33:24 +00004716 if (docu->content == NULL) {
4717 /*
4718 * Then do the parsing for good
4719 */
4720 root = xmlDocGetRootElement(docu->doc);
4721 if (root == NULL) {
4722 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4723 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4724 NULL);
4725 return (NULL);
4726 }
4727 /*
4728 * ns transmission rules
4729 */
4730 ns = xmlGetProp(root, BAD_CAST "ns");
4731 if (ns == NULL) {
4732 tmp = node;
4733 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4734 ns = xmlGetProp(tmp, BAD_CAST "ns");
4735 if (ns != NULL) {
4736 break;
4737 }
4738 tmp = tmp->parent;
4739 }
4740 if (ns != NULL) {
4741 xmlSetProp(root, BAD_CAST "ns", ns);
4742 newNs = 1;
4743 xmlFree(ns);
4744 }
4745 } else {
4746 xmlFree(ns);
4747 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004748
Daniel Veillard4c004142003-10-07 11:33:24 +00004749 /*
4750 * Parsing to get a precompiled schemas.
4751 */
4752 oldflags = ctxt->flags;
4753 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4754 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4755 ctxt->flags = oldflags;
4756 if ((docu->schema != NULL) &&
4757 (docu->schema->topgrammar != NULL)) {
4758 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004759 if (docu->schema->topgrammar->refs)
4760 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004761 }
4762
4763 /*
4764 * the externalRef may be reused in a different ns context
4765 */
4766 if (newNs == 1) {
4767 xmlUnsetProp(root, BAD_CAST "ns");
4768 }
4769 }
4770 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004771 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004772 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004773 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004774 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004775}
4776
4777/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004778 * xmlRelaxNGParsePattern:
4779 * @ctxt: a Relax-NG parser context
4780 * @node: the pattern node.
4781 *
4782 * parse the content of a RelaxNG pattern node.
4783 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004784 * Returns the definition pointer or NULL in case of error or if no
4785 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004786 */
4787static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004788xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4789{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004790 xmlRelaxNGDefinePtr def = NULL;
4791
Daniel Veillardd2298792003-02-14 16:54:11 +00004792 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004793 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004794 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004795 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004796 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004797 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004798 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004799 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004800 def = xmlRelaxNGNewDefine(ctxt, node);
4801 if (def == NULL)
4802 return (NULL);
4803 def->type = XML_RELAXNG_EMPTY;
4804 if (node->children != NULL) {
4805 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4806 "empty: had a child node\n", NULL, NULL);
4807 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004808 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004809 def = xmlRelaxNGNewDefine(ctxt, node);
4810 if (def == NULL)
4811 return (NULL);
4812 def->type = XML_RELAXNG_TEXT;
4813 if (node->children != NULL) {
4814 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4815 "text: had a child node\n", NULL, NULL);
4816 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004817 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004818 def = xmlRelaxNGNewDefine(ctxt, node);
4819 if (def == NULL)
4820 return (NULL);
4821 def->type = XML_RELAXNG_ZEROORMORE;
4822 if (node->children == NULL) {
4823 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4824 "Element %s is empty\n", node->name, NULL);
4825 } else {
4826 def->content =
4827 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4828 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004829 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004830 def = xmlRelaxNGNewDefine(ctxt, node);
4831 if (def == NULL)
4832 return (NULL);
4833 def->type = XML_RELAXNG_ONEORMORE;
4834 if (node->children == NULL) {
4835 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4836 "Element %s is empty\n", node->name, NULL);
4837 } else {
4838 def->content =
4839 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4840 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004841 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004842 def = xmlRelaxNGNewDefine(ctxt, node);
4843 if (def == NULL)
4844 return (NULL);
4845 def->type = XML_RELAXNG_OPTIONAL;
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, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004854 def = xmlRelaxNGNewDefine(ctxt, node);
4855 if (def == NULL)
4856 return (NULL);
4857 def->type = XML_RELAXNG_CHOICE;
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, 0);
4864 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004865 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004866 def = xmlRelaxNGNewDefine(ctxt, node);
4867 if (def == NULL)
4868 return (NULL);
4869 def->type = XML_RELAXNG_GROUP;
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, 0);
4876 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004877 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004878 def = xmlRelaxNGNewDefine(ctxt, node);
4879 if (def == NULL)
4880 return (NULL);
4881 def->type = XML_RELAXNG_REF;
4882 def->name = xmlGetProp(node, BAD_CAST "name");
4883 if (def->name == NULL) {
4884 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4885 NULL, NULL);
4886 } else {
4887 xmlRelaxNGNormExtSpace(def->name);
4888 if (xmlValidateNCName(def->name, 0)) {
4889 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4890 "ref name '%s' is not an NCName\n", def->name,
4891 NULL);
4892 }
4893 }
4894 if (node->children != NULL) {
4895 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4896 NULL, NULL);
4897 }
4898 if (ctxt->grammar->refs == NULL)
4899 ctxt->grammar->refs = xmlHashCreate(10);
4900 if (ctxt->grammar->refs == NULL) {
4901 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4902 "Could not create references hash\n", NULL, NULL);
4903 def = NULL;
4904 } else {
4905 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004906
Daniel Veillard4c004142003-10-07 11:33:24 +00004907 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4908 if (tmp < 0) {
4909 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004910
Daniel Veillard4c004142003-10-07 11:33:24 +00004911 prev = (xmlRelaxNGDefinePtr)
4912 xmlHashLookup(ctxt->grammar->refs, def->name);
4913 if (prev == NULL) {
4914 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004915 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4916 "Error refs definitions '%s'\n",
4917 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004918 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004919 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4920 "Error refs definitions\n",
4921 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004922 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004923 def = NULL;
4924 } else {
4925 def->nextHash = prev->nextHash;
4926 prev->nextHash = def;
4927 }
4928 }
4929 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004930 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004931 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004932 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004933 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004934 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004935 def = xmlRelaxNGNewDefine(ctxt, node);
4936 if (def == NULL)
4937 return (NULL);
4938 def->type = XML_RELAXNG_LIST;
4939 if (node->children == NULL) {
4940 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4941 "Element %s is empty\n", node->name, NULL);
4942 } else {
4943 def->content =
4944 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4945 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004946 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004947 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004948 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004949 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004950 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004951 def = xmlRelaxNGNewDefine(ctxt, node);
4952 if (def == NULL)
4953 return (NULL);
4954 def->type = XML_RELAXNG_NOT_ALLOWED;
4955 if (node->children != NULL) {
4956 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4957 "xmlRelaxNGParse: notAllowed element is not empty\n",
4958 NULL, NULL);
4959 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004960 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004961 xmlRelaxNGGrammarPtr grammar, old;
4962 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004963
Daniel Veillardc482e262003-02-26 14:48:48 +00004964#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004965 xmlGenericError(xmlGenericErrorContext,
4966 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004967#endif
4968
Daniel Veillard4c004142003-10-07 11:33:24 +00004969 oldparent = ctxt->parentgrammar;
4970 old = ctxt->grammar;
4971 ctxt->parentgrammar = old;
4972 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4973 if (old != NULL) {
4974 ctxt->grammar = old;
4975 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004976#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00004977 if (grammar != NULL) {
4978 grammar->next = old->next;
4979 old->next = grammar;
4980 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004981#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00004982 }
4983 if (grammar != NULL)
4984 def = grammar->start;
4985 else
4986 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00004987 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004988 if (ctxt->parentgrammar == NULL) {
4989 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4990 "Use of parentRef without a parent grammar\n", NULL,
4991 NULL);
4992 return (NULL);
4993 }
4994 def = xmlRelaxNGNewDefine(ctxt, node);
4995 if (def == NULL)
4996 return (NULL);
4997 def->type = XML_RELAXNG_PARENTREF;
4998 def->name = xmlGetProp(node, BAD_CAST "name");
4999 if (def->name == NULL) {
5000 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5001 "parentRef has no name\n", NULL, NULL);
5002 } else {
5003 xmlRelaxNGNormExtSpace(def->name);
5004 if (xmlValidateNCName(def->name, 0)) {
5005 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5006 "parentRef name '%s' is not an NCName\n",
5007 def->name, NULL);
5008 }
5009 }
5010 if (node->children != NULL) {
5011 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5012 "parentRef is not empty\n", NULL, NULL);
5013 }
5014 if (ctxt->parentgrammar->refs == NULL)
5015 ctxt->parentgrammar->refs = xmlHashCreate(10);
5016 if (ctxt->parentgrammar->refs == NULL) {
5017 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5018 "Could not create references hash\n", NULL, NULL);
5019 def = NULL;
5020 } else if (def->name != NULL) {
5021 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005022
Daniel Veillard4c004142003-10-07 11:33:24 +00005023 tmp =
5024 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5025 if (tmp < 0) {
5026 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005027
Daniel Veillard4c004142003-10-07 11:33:24 +00005028 prev = (xmlRelaxNGDefinePtr)
5029 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5030 if (prev == NULL) {
5031 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5032 "Internal error parentRef definitions '%s'\n",
5033 def->name, NULL);
5034 def = NULL;
5035 } else {
5036 def->nextHash = prev->nextHash;
5037 prev->nextHash = def;
5038 }
5039 }
5040 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005041 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005042 if (node->children == NULL) {
5043 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5044 NULL, NULL);
5045 def = NULL;
5046 } else {
5047 def = xmlRelaxNGParseInterleave(ctxt, node);
5048 if (def != NULL) {
5049 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005050
Daniel Veillard4c004142003-10-07 11:33:24 +00005051 if ((def->content != NULL) && (def->content->next != NULL)) {
5052 tmp = xmlRelaxNGNewDefine(ctxt, node);
5053 if (tmp != NULL) {
5054 tmp->type = XML_RELAXNG_GROUP;
5055 tmp->content = def->content;
5056 def->content = tmp;
5057 }
5058 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005059
Daniel Veillard4c004142003-10-07 11:33:24 +00005060 tmp = xmlRelaxNGNewDefine(ctxt, node);
5061 if (tmp == NULL)
5062 return (def);
5063 tmp->type = XML_RELAXNG_TEXT;
5064 tmp->next = def->content;
5065 def->content = tmp;
5066 }
5067 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005068 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005069 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5070 "Unexpected node %s is not a pattern\n", node->name,
5071 NULL);
5072 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005073 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005074 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005075}
5076
5077/**
5078 * xmlRelaxNGParseAttribute:
5079 * @ctxt: a Relax-NG parser context
5080 * @node: the element node
5081 *
5082 * parse the content of a RelaxNG attribute node.
5083 *
5084 * Returns the definition pointer or NULL in case of error.
5085 */
5086static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005087xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5088{
Daniel Veillardd2298792003-02-14 16:54:11 +00005089 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005090 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005091 int old_flags;
5092
Daniel Veillardfd573f12003-03-16 17:52:32 +00005093 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005094 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005095 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005096 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005097 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005098 child = node->children;
5099 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005100 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5101 "xmlRelaxNGParseattribute: attribute has no children\n",
5102 NULL, NULL);
5103 return (ret);
5104 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005105 old_flags = ctxt->flags;
5106 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005107 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5108 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005109 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005110
Daniel Veillardd2298792003-02-14 16:54:11 +00005111 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005112 cur = xmlRelaxNGParsePattern(ctxt, child);
5113 if (cur != NULL) {
5114 switch (cur->type) {
5115 case XML_RELAXNG_EMPTY:
5116 case XML_RELAXNG_NOT_ALLOWED:
5117 case XML_RELAXNG_TEXT:
5118 case XML_RELAXNG_ELEMENT:
5119 case XML_RELAXNG_DATATYPE:
5120 case XML_RELAXNG_VALUE:
5121 case XML_RELAXNG_LIST:
5122 case XML_RELAXNG_REF:
5123 case XML_RELAXNG_PARENTREF:
5124 case XML_RELAXNG_EXTERNALREF:
5125 case XML_RELAXNG_DEF:
5126 case XML_RELAXNG_ONEORMORE:
5127 case XML_RELAXNG_ZEROORMORE:
5128 case XML_RELAXNG_OPTIONAL:
5129 case XML_RELAXNG_CHOICE:
5130 case XML_RELAXNG_GROUP:
5131 case XML_RELAXNG_INTERLEAVE:
5132 case XML_RELAXNG_ATTRIBUTE:
5133 ret->content = cur;
5134 cur->parent = ret;
5135 break;
5136 case XML_RELAXNG_START:
5137 case XML_RELAXNG_PARAM:
5138 case XML_RELAXNG_EXCEPT:
5139 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5140 "attribute has invalid content\n", NULL,
5141 NULL);
5142 break;
5143 case XML_RELAXNG_NOOP:
5144 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5145 "RNG Internal error, noop found in attribute\n",
5146 NULL, NULL);
5147 break;
5148 }
5149 }
5150 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005151 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005152 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005153 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5154 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005155 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005156 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005157 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005158}
5159
5160/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005161 * xmlRelaxNGParseExceptNameClass:
5162 * @ctxt: a Relax-NG parser context
5163 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005164 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005165 *
5166 * parse the content of a RelaxNG nameClass node.
5167 *
5168 * Returns the definition pointer or NULL in case of error.
5169 */
5170static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005171xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005172 xmlNodePtr node, int attr)
5173{
Daniel Veillard144fae12003-02-03 13:17:57 +00005174 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5175 xmlNodePtr child;
5176
Daniel Veillardd2298792003-02-14 16:54:11 +00005177 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005178 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5179 "Expecting an except node\n", NULL, NULL);
5180 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005181 }
5182 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005183 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5184 "exceptNameClass allows only a single except node\n",
5185 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005186 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005187 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005188 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5189 NULL, NULL);
5190 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005191 }
5192
Daniel Veillardfd573f12003-03-16 17:52:32 +00005193 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005194 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005195 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005196 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005197 child = node->children;
5198 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005199 cur = xmlRelaxNGNewDefine(ctxt, child);
5200 if (cur == NULL)
5201 break;
5202 if (attr)
5203 cur->type = XML_RELAXNG_ATTRIBUTE;
5204 else
5205 cur->type = XML_RELAXNG_ELEMENT;
5206
Daniel Veillard419a7682003-02-03 23:22:49 +00005207 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005208 if (last == NULL) {
5209 ret->content = cur;
5210 } else {
5211 last->next = cur;
5212 }
5213 last = cur;
5214 }
5215 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005216 }
5217
Daniel Veillard4c004142003-10-07 11:33:24 +00005218 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005219}
5220
5221/**
5222 * xmlRelaxNGParseNameClass:
5223 * @ctxt: a Relax-NG parser context
5224 * @node: the nameClass node
5225 * @def: the current definition
5226 *
5227 * parse the content of a RelaxNG nameClass node.
5228 *
5229 * Returns the definition pointer or NULL in case of error.
5230 */
5231static xmlRelaxNGDefinePtr
5232xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005233 xmlRelaxNGDefinePtr def)
5234{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005235 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005236 xmlChar *val;
5237
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005238 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005239 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005240 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005241 if ((def->type != XML_RELAXNG_ELEMENT) &&
5242 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5243 ret = xmlRelaxNGNewDefine(ctxt, node);
5244 if (ret == NULL)
5245 return (NULL);
5246 ret->parent = def;
5247 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5248 ret->type = XML_RELAXNG_ATTRIBUTE;
5249 else
5250 ret->type = XML_RELAXNG_ELEMENT;
5251 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005252 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005253 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005254 val = xmlNodeGetContent(node);
5255 xmlRelaxNGNormExtSpace(val);
5256 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005257 if (node->parent != NULL)
5258 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5259 "Element %s name '%s' is not an NCName\n",
5260 node->parent->name, val);
5261 else
5262 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5263 "name '%s' is not an NCName\n",
5264 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005265 }
5266 ret->name = val;
5267 val = xmlGetProp(node, BAD_CAST "ns");
5268 ret->ns = val;
5269 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5270 (val != NULL) &&
5271 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005272 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005273 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005274 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005275 }
5276 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5277 (val != NULL) &&
5278 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005279 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5280 "Attribute with QName 'xmlns' is not allowed\n",
5281 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005282 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005283 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005284 ret->name = NULL;
5285 ret->ns = NULL;
5286 if (node->children != NULL) {
5287 ret->nameClass =
5288 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5289 (def->type ==
5290 XML_RELAXNG_ATTRIBUTE));
5291 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005292 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005293 ret->name = NULL;
5294 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5295 if (ret->ns == NULL) {
5296 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5297 "nsName has no ns attribute\n", NULL, NULL);
5298 }
5299 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5300 (ret->ns != NULL) &&
5301 (xmlStrEqual
5302 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5303 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5304 "Attribute with namespace '%s' is not allowed\n",
5305 ret->ns, NULL);
5306 }
5307 if (node->children != NULL) {
5308 ret->nameClass =
5309 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5310 (def->type ==
5311 XML_RELAXNG_ATTRIBUTE));
5312 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005313 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005314 xmlNodePtr child;
5315 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005316
Daniel Veillard4c004142003-10-07 11:33:24 +00005317 ret = xmlRelaxNGNewDefine(ctxt, node);
5318 if (ret == NULL)
5319 return (NULL);
5320 ret->parent = def;
5321 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005322
Daniel Veillard4c004142003-10-07 11:33:24 +00005323 if (node->children == NULL) {
5324 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5325 "Element choice is empty\n", NULL, NULL);
5326 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005327
Daniel Veillard4c004142003-10-07 11:33:24 +00005328 child = node->children;
5329 while (child != NULL) {
5330 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5331 if (tmp != NULL) {
5332 if (last == NULL) {
5333 last = ret->nameClass = tmp;
5334 } else {
5335 last->next = tmp;
5336 last = tmp;
5337 }
5338 }
5339 child = child->next;
5340 }
5341 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005342 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005343 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5344 "expecting name, anyName, nsName or choice : got %s\n",
5345 node->name, NULL);
5346 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005347 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005348 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005349 if (def->nameClass == NULL) {
5350 def->nameClass = ret;
5351 } else {
5352 tmp = def->nameClass;
5353 while (tmp->next != NULL) {
5354 tmp = tmp->next;
5355 }
5356 tmp->next = ret;
5357 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005358 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005359 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005360}
5361
5362/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005363 * xmlRelaxNGParseElement:
5364 * @ctxt: a Relax-NG parser context
5365 * @node: the element node
5366 *
5367 * parse the content of a RelaxNG element node.
5368 *
5369 * Returns the definition pointer or NULL in case of error.
5370 */
5371static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005372xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5373{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005374 xmlRelaxNGDefinePtr ret, cur, last;
5375 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005376 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005377
Daniel Veillardfd573f12003-03-16 17:52:32 +00005378 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005379 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005380 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005381 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005382 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005383 child = node->children;
5384 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005385 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5386 "xmlRelaxNGParseElement: element has no children\n",
5387 NULL, NULL);
5388 return (ret);
5389 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005390 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5391 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005392 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005393
Daniel Veillard6eadf632003-01-23 18:29:16 +00005394 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005395 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5396 "xmlRelaxNGParseElement: element has no content\n",
5397 NULL, NULL);
5398 return (ret);
5399 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005400 olddefine = ctxt->define;
5401 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005402 last = NULL;
5403 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005404 cur = xmlRelaxNGParsePattern(ctxt, child);
5405 if (cur != NULL) {
5406 cur->parent = ret;
5407 switch (cur->type) {
5408 case XML_RELAXNG_EMPTY:
5409 case XML_RELAXNG_NOT_ALLOWED:
5410 case XML_RELAXNG_TEXT:
5411 case XML_RELAXNG_ELEMENT:
5412 case XML_RELAXNG_DATATYPE:
5413 case XML_RELAXNG_VALUE:
5414 case XML_RELAXNG_LIST:
5415 case XML_RELAXNG_REF:
5416 case XML_RELAXNG_PARENTREF:
5417 case XML_RELAXNG_EXTERNALREF:
5418 case XML_RELAXNG_DEF:
5419 case XML_RELAXNG_ZEROORMORE:
5420 case XML_RELAXNG_ONEORMORE:
5421 case XML_RELAXNG_OPTIONAL:
5422 case XML_RELAXNG_CHOICE:
5423 case XML_RELAXNG_GROUP:
5424 case XML_RELAXNG_INTERLEAVE:
5425 if (last == NULL) {
5426 ret->content = last = cur;
5427 } else {
5428 if ((last->type == XML_RELAXNG_ELEMENT) &&
5429 (ret->content == last)) {
5430 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5431 if (ret->content != NULL) {
5432 ret->content->type = XML_RELAXNG_GROUP;
5433 ret->content->content = last;
5434 } else {
5435 ret->content = last;
5436 }
5437 }
5438 last->next = cur;
5439 last = cur;
5440 }
5441 break;
5442 case XML_RELAXNG_ATTRIBUTE:
5443 cur->next = ret->attrs;
5444 ret->attrs = cur;
5445 break;
5446 case XML_RELAXNG_START:
5447 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5448 "RNG Internal error, start found in element\n",
5449 NULL, NULL);
5450 break;
5451 case XML_RELAXNG_PARAM:
5452 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5453 "RNG Internal error, param found in element\n",
5454 NULL, NULL);
5455 break;
5456 case XML_RELAXNG_EXCEPT:
5457 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5458 "RNG Internal error, except found in element\n",
5459 NULL, NULL);
5460 break;
5461 case XML_RELAXNG_NOOP:
5462 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5463 "RNG Internal error, noop found in element\n",
5464 NULL, NULL);
5465 break;
5466 }
5467 }
5468 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005469 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005470 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005471 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005472}
5473
5474/**
5475 * xmlRelaxNGParsePatterns:
5476 * @ctxt: a Relax-NG parser context
5477 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005478 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005479 *
5480 * parse the content of a RelaxNG start node.
5481 *
5482 * Returns the definition pointer or NULL in case of error.
5483 */
5484static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005485xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005486 int group)
5487{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005488 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005489
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005490 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005491 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005492 if (IS_RELAXNG(nodes, "element")) {
5493 cur = xmlRelaxNGParseElement(ctxt, nodes);
5494 if (def == NULL) {
5495 def = last = cur;
5496 } else {
5497 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5498 (def == last)) {
5499 def = xmlRelaxNGNewDefine(ctxt, nodes);
5500 def->type = XML_RELAXNG_GROUP;
5501 def->content = last;
5502 }
5503 last->next = cur;
5504 last = cur;
5505 }
5506 cur->parent = parent;
5507 } else {
5508 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5509 if (cur != NULL) {
5510 if (def == NULL) {
5511 def = last = cur;
5512 } else {
5513 last->next = cur;
5514 last = cur;
5515 }
5516 }
5517 }
5518 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005519 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005520 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005521}
5522
5523/**
5524 * xmlRelaxNGParseStart:
5525 * @ctxt: a Relax-NG parser context
5526 * @nodes: start children nodes
5527 *
5528 * parse the content of a RelaxNG start node.
5529 *
5530 * Returns 0 in case of success, -1 in case of error
5531 */
5532static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005533xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5534{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005535 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005536 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005537
Daniel Veillardd2298792003-02-14 16:54:11 +00005538 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005539 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5540 NULL, NULL);
5541 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005542 }
5543 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005544 def = xmlRelaxNGNewDefine(ctxt, nodes);
5545 if (def == NULL)
5546 return (-1);
5547 def->type = XML_RELAXNG_EMPTY;
5548 if (nodes->children != NULL) {
5549 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5550 "element empty is not empty\n", NULL, NULL);
5551 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005552 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005553 def = xmlRelaxNGNewDefine(ctxt, nodes);
5554 if (def == NULL)
5555 return (-1);
5556 def->type = XML_RELAXNG_NOT_ALLOWED;
5557 if (nodes->children != NULL) {
5558 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5559 "element notAllowed is not empty\n", NULL, NULL);
5560 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005561 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005562 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005563 }
5564 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005565 last = ctxt->grammar->start;
5566 while (last->next != NULL)
5567 last = last->next;
5568 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005569 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005570 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005571 }
5572 nodes = nodes->next;
5573 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005574 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5575 "start more than one children\n", NULL, NULL);
5576 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005577 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005578 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005579}
5580
5581/**
5582 * xmlRelaxNGParseGrammarContent:
5583 * @ctxt: a Relax-NG parser context
5584 * @nodes: grammar children nodes
5585 *
5586 * parse the content of a RelaxNG grammar node.
5587 *
5588 * Returns 0 in case of success, -1 in case of error
5589 */
5590static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005591xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5592 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005593{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005594 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005595
5596 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005597 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5598 "grammar has no children\n", NULL, NULL);
5599 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005600 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005601 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005602 if (IS_RELAXNG(nodes, "start")) {
5603 if (nodes->children == NULL) {
5604 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5605 "start has no children\n", NULL, NULL);
5606 } else {
5607 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5608 if (tmp != 0)
5609 ret = -1;
5610 }
5611 } else if (IS_RELAXNG(nodes, "define")) {
5612 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5613 if (tmp != 0)
5614 ret = -1;
5615 } else if (IS_RELAXNG(nodes, "include")) {
5616 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5617 if (tmp != 0)
5618 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005619 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005620 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5621 "grammar has unexpected child %s\n", nodes->name,
5622 NULL);
5623 ret = -1;
5624 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005625 nodes = nodes->next;
5626 }
5627 return (ret);
5628}
5629
5630/**
5631 * xmlRelaxNGCheckReference:
5632 * @ref: the ref
5633 * @ctxt: a Relax-NG parser context
5634 * @name: the name associated to the defines
5635 *
5636 * Applies the 4.17. combine attribute rule for all the define
5637 * element of a given grammar using the same name.
5638 */
5639static void
5640xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005641 xmlRelaxNGParserCtxtPtr ctxt,
5642 const xmlChar * name)
5643{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005644 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005645 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005646
5647 grammar = ctxt->grammar;
5648 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005649 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5650 "Internal error: no grammar in CheckReference %s\n",
5651 name, NULL);
5652 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005653 }
5654 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005655 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5656 "Internal error: reference has content in CheckReference %s\n",
5657 name, NULL);
5658 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005659 }
5660 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005661 def = xmlHashLookup(grammar->defs, name);
5662 if (def != NULL) {
5663 cur = ref;
5664 while (cur != NULL) {
5665 cur->content = def;
5666 cur = cur->nextHash;
5667 }
5668 } else {
5669 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5670 "Reference %s has no matching definition\n", name,
5671 NULL);
5672 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005673 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005674 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5675 "Reference %s has no matching definition\n", name,
5676 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005677 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005678}
5679
5680/**
5681 * xmlRelaxNGCheckCombine:
5682 * @define: the define(s) list
5683 * @ctxt: a Relax-NG parser context
5684 * @name: the name associated to the defines
5685 *
5686 * Applies the 4.17. combine attribute rule for all the define
5687 * element of a given grammar using the same name.
5688 */
5689static void
5690xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005691 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5692{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005693 xmlChar *combine;
5694 int choiceOrInterleave = -1;
5695 int missing = 0;
5696 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5697
5698 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005699 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005700 cur = define;
5701 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005702 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5703 if (combine != NULL) {
5704 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5705 if (choiceOrInterleave == -1)
5706 choiceOrInterleave = 1;
5707 else if (choiceOrInterleave == 0) {
5708 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5709 "Defines for %s use both 'choice' and 'interleave'\n",
5710 name, NULL);
5711 }
5712 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5713 if (choiceOrInterleave == -1)
5714 choiceOrInterleave = 0;
5715 else if (choiceOrInterleave == 1) {
5716 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5717 "Defines for %s use both 'choice' and 'interleave'\n",
5718 name, NULL);
5719 }
5720 } else {
5721 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5722 "Defines for %s use unknown combine value '%s''\n",
5723 name, combine);
5724 }
5725 xmlFree(combine);
5726 } else {
5727 if (missing == 0)
5728 missing = 1;
5729 else {
5730 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5731 "Some defines for %s needs the combine attribute\n",
5732 name, NULL);
5733 }
5734 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005735
Daniel Veillard4c004142003-10-07 11:33:24 +00005736 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005737 }
5738#ifdef DEBUG
5739 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005740 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5741 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005742#endif
5743 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005744 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005745 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005746 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005747 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005748 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005749 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005750 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005751 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005752 tmp = define;
5753 last = NULL;
5754 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005755 if (tmp->content != NULL) {
5756 if (tmp->content->next != NULL) {
5757 /*
5758 * we need first to create a wrapper.
5759 */
5760 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5761 if (tmp2 == NULL)
5762 break;
5763 tmp2->type = XML_RELAXNG_GROUP;
5764 tmp2->content = tmp->content;
5765 } else {
5766 tmp2 = tmp->content;
5767 }
5768 if (last == NULL) {
5769 cur->content = tmp2;
5770 } else {
5771 last->next = tmp2;
5772 }
5773 last = tmp2;
5774 }
5775 tmp->content = cur;
5776 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005777 }
5778 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005779 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005780 if (ctxt->interleaves == NULL)
5781 ctxt->interleaves = xmlHashCreate(10);
5782 if (ctxt->interleaves == NULL) {
5783 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5784 "Failed to create interleaves hash table\n", NULL,
5785 NULL);
5786 } else {
5787 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005788
Daniel Veillard4c004142003-10-07 11:33:24 +00005789 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5790 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5791 0) {
5792 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5793 "Failed to add %s to hash table\n",
5794 (const xmlChar *) tmpname, NULL);
5795 }
5796 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005797 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005798}
5799
5800/**
5801 * xmlRelaxNGCombineStart:
5802 * @ctxt: a Relax-NG parser context
5803 * @grammar: the grammar
5804 *
5805 * Applies the 4.17. combine rule for all the start
5806 * element of a given grammar.
5807 */
5808static void
5809xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005810 xmlRelaxNGGrammarPtr grammar)
5811{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005812 xmlRelaxNGDefinePtr starts;
5813 xmlChar *combine;
5814 int choiceOrInterleave = -1;
5815 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005816 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005817
Daniel Veillard2df2de22003-02-17 23:34:33 +00005818 starts = grammar->start;
5819 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005820 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005821 cur = starts;
5822 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005823 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5824 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5825 combine = NULL;
5826 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5827 "Internal error: start element not found\n", NULL,
5828 NULL);
5829 } else {
5830 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5831 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005832
Daniel Veillard4c004142003-10-07 11:33:24 +00005833 if (combine != NULL) {
5834 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5835 if (choiceOrInterleave == -1)
5836 choiceOrInterleave = 1;
5837 else if (choiceOrInterleave == 0) {
5838 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5839 "<start> use both 'choice' and 'interleave'\n",
5840 NULL, NULL);
5841 }
5842 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5843 if (choiceOrInterleave == -1)
5844 choiceOrInterleave = 0;
5845 else if (choiceOrInterleave == 1) {
5846 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5847 "<start> use both 'choice' and 'interleave'\n",
5848 NULL, NULL);
5849 }
5850 } else {
5851 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5852 "<start> uses unknown combine value '%s''\n",
5853 combine, NULL);
5854 }
5855 xmlFree(combine);
5856 } else {
5857 if (missing == 0)
5858 missing = 1;
5859 else {
5860 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5861 "Some <start> element miss the combine attribute\n",
5862 NULL, NULL);
5863 }
5864 }
5865
5866 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005867 }
5868#ifdef DEBUG
5869 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005870 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5871 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005872#endif
5873 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005874 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005875 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005876 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005877 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005878 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005879 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005880 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005881 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005882 cur->content = grammar->start;
5883 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005884 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005885 if (ctxt->interleaves == NULL)
5886 ctxt->interleaves = xmlHashCreate(10);
5887 if (ctxt->interleaves == NULL) {
5888 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5889 "Failed to create interleaves hash table\n", NULL,
5890 NULL);
5891 } else {
5892 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005893
Daniel Veillard4c004142003-10-07 11:33:24 +00005894 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5895 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5896 0) {
5897 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5898 "Failed to add %s to hash table\n",
5899 (const xmlChar *) tmpname, NULL);
5900 }
5901 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005902 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005903}
5904
5905/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005906 * xmlRelaxNGCheckCycles:
5907 * @ctxt: a Relax-NG parser context
5908 * @nodes: grammar children nodes
5909 * @depth: the counter
5910 *
5911 * Check for cycles.
5912 *
5913 * Returns 0 if check passed, and -1 in case of error
5914 */
5915static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005916xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5917 xmlRelaxNGDefinePtr cur, int depth)
5918{
Daniel Veillardd4310742003-02-18 21:12:46 +00005919 int ret = 0;
5920
5921 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005922 if ((cur->type == XML_RELAXNG_REF) ||
5923 (cur->type == XML_RELAXNG_PARENTREF)) {
5924 if (cur->depth == -1) {
5925 cur->depth = depth;
5926 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5927 cur->depth = -2;
5928 } else if (depth == cur->depth) {
5929 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5930 "Detected a cycle in %s references\n",
5931 cur->name, NULL);
5932 return (-1);
5933 }
5934 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5935 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5936 } else {
5937 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5938 }
5939 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005940 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005941 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005942}
5943
5944/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005945 * xmlRelaxNGTryUnlink:
5946 * @ctxt: a Relax-NG parser context
5947 * @cur: the definition to unlink
5948 * @parent: the parent definition
5949 * @prev: the previous sibling definition
5950 *
5951 * Try to unlink a definition. If not possble make it a NOOP
5952 *
5953 * Returns the new prev definition
5954 */
5955static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005956xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5957 xmlRelaxNGDefinePtr cur,
5958 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5959{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005960 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005961 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005962 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005963 if (parent != NULL) {
5964 if (parent->content == cur)
5965 parent->content = cur->next;
5966 else if (parent->attrs == cur)
5967 parent->attrs = cur->next;
5968 else if (parent->nameClass == cur)
5969 parent->nameClass = cur->next;
5970 } else {
5971 cur->type = XML_RELAXNG_NOOP;
5972 prev = cur;
5973 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005974 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005975 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005976}
5977
5978/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005979 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005980 * @ctxt: a Relax-NG parser context
5981 * @nodes: grammar children nodes
5982 *
5983 * Check for simplification of empty and notAllowed
5984 */
5985static void
Daniel Veillard4c004142003-10-07 11:33:24 +00005986xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5987 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5988{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005989 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005990
Daniel Veillardfd573f12003-03-16 17:52:32 +00005991 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005992 if ((cur->type == XML_RELAXNG_REF) ||
5993 (cur->type == XML_RELAXNG_PARENTREF)) {
5994 if (cur->depth != -3) {
5995 cur->depth = -3;
5996 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5997 }
5998 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5999 cur->parent = parent;
6000 if ((parent != NULL) &&
6001 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6002 (parent->type == XML_RELAXNG_LIST) ||
6003 (parent->type == XML_RELAXNG_GROUP) ||
6004 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6005 (parent->type == XML_RELAXNG_ONEORMORE) ||
6006 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6007 parent->type = XML_RELAXNG_NOT_ALLOWED;
6008 break;
6009 }
6010 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6011 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6012 } else
6013 prev = cur;
6014 } else if (cur->type == XML_RELAXNG_EMPTY) {
6015 cur->parent = parent;
6016 if ((parent != NULL) &&
6017 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6018 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6019 parent->type = XML_RELAXNG_EMPTY;
6020 break;
6021 }
6022 if ((parent != NULL) &&
6023 ((parent->type == XML_RELAXNG_GROUP) ||
6024 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6025 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6026 } else
6027 prev = cur;
6028 } else {
6029 cur->parent = parent;
6030 if (cur->content != NULL)
6031 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6032 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6033 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6034 if (cur->nameClass != NULL)
6035 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6036 /*
6037 * On Elements, try to move attribute only generating rules on
6038 * the attrs rules.
6039 */
6040 if (cur->type == XML_RELAXNG_ELEMENT) {
6041 int attronly;
6042 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006043
Daniel Veillard4c004142003-10-07 11:33:24 +00006044 while (cur->content != NULL) {
6045 attronly =
6046 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6047 if (attronly == 1) {
6048 /*
6049 * migrate cur->content to attrs
6050 */
6051 tmp = cur->content;
6052 cur->content = tmp->next;
6053 tmp->next = cur->attrs;
6054 cur->attrs = tmp;
6055 } else {
6056 /*
6057 * cur->content can generate elements or text
6058 */
6059 break;
6060 }
6061 }
6062 pre = cur->content;
6063 while ((pre != NULL) && (pre->next != NULL)) {
6064 tmp = pre->next;
6065 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6066 if (attronly == 1) {
6067 /*
6068 * migrate tmp to attrs
6069 */
6070 pre->next = tmp->next;
6071 tmp->next = cur->attrs;
6072 cur->attrs = tmp;
6073 } else {
6074 pre = tmp;
6075 }
6076 }
6077 }
6078 /*
6079 * This may result in a simplification
6080 */
6081 if ((cur->type == XML_RELAXNG_GROUP) ||
6082 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6083 if (cur->content == NULL)
6084 cur->type = XML_RELAXNG_EMPTY;
6085 else if (cur->content->next == NULL) {
6086 if ((parent == NULL) && (prev == NULL)) {
6087 cur->type = XML_RELAXNG_NOOP;
6088 } else if (prev == NULL) {
6089 parent->content = cur->content;
6090 cur->content->next = cur->next;
6091 cur = cur->content;
6092 } else {
6093 cur->content->next = cur->next;
6094 prev->next = cur->content;
6095 cur = cur->content;
6096 }
6097 }
6098 }
6099 /*
6100 * the current node may have been transformed back
6101 */
6102 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6103 (cur->content != NULL) &&
6104 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6105 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6106 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6107 if ((parent != NULL) &&
6108 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6109 (parent->type == XML_RELAXNG_LIST) ||
6110 (parent->type == XML_RELAXNG_GROUP) ||
6111 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6112 (parent->type == XML_RELAXNG_ONEORMORE) ||
6113 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6114 parent->type = XML_RELAXNG_NOT_ALLOWED;
6115 break;
6116 }
6117 if ((parent != NULL) &&
6118 (parent->type == XML_RELAXNG_CHOICE)) {
6119 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6120 } else
6121 prev = cur;
6122 } else if (cur->type == XML_RELAXNG_EMPTY) {
6123 if ((parent != NULL) &&
6124 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6125 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6126 parent->type = XML_RELAXNG_EMPTY;
6127 break;
6128 }
6129 if ((parent != NULL) &&
6130 ((parent->type == XML_RELAXNG_GROUP) ||
6131 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6132 (parent->type == XML_RELAXNG_CHOICE))) {
6133 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6134 } else
6135 prev = cur;
6136 } else {
6137 prev = cur;
6138 }
6139 }
6140 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006141 }
6142}
6143
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006144/**
6145 * xmlRelaxNGGroupContentType:
6146 * @ct1: the first content type
6147 * @ct2: the second content type
6148 *
6149 * Try to group 2 content types
6150 *
6151 * Returns the content type
6152 */
6153static xmlRelaxNGContentType
6154xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006155 xmlRelaxNGContentType ct2)
6156{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006157 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006158 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6159 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006160 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006161 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006162 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006163 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006164 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006165 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6166 return (XML_RELAXNG_CONTENT_COMPLEX);
6167 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006168}
6169
6170/**
6171 * xmlRelaxNGMaxContentType:
6172 * @ct1: the first content type
6173 * @ct2: the second content type
6174 *
6175 * Compute the max content-type
6176 *
6177 * Returns the content type
6178 */
6179static xmlRelaxNGContentType
6180xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006181 xmlRelaxNGContentType ct2)
6182{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006183 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006184 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6185 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006186 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006187 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6188 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006189 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006190 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6191 return (XML_RELAXNG_CONTENT_COMPLEX);
6192 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006193}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006194
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006195/**
6196 * xmlRelaxNGCheckRules:
6197 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006198 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006199 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006200 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006201 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006202 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006203 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006204 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006205 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006206static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006207xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6208 xmlRelaxNGDefinePtr cur, int flags,
6209 xmlRelaxNGType ptype)
6210{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006211 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006212 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006213
Daniel Veillardfd573f12003-03-16 17:52:32 +00006214 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006215 ret = XML_RELAXNG_CONTENT_EMPTY;
6216 if ((cur->type == XML_RELAXNG_REF) ||
6217 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006218 /*
6219 * This should actually be caught by list//element(ref) at the
6220 * element boundaries, c.f. Bug #159968 local refs are dropped
6221 * in step 4.19.
6222 */
6223#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006224 if (flags & XML_RELAXNG_IN_LIST) {
6225 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6226 "Found forbidden pattern list//ref\n", NULL,
6227 NULL);
6228 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006229#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006230 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6231 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6232 "Found forbidden pattern data/except//ref\n",
6233 NULL, NULL);
6234 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006235 if (cur->content == NULL) {
6236 if (cur->type == XML_RELAXNG_PARENTREF)
6237 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6238 "Internal found no define for parent refs\n",
6239 NULL, NULL);
6240 else
6241 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6242 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006243 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006244 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006245 if (cur->depth > -4) {
6246 cur->depth = -4;
6247 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6248 flags, cur->type);
6249 cur->depth = ret - 15;
6250 } else if (cur->depth == -4) {
6251 ret = XML_RELAXNG_CONTENT_COMPLEX;
6252 } else {
6253 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6254 }
6255 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6256 /*
6257 * The 7.3 Attribute derivation rule for groups is plugged there
6258 */
6259 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6260 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6261 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6262 "Found forbidden pattern data/except//element(ref)\n",
6263 NULL, NULL);
6264 }
6265 if (flags & XML_RELAXNG_IN_LIST) {
6266 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6267 "Found forbidden pattern list//element(ref)\n",
6268 NULL, NULL);
6269 }
6270 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6271 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6272 "Found forbidden pattern attribute//element(ref)\n",
6273 NULL, NULL);
6274 }
6275 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6276 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6277 "Found forbidden pattern attribute//element(ref)\n",
6278 NULL, NULL);
6279 }
6280 /*
6281 * reset since in the simple form elements are only child
6282 * of grammar/define
6283 */
6284 nflags = 0;
6285 ret =
6286 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6287 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6288 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6289 "Element %s attributes have a content type error\n",
6290 cur->name, NULL);
6291 }
6292 ret =
6293 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6294 cur->type);
6295 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6296 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6297 "Element %s has a content type error\n",
6298 cur->name, NULL);
6299 } else {
6300 ret = XML_RELAXNG_CONTENT_COMPLEX;
6301 }
6302 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6303 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6304 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6305 "Found forbidden pattern attribute//attribute\n",
6306 NULL, NULL);
6307 }
6308 if (flags & XML_RELAXNG_IN_LIST) {
6309 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6310 "Found forbidden pattern list//attribute\n",
6311 NULL, NULL);
6312 }
6313 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6314 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6315 "Found forbidden pattern oneOrMore//group//attribute\n",
6316 NULL, NULL);
6317 }
6318 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6319 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6320 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6321 NULL, NULL);
6322 }
6323 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6324 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6325 "Found forbidden pattern data/except//attribute\n",
6326 NULL, NULL);
6327 }
6328 if (flags & XML_RELAXNG_IN_START) {
6329 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6330 "Found forbidden pattern start//attribute\n",
6331 NULL, NULL);
6332 }
6333 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6334 && (cur->name == NULL)) {
6335 if (cur->ns == NULL) {
6336 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6337 "Found anyName attribute without oneOrMore ancestor\n",
6338 NULL, NULL);
6339 } else {
6340 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6341 "Found nsName attribute without oneOrMore ancestor\n",
6342 NULL, NULL);
6343 }
6344 }
6345 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6346 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6347 ret = XML_RELAXNG_CONTENT_EMPTY;
6348 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6349 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6350 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6351 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6352 "Found forbidden pattern data/except//oneOrMore\n",
6353 NULL, NULL);
6354 }
6355 if (flags & XML_RELAXNG_IN_START) {
6356 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6357 "Found forbidden pattern start//oneOrMore\n",
6358 NULL, NULL);
6359 }
6360 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6361 ret =
6362 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6363 cur->type);
6364 ret = xmlRelaxNGGroupContentType(ret, ret);
6365 } else if (cur->type == XML_RELAXNG_LIST) {
6366 if (flags & XML_RELAXNG_IN_LIST) {
6367 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6368 "Found forbidden pattern list//list\n", NULL,
6369 NULL);
6370 }
6371 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6372 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6373 "Found forbidden pattern data/except//list\n",
6374 NULL, NULL);
6375 }
6376 if (flags & XML_RELAXNG_IN_START) {
6377 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6378 "Found forbidden pattern start//list\n", NULL,
6379 NULL);
6380 }
6381 nflags = flags | XML_RELAXNG_IN_LIST;
6382 ret =
6383 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6384 cur->type);
6385 } else if (cur->type == XML_RELAXNG_GROUP) {
6386 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6387 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6388 "Found forbidden pattern data/except//group\n",
6389 NULL, NULL);
6390 }
6391 if (flags & XML_RELAXNG_IN_START) {
6392 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6393 "Found forbidden pattern start//group\n", NULL,
6394 NULL);
6395 }
6396 if (flags & XML_RELAXNG_IN_ONEORMORE)
6397 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6398 else
6399 nflags = flags;
6400 ret =
6401 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6402 cur->type);
6403 /*
6404 * The 7.3 Attribute derivation rule for groups is plugged there
6405 */
6406 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6407 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6408 if (flags & XML_RELAXNG_IN_LIST) {
6409 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6410 "Found forbidden pattern list//interleave\n",
6411 NULL, NULL);
6412 }
6413 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6414 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6415 "Found forbidden pattern data/except//interleave\n",
6416 NULL, NULL);
6417 }
6418 if (flags & XML_RELAXNG_IN_START) {
6419 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6420 "Found forbidden pattern start//interleave\n",
6421 NULL, NULL);
6422 }
6423 if (flags & XML_RELAXNG_IN_ONEORMORE)
6424 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6425 else
6426 nflags = flags;
6427 ret =
6428 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6429 cur->type);
6430 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6431 if ((cur->parent != NULL) &&
6432 (cur->parent->type == XML_RELAXNG_DATATYPE))
6433 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6434 else
6435 nflags = flags;
6436 ret =
6437 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6438 cur->type);
6439 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6440 if (flags & XML_RELAXNG_IN_START) {
6441 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6442 "Found forbidden pattern start//data\n", NULL,
6443 NULL);
6444 }
6445 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6446 ret = XML_RELAXNG_CONTENT_SIMPLE;
6447 } else if (cur->type == XML_RELAXNG_VALUE) {
6448 if (flags & XML_RELAXNG_IN_START) {
6449 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6450 "Found forbidden pattern start//value\n", NULL,
6451 NULL);
6452 }
6453 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6454 ret = XML_RELAXNG_CONTENT_SIMPLE;
6455 } else if (cur->type == XML_RELAXNG_TEXT) {
6456 if (flags & XML_RELAXNG_IN_LIST) {
6457 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6458 "Found forbidden pattern list//text\n", NULL,
6459 NULL);
6460 }
6461 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6462 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6463 "Found forbidden pattern data/except//text\n",
6464 NULL, NULL);
6465 }
6466 if (flags & XML_RELAXNG_IN_START) {
6467 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6468 "Found forbidden pattern start//text\n", NULL,
6469 NULL);
6470 }
6471 ret = XML_RELAXNG_CONTENT_COMPLEX;
6472 } else if (cur->type == XML_RELAXNG_EMPTY) {
6473 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6474 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6475 "Found forbidden pattern data/except//empty\n",
6476 NULL, NULL);
6477 }
6478 if (flags & XML_RELAXNG_IN_START) {
6479 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6480 "Found forbidden pattern start//empty\n", NULL,
6481 NULL);
6482 }
6483 ret = XML_RELAXNG_CONTENT_EMPTY;
6484 } else if (cur->type == XML_RELAXNG_CHOICE) {
6485 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6486 ret =
6487 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6488 } else {
6489 ret =
6490 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6491 }
6492 cur = cur->next;
6493 if (ptype == XML_RELAXNG_GROUP) {
6494 val = xmlRelaxNGGroupContentType(val, ret);
6495 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6496 tmp = xmlRelaxNGGroupContentType(val, ret);
6497 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6498 tmp = xmlRelaxNGMaxContentType(val, ret);
6499 } else if (ptype == XML_RELAXNG_CHOICE) {
6500 val = xmlRelaxNGMaxContentType(val, ret);
6501 } else if (ptype == XML_RELAXNG_LIST) {
6502 val = XML_RELAXNG_CONTENT_SIMPLE;
6503 } else if (ptype == XML_RELAXNG_EXCEPT) {
6504 if (ret == XML_RELAXNG_CONTENT_ERROR)
6505 val = XML_RELAXNG_CONTENT_ERROR;
6506 else
6507 val = XML_RELAXNG_CONTENT_SIMPLE;
6508 } else {
6509 val = xmlRelaxNGGroupContentType(val, ret);
6510 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006511
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006512 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006513 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006514}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006515
6516/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006517 * xmlRelaxNGParseGrammar:
6518 * @ctxt: a Relax-NG parser context
6519 * @nodes: grammar children nodes
6520 *
6521 * parse a Relax-NG <grammar> node
6522 *
6523 * Returns the internal xmlRelaxNGGrammarPtr built or
6524 * NULL in case of error
6525 */
6526static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006527xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6528{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006529 xmlRelaxNGGrammarPtr ret, tmp, old;
6530
Daniel Veillardc482e262003-02-26 14:48:48 +00006531#ifdef DEBUG_GRAMMAR
6532 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6533#endif
6534
Daniel Veillard6eadf632003-01-23 18:29:16 +00006535 ret = xmlRelaxNGNewGrammar(ctxt);
6536 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006537 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006538
6539 /*
6540 * Link the new grammar in the tree
6541 */
6542 ret->parent = ctxt->grammar;
6543 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006544 tmp = ctxt->grammar->children;
6545 if (tmp == NULL) {
6546 ctxt->grammar->children = ret;
6547 } else {
6548 while (tmp->next != NULL)
6549 tmp = tmp->next;
6550 tmp->next = ret;
6551 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006552 }
6553
6554 old = ctxt->grammar;
6555 ctxt->grammar = ret;
6556 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6557 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006558 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006559 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6560 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006561 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006562 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6563 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006564 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006565
6566 /*
6567 * Apply 4.17 mergingd rules to defines and starts
6568 */
6569 xmlRelaxNGCombineStart(ctxt, ret);
6570 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006571 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6572 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006573 }
6574
6575 /*
6576 * link together defines and refs in this grammar
6577 */
6578 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006579 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6580 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006581 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006582
Daniel Veillard81c51e12009-08-14 18:52:10 +02006583
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006584 /* @@@@ */
6585
Daniel Veillard6eadf632003-01-23 18:29:16 +00006586 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006587 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006588}
6589
6590/**
6591 * xmlRelaxNGParseDocument:
6592 * @ctxt: a Relax-NG parser context
6593 * @node: the root node of the RelaxNG schema
6594 *
6595 * parse a Relax-NG definition resource and build an internal
6596 * xmlRelaxNG struture which can be used to validate instances.
6597 *
6598 * Returns the internal XML RelaxNG structure built or
6599 * NULL in case of error
6600 */
6601static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006602xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6603{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006604 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006605 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006606 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006607
6608 if ((ctxt == NULL) || (node == NULL))
6609 return (NULL);
6610
6611 schema = xmlRelaxNGNewRelaxNG(ctxt);
6612 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006613 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006614
Daniel Veillard276be4a2003-01-24 01:03:34 +00006615 olddefine = ctxt->define;
6616 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006617 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006618 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006619 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006620 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006621
Daniel Veillard4c004142003-10-07 11:33:24 +00006622 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6623 if (schema->topgrammar == NULL) {
6624 return (schema);
6625 }
6626 /*
6627 * Link the new grammar in the tree
6628 */
6629 ret->parent = ctxt->grammar;
6630 if (ctxt->grammar != NULL) {
6631 tmp = ctxt->grammar->children;
6632 if (tmp == NULL) {
6633 ctxt->grammar->children = ret;
6634 } else {
6635 while (tmp->next != NULL)
6636 tmp = tmp->next;
6637 tmp->next = ret;
6638 }
6639 }
6640 old = ctxt->grammar;
6641 ctxt->grammar = ret;
6642 xmlRelaxNGParseStart(ctxt, node);
6643 if (old != NULL)
6644 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006645 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006646 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006647 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006648 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6649 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6650 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6651 while ((schema->topgrammar->start != NULL) &&
6652 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6653 (schema->topgrammar->start->next != NULL))
6654 schema->topgrammar->start =
6655 schema->topgrammar->start->content;
6656 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6657 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6658 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006659 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006660#ifdef DEBUG
6661 if (schema == NULL)
6662 xmlGenericError(xmlGenericErrorContext,
6663 "xmlRelaxNGParseDocument() failed\n");
6664#endif
6665
6666 return (schema);
6667}
6668
6669/************************************************************************
6670 * *
6671 * Reading RelaxNGs *
6672 * *
6673 ************************************************************************/
6674
6675/**
6676 * xmlRelaxNGNewParserCtxt:
6677 * @URL: the location of the schema
6678 *
6679 * Create an XML RelaxNGs parse context for that file/resource expected
6680 * to contain an XML RelaxNGs file.
6681 *
6682 * Returns the parser context or NULL in case of error
6683 */
6684xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006685xmlRelaxNGNewParserCtxt(const char *URL)
6686{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006687 xmlRelaxNGParserCtxtPtr ret;
6688
6689 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006690 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006691
Daniel Veillard4c004142003-10-07 11:33:24 +00006692 ret =
6693 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006694 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006695 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006696 return (NULL);
6697 }
6698 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006699 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006700 ret->error = xmlGenericError;
6701 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006702 return (ret);
6703}
6704
6705/**
6706 * xmlRelaxNGNewMemParserCtxt:
6707 * @buffer: a pointer to a char array containing the schemas
6708 * @size: the size of the array
6709 *
6710 * Create an XML RelaxNGs parse context for that memory buffer expected
6711 * to contain an XML RelaxNGs file.
6712 *
6713 * Returns the parser context or NULL in case of error
6714 */
6715xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006716xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6717{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006718 xmlRelaxNGParserCtxtPtr ret;
6719
6720 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006721 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006722
Daniel Veillard4c004142003-10-07 11:33:24 +00006723 ret =
6724 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006725 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006726 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006727 return (NULL);
6728 }
6729 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6730 ret->buffer = buffer;
6731 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006732 ret->error = xmlGenericError;
6733 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006734 return (ret);
6735}
6736
6737/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006738 * xmlRelaxNGNewDocParserCtxt:
6739 * @doc: a preparsed document tree
6740 *
6741 * Create an XML RelaxNGs parser context for that document.
6742 * Note: since the process of compiling a RelaxNG schemas modifies the
6743 * document, the @doc parameter is duplicated internally.
6744 *
6745 * Returns the parser context or NULL in case of error
6746 */
6747xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006748xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6749{
Daniel Veillard33300b42003-04-17 09:09:19 +00006750 xmlRelaxNGParserCtxtPtr ret;
6751 xmlDocPtr copy;
6752
6753 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006754 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006755 copy = xmlCopyDoc(doc, 1);
6756 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006757 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006758
Daniel Veillard4c004142003-10-07 11:33:24 +00006759 ret =
6760 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006761 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006762 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006763 return (NULL);
6764 }
6765 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6766 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006767 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006768 ret->userData = xmlGenericErrorContext;
6769 return (ret);
6770}
6771
6772/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006773 * xmlRelaxNGFreeParserCtxt:
6774 * @ctxt: the schema parser context
6775 *
6776 * Free the resources associated to the schema parser context
6777 */
6778void
Daniel Veillard4c004142003-10-07 11:33:24 +00006779xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6780{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006781 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006782 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006783 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006784 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006785 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006786 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006787 if (ctxt->interleaves != NULL)
6788 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006789 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006790 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006791 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006792 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006793 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006794 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006795 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006796 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006797 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006798 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006799
Daniel Veillard4c004142003-10-07 11:33:24 +00006800 for (i = 0; i < ctxt->defNr; i++)
6801 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6802 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006803 }
Daniel Veillard42595322004-11-08 10:52:06 +00006804 if ((ctxt->document != NULL) && (ctxt->freedoc))
6805 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006806 xmlFree(ctxt);
6807}
6808
Daniel Veillard6eadf632003-01-23 18:29:16 +00006809/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006810 * xmlRelaxNGNormExtSpace:
6811 * @value: a value
6812 *
6813 * Removes the leading and ending spaces of the value
6814 * The string is modified "in situ"
6815 */
6816static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006817xmlRelaxNGNormExtSpace(xmlChar * value)
6818{
Daniel Veillardd2298792003-02-14 16:54:11 +00006819 xmlChar *start = value;
6820 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006821
Daniel Veillard4c004142003-10-07 11:33:24 +00006822 if (value == NULL)
6823 return;
6824
William M. Brack76e95df2003-10-18 16:20:14 +00006825 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006826 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006827 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006828 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006829 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006830 cur++;
6831 if (*cur == 0)
6832 return;
6833 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006834 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006835 cur++;
6836 if (*cur == 0) {
6837 *start = 0;
6838 return;
6839 }
6840 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006841 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006842 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006843 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006844 *start++ = *cur++;
6845 if (*cur == 0) {
6846 *start = 0;
6847 return;
6848 }
6849 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006850 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006851 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006852 if (*cur == 0) {
6853 *start = 0;
6854 return;
6855 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006856 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006857 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006858 }
6859}
6860
6861/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006862 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006863 * @ctxt: a Relax-NG parser context
6864 * @node: a Relax-NG node
6865 *
6866 * Check all the attributes on the given node
6867 */
6868static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006869xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6870{
Daniel Veillardd2298792003-02-14 16:54:11 +00006871 xmlAttrPtr cur, next;
6872
6873 cur = node->properties;
6874 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006875 next = cur->next;
6876 if ((cur->ns == NULL) ||
6877 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6878 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6879 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6880 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6881 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6882 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6883 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6884 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6885 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6886 "Attribute %s is not allowed on %s\n",
6887 cur->name, node->name);
6888 }
6889 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6890 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6891 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6892 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6893 "Attribute %s is not allowed on %s\n",
6894 cur->name, node->name);
6895 }
6896 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6897 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6898 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6899 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6900 "Attribute %s is not allowed on %s\n",
6901 cur->name, node->name);
6902 }
6903 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6904 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6905 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6906 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6907 "Attribute %s is not allowed on %s\n",
6908 cur->name, node->name);
6909 }
6910 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6911 xmlChar *val;
6912 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006913
Daniel Veillard4c004142003-10-07 11:33:24 +00006914 val = xmlNodeListGetString(node->doc, cur->children, 1);
6915 if (val != NULL) {
6916 if (val[0] != 0) {
6917 uri = xmlParseURI((const char *) val);
6918 if (uri == NULL) {
6919 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6920 "Attribute %s contains invalid URI %s\n",
6921 cur->name, val);
6922 } else {
6923 if (uri->scheme == NULL) {
6924 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6925 "Attribute %s URI %s is not absolute\n",
6926 cur->name, val);
6927 }
6928 if (uri->fragment != NULL) {
6929 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6930 "Attribute %s URI %s has a fragment ID\n",
6931 cur->name, val);
6932 }
6933 xmlFreeURI(uri);
6934 }
6935 }
6936 xmlFree(val);
6937 }
6938 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6939 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6940 "Unknown attribute %s on %s\n", cur->name,
6941 node->name);
6942 }
6943 }
6944 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006945 }
6946}
6947
6948/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006949 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006950 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006951 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006952 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006953 * Cleanup the subtree from unwanted nodes for parsing, resolve
6954 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006955 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006956static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006957xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6958{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006959 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006960
Daniel Veillard6eadf632003-01-23 18:29:16 +00006961 delete = NULL;
6962 cur = root;
6963 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006964 if (delete != NULL) {
6965 xmlUnlinkNode(delete);
6966 xmlFreeNode(delete);
6967 delete = NULL;
6968 }
6969 if (cur->type == XML_ELEMENT_NODE) {
6970 /*
6971 * Simplification 4.1. Annotations
6972 */
6973 if ((cur->ns == NULL) ||
6974 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6975 if ((cur->parent != NULL) &&
6976 (cur->parent->type == XML_ELEMENT_NODE) &&
6977 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6978 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6979 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6980 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6981 "element %s doesn't allow foreign elements\n",
6982 cur->parent->name, NULL);
6983 }
6984 delete = cur;
6985 goto skip_children;
6986 } else {
6987 xmlRelaxNGCleanupAttributes(ctxt, cur);
6988 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6989 xmlChar *href, *ns, *base, *URL;
6990 xmlRelaxNGDocumentPtr docu;
6991 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00006992 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006993
Daniel Veillard4c004142003-10-07 11:33:24 +00006994 ns = xmlGetProp(cur, BAD_CAST "ns");
6995 if (ns == NULL) {
6996 tmp = cur->parent;
6997 while ((tmp != NULL) &&
6998 (tmp->type == XML_ELEMENT_NODE)) {
6999 ns = xmlGetProp(tmp, BAD_CAST "ns");
7000 if (ns != NULL)
7001 break;
7002 tmp = tmp->parent;
7003 }
7004 }
7005 href = xmlGetProp(cur, BAD_CAST "href");
7006 if (href == NULL) {
7007 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7008 "xmlRelaxNGParse: externalRef has no href attribute\n",
7009 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007010 if (ns != NULL)
7011 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007012 delete = cur;
7013 goto skip_children;
7014 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007015 uri = xmlParseURI((const char *) href);
7016 if (uri == NULL) {
7017 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7018 "Incorrect URI for externalRef %s\n",
7019 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007020 if (ns != NULL)
7021 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007022 if (href != NULL)
7023 xmlFree(href);
7024 delete = cur;
7025 goto skip_children;
7026 }
7027 if (uri->fragment != NULL) {
7028 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7029 "Fragment forbidden in URI for externalRef %s\n",
7030 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007031 if (ns != NULL)
7032 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007033 xmlFreeURI(uri);
7034 if (href != NULL)
7035 xmlFree(href);
7036 delete = cur;
7037 goto skip_children;
7038 }
7039 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007040 base = xmlNodeGetBase(cur->doc, cur);
7041 URL = xmlBuildURI(href, base);
7042 if (URL == NULL) {
7043 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7044 "Failed to compute URL for externalRef %s\n",
7045 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007046 if (ns != NULL)
7047 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007048 if (href != NULL)
7049 xmlFree(href);
7050 if (base != NULL)
7051 xmlFree(base);
7052 delete = cur;
7053 goto skip_children;
7054 }
7055 if (href != NULL)
7056 xmlFree(href);
7057 if (base != NULL)
7058 xmlFree(base);
7059 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7060 if (docu == NULL) {
7061 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7062 "Failed to load externalRef %s\n", URL,
7063 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007064 if (ns != NULL)
7065 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007066 xmlFree(URL);
7067 delete = cur;
7068 goto skip_children;
7069 }
7070 if (ns != NULL)
7071 xmlFree(ns);
7072 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007073 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007074 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7075 xmlChar *href, *ns, *base, *URL;
7076 xmlRelaxNGIncludePtr incl;
7077 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007078
Daniel Veillard4c004142003-10-07 11:33:24 +00007079 href = xmlGetProp(cur, BAD_CAST "href");
7080 if (href == NULL) {
7081 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7082 "xmlRelaxNGParse: include has no href attribute\n",
7083 NULL, NULL);
7084 delete = cur;
7085 goto skip_children;
7086 }
7087 base = xmlNodeGetBase(cur->doc, cur);
7088 URL = xmlBuildURI(href, base);
7089 if (URL == NULL) {
7090 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7091 "Failed to compute URL for include %s\n",
7092 href, NULL);
7093 if (href != NULL)
7094 xmlFree(href);
7095 if (base != NULL)
7096 xmlFree(base);
7097 delete = cur;
7098 goto skip_children;
7099 }
7100 if (href != NULL)
7101 xmlFree(href);
7102 if (base != NULL)
7103 xmlFree(base);
7104 ns = xmlGetProp(cur, BAD_CAST "ns");
7105 if (ns == NULL) {
7106 tmp = cur->parent;
7107 while ((tmp != NULL) &&
7108 (tmp->type == XML_ELEMENT_NODE)) {
7109 ns = xmlGetProp(tmp, BAD_CAST "ns");
7110 if (ns != NULL)
7111 break;
7112 tmp = tmp->parent;
7113 }
7114 }
7115 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7116 if (ns != NULL)
7117 xmlFree(ns);
7118 if (incl == NULL) {
7119 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7120 "Failed to load include %s\n", URL,
7121 NULL);
7122 xmlFree(URL);
7123 delete = cur;
7124 goto skip_children;
7125 }
7126 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007127 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007128 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7129 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7130 {
7131 xmlChar *name, *ns;
7132 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007133
Daniel Veillard4c004142003-10-07 11:33:24 +00007134 /*
7135 * Simplification 4.8. name attribute of element
7136 * and attribute elements
7137 */
7138 name = xmlGetProp(cur, BAD_CAST "name");
7139 if (name != NULL) {
7140 if (cur->children == NULL) {
7141 text =
7142 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7143 name);
7144 } else {
7145 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007146
Daniel Veillard03a53c32004-10-26 16:06:51 +00007147 node = xmlNewDocNode(cur->doc, cur->ns,
7148 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007149 if (node != NULL) {
7150 xmlAddPrevSibling(cur->children, node);
7151 text = xmlNewText(name);
7152 xmlAddChild(node, text);
7153 text = node;
7154 }
7155 }
7156 if (text == NULL) {
7157 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7158 "Failed to create a name %s element\n",
7159 name, NULL);
7160 }
7161 xmlUnsetProp(cur, BAD_CAST "name");
7162 xmlFree(name);
7163 ns = xmlGetProp(cur, BAD_CAST "ns");
7164 if (ns != NULL) {
7165 if (text != NULL) {
7166 xmlSetProp(text, BAD_CAST "ns", ns);
7167 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7168 }
7169 xmlFree(ns);
7170 } else if (xmlStrEqual(cur->name,
7171 BAD_CAST "attribute")) {
7172 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7173 }
7174 }
7175 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7176 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7177 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7178 /*
7179 * Simplification 4.8. name attribute of element
7180 * and attribute elements
7181 */
7182 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7183 xmlNodePtr node;
7184 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007185
Daniel Veillard4c004142003-10-07 11:33:24 +00007186 node = cur->parent;
7187 while ((node != NULL) &&
7188 (node->type == XML_ELEMENT_NODE)) {
7189 ns = xmlGetProp(node, BAD_CAST "ns");
7190 if (ns != NULL) {
7191 break;
7192 }
7193 node = node->parent;
7194 }
7195 if (ns == NULL) {
7196 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7197 } else {
7198 xmlSetProp(cur, BAD_CAST "ns", ns);
7199 xmlFree(ns);
7200 }
7201 }
7202 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7203 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007204
Daniel Veillard4c004142003-10-07 11:33:24 +00007205 /*
7206 * Simplification: 4.10. QNames
7207 */
7208 name = xmlNodeGetContent(cur);
7209 if (name != NULL) {
7210 local = xmlSplitQName2(name, &prefix);
7211 if (local != NULL) {
7212 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007213
Daniel Veillard4c004142003-10-07 11:33:24 +00007214 ns = xmlSearchNs(cur->doc, cur, prefix);
7215 if (ns == NULL) {
7216 xmlRngPErr(ctxt, cur,
7217 XML_RNGP_PREFIX_UNDEFINED,
7218 "xmlRelaxNGParse: no namespace for prefix %s\n",
7219 prefix, NULL);
7220 } else {
7221 xmlSetProp(cur, BAD_CAST "ns",
7222 ns->href);
7223 xmlNodeSetContent(cur, local);
7224 }
7225 xmlFree(local);
7226 xmlFree(prefix);
7227 }
7228 xmlFree(name);
7229 }
7230 }
7231 /*
7232 * 4.16
7233 */
7234 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7235 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7236 xmlRngPErr(ctxt, cur,
7237 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7238 "Found nsName/except//nsName forbidden construct\n",
7239 NULL, NULL);
7240 }
7241 }
7242 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7243 (cur != root)) {
7244 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007245
Daniel Veillard4c004142003-10-07 11:33:24 +00007246 /*
7247 * 4.16
7248 */
7249 if ((cur->parent != NULL) &&
7250 (xmlStrEqual
7251 (cur->parent->name, BAD_CAST "anyName"))) {
7252 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7253 xmlRelaxNGCleanupTree(ctxt, cur);
7254 ctxt->flags = oldflags;
7255 goto skip_children;
7256 } else if ((cur->parent != NULL) &&
7257 (xmlStrEqual
7258 (cur->parent->name, BAD_CAST "nsName"))) {
7259 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7260 xmlRelaxNGCleanupTree(ctxt, cur);
7261 ctxt->flags = oldflags;
7262 goto skip_children;
7263 }
7264 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7265 /*
7266 * 4.16
7267 */
7268 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7269 xmlRngPErr(ctxt, cur,
7270 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7271 "Found anyName/except//anyName forbidden construct\n",
7272 NULL, NULL);
7273 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7274 xmlRngPErr(ctxt, cur,
7275 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7276 "Found nsName/except//anyName forbidden construct\n",
7277 NULL, NULL);
7278 }
7279 }
7280 /*
7281 * Thisd is not an else since "include" is transformed
7282 * into a div
7283 */
7284 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7285 xmlChar *ns;
7286 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007287
Daniel Veillard4c004142003-10-07 11:33:24 +00007288 /*
7289 * implements rule 4.11
7290 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007291
Daniel Veillard4c004142003-10-07 11:33:24 +00007292 ns = xmlGetProp(cur, BAD_CAST "ns");
7293
7294 child = cur->children;
7295 ins = cur;
7296 while (child != NULL) {
7297 if (ns != NULL) {
7298 if (!xmlHasProp(child, BAD_CAST "ns")) {
7299 xmlSetProp(child, BAD_CAST "ns", ns);
7300 }
7301 }
7302 tmp = child->next;
7303 xmlUnlinkNode(child);
7304 ins = xmlAddNextSibling(ins, child);
7305 child = tmp;
7306 }
7307 if (ns != NULL)
7308 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007309 /*
7310 * Since we are about to delete cur, if it's nsDef is non-NULL we
7311 * need to preserve it (it contains the ns definitions for the
7312 * children we just moved). We'll just stick it on to the end
7313 * of cur->parent's list, since it's never going to be re-serialized
7314 * (bug 143738).
7315 */
7316 if (cur->nsDef != NULL) {
7317 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7318 while (parDef->next != NULL)
7319 parDef = parDef->next;
7320 parDef->next = cur->nsDef;
7321 cur->nsDef = NULL;
7322 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007323 delete = cur;
7324 goto skip_children;
7325 }
7326 }
7327 }
7328 /*
7329 * Simplification 4.2 whitespaces
7330 */
7331 else if ((cur->type == XML_TEXT_NODE) ||
7332 (cur->type == XML_CDATA_SECTION_NODE)) {
7333 if (IS_BLANK_NODE(cur)) {
7334 if (cur->parent->type == XML_ELEMENT_NODE) {
7335 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7336 &&
7337 (!xmlStrEqual
7338 (cur->parent->name, BAD_CAST "param")))
7339 delete = cur;
7340 } else {
7341 delete = cur;
7342 goto skip_children;
7343 }
7344 }
7345 } else {
7346 delete = cur;
7347 goto skip_children;
7348 }
7349
7350 /*
7351 * Skip to next node
7352 */
7353 if (cur->children != NULL) {
7354 if ((cur->children->type != XML_ENTITY_DECL) &&
7355 (cur->children->type != XML_ENTITY_REF_NODE) &&
7356 (cur->children->type != XML_ENTITY_NODE)) {
7357 cur = cur->children;
7358 continue;
7359 }
7360 }
7361 skip_children:
7362 if (cur->next != NULL) {
7363 cur = cur->next;
7364 continue;
7365 }
7366
7367 do {
7368 cur = cur->parent;
7369 if (cur == NULL)
7370 break;
7371 if (cur == root) {
7372 cur = NULL;
7373 break;
7374 }
7375 if (cur->next != NULL) {
7376 cur = cur->next;
7377 break;
7378 }
7379 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007380 }
7381 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007382 xmlUnlinkNode(delete);
7383 xmlFreeNode(delete);
7384 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007385 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007386}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007387
Daniel Veillardc5312d72003-02-21 17:14:10 +00007388/**
7389 * xmlRelaxNGCleanupDoc:
7390 * @ctxt: a Relax-NG parser context
7391 * @doc: an xmldocPtr document pointer
7392 *
7393 * Cleanup the document from unwanted nodes for parsing, resolve
7394 * Include and externalRef lookups.
7395 *
7396 * Returns the cleaned up document or NULL in case of error
7397 */
7398static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007399xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7400{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007401 xmlNodePtr root;
7402
7403 /*
7404 * Extract the root
7405 */
7406 root = xmlDocGetRootElement(doc);
7407 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007408 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7409 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007410 return (NULL);
7411 }
7412 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007413 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007414}
7415
7416/**
7417 * xmlRelaxNGParse:
7418 * @ctxt: a Relax-NG parser context
7419 *
7420 * parse a schema definition resource and build an internal
7421 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007422 *
7423 * Returns the internal XML RelaxNG structure built from the resource or
7424 * NULL in case of error
7425 */
7426xmlRelaxNGPtr
7427xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7428{
7429 xmlRelaxNGPtr ret = NULL;
7430 xmlDocPtr doc;
7431 xmlNodePtr root;
7432
7433 xmlRelaxNGInitTypes();
7434
7435 if (ctxt == NULL)
7436 return (NULL);
7437
7438 /*
7439 * First step is to parse the input document into an DOM/Infoset
7440 */
7441 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007442 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007443 if (doc == NULL) {
7444 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7445 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7446 NULL);
7447 return (NULL);
7448 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007449 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007450 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007451 if (doc == NULL) {
7452 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7453 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7454 NULL);
7455 return (NULL);
7456 }
7457 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7458 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007459 } else if (ctxt->document != NULL) {
7460 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007461 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007462 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7463 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7464 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007465 }
7466 ctxt->document = doc;
7467
7468 /*
7469 * Some preprocessing of the document content
7470 */
7471 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7472 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007473 xmlFreeDoc(ctxt->document);
7474 ctxt->document = NULL;
7475 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007476 }
7477
Daniel Veillard6eadf632003-01-23 18:29:16 +00007478 /*
7479 * Then do the parsing for good
7480 */
7481 root = xmlDocGetRootElement(doc);
7482 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007483 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7484 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007485 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillard3f845a92006-04-13 07:33:44 +00007486
7487 xmlFreeDoc(ctxt->document);
7488 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007489 return (NULL);
7490 }
7491 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007492 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007493 xmlFreeDoc(ctxt->document);
7494 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007495 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007496 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007497
7498 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007499 * Check the ref/defines links
7500 */
7501 /*
7502 * try to preprocess interleaves
7503 */
7504 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007505 xmlHashScan(ctxt->interleaves,
7506 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007507 }
7508
7509 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007510 * if there was a parsing error return NULL
7511 */
7512 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007513 xmlRelaxNGFree(ret);
7514 ctxt->document = NULL;
7515 xmlFreeDoc(doc);
7516 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007517 }
7518
7519 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007520 * try to compile (parts of) the schemas
7521 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007522 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7523 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007524 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007525
Daniel Veillard4c004142003-10-07 11:33:24 +00007526 def = xmlRelaxNGNewDefine(ctxt, NULL);
7527 if (def != NULL) {
7528 def->type = XML_RELAXNG_START;
7529 def->content = ret->topgrammar->start;
7530 ret->topgrammar->start = def;
7531 }
7532 }
7533 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007534 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007535
7536 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007537 * Transfer the pointer for cleanup at the schema level.
7538 */
7539 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007540 ctxt->document = NULL;
7541 ret->documents = ctxt->documents;
7542 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007543
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007544 ret->includes = ctxt->includes;
7545 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007546 ret->defNr = ctxt->defNr;
7547 ret->defTab = ctxt->defTab;
7548 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007549 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007550 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007551
7552 return (ret);
7553}
Daniel Veillard4c004142003-10-07 11:33:24 +00007554
Daniel Veillard6eadf632003-01-23 18:29:16 +00007555/**
7556 * xmlRelaxNGSetParserErrors:
7557 * @ctxt: a Relax-NG validation context
7558 * @err: the error callback
7559 * @warn: the warning callback
7560 * @ctx: contextual data for the callbacks
7561 *
7562 * Set the callback functions used to handle errors for a validation context
7563 */
7564void
7565xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007566 xmlRelaxNGValidityErrorFunc err,
7567 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7568{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007569 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007570 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007571 ctxt->error = err;
7572 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007573 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007574 ctxt->userData = ctx;
7575}
Daniel Veillard409a8142003-07-18 15:16:57 +00007576
7577/**
7578 * xmlRelaxNGGetParserErrors:
7579 * @ctxt: a Relax-NG validation context
7580 * @err: the error callback result
7581 * @warn: the warning callback result
7582 * @ctx: contextual data for the callbacks result
7583 *
7584 * Get the callback information used to handle errors for a validation context
7585 *
7586 * Returns -1 in case of failure, 0 otherwise.
7587 */
7588int
7589xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007590 xmlRelaxNGValidityErrorFunc * err,
7591 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7592{
Daniel Veillard409a8142003-07-18 15:16:57 +00007593 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 return (-1);
7595 if (err != NULL)
7596 *err = ctxt->error;
7597 if (warn != NULL)
7598 *warn = ctxt->warning;
7599 if (ctx != NULL)
7600 *ctx = ctxt->userData;
7601 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007602}
7603
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007604/**
7605 * xmlRelaxNGSetParserStructuredErrors:
7606 * @ctxt: a Relax-NG parser context
7607 * @serror: the error callback
7608 * @ctx: contextual data for the callbacks
7609 *
7610 * Set the callback functions used to handle errors for a parsing context
7611 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007612void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007613xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007614 xmlStructuredErrorFunc serror,
7615 void *ctx)
7616{
7617 if (ctxt == NULL)
7618 return;
7619 ctxt->serror = serror;
7620 ctxt->error = NULL;
7621 ctxt->warning = NULL;
7622 ctxt->userData = ctx;
7623}
7624
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007625#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007626
Daniel Veillard6eadf632003-01-23 18:29:16 +00007627/************************************************************************
7628 * *
7629 * Dump back a compiled form *
7630 * *
7631 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007632static void xmlRelaxNGDumpDefine(FILE * output,
7633 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007634
7635/**
7636 * xmlRelaxNGDumpDefines:
7637 * @output: the file output
7638 * @defines: a list of define structures
7639 *
7640 * Dump a RelaxNG structure back
7641 */
7642static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007643xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7644{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007645 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007646 xmlRelaxNGDumpDefine(output, defines);
7647 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007648 }
7649}
7650
7651/**
7652 * xmlRelaxNGDumpDefine:
7653 * @output: the file output
7654 * @define: a define structure
7655 *
7656 * Dump a RelaxNG structure back
7657 */
7658static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007659xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7660{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007661 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007662 return;
7663 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007664 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007665 fprintf(output, "<empty/>\n");
7666 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007667 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007668 fprintf(output, "<notAllowed/>\n");
7669 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007670 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007671 fprintf(output, "<text/>\n");
7672 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007673 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007674 fprintf(output, "<element>\n");
7675 if (define->name != NULL) {
7676 fprintf(output, "<name");
7677 if (define->ns != NULL)
7678 fprintf(output, " ns=\"%s\"", define->ns);
7679 fprintf(output, ">%s</name>\n", define->name);
7680 }
7681 xmlRelaxNGDumpDefines(output, define->attrs);
7682 xmlRelaxNGDumpDefines(output, define->content);
7683 fprintf(output, "</element>\n");
7684 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007685 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007686 fprintf(output, "<list>\n");
7687 xmlRelaxNGDumpDefines(output, define->content);
7688 fprintf(output, "</list>\n");
7689 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007690 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007691 fprintf(output, "<oneOrMore>\n");
7692 xmlRelaxNGDumpDefines(output, define->content);
7693 fprintf(output, "</oneOrMore>\n");
7694 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007695 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007696 fprintf(output, "<zeroOrMore>\n");
7697 xmlRelaxNGDumpDefines(output, define->content);
7698 fprintf(output, "</zeroOrMore>\n");
7699 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007700 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007701 fprintf(output, "<choice>\n");
7702 xmlRelaxNGDumpDefines(output, define->content);
7703 fprintf(output, "</choice>\n");
7704 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007705 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007706 fprintf(output, "<group>\n");
7707 xmlRelaxNGDumpDefines(output, define->content);
7708 fprintf(output, "</group>\n");
7709 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007710 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007711 fprintf(output, "<interleave>\n");
7712 xmlRelaxNGDumpDefines(output, define->content);
7713 fprintf(output, "</interleave>\n");
7714 break;
7715 case XML_RELAXNG_OPTIONAL:
7716 fprintf(output, "<optional>\n");
7717 xmlRelaxNGDumpDefines(output, define->content);
7718 fprintf(output, "</optional>\n");
7719 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007720 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007721 fprintf(output, "<attribute>\n");
7722 xmlRelaxNGDumpDefines(output, define->content);
7723 fprintf(output, "</attribute>\n");
7724 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007725 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007726 fprintf(output, "<define");
7727 if (define->name != NULL)
7728 fprintf(output, " name=\"%s\"", define->name);
7729 fprintf(output, ">\n");
7730 xmlRelaxNGDumpDefines(output, define->content);
7731 fprintf(output, "</define>\n");
7732 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007733 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007734 fprintf(output, "<ref");
7735 if (define->name != NULL)
7736 fprintf(output, " name=\"%s\"", define->name);
7737 fprintf(output, ">\n");
7738 xmlRelaxNGDumpDefines(output, define->content);
7739 fprintf(output, "</ref>\n");
7740 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007741 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007742 fprintf(output, "<parentRef");
7743 if (define->name != NULL)
7744 fprintf(output, " name=\"%s\"", define->name);
7745 fprintf(output, ">\n");
7746 xmlRelaxNGDumpDefines(output, define->content);
7747 fprintf(output, "</parentRef>\n");
7748 break;
7749 case XML_RELAXNG_EXTERNALREF:
7750 fprintf(output, "<externalRef>");
7751 xmlRelaxNGDumpDefines(output, define->content);
7752 fprintf(output, "</externalRef>\n");
7753 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007754 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007755 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007756 TODO break;
7757 case XML_RELAXNG_START:
7758 case XML_RELAXNG_EXCEPT:
7759 case XML_RELAXNG_PARAM:
7760 TODO break;
7761 case XML_RELAXNG_NOOP:
7762 xmlRelaxNGDumpDefines(output, define->content);
7763 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007764 }
7765}
Daniel Veillard4c004142003-10-07 11:33:24 +00007766
Daniel Veillard6eadf632003-01-23 18:29:16 +00007767/**
7768 * xmlRelaxNGDumpGrammar:
7769 * @output: the file output
7770 * @grammar: a grammar structure
7771 * @top: is this a top grammar
7772 *
7773 * Dump a RelaxNG structure back
7774 */
7775static void
7776xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7777{
7778 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007779 return;
7780
Daniel Veillard6eadf632003-01-23 18:29:16 +00007781 fprintf(output, "<grammar");
7782 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007783 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7784 switch (grammar->combine) {
7785 case XML_RELAXNG_COMBINE_UNDEFINED:
7786 break;
7787 case XML_RELAXNG_COMBINE_CHOICE:
7788 fprintf(output, " combine=\"choice\"");
7789 break;
7790 case XML_RELAXNG_COMBINE_INTERLEAVE:
7791 fprintf(output, " combine=\"interleave\"");
7792 break;
7793 default:
7794 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007795 }
7796 fprintf(output, ">\n");
7797 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007798 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007799 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007800 fprintf(output, "<start>\n");
7801 xmlRelaxNGDumpDefine(output, grammar->start);
7802 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007803 }
7804 /* TODO ? Dump the defines ? */
7805 fprintf(output, "</grammar>\n");
7806}
7807
7808/**
7809 * xmlRelaxNGDump:
7810 * @output: the file output
7811 * @schema: a schema structure
7812 *
7813 * Dump a RelaxNG structure back
7814 */
7815void
7816xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7817{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007818 if (output == NULL)
7819 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007820 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007821 fprintf(output, "RelaxNG empty or failed to compile\n");
7822 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007823 }
7824 fprintf(output, "RelaxNG: ");
7825 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007826 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007827 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007828 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007829 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007830 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007831 }
7832 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007833 fprintf(output, "RelaxNG has no top grammar\n");
7834 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007835 }
7836 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7837}
7838
Daniel Veillardfebcca42003-02-16 15:44:18 +00007839/**
7840 * xmlRelaxNGDumpTree:
7841 * @output: the file output
7842 * @schema: a schema structure
7843 *
7844 * Dump the transformed RelaxNG tree.
7845 */
7846void
7847xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7848{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007849 if (output == NULL)
7850 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007851 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007852 fprintf(output, "RelaxNG empty or failed to compile\n");
7853 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007854 }
7855 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007856 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007857 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007858 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007859 }
7860}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007861#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007862
Daniel Veillard6eadf632003-01-23 18:29:16 +00007863/************************************************************************
7864 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007865 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007866 * *
7867 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007868static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7869 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007870
7871/**
7872 * xmlRelaxNGValidateCompiledCallback:
7873 * @exec: the regular expression instance
7874 * @token: the token which matched
7875 * @transdata: callback data, the define for the subelement if available
7876 @ @inputdata: callback data, the Relax NG validation context
7877 *
7878 * Handle the callback and if needed validate the element children.
7879 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007880static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007881xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007882 const xmlChar * token,
7883 void *transdata, void *inputdata)
7884{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007885 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7886 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7887 int ret;
7888
7889#ifdef DEBUG_COMPILE
7890 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007891 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007892#endif
7893 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007894 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007895 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007896 }
7897 if (define == NULL) {
7898 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007899 return;
7900 fprintf(stderr, "callback on %s missing define\n", token);
7901 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7902 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7903 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007904 }
7905 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007906 fprintf(stderr, "callback on %s missing info\n", token);
7907 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7908 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7909 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007910 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007911 fprintf(stderr, "callback on %s define is not element\n", token);
7912 if (ctxt->errNo == XML_RELAXNG_OK)
7913 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7914 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007915 }
7916 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007917 if (ret != 0)
7918 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007919}
7920
7921/**
7922 * xmlRelaxNGValidateCompiledContent:
7923 * @ctxt: the RelaxNG validation context
7924 * @regexp: the regular expression as compiled
7925 * @content: list of children to test against the regexp
7926 *
7927 * Validate the content model of an element or start using the regexp
7928 *
7929 * Returns 0 in case of success, -1 in case of error.
7930 */
7931static int
7932xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007933 xmlRegexpPtr regexp, xmlNodePtr content)
7934{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007935 xmlRegExecCtxtPtr exec;
7936 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007937 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007938 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007939
7940 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007941 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007942 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007943 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007944 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007945 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007946 cur = content;
7947 while (cur != NULL) {
7948 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007949 switch (cur->type) {
7950 case XML_TEXT_NODE:
7951 case XML_CDATA_SECTION_NODE:
7952 if (xmlIsBlankNode(cur))
7953 break;
7954 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7955 if (ret < 0) {
7956 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7957 cur->parent->name);
7958 }
7959 break;
7960 case XML_ELEMENT_NODE:
7961 if (cur->ns != NULL) {
7962 ret = xmlRegExecPushString2(exec, cur->name,
7963 cur->ns->href, ctxt);
7964 } else {
7965 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7966 }
7967 if (ret < 0) {
7968 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7969 }
7970 break;
7971 default:
7972 break;
7973 }
7974 if (ret < 0)
7975 break;
7976 /*
7977 * Switch to next element
7978 */
7979 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007980 }
7981 ret = xmlRegExecPushString(exec, NULL, NULL);
7982 if (ret == 1) {
7983 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00007984 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007985 } else if (ret == 0) {
7986 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00007987 * TODO: get some of the names needed to exit the current state of exec
7988 */
7989 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7990 ret = -1;
7991 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7992 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007993 } else {
7994 ret = -1;
7995 }
7996 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007997 /*
7998 * There might be content model errors outside of the pure
7999 * regexp validation, e.g. for attribute values.
8000 */
8001 if ((ret == 0) && (ctxt->perr != 0)) {
8002 ret = ctxt->perr;
8003 }
8004 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008005 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008006}
8007
8008/************************************************************************
8009 * *
8010 * Progressive validation of when possible *
8011 * *
8012 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008013static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8014 xmlRelaxNGDefinePtr defines);
8015static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008016 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008017static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008018
8019/**
8020 * xmlRelaxNGElemPush:
8021 * @ctxt: the validation context
8022 * @exec: the regexp runtime for the new content model
8023 *
8024 * Push a new regexp for the current node content model on the stack
8025 *
8026 * Returns 0 in case of success and -1 in case of error.
8027 */
8028static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008029xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8030{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008031 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008032 ctxt->elemMax = 10;
8033 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8034 sizeof
8035 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008036 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008037 xmlRngVErrMemory(ctxt, "validating\n");
8038 return (-1);
8039 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008040 }
8041 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008042 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008043 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008044 ctxt->elemMax *
8045 sizeof
8046 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008047 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008048 xmlRngVErrMemory(ctxt, "validating\n");
8049 return (-1);
8050 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008051 }
8052 ctxt->elemTab[ctxt->elemNr++] = exec;
8053 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008054 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008055}
8056
8057/**
8058 * xmlRelaxNGElemPop:
8059 * @ctxt: the validation context
8060 *
8061 * Pop the regexp of the current node content model from the stack
8062 *
8063 * Returns the exec or NULL if empty
8064 */
8065static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008066xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8067{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008068 xmlRegExecCtxtPtr ret;
8069
Daniel Veillard4c004142003-10-07 11:33:24 +00008070 if (ctxt->elemNr <= 0)
8071 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008072 ctxt->elemNr--;
8073 ret = ctxt->elemTab[ctxt->elemNr];
8074 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008075 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008076 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8077 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008078 ctxt->elem = NULL;
8079 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008080}
8081
8082/**
8083 * xmlRelaxNGValidateProgressiveCallback:
8084 * @exec: the regular expression instance
8085 * @token: the token which matched
8086 * @transdata: callback data, the define for the subelement if available
8087 @ @inputdata: callback data, the Relax NG validation context
8088 *
8089 * Handle the callback and if needed validate the element children.
8090 * some of the in/out informations are passed via the context in @inputdata.
8091 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008092static void
8093xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8094 ATTRIBUTE_UNUSED,
8095 const xmlChar * token,
8096 void *transdata, void *inputdata)
8097{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008098 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8099 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008100 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008101 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008102 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008103
8104#ifdef DEBUG_PROGRESSIVE
8105 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008106 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008107#endif
8108 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008109 fprintf(stderr, "callback on %s missing context\n", token);
8110 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008111 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008112 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008113 ctxt->pstate = 1;
8114 if (define == NULL) {
8115 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008116 return;
8117 fprintf(stderr, "callback on %s missing define\n", token);
8118 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8119 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8120 ctxt->pstate = -1;
8121 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008122 }
8123 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008124 fprintf(stderr, "callback on %s missing info\n", token);
8125 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8126 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8127 ctxt->pstate = -1;
8128 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008129 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008130 fprintf(stderr, "callback on %s define is not element\n", token);
8131 if (ctxt->errNo == XML_RELAXNG_OK)
8132 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8133 ctxt->pstate = -1;
8134 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008135 }
8136 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008137 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8138 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8139 xmlRelaxNGDumpValidError(ctxt);
8140 ctxt->pstate = -1;
8141 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008142 }
8143 if (define->contModel == NULL) {
8144 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008145 * this node cannot be validated in a streamable fashion
8146 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008147#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008148 xmlGenericError(xmlGenericErrorContext,
8149 "Element '%s' validation is not streamable\n",
8150 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008151#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008152 ctxt->pstate = 0;
8153 ctxt->pdef = define;
8154 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008155 }
8156 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008157 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008158 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008159 ctxt->pstate = -1;
8160 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008161 }
8162 xmlRelaxNGElemPush(ctxt, exec);
8163
8164 /*
8165 * Validate the attributes part of the content.
8166 */
8167 state = xmlRelaxNGNewValidState(ctxt, node);
8168 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008169 ctxt->pstate = -1;
8170 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008171 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008172 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008173 ctxt->state = state;
8174 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008175 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8176 if (ret != 0) {
8177 ctxt->pstate = -1;
8178 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8179 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008180 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008181 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008182 ctxt->state->seq = NULL;
8183 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8184 if (ret != 0) {
8185 ctxt->pstate = -1;
8186 }
8187 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008188 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008189 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008190
8191 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008192
Daniel Veillard4c004142003-10-07 11:33:24 +00008193 for (i = 0; i < ctxt->states->nbState; i++) {
8194 state = ctxt->states->tabState[i];
8195 ctxt->state = state;
8196 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008197
Daniel Veillard4c004142003-10-07 11:33:24 +00008198 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8199 tmp = 0;
8200 break;
8201 }
8202 }
8203 if (tmp != 0) {
8204 /*
8205 * validation error, log the message for the "best" one
8206 */
8207 ctxt->flags |= FLAGS_IGNORABLE;
8208 xmlRelaxNGLogBestError(ctxt);
8209 }
8210 for (i = 0; i < ctxt->states->nbState; i++) {
8211 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8212 }
8213 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8214 ctxt->states = NULL;
8215 if ((ret == 0) && (tmp == -1))
8216 ctxt->pstate = -1;
8217 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008218 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008219 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008220 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8221 xmlRelaxNGDumpValidError(ctxt);
8222 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008223 }
8224 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008225}
8226
8227/**
8228 * xmlRelaxNGValidatePushElement:
8229 * @ctxt: the validation context
8230 * @doc: a document instance
8231 * @elem: an element instance
8232 *
8233 * Push a new element start on the RelaxNG validation stack.
8234 *
8235 * returns 1 if no validation problem was found or 0 if validating the
8236 * element requires a full node, and -1 in case of error.
8237 */
8238int
Daniel Veillard33300b42003-04-17 09:09:19 +00008239xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8240 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008241 xmlNodePtr elem)
8242{
8243 int ret = 1;
8244
8245 if ((ctxt == NULL) || (elem == NULL))
8246 return (-1);
8247
8248#ifdef DEBUG_PROGRESSIVE
8249 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8250#endif
8251 if (ctxt->elem == 0) {
8252 xmlRelaxNGPtr schema;
8253 xmlRelaxNGGrammarPtr grammar;
8254 xmlRegExecCtxtPtr exec;
8255 xmlRelaxNGDefinePtr define;
8256
8257 schema = ctxt->schema;
8258 if (schema == NULL) {
8259 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8260 return (-1);
8261 }
8262 grammar = schema->topgrammar;
8263 if ((grammar == NULL) || (grammar->start == NULL)) {
8264 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8265 return (-1);
8266 }
8267 define = grammar->start;
8268 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008269 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008270 return (0);
8271 }
8272 exec = xmlRegNewExecCtxt(define->contModel,
8273 xmlRelaxNGValidateProgressiveCallback,
8274 ctxt);
8275 if (exec == NULL) {
8276 return (-1);
8277 }
8278 xmlRelaxNGElemPush(ctxt, exec);
8279 }
8280 ctxt->pnode = elem;
8281 ctxt->pstate = 0;
8282 if (elem->ns != NULL) {
8283 ret =
8284 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8285 ctxt);
8286 } else {
8287 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8288 }
8289 if (ret < 0) {
8290 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8291 } else {
8292 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008293 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008294 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008295 ret = -1;
8296 else
8297 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008298 }
8299#ifdef DEBUG_PROGRESSIVE
8300 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008301 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8302 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008303#endif
8304 return (ret);
8305}
8306
8307/**
8308 * xmlRelaxNGValidatePushCData:
8309 * @ctxt: the RelaxNG validation context
8310 * @data: some character data read
8311 * @len: the lenght of the data
8312 *
8313 * check the CData parsed for validation in the current stack
8314 *
8315 * returns 1 if no validation problem was found or -1 otherwise
8316 */
8317int
8318xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008319 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008320{
8321 int ret = 1;
8322
8323 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8324 return (-1);
8325
8326#ifdef DEBUG_PROGRESSIVE
8327 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8328#endif
8329
8330 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008331 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008332 break;
8333 data++;
8334 }
8335 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008336 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008337
8338 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8339 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008340 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008341#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008342 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008343#endif
8344
Daniel Veillard4c004142003-10-07 11:33:24 +00008345 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008346 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008347 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008348}
8349
8350/**
8351 * xmlRelaxNGValidatePopElement:
8352 * @ctxt: the RelaxNG validation context
8353 * @doc: a document instance
8354 * @elem: an element instance
8355 *
8356 * Pop the element end from the RelaxNG validation stack.
8357 *
8358 * returns 1 if no validation problem was found or 0 otherwise
8359 */
8360int
8361xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8362 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008363 xmlNodePtr elem)
8364{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008365 int ret;
8366 xmlRegExecCtxtPtr exec;
8367
Daniel Veillard4c004142003-10-07 11:33:24 +00008368 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8369 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008370#ifdef DEBUG_PROGRESSIVE
8371 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8372#endif
8373 /*
8374 * verify that we reached a terminal state of the content model.
8375 */
8376 exec = xmlRelaxNGElemPop(ctxt);
8377 ret = xmlRegExecPushString(exec, NULL, NULL);
8378 if (ret == 0) {
8379 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008380 * TODO: get some of the names needed to exit the current state of exec
8381 */
8382 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8383 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008384 } else if (ret < 0) {
8385 ret = -1;
8386 } else {
8387 ret = 1;
8388 }
8389 xmlRegFreeExecCtxt(exec);
8390#ifdef DEBUG_PROGRESSIVE
8391 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008392 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8393 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008394#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008395 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008396}
8397
8398/**
8399 * xmlRelaxNGValidateFullElement:
8400 * @ctxt: the validation context
8401 * @doc: a document instance
8402 * @elem: an element instance
8403 *
8404 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8405 * 0 and the content of the node has been expanded.
8406 *
8407 * returns 1 if no validation problem was found or -1 in case of error.
8408 */
8409int
Daniel Veillard33300b42003-04-17 09:09:19 +00008410xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8411 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008412 xmlNodePtr elem)
8413{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008414 int ret;
8415 xmlRelaxNGValidStatePtr state;
8416
Daniel Veillard4c004142003-10-07 11:33:24 +00008417 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8418 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008419#ifdef DEBUG_PROGRESSIVE
8420 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8421#endif
8422 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8423 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008424 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008425 }
8426 state->seq = elem;
8427 ctxt->state = state;
8428 ctxt->errNo = XML_RELAXNG_OK;
8429 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008430 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8431 ret = -1;
8432 else
8433 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008434 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008435 ctxt->state = NULL;
8436#ifdef DEBUG_PROGRESSIVE
8437 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008438 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8439 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008440#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008441 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008442}
8443
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008444/************************************************************************
8445 * *
8446 * Generic interpreted validation implementation *
8447 * *
8448 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008449static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8450 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008451
8452/**
8453 * xmlRelaxNGSkipIgnored:
8454 * @ctxt: a schema validation context
8455 * @node: the top node.
8456 *
8457 * Skip ignorable nodes in that context
8458 *
8459 * Returns the new sibling or NULL in case of error.
8460 */
8461static xmlNodePtr
8462xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008463 xmlNodePtr node)
8464{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008465 /*
8466 * TODO complete and handle entities
8467 */
8468 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008469 ((node->type == XML_COMMENT_NODE) ||
8470 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008471 (node->type == XML_XINCLUDE_START) ||
8472 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008473 (((node->type == XML_TEXT_NODE) ||
8474 (node->type == XML_CDATA_SECTION_NODE)) &&
8475 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8476 (IS_BLANK_NODE(node)))))) {
8477 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008478 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008479 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008480}
8481
8482/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008483 * xmlRelaxNGNormalize:
8484 * @ctxt: a schema validation context
8485 * @str: the string to normalize
8486 *
8487 * Implements the normalizeWhiteSpace( s ) function from
8488 * section 6.2.9 of the spec
8489 *
8490 * Returns the new string or NULL in case of error.
8491 */
8492static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008493xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8494{
Daniel Veillardedc91922003-01-26 00:52:04 +00008495 xmlChar *ret, *p;
8496 const xmlChar *tmp;
8497 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008498
Daniel Veillardedc91922003-01-26 00:52:04 +00008499 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008500 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008501 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008502 while (*tmp != 0)
8503 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008504 len = tmp - str;
8505
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008506 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008507 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008508 xmlRngVErrMemory(ctxt, "validating\n");
8509 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008510 }
8511 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008512 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008513 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008514 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008515 if (IS_BLANK_CH(*str)) {
8516 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008517 str++;
8518 if (*str == 0)
8519 break;
8520 *p++ = ' ';
8521 } else
8522 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008523 }
8524 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008525 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008526}
8527
8528/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008529 * xmlRelaxNGValidateDatatype:
8530 * @ctxt: a Relax-NG validation context
8531 * @value: the string value
8532 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008533 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008534 *
8535 * Validate the given value against the dataype
8536 *
8537 * Returns 0 if the validation succeeded or an error code.
8538 */
8539static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008540xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8541 const xmlChar * value,
8542 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8543{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008544 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008545 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008546 void *result = NULL;
8547 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008548
8549 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008550 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008551 }
8552 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008553 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008554 if ((define->attrs != NULL) &&
8555 (define->attrs->type == XML_RELAXNG_PARAM)) {
8556 ret =
8557 lib->check(lib->data, define->name, value, &result, node);
8558 } else {
8559 ret = lib->check(lib->data, define->name, value, NULL, node);
8560 }
8561 } else
8562 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008563 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008564 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8565 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8566 lib->freef(lib->data, result);
8567 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008568 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008569 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008570 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008571 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008572 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008573 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8574 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008575 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008576 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008577 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008578 if (lib->facet != NULL) {
8579 tmp = lib->facet(lib->data, define->name, cur->name,
8580 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008581 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008582 ret = -1;
8583 }
8584 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008585 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008586 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008587 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008588
Daniel Veillard4c004142003-10-07 11:33:24 +00008589 oldvalue = ctxt->state->value;
8590 oldendvalue = ctxt->state->endvalue;
8591 ctxt->state->value = (xmlChar *) value;
8592 ctxt->state->endvalue = NULL;
8593 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8594 ctxt->state->value = (xmlChar *) oldvalue;
8595 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008596 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008597 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008598 lib->freef(lib->data, result);
8599 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008600}
8601
8602/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008603 * xmlRelaxNGNextValue:
8604 * @ctxt: a Relax-NG validation context
8605 *
8606 * Skip to the next value when validating within a list
8607 *
8608 * Returns 0 if the operation succeeded or an error code.
8609 */
8610static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008611xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8612{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008613 xmlChar *cur;
8614
8615 cur = ctxt->state->value;
8616 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008617 ctxt->state->value = NULL;
8618 ctxt->state->endvalue = NULL;
8619 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008620 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008621 while (*cur != 0)
8622 cur++;
8623 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8624 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008625 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008626 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008627 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008628 ctxt->state->value = cur;
8629 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008630}
8631
8632/**
8633 * xmlRelaxNGValidateValueList:
8634 * @ctxt: a Relax-NG validation context
8635 * @defines: the list of definitions to verify
8636 *
8637 * Validate the given set of definitions for the current value
8638 *
8639 * Returns 0 if the validation succeeded or an error code.
8640 */
8641static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008642xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8643 xmlRelaxNGDefinePtr defines)
8644{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008645 int ret = 0;
8646
8647 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008648 ret = xmlRelaxNGValidateValue(ctxt, defines);
8649 if (ret != 0)
8650 break;
8651 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008652 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008653 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008654}
8655
8656/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008657 * xmlRelaxNGValidateValue:
8658 * @ctxt: a Relax-NG validation context
8659 * @define: the definition to verify
8660 *
8661 * Validate the given definition 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 +00008666xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8667 xmlRelaxNGDefinePtr define)
8668{
Daniel Veillardedc91922003-01-26 00:52:04 +00008669 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008670 xmlChar *value;
8671
8672 value = ctxt->state->value;
8673 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008674 case XML_RELAXNG_EMPTY:{
8675 if ((value != NULL) && (value[0] != 0)) {
8676 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008677
William M. Brack76e95df2003-10-18 16:20:14 +00008678 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008679 idx++;
8680 if (value[idx] != 0)
8681 ret = -1;
8682 }
8683 break;
8684 }
8685 case XML_RELAXNG_TEXT:
8686 break;
8687 case XML_RELAXNG_VALUE:{
8688 if (!xmlStrEqual(value, define->value)) {
8689 if (define->name != NULL) {
8690 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008691
Daniel Veillard4c004142003-10-07 11:33:24 +00008692 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8693 if ((lib != NULL) && (lib->comp != NULL)) {
8694 ret = lib->comp(lib->data, define->name,
8695 define->value, define->node,
8696 (void *) define->attrs,
8697 value, ctxt->state->node);
8698 } else
8699 ret = -1;
8700 if (ret < 0) {
8701 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8702 define->name);
8703 return (-1);
8704 } else if (ret == 1) {
8705 ret = 0;
8706 } else {
8707 ret = -1;
8708 }
8709 } else {
8710 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008711
Daniel Veillard4c004142003-10-07 11:33:24 +00008712 /*
8713 * TODO: trivial optimizations are possible by
8714 * computing at compile-time
8715 */
8716 nval = xmlRelaxNGNormalize(ctxt, define->value);
8717 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008718
Daniel Veillard4c004142003-10-07 11:33:24 +00008719 if ((nval == NULL) || (nvalue == NULL) ||
8720 (!xmlStrEqual(nval, nvalue)))
8721 ret = -1;
8722 if (nval != NULL)
8723 xmlFree(nval);
8724 if (nvalue != NULL)
8725 xmlFree(nvalue);
8726 }
8727 }
8728 if (ret == 0)
8729 xmlRelaxNGNextValue(ctxt);
8730 break;
8731 }
8732 case XML_RELAXNG_DATATYPE:{
8733 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8734 ctxt->state->seq);
8735 if (ret == 0)
8736 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008737
Daniel Veillard4c004142003-10-07 11:33:24 +00008738 break;
8739 }
8740 case XML_RELAXNG_CHOICE:{
8741 xmlRelaxNGDefinePtr list = define->content;
8742 xmlChar *oldvalue;
8743
8744 oldflags = ctxt->flags;
8745 ctxt->flags |= FLAGS_IGNORABLE;
8746
8747 oldvalue = ctxt->state->value;
8748 while (list != NULL) {
8749 ret = xmlRelaxNGValidateValue(ctxt, list);
8750 if (ret == 0) {
8751 break;
8752 }
8753 ctxt->state->value = oldvalue;
8754 list = list->next;
8755 }
8756 ctxt->flags = oldflags;
8757 if (ret != 0) {
8758 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8759 xmlRelaxNGDumpValidError(ctxt);
8760 } else {
8761 if (ctxt->errNr > 0)
8762 xmlRelaxNGPopErrors(ctxt, 0);
8763 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008764 break;
8765 }
8766 case XML_RELAXNG_LIST:{
8767 xmlRelaxNGDefinePtr list = define->content;
8768 xmlChar *oldvalue, *oldend, *val, *cur;
8769
Daniel Veillard416589a2003-02-17 17:25:42 +00008770#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008771 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008772#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008773
Daniel Veillard4c004142003-10-07 11:33:24 +00008774 oldvalue = ctxt->state->value;
8775 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008776
Daniel Veillard4c004142003-10-07 11:33:24 +00008777 val = xmlStrdup(oldvalue);
8778 if (val == NULL) {
8779 val = xmlStrdup(BAD_CAST "");
8780 }
8781 if (val == NULL) {
8782 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8783 return (-1);
8784 }
8785 cur = val;
8786 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008787 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008788 *cur = 0;
8789 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008790#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008791 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008792#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008793 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008794 *cur++ = 0;
8795 } else
8796 cur++;
8797 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008798#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008799 xmlGenericError(xmlGenericErrorContext,
8800 "list value: '%s' found %d items\n",
8801 oldvalue, nb_values);
8802 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008803#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008804 ctxt->state->endvalue = cur;
8805 cur = val;
8806 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8807 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008808
Daniel Veillard4c004142003-10-07 11:33:24 +00008809 ctxt->state->value = cur;
8810
8811 while (list != NULL) {
8812 if (ctxt->state->value == ctxt->state->endvalue)
8813 ctxt->state->value = NULL;
8814 ret = xmlRelaxNGValidateValue(ctxt, list);
8815 if (ret != 0) {
8816#ifdef DEBUG_LIST
8817 xmlGenericError(xmlGenericErrorContext,
8818 "Failed to validate value: '%s' with %d rule\n",
8819 ctxt->state->value, nb_values);
8820#endif
8821 break;
8822 }
8823#ifdef DEBUG_LIST
8824 nb_values++;
8825#endif
8826 list = list->next;
8827 }
8828
8829 if ((ret == 0) && (ctxt->state->value != NULL) &&
8830 (ctxt->state->value != ctxt->state->endvalue)) {
8831 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8832 ctxt->state->value);
8833 ret = -1;
8834 }
8835 xmlFree(val);
8836 ctxt->state->value = oldvalue;
8837 ctxt->state->endvalue = oldend;
8838 break;
8839 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008840 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008841 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8842 if (ret != 0) {
8843 break;
8844 }
8845 /* no break on purpose */
8846 case XML_RELAXNG_ZEROORMORE:{
8847 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008848
Daniel Veillard4c004142003-10-07 11:33:24 +00008849 oldflags = ctxt->flags;
8850 ctxt->flags |= FLAGS_IGNORABLE;
8851 cur = ctxt->state->value;
8852 temp = NULL;
8853 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8854 (temp != cur)) {
8855 temp = cur;
8856 ret =
8857 xmlRelaxNGValidateValueList(ctxt, define->content);
8858 if (ret != 0) {
8859 ctxt->state->value = temp;
8860 ret = 0;
8861 break;
8862 }
8863 cur = ctxt->state->value;
8864 }
8865 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008866 if (ctxt->errNr > 0)
8867 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008868 break;
8869 }
8870 case XML_RELAXNG_EXCEPT:{
8871 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008872
Daniel Veillard4c004142003-10-07 11:33:24 +00008873 list = define->content;
8874 while (list != NULL) {
8875 ret = xmlRelaxNGValidateValue(ctxt, list);
8876 if (ret == 0) {
8877 ret = -1;
8878 break;
8879 } else
8880 ret = 0;
8881 list = list->next;
8882 }
8883 break;
8884 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008885 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008886 case XML_RELAXNG_GROUP:{
8887 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008888
Daniel Veillard4c004142003-10-07 11:33:24 +00008889 list = define->content;
8890 while (list != NULL) {
8891 ret = xmlRelaxNGValidateValue(ctxt, list);
8892 if (ret != 0) {
8893 ret = -1;
8894 break;
8895 } else
8896 ret = 0;
8897 list = list->next;
8898 }
8899 break;
8900 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008901 case XML_RELAXNG_REF:
8902 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008903 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008904 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8905 ret = -1;
8906 } else {
8907 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8908 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008909 break;
8910 default:
8911 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008912 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008913 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008914}
8915
8916/**
8917 * xmlRelaxNGValidateValueContent:
8918 * @ctxt: a Relax-NG validation context
8919 * @defines: the list of definitions to verify
8920 *
8921 * Validate the given definitions for the current value
8922 *
8923 * Returns 0 if the validation succeeded or an error code.
8924 */
8925static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008926xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8927 xmlRelaxNGDefinePtr defines)
8928{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008929 int ret = 0;
8930
8931 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008932 ret = xmlRelaxNGValidateValue(ctxt, defines);
8933 if (ret != 0)
8934 break;
8935 defines = defines->next;
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/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008941 * xmlRelaxNGAttributeMatch:
8942 * @ctxt: a Relax-NG validation context
8943 * @define: the definition to check
8944 * @prop: the attribute
8945 *
8946 * Check if the attribute matches the definition nameClass
8947 *
8948 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8949 */
8950static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008951xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8952 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8953{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008954 int ret;
8955
8956 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008957 if (!xmlStrEqual(define->name, prop->name))
8958 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008959 }
8960 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008961 if (define->ns[0] == 0) {
8962 if (prop->ns != NULL)
8963 return (0);
8964 } else {
8965 if ((prop->ns == NULL) ||
8966 (!xmlStrEqual(define->ns, prop->ns->href)))
8967 return (0);
8968 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008969 }
8970 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008971 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008972 define = define->nameClass;
8973 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008974 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008975
Daniel Veillard4c004142003-10-07 11:33:24 +00008976 list = define->content;
8977 while (list != NULL) {
8978 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8979 if (ret == 1)
8980 return (0);
8981 if (ret < 0)
8982 return (ret);
8983 list = list->next;
8984 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008985 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008986 TODO}
8987 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008988}
8989
8990/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008991 * xmlRelaxNGValidateAttribute:
8992 * @ctxt: a Relax-NG validation context
8993 * @define: the definition to verify
8994 *
8995 * Validate the given attribute definition for that node
8996 *
8997 * Returns 0 if the validation succeeded or an error code.
8998 */
8999static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009000xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9001 xmlRelaxNGDefinePtr define)
9002{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009003 int ret = 0, i;
9004 xmlChar *value, *oldvalue;
9005 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009006 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009007
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009008 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009009 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009010 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009011 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9012 tmp = ctxt->state->attrs[i];
9013 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9014 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9015 (tmp->ns == NULL)) ||
9016 ((tmp->ns != NULL) &&
9017 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9018 prop = tmp;
9019 break;
9020 }
9021 }
9022 }
9023 if (prop != NULL) {
9024 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9025 oldvalue = ctxt->state->value;
9026 oldseq = ctxt->state->seq;
9027 ctxt->state->seq = (xmlNodePtr) prop;
9028 ctxt->state->value = value;
9029 ctxt->state->endvalue = NULL;
9030 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9031 if (ctxt->state->value != NULL)
9032 value = ctxt->state->value;
9033 if (value != NULL)
9034 xmlFree(value);
9035 ctxt->state->value = oldvalue;
9036 ctxt->state->seq = oldseq;
9037 if (ret == 0) {
9038 /*
9039 * flag the attribute as processed
9040 */
9041 ctxt->state->attrs[i] = NULL;
9042 ctxt->state->nbAttrLeft--;
9043 }
9044 } else {
9045 ret = -1;
9046 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009047#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009048 xmlGenericError(xmlGenericErrorContext,
9049 "xmlRelaxNGValidateAttribute(%s): %d\n",
9050 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009051#endif
9052 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009053 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9054 tmp = ctxt->state->attrs[i];
9055 if ((tmp != NULL) &&
9056 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9057 prop = tmp;
9058 break;
9059 }
9060 }
9061 if (prop != NULL) {
9062 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9063 oldvalue = ctxt->state->value;
9064 oldseq = ctxt->state->seq;
9065 ctxt->state->seq = (xmlNodePtr) prop;
9066 ctxt->state->value = value;
9067 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9068 if (ctxt->state->value != NULL)
9069 value = ctxt->state->value;
9070 if (value != NULL)
9071 xmlFree(value);
9072 ctxt->state->value = oldvalue;
9073 ctxt->state->seq = oldseq;
9074 if (ret == 0) {
9075 /*
9076 * flag the attribute as processed
9077 */
9078 ctxt->state->attrs[i] = NULL;
9079 ctxt->state->nbAttrLeft--;
9080 }
9081 } else {
9082 ret = -1;
9083 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009084#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009085 if (define->ns != NULL) {
9086 xmlGenericError(xmlGenericErrorContext,
9087 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9088 define->ns, ret);
9089 } else {
9090 xmlGenericError(xmlGenericErrorContext,
9091 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9092 ret);
9093 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009094#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009095 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009096
9097 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009098}
9099
9100/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009101 * xmlRelaxNGValidateAttributeList:
9102 * @ctxt: a Relax-NG validation context
9103 * @define: the list of definition to verify
9104 *
9105 * Validate the given node against the list of attribute definitions
9106 *
9107 * Returns 0 if the validation succeeded or an error code.
9108 */
9109static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009110xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9111 xmlRelaxNGDefinePtr defines)
9112{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009113 int ret = 0, res;
9114 int needmore = 0;
9115 xmlRelaxNGDefinePtr cur;
9116
9117 cur = defines;
9118 while (cur != NULL) {
9119 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009120 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9121 ret = -1;
9122 } else
9123 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009124 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009125 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009126 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009127 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009128 cur = defines;
9129 while (cur != NULL) {
9130 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009131 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9132 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9133 if (res < 0)
9134 ret = -1;
9135 } else {
9136 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9137 return (-1);
9138 }
9139 if (res == -1) /* continues on -2 */
9140 break;
9141 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009142 cur = cur->next;
9143 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009144
9145 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009146}
9147
9148/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009149 * xmlRelaxNGNodeMatchesList:
9150 * @node: the node
9151 * @list: a NULL terminated array of definitions
9152 *
9153 * Check if a node can be matched by one of the definitions
9154 *
9155 * Returns 1 if matches 0 otherwise
9156 */
9157static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009158xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9159{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009160 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009161 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009162
9163 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009164 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009165
9166 cur = list[i++];
9167 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009168 if ((node->type == XML_ELEMENT_NODE) &&
9169 (cur->type == XML_RELAXNG_ELEMENT)) {
9170 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9171 if (tmp == 1)
9172 return (1);
9173 } else if (((node->type == XML_TEXT_NODE) ||
9174 (node->type == XML_CDATA_SECTION_NODE)) &&
9175 (cur->type == XML_RELAXNG_TEXT)) {
9176 return (1);
9177 }
9178 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009179 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009180 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009181}
9182
9183/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009184 * xmlRelaxNGValidateInterleave:
9185 * @ctxt: a Relax-NG validation context
9186 * @define: the definition to verify
9187 *
9188 * Validate an interleave definition for a node.
9189 *
9190 * Returns 0 if the validation succeeded or an error code.
9191 */
9192static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009193xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9194 xmlRelaxNGDefinePtr define)
9195{
William M. Brack779af002003-08-01 15:55:39 +00009196 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009197 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009198 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009199
9200 xmlRelaxNGValidStatePtr oldstate;
9201 xmlRelaxNGPartitionPtr partitions;
9202 xmlRelaxNGInterleaveGroupPtr group = NULL;
9203 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9204 xmlNodePtr *list = NULL, *lasts = NULL;
9205
9206 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009207 partitions = (xmlRelaxNGPartitionPtr) define->data;
9208 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009209 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009210 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9211 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009212 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009213 /*
9214 * Optimizations for MIXED
9215 */
9216 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009217 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009218 ctxt->flags |= FLAGS_MIXED_CONTENT;
9219 if (nbgroups == 2) {
9220 /*
9221 * this is a pure <mixed> case
9222 */
9223 if (ctxt->state != NULL)
9224 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9225 ctxt->state->seq);
9226 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9227 ret = xmlRelaxNGValidateDefinition(ctxt,
9228 partitions->groups[1]->
9229 rule);
9230 else
9231 ret = xmlRelaxNGValidateDefinition(ctxt,
9232 partitions->groups[0]->
9233 rule);
9234 if (ret == 0) {
9235 if (ctxt->state != NULL)
9236 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9237 ctxt->state->
9238 seq);
9239 }
9240 ctxt->flags = oldflags;
9241 return (ret);
9242 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009243 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009244
9245 /*
9246 * Build arrays to store the first and last node of the chain
9247 * pertaining to each group
9248 */
9249 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9250 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009251 xmlRngVErrMemory(ctxt, "validating\n");
9252 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009253 }
9254 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9255 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9256 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009257 xmlRngVErrMemory(ctxt, "validating\n");
9258 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009259 }
9260 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9261
9262 /*
9263 * Walk the sequence of children finding the right group and
9264 * sorting them in sequences.
9265 */
9266 cur = ctxt->state->seq;
9267 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9268 start = cur;
9269 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009270 ctxt->state->seq = cur;
9271 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009272 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009273 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009274
Daniel Veillard4c004142003-10-07 11:33:24 +00009275 if ((cur->type == XML_TEXT_NODE) ||
9276 (cur->type == XML_CDATA_SECTION_NODE)) {
9277 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9278 NULL);
9279 } else if (cur->type == XML_ELEMENT_NODE) {
9280 if (cur->ns != NULL) {
9281 tmp = xmlHashLookup2(partitions->triage, cur->name,
9282 cur->ns->href);
9283 if (tmp == NULL)
9284 tmp = xmlHashLookup2(partitions->triage,
9285 BAD_CAST "#any",
9286 cur->ns->href);
9287 } else
9288 tmp =
9289 xmlHashLookup2(partitions->triage, cur->name,
9290 NULL);
9291 if (tmp == NULL)
9292 tmp =
9293 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9294 NULL);
9295 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009296
Daniel Veillard4c004142003-10-07 11:33:24 +00009297 if (tmp == NULL) {
9298 i = nbgroups;
9299 } else {
9300 i = ((long) tmp) - 1;
9301 if (partitions->flags & IS_NEEDCHECK) {
9302 group = partitions->groups[i];
9303 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9304 i = nbgroups;
9305 }
9306 }
9307 } else {
9308 for (i = 0; i < nbgroups; i++) {
9309 group = partitions->groups[i];
9310 if (group == NULL)
9311 continue;
9312 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9313 break;
9314 }
9315 }
9316 /*
9317 * We break as soon as an element not matched is found
9318 */
9319 if (i >= nbgroups) {
9320 break;
9321 }
9322 if (lasts[i] != NULL) {
9323 lasts[i]->next = cur;
9324 lasts[i] = cur;
9325 } else {
9326 list[i] = cur;
9327 lasts[i] = cur;
9328 }
9329 if (cur->next != NULL)
9330 lastchg = cur->next;
9331 else
9332 lastchg = cur;
9333 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009334 }
9335 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009336 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9337 ret = -1;
9338 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009339 }
9340 lastelem = cur;
9341 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009342 for (i = 0; i < nbgroups; i++) {
9343 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9344 group = partitions->groups[i];
9345 if (lasts[i] != NULL) {
9346 last = lasts[i]->next;
9347 lasts[i]->next = NULL;
9348 }
9349 ctxt->state->seq = list[i];
9350 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9351 if (ret != 0)
9352 break;
9353 if (ctxt->state != NULL) {
9354 cur = ctxt->state->seq;
9355 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9356 xmlRelaxNGFreeValidState(ctxt, oldstate);
9357 oldstate = ctxt->state;
9358 ctxt->state = NULL;
9359 if (cur != NULL) {
9360 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9361 ret = -1;
9362 ctxt->state = oldstate;
9363 goto done;
9364 }
9365 } else if (ctxt->states != NULL) {
9366 int j;
9367 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009368 int best = -1;
9369 int lowattr = -1;
9370
9371 /*
9372 * PBM: what happen if there is attributes checks in the interleaves
9373 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009374
Daniel Veillard4c004142003-10-07 11:33:24 +00009375 for (j = 0; j < ctxt->states->nbState; j++) {
9376 cur = ctxt->states->tabState[j]->seq;
9377 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9378 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009379 if (found == 0) {
9380 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9381 best = j;
9382 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009383 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009384 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9385 /* try to keep the latest one to mach old heuristic */
9386 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9387 best = j;
9388 }
9389 if (lowattr == 0)
9390 break;
9391 } else if (found == 0) {
9392 if (lowattr == -1) {
9393 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9394 best = j;
9395 } else
9396 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9397 /* try to keep the latest one to mach old heuristic */
9398 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9399 best = j;
9400 }
9401 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009402 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009403 /*
9404 * BIG PBM: here we pick only one restarting point :-(
9405 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009406 if (ctxt->states->nbState > 0) {
9407 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009408 if (best != -1) {
9409 oldstate = ctxt->states->tabState[best];
9410 ctxt->states->tabState[best] = NULL;
9411 } else {
9412 oldstate =
9413 ctxt->states->tabState[ctxt->states->nbState - 1];
9414 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009415 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009416 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009417 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009418 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009419 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9420 }
9421 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9422 ctxt->states = NULL;
9423 if (found == 0) {
9424 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9425 ret = -1;
9426 ctxt->state = oldstate;
9427 goto done;
9428 }
9429 } else {
9430 ret = -1;
9431 break;
9432 }
9433 if (lasts[i] != NULL) {
9434 lasts[i]->next = last;
9435 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009436 }
9437 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009438 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009439 ctxt->state = oldstate;
9440 ctxt->state->seq = lastelem;
9441 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009442 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9443 ret = -1;
9444 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009445 }
9446
Daniel Veillard4c004142003-10-07 11:33:24 +00009447 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009448 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009449 /*
9450 * builds the next links chain from the prev one
9451 */
9452 cur = lastchg;
9453 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009454 if ((cur == start) || (cur->prev == NULL))
9455 break;
9456 cur->prev->next = cur;
9457 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009458 }
9459 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009460 if (ctxt->errNr > errNr)
9461 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009462 }
9463
9464 xmlFree(list);
9465 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009466 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009467}
9468
9469/**
9470 * xmlRelaxNGValidateDefinitionList:
9471 * @ctxt: a Relax-NG validation context
9472 * @define: the list of definition to verify
9473 *
9474 * Validate the given node content against the (list) of definitions
9475 *
9476 * Returns 0 if the validation succeeded or an error code.
9477 */
9478static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009479xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9480 xmlRelaxNGDefinePtr defines)
9481{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009482 int ret = 0, res;
9483
9484
Daniel Veillard952379b2003-03-17 15:37:12 +00009485 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009486 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9487 BAD_CAST "NULL definition list");
9488 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009489 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009490 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009491 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9492 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9493 if (res < 0)
9494 ret = -1;
9495 } else {
9496 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9497 return (-1);
9498 }
9499 if (res == -1) /* continues on -2 */
9500 break;
9501 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009502 }
9503
Daniel Veillard4c004142003-10-07 11:33:24 +00009504 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009505}
9506
9507/**
9508 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009509 * @ctxt: a Relax-NG validation context
9510 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009511 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009512 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009513 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009514 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009515 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009516 */
9517static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009518xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9519 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9520{
Daniel Veillard580ced82003-03-21 21:22:48 +00009521 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009522
Daniel Veillardfd573f12003-03-16 17:52:32 +00009523 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009524 if (!xmlStrEqual(elem->name, define->name)) {
9525 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9526 return (0);
9527 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009528 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009529 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009530 if (elem->ns == NULL) {
9531 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9532 return (0);
9533 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9534 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9535 elem->name, define->ns);
9536 return (0);
9537 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009538 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009539 (define->name == NULL)) {
9540 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9541 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009542 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009543 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9544 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009545 }
9546
9547 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009548 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009549
9550 define = define->nameClass;
9551 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009552 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009553
Daniel Veillard4c004142003-10-07 11:33:24 +00009554 if (ctxt != NULL) {
9555 oldflags = ctxt->flags;
9556 ctxt->flags |= FLAGS_IGNORABLE;
9557 }
9558
9559 list = define->content;
9560 while (list != NULL) {
9561 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9562 if (ret == 1) {
9563 if (ctxt != NULL)
9564 ctxt->flags = oldflags;
9565 return (0);
9566 }
9567 if (ret < 0) {
9568 if (ctxt != NULL)
9569 ctxt->flags = oldflags;
9570 return (ret);
9571 }
9572 list = list->next;
9573 }
9574 ret = 1;
9575 if (ctxt != NULL) {
9576 ctxt->flags = oldflags;
9577 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009578 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009579 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009580
Daniel Veillard4c004142003-10-07 11:33:24 +00009581 if (ctxt != NULL) {
9582 oldflags = ctxt->flags;
9583 ctxt->flags |= FLAGS_IGNORABLE;
9584 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009585
Daniel Veillard4c004142003-10-07 11:33:24 +00009586 list = define->nameClass;
9587 while (list != NULL) {
9588 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9589 if (ret == 1) {
9590 if (ctxt != NULL)
9591 ctxt->flags = oldflags;
9592 return (1);
9593 }
9594 if (ret < 0) {
9595 if (ctxt != NULL)
9596 ctxt->flags = oldflags;
9597 return (ret);
9598 }
9599 list = list->next;
9600 }
9601 if (ctxt != NULL) {
9602 if (ret != 0) {
9603 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9604 xmlRelaxNGDumpValidError(ctxt);
9605 } else {
9606 if (ctxt->errNr > 0)
9607 xmlRelaxNGPopErrors(ctxt, 0);
9608 }
9609 }
9610 ret = 0;
9611 if (ctxt != NULL) {
9612 ctxt->flags = oldflags;
9613 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009614 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009615 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009616 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009617 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009618}
9619
9620/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009621 * xmlRelaxNGBestState:
9622 * @ctxt: a Relax-NG validation context
9623 *
9624 * Find the "best" state in the ctxt->states list of states to report
9625 * errors about. I.e. a state with no element left in the child list
9626 * or the one with the less attributes left.
9627 * This is called only if a falidation error was detected
9628 *
9629 * Returns the index of the "best" state or -1 in case of error
9630 */
9631static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009632xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9633{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009634 xmlRelaxNGValidStatePtr state;
9635 int i, tmp;
9636 int best = -1;
9637 int value = 1000000;
9638
9639 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9640 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009641 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009642
Daniel Veillard4c004142003-10-07 11:33:24 +00009643 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009644 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009645 if (state == NULL)
9646 continue;
9647 if (state->seq != NULL) {
9648 if ((best == -1) || (value > 100000)) {
9649 value = 100000;
9650 best = i;
9651 }
9652 } else {
9653 tmp = state->nbAttrLeft;
9654 if ((best == -1) || (value > tmp)) {
9655 value = tmp;
9656 best = i;
9657 }
9658 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009659 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009660 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009661}
9662
9663/**
9664 * xmlRelaxNGLogBestError:
9665 * @ctxt: a Relax-NG validation context
9666 *
9667 * Find the "best" state in the ctxt->states list of states to report
9668 * errors about and log it.
9669 */
9670static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009671xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9672{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009673 int best;
9674
9675 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9676 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009677 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009678
9679 best = xmlRelaxNGBestState(ctxt);
9680 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009681 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009682
Daniel Veillard4c004142003-10-07 11:33:24 +00009683 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009684 }
9685}
9686
9687/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009688 * xmlRelaxNGValidateElementEnd:
9689 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009690 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009691 *
9692 * Validate the end of the element, implements check that
9693 * there is nothing left not consumed in the element content
9694 * or in the attribute list.
9695 *
9696 * Returns 0 if the validation succeeded or an error code.
9697 */
9698static int
William M. Brack272693c2003-11-14 16:20:34 +00009699xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009700{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009701 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009702 xmlRelaxNGValidStatePtr state;
9703
9704 state = ctxt->state;
9705 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009706 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9707 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009708 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009709 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9710 state->node->name, state->seq->name);
9711 }
9712 return (-1);
9713 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009714 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009715 for (i = 0; i < state->nbAttrs; i++) {
9716 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009717 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009718 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9719 state->attrs[i]->name, state->node->name);
9720 }
9721 return (-1 - i);
9722 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009723 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009724 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009725}
9726
9727/**
9728 * xmlRelaxNGValidateState:
9729 * @ctxt: a Relax-NG validation context
9730 * @define: the definition to verify
9731 *
9732 * Validate the current state against the definition
9733 *
9734 * Returns 0 if the validation succeeded or an error code.
9735 */
9736static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009737xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9738 xmlRelaxNGDefinePtr define)
9739{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009740 xmlNodePtr node;
9741 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009742 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009743
9744 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009745 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9746 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009747 }
9748
9749 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009750 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009751 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009752 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009753 }
9754#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009755 for (i = 0; i < ctxt->depth; i++)
9756 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009757 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009758 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009759 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009760 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009761 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009762 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009763 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009764 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009765#endif
9766 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009767 switch (define->type) {
9768 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009769 node = xmlRelaxNGSkipIgnored(ctxt, node);
9770 ret = 0;
9771 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009772 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009773 ret = -1;
9774 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009775 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009776 while ((node != NULL) &&
9777 ((node->type == XML_TEXT_NODE) ||
9778 (node->type == XML_COMMENT_NODE) ||
9779 (node->type == XML_PI_NODE) ||
9780 (node->type == XML_CDATA_SECTION_NODE)))
9781 node = node->next;
9782 ctxt->state->seq = node;
9783 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009784 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009785 errNr = ctxt->errNr;
9786 node = xmlRelaxNGSkipIgnored(ctxt, node);
9787 if (node == NULL) {
9788 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9789 ret = -1;
9790 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9791 xmlRelaxNGDumpValidError(ctxt);
9792 break;
9793 }
9794 if (node->type != XML_ELEMENT_NODE) {
9795 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9796 ret = -1;
9797 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9798 xmlRelaxNGDumpValidError(ctxt);
9799 break;
9800 }
9801 /*
9802 * This node was already validated successfully against
9803 * this definition.
9804 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009805 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009806 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9807 if (ctxt->errNr > errNr)
9808 xmlRelaxNGPopErrors(ctxt, errNr);
9809 if (ctxt->errNr != 0) {
9810 while ((ctxt->err != NULL) &&
9811 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9812 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9813 ||
9814 ((ctxt->err->err ==
9815 XML_RELAXNG_ERR_ELEMEXTRANS)
9816 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9817 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9818 || (ctxt->err->err ==
9819 XML_RELAXNG_ERR_NOTELEM)))
9820 xmlRelaxNGValidErrorPop(ctxt);
9821 }
9822 break;
9823 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009824
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009825 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9826 if (ret <= 0) {
9827 ret = -1;
9828 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9829 xmlRelaxNGDumpValidError(ctxt);
9830 break;
9831 }
9832 ret = 0;
9833 if (ctxt->errNr != 0) {
9834 if (ctxt->errNr > errNr)
9835 xmlRelaxNGPopErrors(ctxt, errNr);
9836 while ((ctxt->err != NULL) &&
9837 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9838 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9839 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9840 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9841 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9842 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9843 xmlRelaxNGValidErrorPop(ctxt);
9844 }
9845 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009846
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009847 oldflags = ctxt->flags;
9848 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9849 ctxt->flags -= FLAGS_MIXED_CONTENT;
9850 }
9851 state = xmlRelaxNGNewValidState(ctxt, node);
9852 if (state == NULL) {
9853 ret = -1;
9854 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9855 xmlRelaxNGDumpValidError(ctxt);
9856 break;
9857 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009858
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009859 oldstate = ctxt->state;
9860 ctxt->state = state;
9861 if (define->attrs != NULL) {
9862 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9863 if (tmp != 0) {
9864 ret = -1;
9865 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9866 }
9867 }
9868 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009869 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9870 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9871 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009872
Daniel Veillard4c004142003-10-07 11:33:24 +00009873 nstate = xmlRelaxNGNewValidState(ctxt, node);
9874 ctxt->state = nstate;
9875 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009876
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009877 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9878 define->contModel,
9879 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009880 nseq = ctxt->state->seq;
9881 ctxt->state = tmpstate;
9882 ctxt->states = tmpstates;
9883 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009884
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009885#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009886 xmlGenericError(xmlGenericErrorContext,
9887 "Validating content of '%s' : %d\n",
9888 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009889#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009890 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009891 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009892
9893 if (ctxt->states != NULL) {
9894 tmp = -1;
9895
Daniel Veillardce192eb2003-04-16 15:58:05 +00009896 for (i = 0; i < ctxt->states->nbState; i++) {
9897 state = ctxt->states->tabState[i];
9898 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009899 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009900
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009901 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009902 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009903 break;
9904 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009905 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009906 if (tmp != 0) {
9907 /*
9908 * validation error, log the message for the "best" one
9909 */
9910 ctxt->flags |= FLAGS_IGNORABLE;
9911 xmlRelaxNGLogBestError(ctxt);
9912 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009913 for (i = 0; i < ctxt->states->nbState; i++) {
9914 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009915 ctxt->states->
9916 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009917 }
9918 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9919 ctxt->flags = oldflags;
9920 ctxt->states = NULL;
9921 if ((ret == 0) && (tmp == -1))
9922 ret = -1;
9923 } else {
9924 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +00009925 if (ctxt->state != NULL)
9926 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009927 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009928 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009929 xmlRelaxNGFreeValidState(ctxt, state);
9930 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009931 } else {
9932 if (define->content != NULL) {
9933 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009934 define->
9935 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009936 if (tmp != 0) {
9937 ret = -1;
9938 if (ctxt->state == NULL) {
9939 ctxt->state = oldstate;
9940 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9941 node->name);
9942 ctxt->state = NULL;
9943 } else {
9944 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9945 node->name);
9946 }
9947
9948 }
9949 }
9950 if (ctxt->states != NULL) {
9951 tmp = -1;
9952
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009953 for (i = 0; i < ctxt->states->nbState; i++) {
9954 state = ctxt->states->tabState[i];
9955 ctxt->state = state;
9956
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009957 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009958 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009959 break;
9960 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009961 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009962 if (tmp != 0) {
9963 /*
9964 * validation error, log the message for the "best" one
9965 */
9966 ctxt->flags |= FLAGS_IGNORABLE;
9967 xmlRelaxNGLogBestError(ctxt);
9968 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009969 for (i = 0; i < ctxt->states->nbState; i++) {
9970 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009971 ctxt->states->tabState[i]);
9972 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009973 }
9974 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9975 ctxt->flags = oldflags;
9976 ctxt->states = NULL;
9977 if ((ret == 0) && (tmp == -1))
9978 ret = -1;
9979 } else {
9980 state = ctxt->state;
9981 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009982 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009983 xmlRelaxNGFreeValidState(ctxt, state);
9984 }
9985 }
9986 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +00009987 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009988 }
9989 ctxt->flags = oldflags;
9990 ctxt->state = oldstate;
9991 if (oldstate != NULL)
9992 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9993 if (ret != 0) {
9994 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9995 xmlRelaxNGDumpValidError(ctxt);
9996 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +00009997#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009998 } else {
9999 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010000#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010001 }
10002 } else {
10003 if (ctxt->errNr > errNr)
10004 xmlRelaxNGPopErrors(ctxt, errNr);
10005 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010006
10007#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010008 xmlGenericError(xmlGenericErrorContext,
10009 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10010 node->name, ret);
10011 if (oldstate == NULL)
10012 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10013 else if (oldstate->seq == NULL)
10014 xmlGenericError(xmlGenericErrorContext, ": done\n");
10015 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10016 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10017 oldstate->seq->name);
10018 else
10019 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10020 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010021#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010022 break;
10023 case XML_RELAXNG_OPTIONAL:{
10024 errNr = ctxt->errNr;
10025 oldflags = ctxt->flags;
10026 ctxt->flags |= FLAGS_IGNORABLE;
10027 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10028 ret =
10029 xmlRelaxNGValidateDefinitionList(ctxt,
10030 define->content);
10031 if (ret != 0) {
10032 if (ctxt->state != NULL)
10033 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10034 ctxt->state = oldstate;
10035 ctxt->flags = oldflags;
10036 ret = 0;
10037 if (ctxt->errNr > errNr)
10038 xmlRelaxNGPopErrors(ctxt, errNr);
10039 break;
10040 }
10041 if (ctxt->states != NULL) {
10042 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10043 } else {
10044 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10045 if (ctxt->states == NULL) {
10046 xmlRelaxNGFreeValidState(ctxt, oldstate);
10047 ctxt->flags = oldflags;
10048 ret = -1;
10049 if (ctxt->errNr > errNr)
10050 xmlRelaxNGPopErrors(ctxt, errNr);
10051 break;
10052 }
10053 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10054 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10055 ctxt->state = NULL;
10056 }
10057 ctxt->flags = oldflags;
10058 ret = 0;
10059 if (ctxt->errNr > errNr)
10060 xmlRelaxNGPopErrors(ctxt, errNr);
10061 break;
10062 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010063 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010064 errNr = ctxt->errNr;
10065 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10066 if (ret != 0) {
10067 break;
10068 }
10069 if (ctxt->errNr > errNr)
10070 xmlRelaxNGPopErrors(ctxt, errNr);
10071 /* no break on purpose */
10072 case XML_RELAXNG_ZEROORMORE:{
10073 int progress;
10074 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10075 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010076
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010077 errNr = ctxt->errNr;
10078 res = xmlRelaxNGNewStates(ctxt, 1);
10079 if (res == NULL) {
10080 ret = -1;
10081 break;
10082 }
10083 /*
10084 * All the input states are also exit states
10085 */
10086 if (ctxt->state != NULL) {
10087 xmlRelaxNGAddStates(ctxt, res,
10088 xmlRelaxNGCopyValidState(ctxt,
10089 ctxt->
10090 state));
10091 } else {
10092 for (j = 0; j < ctxt->states->nbState; j++) {
10093 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010094 xmlRelaxNGCopyValidState(ctxt,
10095 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010096 }
10097 }
10098 oldflags = ctxt->flags;
10099 ctxt->flags |= FLAGS_IGNORABLE;
10100 do {
10101 progress = 0;
10102 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010103
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010104 if (ctxt->states != NULL) {
10105 states = ctxt->states;
10106 for (i = 0; i < states->nbState; i++) {
10107 ctxt->state = states->tabState[i];
10108 ctxt->states = NULL;
10109 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10110 define->
10111 content);
10112 if (ret == 0) {
10113 if (ctxt->state != NULL) {
10114 tmp = xmlRelaxNGAddStates(ctxt, res,
10115 ctxt->state);
10116 ctxt->state = NULL;
10117 if (tmp == 1)
10118 progress = 1;
10119 } else if (ctxt->states != NULL) {
10120 for (j = 0; j < ctxt->states->nbState;
10121 j++) {
10122 tmp =
10123 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010124 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010125 if (tmp == 1)
10126 progress = 1;
10127 }
10128 xmlRelaxNGFreeStates(ctxt,
10129 ctxt->states);
10130 ctxt->states = NULL;
10131 }
10132 } else {
10133 if (ctxt->state != NULL) {
10134 xmlRelaxNGFreeValidState(ctxt,
10135 ctxt->state);
10136 ctxt->state = NULL;
10137 }
10138 }
10139 }
10140 } else {
10141 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10142 define->
10143 content);
10144 if (ret != 0) {
10145 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10146 ctxt->state = NULL;
10147 } else {
10148 base = res->nbState;
10149 if (ctxt->state != NULL) {
10150 tmp = xmlRelaxNGAddStates(ctxt, res,
10151 ctxt->state);
10152 ctxt->state = NULL;
10153 if (tmp == 1)
10154 progress = 1;
10155 } else if (ctxt->states != NULL) {
10156 for (j = 0; j < ctxt->states->nbState; j++) {
10157 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010158 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010159 if (tmp == 1)
10160 progress = 1;
10161 }
10162 if (states == NULL) {
10163 states = ctxt->states;
10164 } else {
10165 xmlRelaxNGFreeStates(ctxt,
10166 ctxt->states);
10167 }
10168 ctxt->states = NULL;
10169 }
10170 }
10171 }
10172 if (progress) {
10173 /*
10174 * Collect all the new nodes added at that step
10175 * and make them the new node set
10176 */
10177 if (res->nbState - base == 1) {
10178 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10179 res->
10180 tabState
10181 [base]);
10182 } else {
10183 if (states == NULL) {
10184 xmlRelaxNGNewStates(ctxt,
10185 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010186 states = ctxt->states;
10187 if (states == NULL) {
10188 progress = 0;
10189 break;
10190 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010191 }
10192 states->nbState = 0;
10193 for (i = base; i < res->nbState; i++)
10194 xmlRelaxNGAddStates(ctxt, states,
10195 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010196 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010197 ctxt->states = states;
10198 }
10199 }
10200 } while (progress == 1);
10201 if (states != NULL) {
10202 xmlRelaxNGFreeStates(ctxt, states);
10203 }
10204 ctxt->states = res;
10205 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010206#if 0
10207 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010208 * errors may have to be propagated back...
10209 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010210 if (ctxt->errNr > errNr)
10211 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010212#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010213 ret = 0;
10214 break;
10215 }
10216 case XML_RELAXNG_CHOICE:{
10217 xmlRelaxNGDefinePtr list = NULL;
10218 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010219
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010220 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010221
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010222 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010223 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10224 (node != NULL)) {
10225 /*
10226 * node == NULL can't be optimized since IS_TRIABLE
10227 * doesn't account for choice which may lead to
10228 * only attributes.
10229 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010230 xmlHashTablePtr triage =
10231 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010232
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010233 /*
10234 * Something we can optimize cleanly there is only one
10235 * possble branch out !
10236 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010237 if ((node->type == XML_TEXT_NODE) ||
10238 (node->type == XML_CDATA_SECTION_NODE)) {
10239 list =
10240 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10241 } else if (node->type == XML_ELEMENT_NODE) {
10242 if (node->ns != NULL) {
10243 list = xmlHashLookup2(triage, node->name,
10244 node->ns->href);
10245 if (list == NULL)
10246 list =
10247 xmlHashLookup2(triage, BAD_CAST "#any",
10248 node->ns->href);
10249 } else
10250 list =
10251 xmlHashLookup2(triage, node->name, NULL);
10252 if (list == NULL)
10253 list =
10254 xmlHashLookup2(triage, BAD_CAST "#any",
10255 NULL);
10256 }
10257 if (list == NULL) {
10258 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010259 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010260 break;
10261 }
10262 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10263 if (ret == 0) {
10264 }
10265 break;
10266 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010267
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010268 list = define->content;
10269 oldflags = ctxt->flags;
10270 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010271
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010272 while (list != NULL) {
10273 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10274 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10275 if (ret == 0) {
10276 if (states == NULL) {
10277 states = xmlRelaxNGNewStates(ctxt, 1);
10278 }
10279 if (ctxt->state != NULL) {
10280 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10281 } else if (ctxt->states != NULL) {
10282 for (i = 0; i < ctxt->states->nbState; i++) {
10283 xmlRelaxNGAddStates(ctxt, states,
10284 ctxt->states->
10285 tabState[i]);
10286 }
10287 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10288 ctxt->states = NULL;
10289 }
10290 } else {
10291 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10292 }
10293 ctxt->state = oldstate;
10294 list = list->next;
10295 }
10296 if (states != NULL) {
10297 xmlRelaxNGFreeValidState(ctxt, oldstate);
10298 ctxt->states = states;
10299 ctxt->state = NULL;
10300 ret = 0;
10301 } else {
10302 ctxt->states = NULL;
10303 }
10304 ctxt->flags = oldflags;
10305 if (ret != 0) {
10306 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10307 xmlRelaxNGDumpValidError(ctxt);
10308 }
10309 } else {
10310 if (ctxt->errNr > errNr)
10311 xmlRelaxNGPopErrors(ctxt, errNr);
10312 }
10313 break;
10314 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010315 case XML_RELAXNG_DEF:
10316 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010317 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10318 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010319 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010320 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10321 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010322 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010323 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10324 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010325 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010326 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010327 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010328 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010329 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010330 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10331 break;
10332 case XML_RELAXNG_DATATYPE:{
10333 xmlNodePtr child;
10334 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010335
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010336 child = node;
10337 while (child != NULL) {
10338 if (child->type == XML_ELEMENT_NODE) {
10339 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10340 node->parent->name);
10341 ret = -1;
10342 break;
10343 } else if ((child->type == XML_TEXT_NODE) ||
10344 (child->type == XML_CDATA_SECTION_NODE)) {
10345 content = xmlStrcat(content, child->content);
10346 }
10347 /* TODO: handle entities ... */
10348 child = child->next;
10349 }
10350 if (ret == -1) {
10351 if (content != NULL)
10352 xmlFree(content);
10353 break;
10354 }
10355 if (content == NULL) {
10356 content = xmlStrdup(BAD_CAST "");
10357 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010358 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010359 ret = -1;
10360 break;
10361 }
10362 }
10363 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10364 ctxt->state->seq);
10365 if (ret == -1) {
10366 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10367 } else if (ret == 0) {
10368 ctxt->state->seq = NULL;
10369 }
10370 if (content != NULL)
10371 xmlFree(content);
10372 break;
10373 }
10374 case XML_RELAXNG_VALUE:{
10375 xmlChar *content = NULL;
10376 xmlChar *oldvalue;
10377 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010378
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010379 child = node;
10380 while (child != NULL) {
10381 if (child->type == XML_ELEMENT_NODE) {
10382 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10383 node->parent->name);
10384 ret = -1;
10385 break;
10386 } else if ((child->type == XML_TEXT_NODE) ||
10387 (child->type == XML_CDATA_SECTION_NODE)) {
10388 content = xmlStrcat(content, child->content);
10389 }
10390 /* TODO: handle entities ... */
10391 child = child->next;
10392 }
10393 if (ret == -1) {
10394 if (content != NULL)
10395 xmlFree(content);
10396 break;
10397 }
10398 if (content == NULL) {
10399 content = xmlStrdup(BAD_CAST "");
10400 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010401 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010402 ret = -1;
10403 break;
10404 }
10405 }
10406 oldvalue = ctxt->state->value;
10407 ctxt->state->value = content;
10408 ret = xmlRelaxNGValidateValue(ctxt, define);
10409 ctxt->state->value = oldvalue;
10410 if (ret == -1) {
10411 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10412 } else if (ret == 0) {
10413 ctxt->state->seq = NULL;
10414 }
10415 if (content != NULL)
10416 xmlFree(content);
10417 break;
10418 }
10419 case XML_RELAXNG_LIST:{
10420 xmlChar *content;
10421 xmlNodePtr child;
10422 xmlChar *oldvalue, *oldendvalue;
10423 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010424
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010425 /*
10426 * Make sure it's only text nodes
10427 */
10428
10429 content = NULL;
10430 child = node;
10431 while (child != NULL) {
10432 if (child->type == XML_ELEMENT_NODE) {
10433 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10434 node->parent->name);
10435 ret = -1;
10436 break;
10437 } else if ((child->type == XML_TEXT_NODE) ||
10438 (child->type == XML_CDATA_SECTION_NODE)) {
10439 content = xmlStrcat(content, child->content);
10440 }
10441 /* TODO: handle entities ... */
10442 child = child->next;
10443 }
10444 if (ret == -1) {
10445 if (content != NULL)
10446 xmlFree(content);
10447 break;
10448 }
10449 if (content == NULL) {
10450 content = xmlStrdup(BAD_CAST "");
10451 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010452 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010453 ret = -1;
10454 break;
10455 }
10456 }
10457 len = xmlStrlen(content);
10458 oldvalue = ctxt->state->value;
10459 oldendvalue = ctxt->state->endvalue;
10460 ctxt->state->value = content;
10461 ctxt->state->endvalue = content + len;
10462 ret = xmlRelaxNGValidateValue(ctxt, define);
10463 ctxt->state->value = oldvalue;
10464 ctxt->state->endvalue = oldendvalue;
10465 if (ret == -1) {
10466 VALID_ERR(XML_RELAXNG_ERR_LIST);
10467 } else if ((ret == 0) && (node != NULL)) {
10468 ctxt->state->seq = node->next;
10469 }
10470 if (content != NULL)
10471 xmlFree(content);
10472 break;
10473 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010474 case XML_RELAXNG_EXCEPT:
10475 case XML_RELAXNG_PARAM:
10476 TODO ret = -1;
10477 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010478 }
10479 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010480#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010481 for (i = 0; i < ctxt->depth; i++)
10482 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010483 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010484 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010485 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010486 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010487 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010488 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010489 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010490 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010491#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010492 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010493}
10494
10495/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010496 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010497 * @ctxt: a Relax-NG validation context
10498 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010499 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010500 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010501 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010502 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010503 */
10504static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010505xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10506 xmlRelaxNGDefinePtr define)
10507{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010508 xmlRelaxNGStatesPtr states, res;
10509 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010510
Daniel Veillardfd573f12003-03-16 17:52:32 +000010511 /*
10512 * We should NOT have both ctxt->state and ctxt->states
10513 */
10514 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010515 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10516 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010517 }
10518
10519 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010520 if (ctxt->states != NULL) {
10521 ctxt->state = ctxt->states->tabState[0];
10522 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10523 ctxt->states = NULL;
10524 }
10525 ret = xmlRelaxNGValidateState(ctxt, define);
10526 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10527 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10528 ctxt->state = NULL;
10529 }
10530 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10531 ctxt->state = ctxt->states->tabState[0];
10532 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10533 ctxt->states = NULL;
10534 }
10535 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010536 }
10537
10538 states = ctxt->states;
10539 ctxt->states = NULL;
10540 res = NULL;
10541 j = 0;
10542 oldflags = ctxt->flags;
10543 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010544 for (i = 0; i < states->nbState; i++) {
10545 ctxt->state = states->tabState[i];
10546 ctxt->states = NULL;
10547 ret = xmlRelaxNGValidateState(ctxt, define);
10548 /*
10549 * We should NOT have both ctxt->state and ctxt->states
10550 */
10551 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10552 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10553 ctxt->state = NULL;
10554 }
10555 if (ret == 0) {
10556 if (ctxt->states == NULL) {
10557 if (res != NULL) {
10558 /* add the state to the container */
10559 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10560 ctxt->state = NULL;
10561 } else {
10562 /* add the state directly in states */
10563 states->tabState[j++] = ctxt->state;
10564 ctxt->state = NULL;
10565 }
10566 } else {
10567 if (res == NULL) {
10568 /* make it the new container and copy other results */
10569 res = ctxt->states;
10570 ctxt->states = NULL;
10571 for (k = 0; k < j; k++)
10572 xmlRelaxNGAddStates(ctxt, res,
10573 states->tabState[k]);
10574 } else {
10575 /* add all the new results to res and reff the container */
10576 for (k = 0; k < ctxt->states->nbState; k++)
10577 xmlRelaxNGAddStates(ctxt, res,
10578 ctxt->states->tabState[k]);
10579 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10580 ctxt->states = NULL;
10581 }
10582 }
10583 } else {
10584 if (ctxt->state != NULL) {
10585 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10586 ctxt->state = NULL;
10587 } else if (ctxt->states != NULL) {
10588 for (k = 0; k < ctxt->states->nbState; k++)
10589 xmlRelaxNGFreeValidState(ctxt,
10590 ctxt->states->tabState[k]);
10591 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10592 ctxt->states = NULL;
10593 }
10594 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010595 }
10596 ctxt->flags = oldflags;
10597 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010598 xmlRelaxNGFreeStates(ctxt, states);
10599 ctxt->states = res;
10600 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010601 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010602 states->nbState = j;
10603 ctxt->states = states;
10604 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010605 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010606 ctxt->state = states->tabState[0];
10607 xmlRelaxNGFreeStates(ctxt, states);
10608 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010609 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010610 ret = -1;
10611 xmlRelaxNGFreeStates(ctxt, states);
10612 if (ctxt->states != NULL) {
10613 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10614 ctxt->states = NULL;
10615 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010616 }
10617 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010618 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10619 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010620 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010621 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010622}
10623
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010624/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010625 * xmlRelaxNGValidateDocument:
10626 * @ctxt: a Relax-NG validation context
10627 * @doc: the document
10628 *
10629 * Validate the given document
10630 *
10631 * Returns 0 if the validation succeeded or an error code.
10632 */
10633static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010634xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10635{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010636 int ret;
10637 xmlRelaxNGPtr schema;
10638 xmlRelaxNGGrammarPtr grammar;
10639 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010640 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010641
10642 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010643 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010644
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010645 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010646 schema = ctxt->schema;
10647 grammar = schema->topgrammar;
10648 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010649 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10650 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010651 }
10652 state = xmlRelaxNGNewValidState(ctxt, NULL);
10653 ctxt->state = state;
10654 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010655 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010656 state = ctxt->state;
10657 node = state->seq;
10658 node = xmlRelaxNGSkipIgnored(ctxt, node);
10659 if (node != NULL) {
10660 if (ret != -1) {
10661 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10662 ret = -1;
10663 }
10664 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010665 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010666 int i;
10667 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010668
Daniel Veillard4c004142003-10-07 11:33:24 +000010669 for (i = 0; i < ctxt->states->nbState; i++) {
10670 state = ctxt->states->tabState[i];
10671 node = state->seq;
10672 node = xmlRelaxNGSkipIgnored(ctxt, node);
10673 if (node == NULL)
10674 tmp = 0;
10675 xmlRelaxNGFreeValidState(ctxt, state);
10676 }
10677 if (tmp == -1) {
10678 if (ret != -1) {
10679 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10680 ret = -1;
10681 }
10682 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010683 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010684 if (ctxt->state != NULL) {
10685 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010686 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010687 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010688 if (ret != 0)
10689 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010690#ifdef DEBUG
10691 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010692 ctxt->error(ctxt->userData,
10693 "%d Extra error messages left on stack !\n",
10694 ctxt->errNr);
10695 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010696 }
10697#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010698#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010699 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010700 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010701
Daniel Veillard4c004142003-10-07 11:33:24 +000010702 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10703 vctxt.valid = 1;
10704 vctxt.error = ctxt->error;
10705 vctxt.warning = ctxt->warning;
10706 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010707
Daniel Veillard4c004142003-10-07 11:33:24 +000010708 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10709 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010710 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010711#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010712 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010713 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010714
Daniel Veillard4c004142003-10-07 11:33:24 +000010715 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010716}
10717
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010718/**
10719 * xmlRelaxNGCleanPSVI:
10720 * @node: an input element or document
10721 *
10722 * Call this routine to speed up XPath computation on static documents.
10723 * This stamps all the element nodes with the document order
10724 * Like for line information, the order is kept in the element->content
10725 * field, the value stored is actually - the node number (starting at -1)
10726 * to be able to differentiate from line numbers.
10727 *
10728 * Returns the number of elements found in the document or -1 in case
10729 * of error.
10730 */
10731static void
10732xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10733 xmlNodePtr cur;
10734
10735 if ((node == NULL) ||
10736 ((node->type != XML_ELEMENT_NODE) &&
10737 (node->type != XML_DOCUMENT_NODE) &&
10738 (node->type != XML_HTML_DOCUMENT_NODE)))
10739 return;
10740 if (node->type == XML_ELEMENT_NODE)
10741 node->psvi = NULL;
10742
10743 cur = node->children;
10744 while (cur != NULL) {
10745 if (cur->type == XML_ELEMENT_NODE) {
10746 cur->psvi = NULL;
10747 if (cur->children != NULL) {
10748 cur = cur->children;
10749 continue;
10750 }
10751 }
10752 if (cur->next != NULL) {
10753 cur = cur->next;
10754 continue;
10755 }
10756 do {
10757 cur = cur->parent;
10758 if (cur == NULL)
10759 break;
10760 if (cur == node) {
10761 cur = NULL;
10762 break;
10763 }
10764 if (cur->next != NULL) {
10765 cur = cur->next;
10766 break;
10767 }
10768 } while (cur != NULL);
10769 }
10770 return;
10771}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010772/************************************************************************
10773 * *
10774 * Validation interfaces *
10775 * *
10776 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010777
Daniel Veillard6eadf632003-01-23 18:29:16 +000010778/**
10779 * xmlRelaxNGNewValidCtxt:
10780 * @schema: a precompiled XML RelaxNGs
10781 *
10782 * Create an XML RelaxNGs validation context based on the given schema
10783 *
10784 * Returns the validation context or NULL in case of error
10785 */
10786xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010787xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10788{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010789 xmlRelaxNGValidCtxtPtr ret;
10790
10791 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10792 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010793 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010794 return (NULL);
10795 }
10796 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10797 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010798 ret->error = xmlGenericError;
10799 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010800 ret->errNr = 0;
10801 ret->errMax = 0;
10802 ret->err = NULL;
10803 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010804 if (schema != NULL)
10805 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010806 ret->states = NULL;
10807 ret->freeState = NULL;
10808 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010809 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010810 return (ret);
10811}
10812
10813/**
10814 * xmlRelaxNGFreeValidCtxt:
10815 * @ctxt: the schema validation context
10816 *
10817 * Free the resources associated to the schema validation context
10818 */
10819void
Daniel Veillard4c004142003-10-07 11:33:24 +000010820xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10821{
Daniel Veillard798024a2003-03-19 10:36:09 +000010822 int k;
10823
Daniel Veillard6eadf632003-01-23 18:29:16 +000010824 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010825 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010826 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010827 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010828 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010829 for (k = 0; k < ctxt->freeState->nbState; k++) {
10830 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10831 }
10832 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010833 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010834 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010835 for (k = 0; k < ctxt->freeStatesNr; k++) {
10836 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10837 }
10838 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010839 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010840 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010841 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010842 if (ctxt->elemTab != NULL) {
10843 xmlRegExecCtxtPtr exec;
10844
Daniel Veillard4c004142003-10-07 11:33:24 +000010845 exec = xmlRelaxNGElemPop(ctxt);
10846 while (exec != NULL) {
10847 xmlRegFreeExecCtxt(exec);
10848 exec = xmlRelaxNGElemPop(ctxt);
10849 }
10850 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010851 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010852 xmlFree(ctxt);
10853}
10854
10855/**
10856 * xmlRelaxNGSetValidErrors:
10857 * @ctxt: a Relax-NG validation context
10858 * @err: the error function
10859 * @warn: the warning function
10860 * @ctx: the functions context
10861 *
10862 * Set the error and warning callback informations
10863 */
10864void
10865xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010866 xmlRelaxNGValidityErrorFunc err,
10867 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10868{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010869 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010870 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010871 ctxt->error = err;
10872 ctxt->warning = warn;
10873 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010874 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010875}
10876
10877/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010878 * xmlRelaxNGSetValidStructuredErrors:
10879 * @ctxt: a Relax-NG validation context
10880 * @serror: the structured error function
10881 * @ctx: the functions context
10882 *
10883 * Set the structured error callback
10884 */
10885void
10886xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010887 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010888{
10889 if (ctxt == NULL)
10890 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010891 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010892 ctxt->error = NULL;
10893 ctxt->warning = NULL;
10894 ctxt->userData = ctx;
10895}
10896
10897/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010898 * xmlRelaxNGGetValidErrors:
10899 * @ctxt: a Relax-NG validation context
10900 * @err: the error function result
10901 * @warn: the warning function result
10902 * @ctx: the functions context result
10903 *
10904 * Get the error and warning callback informations
10905 *
10906 * Returns -1 in case of error and 0 otherwise
10907 */
10908int
10909xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010910 xmlRelaxNGValidityErrorFunc * err,
10911 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10912{
Daniel Veillard409a8142003-07-18 15:16:57 +000010913 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010914 return (-1);
10915 if (err != NULL)
10916 *err = ctxt->error;
10917 if (warn != NULL)
10918 *warn = ctxt->warning;
10919 if (ctx != NULL)
10920 *ctx = ctxt->userData;
10921 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010922}
10923
10924/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010925 * xmlRelaxNGValidateDoc:
10926 * @ctxt: a Relax-NG validation context
10927 * @doc: a parsed document tree
10928 *
10929 * Validate a document tree in memory.
10930 *
10931 * Returns 0 if the document is valid, a positive error code
10932 * number otherwise and -1 in case of internal or API error.
10933 */
10934int
Daniel Veillard4c004142003-10-07 11:33:24 +000010935xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10936{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010937 int ret;
10938
10939 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010940 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010941
10942 ctxt->doc = doc;
10943
10944 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010945 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010946 * Remove all left PSVI
10947 */
10948 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
10949
10950 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000010951 * TODO: build error codes
10952 */
10953 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010954 return (1);
10955 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010956}
10957
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010958#define bottom_relaxng
10959#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000010960#endif /* LIBXML_SCHEMAS_ENABLED */