blob: 5779e7fc5f49e2b99c60f7ceb141ef1b79536825 [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
Daniel Veillard264cee62012-08-13 12:40:53 +080042#define IS_RELAXNG(node, typ) \
Daniel Veillard6eadf632003-01-23 18:29:16 +000043 ((node != NULL) && (node->ns != NULL) && \
Daniel Veillard264cee62012-08-13 12:40:53 +080044 (node->type == XML_ELEMENT_NODE) && \
45 (xmlStrEqual(node->name, (const xmlChar *) typ)) && \
Daniel Veillard6eadf632003-01-23 18:29:16 +000046 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
47
48
Daniel Veillard23a47d62008-06-25 04:11:24 +000049#if 0
Daniel Veillard87254c82006-02-19 15:27:17 +000050#define DEBUG 1
Daniel Veillard4c004142003-10-07 11:33:24 +000051
Daniel Veillard87254c82006-02-19 15:27:17 +000052#define DEBUG_GRAMMAR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000053
Daniel Veillard87254c82006-02-19 15:27:17 +000054#define DEBUG_CONTENT 1
Daniel Veillard4c004142003-10-07 11:33:24 +000055
Daniel Veillard87254c82006-02-19 15:27:17 +000056#define DEBUG_TYPE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000057
Daniel Veillard87254c82006-02-19 15:27:17 +000058#define DEBUG_VALID 1
Daniel Veillard4c004142003-10-07 11:33:24 +000059
Daniel Veillard87254c82006-02-19 15:27:17 +000060#define DEBUG_INTERLEAVE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000061
Daniel Veillard87254c82006-02-19 15:27:17 +000062#define DEBUG_LIST 1
Daniel Veillard4c004142003-10-07 11:33:24 +000063
Daniel Veillardf8e3db02012-09-11 13:26:36 +080064#define DEBUG_INCLUDE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000065
Daniel Veillard87254c82006-02-19 15:27:17 +000066#define DEBUG_ERROR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000067
Daniel Veillard87254c82006-02-19 15:27:17 +000068#define DEBUG_COMPILE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000069
Daniel Veillard87254c82006-02-19 15:27:17 +000070#define DEBUG_PROGRESSIVE 1
71#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +000072
Daniel Veillard5f1946a2003-03-31 16:38:16 +000073#define MAX_ERROR 5
74
Daniel Veillardf8e3db02012-09-11 13:26:36 +080075#define TODO \
Daniel Veillard6eadf632003-01-23 18:29:16 +000076 xmlGenericError(xmlGenericErrorContext, \
77 "Unimplemented block at %s:%d\n", \
78 __FILE__, __LINE__);
79
80typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
81typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
82
83typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
84typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
85
Daniel Veillardd41f4f42003-01-29 21:07:52 +000086typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
87typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
88
Daniel Veillarda9d912d2003-02-01 17:43:10 +000089typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
90typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
91
Daniel Veillard6eadf632003-01-23 18:29:16 +000092typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000093 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
94 XML_RELAXNG_COMBINE_CHOICE, /* choice */
95 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000096} xmlRelaxNGCombine;
97
Daniel Veillard4c5cf702003-02-21 15:40:34 +000098typedef enum {
99 XML_RELAXNG_CONTENT_ERROR = -1,
100 XML_RELAXNG_CONTENT_EMPTY = 0,
101 XML_RELAXNG_CONTENT_SIMPLE,
102 XML_RELAXNG_CONTENT_COMPLEX
103} xmlRelaxNGContentType;
104
Daniel Veillard6eadf632003-01-23 18:29:16 +0000105typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
106typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
107
108struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000109 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
110 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
111 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
112 xmlRelaxNGDefinePtr start; /* <start> content */
113 xmlRelaxNGCombine combine; /* the default combine value */
114 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
115 xmlHashTablePtr defs; /* define* */
116 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000117};
118
119
Daniel Veillard6eadf632003-01-23 18:29:16 +0000120typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000121 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
122 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000123 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000124 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
125 XML_RELAXNG_TEXT, /* textual content */
126 XML_RELAXNG_ELEMENT, /* an element */
127 XML_RELAXNG_DATATYPE, /* extenal data type definition */
128 XML_RELAXNG_PARAM, /* extenal data type parameter */
129 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
130 XML_RELAXNG_LIST, /* a list of patterns */
131 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
132 XML_RELAXNG_DEF, /* a definition */
133 XML_RELAXNG_REF, /* reference to a definition */
134 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
135 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
136 XML_RELAXNG_OPTIONAL, /* optional patterns */
137 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
138 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
139 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
140 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
141 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
142 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000143} xmlRelaxNGType;
144
Daniel Veillard52b48c72003-04-13 19:53:42 +0000145#define IS_NULLABLE (1 << 0)
146#define IS_NOT_NULLABLE (1 << 1)
147#define IS_INDETERMINIST (1 << 2)
148#define IS_MIXED (1 << 3)
149#define IS_TRIABLE (1 << 4)
150#define IS_PROCESSED (1 << 5)
151#define IS_COMPILABLE (1 << 6)
152#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillardaa422d92009-09-24 11:31:48 +0200153#define IS_EXTERNAL_REF (1 << 8)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000154
Daniel Veillard6eadf632003-01-23 18:29:16 +0000155struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000156 xmlRelaxNGType type; /* the type of definition */
157 xmlNodePtr node; /* the node in the source */
158 xmlChar *name; /* the element local name if present */
159 xmlChar *ns; /* the namespace local name if present */
160 xmlChar *value; /* value when available */
161 void *data; /* data lib or specific pointer */
162 xmlRelaxNGDefinePtr content; /* the expected content */
163 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
164 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
165 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
166 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
167 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
168 short depth; /* used for the cycle detection */
169 short dflags; /* define related flags */
170 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000171};
172
173/**
174 * _xmlRelaxNG:
175 *
176 * A RelaxNGs definition
177 */
178struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000179 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000180 xmlRelaxNGGrammarPtr topgrammar;
181 xmlDocPtr doc;
182
Daniel Veillard4c004142003-10-07 11:33:24 +0000183 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000184
Daniel Veillard4c004142003-10-07 11:33:24 +0000185 xmlHashTablePtr defs; /* define */
186 xmlHashTablePtr refs; /* references */
187 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
188 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
189 int defNr; /* number of defines used */
190 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000191
Daniel Veillard6eadf632003-01-23 18:29:16 +0000192};
193
Daniel Veillard77648bb2003-02-20 15:03:22 +0000194#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
195#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
196#define XML_RELAXNG_IN_LIST (1 << 2)
197#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
198#define XML_RELAXNG_IN_START (1 << 4)
199#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
200#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
201#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000202#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
203#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000204
205struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000206 void *userData; /* user specific data block */
207 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
208 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000209 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000210 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000211
Daniel Veillard4c004142003-10-07 11:33:24 +0000212 xmlRelaxNGPtr schema; /* The schema in use */
213 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
214 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
215 int flags; /* parser flags */
216 int nbErrors; /* number of errors at parse time */
217 int nbWarnings; /* number of warnings at parse time */
218 const xmlChar *define; /* the current define scope */
219 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000220
Daniel Veillard4c004142003-10-07 11:33:24 +0000221 int nbInterleaves;
222 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000223
Daniel Veillard4c004142003-10-07 11:33:24 +0000224 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
225 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
226 xmlChar *URL;
227 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000228
Daniel Veillard4c004142003-10-07 11:33:24 +0000229 int defNr; /* number of defines used */
230 int defMax; /* number of defines aloocated */
231 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000232
Daniel Veillard4c004142003-10-07 11:33:24 +0000233 const char *buffer;
234 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000235
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000236 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000237 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
238 int docNr; /* Depth of the parsing stack */
239 int docMax; /* Max depth of the parsing stack */
240 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000241
242 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000243 xmlRelaxNGIncludePtr inc; /* Current parsed include */
244 int incNr; /* Depth of the include parsing stack */
245 int incMax; /* Max depth of the parsing stack */
246 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000247
Daniel Veillard4c004142003-10-07 11:33:24 +0000248 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000249
250 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000251 xmlAutomataPtr am; /* the automata */
252 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000253
254 int crng; /* compact syntax and other flags */
Daniel Veillard42595322004-11-08 10:52:06 +0000255 int freedoc; /* need to free the document */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000256};
257
258#define FLAGS_IGNORABLE 1
259#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000260#define FLAGS_MIXED_CONTENT 4
Daniel Veillardb30ca312005-09-04 13:50:03 +0000261#define FLAGS_NOERROR 8
Daniel Veillard6eadf632003-01-23 18:29:16 +0000262
263/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000264 * xmlRelaxNGInterleaveGroup:
265 *
266 * A RelaxNGs partition set associated to lists of definitions
267 */
268typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
269typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
270struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000271 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
272 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
273 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000274};
275
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000276#define IS_DETERMINIST 1
277#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000278
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000279/**
280 * xmlRelaxNGPartitions:
281 *
282 * A RelaxNGs partition associated to an interleave group
283 */
284typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
285typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
286struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000287 int nbgroups; /* number of groups in the partitions */
288 xmlHashTablePtr triage; /* hash table used to direct nodes to the
289 * right group when possible */
290 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000291 xmlRelaxNGInterleaveGroupPtr *groups;
292};
293
294/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000295 * xmlRelaxNGValidState:
296 *
297 * A RelaxNGs validation state
298 */
299#define MAX_ATTR 20
300typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
301typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
302struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000303 xmlNodePtr node; /* the current node */
304 xmlNodePtr seq; /* the sequence of children left to validate */
305 int nbAttrs; /* the number of attributes */
306 int maxAttrs; /* the size of attrs */
307 int nbAttrLeft; /* the number of attributes left to validate */
308 xmlChar *value; /* the value when operating on string */
309 xmlChar *endvalue; /* the end value when operating on string */
310 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000311};
312
313/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000314 * xmlRelaxNGStates:
315 *
316 * A RelaxNGs container for validation state
317 */
318typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
319typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
320struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000321 int nbState; /* the number of states */
322 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000323 xmlRelaxNGValidStatePtr *tabState;
324};
325
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000326#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000327
Daniel Veillardfd573f12003-03-16 17:52:32 +0000328/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000329 * xmlRelaxNGValidError:
330 *
331 * A RelaxNGs validation error
332 */
333typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
334typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
335struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000336 xmlRelaxNGValidErr err; /* the error number */
337 int flags; /* flags */
338 xmlNodePtr node; /* the current node */
339 xmlNodePtr seq; /* the current child */
340 const xmlChar *arg1; /* first arg */
341 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000342};
343
344/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000345 * xmlRelaxNGValidCtxt:
346 *
347 * A RelaxNGs validation context
348 */
349
350struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000351 void *userData; /* user specific data block */
352 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
353 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000354 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000355 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000356
Daniel Veillard4c004142003-10-07 11:33:24 +0000357 xmlRelaxNGPtr schema; /* The schema in use */
358 xmlDocPtr doc; /* the document being validated */
359 int flags; /* validation flags */
360 int depth; /* validation depth */
361 int idref; /* requires idref checking */
362 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000363
364 /*
365 * Errors accumulated in branches may have to be stacked to be
366 * provided back when it's sure they affect validation.
367 */
368 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000369 int errNr; /* Depth of the error stack */
370 int errMax; /* Max depth of the error stack */
371 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000372
Daniel Veillard4c004142003-10-07 11:33:24 +0000373 xmlRelaxNGValidStatePtr state; /* the current validation state */
374 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000375
Daniel Veillard4c004142003-10-07 11:33:24 +0000376 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
377 int freeStatesNr;
378 int freeStatesMax;
379 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000380
381 /*
382 * This is used for "progressive" validation
383 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000384 xmlRegExecCtxtPtr elem; /* the current element regexp */
385 int elemNr; /* the number of element validated */
386 int elemMax; /* the max depth of elements */
387 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
388 int pstate; /* progressive state */
389 xmlNodePtr pnode; /* the current node */
390 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
391 int perr; /* signal error in content model
392 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000393};
394
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000395/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000396 * xmlRelaxNGInclude:
397 *
398 * Structure associated to a RelaxNGs document element
399 */
400struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000401 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
402 xmlChar *href; /* the normalized href value */
403 xmlDocPtr doc; /* the associated XML document */
404 xmlRelaxNGDefinePtr content; /* the definitions */
405 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000406};
407
408/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000409 * xmlRelaxNGDocument:
410 *
411 * Structure associated to a RelaxNGs document element
412 */
413struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000414 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000415 xmlChar *href; /* the normalized href value */
416 xmlDocPtr doc; /* the associated XML document */
417 xmlRelaxNGDefinePtr content; /* the definitions */
418 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillard81c51e12009-08-14 18:52:10 +0200419 int externalRef; /* 1 if an external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000420};
421
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000422
Daniel Veillard6eadf632003-01-23 18:29:16 +0000423/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000424 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800425 * Some factorized error routines *
Daniel Veillard4c004142003-10-07 11:33:24 +0000426 * *
427 ************************************************************************/
428
429/**
430 * xmlRngPErrMemory:
431 * @ctxt: an Relax-NG parser context
432 * @extra: extra informations
433 *
434 * Handle a redefinition of attribute error
435 */
436static void
437xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
438{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000439 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000440 xmlGenericErrorFunc channel = NULL;
441 void *data = NULL;
442
443 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000444 if (ctxt->serror != NULL)
445 schannel = ctxt->serror;
446 else
447 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000448 data = ctxt->userData;
449 ctxt->nbErrors++;
450 }
451 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000452 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000453 NULL, NULL, XML_FROM_RELAXNGP,
454 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
455 NULL, NULL, 0, 0,
456 "Memory allocation failed : %s\n", extra);
457 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000458 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000459 NULL, NULL, XML_FROM_RELAXNGP,
460 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
461 NULL, NULL, 0, 0, "Memory allocation failed\n");
462}
463
464/**
465 * xmlRngVErrMemory:
466 * @ctxt: a Relax-NG validation context
467 * @extra: extra informations
468 *
469 * Handle a redefinition of attribute error
470 */
471static void
472xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
473{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000474 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000475 xmlGenericErrorFunc channel = NULL;
476 void *data = NULL;
477
478 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000479 if (ctxt->serror != NULL)
480 schannel = ctxt->serror;
481 else
482 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000483 data = ctxt->userData;
484 ctxt->nbErrors++;
485 }
486 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000487 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000488 NULL, NULL, XML_FROM_RELAXNGV,
489 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
490 NULL, NULL, 0, 0,
491 "Memory allocation failed : %s\n", extra);
492 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000493 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000494 NULL, NULL, XML_FROM_RELAXNGV,
495 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
496 NULL, NULL, 0, 0, "Memory allocation failed\n");
497}
498
499/**
500 * xmlRngPErr:
501 * @ctxt: a Relax-NG parser context
502 * @node: the node raising the error
503 * @error: the error code
504 * @msg: message
505 * @str1: extra info
506 * @str2: extra info
507 *
508 * Handle a Relax NG Parsing error
509 */
510static void
511xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
512 const char *msg, const xmlChar * str1, const xmlChar * str2)
513{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000514 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000515 xmlGenericErrorFunc channel = NULL;
516 void *data = NULL;
517
518 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000519 if (ctxt->serror != NULL)
520 schannel = ctxt->serror;
521 else
522 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000523 data = ctxt->userData;
524 ctxt->nbErrors++;
525 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000526 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000527 NULL, node, XML_FROM_RELAXNGP,
528 error, XML_ERR_ERROR, NULL, 0,
529 (const char *) str1, (const char *) str2, NULL, 0, 0,
530 msg, str1, str2);
531}
532
533/**
534 * xmlRngVErr:
535 * @ctxt: a Relax-NG validation context
536 * @node: the node raising the error
537 * @error: the error code
538 * @msg: message
539 * @str1: extra info
540 * @str2: extra info
541 *
542 * Handle a Relax NG Validation error
543 */
544static void
545xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
546 const char *msg, const xmlChar * str1, const xmlChar * str2)
547{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000548 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000549 xmlGenericErrorFunc channel = NULL;
550 void *data = NULL;
551
552 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000553 if (ctxt->serror != NULL)
554 schannel = ctxt->serror;
555 else
556 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000557 data = ctxt->userData;
558 ctxt->nbErrors++;
559 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000560 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000561 NULL, node, XML_FROM_RELAXNGV,
562 error, XML_ERR_ERROR, NULL, 0,
563 (const char *) str1, (const char *) str2, NULL, 0, 0,
564 msg, str1, str2);
565}
566
567/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800568 * *
569 * Preliminary type checking interfaces *
570 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000571 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000572
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000573/**
574 * xmlRelaxNGTypeHave:
575 * @data: data needed for the library
576 * @type: the type name
577 * @value: the value to check
578 *
579 * Function provided by a type library to check if a type is exported
580 *
581 * Returns 1 if yes, 0 if no and -1 in case of error.
582 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000583typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000584
585/**
586 * xmlRelaxNGTypeCheck:
587 * @data: data needed for the library
588 * @type: the type name
589 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000590 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000591 *
592 * Function provided by a type library to check if a value match a type
593 *
594 * Returns 1 if yes, 0 if no and -1 in case of error.
595 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000596typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
597 const xmlChar * value, void **result,
598 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000599
600/**
601 * xmlRelaxNGFacetCheck:
602 * @data: data needed for the library
603 * @type: the type name
604 * @facet: the facet name
605 * @val: the facet value
606 * @strval: the string value
607 * @value: the value to check
608 *
609 * Function provided by a type library to check a value facet
610 *
611 * Returns 1 if yes, 0 if no and -1 in case of error.
612 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000613typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
614 const xmlChar * facet,
615 const xmlChar * val,
616 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000617
618/**
619 * xmlRelaxNGTypeFree:
620 * @data: data needed for the library
621 * @result: the value to free
622 *
623 * Function provided by a type library to free a returned result
624 */
625typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000626
627/**
628 * xmlRelaxNGTypeCompare:
629 * @data: data needed for the library
630 * @type: the type name
631 * @value1: the first value
632 * @value2: the second value
633 *
634 * Function provided by a type library to compare two values accordingly
635 * to a type.
636 *
637 * Returns 1 if yes, 0 if no and -1 in case of error.
638 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000639typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
640 const xmlChar * value1,
641 xmlNodePtr ctxt1,
642 void *comp1,
643 const xmlChar * value2,
644 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000645typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
646typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
647struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000648 const xmlChar *namespace; /* the datatypeLibrary value */
649 void *data; /* data needed for the library */
650 xmlRelaxNGTypeHave have; /* the export function */
651 xmlRelaxNGTypeCheck check; /* the checking function */
652 xmlRelaxNGTypeCompare comp; /* the compare function */
653 xmlRelaxNGFacetCheck facet; /* the facet check function */
654 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000655};
656
657/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800658 * *
659 * Allocation functions *
660 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000661 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000662static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
663static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000664static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000665static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000666static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
667 ATTRIBUTE_UNUSED,
668 xmlRelaxNGValidStatePtr state1,
669 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000670static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000671 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000672
673/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000674 * xmlRelaxNGFreeDocument:
675 * @docu: a document structure
676 *
677 * Deallocate a RelaxNG document structure.
678 */
679static void
680xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
681{
682 if (docu == NULL)
683 return;
684
685 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000686 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000687 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000688 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000689 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000690 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000691 xmlFree(docu);
692}
693
694/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000695 * xmlRelaxNGFreeDocumentList:
696 * @docu: a list of document structure
697 *
698 * Deallocate a RelaxNG document structures.
699 */
700static void
701xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
702{
703 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000704
Daniel Veillardc482e262003-02-26 14:48:48 +0000705 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000706 next = docu->next;
707 xmlRelaxNGFreeDocument(docu);
708 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000709 }
710}
711
712/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000713 * xmlRelaxNGFreeInclude:
714 * @incl: a include structure
715 *
716 * Deallocate a RelaxNG include structure.
717 */
718static void
719xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
720{
721 if (incl == NULL)
722 return;
723
724 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000725 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000726 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000727 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000728 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000729 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000730 xmlFree(incl);
731}
732
733/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000734 * xmlRelaxNGFreeIncludeList:
735 * @incl: a include structure list
736 *
737 * Deallocate a RelaxNG include structure.
738 */
739static void
740xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
741{
742 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000743
Daniel Veillardc482e262003-02-26 14:48:48 +0000744 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000745 next = incl->next;
746 xmlRelaxNGFreeInclude(incl);
747 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000748 }
749}
750
751/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000752 * xmlRelaxNGNewRelaxNG:
753 * @ctxt: a Relax-NG validation context (optional)
754 *
755 * Allocate a new RelaxNG structure.
756 *
757 * Returns the newly allocated structure or NULL in case or error
758 */
759static xmlRelaxNGPtr
760xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
761{
762 xmlRelaxNGPtr ret;
763
764 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
765 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000766 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000767 return (NULL);
768 }
769 memset(ret, 0, sizeof(xmlRelaxNG));
770
771 return (ret);
772}
773
774/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000775 * xmlRelaxNGFreeInnerSchema:
776 * @schema: a schema structure
777 *
778 * Deallocate a RelaxNG schema structure.
779 */
780static void
781xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
782{
783 if (schema == NULL)
784 return;
785
786 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000787 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000788 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000789 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000790
Daniel Veillard4c004142003-10-07 11:33:24 +0000791 for (i = 0; i < schema->defNr; i++)
792 xmlRelaxNGFreeDefine(schema->defTab[i]);
793 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000794 }
795
796 xmlFree(schema);
797}
798
799/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000800 * xmlRelaxNGFree:
801 * @schema: a schema structure
802 *
803 * Deallocate a RelaxNG structure.
804 */
805void
806xmlRelaxNGFree(xmlRelaxNGPtr schema)
807{
808 if (schema == NULL)
809 return;
810
Daniel Veillard6eadf632003-01-23 18:29:16 +0000811 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000812 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000813 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000814 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000815 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000816 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000817 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000818 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000819 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000820 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000821
Daniel Veillard4c004142003-10-07 11:33:24 +0000822 for (i = 0; i < schema->defNr; i++)
823 xmlRelaxNGFreeDefine(schema->defTab[i]);
824 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000825 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000826
827 xmlFree(schema);
828}
829
830/**
831 * xmlRelaxNGNewGrammar:
832 * @ctxt: a Relax-NG validation context (optional)
833 *
834 * Allocate a new RelaxNG grammar.
835 *
836 * Returns the newly allocated structure or NULL in case or error
837 */
838static xmlRelaxNGGrammarPtr
839xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
840{
841 xmlRelaxNGGrammarPtr ret;
842
843 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
844 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000845 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000846 return (NULL);
847 }
848 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
849
850 return (ret);
851}
852
853/**
854 * xmlRelaxNGFreeGrammar:
855 * @grammar: a grammar structure
856 *
857 * Deallocate a RelaxNG grammar structure.
858 */
859static void
860xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
861{
862 if (grammar == NULL)
863 return;
864
Daniel Veillardc482e262003-02-26 14:48:48 +0000865 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000866 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000867 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000868 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000869 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000870 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000871 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000872 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000873 }
874 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000875 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000876 }
877
878 xmlFree(grammar);
879}
880
881/**
882 * xmlRelaxNGNewDefine:
883 * @ctxt: a Relax-NG validation context
884 * @node: the node in the input document.
885 *
886 * Allocate a new RelaxNG define.
887 *
888 * Returns the newly allocated structure or NULL in case or error
889 */
890static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000891xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000892{
893 xmlRelaxNGDefinePtr ret;
894
Daniel Veillard419a7682003-02-03 23:22:49 +0000895 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000896 ctxt->defMax = 16;
897 ctxt->defNr = 0;
898 ctxt->defTab = (xmlRelaxNGDefinePtr *)
899 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
900 if (ctxt->defTab == NULL) {
901 xmlRngPErrMemory(ctxt, "allocating define\n");
902 return (NULL);
903 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000904 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000905 xmlRelaxNGDefinePtr *tmp;
906
907 ctxt->defMax *= 2;
908 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
909 ctxt->defMax *
910 sizeof
911 (xmlRelaxNGDefinePtr));
912 if (tmp == NULL) {
913 xmlRngPErrMemory(ctxt, "allocating define\n");
914 return (NULL);
915 }
916 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000917 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000918 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
919 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000920 xmlRngPErrMemory(ctxt, "allocating define\n");
921 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000922 }
923 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000924 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000925 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000926 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000927 return (ret);
928}
929
930/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000931 * xmlRelaxNGFreePartition:
932 * @partitions: a partition set structure
933 *
934 * Deallocate RelaxNG partition set structures.
935 */
936static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000937xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
938{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000939 xmlRelaxNGInterleaveGroupPtr group;
940 int j;
941
942 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000943 if (partitions->groups != NULL) {
944 for (j = 0; j < partitions->nbgroups; j++) {
945 group = partitions->groups[j];
946 if (group != NULL) {
947 if (group->defs != NULL)
948 xmlFree(group->defs);
949 if (group->attrs != NULL)
950 xmlFree(group->attrs);
951 xmlFree(group);
952 }
953 }
954 xmlFree(partitions->groups);
955 }
956 if (partitions->triage != NULL) {
957 xmlHashFree(partitions->triage, NULL);
958 }
959 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000960 }
961}
Daniel Veillard4c004142003-10-07 11:33:24 +0000962
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000963/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000964 * xmlRelaxNGFreeDefine:
965 * @define: a define structure
966 *
967 * Deallocate a RelaxNG define structure.
968 */
969static void
970xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
971{
972 if (define == NULL)
973 return;
974
Daniel Veillard4c004142003-10-07 11:33:24 +0000975 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
976 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000977
Daniel Veillard4c004142003-10-07 11:33:24 +0000978 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
979 if ((lib != NULL) && (lib->freef != NULL))
980 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000981 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000982 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
983 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
984 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
985 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000986 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000987 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000988 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000989 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000990 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000991 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000992 if (define->contModel != NULL)
993 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000994 xmlFree(define);
995}
996
997/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000998 * xmlRelaxNGNewStates:
999 * @ctxt: a Relax-NG validation context
1000 * @size: the default size for the container
1001 *
1002 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001003 *
1004 * Returns the newly allocated structure or NULL in case or error
1005 */
1006static xmlRelaxNGStatesPtr
1007xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1008{
1009 xmlRelaxNGStatesPtr ret;
1010
Daniel Veillard798024a2003-03-19 10:36:09 +00001011 if ((ctxt != NULL) &&
Daniel Veillard9fcd4622009-08-14 16:16:31 +02001012 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001013 ctxt->freeStatesNr--;
1014 ret = ctxt->freeStates[ctxt->freeStatesNr];
1015 ret->nbState = 0;
1016 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001017 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001018 if (size < 16)
1019 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001020
1021 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001022 (size -
1023 1) *
1024 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001025 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001026 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001027 return (NULL);
1028 }
1029 ret->nbState = 0;
1030 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001031 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1032 sizeof
1033 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001034 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001035 xmlRngVErrMemory(ctxt, "allocating states\n");
1036 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001037 return (NULL);
1038 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001039 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001040}
1041
1042/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001043 * xmlRelaxNGAddStateUniq:
1044 * @ctxt: a Relax-NG validation context
1045 * @states: the states container
1046 * @state: the validation state
1047 *
1048 * Add a RelaxNG validation state to the container without checking
1049 * for unicity.
1050 *
1051 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1052 */
1053static int
1054xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001055 xmlRelaxNGStatesPtr states,
1056 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001057{
1058 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001059 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001060 }
1061 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001062 xmlRelaxNGValidStatePtr *tmp;
1063 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001064
Daniel Veillard4c004142003-10-07 11:33:24 +00001065 size = states->maxState * 2;
1066 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1067 (size) *
1068 sizeof
1069 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001070 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001071 xmlRngVErrMemory(ctxt, "adding states\n");
1072 return (-1);
1073 }
1074 states->tabState = tmp;
1075 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001076 }
1077 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001078 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001079}
1080
1081/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001082 * xmlRelaxNGAddState:
1083 * @ctxt: a Relax-NG validation context
1084 * @states: the states container
1085 * @state: the validation state
1086 *
1087 * Add a RelaxNG validation state to the container
1088 *
1089 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1090 */
1091static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001092xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1093 xmlRelaxNGStatesPtr states,
1094 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001095{
1096 int i;
1097
Gaurav Gupta7d2e8c92014-07-14 16:08:28 +08001098 if (state == NULL || states == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001099 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001100 }
1101 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001102 xmlRelaxNGValidStatePtr *tmp;
1103 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001104
Daniel Veillard4c004142003-10-07 11:33:24 +00001105 size = states->maxState * 2;
1106 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1107 (size) *
1108 sizeof
1109 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001110 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001111 xmlRngVErrMemory(ctxt, "adding states\n");
1112 return (-1);
1113 }
1114 states->tabState = tmp;
1115 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001116 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001117 for (i = 0; i < states->nbState; i++) {
1118 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1119 xmlRelaxNGFreeValidState(ctxt, state);
1120 return (0);
1121 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001122 }
1123 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001124 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001125}
1126
1127/**
1128 * xmlRelaxNGFreeStates:
1129 * @ctxt: a Relax-NG validation context
1130 * @states: teh container
1131 *
1132 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001133 */
1134static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001135xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001136 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001137{
Daniel Veillard798024a2003-03-19 10:36:09 +00001138 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001139 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001140 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001141 ctxt->freeStatesMax = 40;
1142 ctxt->freeStatesNr = 0;
1143 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1144 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1145 if (ctxt->freeStates == NULL) {
1146 xmlRngVErrMemory(ctxt, "storing states\n");
1147 }
1148 } else if ((ctxt != NULL)
1149 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1150 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001151
Daniel Veillard4c004142003-10-07 11:33:24 +00001152 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1153 2 * ctxt->freeStatesMax *
1154 sizeof
1155 (xmlRelaxNGStatesPtr));
1156 if (tmp == NULL) {
1157 xmlRngVErrMemory(ctxt, "storing states\n");
1158 xmlFree(states->tabState);
1159 xmlFree(states);
1160 return;
1161 }
1162 ctxt->freeStates = tmp;
1163 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001164 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001165 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001166 xmlFree(states->tabState);
1167 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001168 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001169 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001170 }
1171}
1172
1173/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001174 * xmlRelaxNGNewValidState:
1175 * @ctxt: a Relax-NG validation context
1176 * @node: the current node or NULL for the document
1177 *
1178 * Allocate a new RelaxNG validation state
1179 *
1180 * Returns the newly allocated structure or NULL in case or error
1181 */
1182static xmlRelaxNGValidStatePtr
1183xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1184{
1185 xmlRelaxNGValidStatePtr ret;
1186 xmlAttrPtr attr;
1187 xmlAttrPtr attrs[MAX_ATTR];
1188 int nbAttrs = 0;
1189 xmlNodePtr root = NULL;
1190
1191 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001192 root = xmlDocGetRootElement(ctxt->doc);
1193 if (root == NULL)
1194 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001195 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001196 attr = node->properties;
1197 while (attr != NULL) {
1198 if (nbAttrs < MAX_ATTR)
1199 attrs[nbAttrs++] = attr;
1200 else
1201 nbAttrs++;
1202 attr = attr->next;
1203 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001204 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001205 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1206 ctxt->freeState->nbState--;
1207 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001208 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001209 ret =
1210 (xmlRelaxNGValidStatePtr)
1211 xmlMalloc(sizeof(xmlRelaxNGValidState));
1212 if (ret == NULL) {
1213 xmlRngVErrMemory(ctxt, "allocating states\n");
1214 return (NULL);
1215 }
1216 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001217 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001218 ret->value = NULL;
1219 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001220 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001221 ret->node = (xmlNodePtr) ctxt->doc;
1222 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001223 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001224 ret->node = node;
1225 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001226 }
1227 ret->nbAttrs = 0;
1228 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001229 if (ret->attrs == NULL) {
1230 if (nbAttrs < 4)
1231 ret->maxAttrs = 4;
1232 else
1233 ret->maxAttrs = nbAttrs;
1234 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1235 sizeof(xmlAttrPtr));
1236 if (ret->attrs == NULL) {
1237 xmlRngVErrMemory(ctxt, "allocating states\n");
1238 return (ret);
1239 }
1240 } else if (ret->maxAttrs < nbAttrs) {
1241 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001242
Daniel Veillard4c004142003-10-07 11:33:24 +00001243 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1244 sizeof(xmlAttrPtr));
1245 if (tmp == NULL) {
1246 xmlRngVErrMemory(ctxt, "allocating states\n");
1247 return (ret);
1248 }
1249 ret->attrs = tmp;
1250 ret->maxAttrs = nbAttrs;
1251 }
1252 ret->nbAttrs = nbAttrs;
1253 if (nbAttrs < MAX_ATTR) {
1254 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1255 } else {
1256 attr = node->properties;
1257 nbAttrs = 0;
1258 while (attr != NULL) {
1259 ret->attrs[nbAttrs++] = attr;
1260 attr = attr->next;
1261 }
1262 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001263 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001264 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001265 return (ret);
1266}
1267
1268/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001269 * xmlRelaxNGCopyValidState:
1270 * @ctxt: a Relax-NG validation context
1271 * @state: a validation state
1272 *
1273 * Copy the validation state
1274 *
1275 * Returns the newly allocated structure or NULL in case or error
1276 */
1277static xmlRelaxNGValidStatePtr
1278xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001279 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001280{
1281 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001282 unsigned int maxAttrs;
1283 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001284
1285 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001286 return (NULL);
1287 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1288 ctxt->freeState->nbState--;
1289 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001290 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001291 ret =
1292 (xmlRelaxNGValidStatePtr)
1293 xmlMalloc(sizeof(xmlRelaxNGValidState));
1294 if (ret == NULL) {
1295 xmlRngVErrMemory(ctxt, "allocating states\n");
1296 return (NULL);
1297 }
1298 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001299 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001300 attrs = ret->attrs;
1301 maxAttrs = ret->maxAttrs;
1302 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1303 ret->attrs = attrs;
1304 ret->maxAttrs = maxAttrs;
1305 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001306 if (ret->attrs == NULL) {
1307 ret->maxAttrs = state->maxAttrs;
1308 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1309 sizeof(xmlAttrPtr));
1310 if (ret->attrs == NULL) {
1311 xmlRngVErrMemory(ctxt, "allocating states\n");
1312 ret->nbAttrs = 0;
1313 return (ret);
1314 }
1315 } else if (ret->maxAttrs < state->nbAttrs) {
1316 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001317
Daniel Veillard4c004142003-10-07 11:33:24 +00001318 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1319 sizeof(xmlAttrPtr));
1320 if (tmp == NULL) {
1321 xmlRngVErrMemory(ctxt, "allocating states\n");
1322 ret->nbAttrs = 0;
1323 return (ret);
1324 }
1325 ret->maxAttrs = state->maxAttrs;
1326 ret->attrs = tmp;
1327 }
1328 memcpy(ret->attrs, state->attrs,
1329 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001330 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001331 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001332}
1333
1334/**
1335 * xmlRelaxNGEqualValidState:
1336 * @ctxt: a Relax-NG validation context
1337 * @state1: a validation state
1338 * @state2: a validation state
1339 *
1340 * Compare the validation states for equality
1341 *
1342 * Returns 1 if equald, 0 otherwise
1343 */
1344static int
1345xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001346 xmlRelaxNGValidStatePtr state1,
1347 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001348{
1349 int i;
1350
1351 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001352 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001353 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001354 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001355 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001356 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001357 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001358 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001359 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001360 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001361 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001362 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001363 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001364 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001365 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001366 (!xmlStrEqual(state1->value, state2->value)))
1367 return (0);
1368 for (i = 0; i < state1->nbAttrs; i++) {
1369 if (state1->attrs[i] != state2->attrs[i])
1370 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001371 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001372 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001373}
1374
1375/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001376 * xmlRelaxNGFreeValidState:
1377 * @state: a validation state structure
1378 *
1379 * Deallocate a RelaxNG validation state structure.
1380 */
1381static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001382xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001383 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001384{
1385 if (state == NULL)
1386 return;
1387
Daniel Veillard798024a2003-03-19 10:36:09 +00001388 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001389 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001390 }
1391 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001392 if (state->attrs != NULL)
1393 xmlFree(state->attrs);
1394 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001395 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001396 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001397 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001398}
1399
1400/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001401 * *
1402 * Semi internal functions *
1403 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001404 ************************************************************************/
1405
1406/**
1407 * xmlRelaxParserSetFlag:
1408 * @ctxt: a RelaxNG parser context
1409 * @flags: a set of flags values
1410 *
1411 * Semi private function used to pass informations to a parser context
1412 * which are a combination of xmlRelaxNGParserFlag .
1413 *
1414 * Returns 0 if success and -1 in case of error
1415 */
1416int
1417xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1418{
1419 if (ctxt == NULL) return(-1);
1420 if (flags & XML_RELAXNGP_FREE_DOC) {
1421 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1422 flags -= XML_RELAXNGP_FREE_DOC;
1423 }
1424 if (flags & XML_RELAXNGP_CRNG) {
1425 ctxt->crng |= XML_RELAXNGP_CRNG;
1426 flags -= XML_RELAXNGP_CRNG;
1427 }
1428 if (flags != 0) return(-1);
1429 return(0);
1430}
1431
1432/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001433 * *
1434 * Document functions *
1435 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001436 ************************************************************************/
1437static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001438 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001439
1440/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001441 * xmlRelaxNGIncludePush:
1442 * @ctxt: the parser context
1443 * @value: the element doc
1444 *
1445 * Pushes a new include on top of the include stack
1446 *
1447 * Returns 0 in case of error, the index in the stack otherwise
1448 */
1449static int
1450xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001451 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001452{
1453 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001454 ctxt->incMax = 4;
1455 ctxt->incNr = 0;
1456 ctxt->incTab =
1457 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1458 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001459 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001460 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001461 return (0);
1462 }
1463 }
1464 if (ctxt->incNr >= ctxt->incMax) {
1465 ctxt->incMax *= 2;
1466 ctxt->incTab =
1467 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001468 ctxt->incMax *
1469 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001470 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001471 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001472 return (0);
1473 }
1474 }
1475 ctxt->incTab[ctxt->incNr] = value;
1476 ctxt->inc = value;
1477 return (ctxt->incNr++);
1478}
1479
1480/**
1481 * xmlRelaxNGIncludePop:
1482 * @ctxt: the parser context
1483 *
1484 * Pops the top include from the include stack
1485 *
1486 * Returns the include just removed
1487 */
1488static xmlRelaxNGIncludePtr
1489xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1490{
1491 xmlRelaxNGIncludePtr ret;
1492
1493 if (ctxt->incNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001494 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001495 ctxt->incNr--;
1496 if (ctxt->incNr > 0)
1497 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1498 else
1499 ctxt->inc = NULL;
1500 ret = ctxt->incTab[ctxt->incNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001501 ctxt->incTab[ctxt->incNr] = NULL;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001502 return (ret);
1503}
1504
1505/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001506 * xmlRelaxNGRemoveRedefine:
1507 * @ctxt: the parser context
1508 * @URL: the normalized URL
1509 * @target: the included target
1510 * @name: the define name to eliminate
1511 *
1512 * Applies the elimination algorithm of 4.7
1513 *
1514 * Returns 0 in case of error, 1 in case of success.
1515 */
1516static int
1517xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001518 const xmlChar * URL ATTRIBUTE_UNUSED,
1519 xmlNodePtr target, const xmlChar * name)
1520{
Daniel Veillard5add8682003-03-10 13:13:58 +00001521 int found = 0;
1522 xmlNodePtr tmp, tmp2;
1523 xmlChar *name2;
1524
1525#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001526 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001527 xmlGenericError(xmlGenericErrorContext,
1528 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001529 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001530 xmlGenericError(xmlGenericErrorContext,
1531 "Elimination of <include> define %s from %s\n",
1532 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001533#endif
1534 tmp = target;
1535 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001536 tmp2 = tmp->next;
1537 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1538 found = 1;
1539 xmlUnlinkNode(tmp);
1540 xmlFreeNode(tmp);
1541 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1542 name2 = xmlGetProp(tmp, BAD_CAST "name");
1543 xmlRelaxNGNormExtSpace(name2);
1544 if (name2 != NULL) {
1545 if (xmlStrEqual(name, name2)) {
1546 found = 1;
1547 xmlUnlinkNode(tmp);
1548 xmlFreeNode(tmp);
1549 }
1550 xmlFree(name2);
1551 }
1552 } else if (IS_RELAXNG(tmp, "include")) {
1553 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001554 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001555
Daniel Veillard4c004142003-10-07 11:33:24 +00001556 if ((inc != NULL) && (inc->doc != NULL) &&
1557 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001558
Daniel Veillard4c004142003-10-07 11:33:24 +00001559 if (xmlStrEqual
1560 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001561#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001562 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001563#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001564 if (xmlRelaxNGRemoveRedefine(ctxt, href,
Shaun McCanced7eb9b52011-08-04 10:28:59 -04001565 xmlDocGetRootElement(inc->doc)->children,
1566 name) == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001567 found = 1;
1568 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001569#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001570 if (href != NULL)
1571 xmlFree(href);
Daniel Veillard14b56432006-03-09 18:41:40 +00001572#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001573 }
1574 }
1575 }
1576 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001577 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001578 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001579}
1580
1581/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001582 * xmlRelaxNGLoadInclude:
1583 * @ctxt: the parser context
1584 * @URL: the normalized URL
1585 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001586 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001587 *
1588 * First lookup if the document is already loaded into the parser context,
1589 * check against recursion. If not found the resource is loaded and
1590 * the content is preprocessed before being returned back to the caller.
1591 *
1592 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1593 */
1594static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001595xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1596 xmlNodePtr node, const xmlChar * ns)
1597{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001598 xmlRelaxNGIncludePtr ret = NULL;
1599 xmlDocPtr doc;
1600 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001601 xmlNodePtr root, cur;
1602
1603#ifdef DEBUG_INCLUDE
1604 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001605 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001606#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001607
1608 /*
1609 * check against recursion in the stack
1610 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001611 for (i = 0; i < ctxt->incNr; i++) {
1612 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1613 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1614 "Detected an Include recursion for %s\n", URL,
1615 NULL);
1616 return (NULL);
1617 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001618 }
1619
1620 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001621 * load the document
1622 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001623 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001624 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001625 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1626 "xmlRelaxNG: could not load %s\n", URL, NULL);
1627 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001628 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001629#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001630 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001631#endif
1632
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001633 /*
1634 * Allocate the document structures and register it first.
1635 */
1636 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1637 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001638 xmlRngPErrMemory(ctxt, "allocating include\n");
1639 xmlFreeDoc(doc);
1640 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001641 }
1642 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1643 ret->doc = doc;
1644 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001645 ret->next = ctxt->includes;
1646 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001647
1648 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001649 * transmit the ns if needed
1650 */
1651 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001652 root = xmlDocGetRootElement(doc);
1653 if (root != NULL) {
1654 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1655 xmlSetProp(root, BAD_CAST "ns", ns);
1656 }
1657 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001658 }
1659
1660 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001661 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001662 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001663 xmlRelaxNGIncludePush(ctxt, ret);
1664
1665 /*
1666 * Some preprocessing of the document content, this include recursing
1667 * in the include stack.
1668 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001669#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001670 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001671#endif
1672
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001673 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1674 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001675 ctxt->inc = NULL;
1676 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001677 }
1678
1679 /*
1680 * Pop up the include from the stack
1681 */
1682 xmlRelaxNGIncludePop(ctxt);
1683
Daniel Veillard5add8682003-03-10 13:13:58 +00001684#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001685 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001686#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001687 /*
1688 * Check that the top element is a grammar
1689 */
1690 root = xmlDocGetRootElement(doc);
1691 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001692 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1693 "xmlRelaxNG: included document is empty %s\n", URL,
1694 NULL);
1695 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001696 }
1697 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001698 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1699 "xmlRelaxNG: included document %s root is not a grammar\n",
1700 URL, NULL);
1701 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001702 }
1703
1704 /*
1705 * Elimination of redefined rules in the include.
1706 */
1707 cur = node->children;
1708 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001709 if (IS_RELAXNG(cur, "start")) {
1710 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001711
Daniel Veillard4c004142003-10-07 11:33:24 +00001712 found =
1713 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1714 if (!found) {
1715 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1716 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1717 URL, NULL);
1718 }
1719 } else if (IS_RELAXNG(cur, "define")) {
1720 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001721
Daniel Veillard4c004142003-10-07 11:33:24 +00001722 name = xmlGetProp(cur, BAD_CAST "name");
1723 if (name == NULL) {
1724 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1725 "xmlRelaxNG: include %s has define without name\n",
1726 URL, NULL);
1727 } else {
1728 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001729
Daniel Veillard4c004142003-10-07 11:33:24 +00001730 xmlRelaxNGNormExtSpace(name);
1731 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1732 root->children, name);
1733 if (!found) {
1734 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1735 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1736 URL, name);
1737 }
1738 xmlFree(name);
1739 }
1740 }
1741 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001742 }
1743
1744
Daniel Veillard4c004142003-10-07 11:33:24 +00001745 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001746}
1747
1748/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001749 * xmlRelaxNGValidErrorPush:
1750 * @ctxt: the validation context
1751 * @err: the error code
1752 * @arg1: the first string argument
1753 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001754 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001755 *
1756 * Pushes a new error on top of the error stack
1757 *
1758 * Returns 0 in case of error, the index in the stack otherwise
1759 */
1760static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001761xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1762 xmlRelaxNGValidErr err, const xmlChar * arg1,
1763 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001764{
1765 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001766
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001767#ifdef DEBUG_ERROR
1768 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001769 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001770#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001771 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001772 ctxt->errMax = 8;
1773 ctxt->errNr = 0;
1774 ctxt->errTab =
1775 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1776 sizeof
1777 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001778 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001779 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001780 return (0);
1781 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001782 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001783 }
1784 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001785 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001786 ctxt->errTab =
1787 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001788 ctxt->errMax *
1789 sizeof
1790 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001791 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001792 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001793 return (0);
1794 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001795 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001796 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001797 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001798 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1799 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001800 cur = &ctxt->errTab[ctxt->errNr];
1801 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001802 if (dup) {
1803 cur->arg1 = xmlStrdup(arg1);
1804 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001805 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001806 } else {
1807 cur->arg1 = arg1;
1808 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001809 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001810 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001811 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001812 cur->node = ctxt->state->node;
1813 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001814 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001815 cur->node = NULL;
1816 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001817 }
1818 ctxt->err = cur;
1819 return (ctxt->errNr++);
1820}
1821
1822/**
1823 * xmlRelaxNGValidErrorPop:
1824 * @ctxt: the validation context
1825 *
1826 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001827 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001828static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001829xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1830{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001831 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001832
Daniel Veillard580ced82003-03-21 21:22:48 +00001833 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001834 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001835 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001836 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001837 ctxt->errNr--;
1838 if (ctxt->errNr > 0)
1839 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1840 else
1841 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001842 cur = &ctxt->errTab[ctxt->errNr];
1843 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001844 if (cur->arg1 != NULL)
1845 xmlFree((xmlChar *) cur->arg1);
1846 cur->arg1 = NULL;
1847 if (cur->arg2 != NULL)
1848 xmlFree((xmlChar *) cur->arg2);
1849 cur->arg2 = NULL;
1850 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001851 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001852}
1853
Daniel Veillard42f12e92003-03-07 18:32:59 +00001854/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001855 * xmlRelaxNGDocumentPush:
1856 * @ctxt: the parser context
1857 * @value: the element doc
1858 *
1859 * Pushes a new doc on top of the doc stack
1860 *
1861 * Returns 0 in case of error, the index in the stack otherwise
1862 */
1863static int
1864xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001865 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001866{
1867 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001868 ctxt->docMax = 4;
1869 ctxt->docNr = 0;
1870 ctxt->docTab =
1871 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1872 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001873 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001874 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001875 return (0);
1876 }
1877 }
1878 if (ctxt->docNr >= ctxt->docMax) {
1879 ctxt->docMax *= 2;
1880 ctxt->docTab =
1881 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001882 ctxt->docMax *
1883 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001884 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001885 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001886 return (0);
1887 }
1888 }
1889 ctxt->docTab[ctxt->docNr] = value;
1890 ctxt->doc = value;
1891 return (ctxt->docNr++);
1892}
1893
1894/**
1895 * xmlRelaxNGDocumentPop:
1896 * @ctxt: the parser context
1897 *
1898 * Pops the top doc from the doc stack
1899 *
1900 * Returns the doc just removed
1901 */
1902static xmlRelaxNGDocumentPtr
1903xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1904{
1905 xmlRelaxNGDocumentPtr ret;
1906
1907 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001908 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001909 ctxt->docNr--;
1910 if (ctxt->docNr > 0)
1911 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1912 else
1913 ctxt->doc = NULL;
1914 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001915 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001916 return (ret);
1917}
1918
1919/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001920 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001921 * @ctxt: the parser context
1922 * @URL: the normalized URL
1923 * @ns: the inherited ns if any
1924 *
1925 * First lookup if the document is already loaded into the parser context,
1926 * check against recursion. If not found the resource is loaded and
1927 * the content is preprocessed before being returned back to the caller.
1928 *
1929 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1930 */
1931static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001932xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1933 const xmlChar * URL, const xmlChar * ns)
1934{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001935 xmlRelaxNGDocumentPtr ret = NULL;
1936 xmlDocPtr doc;
1937 xmlNodePtr root;
1938 int i;
1939
1940 /*
1941 * check against recursion in the stack
1942 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001943 for (i = 0; i < ctxt->docNr; i++) {
1944 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1945 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1946 "Detected an externalRef recursion for %s\n", URL,
1947 NULL);
1948 return (NULL);
1949 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001950 }
1951
1952 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001953 * load the document
1954 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001955 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001956 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001957 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1958 "xmlRelaxNG: could not load %s\n", URL, NULL);
1959 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001960 }
1961
1962 /*
1963 * Allocate the document structures and register it first.
1964 */
1965 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1966 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001967 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1968 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1969 xmlFreeDoc(doc);
1970 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001971 }
1972 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1973 ret->doc = doc;
1974 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001975 ret->next = ctxt->documents;
Daniel Veillard81c51e12009-08-14 18:52:10 +02001976 ret->externalRef = 1;
Daniel Veillardc482e262003-02-26 14:48:48 +00001977 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001978
1979 /*
1980 * transmit the ns if needed
1981 */
1982 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001983 root = xmlDocGetRootElement(doc);
1984 if (root != NULL) {
1985 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1986 xmlSetProp(root, BAD_CAST "ns", ns);
1987 }
1988 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001989 }
1990
1991 /*
1992 * push it on the stack and register it in the hash table
1993 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001994 xmlRelaxNGDocumentPush(ctxt, ret);
1995
1996 /*
1997 * Some preprocessing of the document content
1998 */
1999 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2000 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002001 ctxt->doc = NULL;
2002 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002003 }
2004
2005 xmlRelaxNGDocumentPop(ctxt);
2006
Daniel Veillard4c004142003-10-07 11:33:24 +00002007 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002008}
2009
2010/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002011 * *
2012 * Error functions *
2013 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002014 ************************************************************************/
2015
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002016#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2017#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2018#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2019#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2020#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002021
Daniel Veillard231d7912003-02-09 14:22:17 +00002022static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002023xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2024{
Daniel Veillard231d7912003-02-09 14:22:17 +00002025 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002026 return ("none");
2027 switch (def->type) {
2028 case XML_RELAXNG_EMPTY:
2029 return ("empty");
2030 case XML_RELAXNG_NOT_ALLOWED:
2031 return ("notAllowed");
2032 case XML_RELAXNG_EXCEPT:
2033 return ("except");
2034 case XML_RELAXNG_TEXT:
2035 return ("text");
2036 case XML_RELAXNG_ELEMENT:
2037 return ("element");
2038 case XML_RELAXNG_DATATYPE:
2039 return ("datatype");
2040 case XML_RELAXNG_VALUE:
2041 return ("value");
2042 case XML_RELAXNG_LIST:
2043 return ("list");
2044 case XML_RELAXNG_ATTRIBUTE:
2045 return ("attribute");
2046 case XML_RELAXNG_DEF:
2047 return ("def");
2048 case XML_RELAXNG_REF:
2049 return ("ref");
2050 case XML_RELAXNG_EXTERNALREF:
2051 return ("externalRef");
2052 case XML_RELAXNG_PARENTREF:
2053 return ("parentRef");
2054 case XML_RELAXNG_OPTIONAL:
2055 return ("optional");
2056 case XML_RELAXNG_ZEROORMORE:
2057 return ("zeroOrMore");
2058 case XML_RELAXNG_ONEORMORE:
2059 return ("oneOrMore");
2060 case XML_RELAXNG_CHOICE:
2061 return ("choice");
2062 case XML_RELAXNG_GROUP:
2063 return ("group");
2064 case XML_RELAXNG_INTERLEAVE:
2065 return ("interleave");
2066 case XML_RELAXNG_START:
2067 return ("start");
2068 case XML_RELAXNG_NOOP:
2069 return ("noop");
2070 case XML_RELAXNG_PARAM:
2071 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002072 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002073 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002074}
Daniel Veillardd2298792003-02-14 16:54:11 +00002075
Daniel Veillard6eadf632003-01-23 18:29:16 +00002076/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002077 * xmlRelaxNGGetErrorString:
2078 * @err: the error code
2079 * @arg1: the first string argument
2080 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002081 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002082 * computes a formatted error string for the given error code and args
2083 *
2084 * Returns the error string, it must be deallocated by the caller
2085 */
2086static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002087xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2088 const xmlChar * arg2)
2089{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002090 char msg[1000];
2091
2092 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002093 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002094 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002095 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002096
2097 msg[0] = 0;
2098 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002099 case XML_RELAXNG_OK:
2100 return (NULL);
2101 case XML_RELAXNG_ERR_MEMORY:
2102 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002103 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002104 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2105 break;
2106 case XML_RELAXNG_ERR_TYPEVAL:
2107 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2108 arg2);
2109 break;
2110 case XML_RELAXNG_ERR_DUPID:
2111 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2112 break;
2113 case XML_RELAXNG_ERR_TYPECMP:
2114 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2115 break;
2116 case XML_RELAXNG_ERR_NOSTATE:
2117 return (xmlCharStrdup("Internal error: no state\n"));
2118 case XML_RELAXNG_ERR_NODEFINE:
2119 return (xmlCharStrdup("Internal error: no define\n"));
2120 case XML_RELAXNG_ERR_INTERNAL:
2121 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2122 break;
2123 case XML_RELAXNG_ERR_LISTEXTRA:
2124 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2125 break;
2126 case XML_RELAXNG_ERR_INTERNODATA:
2127 return (xmlCharStrdup
2128 ("Internal: interleave block has no data\n"));
2129 case XML_RELAXNG_ERR_INTERSEQ:
2130 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2131 case XML_RELAXNG_ERR_INTEREXTRA:
2132 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2133 break;
2134 case XML_RELAXNG_ERR_ELEMNAME:
2135 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2136 arg2);
2137 break;
2138 case XML_RELAXNG_ERR_ELEMNONS:
2139 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2140 arg1);
2141 break;
2142 case XML_RELAXNG_ERR_ELEMWRONGNS:
2143 snprintf(msg, 1000,
2144 "Element %s has wrong namespace: expecting %s\n", arg1,
2145 arg2);
2146 break;
2147 case XML_RELAXNG_ERR_ELEMWRONG:
2148 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2149 break;
2150 case XML_RELAXNG_ERR_TEXTWRONG:
2151 snprintf(msg, 1000,
2152 "Did not expect text in element %s content\n", arg1);
2153 break;
2154 case XML_RELAXNG_ERR_ELEMEXTRANS:
2155 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2156 arg1);
2157 break;
2158 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2159 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2160 break;
2161 case XML_RELAXNG_ERR_NOELEM:
2162 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2163 arg1);
2164 break;
2165 case XML_RELAXNG_ERR_NOTELEM:
2166 return (xmlCharStrdup("Expecting an element got text\n"));
2167 case XML_RELAXNG_ERR_ATTRVALID:
2168 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2169 arg1);
2170 break;
2171 case XML_RELAXNG_ERR_CONTENTVALID:
2172 snprintf(msg, 1000, "Element %s failed to validate content\n",
2173 arg1);
2174 break;
2175 case XML_RELAXNG_ERR_EXTRACONTENT:
2176 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2177 arg1, arg2);
2178 break;
2179 case XML_RELAXNG_ERR_INVALIDATTR:
2180 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2181 arg1, arg2);
2182 break;
2183 case XML_RELAXNG_ERR_LACKDATA:
2184 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2185 arg1);
2186 break;
2187 case XML_RELAXNG_ERR_DATAELEM:
2188 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2189 arg1);
2190 break;
2191 case XML_RELAXNG_ERR_VALELEM:
2192 snprintf(msg, 1000, "Value element %s has child elements\n",
2193 arg1);
2194 break;
2195 case XML_RELAXNG_ERR_LISTELEM:
2196 snprintf(msg, 1000, "List element %s has child elements\n",
2197 arg1);
2198 break;
2199 case XML_RELAXNG_ERR_DATATYPE:
2200 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2201 break;
2202 case XML_RELAXNG_ERR_VALUE:
2203 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2204 break;
2205 case XML_RELAXNG_ERR_LIST:
2206 return (xmlCharStrdup("Error validating list\n"));
2207 case XML_RELAXNG_ERR_NOGRAMMAR:
2208 return (xmlCharStrdup("No top grammar defined\n"));
2209 case XML_RELAXNG_ERR_EXTRADATA:
2210 return (xmlCharStrdup("Extra data in the document\n"));
2211 default:
2212 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002213 }
2214 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002215 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002216 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002217 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002218 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002219}
2220
2221/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002222 * xmlRelaxNGShowValidError:
2223 * @ctxt: the validation context
2224 * @err: the error number
2225 * @node: the node
2226 * @child: the node child generating the problem.
2227 * @arg1: the first argument
2228 * @arg2: the second argument
2229 *
2230 * Show a validation error.
2231 */
2232static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002233xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2234 xmlRelaxNGValidErr err, xmlNodePtr node,
2235 xmlNodePtr child, const xmlChar * arg1,
2236 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002237{
2238 xmlChar *msg;
2239
Daniel Veillardb30ca312005-09-04 13:50:03 +00002240 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002241 return;
2242
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002243#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002244 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002245#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002246 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2247 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002248 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002249
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002250 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002251 ctxt->errNo = err;
2252 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2253 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002254 xmlFree(msg);
2255}
2256
2257/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002258 * xmlRelaxNGPopErrors:
2259 * @ctxt: the validation context
2260 * @level: the error level in the stack
2261 *
2262 * pop and discard all errors until the given level is reached
2263 */
2264static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002265xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2266{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002267 int i;
2268 xmlRelaxNGValidErrorPtr err;
2269
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002270#ifdef DEBUG_ERROR
2271 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002272 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002273#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002274 for (i = level; i < ctxt->errNr; i++) {
2275 err = &ctxt->errTab[i];
2276 if (err->flags & ERROR_IS_DUP) {
2277 if (err->arg1 != NULL)
2278 xmlFree((xmlChar *) err->arg1);
2279 err->arg1 = NULL;
2280 if (err->arg2 != NULL)
2281 xmlFree((xmlChar *) err->arg2);
2282 err->arg2 = NULL;
2283 err->flags = 0;
2284 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002285 }
2286 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002287 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002288 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002289}
Daniel Veillard4c004142003-10-07 11:33:24 +00002290
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002291/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002292 * xmlRelaxNGDumpValidError:
2293 * @ctxt: the validation context
2294 *
2295 * Show all validation error over a given index.
2296 */
2297static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002298xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2299{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002300 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002301 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002302
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002303#ifdef DEBUG_ERROR
2304 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002305 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002306#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002307 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2308 err = &ctxt->errTab[i];
2309 if (k < MAX_ERROR) {
2310 for (j = 0; j < i; j++) {
2311 dup = &ctxt->errTab[j];
2312 if ((err->err == dup->err) && (err->node == dup->node) &&
2313 (xmlStrEqual(err->arg1, dup->arg1)) &&
2314 (xmlStrEqual(err->arg2, dup->arg2))) {
2315 goto skip;
2316 }
2317 }
2318 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2319 err->arg1, err->arg2);
2320 k++;
2321 }
2322 skip:
2323 if (err->flags & ERROR_IS_DUP) {
2324 if (err->arg1 != NULL)
2325 xmlFree((xmlChar *) err->arg1);
2326 err->arg1 = NULL;
2327 if (err->arg2 != NULL)
2328 xmlFree((xmlChar *) err->arg2);
2329 err->arg2 = NULL;
2330 err->flags = 0;
2331 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002332 }
2333 ctxt->errNr = 0;
2334}
Daniel Veillard4c004142003-10-07 11:33:24 +00002335
Daniel Veillard42f12e92003-03-07 18:32:59 +00002336/**
2337 * xmlRelaxNGAddValidError:
2338 * @ctxt: the validation context
2339 * @err: the error number
2340 * @arg1: the first argument
2341 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002342 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002343 *
2344 * Register a validation error, either generating it if it's sure
2345 * or stacking it for later handling if unsure.
2346 */
2347static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002348xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2349 xmlRelaxNGValidErr err, const xmlChar * arg1,
2350 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002351{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002352 if (ctxt == NULL)
2353 return;
2354 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002355 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002356
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002357#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002358 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002359#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002360 /*
2361 * generate the error directly
2362 */
William M. Brack60929622004-03-27 17:54:18 +00002363 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002364 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002365 xmlNodePtr node, seq;
2366
2367 /*
2368 * Flush first any stacked error which might be the
2369 * real cause of the problem.
2370 */
2371 if (ctxt->errNr != 0)
2372 xmlRelaxNGDumpValidError(ctxt);
2373 if (ctxt->state != NULL) {
2374 node = ctxt->state->node;
2375 seq = ctxt->state->seq;
2376 } else {
2377 node = seq = NULL;
2378 }
Daniel Veillardec18c962009-08-26 18:37:43 +02002379 if ((node == NULL) && (seq == NULL)) {
2380 node = ctxt->pnode;
2381 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002382 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002383 }
2384 /*
2385 * Stack the error for later processing if needed
2386 */
2387 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002388 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002389 }
2390}
2391
Daniel Veillard6eadf632003-01-23 18:29:16 +00002392
2393/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002394 * *
2395 * Type library hooks *
2396 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002397 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002398static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002399 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002400
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002401/**
2402 * xmlRelaxNGSchemaTypeHave:
2403 * @data: data needed for the library
2404 * @type: the type name
2405 *
2406 * Check if the given type is provided by
2407 * the W3C XMLSchema Datatype library.
2408 *
2409 * Returns 1 if yes, 0 if no and -1 in case of error.
2410 */
2411static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002412xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2413{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002414 xmlSchemaTypePtr typ;
2415
2416 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002417 return (-1);
2418 typ = xmlSchemaGetPredefinedType(type,
2419 BAD_CAST
2420 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002421 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002422 return (0);
2423 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002424}
2425
2426/**
2427 * xmlRelaxNGSchemaTypeCheck:
2428 * @data: data needed for the library
2429 * @type: the type name
2430 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002431 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002432 *
2433 * Check if the given type and value are validated by
2434 * the W3C XMLSchema Datatype library.
2435 *
2436 * Returns 1 if yes, 0 if no and -1 in case of error.
2437 */
2438static int
2439xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002440 const xmlChar * type,
2441 const xmlChar * value,
2442 void **result, xmlNodePtr node)
2443{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002444 xmlSchemaTypePtr typ;
2445 int ret;
2446
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002447 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002448 return (-1);
2449 typ = xmlSchemaGetPredefinedType(type,
2450 BAD_CAST
2451 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002452 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002453 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002454 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002455 (xmlSchemaValPtr *) result, node);
2456 if (ret == 2) /* special ID error code */
2457 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002458 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002459 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002460 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002461 return (0);
2462 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002463}
2464
2465/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002466 * xmlRelaxNGSchemaFacetCheck:
2467 * @data: data needed for the library
2468 * @type: the type name
2469 * @facet: the facet name
2470 * @val: the facet value
2471 * @strval: the string value
2472 * @value: the value to check
2473 *
2474 * Function provided by a type library to check a value facet
2475 *
2476 * Returns 1 if yes, 0 if no and -1 in case of error.
2477 */
2478static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002479xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2480 const xmlChar * type, const xmlChar * facetname,
2481 const xmlChar * val, const xmlChar * strval,
2482 void *value)
2483{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002484 xmlSchemaFacetPtr facet;
2485 xmlSchemaTypePtr typ;
2486 int ret;
2487
2488 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002489 return (-1);
2490 typ = xmlSchemaGetPredefinedType(type,
2491 BAD_CAST
2492 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002493 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002494 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002495
2496 facet = xmlSchemaNewFacet();
2497 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002498 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002499
Daniel Veillard4c004142003-10-07 11:33:24 +00002500 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002501 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002502 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002503 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002504 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002505 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002506 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002507 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002508 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002509 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002510 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002511 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002512 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002513 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002514 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002515 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002516 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002517 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002518 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002519 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002520 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002521 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2522 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2523 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2524 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002525 xmlSchemaFreeFacet(facet);
2526 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002527 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002528 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002529 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2530 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002531 xmlSchemaFreeFacet(facet);
2532 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002533 }
2534 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2535 xmlSchemaFreeFacet(facet);
2536 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002537 return (-1);
2538 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002539}
2540
2541/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002542 * xmlRelaxNGSchemaFreeValue:
2543 * @data: data needed for the library
2544 * @value: the value to free
2545 *
2546 * Function provided by a type library to free a Schemas value
2547 *
2548 * Returns 1 if yes, 0 if no and -1 in case of error.
2549 */
2550static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002551xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2552{
Daniel Veillard80b19092003-03-28 13:29:53 +00002553 xmlSchemaFreeValue(value);
2554}
2555
2556/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002557 * xmlRelaxNGSchemaTypeCompare:
2558 * @data: data needed for the library
2559 * @type: the type name
2560 * @value1: the first value
2561 * @value2: the second value
2562 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002563 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002564 * Datatype library.
2565 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002566 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002567 */
2568static int
2569xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002570 const xmlChar * type,
2571 const xmlChar * value1,
2572 xmlNodePtr ctxt1,
2573 void *comp1,
2574 const xmlChar * value2, xmlNodePtr ctxt2)
2575{
Daniel Veillard80b19092003-03-28 13:29:53 +00002576 int ret;
2577 xmlSchemaTypePtr typ;
2578 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2579
2580 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002581 return (-1);
2582 typ = xmlSchemaGetPredefinedType(type,
2583 BAD_CAST
2584 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002585 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002586 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002587 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002588 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2589 if (ret != 0)
2590 return (-1);
2591 if (res1 == NULL)
2592 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002593 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002594 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002595 }
2596 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002597 if (ret != 0) {
Gaurav7d4e2592013-09-30 11:27:41 +08002598 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillardf4644032005-06-13 11:41:31 +00002599 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002600 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002601 }
Daniel Veillard80b19092003-03-28 13:29:53 +00002602 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002603 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002604 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002605 xmlSchemaFreeValue(res2);
2606 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002607 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002608 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002609 return (1);
2610 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002611}
Daniel Veillard4c004142003-10-07 11:33:24 +00002612
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002613/**
2614 * xmlRelaxNGDefaultTypeHave:
2615 * @data: data needed for the library
2616 * @type: the type name
2617 *
2618 * Check if the given type is provided by
2619 * the default datatype library.
2620 *
2621 * Returns 1 if yes, 0 if no and -1 in case of error.
2622 */
2623static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002624xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2625 const xmlChar * type)
2626{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002627 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002628 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002629 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002630 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002631 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002632 return (1);
2633 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002634}
2635
2636/**
2637 * xmlRelaxNGDefaultTypeCheck:
2638 * @data: data needed for the library
2639 * @type: the type name
2640 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002641 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002642 *
2643 * Check if the given type and value are validated by
2644 * the default datatype library.
2645 *
2646 * Returns 1 if yes, 0 if no and -1 in case of error.
2647 */
2648static int
2649xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002650 const xmlChar * type ATTRIBUTE_UNUSED,
2651 const xmlChar * value ATTRIBUTE_UNUSED,
2652 void **result ATTRIBUTE_UNUSED,
2653 xmlNodePtr node ATTRIBUTE_UNUSED)
2654{
Daniel Veillardd4310742003-02-18 21:12:46 +00002655 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002656 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002657 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002658 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002659 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002660 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002661 }
2662
Daniel Veillard4c004142003-10-07 11:33:24 +00002663 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002664}
2665
2666/**
2667 * xmlRelaxNGDefaultTypeCompare:
2668 * @data: data needed for the library
2669 * @type: the type name
2670 * @value1: the first value
2671 * @value2: the second value
2672 *
2673 * Compare two values accordingly a type from the default
2674 * datatype library.
2675 *
2676 * Returns 1 if yes, 0 if no and -1 in case of error.
2677 */
2678static int
2679xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002680 const xmlChar * type,
2681 const xmlChar * value1,
2682 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2683 void *comp1 ATTRIBUTE_UNUSED,
2684 const xmlChar * value2,
2685 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2686{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002687 int ret = -1;
2688
2689 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002690 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002691 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002692 if (!xmlStrEqual(value1, value2)) {
2693 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002694
Daniel Veillard4c004142003-10-07 11:33:24 +00002695 /*
2696 * TODO: trivial optimizations are possible by
2697 * computing at compile-time
2698 */
2699 nval = xmlRelaxNGNormalize(NULL, value1);
2700 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002701
Daniel Veillard4c004142003-10-07 11:33:24 +00002702 if ((nval == NULL) || (nvalue == NULL))
2703 ret = -1;
2704 else if (xmlStrEqual(nval, nvalue))
2705 ret = 1;
2706 else
2707 ret = 0;
2708 if (nval != NULL)
2709 xmlFree(nval);
2710 if (nvalue != NULL)
2711 xmlFree(nvalue);
2712 } else
2713 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002714 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002715 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002716}
Daniel Veillard4c004142003-10-07 11:33:24 +00002717
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002718static int xmlRelaxNGTypeInitialized = 0;
2719static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2720
2721/**
2722 * xmlRelaxNGFreeTypeLibrary:
2723 * @lib: the type library structure
2724 * @namespace: the URI bound to the library
2725 *
2726 * Free the structure associated to the type library
2727 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002728static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002729xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002730 const xmlChar * namespace ATTRIBUTE_UNUSED)
2731{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002732 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002733 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002734 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002735 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002736 xmlFree(lib);
2737}
2738
2739/**
2740 * xmlRelaxNGRegisterTypeLibrary:
2741 * @namespace: the URI bound to the library
2742 * @data: data associated to the library
2743 * @have: the provide function
2744 * @check: the checking function
2745 * @comp: the comparison function
2746 *
2747 * Register a new type library
2748 *
2749 * Returns 0 in case of success and -1 in case of error.
2750 */
2751static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002752xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2753 xmlRelaxNGTypeHave have,
2754 xmlRelaxNGTypeCheck check,
2755 xmlRelaxNGTypeCompare comp,
2756 xmlRelaxNGFacetCheck facet,
2757 xmlRelaxNGTypeFree freef)
2758{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002759 xmlRelaxNGTypeLibraryPtr lib;
2760 int ret;
2761
2762 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002763 (check == NULL) || (comp == NULL))
2764 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002765 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002766 xmlGenericError(xmlGenericErrorContext,
2767 "Relax-NG types library '%s' already registered\n",
2768 namespace);
2769 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002770 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002771 lib =
2772 (xmlRelaxNGTypeLibraryPtr)
2773 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002774 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002775 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002776 return (-1);
2777 }
2778 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2779 lib->namespace = xmlStrdup(namespace);
2780 lib->data = data;
2781 lib->have = have;
2782 lib->comp = comp;
2783 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002784 lib->facet = facet;
2785 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002786 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2787 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002788 xmlGenericError(xmlGenericErrorContext,
2789 "Relax-NG types library failed to register '%s'\n",
2790 namespace);
2791 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2792 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002793 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002794 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002795}
2796
2797/**
2798 * xmlRelaxNGInitTypes:
2799 *
2800 * Initilize the default type libraries.
2801 *
2802 * Returns 0 in case of success and -1 in case of error.
2803 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002804int
Daniel Veillard4c004142003-10-07 11:33:24 +00002805xmlRelaxNGInitTypes(void)
2806{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002807 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002808 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002809 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2810 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002811 xmlGenericError(xmlGenericErrorContext,
2812 "Failed to allocate sh table for Relax-NG types\n");
2813 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002814 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002815 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2816 "http://www.w3.org/2001/XMLSchema-datatypes",
2817 NULL, xmlRelaxNGSchemaTypeHave,
2818 xmlRelaxNGSchemaTypeCheck,
2819 xmlRelaxNGSchemaTypeCompare,
2820 xmlRelaxNGSchemaFacetCheck,
2821 xmlRelaxNGSchemaFreeValue);
2822 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2823 xmlRelaxNGDefaultTypeHave,
2824 xmlRelaxNGDefaultTypeCheck,
2825 xmlRelaxNGDefaultTypeCompare, NULL,
2826 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002827 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002828 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002829}
2830
2831/**
2832 * xmlRelaxNGCleanupTypes:
2833 *
2834 * Cleanup the default Schemas type library associated to RelaxNG
2835 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002836void
2837xmlRelaxNGCleanupTypes(void)
2838{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002839 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002840 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002841 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002842 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002843 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002844 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002845}
2846
2847/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002848 * *
2849 * Compiling element content into regexp *
2850 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002851 * Sometime the element content can be compiled into a pure regexp, *
2852 * This allows a faster execution and streamability at that level *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002853 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002854 ************************************************************************/
2855
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002856/* from automata.c but not exported */
2857void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2858
2859
Daniel Veillard52b48c72003-04-13 19:53:42 +00002860static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2861 xmlRelaxNGDefinePtr def);
2862
Daniel Veillard952379b2003-03-17 15:37:12 +00002863/**
2864 * xmlRelaxNGIsCompileable:
2865 * @define: the definition to check
2866 *
2867 * Check if a definition is nullable.
2868 *
2869 * Returns 1 if yes, 0 if no and -1 in case of error
2870 */
2871static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002872xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2873{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002874 int ret = -1;
2875
Daniel Veillard952379b2003-03-17 15:37:12 +00002876 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002877 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002878 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002879 if ((def->type != XML_RELAXNG_ELEMENT) &&
2880 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002881 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002882 if ((def->type != XML_RELAXNG_ELEMENT) &&
2883 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002884 return (0);
2885 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002886 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002887 ret = xmlRelaxNGIsCompileable(def->content);
2888 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002889 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002890 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002891 ret = 1;
2892 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002893 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002894 /*
2895 * Check if the element content is compileable
2896 */
2897 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2898 ((def->dflags & IS_COMPILABLE) == 0)) {
2899 xmlRelaxNGDefinePtr list;
2900
2901 list = def->content;
2902 while (list != NULL) {
2903 ret = xmlRelaxNGIsCompileable(list);
2904 if (ret != 1)
2905 break;
2906 list = list->next;
2907 }
William M. Brack60929622004-03-27 17:54:18 +00002908 /*
2909 * Because the routine is recursive, we must guard against
2910 * discovering both COMPILABLE and NOT_COMPILABLE
2911 */
2912 if (ret == 0) {
2913 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002914 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002915 }
2916 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002917 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002918#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002919 if (ret == 1) {
2920 xmlGenericError(xmlGenericErrorContext,
2921 "element content for %s is compilable\n",
2922 def->name);
2923 } else if (ret == 0) {
2924 xmlGenericError(xmlGenericErrorContext,
2925 "element content for %s is not compilable\n",
2926 def->name);
2927 } else {
2928 xmlGenericError(xmlGenericErrorContext,
2929 "Problem in RelaxNGIsCompileable for element %s\n",
2930 def->name);
2931 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002932#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002933 }
2934 /*
2935 * All elements return a compileable status unless they
2936 * are generic like anyName
2937 */
2938 if ((def->nameClass != NULL) || (def->name == NULL))
2939 ret = 0;
2940 else
2941 ret = 1;
2942 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002943 case XML_RELAXNG_REF:
2944 case XML_RELAXNG_EXTERNALREF:
2945 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002946 if (def->depth == -20) {
2947 return (1);
2948 } else {
2949 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002950
Daniel Veillard4c004142003-10-07 11:33:24 +00002951 def->depth = -20;
2952 list = def->content;
2953 while (list != NULL) {
2954 ret = xmlRelaxNGIsCompileable(list);
2955 if (ret != 1)
2956 break;
2957 list = list->next;
2958 }
2959 }
2960 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002961 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002962 case XML_RELAXNG_OPTIONAL:
2963 case XML_RELAXNG_ZEROORMORE:
2964 case XML_RELAXNG_ONEORMORE:
2965 case XML_RELAXNG_CHOICE:
2966 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002967 case XML_RELAXNG_DEF:{
2968 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002969
Daniel Veillard4c004142003-10-07 11:33:24 +00002970 list = def->content;
2971 while (list != NULL) {
2972 ret = xmlRelaxNGIsCompileable(list);
2973 if (ret != 1)
2974 break;
2975 list = list->next;
2976 }
2977 break;
2978 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002979 case XML_RELAXNG_EXCEPT:
2980 case XML_RELAXNG_ATTRIBUTE:
2981 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002982 case XML_RELAXNG_DATATYPE:
2983 case XML_RELAXNG_LIST:
2984 case XML_RELAXNG_PARAM:
2985 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002986 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002987 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002988 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002989 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002990 if (ret == 0)
2991 def->dflags |= IS_NOT_COMPILABLE;
2992 if (ret == 1)
2993 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002994#ifdef DEBUG_COMPILE
2995 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002996 xmlGenericError(xmlGenericErrorContext,
2997 "RelaxNGIsCompileable %s : true\n",
2998 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00002999 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003000 xmlGenericError(xmlGenericErrorContext,
3001 "RelaxNGIsCompileable %s : false\n",
3002 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003003 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003004 xmlGenericError(xmlGenericErrorContext,
3005 "Problem in RelaxNGIsCompileable %s\n",
3006 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003007 }
3008#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003009 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003010}
3011
3012/**
3013 * xmlRelaxNGCompile:
3014 * ctxt: the RelaxNG parser context
3015 * @define: the definition tree to compile
3016 *
3017 * Compile the set of definitions, it works recursively, till the
3018 * element boundaries, where it tries to compile the content if possible
3019 *
3020 * Returns 0 if success and -1 in case of error
3021 */
3022static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003023xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3024{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003025 int ret = 0;
3026 xmlRelaxNGDefinePtr list;
3027
Daniel Veillard4c004142003-10-07 11:33:24 +00003028 if ((ctxt == NULL) || (def == NULL))
3029 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003030
Daniel Veillard4c004142003-10-07 11:33:24 +00003031 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003032 case XML_RELAXNG_START:
3033 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003034 xmlAutomataPtr oldam = ctxt->am;
3035 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003036
3037 def->depth = -25;
3038
Daniel Veillard4c004142003-10-07 11:33:24 +00003039 list = def->content;
3040 ctxt->am = xmlNewAutomata();
3041 if (ctxt->am == NULL)
3042 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003043
3044 /*
3045 * assume identical strings but not same pointer are different
3046 * atoms, needed for non-determinism detection
3047 * That way if 2 elements with the same name are in a choice
3048 * branch the automata is found non-deterministic and
3049 * we fallback to the normal validation which does the right
3050 * thing of exploring both choices.
3051 */
3052 xmlAutomataSetFlags(ctxt->am, 1);
3053
Daniel Veillard4c004142003-10-07 11:33:24 +00003054 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3055 while (list != NULL) {
3056 xmlRelaxNGCompile(ctxt, list);
3057 list = list->next;
3058 }
3059 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Noam9313ae82012-05-15 11:03:46 +08003060 if (xmlAutomataIsDeterminist(ctxt->am))
3061 def->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003062
Daniel Veillard4c004142003-10-07 11:33:24 +00003063 xmlFreeAutomata(ctxt->am);
3064 ctxt->state = oldstate;
3065 ctxt->am = oldam;
3066 }
3067 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003068 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003069 if ((ctxt->am != NULL) && (def->name != NULL)) {
3070 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3071 ctxt->state, NULL,
3072 def->name, def->ns,
3073 def);
3074 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003075 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003076 xmlAutomataPtr oldam = ctxt->am;
3077 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003078
3079 def->depth = -25;
3080
Daniel Veillard4c004142003-10-07 11:33:24 +00003081 list = def->content;
3082 ctxt->am = xmlNewAutomata();
3083 if (ctxt->am == NULL)
3084 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003085 xmlAutomataSetFlags(ctxt->am, 1);
Daniel Veillard4c004142003-10-07 11:33:24 +00003086 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3087 while (list != NULL) {
3088 xmlRelaxNGCompile(ctxt, list);
3089 list = list->next;
3090 }
3091 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3092 def->contModel = xmlAutomataCompile(ctxt->am);
3093 if (!xmlRegexpIsDeterminist(def->contModel)) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003094#ifdef DEBUG_COMPILE
3095 xmlGenericError(xmlGenericErrorContext,
3096 "Content model not determinist %s\n",
3097 def->name);
3098#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003099 /*
3100 * we can only use the automata if it is determinist
3101 */
3102 xmlRegFreeRegexp(def->contModel);
3103 def->contModel = NULL;
3104 }
3105 xmlFreeAutomata(ctxt->am);
3106 ctxt->state = oldstate;
3107 ctxt->am = oldam;
3108 } else {
3109 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003110
Daniel Veillard4c004142003-10-07 11:33:24 +00003111 /*
3112 * we can't build the content model for this element content
3113 * but it still might be possible to build it for some of its
3114 * children, recurse.
3115 */
3116 ret = xmlRelaxNGTryCompile(ctxt, def);
3117 ctxt->am = oldam;
3118 }
3119 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003120 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003121 ret = xmlRelaxNGCompile(ctxt, def->content);
3122 break;
3123 case XML_RELAXNG_OPTIONAL:{
3124 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003125
Daniel Veillardfd780772009-08-26 18:35:29 +02003126 list = def->content;
3127 while (list != NULL) {
3128 xmlRelaxNGCompile(ctxt, list);
3129 list = list->next;
3130 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003131 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3132 break;
3133 }
3134 case XML_RELAXNG_ZEROORMORE:{
3135 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003136
Daniel Veillard4c004142003-10-07 11:33:24 +00003137 ctxt->state =
3138 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3139 oldstate = ctxt->state;
3140 list = def->content;
3141 while (list != NULL) {
3142 xmlRelaxNGCompile(ctxt, list);
3143 list = list->next;
3144 }
3145 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3146 ctxt->state =
3147 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3148 break;
3149 }
3150 case XML_RELAXNG_ONEORMORE:{
3151 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003152
Daniel Veillard4c004142003-10-07 11:33:24 +00003153 list = def->content;
3154 while (list != NULL) {
3155 xmlRelaxNGCompile(ctxt, list);
3156 list = list->next;
3157 }
3158 oldstate = ctxt->state;
3159 list = def->content;
3160 while (list != NULL) {
3161 xmlRelaxNGCompile(ctxt, list);
3162 list = list->next;
3163 }
3164 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3165 ctxt->state =
3166 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3167 break;
3168 }
3169 case XML_RELAXNG_CHOICE:{
3170 xmlAutomataStatePtr target = NULL;
3171 xmlAutomataStatePtr oldstate = ctxt->state;
3172
3173 list = def->content;
3174 while (list != NULL) {
3175 ctxt->state = oldstate;
3176 ret = xmlRelaxNGCompile(ctxt, list);
3177 if (ret != 0)
3178 break;
3179 if (target == NULL)
3180 target = ctxt->state;
3181 else {
3182 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3183 target);
3184 }
3185 list = list->next;
3186 }
3187 ctxt->state = target;
3188
3189 break;
3190 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003191 case XML_RELAXNG_REF:
3192 case XML_RELAXNG_EXTERNALREF:
3193 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003194 case XML_RELAXNG_GROUP:
3195 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003196 list = def->content;
3197 while (list != NULL) {
3198 ret = xmlRelaxNGCompile(ctxt, list);
3199 if (ret != 0)
3200 break;
3201 list = list->next;
3202 }
3203 break;
3204 case XML_RELAXNG_TEXT:{
3205 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003206
Daniel Veillard4c004142003-10-07 11:33:24 +00003207 ctxt->state =
3208 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3209 oldstate = ctxt->state;
3210 xmlRelaxNGCompile(ctxt, def->content);
3211 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3212 ctxt->state, BAD_CAST "#text",
3213 NULL);
3214 ctxt->state =
3215 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3216 break;
3217 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003218 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003219 ctxt->state =
3220 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3221 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003222 case XML_RELAXNG_EXCEPT:
3223 case XML_RELAXNG_ATTRIBUTE:
3224 case XML_RELAXNG_INTERLEAVE:
3225 case XML_RELAXNG_NOT_ALLOWED:
3226 case XML_RELAXNG_DATATYPE:
3227 case XML_RELAXNG_LIST:
3228 case XML_RELAXNG_PARAM:
3229 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003230 /* This should not happen and generate an internal error */
3231 fprintf(stderr, "RNG internal error trying to compile %s\n",
3232 xmlRelaxNGDefName(def));
3233 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003234 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003235 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003236}
3237
3238/**
3239 * xmlRelaxNGTryCompile:
3240 * ctxt: the RelaxNG parser context
3241 * @define: the definition tree to compile
3242 *
3243 * Try to compile the set of definitions, it works recursively,
3244 * possibly ignoring parts which cannot be compiled.
3245 *
3246 * Returns 0 if success and -1 in case of error
3247 */
3248static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003249xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3250{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003251 int ret = 0;
3252 xmlRelaxNGDefinePtr list;
3253
Daniel Veillard4c004142003-10-07 11:33:24 +00003254 if ((ctxt == NULL) || (def == NULL))
3255 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003256
3257 if ((def->type == XML_RELAXNG_START) ||
3258 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003259 ret = xmlRelaxNGIsCompileable(def);
3260 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3261 ctxt->am = NULL;
3262 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003263#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003264 if (ret == 0) {
3265 if (def->type == XML_RELAXNG_START)
3266 xmlGenericError(xmlGenericErrorContext,
3267 "compiled the start\n");
3268 else
3269 xmlGenericError(xmlGenericErrorContext,
3270 "compiled element %s\n", def->name);
3271 } else {
3272 if (def->type == XML_RELAXNG_START)
3273 xmlGenericError(xmlGenericErrorContext,
3274 "failed to compile the start\n");
3275 else
3276 xmlGenericError(xmlGenericErrorContext,
3277 "failed to compile element %s\n",
3278 def->name);
3279 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003280#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003281 return (ret);
3282 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003283 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003284 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003285 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003286 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3287 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003288 case XML_RELAXNG_TEXT:
3289 case XML_RELAXNG_DATATYPE:
3290 case XML_RELAXNG_LIST:
3291 case XML_RELAXNG_PARAM:
3292 case XML_RELAXNG_VALUE:
3293 case XML_RELAXNG_EMPTY:
3294 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003295 ret = 0;
3296 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003297 case XML_RELAXNG_OPTIONAL:
3298 case XML_RELAXNG_ZEROORMORE:
3299 case XML_RELAXNG_ONEORMORE:
3300 case XML_RELAXNG_CHOICE:
3301 case XML_RELAXNG_GROUP:
3302 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003303 case XML_RELAXNG_START:
3304 case XML_RELAXNG_REF:
3305 case XML_RELAXNG_EXTERNALREF:
3306 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003307 list = def->content;
3308 while (list != NULL) {
3309 ret = xmlRelaxNGTryCompile(ctxt, list);
3310 if (ret != 0)
3311 break;
3312 list = list->next;
3313 }
3314 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003315 case XML_RELAXNG_EXCEPT:
3316 case XML_RELAXNG_ATTRIBUTE:
3317 case XML_RELAXNG_INTERLEAVE:
3318 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003319 ret = 0;
3320 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003321 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003322 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003323}
3324
3325/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003326 * *
3327 * Parsing functions *
3328 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003329 ************************************************************************/
3330
Daniel Veillard4c004142003-10-07 11:33:24 +00003331static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3332 ctxt, xmlNodePtr node);
3333static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3334 ctxt, xmlNodePtr node);
3335static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3336 ctxt, xmlNodePtr nodes,
3337 int group);
3338static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3339 ctxt, xmlNodePtr node);
3340static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3341 xmlNodePtr node);
3342static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3343 xmlNodePtr nodes);
3344static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3345 ctxt, xmlNodePtr node,
3346 xmlRelaxNGDefinePtr
3347 def);
3348static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3349 ctxt, xmlNodePtr nodes);
3350static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3351 xmlRelaxNGDefinePtr define,
3352 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003353
3354
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003355#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003356
3357/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003358 * xmlRelaxNGIsNullable:
3359 * @define: the definition to verify
3360 *
3361 * Check if a definition is nullable.
3362 *
3363 * Returns 1 if yes, 0 if no and -1 in case of error
3364 */
3365static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003366xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3367{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003368 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003369
Daniel Veillardfd573f12003-03-16 17:52:32 +00003370 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003371 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003372
Daniel Veillarde063f482003-03-21 16:53:17 +00003373 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003374 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003375 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003376 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003377 switch (define->type) {
3378 case XML_RELAXNG_EMPTY:
3379 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003380 ret = 1;
3381 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003382 case XML_RELAXNG_NOOP:
3383 case XML_RELAXNG_DEF:
3384 case XML_RELAXNG_REF:
3385 case XML_RELAXNG_EXTERNALREF:
3386 case XML_RELAXNG_PARENTREF:
3387 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003388 ret = xmlRelaxNGIsNullable(define->content);
3389 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003390 case XML_RELAXNG_EXCEPT:
3391 case XML_RELAXNG_NOT_ALLOWED:
3392 case XML_RELAXNG_ELEMENT:
3393 case XML_RELAXNG_DATATYPE:
3394 case XML_RELAXNG_PARAM:
3395 case XML_RELAXNG_VALUE:
3396 case XML_RELAXNG_LIST:
3397 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003398 ret = 0;
3399 break;
3400 case XML_RELAXNG_CHOICE:{
3401 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003402
Daniel Veillard4c004142003-10-07 11:33:24 +00003403 while (list != NULL) {
3404 ret = xmlRelaxNGIsNullable(list);
3405 if (ret != 0)
3406 goto done;
3407 list = list->next;
3408 }
3409 ret = 0;
3410 break;
3411 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003412 case XML_RELAXNG_START:
3413 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003414 case XML_RELAXNG_GROUP:{
3415 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003416
Daniel Veillard4c004142003-10-07 11:33:24 +00003417 while (list != NULL) {
3418 ret = xmlRelaxNGIsNullable(list);
3419 if (ret != 1)
3420 goto done;
3421 list = list->next;
3422 }
3423 return (1);
3424 }
3425 default:
3426 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003427 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003428 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003429 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003430 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003431 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003432 define->dflags |= IS_NULLABLE;
3433 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003434}
3435
3436/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003437 * xmlRelaxNGIsBlank:
3438 * @str: a string
3439 *
3440 * Check if a string is ignorable c.f. 4.2. Whitespace
3441 *
3442 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3443 */
3444static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003445xmlRelaxNGIsBlank(xmlChar * str)
3446{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003447 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003448 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003449 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003450 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003451 return (0);
3452 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003453 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003454 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003455}
3456
Daniel Veillard6eadf632003-01-23 18:29:16 +00003457/**
3458 * xmlRelaxNGGetDataTypeLibrary:
3459 * @ctxt: a Relax-NG parser context
3460 * @node: the current data or value element
3461 *
3462 * Applies algorithm from 4.3. datatypeLibrary attribute
3463 *
3464 * Returns the datatypeLibary value or NULL if not found
3465 */
3466static xmlChar *
3467xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003468 xmlNodePtr node)
3469{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003470 xmlChar *ret, *escape;
3471
Daniel Veillardd44b9362009-09-07 12:15:08 +02003472 if (node == NULL)
3473 return(NULL);
3474
Daniel Veillard6eadf632003-01-23 18:29:16 +00003475 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003476 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3477 if (ret != NULL) {
3478 if (ret[0] == 0) {
3479 xmlFree(ret);
3480 return (NULL);
3481 }
3482 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3483 if (escape == NULL) {
3484 return (ret);
3485 }
3486 xmlFree(ret);
3487 return (escape);
3488 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003489 }
3490 node = node->parent;
3491 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003492 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3493 if (ret != NULL) {
3494 if (ret[0] == 0) {
3495 xmlFree(ret);
3496 return (NULL);
3497 }
3498 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3499 if (escape == NULL) {
3500 return (ret);
3501 }
3502 xmlFree(ret);
3503 return (escape);
3504 }
3505 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003506 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003507 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003508}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003509
3510/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003511 * xmlRelaxNGParseValue:
3512 * @ctxt: a Relax-NG parser context
3513 * @node: the data node.
3514 *
3515 * parse the content of a RelaxNG value node.
3516 *
3517 * Returns the definition pointer or NULL in case of error
3518 */
3519static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003520xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3521{
Daniel Veillardedc91922003-01-26 00:52:04 +00003522 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003523 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003524 xmlChar *type;
3525 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003526 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003527
Daniel Veillardfd573f12003-03-16 17:52:32 +00003528 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003529 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003530 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003531 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003532
3533 type = xmlGetProp(node, BAD_CAST "type");
3534 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003535 xmlRelaxNGNormExtSpace(type);
3536 if (xmlValidateNCName(type, 0)) {
3537 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3538 "value type '%s' is not an NCName\n", type, NULL);
3539 }
3540 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3541 if (library == NULL)
3542 library =
3543 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003544
Daniel Veillard4c004142003-10-07 11:33:24 +00003545 def->name = type;
3546 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003547
Daniel Veillard4c004142003-10-07 11:33:24 +00003548 lib = (xmlRelaxNGTypeLibraryPtr)
3549 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3550 if (lib == NULL) {
3551 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3552 "Use of unregistered type library '%s'\n", library,
3553 NULL);
3554 def->data = NULL;
3555 } else {
3556 def->data = lib;
3557 if (lib->have == NULL) {
3558 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3559 "Internal error with type library '%s': no 'have'\n",
3560 library, NULL);
3561 } else {
3562 success = lib->have(lib->data, def->name);
3563 if (success != 1) {
3564 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3565 "Error type '%s' is not exported by type library '%s'\n",
3566 def->name, library);
3567 }
3568 }
3569 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003570 }
3571 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003572 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003573 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003574 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3575 (node->children->next != NULL)) {
3576 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3577 "Expecting a single text value for <value>content\n",
3578 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003579 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003580 def->value = xmlNodeGetContent(node);
3581 if (def->value == NULL) {
3582 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3583 "Element <value> has no content\n", NULL, NULL);
3584 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3585 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003586
Daniel Veillard4c004142003-10-07 11:33:24 +00003587 success =
3588 lib->check(lib->data, def->name, def->value, &val, node);
3589 if (success != 1) {
3590 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3591 "Value '%s' is not acceptable for type '%s'\n",
3592 def->value, def->name);
3593 } else {
3594 if (val != NULL)
3595 def->attrs = val;
3596 }
3597 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003598 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003599 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003600}
3601
3602/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003603 * xmlRelaxNGParseData:
3604 * @ctxt: a Relax-NG parser context
3605 * @node: the data node.
3606 *
3607 * parse the content of a RelaxNG data node.
3608 *
3609 * Returns the definition pointer or NULL in case of error
3610 */
3611static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003612xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3613{
Daniel Veillard14b56432006-03-09 18:41:40 +00003614 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003615 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003616 xmlRelaxNGTypeLibraryPtr lib;
3617 xmlChar *type;
3618 xmlChar *library;
3619 xmlNodePtr content;
3620 int tmp;
3621
3622 type = xmlGetProp(node, BAD_CAST "type");
3623 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003624 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3625 NULL);
3626 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003627 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003628 xmlRelaxNGNormExtSpace(type);
3629 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003630 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3631 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003632 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003633 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3634 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003635 library =
3636 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003637
Daniel Veillardfd573f12003-03-16 17:52:32 +00003638 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003639 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003640 xmlFree(type);
3641 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003642 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003643 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003644 def->name = type;
3645 def->ns = library;
3646
3647 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003648 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003649 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003650 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3651 "Use of unregistered type library '%s'\n", library,
3652 NULL);
3653 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003654 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003655 def->data = lib;
3656 if (lib->have == NULL) {
3657 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3658 "Internal error with type library '%s': no 'have'\n",
3659 library, NULL);
3660 } else {
3661 tmp = lib->have(lib->data, def->name);
3662 if (tmp != 1) {
3663 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3664 "Error type '%s' is not exported by type library '%s'\n",
3665 def->name, library);
3666 } else
3667 if ((xmlStrEqual
3668 (library,
3669 BAD_CAST
3670 "http://www.w3.org/2001/XMLSchema-datatypes"))
3671 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3672 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3673 ctxt->idref = 1;
3674 }
3675 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003676 }
3677 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003678
3679 /*
3680 * Handle optional params
3681 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003682 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003683 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3684 break;
3685 if (xmlStrEqual(library,
3686 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3687 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3688 "Type library '%s' does not allow type parameters\n",
3689 library, NULL);
3690 content = content->next;
3691 while ((content != NULL) &&
3692 (xmlStrEqual(content->name, BAD_CAST "param")))
3693 content = content->next;
3694 } else {
3695 param = xmlRelaxNGNewDefine(ctxt, node);
3696 if (param != NULL) {
3697 param->type = XML_RELAXNG_PARAM;
3698 param->name = xmlGetProp(content, BAD_CAST "name");
3699 if (param->name == NULL) {
3700 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3701 "param has no name\n", NULL, NULL);
3702 }
3703 param->value = xmlNodeGetContent(content);
3704 if (lastparam == NULL) {
3705 def->attrs = lastparam = param;
3706 } else {
3707 lastparam->next = param;
3708 lastparam = param;
3709 }
3710 if (lib != NULL) {
3711 }
3712 }
3713 content = content->next;
3714 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003715 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003716 /*
3717 * Handle optional except
3718 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003719 if ((content != NULL)
3720 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3721 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003722 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003723
Daniel Veillard4c004142003-10-07 11:33:24 +00003724 except = xmlRelaxNGNewDefine(ctxt, node);
3725 if (except == NULL) {
3726 return (def);
3727 }
3728 except->type = XML_RELAXNG_EXCEPT;
3729 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003730 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003731 if (child == NULL) {
3732 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3733 "except has no content\n", NULL, NULL);
3734 }
3735 while (child != NULL) {
3736 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3737 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003738 if (last == NULL) {
3739 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003740 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003741 last->next = tmp2;
3742 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003743 }
3744 }
3745 child = child->next;
3746 }
3747 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003748 }
3749 /*
3750 * Check there is no unhandled data
3751 */
3752 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003753 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3754 "Element data has unexpected content %s\n",
3755 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003756 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003757
Daniel Veillard4c004142003-10-07 11:33:24 +00003758 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003759}
3760
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003761static const xmlChar *invalidName = BAD_CAST "\1";
3762
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003763/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003764 * xmlRelaxNGCompareNameClasses:
3765 * @defs1: the first element/attribute defs
3766 * @defs2: the second element/attribute defs
3767 * @name: the restriction on the name
3768 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003769 *
3770 * Compare the 2 lists of element definitions. The comparison is
3771 * that if both lists do not accept the same QNames, it returns 1
3772 * If the 2 lists can accept the same QName the comparison returns 0
3773 *
3774 * Returns 1 disttinct, 0 if equal
3775 */
3776static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003777xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003778 xmlRelaxNGDefinePtr def2)
3779{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003780 int ret = 1;
3781 xmlNode node;
3782 xmlNs ns;
3783 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003784
Daniel Veillard42f12e92003-03-07 18:32:59 +00003785 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3786
Daniel Veillardb30ca312005-09-04 13:50:03 +00003787 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3788
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003789 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003790 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3791 if (def2->type == XML_RELAXNG_TEXT)
3792 return (1);
3793 if (def1->name != NULL) {
3794 node.name = def1->name;
3795 } else {
3796 node.name = invalidName;
3797 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003798 if (def1->ns != NULL) {
3799 if (def1->ns[0] == 0) {
3800 node.ns = NULL;
3801 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003802 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003803 ns.href = def1->ns;
3804 }
3805 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003806 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003807 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003808 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003809 if (def1->nameClass != NULL) {
3810 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3811 } else {
3812 ret = 0;
3813 }
3814 } else {
3815 ret = 1;
3816 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003817 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003818 if (def2->type == XML_RELAXNG_TEXT)
3819 return (0);
3820 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003821 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard2fab2352015-03-16 08:38:36 +08003822 ret = xmlRelaxNGCompareNameClasses(def1->content, def2);
3823 if (ret == 0)
3824 ret = 1;
3825 else if (ret == 1)
3826 ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003827 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003828 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003829 }
3830 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003831 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003832 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003833 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3834 if (def2->name != NULL) {
3835 node.name = def2->name;
3836 } else {
3837 node.name = invalidName;
3838 }
3839 node.ns = &ns;
3840 if (def2->ns != NULL) {
3841 if (def2->ns[0] == 0) {
3842 node.ns = NULL;
3843 } else {
3844 ns.href = def2->ns;
3845 }
3846 } else {
3847 ns.href = invalidName;
3848 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003849 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003850 if (def2->nameClass != NULL) {
3851 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3852 } else {
3853 ret = 0;
3854 }
3855 } else {
3856 ret = 1;
3857 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003858 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003859 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003860 }
3861
Daniel Veillard4c004142003-10-07 11:33:24 +00003862 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003863}
3864
3865/**
3866 * xmlRelaxNGCompareElemDefLists:
3867 * @ctxt: a Relax-NG parser context
3868 * @defs1: the first list of element/attribute defs
3869 * @defs2: the second list of element/attribute defs
3870 *
3871 * Compare the 2 lists of element or attribute definitions. The comparison
3872 * is that if both lists do not accept the same QNames, it returns 1
3873 * If the 2 lists can accept the same QName the comparison returns 0
3874 *
3875 * Returns 1 disttinct, 0 if equal
3876 */
3877static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003878xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3879 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3880 xmlRelaxNGDefinePtr * def2)
3881{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003882 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003883
Daniel Veillard154877e2003-01-30 12:17:05 +00003884 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003885 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003886 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003887 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003888 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003889 while ((*def2) != NULL) {
3890 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3891 return (0);
3892 def2++;
3893 }
3894 def2 = basedef2;
3895 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003896 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003897 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003898}
3899
3900/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003901 * xmlRelaxNGGenerateAttributes:
3902 * @ctxt: a Relax-NG parser context
3903 * @def: the definition definition
3904 *
3905 * Check if the definition can only generate attributes
3906 *
3907 * Returns 1 if yes, 0 if no and -1 in case of error.
3908 */
3909static int
3910xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003911 xmlRelaxNGDefinePtr def)
3912{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003913 xmlRelaxNGDefinePtr parent, cur, tmp;
3914
3915 /*
3916 * Don't run that check in case of error. Infinite recursion
3917 * becomes possible.
3918 */
3919 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003920 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003921
3922 parent = NULL;
3923 cur = def;
3924 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003925 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3926 (cur->type == XML_RELAXNG_TEXT) ||
3927 (cur->type == XML_RELAXNG_DATATYPE) ||
3928 (cur->type == XML_RELAXNG_PARAM) ||
3929 (cur->type == XML_RELAXNG_LIST) ||
3930 (cur->type == XML_RELAXNG_VALUE) ||
3931 (cur->type == XML_RELAXNG_EMPTY))
3932 return (0);
3933 if ((cur->type == XML_RELAXNG_CHOICE) ||
3934 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3935 (cur->type == XML_RELAXNG_GROUP) ||
3936 (cur->type == XML_RELAXNG_ONEORMORE) ||
3937 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3938 (cur->type == XML_RELAXNG_OPTIONAL) ||
3939 (cur->type == XML_RELAXNG_PARENTREF) ||
3940 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3941 (cur->type == XML_RELAXNG_REF) ||
3942 (cur->type == XML_RELAXNG_DEF)) {
3943 if (cur->content != NULL) {
3944 parent = cur;
3945 cur = cur->content;
3946 tmp = cur;
3947 while (tmp != NULL) {
3948 tmp->parent = parent;
3949 tmp = tmp->next;
3950 }
3951 continue;
3952 }
3953 }
3954 if (cur == def)
3955 break;
3956 if (cur->next != NULL) {
3957 cur = cur->next;
3958 continue;
3959 }
3960 do {
3961 cur = cur->parent;
3962 if (cur == NULL)
3963 break;
3964 if (cur == def)
3965 return (1);
3966 if (cur->next != NULL) {
3967 cur = cur->next;
3968 break;
3969 }
3970 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003971 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003972 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003973}
Daniel Veillard4c004142003-10-07 11:33:24 +00003974
Daniel Veillardce192eb2003-04-16 15:58:05 +00003975/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003976 * xmlRelaxNGGetElements:
3977 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003978 * @def: the definition definition
3979 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003980 *
3981 * Compute the list of top elements a definition can generate
3982 *
3983 * Returns a list of elements or NULL if none was found.
3984 */
3985static xmlRelaxNGDefinePtr *
3986xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003987 xmlRelaxNGDefinePtr def, int eora)
3988{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003989 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003990 int len = 0;
3991 int max = 0;
3992
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003993 /*
3994 * Don't run that check in case of error. Infinite recursion
3995 * becomes possible.
3996 */
3997 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003998 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003999
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004000 parent = NULL;
4001 cur = def;
4002 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004003 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4004 (cur->type == XML_RELAXNG_TEXT))) ||
4005 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
4006 if (ret == NULL) {
4007 max = 10;
4008 ret = (xmlRelaxNGDefinePtr *)
4009 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4010 if (ret == NULL) {
4011 xmlRngPErrMemory(ctxt, "getting element list\n");
4012 return (NULL);
4013 }
4014 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00004015 xmlRelaxNGDefinePtr *temp;
4016
Daniel Veillard4c004142003-10-07 11:33:24 +00004017 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00004018 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00004019 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00004020 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004021 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00004022 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00004023 return (NULL);
4024 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00004025 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00004026 }
4027 ret[len++] = cur;
4028 ret[len] = NULL;
4029 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4030 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4031 (cur->type == XML_RELAXNG_GROUP) ||
4032 (cur->type == XML_RELAXNG_ONEORMORE) ||
4033 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4034 (cur->type == XML_RELAXNG_OPTIONAL) ||
4035 (cur->type == XML_RELAXNG_PARENTREF) ||
4036 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004037 (cur->type == XML_RELAXNG_DEF) ||
4038 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004039 /*
4040 * Don't go within elements or attributes or string values.
4041 * Just gather the element top list
4042 */
4043 if (cur->content != NULL) {
4044 parent = cur;
4045 cur = cur->content;
4046 tmp = cur;
4047 while (tmp != NULL) {
4048 tmp->parent = parent;
4049 tmp = tmp->next;
4050 }
4051 continue;
4052 }
4053 }
4054 if (cur == def)
4055 break;
4056 if (cur->next != NULL) {
4057 cur = cur->next;
4058 continue;
4059 }
4060 do {
4061 cur = cur->parent;
4062 if (cur == NULL)
4063 break;
4064 if (cur == def)
4065 return (ret);
4066 if (cur->next != NULL) {
4067 cur = cur->next;
4068 break;
4069 }
4070 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004071 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004072 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004073}
Daniel Veillard4c004142003-10-07 11:33:24 +00004074
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004075/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004076 * xmlRelaxNGCheckChoiceDeterminism:
4077 * @ctxt: a Relax-NG parser context
4078 * @def: the choice definition
4079 *
4080 * Also used to find indeterministic pattern in choice
4081 */
4082static void
4083xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004084 xmlRelaxNGDefinePtr def)
4085{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004086 xmlRelaxNGDefinePtr **list;
4087 xmlRelaxNGDefinePtr cur;
4088 int nbchild = 0, i, j, ret;
4089 int is_nullable = 0;
4090 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004091 xmlHashTablePtr triage = NULL;
4092 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004093
Daniel Veillard4c004142003-10-07 11:33:24 +00004094 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4095 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004096
Daniel Veillarde063f482003-03-21 16:53:17 +00004097 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004098 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004099
Daniel Veillardfd573f12003-03-16 17:52:32 +00004100 /*
4101 * Don't run that check in case of error. Infinite recursion
4102 * becomes possible.
4103 */
4104 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004105 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004106
4107 is_nullable = xmlRelaxNGIsNullable(def);
4108
4109 cur = def->content;
4110 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004111 nbchild++;
4112 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004113 }
4114
4115 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004116 sizeof(xmlRelaxNGDefinePtr
4117 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004118 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004119 xmlRngPErrMemory(ctxt, "building choice\n");
4120 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004121 }
4122 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004123 /*
4124 * a bit strong but safe
4125 */
4126 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004127 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004128 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004129 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004130 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004131 cur = def->content;
4132 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004133 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4134 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4135 is_triable = 0;
4136 } else if (is_triable == 1) {
4137 xmlRelaxNGDefinePtr *tmp;
4138 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004139
Daniel Veillard4c004142003-10-07 11:33:24 +00004140 tmp = list[i];
4141 while ((*tmp != NULL) && (is_triable == 1)) {
4142 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4143 res = xmlHashAddEntry2(triage,
4144 BAD_CAST "#text", NULL,
4145 (void *) cur);
4146 if (res != 0)
4147 is_triable = -1;
4148 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4149 ((*tmp)->name != NULL)) {
4150 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4151 res = xmlHashAddEntry2(triage,
4152 (*tmp)->name, NULL,
4153 (void *) cur);
4154 else
4155 res = xmlHashAddEntry2(triage,
4156 (*tmp)->name, (*tmp)->ns,
4157 (void *) cur);
4158 if (res != 0)
4159 is_triable = -1;
4160 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4161 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4162 res = xmlHashAddEntry2(triage,
4163 BAD_CAST "#any", NULL,
4164 (void *) cur);
4165 else
4166 res = xmlHashAddEntry2(triage,
4167 BAD_CAST "#any", (*tmp)->ns,
4168 (void *) cur);
4169 if (res != 0)
4170 is_triable = -1;
4171 } else {
4172 is_triable = -1;
4173 }
4174 tmp++;
4175 }
4176 }
4177 i++;
4178 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004179 }
4180
Daniel Veillard4c004142003-10-07 11:33:24 +00004181 for (i = 0; i < nbchild; i++) {
4182 if (list[i] == NULL)
4183 continue;
4184 for (j = 0; j < i; j++) {
4185 if (list[j] == NULL)
4186 continue;
4187 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4188 if (ret == 0) {
4189 is_indeterminist = 1;
4190 }
4191 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004192 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004193 for (i = 0; i < nbchild; i++) {
4194 if (list[i] != NULL)
4195 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004196 }
4197
4198 xmlFree(list);
4199 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004200 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004201 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004202 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004203 def->dflags |= IS_TRIABLE;
4204 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004205 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004206 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004207 }
4208 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004209}
4210
4211/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004212 * xmlRelaxNGCheckGroupAttrs:
4213 * @ctxt: a Relax-NG parser context
4214 * @def: the group definition
4215 *
4216 * Detects violations of rule 7.3
4217 */
4218static void
4219xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004220 xmlRelaxNGDefinePtr def)
4221{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004222 xmlRelaxNGDefinePtr **list;
4223 xmlRelaxNGDefinePtr cur;
4224 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004225
4226 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004227 ((def->type != XML_RELAXNG_GROUP) &&
4228 (def->type != XML_RELAXNG_ELEMENT)))
4229 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004230
Daniel Veillarde063f482003-03-21 16:53:17 +00004231 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004232 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004233
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004234 /*
4235 * Don't run that check in case of error. Infinite recursion
4236 * becomes possible.
4237 */
4238 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004239 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004240
Daniel Veillardfd573f12003-03-16 17:52:32 +00004241 cur = def->attrs;
4242 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004243 nbchild++;
4244 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004245 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004246 cur = def->content;
4247 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004248 nbchild++;
4249 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004250 }
4251
4252 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004253 sizeof(xmlRelaxNGDefinePtr
4254 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004255 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004256 xmlRngPErrMemory(ctxt, "building group\n");
4257 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004258 }
4259 i = 0;
4260 cur = def->attrs;
4261 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004262 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4263 i++;
4264 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004265 }
4266 cur = def->content;
4267 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004268 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4269 i++;
4270 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004271 }
4272
Daniel Veillard4c004142003-10-07 11:33:24 +00004273 for (i = 0; i < nbchild; i++) {
4274 if (list[i] == NULL)
4275 continue;
4276 for (j = 0; j < i; j++) {
4277 if (list[j] == NULL)
4278 continue;
4279 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4280 if (ret == 0) {
4281 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4282 "Attributes conflicts in group\n", NULL, NULL);
4283 }
4284 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004285 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004286 for (i = 0; i < nbchild; i++) {
4287 if (list[i] != NULL)
4288 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004289 }
4290
4291 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004292 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004293}
4294
4295/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004296 * xmlRelaxNGComputeInterleaves:
4297 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004298 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004299 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004300 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004301 * A lot of work for preprocessing interleave definitions
4302 * is potentially needed to get a decent execution speed at runtime
4303 * - trying to get a total order on the element nodes generated
4304 * by the interleaves, order the list of interleave definitions
4305 * following that order.
4306 * - if <text/> is used to handle mixed content, it is better to
4307 * flag this in the define and simplify the runtime checking
4308 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004309 */
4310static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004311xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004312 xmlRelaxNGParserCtxtPtr ctxt,
4313 xmlChar * name ATTRIBUTE_UNUSED)
4314{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004315 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004316
Daniel Veillardfd573f12003-03-16 17:52:32 +00004317 xmlRelaxNGPartitionPtr partitions = NULL;
4318 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4319 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004320 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004321 int nbgroups = 0;
4322 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004323 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004324 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004325
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004326 /*
4327 * Don't run that check in case of error. Infinite recursion
4328 * becomes possible.
4329 */
4330 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004331 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004332
Daniel Veillardfd573f12003-03-16 17:52:32 +00004333#ifdef DEBUG_INTERLEAVE
4334 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004335 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004336#endif
4337 cur = def->content;
4338 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004339 nbchild++;
4340 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004341 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004342
Daniel Veillardfd573f12003-03-16 17:52:32 +00004343#ifdef DEBUG_INTERLEAVE
4344 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4345#endif
4346 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004347 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004348 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004349 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004350 cur = def->content;
4351 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004352 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4353 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4354 if (groups[nbgroups] == NULL)
4355 goto error;
4356 if (cur->type == XML_RELAXNG_TEXT)
4357 is_mixed++;
4358 groups[nbgroups]->rule = cur;
4359 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4360 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4361 nbgroups++;
4362 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004363 }
4364#ifdef DEBUG_INTERLEAVE
4365 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4366#endif
4367
4368 /*
4369 * Let's check that all rules makes a partitions according to 7.4
4370 */
4371 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004372 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004373 if (partitions == NULL)
4374 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004375 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004376 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004377 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004378 for (i = 0; i < nbgroups; i++) {
4379 group = groups[i];
4380 for (j = i + 1; j < nbgroups; j++) {
4381 if (groups[j] == NULL)
4382 continue;
4383
4384 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4385 groups[j]->defs);
4386 if (ret == 0) {
4387 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4388 "Element or text conflicts in interleave\n",
4389 NULL, NULL);
4390 }
4391 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4392 groups[j]->attrs);
4393 if (ret == 0) {
4394 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4395 "Attributes conflicts in interleave\n", NULL,
4396 NULL);
4397 }
4398 }
4399 tmp = group->defs;
4400 if ((tmp != NULL) && (*tmp != NULL)) {
4401 while (*tmp != NULL) {
4402 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4403 res = xmlHashAddEntry2(partitions->triage,
4404 BAD_CAST "#text", NULL,
4405 (void *) (long) (i + 1));
4406 if (res != 0)
4407 is_determinist = -1;
4408 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4409 ((*tmp)->name != NULL)) {
4410 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4411 res = xmlHashAddEntry2(partitions->triage,
4412 (*tmp)->name, NULL,
4413 (void *) (long) (i + 1));
4414 else
4415 res = xmlHashAddEntry2(partitions->triage,
4416 (*tmp)->name, (*tmp)->ns,
4417 (void *) (long) (i + 1));
4418 if (res != 0)
4419 is_determinist = -1;
4420 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4421 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4422 res = xmlHashAddEntry2(partitions->triage,
4423 BAD_CAST "#any", NULL,
4424 (void *) (long) (i + 1));
4425 else
4426 res = xmlHashAddEntry2(partitions->triage,
4427 BAD_CAST "#any", (*tmp)->ns,
4428 (void *) (long) (i + 1));
4429 if ((*tmp)->nameClass != NULL)
4430 is_determinist = 2;
4431 if (res != 0)
4432 is_determinist = -1;
4433 } else {
4434 is_determinist = -1;
4435 }
4436 tmp++;
4437 }
4438 } else {
4439 is_determinist = 0;
4440 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004441 }
4442 partitions->groups = groups;
4443
4444 /*
4445 * and save the partition list back in the def
4446 */
4447 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004448 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004449 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004450 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004451 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004452 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004453 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004454 return;
4455
Daniel Veillard4c004142003-10-07 11:33:24 +00004456 error:
4457 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004458 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004459 for (i = 0; i < nbgroups; i++)
4460 if (groups[i] != NULL) {
4461 if (groups[i]->defs != NULL)
4462 xmlFree(groups[i]->defs);
4463 xmlFree(groups[i]);
4464 }
4465 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004466 }
4467 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004468}
4469
4470/**
4471 * xmlRelaxNGParseInterleave:
4472 * @ctxt: a Relax-NG parser context
4473 * @node: the data node.
4474 *
4475 * parse the content of a RelaxNG interleave node.
4476 *
4477 * Returns the definition pointer or NULL in case of error
4478 */
4479static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004480xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4481{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004482 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004483 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004484 xmlNodePtr child;
4485
Daniel Veillardfd573f12003-03-16 17:52:32 +00004486 def = xmlRelaxNGNewDefine(ctxt, node);
4487 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004488 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004489 }
4490 def->type = XML_RELAXNG_INTERLEAVE;
4491
4492 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004493 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004494 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004495 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004496 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004497 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004498
Daniel Veillard4c004142003-10-07 11:33:24 +00004499 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4500 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4501 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4502 "Failed to add %s to hash table\n",
4503 (const xmlChar *) name, NULL);
4504 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004505 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004506 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004507 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004508 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4509 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004510 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004511 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004512 if (IS_RELAXNG(child, "element")) {
4513 cur = xmlRelaxNGParseElement(ctxt, child);
4514 } else {
4515 cur = xmlRelaxNGParsePattern(ctxt, child);
4516 }
4517 if (cur != NULL) {
4518 cur->parent = def;
4519 if (last == NULL) {
4520 def->content = last = cur;
4521 } else {
4522 last->next = cur;
4523 last = cur;
4524 }
4525 }
4526 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004527 }
4528
Daniel Veillard4c004142003-10-07 11:33:24 +00004529 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004530}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004531
4532/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004533 * xmlRelaxNGParseInclude:
4534 * @ctxt: a Relax-NG parser context
4535 * @node: the include node
4536 *
4537 * Integrate the content of an include node in the current grammar
4538 *
4539 * Returns 0 in case of success or -1 in case of error
4540 */
4541static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004542xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4543{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004544 xmlRelaxNGIncludePtr incl;
4545 xmlNodePtr root;
4546 int ret = 0, tmp;
4547
Daniel Veillard807daf82004-02-22 22:13:27 +00004548 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004549 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004550 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4551 "Include node has no data\n", NULL, NULL);
4552 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004553 }
4554 root = xmlDocGetRootElement(incl->doc);
4555 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004556 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4557 NULL, NULL);
4558 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004559 }
4560 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004561 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4562 "Include document root is not a grammar\n", NULL, NULL);
4563 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004564 }
4565
4566 /*
4567 * Merge the definition from both the include and the internal list
4568 */
4569 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004570 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4571 if (tmp != 0)
4572 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004573 }
4574 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004575 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4576 if (tmp != 0)
4577 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004578 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004579 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004580}
4581
4582/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004583 * xmlRelaxNGParseDefine:
4584 * @ctxt: a Relax-NG parser context
4585 * @node: the define node
4586 *
4587 * parse the content of a RelaxNG define element node.
4588 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004589 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004590 */
4591static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004592xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4593{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004594 xmlChar *name;
4595 int ret = 0, tmp;
4596 xmlRelaxNGDefinePtr def;
4597 const xmlChar *olddefine;
4598
4599 name = xmlGetProp(node, BAD_CAST "name");
4600 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004601 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4602 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004603 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004604 xmlRelaxNGNormExtSpace(name);
4605 if (xmlValidateNCName(name, 0)) {
4606 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4607 "define name '%s' is not an NCName\n", name, NULL);
4608 }
4609 def = xmlRelaxNGNewDefine(ctxt, node);
4610 if (def == NULL) {
4611 xmlFree(name);
4612 return (-1);
4613 }
4614 def->type = XML_RELAXNG_DEF;
4615 def->name = name;
4616 if (node->children == NULL) {
4617 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4618 "define has no children\n", NULL, NULL);
4619 } else {
4620 olddefine = ctxt->define;
4621 ctxt->define = name;
4622 def->content =
4623 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4624 ctxt->define = olddefine;
4625 }
4626 if (ctxt->grammar->defs == NULL)
4627 ctxt->grammar->defs = xmlHashCreate(10);
4628 if (ctxt->grammar->defs == NULL) {
4629 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4630 "Could not create definition hash\n", NULL, NULL);
4631 ret = -1;
4632 } else {
4633 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4634 if (tmp < 0) {
4635 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004636
Daniel Veillard4c004142003-10-07 11:33:24 +00004637 prev = xmlHashLookup(ctxt->grammar->defs, name);
4638 if (prev == NULL) {
4639 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4640 "Internal error on define aggregation of %s\n",
4641 name, NULL);
4642 ret = -1;
4643 } else {
4644 while (prev->nextHash != NULL)
4645 prev = prev->nextHash;
4646 prev->nextHash = def;
4647 }
4648 }
4649 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004650 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004651 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004652}
4653
4654/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004655 * xmlRelaxNGParseImportRef:
4656 * @payload: the parser context
4657 * @data: the current grammar
4658 * @name: the reference name
4659 *
4660 * Import import one references into the current grammar
4661 */
4662static void
4663xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4664 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4665 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4666 int tmp;
4667
Daniel Veillardaa422d92009-09-24 11:31:48 +02004668 def->dflags |= IS_EXTERNAL_REF;
4669
Daniel Veillard81c51e12009-08-14 18:52:10 +02004670 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4671 if (tmp < 0) {
4672 xmlRelaxNGDefinePtr prev;
4673
4674 prev = (xmlRelaxNGDefinePtr)
4675 xmlHashLookup(ctxt->grammar->refs, def->name);
4676 if (prev == NULL) {
4677 if (def->name != NULL) {
4678 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4679 "Error refs definitions '%s'\n",
4680 def->name, NULL);
4681 } else {
4682 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4683 "Error refs definitions\n",
4684 NULL, NULL);
4685 }
4686 } else {
4687 def->nextHash = prev->nextHash;
4688 prev->nextHash = def;
4689 }
4690 }
4691}
4692
4693/**
4694 * xmlRelaxNGParseImportRefs:
4695 * @ctxt: the parser context
4696 * @grammar: the sub grammar
4697 *
4698 * Import references from the subgrammar into the current grammar
4699 *
4700 * Returns 0 in case of success, -1 in case of failure
4701 */
4702static int
4703xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4704 xmlRelaxNGGrammarPtr grammar) {
4705 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4706 return(-1);
4707 if (grammar->refs == NULL)
4708 return(0);
4709 if (ctxt->grammar->refs == NULL)
4710 ctxt->grammar->refs = xmlHashCreate(10);
4711 if (ctxt->grammar->refs == NULL) {
4712 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4713 "Could not create references hash\n", NULL, NULL);
4714 return(-1);
4715 }
4716 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004717 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004718}
4719
4720/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004721 * xmlRelaxNGProcessExternalRef:
4722 * @ctxt: the parser context
4723 * @node: the externlRef node
4724 *
4725 * Process and compile an externlRef node
4726 *
4727 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4728 */
4729static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004730xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4731{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004732 xmlRelaxNGDocumentPtr docu;
4733 xmlNodePtr root, tmp;
4734 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004735 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004736 xmlRelaxNGDefinePtr def;
4737
Daniel Veillard807daf82004-02-22 22:13:27 +00004738 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004739 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004740 def = xmlRelaxNGNewDefine(ctxt, node);
4741 if (def == NULL)
4742 return (NULL);
4743 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004744
Daniel Veillard4c004142003-10-07 11:33:24 +00004745 if (docu->content == NULL) {
4746 /*
4747 * Then do the parsing for good
4748 */
4749 root = xmlDocGetRootElement(docu->doc);
4750 if (root == NULL) {
4751 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4752 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4753 NULL);
4754 return (NULL);
4755 }
4756 /*
4757 * ns transmission rules
4758 */
4759 ns = xmlGetProp(root, BAD_CAST "ns");
4760 if (ns == NULL) {
4761 tmp = node;
4762 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4763 ns = xmlGetProp(tmp, BAD_CAST "ns");
4764 if (ns != NULL) {
4765 break;
4766 }
4767 tmp = tmp->parent;
4768 }
4769 if (ns != NULL) {
4770 xmlSetProp(root, BAD_CAST "ns", ns);
4771 newNs = 1;
4772 xmlFree(ns);
4773 }
4774 } else {
4775 xmlFree(ns);
4776 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004777
Daniel Veillard4c004142003-10-07 11:33:24 +00004778 /*
4779 * Parsing to get a precompiled schemas.
4780 */
4781 oldflags = ctxt->flags;
4782 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4783 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4784 ctxt->flags = oldflags;
4785 if ((docu->schema != NULL) &&
4786 (docu->schema->topgrammar != NULL)) {
4787 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004788 if (docu->schema->topgrammar->refs)
4789 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004790 }
4791
4792 /*
4793 * the externalRef may be reused in a different ns context
4794 */
4795 if (newNs == 1) {
4796 xmlUnsetProp(root, BAD_CAST "ns");
4797 }
4798 }
4799 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004800 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004801 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004802 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004803 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004804}
4805
4806/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004807 * xmlRelaxNGParsePattern:
4808 * @ctxt: a Relax-NG parser context
4809 * @node: the pattern node.
4810 *
4811 * parse the content of a RelaxNG pattern node.
4812 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004813 * Returns the definition pointer or NULL in case of error or if no
4814 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004815 */
4816static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004817xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4818{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004819 xmlRelaxNGDefinePtr def = NULL;
4820
Daniel Veillardd2298792003-02-14 16:54:11 +00004821 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004822 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004823 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004824 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004825 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004826 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004827 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004828 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004829 def = xmlRelaxNGNewDefine(ctxt, node);
4830 if (def == NULL)
4831 return (NULL);
4832 def->type = XML_RELAXNG_EMPTY;
4833 if (node->children != NULL) {
4834 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4835 "empty: had a child node\n", NULL, NULL);
4836 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004837 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004838 def = xmlRelaxNGNewDefine(ctxt, node);
4839 if (def == NULL)
4840 return (NULL);
4841 def->type = XML_RELAXNG_TEXT;
4842 if (node->children != NULL) {
4843 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4844 "text: had a child node\n", NULL, NULL);
4845 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004846 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004847 def = xmlRelaxNGNewDefine(ctxt, node);
4848 if (def == NULL)
4849 return (NULL);
4850 def->type = XML_RELAXNG_ZEROORMORE;
4851 if (node->children == NULL) {
4852 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4853 "Element %s is empty\n", node->name, NULL);
4854 } else {
4855 def->content =
4856 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4857 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004858 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004859 def = xmlRelaxNGNewDefine(ctxt, node);
4860 if (def == NULL)
4861 return (NULL);
4862 def->type = XML_RELAXNG_ONEORMORE;
4863 if (node->children == NULL) {
4864 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4865 "Element %s is empty\n", node->name, NULL);
4866 } else {
4867 def->content =
4868 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4869 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004870 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004871 def = xmlRelaxNGNewDefine(ctxt, node);
4872 if (def == NULL)
4873 return (NULL);
4874 def->type = XML_RELAXNG_OPTIONAL;
4875 if (node->children == NULL) {
4876 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4877 "Element %s is empty\n", node->name, NULL);
4878 } else {
4879 def->content =
4880 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4881 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004882 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004883 def = xmlRelaxNGNewDefine(ctxt, node);
4884 if (def == NULL)
4885 return (NULL);
4886 def->type = XML_RELAXNG_CHOICE;
4887 if (node->children == NULL) {
4888 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4889 "Element %s is empty\n", node->name, NULL);
4890 } else {
4891 def->content =
4892 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4893 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004894 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004895 def = xmlRelaxNGNewDefine(ctxt, node);
4896 if (def == NULL)
4897 return (NULL);
4898 def->type = XML_RELAXNG_GROUP;
4899 if (node->children == NULL) {
4900 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4901 "Element %s is empty\n", node->name, NULL);
4902 } else {
4903 def->content =
4904 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4905 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004906 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004907 def = xmlRelaxNGNewDefine(ctxt, node);
4908 if (def == NULL)
4909 return (NULL);
4910 def->type = XML_RELAXNG_REF;
4911 def->name = xmlGetProp(node, BAD_CAST "name");
4912 if (def->name == NULL) {
4913 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4914 NULL, NULL);
4915 } else {
4916 xmlRelaxNGNormExtSpace(def->name);
4917 if (xmlValidateNCName(def->name, 0)) {
4918 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4919 "ref name '%s' is not an NCName\n", def->name,
4920 NULL);
4921 }
4922 }
4923 if (node->children != NULL) {
4924 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4925 NULL, NULL);
4926 }
4927 if (ctxt->grammar->refs == NULL)
4928 ctxt->grammar->refs = xmlHashCreate(10);
4929 if (ctxt->grammar->refs == NULL) {
4930 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4931 "Could not create references hash\n", NULL, NULL);
4932 def = NULL;
4933 } else {
4934 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004935
Daniel Veillard4c004142003-10-07 11:33:24 +00004936 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4937 if (tmp < 0) {
4938 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004939
Daniel Veillard4c004142003-10-07 11:33:24 +00004940 prev = (xmlRelaxNGDefinePtr)
4941 xmlHashLookup(ctxt->grammar->refs, def->name);
4942 if (prev == NULL) {
4943 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004944 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4945 "Error refs definitions '%s'\n",
4946 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004947 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004948 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4949 "Error refs definitions\n",
4950 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004951 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004952 def = NULL;
4953 } else {
4954 def->nextHash = prev->nextHash;
4955 prev->nextHash = def;
4956 }
4957 }
4958 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004959 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004960 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004961 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004962 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004963 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004964 def = xmlRelaxNGNewDefine(ctxt, node);
4965 if (def == NULL)
4966 return (NULL);
4967 def->type = XML_RELAXNG_LIST;
4968 if (node->children == NULL) {
4969 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4970 "Element %s is empty\n", node->name, NULL);
4971 } else {
4972 def->content =
4973 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4974 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004975 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004976 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004977 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004978 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004979 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004980 def = xmlRelaxNGNewDefine(ctxt, node);
4981 if (def == NULL)
4982 return (NULL);
4983 def->type = XML_RELAXNG_NOT_ALLOWED;
4984 if (node->children != NULL) {
4985 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4986 "xmlRelaxNGParse: notAllowed element is not empty\n",
4987 NULL, NULL);
4988 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004989 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004990 xmlRelaxNGGrammarPtr grammar, old;
4991 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004992
Daniel Veillardc482e262003-02-26 14:48:48 +00004993#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004994 xmlGenericError(xmlGenericErrorContext,
4995 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004996#endif
4997
Daniel Veillard4c004142003-10-07 11:33:24 +00004998 oldparent = ctxt->parentgrammar;
4999 old = ctxt->grammar;
5000 ctxt->parentgrammar = old;
5001 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5002 if (old != NULL) {
5003 ctxt->grammar = old;
5004 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00005005#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00005006 if (grammar != NULL) {
5007 grammar->next = old->next;
5008 old->next = grammar;
5009 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005010#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00005011 }
5012 if (grammar != NULL)
5013 def = grammar->start;
5014 else
5015 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00005016 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005017 if (ctxt->parentgrammar == NULL) {
5018 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5019 "Use of parentRef without a parent grammar\n", NULL,
5020 NULL);
5021 return (NULL);
5022 }
5023 def = xmlRelaxNGNewDefine(ctxt, node);
5024 if (def == NULL)
5025 return (NULL);
5026 def->type = XML_RELAXNG_PARENTREF;
5027 def->name = xmlGetProp(node, BAD_CAST "name");
5028 if (def->name == NULL) {
5029 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5030 "parentRef has no name\n", NULL, NULL);
5031 } else {
5032 xmlRelaxNGNormExtSpace(def->name);
5033 if (xmlValidateNCName(def->name, 0)) {
5034 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5035 "parentRef name '%s' is not an NCName\n",
5036 def->name, NULL);
5037 }
5038 }
5039 if (node->children != NULL) {
5040 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5041 "parentRef is not empty\n", NULL, NULL);
5042 }
5043 if (ctxt->parentgrammar->refs == NULL)
5044 ctxt->parentgrammar->refs = xmlHashCreate(10);
5045 if (ctxt->parentgrammar->refs == NULL) {
5046 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5047 "Could not create references hash\n", NULL, NULL);
5048 def = NULL;
5049 } else if (def->name != NULL) {
5050 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005051
Daniel Veillard4c004142003-10-07 11:33:24 +00005052 tmp =
5053 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5054 if (tmp < 0) {
5055 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005056
Daniel Veillard4c004142003-10-07 11:33:24 +00005057 prev = (xmlRelaxNGDefinePtr)
5058 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5059 if (prev == NULL) {
5060 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5061 "Internal error parentRef definitions '%s'\n",
5062 def->name, NULL);
5063 def = NULL;
5064 } else {
5065 def->nextHash = prev->nextHash;
5066 prev->nextHash = def;
5067 }
5068 }
5069 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005070 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005071 if (node->children == NULL) {
5072 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5073 NULL, NULL);
5074 def = NULL;
5075 } else {
5076 def = xmlRelaxNGParseInterleave(ctxt, node);
5077 if (def != NULL) {
5078 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005079
Daniel Veillard4c004142003-10-07 11:33:24 +00005080 if ((def->content != NULL) && (def->content->next != NULL)) {
5081 tmp = xmlRelaxNGNewDefine(ctxt, node);
5082 if (tmp != NULL) {
5083 tmp->type = XML_RELAXNG_GROUP;
5084 tmp->content = def->content;
5085 def->content = tmp;
5086 }
5087 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005088
Daniel Veillard4c004142003-10-07 11:33:24 +00005089 tmp = xmlRelaxNGNewDefine(ctxt, node);
5090 if (tmp == NULL)
5091 return (def);
5092 tmp->type = XML_RELAXNG_TEXT;
5093 tmp->next = def->content;
5094 def->content = tmp;
5095 }
5096 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005097 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005098 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5099 "Unexpected node %s is not a pattern\n", node->name,
5100 NULL);
5101 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005102 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005103 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005104}
5105
5106/**
5107 * xmlRelaxNGParseAttribute:
5108 * @ctxt: a Relax-NG parser context
5109 * @node: the element node
5110 *
5111 * parse the content of a RelaxNG attribute node.
5112 *
5113 * Returns the definition pointer or NULL in case of error.
5114 */
5115static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005116xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5117{
Daniel Veillardd2298792003-02-14 16:54:11 +00005118 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005119 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005120 int old_flags;
5121
Daniel Veillardfd573f12003-03-16 17:52:32 +00005122 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005123 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005124 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005125 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005126 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005127 child = node->children;
5128 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005129 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5130 "xmlRelaxNGParseattribute: attribute has no children\n",
5131 NULL, NULL);
5132 return (ret);
5133 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005134 old_flags = ctxt->flags;
5135 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005136 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5137 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005138 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005139
Daniel Veillardd2298792003-02-14 16:54:11 +00005140 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005141 cur = xmlRelaxNGParsePattern(ctxt, child);
5142 if (cur != NULL) {
5143 switch (cur->type) {
5144 case XML_RELAXNG_EMPTY:
5145 case XML_RELAXNG_NOT_ALLOWED:
5146 case XML_RELAXNG_TEXT:
5147 case XML_RELAXNG_ELEMENT:
5148 case XML_RELAXNG_DATATYPE:
5149 case XML_RELAXNG_VALUE:
5150 case XML_RELAXNG_LIST:
5151 case XML_RELAXNG_REF:
5152 case XML_RELAXNG_PARENTREF:
5153 case XML_RELAXNG_EXTERNALREF:
5154 case XML_RELAXNG_DEF:
5155 case XML_RELAXNG_ONEORMORE:
5156 case XML_RELAXNG_ZEROORMORE:
5157 case XML_RELAXNG_OPTIONAL:
5158 case XML_RELAXNG_CHOICE:
5159 case XML_RELAXNG_GROUP:
5160 case XML_RELAXNG_INTERLEAVE:
5161 case XML_RELAXNG_ATTRIBUTE:
5162 ret->content = cur;
5163 cur->parent = ret;
5164 break;
5165 case XML_RELAXNG_START:
5166 case XML_RELAXNG_PARAM:
5167 case XML_RELAXNG_EXCEPT:
5168 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5169 "attribute has invalid content\n", NULL,
5170 NULL);
5171 break;
5172 case XML_RELAXNG_NOOP:
5173 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5174 "RNG Internal error, noop found in attribute\n",
5175 NULL, NULL);
5176 break;
5177 }
5178 }
5179 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005180 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005181 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005182 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5183 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005184 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005185 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005186 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005187}
5188
5189/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005190 * xmlRelaxNGParseExceptNameClass:
5191 * @ctxt: a Relax-NG parser context
5192 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005193 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005194 *
5195 * parse the content of a RelaxNG nameClass node.
5196 *
5197 * Returns the definition pointer or NULL in case of error.
5198 */
5199static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005200xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005201 xmlNodePtr node, int attr)
5202{
Daniel Veillard144fae12003-02-03 13:17:57 +00005203 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5204 xmlNodePtr child;
5205
Daniel Veillardd2298792003-02-14 16:54:11 +00005206 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005207 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5208 "Expecting an except node\n", NULL, NULL);
5209 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005210 }
5211 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005212 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5213 "exceptNameClass allows only a single except node\n",
5214 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005215 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005216 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005217 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5218 NULL, NULL);
5219 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005220 }
5221
Daniel Veillardfd573f12003-03-16 17:52:32 +00005222 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005223 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005224 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005225 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005226 child = node->children;
5227 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005228 cur = xmlRelaxNGNewDefine(ctxt, child);
5229 if (cur == NULL)
5230 break;
5231 if (attr)
5232 cur->type = XML_RELAXNG_ATTRIBUTE;
5233 else
5234 cur->type = XML_RELAXNG_ELEMENT;
5235
Daniel Veillard419a7682003-02-03 23:22:49 +00005236 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005237 if (last == NULL) {
5238 ret->content = cur;
5239 } else {
5240 last->next = cur;
5241 }
5242 last = cur;
5243 }
5244 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005245 }
5246
Daniel Veillard4c004142003-10-07 11:33:24 +00005247 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005248}
5249
5250/**
5251 * xmlRelaxNGParseNameClass:
5252 * @ctxt: a Relax-NG parser context
5253 * @node: the nameClass node
5254 * @def: the current definition
5255 *
5256 * parse the content of a RelaxNG nameClass node.
5257 *
5258 * Returns the definition pointer or NULL in case of error.
5259 */
5260static xmlRelaxNGDefinePtr
5261xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005262 xmlRelaxNGDefinePtr def)
5263{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005264 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005265 xmlChar *val;
5266
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005267 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005268 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005269 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005270 if ((def->type != XML_RELAXNG_ELEMENT) &&
5271 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5272 ret = xmlRelaxNGNewDefine(ctxt, node);
5273 if (ret == NULL)
5274 return (NULL);
5275 ret->parent = def;
5276 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5277 ret->type = XML_RELAXNG_ATTRIBUTE;
5278 else
5279 ret->type = XML_RELAXNG_ELEMENT;
5280 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005281 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005282 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005283 val = xmlNodeGetContent(node);
5284 xmlRelaxNGNormExtSpace(val);
5285 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005286 if (node->parent != NULL)
5287 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5288 "Element %s name '%s' is not an NCName\n",
5289 node->parent->name, val);
5290 else
5291 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5292 "name '%s' is not an NCName\n",
5293 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005294 }
5295 ret->name = val;
5296 val = xmlGetProp(node, BAD_CAST "ns");
5297 ret->ns = val;
5298 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5299 (val != NULL) &&
5300 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005301 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005302 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005303 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005304 }
5305 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5306 (val != NULL) &&
5307 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005308 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5309 "Attribute with QName 'xmlns' is not allowed\n",
5310 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005311 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005312 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005313 ret->name = NULL;
5314 ret->ns = NULL;
5315 if (node->children != NULL) {
5316 ret->nameClass =
5317 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5318 (def->type ==
5319 XML_RELAXNG_ATTRIBUTE));
5320 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005321 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005322 ret->name = NULL;
5323 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5324 if (ret->ns == NULL) {
5325 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5326 "nsName has no ns attribute\n", NULL, NULL);
5327 }
5328 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5329 (ret->ns != NULL) &&
5330 (xmlStrEqual
5331 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5332 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5333 "Attribute with namespace '%s' is not allowed\n",
5334 ret->ns, NULL);
5335 }
5336 if (node->children != NULL) {
5337 ret->nameClass =
5338 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5339 (def->type ==
5340 XML_RELAXNG_ATTRIBUTE));
5341 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005342 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005343 xmlNodePtr child;
5344 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005345
Daniel Veillard4c004142003-10-07 11:33:24 +00005346 ret = xmlRelaxNGNewDefine(ctxt, node);
5347 if (ret == NULL)
5348 return (NULL);
5349 ret->parent = def;
5350 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005351
Daniel Veillard4c004142003-10-07 11:33:24 +00005352 if (node->children == NULL) {
5353 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5354 "Element choice is empty\n", NULL, NULL);
5355 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005356
Daniel Veillard4c004142003-10-07 11:33:24 +00005357 child = node->children;
5358 while (child != NULL) {
5359 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5360 if (tmp != NULL) {
5361 if (last == NULL) {
5362 last = ret->nameClass = tmp;
5363 } else {
5364 last->next = tmp;
5365 last = tmp;
5366 }
5367 }
5368 child = child->next;
5369 }
5370 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005371 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005372 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5373 "expecting name, anyName, nsName or choice : got %s\n",
Ben Waltona7a6a4b2010-03-15 10:06:36 +01005374 (node == NULL ? (const xmlChar *) "nothing" : node->name),
5375 NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005376 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005377 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005378 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005379 if (def->nameClass == NULL) {
5380 def->nameClass = ret;
5381 } else {
5382 tmp = def->nameClass;
5383 while (tmp->next != NULL) {
5384 tmp = tmp->next;
5385 }
5386 tmp->next = ret;
5387 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005388 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005389 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005390}
5391
5392/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005393 * xmlRelaxNGParseElement:
5394 * @ctxt: a Relax-NG parser context
5395 * @node: the element node
5396 *
5397 * parse the content of a RelaxNG element node.
5398 *
5399 * Returns the definition pointer or NULL in case of error.
5400 */
5401static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005402xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5403{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005404 xmlRelaxNGDefinePtr ret, cur, last;
5405 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005406 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005407
Daniel Veillardfd573f12003-03-16 17:52:32 +00005408 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005409 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005410 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005411 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005412 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005413 child = node->children;
5414 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005415 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5416 "xmlRelaxNGParseElement: element has no children\n",
5417 NULL, NULL);
5418 return (ret);
5419 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005420 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5421 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005422 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005423
Daniel Veillard6eadf632003-01-23 18:29:16 +00005424 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005425 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5426 "xmlRelaxNGParseElement: element has no content\n",
5427 NULL, NULL);
5428 return (ret);
5429 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005430 olddefine = ctxt->define;
5431 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005432 last = NULL;
5433 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005434 cur = xmlRelaxNGParsePattern(ctxt, child);
5435 if (cur != NULL) {
5436 cur->parent = ret;
5437 switch (cur->type) {
5438 case XML_RELAXNG_EMPTY:
5439 case XML_RELAXNG_NOT_ALLOWED:
5440 case XML_RELAXNG_TEXT:
5441 case XML_RELAXNG_ELEMENT:
5442 case XML_RELAXNG_DATATYPE:
5443 case XML_RELAXNG_VALUE:
5444 case XML_RELAXNG_LIST:
5445 case XML_RELAXNG_REF:
5446 case XML_RELAXNG_PARENTREF:
5447 case XML_RELAXNG_EXTERNALREF:
5448 case XML_RELAXNG_DEF:
5449 case XML_RELAXNG_ZEROORMORE:
5450 case XML_RELAXNG_ONEORMORE:
5451 case XML_RELAXNG_OPTIONAL:
5452 case XML_RELAXNG_CHOICE:
5453 case XML_RELAXNG_GROUP:
5454 case XML_RELAXNG_INTERLEAVE:
5455 if (last == NULL) {
5456 ret->content = last = cur;
5457 } else {
5458 if ((last->type == XML_RELAXNG_ELEMENT) &&
5459 (ret->content == last)) {
5460 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5461 if (ret->content != NULL) {
5462 ret->content->type = XML_RELAXNG_GROUP;
5463 ret->content->content = last;
5464 } else {
5465 ret->content = last;
5466 }
5467 }
5468 last->next = cur;
5469 last = cur;
5470 }
5471 break;
5472 case XML_RELAXNG_ATTRIBUTE:
5473 cur->next = ret->attrs;
5474 ret->attrs = cur;
5475 break;
5476 case XML_RELAXNG_START:
5477 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5478 "RNG Internal error, start found in element\n",
5479 NULL, NULL);
5480 break;
5481 case XML_RELAXNG_PARAM:
5482 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5483 "RNG Internal error, param found in element\n",
5484 NULL, NULL);
5485 break;
5486 case XML_RELAXNG_EXCEPT:
5487 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5488 "RNG Internal error, except found in element\n",
5489 NULL, NULL);
5490 break;
5491 case XML_RELAXNG_NOOP:
5492 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5493 "RNG Internal error, noop found in element\n",
5494 NULL, NULL);
5495 break;
5496 }
5497 }
5498 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005499 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005500 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005501 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005502}
5503
5504/**
5505 * xmlRelaxNGParsePatterns:
5506 * @ctxt: a Relax-NG parser context
5507 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005508 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005509 *
5510 * parse the content of a RelaxNG start node.
5511 *
5512 * Returns the definition pointer or NULL in case of error.
5513 */
5514static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005515xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005516 int group)
5517{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005518 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005519
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005520 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005521 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005522 if (IS_RELAXNG(nodes, "element")) {
5523 cur = xmlRelaxNGParseElement(ctxt, nodes);
5524 if (def == NULL) {
5525 def = last = cur;
5526 } else {
5527 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5528 (def == last)) {
5529 def = xmlRelaxNGNewDefine(ctxt, nodes);
5530 def->type = XML_RELAXNG_GROUP;
5531 def->content = last;
5532 }
5533 last->next = cur;
5534 last = cur;
5535 }
5536 cur->parent = parent;
5537 } else {
5538 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5539 if (cur != NULL) {
5540 if (def == NULL) {
5541 def = last = cur;
5542 } else {
5543 last->next = cur;
5544 last = cur;
5545 }
5546 }
5547 }
5548 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005549 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005550 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005551}
5552
5553/**
5554 * xmlRelaxNGParseStart:
5555 * @ctxt: a Relax-NG parser context
5556 * @nodes: start children nodes
5557 *
5558 * parse the content of a RelaxNG start node.
5559 *
5560 * Returns 0 in case of success, -1 in case of error
5561 */
5562static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005563xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5564{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005565 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005566 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005567
Daniel Veillardd2298792003-02-14 16:54:11 +00005568 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005569 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5570 NULL, NULL);
5571 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005572 }
5573 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005574 def = xmlRelaxNGNewDefine(ctxt, nodes);
5575 if (def == NULL)
5576 return (-1);
5577 def->type = XML_RELAXNG_EMPTY;
5578 if (nodes->children != NULL) {
5579 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5580 "element empty is not empty\n", NULL, NULL);
5581 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005582 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005583 def = xmlRelaxNGNewDefine(ctxt, nodes);
5584 if (def == NULL)
5585 return (-1);
5586 def->type = XML_RELAXNG_NOT_ALLOWED;
5587 if (nodes->children != NULL) {
5588 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5589 "element notAllowed is not empty\n", NULL, NULL);
5590 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005591 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005592 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005593 }
5594 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005595 last = ctxt->grammar->start;
5596 while (last->next != NULL)
5597 last = last->next;
5598 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005599 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005600 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005601 }
5602 nodes = nodes->next;
5603 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005604 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5605 "start more than one children\n", NULL, NULL);
5606 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005607 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005608 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005609}
5610
5611/**
5612 * xmlRelaxNGParseGrammarContent:
5613 * @ctxt: a Relax-NG parser context
5614 * @nodes: grammar children nodes
5615 *
5616 * parse the content of a RelaxNG grammar node.
5617 *
5618 * Returns 0 in case of success, -1 in case of error
5619 */
5620static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005621xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5622 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005623{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005624 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005625
5626 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005627 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5628 "grammar has no children\n", NULL, NULL);
5629 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005630 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005631 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005632 if (IS_RELAXNG(nodes, "start")) {
5633 if (nodes->children == NULL) {
5634 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5635 "start has no children\n", NULL, NULL);
5636 } else {
5637 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5638 if (tmp != 0)
5639 ret = -1;
5640 }
5641 } else if (IS_RELAXNG(nodes, "define")) {
5642 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5643 if (tmp != 0)
5644 ret = -1;
5645 } else if (IS_RELAXNG(nodes, "include")) {
5646 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5647 if (tmp != 0)
5648 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005649 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005650 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5651 "grammar has unexpected child %s\n", nodes->name,
5652 NULL);
5653 ret = -1;
5654 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005655 nodes = nodes->next;
5656 }
5657 return (ret);
5658}
5659
5660/**
5661 * xmlRelaxNGCheckReference:
5662 * @ref: the ref
5663 * @ctxt: a Relax-NG parser context
5664 * @name: the name associated to the defines
5665 *
5666 * Applies the 4.17. combine attribute rule for all the define
5667 * element of a given grammar using the same name.
5668 */
5669static void
5670xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005671 xmlRelaxNGParserCtxtPtr ctxt,
5672 const xmlChar * name)
5673{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005674 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005675 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005676
Daniel Veillardaa422d92009-09-24 11:31:48 +02005677 /*
5678 * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5679 */
5680 if (ref->dflags & IS_EXTERNAL_REF)
5681 return;
5682
Daniel Veillard6eadf632003-01-23 18:29:16 +00005683 grammar = ctxt->grammar;
5684 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005685 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5686 "Internal error: no grammar in CheckReference %s\n",
5687 name, NULL);
5688 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005689 }
5690 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005691 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5692 "Internal error: reference has content in CheckReference %s\n",
5693 name, NULL);
5694 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005695 }
5696 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005697 def = xmlHashLookup(grammar->defs, name);
5698 if (def != NULL) {
5699 cur = ref;
5700 while (cur != NULL) {
5701 cur->content = def;
5702 cur = cur->nextHash;
5703 }
5704 } else {
5705 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5706 "Reference %s has no matching definition\n", name,
5707 NULL);
5708 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005709 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005710 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5711 "Reference %s has no matching definition\n", name,
5712 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005713 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005714}
5715
5716/**
5717 * xmlRelaxNGCheckCombine:
5718 * @define: the define(s) list
5719 * @ctxt: a Relax-NG parser context
5720 * @name: the name associated to the defines
5721 *
5722 * Applies the 4.17. combine attribute rule for all the define
5723 * element of a given grammar using the same name.
5724 */
5725static void
5726xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005727 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5728{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005729 xmlChar *combine;
5730 int choiceOrInterleave = -1;
5731 int missing = 0;
5732 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5733
5734 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005735 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005736 cur = define;
5737 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005738 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5739 if (combine != NULL) {
5740 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5741 if (choiceOrInterleave == -1)
5742 choiceOrInterleave = 1;
5743 else if (choiceOrInterleave == 0) {
5744 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5745 "Defines for %s use both 'choice' and 'interleave'\n",
5746 name, NULL);
5747 }
5748 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5749 if (choiceOrInterleave == -1)
5750 choiceOrInterleave = 0;
5751 else if (choiceOrInterleave == 1) {
5752 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5753 "Defines for %s use both 'choice' and 'interleave'\n",
5754 name, NULL);
5755 }
5756 } else {
5757 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5758 "Defines for %s use unknown combine value '%s''\n",
5759 name, combine);
5760 }
5761 xmlFree(combine);
5762 } else {
5763 if (missing == 0)
5764 missing = 1;
5765 else {
5766 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5767 "Some defines for %s needs the combine attribute\n",
5768 name, NULL);
5769 }
5770 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005771
Daniel Veillard4c004142003-10-07 11:33:24 +00005772 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005773 }
5774#ifdef DEBUG
5775 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005776 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5777 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005778#endif
5779 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005780 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005781 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005782 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005783 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005784 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005785 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005786 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005787 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005788 tmp = define;
5789 last = NULL;
5790 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005791 if (tmp->content != NULL) {
5792 if (tmp->content->next != NULL) {
5793 /*
5794 * we need first to create a wrapper.
5795 */
5796 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5797 if (tmp2 == NULL)
5798 break;
5799 tmp2->type = XML_RELAXNG_GROUP;
5800 tmp2->content = tmp->content;
5801 } else {
5802 tmp2 = tmp->content;
5803 }
5804 if (last == NULL) {
5805 cur->content = tmp2;
5806 } else {
5807 last->next = tmp2;
5808 }
5809 last = tmp2;
5810 }
5811 tmp->content = cur;
5812 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005813 }
5814 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005815 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005816 if (ctxt->interleaves == NULL)
5817 ctxt->interleaves = xmlHashCreate(10);
5818 if (ctxt->interleaves == NULL) {
5819 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5820 "Failed to create interleaves hash table\n", NULL,
5821 NULL);
5822 } else {
5823 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005824
Daniel Veillard4c004142003-10-07 11:33:24 +00005825 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5826 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5827 0) {
5828 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5829 "Failed to add %s to hash table\n",
5830 (const xmlChar *) tmpname, NULL);
5831 }
5832 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005833 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005834}
5835
5836/**
5837 * xmlRelaxNGCombineStart:
5838 * @ctxt: a Relax-NG parser context
5839 * @grammar: the grammar
5840 *
5841 * Applies the 4.17. combine rule for all the start
5842 * element of a given grammar.
5843 */
5844static void
5845xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005846 xmlRelaxNGGrammarPtr grammar)
5847{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005848 xmlRelaxNGDefinePtr starts;
5849 xmlChar *combine;
5850 int choiceOrInterleave = -1;
5851 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005852 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005853
Daniel Veillard2df2de22003-02-17 23:34:33 +00005854 starts = grammar->start;
5855 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005856 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005857 cur = starts;
5858 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005859 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5860 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5861 combine = NULL;
5862 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5863 "Internal error: start element not found\n", NULL,
5864 NULL);
5865 } else {
5866 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5867 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005868
Daniel Veillard4c004142003-10-07 11:33:24 +00005869 if (combine != NULL) {
5870 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5871 if (choiceOrInterleave == -1)
5872 choiceOrInterleave = 1;
5873 else if (choiceOrInterleave == 0) {
5874 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5875 "<start> use both 'choice' and 'interleave'\n",
5876 NULL, NULL);
5877 }
5878 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5879 if (choiceOrInterleave == -1)
5880 choiceOrInterleave = 0;
5881 else if (choiceOrInterleave == 1) {
5882 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5883 "<start> use both 'choice' and 'interleave'\n",
5884 NULL, NULL);
5885 }
5886 } else {
5887 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5888 "<start> uses unknown combine value '%s''\n",
5889 combine, NULL);
5890 }
5891 xmlFree(combine);
5892 } else {
5893 if (missing == 0)
5894 missing = 1;
5895 else {
5896 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5897 "Some <start> element miss the combine attribute\n",
5898 NULL, NULL);
5899 }
5900 }
5901
5902 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005903 }
5904#ifdef DEBUG
5905 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005906 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5907 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005908#endif
5909 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005910 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005911 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005912 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005913 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005914 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005915 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005916 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005917 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005918 cur->content = grammar->start;
5919 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005920 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005921 if (ctxt->interleaves == NULL)
5922 ctxt->interleaves = xmlHashCreate(10);
5923 if (ctxt->interleaves == NULL) {
5924 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5925 "Failed to create interleaves hash table\n", NULL,
5926 NULL);
5927 } else {
5928 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005929
Daniel Veillard4c004142003-10-07 11:33:24 +00005930 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5931 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5932 0) {
5933 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5934 "Failed to add %s to hash table\n",
5935 (const xmlChar *) tmpname, NULL);
5936 }
5937 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005938 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005939}
5940
5941/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005942 * xmlRelaxNGCheckCycles:
5943 * @ctxt: a Relax-NG parser context
5944 * @nodes: grammar children nodes
5945 * @depth: the counter
5946 *
5947 * Check for cycles.
5948 *
5949 * Returns 0 if check passed, and -1 in case of error
5950 */
5951static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005952xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5953 xmlRelaxNGDefinePtr cur, int depth)
5954{
Daniel Veillardd4310742003-02-18 21:12:46 +00005955 int ret = 0;
5956
5957 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005958 if ((cur->type == XML_RELAXNG_REF) ||
5959 (cur->type == XML_RELAXNG_PARENTREF)) {
5960 if (cur->depth == -1) {
5961 cur->depth = depth;
5962 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5963 cur->depth = -2;
5964 } else if (depth == cur->depth) {
5965 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5966 "Detected a cycle in %s references\n",
5967 cur->name, NULL);
5968 return (-1);
5969 }
5970 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5971 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5972 } else {
5973 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5974 }
5975 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005976 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005977 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005978}
5979
5980/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005981 * xmlRelaxNGTryUnlink:
5982 * @ctxt: a Relax-NG parser context
5983 * @cur: the definition to unlink
5984 * @parent: the parent definition
5985 * @prev: the previous sibling definition
5986 *
5987 * Try to unlink a definition. If not possble make it a NOOP
5988 *
5989 * Returns the new prev definition
5990 */
5991static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005992xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5993 xmlRelaxNGDefinePtr cur,
5994 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5995{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005996 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005997 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005998 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005999 if (parent != NULL) {
6000 if (parent->content == cur)
6001 parent->content = cur->next;
6002 else if (parent->attrs == cur)
6003 parent->attrs = cur->next;
6004 else if (parent->nameClass == cur)
6005 parent->nameClass = cur->next;
6006 } else {
6007 cur->type = XML_RELAXNG_NOOP;
6008 prev = cur;
6009 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006010 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006011 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006012}
6013
6014/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006015 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006016 * @ctxt: a Relax-NG parser context
6017 * @nodes: grammar children nodes
6018 *
6019 * Check for simplification of empty and notAllowed
6020 */
6021static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006022xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6023 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6024{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006025 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006026
Daniel Veillardfd573f12003-03-16 17:52:32 +00006027 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006028 if ((cur->type == XML_RELAXNG_REF) ||
6029 (cur->type == XML_RELAXNG_PARENTREF)) {
6030 if (cur->depth != -3) {
6031 cur->depth = -3;
6032 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6033 }
6034 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6035 cur->parent = parent;
6036 if ((parent != NULL) &&
6037 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6038 (parent->type == XML_RELAXNG_LIST) ||
6039 (parent->type == XML_RELAXNG_GROUP) ||
6040 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6041 (parent->type == XML_RELAXNG_ONEORMORE) ||
6042 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6043 parent->type = XML_RELAXNG_NOT_ALLOWED;
6044 break;
6045 }
6046 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6047 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6048 } else
6049 prev = cur;
6050 } else if (cur->type == XML_RELAXNG_EMPTY) {
6051 cur->parent = parent;
6052 if ((parent != NULL) &&
6053 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6054 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6055 parent->type = XML_RELAXNG_EMPTY;
6056 break;
6057 }
6058 if ((parent != NULL) &&
6059 ((parent->type == XML_RELAXNG_GROUP) ||
6060 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6061 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6062 } else
6063 prev = cur;
6064 } else {
6065 cur->parent = parent;
6066 if (cur->content != NULL)
6067 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6068 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6069 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6070 if (cur->nameClass != NULL)
6071 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6072 /*
6073 * On Elements, try to move attribute only generating rules on
6074 * the attrs rules.
6075 */
6076 if (cur->type == XML_RELAXNG_ELEMENT) {
6077 int attronly;
6078 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006079
Daniel Veillard4c004142003-10-07 11:33:24 +00006080 while (cur->content != NULL) {
6081 attronly =
6082 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6083 if (attronly == 1) {
6084 /*
6085 * migrate cur->content to attrs
6086 */
6087 tmp = cur->content;
6088 cur->content = tmp->next;
6089 tmp->next = cur->attrs;
6090 cur->attrs = tmp;
6091 } else {
6092 /*
6093 * cur->content can generate elements or text
6094 */
6095 break;
6096 }
6097 }
6098 pre = cur->content;
6099 while ((pre != NULL) && (pre->next != NULL)) {
6100 tmp = pre->next;
6101 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6102 if (attronly == 1) {
6103 /*
6104 * migrate tmp to attrs
6105 */
6106 pre->next = tmp->next;
6107 tmp->next = cur->attrs;
6108 cur->attrs = tmp;
6109 } else {
6110 pre = tmp;
6111 }
6112 }
6113 }
6114 /*
6115 * This may result in a simplification
6116 */
6117 if ((cur->type == XML_RELAXNG_GROUP) ||
6118 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6119 if (cur->content == NULL)
6120 cur->type = XML_RELAXNG_EMPTY;
6121 else if (cur->content->next == NULL) {
6122 if ((parent == NULL) && (prev == NULL)) {
6123 cur->type = XML_RELAXNG_NOOP;
6124 } else if (prev == NULL) {
6125 parent->content = cur->content;
6126 cur->content->next = cur->next;
6127 cur = cur->content;
6128 } else {
6129 cur->content->next = cur->next;
6130 prev->next = cur->content;
6131 cur = cur->content;
6132 }
6133 }
6134 }
6135 /*
6136 * the current node may have been transformed back
6137 */
6138 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6139 (cur->content != NULL) &&
6140 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6141 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6142 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6143 if ((parent != NULL) &&
6144 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6145 (parent->type == XML_RELAXNG_LIST) ||
6146 (parent->type == XML_RELAXNG_GROUP) ||
6147 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6148 (parent->type == XML_RELAXNG_ONEORMORE) ||
6149 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6150 parent->type = XML_RELAXNG_NOT_ALLOWED;
6151 break;
6152 }
6153 if ((parent != NULL) &&
6154 (parent->type == XML_RELAXNG_CHOICE)) {
6155 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6156 } else
6157 prev = cur;
6158 } else if (cur->type == XML_RELAXNG_EMPTY) {
6159 if ((parent != NULL) &&
6160 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6161 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6162 parent->type = XML_RELAXNG_EMPTY;
6163 break;
6164 }
6165 if ((parent != NULL) &&
6166 ((parent->type == XML_RELAXNG_GROUP) ||
6167 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6168 (parent->type == XML_RELAXNG_CHOICE))) {
6169 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6170 } else
6171 prev = cur;
6172 } else {
6173 prev = cur;
6174 }
6175 }
6176 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006177 }
6178}
6179
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006180/**
6181 * xmlRelaxNGGroupContentType:
6182 * @ct1: the first content type
6183 * @ct2: the second content type
6184 *
6185 * Try to group 2 content types
6186 *
6187 * Returns the content type
6188 */
6189static xmlRelaxNGContentType
6190xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006191 xmlRelaxNGContentType ct2)
6192{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006193 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006194 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6195 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006196 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006197 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006198 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006199 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006200 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006201 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6202 return (XML_RELAXNG_CONTENT_COMPLEX);
6203 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006204}
6205
6206/**
6207 * xmlRelaxNGMaxContentType:
6208 * @ct1: the first content type
6209 * @ct2: the second content type
6210 *
6211 * Compute the max content-type
6212 *
6213 * Returns the content type
6214 */
6215static xmlRelaxNGContentType
6216xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006217 xmlRelaxNGContentType ct2)
6218{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006219 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006220 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6221 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006222 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006223 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6224 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006225 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006226 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6227 return (XML_RELAXNG_CONTENT_COMPLEX);
6228 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006229}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006230
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006231/**
6232 * xmlRelaxNGCheckRules:
6233 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006234 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006235 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006236 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006237 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006238 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006239 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006240 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006241 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006242static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006243xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6244 xmlRelaxNGDefinePtr cur, int flags,
6245 xmlRelaxNGType ptype)
6246{
Daniel Veillardd44b9362009-09-07 12:15:08 +02006247 int nflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006248 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006249
Daniel Veillardfd573f12003-03-16 17:52:32 +00006250 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006251 ret = XML_RELAXNG_CONTENT_EMPTY;
6252 if ((cur->type == XML_RELAXNG_REF) ||
6253 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006254 /*
6255 * This should actually be caught by list//element(ref) at the
6256 * element boundaries, c.f. Bug #159968 local refs are dropped
6257 * in step 4.19.
6258 */
6259#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006260 if (flags & XML_RELAXNG_IN_LIST) {
6261 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6262 "Found forbidden pattern list//ref\n", NULL,
6263 NULL);
6264 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006265#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006266 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6267 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6268 "Found forbidden pattern data/except//ref\n",
6269 NULL, NULL);
6270 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006271 if (cur->content == NULL) {
6272 if (cur->type == XML_RELAXNG_PARENTREF)
6273 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6274 "Internal found no define for parent refs\n",
6275 NULL, NULL);
6276 else
6277 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6278 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006279 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006280 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006281 if (cur->depth > -4) {
6282 cur->depth = -4;
6283 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6284 flags, cur->type);
6285 cur->depth = ret - 15;
6286 } else if (cur->depth == -4) {
6287 ret = XML_RELAXNG_CONTENT_COMPLEX;
6288 } else {
6289 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6290 }
6291 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6292 /*
6293 * The 7.3 Attribute derivation rule for groups is plugged there
6294 */
6295 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6296 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6297 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6298 "Found forbidden pattern data/except//element(ref)\n",
6299 NULL, NULL);
6300 }
6301 if (flags & XML_RELAXNG_IN_LIST) {
6302 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6303 "Found forbidden pattern list//element(ref)\n",
6304 NULL, NULL);
6305 }
6306 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6307 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6308 "Found forbidden pattern attribute//element(ref)\n",
6309 NULL, NULL);
6310 }
6311 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6312 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6313 "Found forbidden pattern attribute//element(ref)\n",
6314 NULL, NULL);
6315 }
6316 /*
6317 * reset since in the simple form elements are only child
6318 * of grammar/define
6319 */
6320 nflags = 0;
6321 ret =
6322 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6323 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6324 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6325 "Element %s attributes have a content type error\n",
6326 cur->name, NULL);
6327 }
6328 ret =
6329 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6330 cur->type);
6331 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6332 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6333 "Element %s has a content type error\n",
6334 cur->name, NULL);
6335 } else {
6336 ret = XML_RELAXNG_CONTENT_COMPLEX;
6337 }
6338 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6339 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6340 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6341 "Found forbidden pattern attribute//attribute\n",
6342 NULL, NULL);
6343 }
6344 if (flags & XML_RELAXNG_IN_LIST) {
6345 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6346 "Found forbidden pattern list//attribute\n",
6347 NULL, NULL);
6348 }
6349 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6350 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6351 "Found forbidden pattern oneOrMore//group//attribute\n",
6352 NULL, NULL);
6353 }
6354 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6355 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6356 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6357 NULL, NULL);
6358 }
6359 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6360 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6361 "Found forbidden pattern data/except//attribute\n",
6362 NULL, NULL);
6363 }
6364 if (flags & XML_RELAXNG_IN_START) {
6365 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6366 "Found forbidden pattern start//attribute\n",
6367 NULL, NULL);
6368 }
6369 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6370 && (cur->name == NULL)) {
6371 if (cur->ns == NULL) {
6372 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6373 "Found anyName attribute without oneOrMore ancestor\n",
6374 NULL, NULL);
6375 } else {
6376 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6377 "Found nsName attribute without oneOrMore ancestor\n",
6378 NULL, NULL);
6379 }
6380 }
6381 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6382 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6383 ret = XML_RELAXNG_CONTENT_EMPTY;
6384 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6385 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6386 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6387 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6388 "Found forbidden pattern data/except//oneOrMore\n",
6389 NULL, NULL);
6390 }
6391 if (flags & XML_RELAXNG_IN_START) {
6392 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6393 "Found forbidden pattern start//oneOrMore\n",
6394 NULL, NULL);
6395 }
6396 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6397 ret =
6398 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6399 cur->type);
6400 ret = xmlRelaxNGGroupContentType(ret, ret);
6401 } else if (cur->type == XML_RELAXNG_LIST) {
6402 if (flags & XML_RELAXNG_IN_LIST) {
6403 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6404 "Found forbidden pattern list//list\n", NULL,
6405 NULL);
6406 }
6407 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6408 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6409 "Found forbidden pattern data/except//list\n",
6410 NULL, NULL);
6411 }
6412 if (flags & XML_RELAXNG_IN_START) {
6413 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6414 "Found forbidden pattern start//list\n", NULL,
6415 NULL);
6416 }
6417 nflags = flags | XML_RELAXNG_IN_LIST;
6418 ret =
6419 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6420 cur->type);
6421 } else if (cur->type == XML_RELAXNG_GROUP) {
6422 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6423 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6424 "Found forbidden pattern data/except//group\n",
6425 NULL, NULL);
6426 }
6427 if (flags & XML_RELAXNG_IN_START) {
6428 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6429 "Found forbidden pattern start//group\n", NULL,
6430 NULL);
6431 }
6432 if (flags & XML_RELAXNG_IN_ONEORMORE)
6433 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6434 else
6435 nflags = flags;
6436 ret =
6437 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6438 cur->type);
6439 /*
6440 * The 7.3 Attribute derivation rule for groups is plugged there
6441 */
6442 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6443 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6444 if (flags & XML_RELAXNG_IN_LIST) {
6445 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6446 "Found forbidden pattern list//interleave\n",
6447 NULL, NULL);
6448 }
6449 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6450 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6451 "Found forbidden pattern data/except//interleave\n",
6452 NULL, NULL);
6453 }
6454 if (flags & XML_RELAXNG_IN_START) {
6455 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6456 "Found forbidden pattern start//interleave\n",
6457 NULL, NULL);
6458 }
6459 if (flags & XML_RELAXNG_IN_ONEORMORE)
6460 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6461 else
6462 nflags = flags;
6463 ret =
6464 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6465 cur->type);
6466 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6467 if ((cur->parent != NULL) &&
6468 (cur->parent->type == XML_RELAXNG_DATATYPE))
6469 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6470 else
6471 nflags = flags;
6472 ret =
6473 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6474 cur->type);
6475 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6476 if (flags & XML_RELAXNG_IN_START) {
6477 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6478 "Found forbidden pattern start//data\n", NULL,
6479 NULL);
6480 }
6481 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6482 ret = XML_RELAXNG_CONTENT_SIMPLE;
6483 } else if (cur->type == XML_RELAXNG_VALUE) {
6484 if (flags & XML_RELAXNG_IN_START) {
6485 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6486 "Found forbidden pattern start//value\n", NULL,
6487 NULL);
6488 }
6489 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6490 ret = XML_RELAXNG_CONTENT_SIMPLE;
6491 } else if (cur->type == XML_RELAXNG_TEXT) {
6492 if (flags & XML_RELAXNG_IN_LIST) {
6493 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6494 "Found forbidden pattern list//text\n", NULL,
6495 NULL);
6496 }
6497 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6498 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6499 "Found forbidden pattern data/except//text\n",
6500 NULL, NULL);
6501 }
6502 if (flags & XML_RELAXNG_IN_START) {
6503 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6504 "Found forbidden pattern start//text\n", NULL,
6505 NULL);
6506 }
6507 ret = XML_RELAXNG_CONTENT_COMPLEX;
6508 } else if (cur->type == XML_RELAXNG_EMPTY) {
6509 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6510 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6511 "Found forbidden pattern data/except//empty\n",
6512 NULL, NULL);
6513 }
6514 if (flags & XML_RELAXNG_IN_START) {
6515 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6516 "Found forbidden pattern start//empty\n", NULL,
6517 NULL);
6518 }
6519 ret = XML_RELAXNG_CONTENT_EMPTY;
6520 } else if (cur->type == XML_RELAXNG_CHOICE) {
6521 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6522 ret =
6523 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6524 } else {
6525 ret =
6526 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6527 }
6528 cur = cur->next;
6529 if (ptype == XML_RELAXNG_GROUP) {
6530 val = xmlRelaxNGGroupContentType(val, ret);
6531 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02006532 /*
6533 * TODO: scan complain that tmp is never used, seems on purpose
6534 * need double-checking
6535 */
Daniel Veillard4c004142003-10-07 11:33:24 +00006536 tmp = xmlRelaxNGGroupContentType(val, ret);
6537 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6538 tmp = xmlRelaxNGMaxContentType(val, ret);
6539 } else if (ptype == XML_RELAXNG_CHOICE) {
6540 val = xmlRelaxNGMaxContentType(val, ret);
6541 } else if (ptype == XML_RELAXNG_LIST) {
6542 val = XML_RELAXNG_CONTENT_SIMPLE;
6543 } else if (ptype == XML_RELAXNG_EXCEPT) {
6544 if (ret == XML_RELAXNG_CONTENT_ERROR)
6545 val = XML_RELAXNG_CONTENT_ERROR;
6546 else
6547 val = XML_RELAXNG_CONTENT_SIMPLE;
6548 } else {
6549 val = xmlRelaxNGGroupContentType(val, ret);
6550 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006551
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006552 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006553 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006554}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006555
6556/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006557 * xmlRelaxNGParseGrammar:
6558 * @ctxt: a Relax-NG parser context
6559 * @nodes: grammar children nodes
6560 *
6561 * parse a Relax-NG <grammar> node
6562 *
6563 * Returns the internal xmlRelaxNGGrammarPtr built or
6564 * NULL in case of error
6565 */
6566static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006567xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6568{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006569 xmlRelaxNGGrammarPtr ret, tmp, old;
6570
Daniel Veillardc482e262003-02-26 14:48:48 +00006571#ifdef DEBUG_GRAMMAR
6572 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6573#endif
6574
Daniel Veillard6eadf632003-01-23 18:29:16 +00006575 ret = xmlRelaxNGNewGrammar(ctxt);
6576 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006577 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006578
6579 /*
6580 * Link the new grammar in the tree
6581 */
6582 ret->parent = ctxt->grammar;
6583 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006584 tmp = ctxt->grammar->children;
6585 if (tmp == NULL) {
6586 ctxt->grammar->children = ret;
6587 } else {
6588 while (tmp->next != NULL)
6589 tmp = tmp->next;
6590 tmp->next = ret;
6591 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006592 }
6593
6594 old = ctxt->grammar;
6595 ctxt->grammar = ret;
6596 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6597 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006598 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006599 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6600 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006601 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006602 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6603 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006604 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006605
6606 /*
Jan Pokornýacace882014-06-09 23:45:24 +02006607 * Apply 4.17 merging rules to defines and starts
Daniel Veillard6eadf632003-01-23 18:29:16 +00006608 */
6609 xmlRelaxNGCombineStart(ctxt, ret);
6610 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006611 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6612 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006613 }
6614
6615 /*
6616 * link together defines and refs in this grammar
6617 */
6618 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006619 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6620 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006621 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006622
Daniel Veillard81c51e12009-08-14 18:52:10 +02006623
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006624 /* @@@@ */
6625
Daniel Veillard6eadf632003-01-23 18:29:16 +00006626 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006627 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006628}
6629
6630/**
6631 * xmlRelaxNGParseDocument:
6632 * @ctxt: a Relax-NG parser context
6633 * @node: the root node of the RelaxNG schema
6634 *
6635 * parse a Relax-NG definition resource and build an internal
6636 * xmlRelaxNG struture which can be used to validate instances.
6637 *
6638 * Returns the internal XML RelaxNG structure built or
6639 * NULL in case of error
6640 */
6641static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006642xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6643{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006644 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006645 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006646 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006647
6648 if ((ctxt == NULL) || (node == NULL))
6649 return (NULL);
6650
6651 schema = xmlRelaxNGNewRelaxNG(ctxt);
6652 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006653 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006654
Daniel Veillard276be4a2003-01-24 01:03:34 +00006655 olddefine = ctxt->define;
6656 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006657 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006658 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard42870f42014-07-26 21:04:54 +08006659 if (schema->topgrammar == NULL) {
6660 xmlRelaxNGFree(schema);
6661 return (NULL);
6662 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006663 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006664 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006665
Daniel Veillard4c004142003-10-07 11:33:24 +00006666 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6667 if (schema->topgrammar == NULL) {
Daniel Veillard42870f42014-07-26 21:04:54 +08006668 xmlRelaxNGFree(schema);
6669 return (NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00006670 }
6671 /*
6672 * Link the new grammar in the tree
6673 */
6674 ret->parent = ctxt->grammar;
6675 if (ctxt->grammar != NULL) {
6676 tmp = ctxt->grammar->children;
6677 if (tmp == NULL) {
6678 ctxt->grammar->children = ret;
6679 } else {
6680 while (tmp->next != NULL)
6681 tmp = tmp->next;
6682 tmp->next = ret;
6683 }
6684 }
6685 old = ctxt->grammar;
6686 ctxt->grammar = ret;
6687 xmlRelaxNGParseStart(ctxt, node);
6688 if (old != NULL)
6689 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006690 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006691 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006692 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006693 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6694 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6695 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6696 while ((schema->topgrammar->start != NULL) &&
6697 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6698 (schema->topgrammar->start->next != NULL))
6699 schema->topgrammar->start =
6700 schema->topgrammar->start->content;
6701 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6702 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6703 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006704 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006705#ifdef DEBUG
6706 if (schema == NULL)
6707 xmlGenericError(xmlGenericErrorContext,
6708 "xmlRelaxNGParseDocument() failed\n");
6709#endif
6710
6711 return (schema);
6712}
6713
6714/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006715 * *
6716 * Reading RelaxNGs *
6717 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00006718 ************************************************************************/
6719
6720/**
6721 * xmlRelaxNGNewParserCtxt:
6722 * @URL: the location of the schema
6723 *
6724 * Create an XML RelaxNGs parse context for that file/resource expected
6725 * to contain an XML RelaxNGs file.
6726 *
6727 * Returns the parser context or NULL in case of error
6728 */
6729xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006730xmlRelaxNGNewParserCtxt(const char *URL)
6731{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006732 xmlRelaxNGParserCtxtPtr ret;
6733
6734 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006735 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006736
Daniel Veillard4c004142003-10-07 11:33:24 +00006737 ret =
6738 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006739 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006740 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006741 return (NULL);
6742 }
6743 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006744 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006745 ret->error = xmlGenericError;
6746 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006747 return (ret);
6748}
6749
6750/**
6751 * xmlRelaxNGNewMemParserCtxt:
6752 * @buffer: a pointer to a char array containing the schemas
6753 * @size: the size of the array
6754 *
6755 * Create an XML RelaxNGs parse context for that memory buffer expected
6756 * to contain an XML RelaxNGs file.
6757 *
6758 * Returns the parser context or NULL in case of error
6759 */
6760xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006761xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6762{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006763 xmlRelaxNGParserCtxtPtr ret;
6764
6765 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006766 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006767
Daniel Veillard4c004142003-10-07 11:33:24 +00006768 ret =
6769 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006770 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006771 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006772 return (NULL);
6773 }
6774 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6775 ret->buffer = buffer;
6776 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006777 ret->error = xmlGenericError;
6778 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006779 return (ret);
6780}
6781
6782/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006783 * xmlRelaxNGNewDocParserCtxt:
6784 * @doc: a preparsed document tree
6785 *
6786 * Create an XML RelaxNGs parser context for that document.
6787 * Note: since the process of compiling a RelaxNG schemas modifies the
6788 * document, the @doc parameter is duplicated internally.
6789 *
6790 * Returns the parser context or NULL in case of error
6791 */
6792xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006793xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6794{
Daniel Veillard33300b42003-04-17 09:09:19 +00006795 xmlRelaxNGParserCtxtPtr ret;
6796 xmlDocPtr copy;
6797
6798 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006799 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006800 copy = xmlCopyDoc(doc, 1);
6801 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006802 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006803
Daniel Veillard4c004142003-10-07 11:33:24 +00006804 ret =
6805 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006806 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006807 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006808 return (NULL);
6809 }
6810 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6811 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006812 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006813 ret->userData = xmlGenericErrorContext;
6814 return (ret);
6815}
6816
6817/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006818 * xmlRelaxNGFreeParserCtxt:
6819 * @ctxt: the schema parser context
6820 *
6821 * Free the resources associated to the schema parser context
6822 */
6823void
Daniel Veillard4c004142003-10-07 11:33:24 +00006824xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6825{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006826 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006827 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006828 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006829 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006830 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006831 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006832 if (ctxt->interleaves != NULL)
6833 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006834 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006835 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006836 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006837 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006838 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006839 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006840 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006841 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006842 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006843 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006844
Daniel Veillard4c004142003-10-07 11:33:24 +00006845 for (i = 0; i < ctxt->defNr; i++)
6846 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6847 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006848 }
Daniel Veillard42595322004-11-08 10:52:06 +00006849 if ((ctxt->document != NULL) && (ctxt->freedoc))
6850 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006851 xmlFree(ctxt);
6852}
6853
Daniel Veillard6eadf632003-01-23 18:29:16 +00006854/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006855 * xmlRelaxNGNormExtSpace:
6856 * @value: a value
6857 *
6858 * Removes the leading and ending spaces of the value
6859 * The string is modified "in situ"
6860 */
6861static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006862xmlRelaxNGNormExtSpace(xmlChar * value)
6863{
Daniel Veillardd2298792003-02-14 16:54:11 +00006864 xmlChar *start = value;
6865 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006866
Daniel Veillard4c004142003-10-07 11:33:24 +00006867 if (value == NULL)
6868 return;
6869
William M. Brack76e95df2003-10-18 16:20:14 +00006870 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006871 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006872 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006873 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006874 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006875 cur++;
6876 if (*cur == 0)
6877 return;
6878 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006879 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006880 cur++;
6881 if (*cur == 0) {
6882 *start = 0;
6883 return;
6884 }
6885 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006886 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006887 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006888 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006889 *start++ = *cur++;
6890 if (*cur == 0) {
6891 *start = 0;
6892 return;
6893 }
6894 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006895 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006896 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006897 if (*cur == 0) {
6898 *start = 0;
6899 return;
6900 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006901 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006902 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006903 }
6904}
6905
6906/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006907 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006908 * @ctxt: a Relax-NG parser context
6909 * @node: a Relax-NG node
6910 *
6911 * Check all the attributes on the given node
6912 */
6913static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006914xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6915{
Daniel Veillardd2298792003-02-14 16:54:11 +00006916 xmlAttrPtr cur, next;
6917
6918 cur = node->properties;
6919 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006920 next = cur->next;
6921 if ((cur->ns == NULL) ||
6922 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6923 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6924 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6925 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6926 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6927 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6928 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6929 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6930 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6931 "Attribute %s is not allowed on %s\n",
6932 cur->name, node->name);
6933 }
6934 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6935 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6936 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6937 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6938 "Attribute %s is not allowed on %s\n",
6939 cur->name, node->name);
6940 }
6941 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6942 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6943 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6944 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6945 "Attribute %s is not allowed on %s\n",
6946 cur->name, node->name);
6947 }
6948 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6949 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6950 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6951 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6952 "Attribute %s is not allowed on %s\n",
6953 cur->name, node->name);
6954 }
6955 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6956 xmlChar *val;
6957 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006958
Daniel Veillard4c004142003-10-07 11:33:24 +00006959 val = xmlNodeListGetString(node->doc, cur->children, 1);
6960 if (val != NULL) {
6961 if (val[0] != 0) {
6962 uri = xmlParseURI((const char *) val);
6963 if (uri == NULL) {
6964 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6965 "Attribute %s contains invalid URI %s\n",
6966 cur->name, val);
6967 } else {
6968 if (uri->scheme == NULL) {
6969 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6970 "Attribute %s URI %s is not absolute\n",
6971 cur->name, val);
6972 }
6973 if (uri->fragment != NULL) {
6974 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6975 "Attribute %s URI %s has a fragment ID\n",
6976 cur->name, val);
6977 }
6978 xmlFreeURI(uri);
6979 }
6980 }
6981 xmlFree(val);
6982 }
6983 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6984 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6985 "Unknown attribute %s on %s\n", cur->name,
6986 node->name);
6987 }
6988 }
6989 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006990 }
6991}
6992
6993/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006994 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006995 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006996 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006997 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006998 * Cleanup the subtree from unwanted nodes for parsing, resolve
6999 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00007000 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00007001static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007002xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
7003{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007004 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007005
Daniel Veillard6eadf632003-01-23 18:29:16 +00007006 delete = NULL;
7007 cur = root;
7008 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007009 if (delete != NULL) {
7010 xmlUnlinkNode(delete);
7011 xmlFreeNode(delete);
7012 delete = NULL;
7013 }
7014 if (cur->type == XML_ELEMENT_NODE) {
7015 /*
7016 * Simplification 4.1. Annotations
7017 */
7018 if ((cur->ns == NULL) ||
7019 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
7020 if ((cur->parent != NULL) &&
7021 (cur->parent->type == XML_ELEMENT_NODE) &&
7022 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7023 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7024 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7025 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7026 "element %s doesn't allow foreign elements\n",
7027 cur->parent->name, NULL);
7028 }
7029 delete = cur;
7030 goto skip_children;
7031 } else {
7032 xmlRelaxNGCleanupAttributes(ctxt, cur);
7033 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7034 xmlChar *href, *ns, *base, *URL;
7035 xmlRelaxNGDocumentPtr docu;
7036 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00007037 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007038
Daniel Veillard4c004142003-10-07 11:33:24 +00007039 ns = xmlGetProp(cur, BAD_CAST "ns");
7040 if (ns == NULL) {
7041 tmp = cur->parent;
7042 while ((tmp != NULL) &&
7043 (tmp->type == XML_ELEMENT_NODE)) {
7044 ns = xmlGetProp(tmp, BAD_CAST "ns");
7045 if (ns != NULL)
7046 break;
7047 tmp = tmp->parent;
7048 }
7049 }
7050 href = xmlGetProp(cur, BAD_CAST "href");
7051 if (href == NULL) {
7052 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7053 "xmlRelaxNGParse: externalRef has no href attribute\n",
7054 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007055 if (ns != NULL)
7056 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007057 delete = cur;
7058 goto skip_children;
7059 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007060 uri = xmlParseURI((const char *) href);
7061 if (uri == NULL) {
7062 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7063 "Incorrect URI for externalRef %s\n",
7064 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007065 if (ns != NULL)
7066 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007067 if (href != NULL)
7068 xmlFree(href);
7069 delete = cur;
7070 goto skip_children;
7071 }
7072 if (uri->fragment != NULL) {
7073 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7074 "Fragment forbidden in URI for externalRef %s\n",
7075 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007076 if (ns != NULL)
7077 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007078 xmlFreeURI(uri);
7079 if (href != NULL)
7080 xmlFree(href);
7081 delete = cur;
7082 goto skip_children;
7083 }
7084 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007085 base = xmlNodeGetBase(cur->doc, cur);
7086 URL = xmlBuildURI(href, base);
7087 if (URL == NULL) {
7088 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7089 "Failed to compute URL for externalRef %s\n",
7090 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007091 if (ns != NULL)
7092 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007093 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 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7105 if (docu == NULL) {
7106 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7107 "Failed to load externalRef %s\n", URL,
7108 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007109 if (ns != NULL)
7110 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007111 xmlFree(URL);
7112 delete = cur;
7113 goto skip_children;
7114 }
7115 if (ns != NULL)
7116 xmlFree(ns);
7117 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007118 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007119 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7120 xmlChar *href, *ns, *base, *URL;
7121 xmlRelaxNGIncludePtr incl;
7122 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007123
Daniel Veillard4c004142003-10-07 11:33:24 +00007124 href = xmlGetProp(cur, BAD_CAST "href");
7125 if (href == NULL) {
7126 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7127 "xmlRelaxNGParse: include has no href attribute\n",
7128 NULL, NULL);
7129 delete = cur;
7130 goto skip_children;
7131 }
7132 base = xmlNodeGetBase(cur->doc, cur);
7133 URL = xmlBuildURI(href, base);
7134 if (URL == NULL) {
7135 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7136 "Failed to compute URL for include %s\n",
7137 href, NULL);
7138 if (href != NULL)
7139 xmlFree(href);
7140 if (base != NULL)
7141 xmlFree(base);
7142 delete = cur;
7143 goto skip_children;
7144 }
7145 if (href != NULL)
7146 xmlFree(href);
7147 if (base != NULL)
7148 xmlFree(base);
7149 ns = xmlGetProp(cur, BAD_CAST "ns");
7150 if (ns == NULL) {
7151 tmp = cur->parent;
7152 while ((tmp != NULL) &&
7153 (tmp->type == XML_ELEMENT_NODE)) {
7154 ns = xmlGetProp(tmp, BAD_CAST "ns");
7155 if (ns != NULL)
7156 break;
7157 tmp = tmp->parent;
7158 }
7159 }
7160 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7161 if (ns != NULL)
7162 xmlFree(ns);
7163 if (incl == NULL) {
7164 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7165 "Failed to load include %s\n", URL,
7166 NULL);
7167 xmlFree(URL);
7168 delete = cur;
7169 goto skip_children;
7170 }
7171 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007172 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007173 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7174 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7175 {
7176 xmlChar *name, *ns;
7177 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007178
Daniel Veillard4c004142003-10-07 11:33:24 +00007179 /*
7180 * Simplification 4.8. name attribute of element
7181 * and attribute elements
7182 */
7183 name = xmlGetProp(cur, BAD_CAST "name");
7184 if (name != NULL) {
7185 if (cur->children == NULL) {
7186 text =
7187 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7188 name);
7189 } else {
7190 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007191
Daniel Veillard03a53c32004-10-26 16:06:51 +00007192 node = xmlNewDocNode(cur->doc, cur->ns,
7193 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007194 if (node != NULL) {
7195 xmlAddPrevSibling(cur->children, node);
7196 text = xmlNewText(name);
7197 xmlAddChild(node, text);
7198 text = node;
7199 }
7200 }
7201 if (text == NULL) {
7202 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7203 "Failed to create a name %s element\n",
7204 name, NULL);
7205 }
7206 xmlUnsetProp(cur, BAD_CAST "name");
7207 xmlFree(name);
7208 ns = xmlGetProp(cur, BAD_CAST "ns");
7209 if (ns != NULL) {
7210 if (text != NULL) {
7211 xmlSetProp(text, BAD_CAST "ns", ns);
7212 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7213 }
7214 xmlFree(ns);
7215 } else if (xmlStrEqual(cur->name,
7216 BAD_CAST "attribute")) {
7217 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7218 }
7219 }
7220 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7221 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7222 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7223 /*
7224 * Simplification 4.8. name attribute of element
7225 * and attribute elements
7226 */
7227 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7228 xmlNodePtr node;
7229 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007230
Daniel Veillard4c004142003-10-07 11:33:24 +00007231 node = cur->parent;
7232 while ((node != NULL) &&
7233 (node->type == XML_ELEMENT_NODE)) {
7234 ns = xmlGetProp(node, BAD_CAST "ns");
7235 if (ns != NULL) {
7236 break;
7237 }
7238 node = node->parent;
7239 }
7240 if (ns == NULL) {
7241 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7242 } else {
7243 xmlSetProp(cur, BAD_CAST "ns", ns);
7244 xmlFree(ns);
7245 }
7246 }
7247 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7248 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007249
Daniel Veillard4c004142003-10-07 11:33:24 +00007250 /*
7251 * Simplification: 4.10. QNames
7252 */
7253 name = xmlNodeGetContent(cur);
7254 if (name != NULL) {
7255 local = xmlSplitQName2(name, &prefix);
7256 if (local != NULL) {
7257 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007258
Daniel Veillard4c004142003-10-07 11:33:24 +00007259 ns = xmlSearchNs(cur->doc, cur, prefix);
7260 if (ns == NULL) {
7261 xmlRngPErr(ctxt, cur,
7262 XML_RNGP_PREFIX_UNDEFINED,
7263 "xmlRelaxNGParse: no namespace for prefix %s\n",
7264 prefix, NULL);
7265 } else {
7266 xmlSetProp(cur, BAD_CAST "ns",
7267 ns->href);
7268 xmlNodeSetContent(cur, local);
7269 }
7270 xmlFree(local);
7271 xmlFree(prefix);
7272 }
7273 xmlFree(name);
7274 }
7275 }
7276 /*
7277 * 4.16
7278 */
7279 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7280 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7281 xmlRngPErr(ctxt, cur,
7282 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7283 "Found nsName/except//nsName forbidden construct\n",
7284 NULL, NULL);
7285 }
7286 }
7287 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7288 (cur != root)) {
7289 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007290
Daniel Veillard4c004142003-10-07 11:33:24 +00007291 /*
7292 * 4.16
7293 */
7294 if ((cur->parent != NULL) &&
7295 (xmlStrEqual
7296 (cur->parent->name, BAD_CAST "anyName"))) {
7297 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7298 xmlRelaxNGCleanupTree(ctxt, cur);
7299 ctxt->flags = oldflags;
7300 goto skip_children;
7301 } else if ((cur->parent != NULL) &&
7302 (xmlStrEqual
7303 (cur->parent->name, BAD_CAST "nsName"))) {
7304 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7305 xmlRelaxNGCleanupTree(ctxt, cur);
7306 ctxt->flags = oldflags;
7307 goto skip_children;
7308 }
7309 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7310 /*
7311 * 4.16
7312 */
7313 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7314 xmlRngPErr(ctxt, cur,
7315 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7316 "Found anyName/except//anyName forbidden construct\n",
7317 NULL, NULL);
7318 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7319 xmlRngPErr(ctxt, cur,
7320 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7321 "Found nsName/except//anyName forbidden construct\n",
7322 NULL, NULL);
7323 }
7324 }
7325 /*
Jan Pokornýacace882014-06-09 23:45:24 +02007326 * This is not an else since "include" is transformed
Daniel Veillard4c004142003-10-07 11:33:24 +00007327 * into a div
7328 */
7329 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7330 xmlChar *ns;
7331 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007332
Daniel Veillard4c004142003-10-07 11:33:24 +00007333 /*
7334 * implements rule 4.11
7335 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007336
Daniel Veillard4c004142003-10-07 11:33:24 +00007337 ns = xmlGetProp(cur, BAD_CAST "ns");
7338
7339 child = cur->children;
7340 ins = cur;
7341 while (child != NULL) {
7342 if (ns != NULL) {
7343 if (!xmlHasProp(child, BAD_CAST "ns")) {
7344 xmlSetProp(child, BAD_CAST "ns", ns);
7345 }
7346 }
7347 tmp = child->next;
7348 xmlUnlinkNode(child);
7349 ins = xmlAddNextSibling(ins, child);
7350 child = tmp;
7351 }
7352 if (ns != NULL)
7353 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007354 /*
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007355 * Since we are about to delete cur, if its nsDef is non-NULL we
William M. Brack8eabb052004-06-07 14:15:54 +00007356 * need to preserve it (it contains the ns definitions for the
7357 * children we just moved). We'll just stick it on to the end
7358 * of cur->parent's list, since it's never going to be re-serialized
7359 * (bug 143738).
7360 */
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007361 if ((cur->nsDef != NULL) && (cur->parent != NULL)) {
William M. Brack8eabb052004-06-07 14:15:54 +00007362 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7363 while (parDef->next != NULL)
7364 parDef = parDef->next;
7365 parDef->next = cur->nsDef;
7366 cur->nsDef = NULL;
7367 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007368 delete = cur;
7369 goto skip_children;
7370 }
7371 }
7372 }
7373 /*
7374 * Simplification 4.2 whitespaces
7375 */
7376 else if ((cur->type == XML_TEXT_NODE) ||
7377 (cur->type == XML_CDATA_SECTION_NODE)) {
7378 if (IS_BLANK_NODE(cur)) {
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007379 if ((cur->parent != NULL) &&
7380 (cur->parent->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007381 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7382 &&
7383 (!xmlStrEqual
7384 (cur->parent->name, BAD_CAST "param")))
7385 delete = cur;
7386 } else {
7387 delete = cur;
7388 goto skip_children;
7389 }
7390 }
7391 } else {
7392 delete = cur;
7393 goto skip_children;
7394 }
7395
7396 /*
7397 * Skip to next node
7398 */
7399 if (cur->children != NULL) {
7400 if ((cur->children->type != XML_ENTITY_DECL) &&
7401 (cur->children->type != XML_ENTITY_REF_NODE) &&
7402 (cur->children->type != XML_ENTITY_NODE)) {
7403 cur = cur->children;
7404 continue;
7405 }
7406 }
7407 skip_children:
7408 if (cur->next != NULL) {
7409 cur = cur->next;
7410 continue;
7411 }
7412
7413 do {
7414 cur = cur->parent;
7415 if (cur == NULL)
7416 break;
7417 if (cur == root) {
7418 cur = NULL;
7419 break;
7420 }
7421 if (cur->next != NULL) {
7422 cur = cur->next;
7423 break;
7424 }
7425 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007426 }
7427 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007428 xmlUnlinkNode(delete);
7429 xmlFreeNode(delete);
7430 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007431 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007432}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007433
Daniel Veillardc5312d72003-02-21 17:14:10 +00007434/**
7435 * xmlRelaxNGCleanupDoc:
7436 * @ctxt: a Relax-NG parser context
7437 * @doc: an xmldocPtr document pointer
7438 *
7439 * Cleanup the document from unwanted nodes for parsing, resolve
7440 * Include and externalRef lookups.
7441 *
7442 * Returns the cleaned up document or NULL in case of error
7443 */
7444static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007445xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7446{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007447 xmlNodePtr root;
7448
7449 /*
7450 * Extract the root
7451 */
7452 root = xmlDocGetRootElement(doc);
7453 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007454 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7455 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007456 return (NULL);
7457 }
7458 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007459 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007460}
7461
7462/**
7463 * xmlRelaxNGParse:
7464 * @ctxt: a Relax-NG parser context
7465 *
7466 * parse a schema definition resource and build an internal
7467 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007468 *
7469 * Returns the internal XML RelaxNG structure built from the resource or
7470 * NULL in case of error
7471 */
7472xmlRelaxNGPtr
7473xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7474{
7475 xmlRelaxNGPtr ret = NULL;
7476 xmlDocPtr doc;
7477 xmlNodePtr root;
7478
7479 xmlRelaxNGInitTypes();
7480
7481 if (ctxt == NULL)
7482 return (NULL);
7483
7484 /*
7485 * First step is to parse the input document into an DOM/Infoset
7486 */
7487 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007488 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007489 if (doc == NULL) {
7490 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7491 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7492 NULL);
7493 return (NULL);
7494 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007495 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007496 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007497 if (doc == NULL) {
7498 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7499 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7500 NULL);
7501 return (NULL);
7502 }
7503 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7504 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007505 } else if (ctxt->document != NULL) {
7506 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007507 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007508 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7509 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7510 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007511 }
7512 ctxt->document = doc;
7513
7514 /*
7515 * Some preprocessing of the document content
7516 */
7517 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7518 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007519 xmlFreeDoc(ctxt->document);
7520 ctxt->document = NULL;
7521 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007522 }
7523
Daniel Veillard6eadf632003-01-23 18:29:16 +00007524 /*
7525 * Then do the parsing for good
7526 */
7527 root = xmlDocGetRootElement(doc);
7528 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007529 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7530 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007531 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007532
Daniel Veillard3f845a92006-04-13 07:33:44 +00007533 xmlFreeDoc(ctxt->document);
7534 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007535 return (NULL);
7536 }
7537 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007538 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007539 xmlFreeDoc(ctxt->document);
7540 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007541 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007542 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007543
7544 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007545 * Check the ref/defines links
7546 */
7547 /*
7548 * try to preprocess interleaves
7549 */
7550 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007551 xmlHashScan(ctxt->interleaves,
7552 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007553 }
7554
7555 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007556 * if there was a parsing error return NULL
7557 */
7558 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007559 xmlRelaxNGFree(ret);
7560 ctxt->document = NULL;
7561 xmlFreeDoc(doc);
7562 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007563 }
7564
7565 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007566 * try to compile (parts of) the schemas
7567 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007568 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7569 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007570 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007571
Daniel Veillard4c004142003-10-07 11:33:24 +00007572 def = xmlRelaxNGNewDefine(ctxt, NULL);
7573 if (def != NULL) {
7574 def->type = XML_RELAXNG_START;
7575 def->content = ret->topgrammar->start;
7576 ret->topgrammar->start = def;
7577 }
7578 }
7579 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007580 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007581
7582 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007583 * Transfer the pointer for cleanup at the schema level.
7584 */
7585 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007586 ctxt->document = NULL;
7587 ret->documents = ctxt->documents;
7588 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007589
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007590 ret->includes = ctxt->includes;
7591 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007592 ret->defNr = ctxt->defNr;
7593 ret->defTab = ctxt->defTab;
7594 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007595 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007596 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007597
7598 return (ret);
7599}
Daniel Veillard4c004142003-10-07 11:33:24 +00007600
Daniel Veillard6eadf632003-01-23 18:29:16 +00007601/**
7602 * xmlRelaxNGSetParserErrors:
7603 * @ctxt: a Relax-NG validation context
7604 * @err: the error callback
7605 * @warn: the warning callback
7606 * @ctx: contextual data for the callbacks
7607 *
7608 * Set the callback functions used to handle errors for a validation context
7609 */
7610void
7611xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007612 xmlRelaxNGValidityErrorFunc err,
7613 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7614{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007615 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007616 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007617 ctxt->error = err;
7618 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007619 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007620 ctxt->userData = ctx;
7621}
Daniel Veillard409a8142003-07-18 15:16:57 +00007622
7623/**
7624 * xmlRelaxNGGetParserErrors:
7625 * @ctxt: a Relax-NG validation context
7626 * @err: the error callback result
7627 * @warn: the warning callback result
7628 * @ctx: contextual data for the callbacks result
7629 *
7630 * Get the callback information used to handle errors for a validation context
7631 *
7632 * Returns -1 in case of failure, 0 otherwise.
7633 */
7634int
7635xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007636 xmlRelaxNGValidityErrorFunc * err,
7637 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7638{
Daniel Veillard409a8142003-07-18 15:16:57 +00007639 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007640 return (-1);
7641 if (err != NULL)
7642 *err = ctxt->error;
7643 if (warn != NULL)
7644 *warn = ctxt->warning;
7645 if (ctx != NULL)
7646 *ctx = ctxt->userData;
7647 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007648}
7649
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007650/**
7651 * xmlRelaxNGSetParserStructuredErrors:
7652 * @ctxt: a Relax-NG parser context
7653 * @serror: the error callback
7654 * @ctx: contextual data for the callbacks
7655 *
7656 * Set the callback functions used to handle errors for a parsing context
7657 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007658void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007659xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007660 xmlStructuredErrorFunc serror,
7661 void *ctx)
7662{
7663 if (ctxt == NULL)
7664 return;
7665 ctxt->serror = serror;
7666 ctxt->error = NULL;
7667 ctxt->warning = NULL;
7668 ctxt->userData = ctx;
7669}
7670
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007671#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007672
Daniel Veillard6eadf632003-01-23 18:29:16 +00007673/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007674 * *
7675 * Dump back a compiled form *
7676 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007677 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007678static void xmlRelaxNGDumpDefine(FILE * output,
7679 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007680
7681/**
7682 * xmlRelaxNGDumpDefines:
7683 * @output: the file output
7684 * @defines: a list of define structures
7685 *
7686 * Dump a RelaxNG structure back
7687 */
7688static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007689xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7690{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007691 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007692 xmlRelaxNGDumpDefine(output, defines);
7693 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007694 }
7695}
7696
7697/**
7698 * xmlRelaxNGDumpDefine:
7699 * @output: the file output
7700 * @define: a define structure
7701 *
7702 * Dump a RelaxNG structure back
7703 */
7704static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007705xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7706{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007707 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007708 return;
7709 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007710 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007711 fprintf(output, "<empty/>\n");
7712 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007713 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007714 fprintf(output, "<notAllowed/>\n");
7715 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007716 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007717 fprintf(output, "<text/>\n");
7718 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007719 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007720 fprintf(output, "<element>\n");
7721 if (define->name != NULL) {
7722 fprintf(output, "<name");
7723 if (define->ns != NULL)
7724 fprintf(output, " ns=\"%s\"", define->ns);
7725 fprintf(output, ">%s</name>\n", define->name);
7726 }
7727 xmlRelaxNGDumpDefines(output, define->attrs);
7728 xmlRelaxNGDumpDefines(output, define->content);
7729 fprintf(output, "</element>\n");
7730 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007731 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007732 fprintf(output, "<list>\n");
7733 xmlRelaxNGDumpDefines(output, define->content);
7734 fprintf(output, "</list>\n");
7735 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007736 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007737 fprintf(output, "<oneOrMore>\n");
7738 xmlRelaxNGDumpDefines(output, define->content);
7739 fprintf(output, "</oneOrMore>\n");
7740 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007741 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007742 fprintf(output, "<zeroOrMore>\n");
7743 xmlRelaxNGDumpDefines(output, define->content);
7744 fprintf(output, "</zeroOrMore>\n");
7745 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007746 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007747 fprintf(output, "<choice>\n");
7748 xmlRelaxNGDumpDefines(output, define->content);
7749 fprintf(output, "</choice>\n");
7750 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007751 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007752 fprintf(output, "<group>\n");
7753 xmlRelaxNGDumpDefines(output, define->content);
7754 fprintf(output, "</group>\n");
7755 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007756 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007757 fprintf(output, "<interleave>\n");
7758 xmlRelaxNGDumpDefines(output, define->content);
7759 fprintf(output, "</interleave>\n");
7760 break;
7761 case XML_RELAXNG_OPTIONAL:
7762 fprintf(output, "<optional>\n");
7763 xmlRelaxNGDumpDefines(output, define->content);
7764 fprintf(output, "</optional>\n");
7765 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007766 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007767 fprintf(output, "<attribute>\n");
7768 xmlRelaxNGDumpDefines(output, define->content);
7769 fprintf(output, "</attribute>\n");
7770 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007771 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007772 fprintf(output, "<define");
7773 if (define->name != NULL)
7774 fprintf(output, " name=\"%s\"", define->name);
7775 fprintf(output, ">\n");
7776 xmlRelaxNGDumpDefines(output, define->content);
7777 fprintf(output, "</define>\n");
7778 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007779 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007780 fprintf(output, "<ref");
7781 if (define->name != NULL)
7782 fprintf(output, " name=\"%s\"", define->name);
7783 fprintf(output, ">\n");
7784 xmlRelaxNGDumpDefines(output, define->content);
7785 fprintf(output, "</ref>\n");
7786 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007787 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007788 fprintf(output, "<parentRef");
7789 if (define->name != NULL)
7790 fprintf(output, " name=\"%s\"", define->name);
7791 fprintf(output, ">\n");
7792 xmlRelaxNGDumpDefines(output, define->content);
7793 fprintf(output, "</parentRef>\n");
7794 break;
7795 case XML_RELAXNG_EXTERNALREF:
7796 fprintf(output, "<externalRef>");
7797 xmlRelaxNGDumpDefines(output, define->content);
7798 fprintf(output, "</externalRef>\n");
7799 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007800 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007801 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007802 TODO break;
7803 case XML_RELAXNG_START:
7804 case XML_RELAXNG_EXCEPT:
7805 case XML_RELAXNG_PARAM:
7806 TODO break;
7807 case XML_RELAXNG_NOOP:
7808 xmlRelaxNGDumpDefines(output, define->content);
7809 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007810 }
7811}
Daniel Veillard4c004142003-10-07 11:33:24 +00007812
Daniel Veillard6eadf632003-01-23 18:29:16 +00007813/**
7814 * xmlRelaxNGDumpGrammar:
7815 * @output: the file output
7816 * @grammar: a grammar structure
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007817 * @top: is this a top grammar
Daniel Veillard6eadf632003-01-23 18:29:16 +00007818 *
7819 * Dump a RelaxNG structure back
7820 */
7821static void
7822xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7823{
7824 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007825 return;
7826
Daniel Veillard6eadf632003-01-23 18:29:16 +00007827 fprintf(output, "<grammar");
7828 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007829 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7830 switch (grammar->combine) {
7831 case XML_RELAXNG_COMBINE_UNDEFINED:
7832 break;
7833 case XML_RELAXNG_COMBINE_CHOICE:
7834 fprintf(output, " combine=\"choice\"");
7835 break;
7836 case XML_RELAXNG_COMBINE_INTERLEAVE:
7837 fprintf(output, " combine=\"interleave\"");
7838 break;
7839 default:
7840 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007841 }
7842 fprintf(output, ">\n");
7843 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007844 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007845 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007846 fprintf(output, "<start>\n");
7847 xmlRelaxNGDumpDefine(output, grammar->start);
7848 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007849 }
7850 /* TODO ? Dump the defines ? */
7851 fprintf(output, "</grammar>\n");
7852}
7853
7854/**
7855 * xmlRelaxNGDump:
7856 * @output: the file output
7857 * @schema: a schema structure
7858 *
7859 * Dump a RelaxNG structure back
7860 */
7861void
7862xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7863{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007864 if (output == NULL)
7865 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007866 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007867 fprintf(output, "RelaxNG empty or failed to compile\n");
7868 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007869 }
7870 fprintf(output, "RelaxNG: ");
7871 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007872 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007873 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007874 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007875 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007876 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007877 }
7878 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007879 fprintf(output, "RelaxNG has no top grammar\n");
7880 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007881 }
7882 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7883}
7884
Daniel Veillardfebcca42003-02-16 15:44:18 +00007885/**
7886 * xmlRelaxNGDumpTree:
7887 * @output: the file output
7888 * @schema: a schema structure
7889 *
7890 * Dump the transformed RelaxNG tree.
7891 */
7892void
7893xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7894{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007895 if (output == NULL)
7896 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007897 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007898 fprintf(output, "RelaxNG empty or failed to compile\n");
7899 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007900 }
7901 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007902 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007903 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007904 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007905 }
7906}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007907#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007908
Daniel Veillard6eadf632003-01-23 18:29:16 +00007909/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007910 * *
7911 * Validation of compiled content *
7912 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007913 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007914static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7915 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007916
7917/**
7918 * xmlRelaxNGValidateCompiledCallback:
7919 * @exec: the regular expression instance
7920 * @token: the token which matched
7921 * @transdata: callback data, the define for the subelement if available
7922 @ @inputdata: callback data, the Relax NG validation context
7923 *
7924 * Handle the callback and if needed validate the element children.
7925 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007926static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007927xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007928 const xmlChar * token,
7929 void *transdata, void *inputdata)
7930{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007931 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7932 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7933 int ret;
7934
7935#ifdef DEBUG_COMPILE
7936 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007937 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007938#endif
7939 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007940 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007941 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007942 }
7943 if (define == NULL) {
7944 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007945 return;
7946 fprintf(stderr, "callback on %s missing define\n", token);
7947 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7948 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7949 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007950 }
7951 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007952 fprintf(stderr, "callback on %s missing info\n", token);
7953 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7954 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7955 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007956 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007957 fprintf(stderr, "callback on %s define is not element\n", token);
7958 if (ctxt->errNo == XML_RELAXNG_OK)
7959 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7960 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007961 }
7962 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007963 if (ret != 0)
7964 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007965}
7966
7967/**
7968 * xmlRelaxNGValidateCompiledContent:
7969 * @ctxt: the RelaxNG validation context
7970 * @regexp: the regular expression as compiled
7971 * @content: list of children to test against the regexp
7972 *
7973 * Validate the content model of an element or start using the regexp
7974 *
7975 * Returns 0 in case of success, -1 in case of error.
7976 */
7977static int
7978xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007979 xmlRegexpPtr regexp, xmlNodePtr content)
7980{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007981 xmlRegExecCtxtPtr exec;
7982 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007983 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007984 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007985
7986 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007987 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007988 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007989 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007990 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007991 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007992 cur = content;
7993 while (cur != NULL) {
7994 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007995 switch (cur->type) {
7996 case XML_TEXT_NODE:
7997 case XML_CDATA_SECTION_NODE:
7998 if (xmlIsBlankNode(cur))
7999 break;
8000 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
8001 if (ret < 0) {
8002 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
8003 cur->parent->name);
8004 }
8005 break;
8006 case XML_ELEMENT_NODE:
8007 if (cur->ns != NULL) {
8008 ret = xmlRegExecPushString2(exec, cur->name,
8009 cur->ns->href, ctxt);
8010 } else {
8011 ret = xmlRegExecPushString(exec, cur->name, ctxt);
8012 }
8013 if (ret < 0) {
8014 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
8015 }
8016 break;
8017 default:
8018 break;
8019 }
8020 if (ret < 0)
8021 break;
8022 /*
8023 * Switch to next element
8024 */
8025 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008026 }
8027 ret = xmlRegExecPushString(exec, NULL, NULL);
8028 if (ret == 1) {
8029 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008030 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008031 } else if (ret == 0) {
8032 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008033 * TODO: get some of the names needed to exit the current state of exec
8034 */
8035 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8036 ret = -1;
8037 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8038 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008039 } else {
8040 ret = -1;
8041 }
8042 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008043 /*
8044 * There might be content model errors outside of the pure
8045 * regexp validation, e.g. for attribute values.
8046 */
8047 if ((ret == 0) && (ctxt->perr != 0)) {
8048 ret = ctxt->perr;
8049 }
8050 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008051 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008052}
8053
8054/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008055 * *
8056 * Progressive validation of when possible *
8057 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008058 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008059static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8060 xmlRelaxNGDefinePtr defines);
8061static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008062 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008063static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008064
8065/**
8066 * xmlRelaxNGElemPush:
8067 * @ctxt: the validation context
8068 * @exec: the regexp runtime for the new content model
8069 *
8070 * Push a new regexp for the current node content model on the stack
8071 *
8072 * Returns 0 in case of success and -1 in case of error.
8073 */
8074static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008075xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8076{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008077 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008078 ctxt->elemMax = 10;
8079 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8080 sizeof
8081 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008082 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008083 xmlRngVErrMemory(ctxt, "validating\n");
8084 return (-1);
8085 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008086 }
8087 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008088 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008089 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008090 ctxt->elemMax *
8091 sizeof
8092 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008093 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008094 xmlRngVErrMemory(ctxt, "validating\n");
8095 return (-1);
8096 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008097 }
8098 ctxt->elemTab[ctxt->elemNr++] = exec;
8099 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008100 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008101}
8102
8103/**
8104 * xmlRelaxNGElemPop:
8105 * @ctxt: the validation context
8106 *
8107 * Pop the regexp of the current node content model from the stack
8108 *
8109 * Returns the exec or NULL if empty
8110 */
8111static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008112xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8113{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008114 xmlRegExecCtxtPtr ret;
8115
Daniel Veillard4c004142003-10-07 11:33:24 +00008116 if (ctxt->elemNr <= 0)
8117 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008118 ctxt->elemNr--;
8119 ret = ctxt->elemTab[ctxt->elemNr];
8120 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008121 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008122 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8123 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008124 ctxt->elem = NULL;
8125 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008126}
8127
8128/**
8129 * xmlRelaxNGValidateProgressiveCallback:
8130 * @exec: the regular expression instance
8131 * @token: the token which matched
8132 * @transdata: callback data, the define for the subelement if available
8133 @ @inputdata: callback data, the Relax NG validation context
8134 *
8135 * Handle the callback and if needed validate the element children.
8136 * some of the in/out informations are passed via the context in @inputdata.
8137 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008138static void
8139xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8140 ATTRIBUTE_UNUSED,
8141 const xmlChar * token,
8142 void *transdata, void *inputdata)
8143{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008144 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8145 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008146 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008147 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008148 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008149
8150#ifdef DEBUG_PROGRESSIVE
8151 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008152 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008153#endif
8154 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008155 fprintf(stderr, "callback on %s missing context\n", token);
8156 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008157 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008158 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008159 ctxt->pstate = 1;
8160 if (define == NULL) {
8161 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008162 return;
8163 fprintf(stderr, "callback on %s missing define\n", token);
8164 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8165 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8166 ctxt->pstate = -1;
8167 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008168 }
8169 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008170 fprintf(stderr, "callback on %s missing info\n", token);
8171 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8172 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8173 ctxt->pstate = -1;
8174 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008175 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008176 fprintf(stderr, "callback on %s define is not element\n", token);
8177 if (ctxt->errNo == XML_RELAXNG_OK)
8178 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8179 ctxt->pstate = -1;
8180 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008181 }
8182 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008183 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8184 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8185 xmlRelaxNGDumpValidError(ctxt);
8186 ctxt->pstate = -1;
8187 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008188 }
8189 if (define->contModel == NULL) {
8190 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008191 * this node cannot be validated in a streamable fashion
8192 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008193#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008194 xmlGenericError(xmlGenericErrorContext,
8195 "Element '%s' validation is not streamable\n",
8196 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008197#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008198 ctxt->pstate = 0;
8199 ctxt->pdef = define;
8200 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008201 }
8202 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008203 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008204 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008205 ctxt->pstate = -1;
8206 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008207 }
8208 xmlRelaxNGElemPush(ctxt, exec);
8209
8210 /*
8211 * Validate the attributes part of the content.
8212 */
8213 state = xmlRelaxNGNewValidState(ctxt, node);
8214 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008215 ctxt->pstate = -1;
8216 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008217 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008218 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008219 ctxt->state = state;
8220 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008221 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8222 if (ret != 0) {
8223 ctxt->pstate = -1;
8224 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8225 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008226 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008227 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008228 ctxt->state->seq = NULL;
8229 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8230 if (ret != 0) {
8231 ctxt->pstate = -1;
8232 }
8233 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008234 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008235 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008236
8237 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008238
Daniel Veillard4c004142003-10-07 11:33:24 +00008239 for (i = 0; i < ctxt->states->nbState; i++) {
8240 state = ctxt->states->tabState[i];
8241 ctxt->state = state;
8242 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008243
Daniel Veillard4c004142003-10-07 11:33:24 +00008244 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8245 tmp = 0;
8246 break;
8247 }
8248 }
8249 if (tmp != 0) {
8250 /*
8251 * validation error, log the message for the "best" one
8252 */
8253 ctxt->flags |= FLAGS_IGNORABLE;
8254 xmlRelaxNGLogBestError(ctxt);
8255 }
8256 for (i = 0; i < ctxt->states->nbState; i++) {
8257 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8258 }
8259 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8260 ctxt->states = NULL;
8261 if ((ret == 0) && (tmp == -1))
8262 ctxt->pstate = -1;
8263 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008264 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008265 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008266 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8267 xmlRelaxNGDumpValidError(ctxt);
8268 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008269 }
8270 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008271}
8272
8273/**
8274 * xmlRelaxNGValidatePushElement:
8275 * @ctxt: the validation context
8276 * @doc: a document instance
8277 * @elem: an element instance
8278 *
8279 * Push a new element start on the RelaxNG validation stack.
8280 *
8281 * returns 1 if no validation problem was found or 0 if validating the
8282 * element requires a full node, and -1 in case of error.
8283 */
8284int
Daniel Veillard33300b42003-04-17 09:09:19 +00008285xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8286 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008287 xmlNodePtr elem)
8288{
8289 int ret = 1;
8290
8291 if ((ctxt == NULL) || (elem == NULL))
8292 return (-1);
8293
8294#ifdef DEBUG_PROGRESSIVE
8295 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8296#endif
8297 if (ctxt->elem == 0) {
8298 xmlRelaxNGPtr schema;
8299 xmlRelaxNGGrammarPtr grammar;
8300 xmlRegExecCtxtPtr exec;
8301 xmlRelaxNGDefinePtr define;
8302
8303 schema = ctxt->schema;
8304 if (schema == NULL) {
8305 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8306 return (-1);
8307 }
8308 grammar = schema->topgrammar;
8309 if ((grammar == NULL) || (grammar->start == NULL)) {
8310 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8311 return (-1);
8312 }
8313 define = grammar->start;
8314 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008315 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008316 return (0);
8317 }
8318 exec = xmlRegNewExecCtxt(define->contModel,
8319 xmlRelaxNGValidateProgressiveCallback,
8320 ctxt);
8321 if (exec == NULL) {
8322 return (-1);
8323 }
8324 xmlRelaxNGElemPush(ctxt, exec);
8325 }
8326 ctxt->pnode = elem;
8327 ctxt->pstate = 0;
8328 if (elem->ns != NULL) {
8329 ret =
8330 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8331 ctxt);
8332 } else {
8333 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8334 }
8335 if (ret < 0) {
8336 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8337 } else {
8338 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008339 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008340 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008341 ret = -1;
8342 else
8343 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008344 }
8345#ifdef DEBUG_PROGRESSIVE
8346 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008347 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8348 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008349#endif
8350 return (ret);
8351}
8352
8353/**
8354 * xmlRelaxNGValidatePushCData:
8355 * @ctxt: the RelaxNG validation context
8356 * @data: some character data read
Michael Woodfb27e2c2012-09-28 08:59:33 +02008357 * @len: the length of the data
Daniel Veillardf4e55762003-04-15 23:32:22 +00008358 *
8359 * check the CData parsed for validation in the current stack
8360 *
8361 * returns 1 if no validation problem was found or -1 otherwise
8362 */
8363int
8364xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008365 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008366{
8367 int ret = 1;
8368
8369 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8370 return (-1);
8371
8372#ifdef DEBUG_PROGRESSIVE
8373 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8374#endif
8375
8376 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008377 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008378 break;
8379 data++;
8380 }
8381 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008382 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008383
8384 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8385 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008386 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008387#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008388 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008389#endif
8390
Daniel Veillard4c004142003-10-07 11:33:24 +00008391 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008392 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008393 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008394}
8395
8396/**
8397 * xmlRelaxNGValidatePopElement:
8398 * @ctxt: the RelaxNG validation context
8399 * @doc: a document instance
8400 * @elem: an element instance
8401 *
8402 * Pop the element end from the RelaxNG validation stack.
8403 *
8404 * returns 1 if no validation problem was found or 0 otherwise
8405 */
8406int
8407xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8408 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008409 xmlNodePtr elem)
8410{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008411 int ret;
8412 xmlRegExecCtxtPtr exec;
8413
Daniel Veillard4c004142003-10-07 11:33:24 +00008414 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8415 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008416#ifdef DEBUG_PROGRESSIVE
8417 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8418#endif
8419 /*
8420 * verify that we reached a terminal state of the content model.
8421 */
8422 exec = xmlRelaxNGElemPop(ctxt);
8423 ret = xmlRegExecPushString(exec, NULL, NULL);
8424 if (ret == 0) {
8425 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008426 * TODO: get some of the names needed to exit the current state of exec
8427 */
8428 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8429 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008430 } else if (ret < 0) {
8431 ret = -1;
8432 } else {
8433 ret = 1;
8434 }
8435 xmlRegFreeExecCtxt(exec);
8436#ifdef DEBUG_PROGRESSIVE
8437 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008438 xmlGenericError(xmlGenericErrorContext, "PopElem %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
8444/**
8445 * xmlRelaxNGValidateFullElement:
8446 * @ctxt: the validation context
8447 * @doc: a document instance
8448 * @elem: an element instance
8449 *
8450 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8451 * 0 and the content of the node has been expanded.
8452 *
8453 * returns 1 if no validation problem was found or -1 in case of error.
8454 */
8455int
Daniel Veillard33300b42003-04-17 09:09:19 +00008456xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8457 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008458 xmlNodePtr elem)
8459{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008460 int ret;
8461 xmlRelaxNGValidStatePtr state;
8462
Daniel Veillard4c004142003-10-07 11:33:24 +00008463 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8464 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008465#ifdef DEBUG_PROGRESSIVE
8466 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8467#endif
8468 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8469 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008470 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008471 }
8472 state->seq = elem;
8473 ctxt->state = state;
8474 ctxt->errNo = XML_RELAXNG_OK;
8475 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008476 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8477 ret = -1;
8478 else
8479 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008480 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008481 ctxt->state = NULL;
8482#ifdef DEBUG_PROGRESSIVE
8483 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008484 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8485 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008486#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008487 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008488}
8489
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008490/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008491 * *
8492 * Generic interpreted validation implementation *
8493 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008494 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008495static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8496 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008497
8498/**
8499 * xmlRelaxNGSkipIgnored:
8500 * @ctxt: a schema validation context
8501 * @node: the top node.
8502 *
8503 * Skip ignorable nodes in that context
8504 *
8505 * Returns the new sibling or NULL in case of error.
8506 */
8507static xmlNodePtr
8508xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008509 xmlNodePtr node)
8510{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008511 /*
8512 * TODO complete and handle entities
8513 */
8514 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008515 ((node->type == XML_COMMENT_NODE) ||
8516 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008517 (node->type == XML_XINCLUDE_START) ||
8518 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008519 (((node->type == XML_TEXT_NODE) ||
8520 (node->type == XML_CDATA_SECTION_NODE)) &&
8521 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8522 (IS_BLANK_NODE(node)))))) {
8523 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008524 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008525 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008526}
8527
8528/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008529 * xmlRelaxNGNormalize:
8530 * @ctxt: a schema validation context
8531 * @str: the string to normalize
8532 *
8533 * Implements the normalizeWhiteSpace( s ) function from
8534 * section 6.2.9 of the spec
8535 *
8536 * Returns the new string or NULL in case of error.
8537 */
8538static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008539xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8540{
Daniel Veillardedc91922003-01-26 00:52:04 +00008541 xmlChar *ret, *p;
8542 const xmlChar *tmp;
8543 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008544
Daniel Veillardedc91922003-01-26 00:52:04 +00008545 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008546 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008547 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008548 while (*tmp != 0)
8549 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008550 len = tmp - str;
8551
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008552 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008553 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008554 xmlRngVErrMemory(ctxt, "validating\n");
8555 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008556 }
8557 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008558 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008559 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008560 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008561 if (IS_BLANK_CH(*str)) {
8562 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008563 str++;
8564 if (*str == 0)
8565 break;
8566 *p++ = ' ';
8567 } else
8568 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008569 }
8570 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008571 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008572}
8573
8574/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008575 * xmlRelaxNGValidateDatatype:
8576 * @ctxt: a Relax-NG validation context
8577 * @value: the string value
8578 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008579 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008580 *
8581 * Validate the given value against the dataype
8582 *
8583 * Returns 0 if the validation succeeded or an error code.
8584 */
8585static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008586xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8587 const xmlChar * value,
8588 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8589{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008590 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008591 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008592 void *result = NULL;
8593 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008594
8595 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008596 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008597 }
8598 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008599 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008600 if ((define->attrs != NULL) &&
8601 (define->attrs->type == XML_RELAXNG_PARAM)) {
8602 ret =
8603 lib->check(lib->data, define->name, value, &result, node);
8604 } else {
8605 ret = lib->check(lib->data, define->name, value, NULL, node);
8606 }
8607 } else
8608 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008609 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008610 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8611 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8612 lib->freef(lib->data, result);
8613 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008614 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008615 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008616 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008617 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008618 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008619 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8620 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008621 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008622 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008623 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008624 if (lib->facet != NULL) {
8625 tmp = lib->facet(lib->data, define->name, cur->name,
8626 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008627 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008628 ret = -1;
8629 }
8630 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008631 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008632 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008633 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008634
Daniel Veillard4c004142003-10-07 11:33:24 +00008635 oldvalue = ctxt->state->value;
8636 oldendvalue = ctxt->state->endvalue;
8637 ctxt->state->value = (xmlChar *) value;
8638 ctxt->state->endvalue = NULL;
8639 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8640 ctxt->state->value = (xmlChar *) oldvalue;
8641 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008642 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008643 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008644 lib->freef(lib->data, result);
8645 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008646}
8647
8648/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008649 * xmlRelaxNGNextValue:
8650 * @ctxt: a Relax-NG validation context
8651 *
8652 * Skip to the next value when validating within a list
8653 *
8654 * Returns 0 if the operation succeeded or an error code.
8655 */
8656static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008657xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8658{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008659 xmlChar *cur;
8660
8661 cur = ctxt->state->value;
8662 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008663 ctxt->state->value = NULL;
8664 ctxt->state->endvalue = NULL;
8665 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008666 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008667 while (*cur != 0)
8668 cur++;
8669 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8670 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008671 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008672 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008673 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008674 ctxt->state->value = cur;
8675 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008676}
8677
8678/**
8679 * xmlRelaxNGValidateValueList:
8680 * @ctxt: a Relax-NG validation context
8681 * @defines: the list of definitions to verify
8682 *
8683 * Validate the given set of definitions for the current value
8684 *
8685 * Returns 0 if the validation succeeded or an error code.
8686 */
8687static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008688xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8689 xmlRelaxNGDefinePtr defines)
8690{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008691 int ret = 0;
8692
8693 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008694 ret = xmlRelaxNGValidateValue(ctxt, defines);
8695 if (ret != 0)
8696 break;
8697 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008698 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008699 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008700}
8701
8702/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008703 * xmlRelaxNGValidateValue:
8704 * @ctxt: a Relax-NG validation context
8705 * @define: the definition to verify
8706 *
8707 * Validate the given definition for the current value
8708 *
8709 * Returns 0 if the validation succeeded or an error code.
8710 */
8711static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008712xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8713 xmlRelaxNGDefinePtr define)
8714{
Daniel Veillardedc91922003-01-26 00:52:04 +00008715 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008716 xmlChar *value;
8717
8718 value = ctxt->state->value;
8719 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008720 case XML_RELAXNG_EMPTY:{
8721 if ((value != NULL) && (value[0] != 0)) {
8722 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008723
William M. Brack76e95df2003-10-18 16:20:14 +00008724 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008725 idx++;
8726 if (value[idx] != 0)
8727 ret = -1;
8728 }
8729 break;
8730 }
8731 case XML_RELAXNG_TEXT:
8732 break;
8733 case XML_RELAXNG_VALUE:{
8734 if (!xmlStrEqual(value, define->value)) {
8735 if (define->name != NULL) {
8736 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008737
Daniel Veillard4c004142003-10-07 11:33:24 +00008738 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8739 if ((lib != NULL) && (lib->comp != NULL)) {
8740 ret = lib->comp(lib->data, define->name,
8741 define->value, define->node,
8742 (void *) define->attrs,
8743 value, ctxt->state->node);
8744 } else
8745 ret = -1;
8746 if (ret < 0) {
8747 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8748 define->name);
8749 return (-1);
8750 } else if (ret == 1) {
8751 ret = 0;
8752 } else {
8753 ret = -1;
8754 }
8755 } else {
8756 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008757
Daniel Veillard4c004142003-10-07 11:33:24 +00008758 /*
8759 * TODO: trivial optimizations are possible by
8760 * computing at compile-time
8761 */
8762 nval = xmlRelaxNGNormalize(ctxt, define->value);
8763 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008764
Daniel Veillard4c004142003-10-07 11:33:24 +00008765 if ((nval == NULL) || (nvalue == NULL) ||
8766 (!xmlStrEqual(nval, nvalue)))
8767 ret = -1;
8768 if (nval != NULL)
8769 xmlFree(nval);
8770 if (nvalue != NULL)
8771 xmlFree(nvalue);
8772 }
8773 }
8774 if (ret == 0)
8775 xmlRelaxNGNextValue(ctxt);
8776 break;
8777 }
8778 case XML_RELAXNG_DATATYPE:{
8779 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8780 ctxt->state->seq);
8781 if (ret == 0)
8782 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008783
Daniel Veillard4c004142003-10-07 11:33:24 +00008784 break;
8785 }
8786 case XML_RELAXNG_CHOICE:{
8787 xmlRelaxNGDefinePtr list = define->content;
8788 xmlChar *oldvalue;
8789
8790 oldflags = ctxt->flags;
8791 ctxt->flags |= FLAGS_IGNORABLE;
8792
8793 oldvalue = ctxt->state->value;
8794 while (list != NULL) {
8795 ret = xmlRelaxNGValidateValue(ctxt, list);
8796 if (ret == 0) {
8797 break;
8798 }
8799 ctxt->state->value = oldvalue;
8800 list = list->next;
8801 }
8802 ctxt->flags = oldflags;
8803 if (ret != 0) {
8804 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8805 xmlRelaxNGDumpValidError(ctxt);
8806 } else {
8807 if (ctxt->errNr > 0)
8808 xmlRelaxNGPopErrors(ctxt, 0);
8809 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008810 break;
8811 }
8812 case XML_RELAXNG_LIST:{
8813 xmlRelaxNGDefinePtr list = define->content;
8814 xmlChar *oldvalue, *oldend, *val, *cur;
8815
Daniel Veillard416589a2003-02-17 17:25:42 +00008816#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008817 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008818#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008819
Daniel Veillard4c004142003-10-07 11:33:24 +00008820 oldvalue = ctxt->state->value;
8821 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008822
Daniel Veillard4c004142003-10-07 11:33:24 +00008823 val = xmlStrdup(oldvalue);
8824 if (val == NULL) {
8825 val = xmlStrdup(BAD_CAST "");
8826 }
8827 if (val == NULL) {
8828 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8829 return (-1);
8830 }
8831 cur = val;
8832 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008833 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008834 *cur = 0;
8835 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008836#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008837 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008838#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008839 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008840 *cur++ = 0;
8841 } else
8842 cur++;
8843 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008844#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008845 xmlGenericError(xmlGenericErrorContext,
8846 "list value: '%s' found %d items\n",
8847 oldvalue, nb_values);
8848 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008849#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008850 ctxt->state->endvalue = cur;
8851 cur = val;
8852 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8853 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008854
Daniel Veillard4c004142003-10-07 11:33:24 +00008855 ctxt->state->value = cur;
8856
8857 while (list != NULL) {
8858 if (ctxt->state->value == ctxt->state->endvalue)
8859 ctxt->state->value = NULL;
8860 ret = xmlRelaxNGValidateValue(ctxt, list);
8861 if (ret != 0) {
8862#ifdef DEBUG_LIST
8863 xmlGenericError(xmlGenericErrorContext,
8864 "Failed to validate value: '%s' with %d rule\n",
8865 ctxt->state->value, nb_values);
8866#endif
8867 break;
8868 }
8869#ifdef DEBUG_LIST
8870 nb_values++;
8871#endif
8872 list = list->next;
8873 }
8874
8875 if ((ret == 0) && (ctxt->state->value != NULL) &&
8876 (ctxt->state->value != ctxt->state->endvalue)) {
8877 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8878 ctxt->state->value);
8879 ret = -1;
8880 }
8881 xmlFree(val);
8882 ctxt->state->value = oldvalue;
8883 ctxt->state->endvalue = oldend;
8884 break;
8885 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008886 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008887 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8888 if (ret != 0) {
8889 break;
8890 }
8891 /* no break on purpose */
8892 case XML_RELAXNG_ZEROORMORE:{
8893 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008894
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008895 if ((ctxt->state->value == NULL) ||
8896 (*ctxt->state->value == 0)) {
8897 ret = 0;
8898 break;
8899 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008900 oldflags = ctxt->flags;
8901 ctxt->flags |= FLAGS_IGNORABLE;
8902 cur = ctxt->state->value;
8903 temp = NULL;
8904 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8905 (temp != cur)) {
8906 temp = cur;
8907 ret =
8908 xmlRelaxNGValidateValueList(ctxt, define->content);
8909 if (ret != 0) {
8910 ctxt->state->value = temp;
8911 ret = 0;
8912 break;
8913 }
8914 cur = ctxt->state->value;
8915 }
8916 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008917 if (ctxt->errNr > 0)
8918 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008919 break;
8920 }
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008921 case XML_RELAXNG_OPTIONAL:{
8922 xmlChar *temp;
8923
8924 if ((ctxt->state->value == NULL) ||
8925 (*ctxt->state->value == 0)) {
8926 ret = 0;
8927 break;
8928 }
8929 oldflags = ctxt->flags;
8930 ctxt->flags |= FLAGS_IGNORABLE;
8931 temp = ctxt->state->value;
8932 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8933 ctxt->flags = oldflags;
8934 if (ret != 0) {
8935 ctxt->state->value = temp;
8936 if (ctxt->errNr > 0)
8937 xmlRelaxNGPopErrors(ctxt, 0);
8938 ret = 0;
8939 break;
8940 }
8941 if (ctxt->errNr > 0)
8942 xmlRelaxNGPopErrors(ctxt, 0);
8943 break;
8944 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008945 case XML_RELAXNG_EXCEPT:{
8946 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008947
Daniel Veillard4c004142003-10-07 11:33:24 +00008948 list = define->content;
8949 while (list != NULL) {
8950 ret = xmlRelaxNGValidateValue(ctxt, list);
8951 if (ret == 0) {
8952 ret = -1;
8953 break;
8954 } else
8955 ret = 0;
8956 list = list->next;
8957 }
8958 break;
8959 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008960 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008961 case XML_RELAXNG_GROUP:{
8962 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008963
Daniel Veillard4c004142003-10-07 11:33:24 +00008964 list = define->content;
8965 while (list != NULL) {
8966 ret = xmlRelaxNGValidateValue(ctxt, list);
8967 if (ret != 0) {
8968 ret = -1;
8969 break;
8970 } else
8971 ret = 0;
8972 list = list->next;
8973 }
8974 break;
8975 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008976 case XML_RELAXNG_REF:
8977 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008978 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008979 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8980 ret = -1;
8981 } else {
8982 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8983 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008984 break;
8985 default:
8986 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008987 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008988 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008989}
8990
8991/**
8992 * xmlRelaxNGValidateValueContent:
8993 * @ctxt: a Relax-NG validation context
8994 * @defines: the list of definitions to verify
8995 *
8996 * Validate the given definitions for the current value
8997 *
8998 * Returns 0 if the validation succeeded or an error code.
8999 */
9000static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009001xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
9002 xmlRelaxNGDefinePtr defines)
9003{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009004 int ret = 0;
9005
9006 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009007 ret = xmlRelaxNGValidateValue(ctxt, defines);
9008 if (ret != 0)
9009 break;
9010 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009011 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009012 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009013}
9014
9015/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009016 * xmlRelaxNGAttributeMatch:
9017 * @ctxt: a Relax-NG validation context
9018 * @define: the definition to check
9019 * @prop: the attribute
9020 *
9021 * Check if the attribute matches the definition nameClass
9022 *
9023 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
9024 */
9025static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009026xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
9027 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
9028{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009029 int ret;
9030
9031 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009032 if (!xmlStrEqual(define->name, prop->name))
9033 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009034 }
9035 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009036 if (define->ns[0] == 0) {
9037 if (prop->ns != NULL)
9038 return (0);
9039 } else {
9040 if ((prop->ns == NULL) ||
9041 (!xmlStrEqual(define->ns, prop->ns->href)))
9042 return (0);
9043 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009044 }
9045 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009046 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009047 define = define->nameClass;
9048 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009049 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009050
Daniel Veillard4c004142003-10-07 11:33:24 +00009051 list = define->content;
9052 while (list != NULL) {
9053 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9054 if (ret == 1)
9055 return (0);
9056 if (ret < 0)
9057 return (ret);
9058 list = list->next;
9059 }
Shaun McCance6473a412013-10-23 14:51:33 -04009060 } else if (define->type == XML_RELAXNG_CHOICE) {
9061 xmlRelaxNGDefinePtr list;
9062
9063 list = define->nameClass;
9064 while (list != NULL) {
9065 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9066 if (ret == 1)
9067 return (1);
9068 if (ret < 0)
9069 return (ret);
9070 list = list->next;
9071 }
9072 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009073 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009074 TODO}
9075 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009076}
9077
9078/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009079 * xmlRelaxNGValidateAttribute:
9080 * @ctxt: a Relax-NG validation context
9081 * @define: the definition to verify
9082 *
9083 * Validate the given attribute definition for that node
9084 *
9085 * Returns 0 if the validation succeeded or an error code.
9086 */
9087static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009088xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9089 xmlRelaxNGDefinePtr define)
9090{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009091 int ret = 0, i;
9092 xmlChar *value, *oldvalue;
9093 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009094 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009095
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009096 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009097 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009098 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009099 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9100 tmp = ctxt->state->attrs[i];
9101 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9102 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9103 (tmp->ns == NULL)) ||
9104 ((tmp->ns != NULL) &&
9105 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9106 prop = tmp;
9107 break;
9108 }
9109 }
9110 }
9111 if (prop != NULL) {
9112 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9113 oldvalue = ctxt->state->value;
9114 oldseq = ctxt->state->seq;
9115 ctxt->state->seq = (xmlNodePtr) prop;
9116 ctxt->state->value = value;
9117 ctxt->state->endvalue = NULL;
9118 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9119 if (ctxt->state->value != NULL)
9120 value = ctxt->state->value;
9121 if (value != NULL)
9122 xmlFree(value);
9123 ctxt->state->value = oldvalue;
9124 ctxt->state->seq = oldseq;
9125 if (ret == 0) {
9126 /*
9127 * flag the attribute as processed
9128 */
9129 ctxt->state->attrs[i] = NULL;
9130 ctxt->state->nbAttrLeft--;
9131 }
9132 } else {
9133 ret = -1;
9134 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009135#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009136 xmlGenericError(xmlGenericErrorContext,
9137 "xmlRelaxNGValidateAttribute(%s): %d\n",
9138 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009139#endif
9140 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009141 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9142 tmp = ctxt->state->attrs[i];
9143 if ((tmp != NULL) &&
9144 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9145 prop = tmp;
9146 break;
9147 }
9148 }
9149 if (prop != NULL) {
9150 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9151 oldvalue = ctxt->state->value;
9152 oldseq = ctxt->state->seq;
9153 ctxt->state->seq = (xmlNodePtr) prop;
9154 ctxt->state->value = value;
9155 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9156 if (ctxt->state->value != NULL)
9157 value = ctxt->state->value;
9158 if (value != NULL)
9159 xmlFree(value);
9160 ctxt->state->value = oldvalue;
9161 ctxt->state->seq = oldseq;
9162 if (ret == 0) {
9163 /*
9164 * flag the attribute as processed
9165 */
9166 ctxt->state->attrs[i] = NULL;
9167 ctxt->state->nbAttrLeft--;
9168 }
9169 } else {
9170 ret = -1;
9171 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009172#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009173 if (define->ns != NULL) {
9174 xmlGenericError(xmlGenericErrorContext,
9175 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9176 define->ns, ret);
9177 } else {
9178 xmlGenericError(xmlGenericErrorContext,
9179 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9180 ret);
9181 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009182#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009183 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009184
9185 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009186}
9187
9188/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009189 * xmlRelaxNGValidateAttributeList:
9190 * @ctxt: a Relax-NG validation context
9191 * @define: the list of definition to verify
9192 *
9193 * Validate the given node against the list of attribute definitions
9194 *
9195 * Returns 0 if the validation succeeded or an error code.
9196 */
9197static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009198xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9199 xmlRelaxNGDefinePtr defines)
9200{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009201 int ret = 0, res;
9202 int needmore = 0;
9203 xmlRelaxNGDefinePtr cur;
9204
9205 cur = defines;
9206 while (cur != NULL) {
9207 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009208 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9209 ret = -1;
9210 } else
9211 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009212 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009213 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009214 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009215 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009216 cur = defines;
9217 while (cur != NULL) {
9218 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009219 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9220 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9221 if (res < 0)
9222 ret = -1;
9223 } else {
9224 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9225 return (-1);
9226 }
9227 if (res == -1) /* continues on -2 */
9228 break;
9229 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009230 cur = cur->next;
9231 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009232
9233 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009234}
9235
9236/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009237 * xmlRelaxNGNodeMatchesList:
9238 * @node: the node
9239 * @list: a NULL terminated array of definitions
9240 *
9241 * Check if a node can be matched by one of the definitions
9242 *
9243 * Returns 1 if matches 0 otherwise
9244 */
9245static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009246xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9247{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009248 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009249 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009250
9251 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009252 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009253
9254 cur = list[i++];
9255 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009256 if ((node->type == XML_ELEMENT_NODE) &&
9257 (cur->type == XML_RELAXNG_ELEMENT)) {
9258 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9259 if (tmp == 1)
9260 return (1);
9261 } else if (((node->type == XML_TEXT_NODE) ||
9262 (node->type == XML_CDATA_SECTION_NODE)) &&
9263 (cur->type == XML_RELAXNG_TEXT)) {
9264 return (1);
9265 }
9266 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009267 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009268 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009269}
9270
9271/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009272 * xmlRelaxNGValidateInterleave:
9273 * @ctxt: a Relax-NG validation context
9274 * @define: the definition to verify
9275 *
9276 * Validate an interleave definition for a node.
9277 *
9278 * Returns 0 if the validation succeeded or an error code.
9279 */
9280static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009281xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9282 xmlRelaxNGDefinePtr define)
9283{
William M. Brack779af002003-08-01 15:55:39 +00009284 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009285 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009286 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009287
9288 xmlRelaxNGValidStatePtr oldstate;
9289 xmlRelaxNGPartitionPtr partitions;
9290 xmlRelaxNGInterleaveGroupPtr group = NULL;
9291 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9292 xmlNodePtr *list = NULL, *lasts = NULL;
9293
9294 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009295 partitions = (xmlRelaxNGPartitionPtr) define->data;
9296 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009297 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009298 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9299 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009300 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009301 /*
9302 * Optimizations for MIXED
9303 */
9304 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009305 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009306 ctxt->flags |= FLAGS_MIXED_CONTENT;
9307 if (nbgroups == 2) {
9308 /*
9309 * this is a pure <mixed> case
9310 */
9311 if (ctxt->state != NULL)
9312 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9313 ctxt->state->seq);
9314 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9315 ret = xmlRelaxNGValidateDefinition(ctxt,
9316 partitions->groups[1]->
9317 rule);
9318 else
9319 ret = xmlRelaxNGValidateDefinition(ctxt,
9320 partitions->groups[0]->
9321 rule);
9322 if (ret == 0) {
9323 if (ctxt->state != NULL)
9324 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9325 ctxt->state->
9326 seq);
9327 }
9328 ctxt->flags = oldflags;
9329 return (ret);
9330 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009331 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009332
9333 /*
9334 * Build arrays to store the first and last node of the chain
9335 * pertaining to each group
9336 */
9337 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9338 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009339 xmlRngVErrMemory(ctxt, "validating\n");
9340 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009341 }
9342 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9343 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9344 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009345 xmlRngVErrMemory(ctxt, "validating\n");
9346 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009347 }
9348 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9349
9350 /*
9351 * Walk the sequence of children finding the right group and
9352 * sorting them in sequences.
9353 */
9354 cur = ctxt->state->seq;
9355 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9356 start = cur;
9357 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009358 ctxt->state->seq = cur;
9359 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009360 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009361 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009362
Daniel Veillard4c004142003-10-07 11:33:24 +00009363 if ((cur->type == XML_TEXT_NODE) ||
9364 (cur->type == XML_CDATA_SECTION_NODE)) {
9365 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9366 NULL);
9367 } else if (cur->type == XML_ELEMENT_NODE) {
9368 if (cur->ns != NULL) {
9369 tmp = xmlHashLookup2(partitions->triage, cur->name,
9370 cur->ns->href);
9371 if (tmp == NULL)
9372 tmp = xmlHashLookup2(partitions->triage,
9373 BAD_CAST "#any",
9374 cur->ns->href);
9375 } else
9376 tmp =
9377 xmlHashLookup2(partitions->triage, cur->name,
9378 NULL);
9379 if (tmp == NULL)
9380 tmp =
9381 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9382 NULL);
9383 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009384
Daniel Veillard4c004142003-10-07 11:33:24 +00009385 if (tmp == NULL) {
9386 i = nbgroups;
9387 } else {
9388 i = ((long) tmp) - 1;
9389 if (partitions->flags & IS_NEEDCHECK) {
9390 group = partitions->groups[i];
9391 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9392 i = nbgroups;
9393 }
9394 }
9395 } else {
9396 for (i = 0; i < nbgroups; i++) {
9397 group = partitions->groups[i];
9398 if (group == NULL)
9399 continue;
9400 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9401 break;
9402 }
9403 }
9404 /*
9405 * We break as soon as an element not matched is found
9406 */
9407 if (i >= nbgroups) {
9408 break;
9409 }
9410 if (lasts[i] != NULL) {
9411 lasts[i]->next = cur;
9412 lasts[i] = cur;
9413 } else {
9414 list[i] = cur;
9415 lasts[i] = cur;
9416 }
9417 if (cur->next != NULL)
9418 lastchg = cur->next;
9419 else
9420 lastchg = cur;
9421 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009422 }
9423 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009424 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9425 ret = -1;
9426 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009427 }
9428 lastelem = cur;
9429 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009430 for (i = 0; i < nbgroups; i++) {
9431 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
Gaurav Gupta6d753992014-07-14 16:01:10 +08009432 if (ctxt->state == NULL) {
9433 ret = -1;
9434 break;
9435 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009436 group = partitions->groups[i];
9437 if (lasts[i] != NULL) {
9438 last = lasts[i]->next;
9439 lasts[i]->next = NULL;
9440 }
9441 ctxt->state->seq = list[i];
9442 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9443 if (ret != 0)
9444 break;
9445 if (ctxt->state != NULL) {
9446 cur = ctxt->state->seq;
9447 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9448 xmlRelaxNGFreeValidState(ctxt, oldstate);
9449 oldstate = ctxt->state;
9450 ctxt->state = NULL;
9451 if (cur != NULL) {
9452 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9453 ret = -1;
9454 ctxt->state = oldstate;
9455 goto done;
9456 }
9457 } else if (ctxt->states != NULL) {
9458 int j;
9459 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009460 int best = -1;
9461 int lowattr = -1;
9462
9463 /*
9464 * PBM: what happen if there is attributes checks in the interleaves
9465 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009466
Daniel Veillard4c004142003-10-07 11:33:24 +00009467 for (j = 0; j < ctxt->states->nbState; j++) {
9468 cur = ctxt->states->tabState[j]->seq;
9469 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9470 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009471 if (found == 0) {
9472 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9473 best = j;
9474 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009475 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009476 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9477 /* try to keep the latest one to mach old heuristic */
9478 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9479 best = j;
9480 }
9481 if (lowattr == 0)
9482 break;
9483 } else if (found == 0) {
9484 if (lowattr == -1) {
9485 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9486 best = j;
9487 } else
9488 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9489 /* try to keep the latest one to mach old heuristic */
9490 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9491 best = j;
9492 }
9493 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009494 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009495 /*
9496 * BIG PBM: here we pick only one restarting point :-(
9497 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009498 if (ctxt->states->nbState > 0) {
9499 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009500 if (best != -1) {
9501 oldstate = ctxt->states->tabState[best];
9502 ctxt->states->tabState[best] = NULL;
9503 } else {
9504 oldstate =
9505 ctxt->states->tabState[ctxt->states->nbState - 1];
9506 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009507 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009508 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009509 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009510 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009511 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9512 }
9513 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9514 ctxt->states = NULL;
9515 if (found == 0) {
Daniel Veillard76d36452009-09-07 11:19:33 +02009516 if (cur == NULL) {
Ben Waltona7a6a4b2010-03-15 10:06:36 +01009517 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
9518 (const xmlChar *) "noname");
Daniel Veillard76d36452009-09-07 11:19:33 +02009519 } else {
9520 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9521 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009522 ret = -1;
9523 ctxt->state = oldstate;
9524 goto done;
9525 }
9526 } else {
9527 ret = -1;
9528 break;
9529 }
9530 if (lasts[i] != NULL) {
9531 lasts[i]->next = last;
9532 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009533 }
9534 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009535 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009536 ctxt->state = oldstate;
9537 ctxt->state->seq = lastelem;
9538 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009539 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9540 ret = -1;
9541 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009542 }
9543
Daniel Veillard4c004142003-10-07 11:33:24 +00009544 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009545 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009546 /*
9547 * builds the next links chain from the prev one
9548 */
9549 cur = lastchg;
9550 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009551 if ((cur == start) || (cur->prev == NULL))
9552 break;
9553 cur->prev->next = cur;
9554 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009555 }
9556 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009557 if (ctxt->errNr > errNr)
9558 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009559 }
9560
9561 xmlFree(list);
9562 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009563 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009564}
9565
9566/**
9567 * xmlRelaxNGValidateDefinitionList:
9568 * @ctxt: a Relax-NG validation context
9569 * @define: the list of definition to verify
9570 *
9571 * Validate the given node content against the (list) of definitions
9572 *
9573 * Returns 0 if the validation succeeded or an error code.
9574 */
9575static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009576xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9577 xmlRelaxNGDefinePtr defines)
9578{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009579 int ret = 0, res;
9580
9581
Daniel Veillard952379b2003-03-17 15:37:12 +00009582 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009583 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9584 BAD_CAST "NULL definition list");
9585 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009586 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009587 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009588 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9589 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9590 if (res < 0)
9591 ret = -1;
9592 } else {
9593 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9594 return (-1);
9595 }
9596 if (res == -1) /* continues on -2 */
9597 break;
9598 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009599 }
9600
Daniel Veillard4c004142003-10-07 11:33:24 +00009601 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009602}
9603
9604/**
9605 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009606 * @ctxt: a Relax-NG validation context
9607 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009608 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009609 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009610 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009611 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009612 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009613 */
9614static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009615xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9616 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9617{
Daniel Veillard580ced82003-03-21 21:22:48 +00009618 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009619
Daniel Veillardfd573f12003-03-16 17:52:32 +00009620 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009621 if (!xmlStrEqual(elem->name, define->name)) {
9622 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9623 return (0);
9624 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009625 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009626 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009627 if (elem->ns == NULL) {
9628 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9629 return (0);
9630 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9631 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9632 elem->name, define->ns);
9633 return (0);
9634 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009635 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009636 (define->name == NULL)) {
9637 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9638 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009639 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009640 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9641 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009642 }
9643
9644 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009645 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009646
9647 define = define->nameClass;
9648 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009649 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009650
Daniel Veillard4c004142003-10-07 11:33:24 +00009651 if (ctxt != NULL) {
9652 oldflags = ctxt->flags;
9653 ctxt->flags |= FLAGS_IGNORABLE;
9654 }
9655
9656 list = define->content;
9657 while (list != NULL) {
9658 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9659 if (ret == 1) {
9660 if (ctxt != NULL)
9661 ctxt->flags = oldflags;
9662 return (0);
9663 }
9664 if (ret < 0) {
9665 if (ctxt != NULL)
9666 ctxt->flags = oldflags;
9667 return (ret);
9668 }
9669 list = list->next;
9670 }
9671 ret = 1;
9672 if (ctxt != NULL) {
9673 ctxt->flags = oldflags;
9674 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009675 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009676 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009677
Daniel Veillard4c004142003-10-07 11:33:24 +00009678 if (ctxt != NULL) {
9679 oldflags = ctxt->flags;
9680 ctxt->flags |= FLAGS_IGNORABLE;
9681 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009682
Daniel Veillard4c004142003-10-07 11:33:24 +00009683 list = define->nameClass;
9684 while (list != NULL) {
9685 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9686 if (ret == 1) {
9687 if (ctxt != NULL)
9688 ctxt->flags = oldflags;
9689 return (1);
9690 }
9691 if (ret < 0) {
9692 if (ctxt != NULL)
9693 ctxt->flags = oldflags;
9694 return (ret);
9695 }
9696 list = list->next;
9697 }
9698 if (ctxt != NULL) {
9699 if (ret != 0) {
9700 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9701 xmlRelaxNGDumpValidError(ctxt);
9702 } else {
9703 if (ctxt->errNr > 0)
9704 xmlRelaxNGPopErrors(ctxt, 0);
9705 }
9706 }
9707 ret = 0;
9708 if (ctxt != NULL) {
9709 ctxt->flags = oldflags;
9710 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009711 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009712 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009713 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009714 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009715}
9716
9717/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009718 * xmlRelaxNGBestState:
9719 * @ctxt: a Relax-NG validation context
9720 *
9721 * Find the "best" state in the ctxt->states list of states to report
9722 * errors about. I.e. a state with no element left in the child list
9723 * or the one with the less attributes left.
9724 * This is called only if a falidation error was detected
9725 *
9726 * Returns the index of the "best" state or -1 in case of error
9727 */
9728static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009729xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9730{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009731 xmlRelaxNGValidStatePtr state;
9732 int i, tmp;
9733 int best = -1;
9734 int value = 1000000;
9735
9736 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9737 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009738 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009739
Daniel Veillard4c004142003-10-07 11:33:24 +00009740 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009741 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009742 if (state == NULL)
9743 continue;
9744 if (state->seq != NULL) {
9745 if ((best == -1) || (value > 100000)) {
9746 value = 100000;
9747 best = i;
9748 }
9749 } else {
9750 tmp = state->nbAttrLeft;
9751 if ((best == -1) || (value > tmp)) {
9752 value = tmp;
9753 best = i;
9754 }
9755 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009756 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009757 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009758}
9759
9760/**
9761 * xmlRelaxNGLogBestError:
9762 * @ctxt: a Relax-NG validation context
9763 *
9764 * Find the "best" state in the ctxt->states list of states to report
9765 * errors about and log it.
9766 */
9767static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009768xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9769{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009770 int best;
9771
9772 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9773 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009774 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009775
9776 best = xmlRelaxNGBestState(ctxt);
9777 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009778 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009779
Daniel Veillard4c004142003-10-07 11:33:24 +00009780 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009781 }
9782}
9783
9784/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009785 * xmlRelaxNGValidateElementEnd:
9786 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009787 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009788 *
9789 * Validate the end of the element, implements check that
9790 * there is nothing left not consumed in the element content
9791 * or in the attribute list.
9792 *
9793 * Returns 0 if the validation succeeded or an error code.
9794 */
9795static int
William M. Brack272693c2003-11-14 16:20:34 +00009796xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009797{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009798 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009799 xmlRelaxNGValidStatePtr state;
9800
9801 state = ctxt->state;
9802 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009803 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9804 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009805 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009806 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9807 state->node->name, state->seq->name);
9808 }
9809 return (-1);
9810 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009811 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009812 for (i = 0; i < state->nbAttrs; i++) {
9813 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009814 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009815 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9816 state->attrs[i]->name, state->node->name);
9817 }
9818 return (-1 - i);
9819 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009820 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009821 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009822}
9823
9824/**
9825 * xmlRelaxNGValidateState:
9826 * @ctxt: a Relax-NG validation context
9827 * @define: the definition to verify
9828 *
9829 * Validate the current state against the definition
9830 *
9831 * Returns 0 if the validation succeeded or an error code.
9832 */
9833static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009834xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9835 xmlRelaxNGDefinePtr define)
9836{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009837 xmlNodePtr node;
9838 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009839 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009840
9841 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009842 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9843 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009844 }
9845
9846 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009847 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009848 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009849 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009850 }
9851#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009852 for (i = 0; i < ctxt->depth; i++)
9853 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009854 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009855 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009856 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009857 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009858 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009859 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009860 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009861 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009862#endif
9863 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009864 switch (define->type) {
9865 case XML_RELAXNG_EMPTY:
Philip Withnall57941042014-10-26 18:08:04 +00009866 xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009867 ret = 0;
9868 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009869 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009870 ret = -1;
9871 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009872 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009873 while ((node != NULL) &&
9874 ((node->type == XML_TEXT_NODE) ||
9875 (node->type == XML_COMMENT_NODE) ||
9876 (node->type == XML_PI_NODE) ||
9877 (node->type == XML_CDATA_SECTION_NODE)))
9878 node = node->next;
9879 ctxt->state->seq = node;
9880 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009881 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009882 errNr = ctxt->errNr;
9883 node = xmlRelaxNGSkipIgnored(ctxt, node);
9884 if (node == NULL) {
9885 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9886 ret = -1;
9887 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9888 xmlRelaxNGDumpValidError(ctxt);
9889 break;
9890 }
9891 if (node->type != XML_ELEMENT_NODE) {
9892 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9893 ret = -1;
9894 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9895 xmlRelaxNGDumpValidError(ctxt);
9896 break;
9897 }
9898 /*
9899 * This node was already validated successfully against
9900 * this definition.
9901 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009902 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009903 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9904 if (ctxt->errNr > errNr)
9905 xmlRelaxNGPopErrors(ctxt, errNr);
9906 if (ctxt->errNr != 0) {
9907 while ((ctxt->err != NULL) &&
9908 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9909 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9910 ||
9911 ((ctxt->err->err ==
9912 XML_RELAXNG_ERR_ELEMEXTRANS)
9913 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9914 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9915 || (ctxt->err->err ==
9916 XML_RELAXNG_ERR_NOTELEM)))
9917 xmlRelaxNGValidErrorPop(ctxt);
9918 }
9919 break;
9920 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009921
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009922 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9923 if (ret <= 0) {
9924 ret = -1;
9925 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9926 xmlRelaxNGDumpValidError(ctxt);
9927 break;
9928 }
9929 ret = 0;
9930 if (ctxt->errNr != 0) {
9931 if (ctxt->errNr > errNr)
9932 xmlRelaxNGPopErrors(ctxt, errNr);
9933 while ((ctxt->err != NULL) &&
9934 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9935 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9936 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9937 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9938 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9939 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9940 xmlRelaxNGValidErrorPop(ctxt);
9941 }
9942 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009943
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009944 oldflags = ctxt->flags;
9945 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9946 ctxt->flags -= FLAGS_MIXED_CONTENT;
9947 }
9948 state = xmlRelaxNGNewValidState(ctxt, node);
9949 if (state == NULL) {
9950 ret = -1;
9951 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9952 xmlRelaxNGDumpValidError(ctxt);
9953 break;
9954 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009955
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009956 oldstate = ctxt->state;
9957 ctxt->state = state;
9958 if (define->attrs != NULL) {
9959 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9960 if (tmp != 0) {
9961 ret = -1;
9962 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9963 }
9964 }
9965 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009966 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9967 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9968 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009969
Daniel Veillard4c004142003-10-07 11:33:24 +00009970 nstate = xmlRelaxNGNewValidState(ctxt, node);
9971 ctxt->state = nstate;
9972 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009973
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009974 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9975 define->contModel,
9976 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009977 nseq = ctxt->state->seq;
9978 ctxt->state = tmpstate;
9979 ctxt->states = tmpstates;
9980 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009981
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009982#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009983 xmlGenericError(xmlGenericErrorContext,
9984 "Validating content of '%s' : %d\n",
9985 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009986#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009987 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009988 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009989
9990 if (ctxt->states != NULL) {
9991 tmp = -1;
9992
Daniel Veillardce192eb2003-04-16 15:58:05 +00009993 for (i = 0; i < ctxt->states->nbState; i++) {
9994 state = ctxt->states->tabState[i];
9995 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009996 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009997
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009998 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009999 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010000 break;
10001 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010002 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010003 if (tmp != 0) {
10004 /*
10005 * validation error, log the message for the "best" one
10006 */
10007 ctxt->flags |= FLAGS_IGNORABLE;
10008 xmlRelaxNGLogBestError(ctxt);
10009 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010010 for (i = 0; i < ctxt->states->nbState; i++) {
10011 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010012 ctxt->states->
10013 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010014 }
10015 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10016 ctxt->flags = oldflags;
10017 ctxt->states = NULL;
10018 if ((ret == 0) && (tmp == -1))
10019 ret = -1;
10020 } else {
10021 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +000010022 if (ctxt->state != NULL)
10023 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010024 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010025 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010026 xmlRelaxNGFreeValidState(ctxt, state);
10027 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010028 } else {
10029 if (define->content != NULL) {
10030 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010031 define->
10032 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010033 if (tmp != 0) {
10034 ret = -1;
10035 if (ctxt->state == NULL) {
10036 ctxt->state = oldstate;
10037 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10038 node->name);
10039 ctxt->state = NULL;
10040 } else {
10041 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10042 node->name);
10043 }
10044
10045 }
10046 }
10047 if (ctxt->states != NULL) {
10048 tmp = -1;
10049
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010050 for (i = 0; i < ctxt->states->nbState; i++) {
10051 state = ctxt->states->tabState[i];
10052 ctxt->state = state;
10053
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010054 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010055 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010056 break;
10057 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010058 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010059 if (tmp != 0) {
10060 /*
10061 * validation error, log the message for the "best" one
10062 */
10063 ctxt->flags |= FLAGS_IGNORABLE;
10064 xmlRelaxNGLogBestError(ctxt);
10065 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010066 for (i = 0; i < ctxt->states->nbState; i++) {
10067 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010068 ctxt->states->tabState[i]);
10069 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010070 }
10071 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10072 ctxt->flags = oldflags;
10073 ctxt->states = NULL;
10074 if ((ret == 0) && (tmp == -1))
10075 ret = -1;
10076 } else {
10077 state = ctxt->state;
10078 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010079 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010080 xmlRelaxNGFreeValidState(ctxt, state);
10081 }
10082 }
10083 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +000010084 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010085 }
10086 ctxt->flags = oldflags;
10087 ctxt->state = oldstate;
10088 if (oldstate != NULL)
10089 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10090 if (ret != 0) {
10091 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10092 xmlRelaxNGDumpValidError(ctxt);
10093 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010094#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010095 } else {
10096 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010097#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010098 }
10099 } else {
10100 if (ctxt->errNr > errNr)
10101 xmlRelaxNGPopErrors(ctxt, errNr);
10102 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010103
10104#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010105 xmlGenericError(xmlGenericErrorContext,
10106 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10107 node->name, ret);
10108 if (oldstate == NULL)
10109 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10110 else if (oldstate->seq == NULL)
10111 xmlGenericError(xmlGenericErrorContext, ": done\n");
10112 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10113 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10114 oldstate->seq->name);
10115 else
10116 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10117 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010118#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010119 break;
10120 case XML_RELAXNG_OPTIONAL:{
10121 errNr = ctxt->errNr;
10122 oldflags = ctxt->flags;
10123 ctxt->flags |= FLAGS_IGNORABLE;
10124 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10125 ret =
10126 xmlRelaxNGValidateDefinitionList(ctxt,
10127 define->content);
10128 if (ret != 0) {
10129 if (ctxt->state != NULL)
10130 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10131 ctxt->state = oldstate;
10132 ctxt->flags = oldflags;
10133 ret = 0;
10134 if (ctxt->errNr > errNr)
10135 xmlRelaxNGPopErrors(ctxt, errNr);
10136 break;
10137 }
10138 if (ctxt->states != NULL) {
10139 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10140 } else {
10141 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10142 if (ctxt->states == NULL) {
10143 xmlRelaxNGFreeValidState(ctxt, oldstate);
10144 ctxt->flags = oldflags;
10145 ret = -1;
10146 if (ctxt->errNr > errNr)
10147 xmlRelaxNGPopErrors(ctxt, errNr);
10148 break;
10149 }
10150 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10151 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10152 ctxt->state = NULL;
10153 }
10154 ctxt->flags = oldflags;
10155 ret = 0;
10156 if (ctxt->errNr > errNr)
10157 xmlRelaxNGPopErrors(ctxt, errNr);
10158 break;
10159 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010160 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010161 errNr = ctxt->errNr;
10162 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10163 if (ret != 0) {
10164 break;
10165 }
10166 if (ctxt->errNr > errNr)
10167 xmlRelaxNGPopErrors(ctxt, errNr);
10168 /* no break on purpose */
10169 case XML_RELAXNG_ZEROORMORE:{
10170 int progress;
10171 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10172 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010173
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010174 errNr = ctxt->errNr;
10175 res = xmlRelaxNGNewStates(ctxt, 1);
10176 if (res == NULL) {
10177 ret = -1;
10178 break;
10179 }
10180 /*
10181 * All the input states are also exit states
10182 */
10183 if (ctxt->state != NULL) {
10184 xmlRelaxNGAddStates(ctxt, res,
10185 xmlRelaxNGCopyValidState(ctxt,
10186 ctxt->
10187 state));
10188 } else {
10189 for (j = 0; j < ctxt->states->nbState; j++) {
10190 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010191 xmlRelaxNGCopyValidState(ctxt,
10192 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010193 }
10194 }
10195 oldflags = ctxt->flags;
10196 ctxt->flags |= FLAGS_IGNORABLE;
10197 do {
10198 progress = 0;
10199 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010200
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010201 if (ctxt->states != NULL) {
10202 states = ctxt->states;
10203 for (i = 0; i < states->nbState; i++) {
10204 ctxt->state = states->tabState[i];
10205 ctxt->states = NULL;
10206 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10207 define->
10208 content);
10209 if (ret == 0) {
10210 if (ctxt->state != NULL) {
10211 tmp = xmlRelaxNGAddStates(ctxt, res,
10212 ctxt->state);
10213 ctxt->state = NULL;
10214 if (tmp == 1)
10215 progress = 1;
10216 } else if (ctxt->states != NULL) {
10217 for (j = 0; j < ctxt->states->nbState;
10218 j++) {
10219 tmp =
10220 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010221 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010222 if (tmp == 1)
10223 progress = 1;
10224 }
10225 xmlRelaxNGFreeStates(ctxt,
10226 ctxt->states);
10227 ctxt->states = NULL;
10228 }
10229 } else {
10230 if (ctxt->state != NULL) {
10231 xmlRelaxNGFreeValidState(ctxt,
10232 ctxt->state);
10233 ctxt->state = NULL;
10234 }
10235 }
10236 }
10237 } else {
10238 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10239 define->
10240 content);
10241 if (ret != 0) {
10242 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10243 ctxt->state = NULL;
10244 } else {
10245 base = res->nbState;
10246 if (ctxt->state != NULL) {
10247 tmp = xmlRelaxNGAddStates(ctxt, res,
10248 ctxt->state);
10249 ctxt->state = NULL;
10250 if (tmp == 1)
10251 progress = 1;
10252 } else if (ctxt->states != NULL) {
10253 for (j = 0; j < ctxt->states->nbState; j++) {
10254 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010255 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010256 if (tmp == 1)
10257 progress = 1;
10258 }
10259 if (states == NULL) {
10260 states = ctxt->states;
10261 } else {
10262 xmlRelaxNGFreeStates(ctxt,
10263 ctxt->states);
10264 }
10265 ctxt->states = NULL;
10266 }
10267 }
10268 }
10269 if (progress) {
10270 /*
10271 * Collect all the new nodes added at that step
10272 * and make them the new node set
10273 */
10274 if (res->nbState - base == 1) {
10275 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10276 res->
10277 tabState
10278 [base]);
10279 } else {
10280 if (states == NULL) {
10281 xmlRelaxNGNewStates(ctxt,
10282 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010283 states = ctxt->states;
10284 if (states == NULL) {
10285 progress = 0;
10286 break;
10287 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010288 }
10289 states->nbState = 0;
10290 for (i = base; i < res->nbState; i++)
10291 xmlRelaxNGAddStates(ctxt, states,
10292 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010293 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010294 ctxt->states = states;
10295 }
10296 }
10297 } while (progress == 1);
10298 if (states != NULL) {
10299 xmlRelaxNGFreeStates(ctxt, states);
10300 }
10301 ctxt->states = res;
10302 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010303#if 0
10304 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010305 * errors may have to be propagated back...
10306 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010307 if (ctxt->errNr > errNr)
10308 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010309#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010310 ret = 0;
10311 break;
10312 }
10313 case XML_RELAXNG_CHOICE:{
10314 xmlRelaxNGDefinePtr list = NULL;
10315 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010316
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010317 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010318
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010319 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010320 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10321 (node != NULL)) {
10322 /*
10323 * node == NULL can't be optimized since IS_TRIABLE
10324 * doesn't account for choice which may lead to
10325 * only attributes.
10326 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010327 xmlHashTablePtr triage =
10328 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010329
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010330 /*
10331 * Something we can optimize cleanly there is only one
10332 * possble branch out !
10333 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010334 if ((node->type == XML_TEXT_NODE) ||
10335 (node->type == XML_CDATA_SECTION_NODE)) {
10336 list =
10337 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10338 } else if (node->type == XML_ELEMENT_NODE) {
10339 if (node->ns != NULL) {
10340 list = xmlHashLookup2(triage, node->name,
10341 node->ns->href);
10342 if (list == NULL)
10343 list =
10344 xmlHashLookup2(triage, BAD_CAST "#any",
10345 node->ns->href);
10346 } else
10347 list =
10348 xmlHashLookup2(triage, node->name, NULL);
10349 if (list == NULL)
10350 list =
10351 xmlHashLookup2(triage, BAD_CAST "#any",
10352 NULL);
10353 }
10354 if (list == NULL) {
10355 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010356 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010357 break;
10358 }
10359 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10360 if (ret == 0) {
10361 }
10362 break;
10363 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010364
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010365 list = define->content;
10366 oldflags = ctxt->flags;
10367 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010368
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010369 while (list != NULL) {
10370 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10371 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10372 if (ret == 0) {
10373 if (states == NULL) {
10374 states = xmlRelaxNGNewStates(ctxt, 1);
10375 }
10376 if (ctxt->state != NULL) {
10377 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10378 } else if (ctxt->states != NULL) {
10379 for (i = 0; i < ctxt->states->nbState; i++) {
10380 xmlRelaxNGAddStates(ctxt, states,
10381 ctxt->states->
10382 tabState[i]);
10383 }
10384 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10385 ctxt->states = NULL;
10386 }
10387 } else {
10388 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10389 }
10390 ctxt->state = oldstate;
10391 list = list->next;
10392 }
10393 if (states != NULL) {
10394 xmlRelaxNGFreeValidState(ctxt, oldstate);
10395 ctxt->states = states;
10396 ctxt->state = NULL;
10397 ret = 0;
10398 } else {
10399 ctxt->states = NULL;
10400 }
10401 ctxt->flags = oldflags;
10402 if (ret != 0) {
10403 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10404 xmlRelaxNGDumpValidError(ctxt);
10405 }
10406 } else {
10407 if (ctxt->errNr > errNr)
10408 xmlRelaxNGPopErrors(ctxt, errNr);
10409 }
10410 break;
10411 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010412 case XML_RELAXNG_DEF:
10413 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010414 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10415 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010416 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010417 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10418 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010419 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010420 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10421 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010422 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010423 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010424 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010425 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010426 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010427 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10428 break;
10429 case XML_RELAXNG_DATATYPE:{
10430 xmlNodePtr child;
10431 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010432
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010433 child = node;
10434 while (child != NULL) {
10435 if (child->type == XML_ELEMENT_NODE) {
10436 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10437 node->parent->name);
10438 ret = -1;
10439 break;
10440 } else if ((child->type == XML_TEXT_NODE) ||
10441 (child->type == XML_CDATA_SECTION_NODE)) {
10442 content = xmlStrcat(content, child->content);
10443 }
10444 /* TODO: handle entities ... */
10445 child = child->next;
10446 }
10447 if (ret == -1) {
10448 if (content != NULL)
10449 xmlFree(content);
10450 break;
10451 }
10452 if (content == NULL) {
10453 content = xmlStrdup(BAD_CAST "");
10454 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010455 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010456 ret = -1;
10457 break;
10458 }
10459 }
10460 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10461 ctxt->state->seq);
10462 if (ret == -1) {
10463 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10464 } else if (ret == 0) {
10465 ctxt->state->seq = NULL;
10466 }
10467 if (content != NULL)
10468 xmlFree(content);
10469 break;
10470 }
10471 case XML_RELAXNG_VALUE:{
10472 xmlChar *content = NULL;
10473 xmlChar *oldvalue;
10474 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010475
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010476 child = node;
10477 while (child != NULL) {
10478 if (child->type == XML_ELEMENT_NODE) {
10479 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10480 node->parent->name);
10481 ret = -1;
10482 break;
10483 } else if ((child->type == XML_TEXT_NODE) ||
10484 (child->type == XML_CDATA_SECTION_NODE)) {
10485 content = xmlStrcat(content, child->content);
10486 }
10487 /* TODO: handle entities ... */
10488 child = child->next;
10489 }
10490 if (ret == -1) {
10491 if (content != NULL)
10492 xmlFree(content);
10493 break;
10494 }
10495 if (content == NULL) {
10496 content = xmlStrdup(BAD_CAST "");
10497 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010498 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010499 ret = -1;
10500 break;
10501 }
10502 }
10503 oldvalue = ctxt->state->value;
10504 ctxt->state->value = content;
10505 ret = xmlRelaxNGValidateValue(ctxt, define);
10506 ctxt->state->value = oldvalue;
10507 if (ret == -1) {
10508 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10509 } else if (ret == 0) {
10510 ctxt->state->seq = NULL;
10511 }
10512 if (content != NULL)
10513 xmlFree(content);
10514 break;
10515 }
10516 case XML_RELAXNG_LIST:{
10517 xmlChar *content;
10518 xmlNodePtr child;
10519 xmlChar *oldvalue, *oldendvalue;
10520 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010521
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010522 /*
10523 * Make sure it's only text nodes
10524 */
10525
10526 content = NULL;
10527 child = node;
10528 while (child != NULL) {
10529 if (child->type == XML_ELEMENT_NODE) {
10530 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10531 node->parent->name);
10532 ret = -1;
10533 break;
10534 } else if ((child->type == XML_TEXT_NODE) ||
10535 (child->type == XML_CDATA_SECTION_NODE)) {
10536 content = xmlStrcat(content, child->content);
10537 }
10538 /* TODO: handle entities ... */
10539 child = child->next;
10540 }
10541 if (ret == -1) {
10542 if (content != NULL)
10543 xmlFree(content);
10544 break;
10545 }
10546 if (content == NULL) {
10547 content = xmlStrdup(BAD_CAST "");
10548 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010549 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010550 ret = -1;
10551 break;
10552 }
10553 }
10554 len = xmlStrlen(content);
10555 oldvalue = ctxt->state->value;
10556 oldendvalue = ctxt->state->endvalue;
10557 ctxt->state->value = content;
10558 ctxt->state->endvalue = content + len;
10559 ret = xmlRelaxNGValidateValue(ctxt, define);
10560 ctxt->state->value = oldvalue;
10561 ctxt->state->endvalue = oldendvalue;
10562 if (ret == -1) {
10563 VALID_ERR(XML_RELAXNG_ERR_LIST);
10564 } else if ((ret == 0) && (node != NULL)) {
10565 ctxt->state->seq = node->next;
10566 }
10567 if (content != NULL)
10568 xmlFree(content);
10569 break;
10570 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010571 case XML_RELAXNG_EXCEPT:
10572 case XML_RELAXNG_PARAM:
10573 TODO ret = -1;
10574 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010575 }
10576 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010577#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010578 for (i = 0; i < ctxt->depth; i++)
10579 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010580 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010581 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010582 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010583 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010584 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010585 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010586 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010587 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010588#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010589 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010590}
10591
10592/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010593 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010594 * @ctxt: a Relax-NG validation context
10595 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010596 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010597 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010598 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010599 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010600 */
10601static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010602xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10603 xmlRelaxNGDefinePtr define)
10604{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010605 xmlRelaxNGStatesPtr states, res;
10606 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010607
Daniel Veillardfd573f12003-03-16 17:52:32 +000010608 /*
10609 * We should NOT have both ctxt->state and ctxt->states
10610 */
10611 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010612 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10613 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010614 }
10615
10616 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010617 if (ctxt->states != NULL) {
10618 ctxt->state = ctxt->states->tabState[0];
10619 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10620 ctxt->states = NULL;
10621 }
10622 ret = xmlRelaxNGValidateState(ctxt, define);
10623 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10624 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10625 ctxt->state = NULL;
10626 }
10627 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10628 ctxt->state = ctxt->states->tabState[0];
10629 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10630 ctxt->states = NULL;
10631 }
10632 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010633 }
10634
10635 states = ctxt->states;
10636 ctxt->states = NULL;
10637 res = NULL;
10638 j = 0;
10639 oldflags = ctxt->flags;
10640 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010641 for (i = 0; i < states->nbState; i++) {
10642 ctxt->state = states->tabState[i];
10643 ctxt->states = NULL;
10644 ret = xmlRelaxNGValidateState(ctxt, define);
10645 /*
10646 * We should NOT have both ctxt->state and ctxt->states
10647 */
10648 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10649 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10650 ctxt->state = NULL;
10651 }
10652 if (ret == 0) {
10653 if (ctxt->states == NULL) {
10654 if (res != NULL) {
10655 /* add the state to the container */
10656 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10657 ctxt->state = NULL;
10658 } else {
10659 /* add the state directly in states */
10660 states->tabState[j++] = ctxt->state;
10661 ctxt->state = NULL;
10662 }
10663 } else {
10664 if (res == NULL) {
10665 /* make it the new container and copy other results */
10666 res = ctxt->states;
10667 ctxt->states = NULL;
10668 for (k = 0; k < j; k++)
10669 xmlRelaxNGAddStates(ctxt, res,
10670 states->tabState[k]);
10671 } else {
10672 /* add all the new results to res and reff the container */
10673 for (k = 0; k < ctxt->states->nbState; k++)
10674 xmlRelaxNGAddStates(ctxt, res,
10675 ctxt->states->tabState[k]);
10676 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10677 ctxt->states = NULL;
10678 }
10679 }
10680 } else {
10681 if (ctxt->state != NULL) {
10682 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10683 ctxt->state = NULL;
10684 } else if (ctxt->states != NULL) {
10685 for (k = 0; k < ctxt->states->nbState; k++)
10686 xmlRelaxNGFreeValidState(ctxt,
10687 ctxt->states->tabState[k]);
10688 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10689 ctxt->states = NULL;
10690 }
10691 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010692 }
10693 ctxt->flags = oldflags;
10694 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010695 xmlRelaxNGFreeStates(ctxt, states);
10696 ctxt->states = res;
10697 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010698 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010699 states->nbState = j;
10700 ctxt->states = states;
10701 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010702 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010703 ctxt->state = states->tabState[0];
10704 xmlRelaxNGFreeStates(ctxt, states);
10705 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010706 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010707 ret = -1;
10708 xmlRelaxNGFreeStates(ctxt, states);
10709 if (ctxt->states != NULL) {
10710 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10711 ctxt->states = NULL;
10712 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010713 }
10714 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010715 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10716 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010717 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010718 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010719}
10720
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010721/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010722 * xmlRelaxNGValidateDocument:
10723 * @ctxt: a Relax-NG validation context
10724 * @doc: the document
10725 *
10726 * Validate the given document
10727 *
10728 * Returns 0 if the validation succeeded or an error code.
10729 */
10730static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010731xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10732{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010733 int ret;
10734 xmlRelaxNGPtr schema;
10735 xmlRelaxNGGrammarPtr grammar;
10736 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010737 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010738
10739 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010740 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010741
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010742 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010743 schema = ctxt->schema;
10744 grammar = schema->topgrammar;
10745 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010746 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10747 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010748 }
10749 state = xmlRelaxNGNewValidState(ctxt, NULL);
10750 ctxt->state = state;
10751 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010752 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010753 state = ctxt->state;
10754 node = state->seq;
10755 node = xmlRelaxNGSkipIgnored(ctxt, node);
10756 if (node != NULL) {
10757 if (ret != -1) {
10758 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10759 ret = -1;
10760 }
10761 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010762 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010763 int i;
10764 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010765
Daniel Veillard4c004142003-10-07 11:33:24 +000010766 for (i = 0; i < ctxt->states->nbState; i++) {
10767 state = ctxt->states->tabState[i];
10768 node = state->seq;
10769 node = xmlRelaxNGSkipIgnored(ctxt, node);
10770 if (node == NULL)
10771 tmp = 0;
10772 xmlRelaxNGFreeValidState(ctxt, state);
10773 }
10774 if (tmp == -1) {
10775 if (ret != -1) {
10776 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10777 ret = -1;
10778 }
10779 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010780 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010781 if (ctxt->state != NULL) {
10782 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010783 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010784 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010785 if (ret != 0)
10786 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010787#ifdef DEBUG
10788 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010789 ctxt->error(ctxt->userData,
10790 "%d Extra error messages left on stack !\n",
10791 ctxt->errNr);
10792 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010793 }
10794#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010795#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010796 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010797 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010798
Daniel Veillard4c004142003-10-07 11:33:24 +000010799 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10800 vctxt.valid = 1;
10801 vctxt.error = ctxt->error;
10802 vctxt.warning = ctxt->warning;
10803 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010804
Daniel Veillard4c004142003-10-07 11:33:24 +000010805 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10806 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010807 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010808#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010809 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010810 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010811
Daniel Veillard4c004142003-10-07 11:33:24 +000010812 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010813}
10814
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010815/**
10816 * xmlRelaxNGCleanPSVI:
10817 * @node: an input element or document
10818 *
10819 * Call this routine to speed up XPath computation on static documents.
10820 * This stamps all the element nodes with the document order
10821 * Like for line information, the order is kept in the element->content
10822 * field, the value stored is actually - the node number (starting at -1)
10823 * to be able to differentiate from line numbers.
10824 *
10825 * Returns the number of elements found in the document or -1 in case
10826 * of error.
10827 */
10828static void
10829xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10830 xmlNodePtr cur;
10831
10832 if ((node == NULL) ||
10833 ((node->type != XML_ELEMENT_NODE) &&
10834 (node->type != XML_DOCUMENT_NODE) &&
10835 (node->type != XML_HTML_DOCUMENT_NODE)))
10836 return;
10837 if (node->type == XML_ELEMENT_NODE)
10838 node->psvi = NULL;
10839
10840 cur = node->children;
10841 while (cur != NULL) {
10842 if (cur->type == XML_ELEMENT_NODE) {
10843 cur->psvi = NULL;
10844 if (cur->children != NULL) {
10845 cur = cur->children;
10846 continue;
10847 }
10848 }
10849 if (cur->next != NULL) {
10850 cur = cur->next;
10851 continue;
10852 }
10853 do {
10854 cur = cur->parent;
10855 if (cur == NULL)
10856 break;
10857 if (cur == node) {
10858 cur = NULL;
10859 break;
10860 }
10861 if (cur->next != NULL) {
10862 cur = cur->next;
10863 break;
10864 }
10865 } while (cur != NULL);
10866 }
10867 return;
10868}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010869/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080010870 * *
10871 * Validation interfaces *
10872 * *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010873 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010874
Daniel Veillard6eadf632003-01-23 18:29:16 +000010875/**
10876 * xmlRelaxNGNewValidCtxt:
10877 * @schema: a precompiled XML RelaxNGs
10878 *
10879 * Create an XML RelaxNGs validation context based on the given schema
10880 *
10881 * Returns the validation context or NULL in case of error
10882 */
10883xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010884xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10885{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010886 xmlRelaxNGValidCtxtPtr ret;
10887
10888 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10889 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010890 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010891 return (NULL);
10892 }
10893 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10894 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010895 ret->error = xmlGenericError;
10896 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010897 ret->errNr = 0;
10898 ret->errMax = 0;
10899 ret->err = NULL;
10900 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010901 if (schema != NULL)
10902 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010903 ret->states = NULL;
10904 ret->freeState = NULL;
10905 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010906 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010907 return (ret);
10908}
10909
10910/**
10911 * xmlRelaxNGFreeValidCtxt:
10912 * @ctxt: the schema validation context
10913 *
10914 * Free the resources associated to the schema validation context
10915 */
10916void
Daniel Veillard4c004142003-10-07 11:33:24 +000010917xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10918{
Daniel Veillard798024a2003-03-19 10:36:09 +000010919 int k;
10920
Daniel Veillard6eadf632003-01-23 18:29:16 +000010921 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010922 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010923 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010924 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010925 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010926 for (k = 0; k < ctxt->freeState->nbState; k++) {
10927 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10928 }
10929 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010930 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010931 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010932 for (k = 0; k < ctxt->freeStatesNr; k++) {
10933 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10934 }
10935 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010936 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010937 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010938 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010939 if (ctxt->elemTab != NULL) {
10940 xmlRegExecCtxtPtr exec;
10941
Daniel Veillard4c004142003-10-07 11:33:24 +000010942 exec = xmlRelaxNGElemPop(ctxt);
10943 while (exec != NULL) {
10944 xmlRegFreeExecCtxt(exec);
10945 exec = xmlRelaxNGElemPop(ctxt);
10946 }
10947 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010948 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010949 xmlFree(ctxt);
10950}
10951
10952/**
10953 * xmlRelaxNGSetValidErrors:
10954 * @ctxt: a Relax-NG validation context
10955 * @err: the error function
10956 * @warn: the warning function
10957 * @ctx: the functions context
10958 *
10959 * Set the error and warning callback informations
10960 */
10961void
10962xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010963 xmlRelaxNGValidityErrorFunc err,
10964 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10965{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010966 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010967 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010968 ctxt->error = err;
10969 ctxt->warning = warn;
10970 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010971 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010972}
10973
10974/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010975 * xmlRelaxNGSetValidStructuredErrors:
10976 * @ctxt: a Relax-NG validation context
10977 * @serror: the structured error function
10978 * @ctx: the functions context
10979 *
10980 * Set the structured error callback
10981 */
10982void
10983xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010984 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010985{
10986 if (ctxt == NULL)
10987 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010988 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010989 ctxt->error = NULL;
10990 ctxt->warning = NULL;
10991 ctxt->userData = ctx;
10992}
10993
10994/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010995 * xmlRelaxNGGetValidErrors:
10996 * @ctxt: a Relax-NG validation context
10997 * @err: the error function result
10998 * @warn: the warning function result
10999 * @ctx: the functions context result
11000 *
11001 * Get the error and warning callback informations
11002 *
11003 * Returns -1 in case of error and 0 otherwise
11004 */
11005int
11006xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000011007 xmlRelaxNGValidityErrorFunc * err,
11008 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
11009{
Daniel Veillard409a8142003-07-18 15:16:57 +000011010 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000011011 return (-1);
11012 if (err != NULL)
11013 *err = ctxt->error;
11014 if (warn != NULL)
11015 *warn = ctxt->warning;
11016 if (ctx != NULL)
11017 *ctx = ctxt->userData;
11018 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000011019}
11020
11021/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000011022 * xmlRelaxNGValidateDoc:
11023 * @ctxt: a Relax-NG validation context
11024 * @doc: a parsed document tree
11025 *
11026 * Validate a document tree in memory.
11027 *
11028 * Returns 0 if the document is valid, a positive error code
11029 * number otherwise and -1 in case of internal or API error.
11030 */
11031int
Daniel Veillard4c004142003-10-07 11:33:24 +000011032xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
11033{
Daniel Veillard6eadf632003-01-23 18:29:16 +000011034 int ret;
11035
11036 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000011037 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011038
11039 ctxt->doc = doc;
11040
11041 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000011042 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020011043 * Remove all left PSVI
11044 */
11045 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
11046
11047 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000011048 * TODO: build error codes
11049 */
11050 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000011051 return (1);
11052 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011053}
11054
Daniel Veillard5d4644e2005-04-01 13:11:58 +000011055#define bottom_relaxng
11056#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000011057#endif /* LIBXML_SCHEMAS_ENABLED */