blob: 0122d571d059178429ab9cdce3149e557762473f [file] [log] [blame]
Daniel Veillard6eadf632003-01-23 18:29:16 +00001/*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
Daniel Veillardd41f4f42003-01-29 21:07:52 +00009/**
10 * TODO:
Daniel Veillardf4b4f982003-02-13 11:02:08 +000011 * - add support for DTD compatibility spec
12 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
Daniel Veillardac297932003-04-17 12:55:35 +000013 * - report better mem allocations pbms at runtime and abort immediately.
Daniel Veillardd41f4f42003-01-29 21:07:52 +000014 */
15
Daniel Veillard6eadf632003-01-23 18:29:16 +000016#define IN_LIBXML
17#include "libxml.h"
18
19#ifdef LIBXML_SCHEMAS_ENABLED
20
21#include <string.h>
22#include <stdio.h>
23#include <libxml/xmlmemory.h>
24#include <libxml/parser.h>
25#include <libxml/parserInternals.h>
26#include <libxml/hash.h>
27#include <libxml/uri.h>
28
29#include <libxml/relaxng.h>
30
31#include <libxml/xmlschemastypes.h>
32#include <libxml/xmlautomata.h>
33#include <libxml/xmlregexp.h>
Daniel Veillardc6e997c2003-01-27 12:35:42 +000034#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000035
36/*
37 * The Relax-NG namespace
38 */
39static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
40 "http://relaxng.org/ns/structure/1.0";
41
42#define IS_RELAXNG(node, type) \
43 ((node != NULL) && (node->ns != NULL) && \
44 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
45 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
46
47
Daniel Veillard23a47d62008-06-25 04:11:24 +000048#if 0
Daniel Veillard87254c82006-02-19 15:27:17 +000049#define DEBUG 1
Daniel Veillard4c004142003-10-07 11:33:24 +000050
Daniel Veillard87254c82006-02-19 15:27:17 +000051#define DEBUG_GRAMMAR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000052
Daniel Veillard87254c82006-02-19 15:27:17 +000053#define DEBUG_CONTENT 1
Daniel Veillard4c004142003-10-07 11:33:24 +000054
Daniel Veillard87254c82006-02-19 15:27:17 +000055#define DEBUG_TYPE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000056
Daniel Veillard87254c82006-02-19 15:27:17 +000057#define DEBUG_VALID 1
Daniel Veillard4c004142003-10-07 11:33:24 +000058
Daniel Veillard87254c82006-02-19 15:27:17 +000059#define DEBUG_INTERLEAVE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000060
Daniel Veillard87254c82006-02-19 15:27:17 +000061#define DEBUG_LIST 1
Daniel Veillard4c004142003-10-07 11:33:24 +000062
Daniel Veillard87254c82006-02-19 15:27:17 +000063#define DEBUG_INCLUDE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000064
Daniel Veillard87254c82006-02-19 15:27:17 +000065#define DEBUG_ERROR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000066
Daniel Veillard87254c82006-02-19 15:27:17 +000067#define DEBUG_COMPILE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000068
Daniel Veillard87254c82006-02-19 15:27:17 +000069#define DEBUG_PROGRESSIVE 1
70#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +000071
Daniel Veillard5f1946a2003-03-31 16:38:16 +000072#define MAX_ERROR 5
73
Daniel Veillard6eadf632003-01-23 18:29:16 +000074#define TODO \
75 xmlGenericError(xmlGenericErrorContext, \
76 "Unimplemented block at %s:%d\n", \
77 __FILE__, __LINE__);
78
79typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
80typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
81
82typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
83typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
84
Daniel Veillardd41f4f42003-01-29 21:07:52 +000085typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
86typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
87
Daniel Veillarda9d912d2003-02-01 17:43:10 +000088typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
89typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
90
Daniel Veillard6eadf632003-01-23 18:29:16 +000091typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000092 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
93 XML_RELAXNG_COMBINE_CHOICE, /* choice */
94 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000095} xmlRelaxNGCombine;
96
Daniel Veillard4c5cf702003-02-21 15:40:34 +000097typedef enum {
98 XML_RELAXNG_CONTENT_ERROR = -1,
99 XML_RELAXNG_CONTENT_EMPTY = 0,
100 XML_RELAXNG_CONTENT_SIMPLE,
101 XML_RELAXNG_CONTENT_COMPLEX
102} xmlRelaxNGContentType;
103
Daniel Veillard6eadf632003-01-23 18:29:16 +0000104typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
105typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
106
107struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000108 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
109 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
110 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
111 xmlRelaxNGDefinePtr start; /* <start> content */
112 xmlRelaxNGCombine combine; /* the default combine value */
113 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
114 xmlHashTablePtr defs; /* define* */
115 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000116};
117
118
Daniel Veillard6eadf632003-01-23 18:29:16 +0000119typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000120 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
121 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000122 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000123 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
124 XML_RELAXNG_TEXT, /* textual content */
125 XML_RELAXNG_ELEMENT, /* an element */
126 XML_RELAXNG_DATATYPE, /* extenal data type definition */
127 XML_RELAXNG_PARAM, /* extenal data type parameter */
128 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
129 XML_RELAXNG_LIST, /* a list of patterns */
130 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
131 XML_RELAXNG_DEF, /* a definition */
132 XML_RELAXNG_REF, /* reference to a definition */
133 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
134 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
135 XML_RELAXNG_OPTIONAL, /* optional patterns */
136 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
137 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
138 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
139 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
140 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
141 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000142} xmlRelaxNGType;
143
Daniel Veillard52b48c72003-04-13 19:53:42 +0000144#define IS_NULLABLE (1 << 0)
145#define IS_NOT_NULLABLE (1 << 1)
146#define IS_INDETERMINIST (1 << 2)
147#define IS_MIXED (1 << 3)
148#define IS_TRIABLE (1 << 4)
149#define IS_PROCESSED (1 << 5)
150#define IS_COMPILABLE (1 << 6)
151#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000152
Daniel Veillard6eadf632003-01-23 18:29:16 +0000153struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000154 xmlRelaxNGType type; /* the type of definition */
155 xmlNodePtr node; /* the node in the source */
156 xmlChar *name; /* the element local name if present */
157 xmlChar *ns; /* the namespace local name if present */
158 xmlChar *value; /* value when available */
159 void *data; /* data lib or specific pointer */
160 xmlRelaxNGDefinePtr content; /* the expected content */
161 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
162 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
163 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
164 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
165 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
166 short depth; /* used for the cycle detection */
167 short dflags; /* define related flags */
168 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000169};
170
171/**
172 * _xmlRelaxNG:
173 *
174 * A RelaxNGs definition
175 */
176struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000177 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000178 xmlRelaxNGGrammarPtr topgrammar;
179 xmlDocPtr doc;
180
Daniel Veillard4c004142003-10-07 11:33:24 +0000181 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000182
Daniel Veillard4c004142003-10-07 11:33:24 +0000183 xmlHashTablePtr defs; /* define */
184 xmlHashTablePtr refs; /* references */
185 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
186 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
187 int defNr; /* number of defines used */
188 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000189
Daniel Veillard6eadf632003-01-23 18:29:16 +0000190};
191
Daniel Veillard77648bb2003-02-20 15:03:22 +0000192#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
193#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
194#define XML_RELAXNG_IN_LIST (1 << 2)
195#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
196#define XML_RELAXNG_IN_START (1 << 4)
197#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
198#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
199#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000200#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
201#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000202
203struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000204 void *userData; /* user specific data block */
205 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
206 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000207 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000208 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000209
Daniel Veillard4c004142003-10-07 11:33:24 +0000210 xmlRelaxNGPtr schema; /* The schema in use */
211 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
212 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
213 int flags; /* parser flags */
214 int nbErrors; /* number of errors at parse time */
215 int nbWarnings; /* number of warnings at parse time */
216 const xmlChar *define; /* the current define scope */
217 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000218
Daniel Veillard4c004142003-10-07 11:33:24 +0000219 int nbInterleaves;
220 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000221
Daniel Veillard4c004142003-10-07 11:33:24 +0000222 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
223 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
224 xmlChar *URL;
225 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000226
Daniel Veillard4c004142003-10-07 11:33:24 +0000227 int defNr; /* number of defines used */
228 int defMax; /* number of defines aloocated */
229 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000230
Daniel Veillard4c004142003-10-07 11:33:24 +0000231 const char *buffer;
232 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000233
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000234 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000235 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
236 int docNr; /* Depth of the parsing stack */
237 int docMax; /* Max depth of the parsing stack */
238 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000239
240 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000241 xmlRelaxNGIncludePtr inc; /* Current parsed include */
242 int incNr; /* Depth of the include parsing stack */
243 int incMax; /* Max depth of the parsing stack */
244 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000245
Daniel Veillard4c004142003-10-07 11:33:24 +0000246 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000247
248 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000249 xmlAutomataPtr am; /* the automata */
250 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000251
252 int crng; /* compact syntax and other flags */
Daniel Veillard42595322004-11-08 10:52:06 +0000253 int freedoc; /* need to free the document */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000254};
255
256#define FLAGS_IGNORABLE 1
257#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000258#define FLAGS_MIXED_CONTENT 4
Daniel Veillardb30ca312005-09-04 13:50:03 +0000259#define FLAGS_NOERROR 8
Daniel Veillard6eadf632003-01-23 18:29:16 +0000260
261/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000262 * xmlRelaxNGInterleaveGroup:
263 *
264 * A RelaxNGs partition set associated to lists of definitions
265 */
266typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
267typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
268struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000269 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
270 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
271 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000272};
273
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000274#define IS_DETERMINIST 1
275#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000276
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000277/**
278 * xmlRelaxNGPartitions:
279 *
280 * A RelaxNGs partition associated to an interleave group
281 */
282typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
283typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
284struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000285 int nbgroups; /* number of groups in the partitions */
286 xmlHashTablePtr triage; /* hash table used to direct nodes to the
287 * right group when possible */
288 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000289 xmlRelaxNGInterleaveGroupPtr *groups;
290};
291
292/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000293 * xmlRelaxNGValidState:
294 *
295 * A RelaxNGs validation state
296 */
297#define MAX_ATTR 20
298typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
299typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
300struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000301 xmlNodePtr node; /* the current node */
302 xmlNodePtr seq; /* the sequence of children left to validate */
303 int nbAttrs; /* the number of attributes */
304 int maxAttrs; /* the size of attrs */
305 int nbAttrLeft; /* the number of attributes left to validate */
306 xmlChar *value; /* the value when operating on string */
307 xmlChar *endvalue; /* the end value when operating on string */
308 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000309};
310
311/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000312 * xmlRelaxNGStates:
313 *
314 * A RelaxNGs container for validation state
315 */
316typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
317typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
318struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000319 int nbState; /* the number of states */
320 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000321 xmlRelaxNGValidStatePtr *tabState;
322};
323
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000324#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000325
Daniel Veillardfd573f12003-03-16 17:52:32 +0000326/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000327 * xmlRelaxNGValidError:
328 *
329 * A RelaxNGs validation error
330 */
331typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
332typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
333struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000334 xmlRelaxNGValidErr err; /* the error number */
335 int flags; /* flags */
336 xmlNodePtr node; /* the current node */
337 xmlNodePtr seq; /* the current child */
338 const xmlChar *arg1; /* first arg */
339 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000340};
341
342/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000343 * xmlRelaxNGValidCtxt:
344 *
345 * A RelaxNGs validation context
346 */
347
348struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000349 void *userData; /* user specific data block */
350 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
351 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000352 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000353 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000354
Daniel Veillard4c004142003-10-07 11:33:24 +0000355 xmlRelaxNGPtr schema; /* The schema in use */
356 xmlDocPtr doc; /* the document being validated */
357 int flags; /* validation flags */
358 int depth; /* validation depth */
359 int idref; /* requires idref checking */
360 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000361
362 /*
363 * Errors accumulated in branches may have to be stacked to be
364 * provided back when it's sure they affect validation.
365 */
366 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000367 int errNr; /* Depth of the error stack */
368 int errMax; /* Max depth of the error stack */
369 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000370
Daniel Veillard4c004142003-10-07 11:33:24 +0000371 xmlRelaxNGValidStatePtr state; /* the current validation state */
372 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000373
Daniel Veillard4c004142003-10-07 11:33:24 +0000374 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
375 int freeStatesNr;
376 int freeStatesMax;
377 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000378
379 /*
380 * This is used for "progressive" validation
381 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000382 xmlRegExecCtxtPtr elem; /* the current element regexp */
383 int elemNr; /* the number of element validated */
384 int elemMax; /* the max depth of elements */
385 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
386 int pstate; /* progressive state */
387 xmlNodePtr pnode; /* the current node */
388 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
389 int perr; /* signal error in content model
390 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000391};
392
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000393/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000394 * xmlRelaxNGInclude:
395 *
396 * Structure associated to a RelaxNGs document element
397 */
398struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000399 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
400 xmlChar *href; /* the normalized href value */
401 xmlDocPtr doc; /* the associated XML document */
402 xmlRelaxNGDefinePtr content; /* the definitions */
403 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000404};
405
406/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000407 * xmlRelaxNGDocument:
408 *
409 * Structure associated to a RelaxNGs document element
410 */
411struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000412 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000413 xmlChar *href; /* the normalized href value */
414 xmlDocPtr doc; /* the associated XML document */
415 xmlRelaxNGDefinePtr content; /* the definitions */
416 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillard81c51e12009-08-14 18:52:10 +0200417 int externalRef; /* 1 if an external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000418};
419
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000420
Daniel Veillard6eadf632003-01-23 18:29:16 +0000421/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000422 * *
423 * Some factorized error routines *
424 * *
425 ************************************************************************/
426
427/**
428 * xmlRngPErrMemory:
429 * @ctxt: an Relax-NG parser context
430 * @extra: extra informations
431 *
432 * Handle a redefinition of attribute error
433 */
434static void
435xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
436{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000437 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000438 xmlGenericErrorFunc channel = NULL;
439 void *data = NULL;
440
441 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000442 if (ctxt->serror != NULL)
443 schannel = ctxt->serror;
444 else
445 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000446 data = ctxt->userData;
447 ctxt->nbErrors++;
448 }
449 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000450 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000451 NULL, NULL, XML_FROM_RELAXNGP,
452 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
453 NULL, NULL, 0, 0,
454 "Memory allocation failed : %s\n", extra);
455 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000456 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000457 NULL, NULL, XML_FROM_RELAXNGP,
458 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
459 NULL, NULL, 0, 0, "Memory allocation failed\n");
460}
461
462/**
463 * xmlRngVErrMemory:
464 * @ctxt: a Relax-NG validation context
465 * @extra: extra informations
466 *
467 * Handle a redefinition of attribute error
468 */
469static void
470xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
471{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000472 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000473 xmlGenericErrorFunc channel = NULL;
474 void *data = NULL;
475
476 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000477 if (ctxt->serror != NULL)
478 schannel = ctxt->serror;
479 else
480 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000481 data = ctxt->userData;
482 ctxt->nbErrors++;
483 }
484 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000485 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000486 NULL, NULL, XML_FROM_RELAXNGV,
487 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
488 NULL, NULL, 0, 0,
489 "Memory allocation failed : %s\n", extra);
490 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000491 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000492 NULL, NULL, XML_FROM_RELAXNGV,
493 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
494 NULL, NULL, 0, 0, "Memory allocation failed\n");
495}
496
497/**
498 * xmlRngPErr:
499 * @ctxt: a Relax-NG parser context
500 * @node: the node raising the error
501 * @error: the error code
502 * @msg: message
503 * @str1: extra info
504 * @str2: extra info
505 *
506 * Handle a Relax NG Parsing error
507 */
508static void
509xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
510 const char *msg, const xmlChar * str1, const xmlChar * str2)
511{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000512 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000513 xmlGenericErrorFunc channel = NULL;
514 void *data = NULL;
515
516 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000517 if (ctxt->serror != NULL)
518 schannel = ctxt->serror;
519 else
520 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000521 data = ctxt->userData;
522 ctxt->nbErrors++;
523 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000524 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000525 NULL, node, XML_FROM_RELAXNGP,
526 error, XML_ERR_ERROR, NULL, 0,
527 (const char *) str1, (const char *) str2, NULL, 0, 0,
528 msg, str1, str2);
529}
530
531/**
532 * xmlRngVErr:
533 * @ctxt: a Relax-NG validation context
534 * @node: the node raising the error
535 * @error: the error code
536 * @msg: message
537 * @str1: extra info
538 * @str2: extra info
539 *
540 * Handle a Relax NG Validation error
541 */
542static void
543xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
544 const char *msg, const xmlChar * str1, const xmlChar * str2)
545{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000546 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000547 xmlGenericErrorFunc channel = NULL;
548 void *data = NULL;
549
550 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000551 if (ctxt->serror != NULL)
552 schannel = ctxt->serror;
553 else
554 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000555 data = ctxt->userData;
556 ctxt->nbErrors++;
557 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000558 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000559 NULL, node, XML_FROM_RELAXNGV,
560 error, XML_ERR_ERROR, NULL, 0,
561 (const char *) str1, (const char *) str2, NULL, 0, 0,
562 msg, str1, str2);
563}
564
565/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000566 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000567 * Preliminary type checking interfaces *
568 * *
569 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000570
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000571/**
572 * xmlRelaxNGTypeHave:
573 * @data: data needed for the library
574 * @type: the type name
575 * @value: the value to check
576 *
577 * Function provided by a type library to check if a type is exported
578 *
579 * Returns 1 if yes, 0 if no and -1 in case of error.
580 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000581typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000582
583/**
584 * xmlRelaxNGTypeCheck:
585 * @data: data needed for the library
586 * @type: the type name
587 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000588 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000589 *
590 * Function provided by a type library to check if a value match a type
591 *
592 * Returns 1 if yes, 0 if no and -1 in case of error.
593 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000594typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
595 const xmlChar * value, void **result,
596 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000597
598/**
599 * xmlRelaxNGFacetCheck:
600 * @data: data needed for the library
601 * @type: the type name
602 * @facet: the facet name
603 * @val: the facet value
604 * @strval: the string value
605 * @value: the value to check
606 *
607 * Function provided by a type library to check a value facet
608 *
609 * Returns 1 if yes, 0 if no and -1 in case of error.
610 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000611typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
612 const xmlChar * facet,
613 const xmlChar * val,
614 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000615
616/**
617 * xmlRelaxNGTypeFree:
618 * @data: data needed for the library
619 * @result: the value to free
620 *
621 * Function provided by a type library to free a returned result
622 */
623typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000624
625/**
626 * xmlRelaxNGTypeCompare:
627 * @data: data needed for the library
628 * @type: the type name
629 * @value1: the first value
630 * @value2: the second value
631 *
632 * Function provided by a type library to compare two values accordingly
633 * to a type.
634 *
635 * Returns 1 if yes, 0 if no and -1 in case of error.
636 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000637typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
638 const xmlChar * value1,
639 xmlNodePtr ctxt1,
640 void *comp1,
641 const xmlChar * value2,
642 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000643typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
644typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
645struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000646 const xmlChar *namespace; /* the datatypeLibrary value */
647 void *data; /* data needed for the library */
648 xmlRelaxNGTypeHave have; /* the export function */
649 xmlRelaxNGTypeCheck check; /* the checking function */
650 xmlRelaxNGTypeCompare comp; /* the compare function */
651 xmlRelaxNGFacetCheck facet; /* the facet check function */
652 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000653};
654
655/************************************************************************
656 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000657 * Allocation functions *
658 * *
659 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000660static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
661static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000662static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000663static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000664static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
665 ATTRIBUTE_UNUSED,
666 xmlRelaxNGValidStatePtr state1,
667 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000668static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000669 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000670
671/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000672 * xmlRelaxNGFreeDocument:
673 * @docu: a document structure
674 *
675 * Deallocate a RelaxNG document structure.
676 */
677static void
678xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
679{
680 if (docu == NULL)
681 return;
682
683 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000684 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000685 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000686 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000687 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000688 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000689 xmlFree(docu);
690}
691
692/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000693 * xmlRelaxNGFreeDocumentList:
694 * @docu: a list of document structure
695 *
696 * Deallocate a RelaxNG document structures.
697 */
698static void
699xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
700{
701 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000702
Daniel Veillardc482e262003-02-26 14:48:48 +0000703 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000704 next = docu->next;
705 xmlRelaxNGFreeDocument(docu);
706 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000707 }
708}
709
710/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000711 * xmlRelaxNGFreeInclude:
712 * @incl: a include structure
713 *
714 * Deallocate a RelaxNG include structure.
715 */
716static void
717xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
718{
719 if (incl == NULL)
720 return;
721
722 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000723 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000724 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000725 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000726 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000727 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000728 xmlFree(incl);
729}
730
731/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000732 * xmlRelaxNGFreeIncludeList:
733 * @incl: a include structure list
734 *
735 * Deallocate a RelaxNG include structure.
736 */
737static void
738xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
739{
740 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000741
Daniel Veillardc482e262003-02-26 14:48:48 +0000742 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000743 next = incl->next;
744 xmlRelaxNGFreeInclude(incl);
745 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000746 }
747}
748
749/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000750 * xmlRelaxNGNewRelaxNG:
751 * @ctxt: a Relax-NG validation context (optional)
752 *
753 * Allocate a new RelaxNG structure.
754 *
755 * Returns the newly allocated structure or NULL in case or error
756 */
757static xmlRelaxNGPtr
758xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
759{
760 xmlRelaxNGPtr ret;
761
762 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
763 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000764 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000765 return (NULL);
766 }
767 memset(ret, 0, sizeof(xmlRelaxNG));
768
769 return (ret);
770}
771
772/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000773 * xmlRelaxNGFreeInnerSchema:
774 * @schema: a schema structure
775 *
776 * Deallocate a RelaxNG schema structure.
777 */
778static void
779xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
780{
781 if (schema == NULL)
782 return;
783
784 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000785 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000786 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000787 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000788
Daniel Veillard4c004142003-10-07 11:33:24 +0000789 for (i = 0; i < schema->defNr; i++)
790 xmlRelaxNGFreeDefine(schema->defTab[i]);
791 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000792 }
793
794 xmlFree(schema);
795}
796
797/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000798 * xmlRelaxNGFree:
799 * @schema: a schema structure
800 *
801 * Deallocate a RelaxNG structure.
802 */
803void
804xmlRelaxNGFree(xmlRelaxNGPtr schema)
805{
806 if (schema == NULL)
807 return;
808
Daniel Veillard6eadf632003-01-23 18:29:16 +0000809 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000810 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000811 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000812 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000813 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000814 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000815 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000816 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000817 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000818 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000819
Daniel Veillard4c004142003-10-07 11:33:24 +0000820 for (i = 0; i < schema->defNr; i++)
821 xmlRelaxNGFreeDefine(schema->defTab[i]);
822 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000823 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000824
825 xmlFree(schema);
826}
827
828/**
829 * xmlRelaxNGNewGrammar:
830 * @ctxt: a Relax-NG validation context (optional)
831 *
832 * Allocate a new RelaxNG grammar.
833 *
834 * Returns the newly allocated structure or NULL in case or error
835 */
836static xmlRelaxNGGrammarPtr
837xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
838{
839 xmlRelaxNGGrammarPtr ret;
840
841 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
842 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000843 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000844 return (NULL);
845 }
846 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
847
848 return (ret);
849}
850
851/**
852 * xmlRelaxNGFreeGrammar:
853 * @grammar: a grammar structure
854 *
855 * Deallocate a RelaxNG grammar structure.
856 */
857static void
858xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
859{
860 if (grammar == NULL)
861 return;
862
Daniel Veillardc482e262003-02-26 14:48:48 +0000863 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000864 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000865 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000866 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000867 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000868 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000869 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000870 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000871 }
872 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000873 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000874 }
875
876 xmlFree(grammar);
877}
878
879/**
880 * xmlRelaxNGNewDefine:
881 * @ctxt: a Relax-NG validation context
882 * @node: the node in the input document.
883 *
884 * Allocate a new RelaxNG define.
885 *
886 * Returns the newly allocated structure or NULL in case or error
887 */
888static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000889xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000890{
891 xmlRelaxNGDefinePtr ret;
892
Daniel Veillard419a7682003-02-03 23:22:49 +0000893 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000894 ctxt->defMax = 16;
895 ctxt->defNr = 0;
896 ctxt->defTab = (xmlRelaxNGDefinePtr *)
897 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
898 if (ctxt->defTab == NULL) {
899 xmlRngPErrMemory(ctxt, "allocating define\n");
900 return (NULL);
901 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000902 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000903 xmlRelaxNGDefinePtr *tmp;
904
905 ctxt->defMax *= 2;
906 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
907 ctxt->defMax *
908 sizeof
909 (xmlRelaxNGDefinePtr));
910 if (tmp == NULL) {
911 xmlRngPErrMemory(ctxt, "allocating define\n");
912 return (NULL);
913 }
914 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000915 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000916 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
917 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000918 xmlRngPErrMemory(ctxt, "allocating define\n");
919 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000920 }
921 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000922 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000923 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000924 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000925 return (ret);
926}
927
928/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000929 * xmlRelaxNGFreePartition:
930 * @partitions: a partition set structure
931 *
932 * Deallocate RelaxNG partition set structures.
933 */
934static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000935xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
936{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000937 xmlRelaxNGInterleaveGroupPtr group;
938 int j;
939
940 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000941 if (partitions->groups != NULL) {
942 for (j = 0; j < partitions->nbgroups; j++) {
943 group = partitions->groups[j];
944 if (group != NULL) {
945 if (group->defs != NULL)
946 xmlFree(group->defs);
947 if (group->attrs != NULL)
948 xmlFree(group->attrs);
949 xmlFree(group);
950 }
951 }
952 xmlFree(partitions->groups);
953 }
954 if (partitions->triage != NULL) {
955 xmlHashFree(partitions->triage, NULL);
956 }
957 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000958 }
959}
Daniel Veillard4c004142003-10-07 11:33:24 +0000960
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000961/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000962 * xmlRelaxNGFreeDefine:
963 * @define: a define structure
964 *
965 * Deallocate a RelaxNG define structure.
966 */
967static void
968xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
969{
970 if (define == NULL)
971 return;
972
Daniel Veillard4c004142003-10-07 11:33:24 +0000973 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
974 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000975
Daniel Veillard4c004142003-10-07 11:33:24 +0000976 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
977 if ((lib != NULL) && (lib->freef != NULL))
978 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000979 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000980 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
981 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
982 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
983 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000984 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000985 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000986 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000987 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000988 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000989 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000990 if (define->contModel != NULL)
991 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000992 xmlFree(define);
993}
994
995/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000996 * xmlRelaxNGNewStates:
997 * @ctxt: a Relax-NG validation context
998 * @size: the default size for the container
999 *
1000 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001001 *
1002 * Returns the newly allocated structure or NULL in case or error
1003 */
1004static xmlRelaxNGStatesPtr
1005xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1006{
1007 xmlRelaxNGStatesPtr ret;
1008
Daniel Veillard798024a2003-03-19 10:36:09 +00001009 if ((ctxt != NULL) &&
Daniel Veillard9fcd4622009-08-14 16:16:31 +02001010 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001011 ctxt->freeStatesNr--;
1012 ret = ctxt->freeStates[ctxt->freeStatesNr];
1013 ret->nbState = 0;
1014 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001015 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001016 if (size < 16)
1017 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001018
1019 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001020 (size -
1021 1) *
1022 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001023 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001024 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001025 return (NULL);
1026 }
1027 ret->nbState = 0;
1028 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001029 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1030 sizeof
1031 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001032 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001033 xmlRngVErrMemory(ctxt, "allocating states\n");
1034 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001035 return (NULL);
1036 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001037 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001038}
1039
1040/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001041 * xmlRelaxNGAddStateUniq:
1042 * @ctxt: a Relax-NG validation context
1043 * @states: the states container
1044 * @state: the validation state
1045 *
1046 * Add a RelaxNG validation state to the container without checking
1047 * for unicity.
1048 *
1049 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1050 */
1051static int
1052xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001053 xmlRelaxNGStatesPtr states,
1054 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001055{
1056 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001057 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001058 }
1059 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001060 xmlRelaxNGValidStatePtr *tmp;
1061 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001062
Daniel Veillard4c004142003-10-07 11:33:24 +00001063 size = states->maxState * 2;
1064 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1065 (size) *
1066 sizeof
1067 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001068 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001069 xmlRngVErrMemory(ctxt, "adding states\n");
1070 return (-1);
1071 }
1072 states->tabState = tmp;
1073 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001074 }
1075 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001076 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001077}
1078
1079/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001080 * xmlRelaxNGAddState:
1081 * @ctxt: a Relax-NG validation context
1082 * @states: the states container
1083 * @state: the validation state
1084 *
1085 * Add a RelaxNG validation state to the container
1086 *
1087 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1088 */
1089static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001090xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1091 xmlRelaxNGStatesPtr states,
1092 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001093{
1094 int i;
1095
1096 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001097 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001098 }
1099 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001100 xmlRelaxNGValidStatePtr *tmp;
1101 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001102
Daniel Veillard4c004142003-10-07 11:33:24 +00001103 size = states->maxState * 2;
1104 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1105 (size) *
1106 sizeof
1107 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001108 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001109 xmlRngVErrMemory(ctxt, "adding states\n");
1110 return (-1);
1111 }
1112 states->tabState = tmp;
1113 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001114 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001115 for (i = 0; i < states->nbState; i++) {
1116 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1117 xmlRelaxNGFreeValidState(ctxt, state);
1118 return (0);
1119 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001120 }
1121 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001122 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001123}
1124
1125/**
1126 * xmlRelaxNGFreeStates:
1127 * @ctxt: a Relax-NG validation context
1128 * @states: teh container
1129 *
1130 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001131 */
1132static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001133xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001134 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001135{
Daniel Veillard798024a2003-03-19 10:36:09 +00001136 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001137 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001138 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001139 ctxt->freeStatesMax = 40;
1140 ctxt->freeStatesNr = 0;
1141 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1142 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1143 if (ctxt->freeStates == NULL) {
1144 xmlRngVErrMemory(ctxt, "storing states\n");
1145 }
1146 } else if ((ctxt != NULL)
1147 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1148 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001149
Daniel Veillard4c004142003-10-07 11:33:24 +00001150 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1151 2 * ctxt->freeStatesMax *
1152 sizeof
1153 (xmlRelaxNGStatesPtr));
1154 if (tmp == NULL) {
1155 xmlRngVErrMemory(ctxt, "storing states\n");
1156 xmlFree(states->tabState);
1157 xmlFree(states);
1158 return;
1159 }
1160 ctxt->freeStates = tmp;
1161 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001162 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001163 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001164 xmlFree(states->tabState);
1165 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001166 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001167 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001168 }
1169}
1170
1171/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001172 * xmlRelaxNGNewValidState:
1173 * @ctxt: a Relax-NG validation context
1174 * @node: the current node or NULL for the document
1175 *
1176 * Allocate a new RelaxNG validation state
1177 *
1178 * Returns the newly allocated structure or NULL in case or error
1179 */
1180static xmlRelaxNGValidStatePtr
1181xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1182{
1183 xmlRelaxNGValidStatePtr ret;
1184 xmlAttrPtr attr;
1185 xmlAttrPtr attrs[MAX_ATTR];
1186 int nbAttrs = 0;
1187 xmlNodePtr root = NULL;
1188
1189 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001190 root = xmlDocGetRootElement(ctxt->doc);
1191 if (root == NULL)
1192 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001193 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001194 attr = node->properties;
1195 while (attr != NULL) {
1196 if (nbAttrs < MAX_ATTR)
1197 attrs[nbAttrs++] = attr;
1198 else
1199 nbAttrs++;
1200 attr = attr->next;
1201 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001202 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001203 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1204 ctxt->freeState->nbState--;
1205 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001206 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001207 ret =
1208 (xmlRelaxNGValidStatePtr)
1209 xmlMalloc(sizeof(xmlRelaxNGValidState));
1210 if (ret == NULL) {
1211 xmlRngVErrMemory(ctxt, "allocating states\n");
1212 return (NULL);
1213 }
1214 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001215 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001216 ret->value = NULL;
1217 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001218 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001219 ret->node = (xmlNodePtr) ctxt->doc;
1220 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001221 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001222 ret->node = node;
1223 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001224 }
1225 ret->nbAttrs = 0;
1226 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001227 if (ret->attrs == NULL) {
1228 if (nbAttrs < 4)
1229 ret->maxAttrs = 4;
1230 else
1231 ret->maxAttrs = nbAttrs;
1232 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1233 sizeof(xmlAttrPtr));
1234 if (ret->attrs == NULL) {
1235 xmlRngVErrMemory(ctxt, "allocating states\n");
1236 return (ret);
1237 }
1238 } else if (ret->maxAttrs < nbAttrs) {
1239 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001240
Daniel Veillard4c004142003-10-07 11:33:24 +00001241 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1242 sizeof(xmlAttrPtr));
1243 if (tmp == NULL) {
1244 xmlRngVErrMemory(ctxt, "allocating states\n");
1245 return (ret);
1246 }
1247 ret->attrs = tmp;
1248 ret->maxAttrs = nbAttrs;
1249 }
1250 ret->nbAttrs = nbAttrs;
1251 if (nbAttrs < MAX_ATTR) {
1252 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1253 } else {
1254 attr = node->properties;
1255 nbAttrs = 0;
1256 while (attr != NULL) {
1257 ret->attrs[nbAttrs++] = attr;
1258 attr = attr->next;
1259 }
1260 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001261 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001262 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001263 return (ret);
1264}
1265
1266/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001267 * xmlRelaxNGCopyValidState:
1268 * @ctxt: a Relax-NG validation context
1269 * @state: a validation state
1270 *
1271 * Copy the validation state
1272 *
1273 * Returns the newly allocated structure or NULL in case or error
1274 */
1275static xmlRelaxNGValidStatePtr
1276xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001277 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001278{
1279 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001280 unsigned int maxAttrs;
1281 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001282
1283 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001284 return (NULL);
1285 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1286 ctxt->freeState->nbState--;
1287 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001288 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001289 ret =
1290 (xmlRelaxNGValidStatePtr)
1291 xmlMalloc(sizeof(xmlRelaxNGValidState));
1292 if (ret == NULL) {
1293 xmlRngVErrMemory(ctxt, "allocating states\n");
1294 return (NULL);
1295 }
1296 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001297 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001298 attrs = ret->attrs;
1299 maxAttrs = ret->maxAttrs;
1300 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1301 ret->attrs = attrs;
1302 ret->maxAttrs = maxAttrs;
1303 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001304 if (ret->attrs == NULL) {
1305 ret->maxAttrs = state->maxAttrs;
1306 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1307 sizeof(xmlAttrPtr));
1308 if (ret->attrs == NULL) {
1309 xmlRngVErrMemory(ctxt, "allocating states\n");
1310 ret->nbAttrs = 0;
1311 return (ret);
1312 }
1313 } else if (ret->maxAttrs < state->nbAttrs) {
1314 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001315
Daniel Veillard4c004142003-10-07 11:33:24 +00001316 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1317 sizeof(xmlAttrPtr));
1318 if (tmp == NULL) {
1319 xmlRngVErrMemory(ctxt, "allocating states\n");
1320 ret->nbAttrs = 0;
1321 return (ret);
1322 }
1323 ret->maxAttrs = state->maxAttrs;
1324 ret->attrs = tmp;
1325 }
1326 memcpy(ret->attrs, state->attrs,
1327 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001328 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001329 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001330}
1331
1332/**
1333 * xmlRelaxNGEqualValidState:
1334 * @ctxt: a Relax-NG validation context
1335 * @state1: a validation state
1336 * @state2: a validation state
1337 *
1338 * Compare the validation states for equality
1339 *
1340 * Returns 1 if equald, 0 otherwise
1341 */
1342static int
1343xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001344 xmlRelaxNGValidStatePtr state1,
1345 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001346{
1347 int i;
1348
1349 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001350 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001351 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001352 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001353 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001354 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001355 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001356 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001357 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001358 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001359 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001360 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001361 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001362 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001363 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001364 (!xmlStrEqual(state1->value, state2->value)))
1365 return (0);
1366 for (i = 0; i < state1->nbAttrs; i++) {
1367 if (state1->attrs[i] != state2->attrs[i])
1368 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001369 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001370 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001371}
1372
1373/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001374 * xmlRelaxNGFreeValidState:
1375 * @state: a validation state structure
1376 *
1377 * Deallocate a RelaxNG validation state structure.
1378 */
1379static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001380xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001381 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001382{
1383 if (state == NULL)
1384 return;
1385
Daniel Veillard798024a2003-03-19 10:36:09 +00001386 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001387 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001388 }
1389 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001390 if (state->attrs != NULL)
1391 xmlFree(state->attrs);
1392 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001393 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001394 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001395 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001396}
1397
1398/************************************************************************
1399 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001400 * Semi internal functions *
1401 * *
1402 ************************************************************************/
1403
1404/**
1405 * xmlRelaxParserSetFlag:
1406 * @ctxt: a RelaxNG parser context
1407 * @flags: a set of flags values
1408 *
1409 * Semi private function used to pass informations to a parser context
1410 * which are a combination of xmlRelaxNGParserFlag .
1411 *
1412 * Returns 0 if success and -1 in case of error
1413 */
1414int
1415xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1416{
1417 if (ctxt == NULL) return(-1);
1418 if (flags & XML_RELAXNGP_FREE_DOC) {
1419 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1420 flags -= XML_RELAXNGP_FREE_DOC;
1421 }
1422 if (flags & XML_RELAXNGP_CRNG) {
1423 ctxt->crng |= XML_RELAXNGP_CRNG;
1424 flags -= XML_RELAXNGP_CRNG;
1425 }
1426 if (flags != 0) return(-1);
1427 return(0);
1428}
1429
1430/************************************************************************
1431 * *
1432 * Document functions *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001433 * *
1434 ************************************************************************/
1435static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001436 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001437
1438/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001439 * xmlRelaxNGIncludePush:
1440 * @ctxt: the parser context
1441 * @value: the element doc
1442 *
1443 * Pushes a new include on top of the include stack
1444 *
1445 * Returns 0 in case of error, the index in the stack otherwise
1446 */
1447static int
1448xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001449 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001450{
1451 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001452 ctxt->incMax = 4;
1453 ctxt->incNr = 0;
1454 ctxt->incTab =
1455 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1456 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001457 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001458 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001459 return (0);
1460 }
1461 }
1462 if (ctxt->incNr >= ctxt->incMax) {
1463 ctxt->incMax *= 2;
1464 ctxt->incTab =
1465 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001466 ctxt->incMax *
1467 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001468 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001469 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001470 return (0);
1471 }
1472 }
1473 ctxt->incTab[ctxt->incNr] = value;
1474 ctxt->inc = value;
1475 return (ctxt->incNr++);
1476}
1477
1478/**
1479 * xmlRelaxNGIncludePop:
1480 * @ctxt: the parser context
1481 *
1482 * Pops the top include from the include stack
1483 *
1484 * Returns the include just removed
1485 */
1486static xmlRelaxNGIncludePtr
1487xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1488{
1489 xmlRelaxNGIncludePtr ret;
1490
1491 if (ctxt->incNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001492 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001493 ctxt->incNr--;
1494 if (ctxt->incNr > 0)
1495 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1496 else
1497 ctxt->inc = NULL;
1498 ret = ctxt->incTab[ctxt->incNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001499 ctxt->incTab[ctxt->incNr] = NULL;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001500 return (ret);
1501}
1502
1503/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001504 * xmlRelaxNGRemoveRedefine:
1505 * @ctxt: the parser context
1506 * @URL: the normalized URL
1507 * @target: the included target
1508 * @name: the define name to eliminate
1509 *
1510 * Applies the elimination algorithm of 4.7
1511 *
1512 * Returns 0 in case of error, 1 in case of success.
1513 */
1514static int
1515xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001516 const xmlChar * URL ATTRIBUTE_UNUSED,
1517 xmlNodePtr target, const xmlChar * name)
1518{
Daniel Veillard5add8682003-03-10 13:13:58 +00001519 int found = 0;
1520 xmlNodePtr tmp, tmp2;
1521 xmlChar *name2;
1522
1523#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001524 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001525 xmlGenericError(xmlGenericErrorContext,
1526 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001527 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001528 xmlGenericError(xmlGenericErrorContext,
1529 "Elimination of <include> define %s from %s\n",
1530 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001531#endif
1532 tmp = target;
1533 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001534 tmp2 = tmp->next;
1535 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1536 found = 1;
1537 xmlUnlinkNode(tmp);
1538 xmlFreeNode(tmp);
1539 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1540 name2 = xmlGetProp(tmp, BAD_CAST "name");
1541 xmlRelaxNGNormExtSpace(name2);
1542 if (name2 != NULL) {
1543 if (xmlStrEqual(name, name2)) {
1544 found = 1;
1545 xmlUnlinkNode(tmp);
1546 xmlFreeNode(tmp);
1547 }
1548 xmlFree(name2);
1549 }
1550 } else if (IS_RELAXNG(tmp, "include")) {
1551 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001552 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001553
Daniel Veillard4c004142003-10-07 11:33:24 +00001554 if ((inc != NULL) && (inc->doc != NULL) &&
1555 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001556
Daniel Veillard4c004142003-10-07 11:33:24 +00001557 if (xmlStrEqual
1558 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001559#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001560 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001561#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001562 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1563 inc->doc->children->
1564 children, name) == 1) {
1565 found = 1;
1566 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001567#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001568 if (href != NULL)
1569 xmlFree(href);
Daniel Veillard14b56432006-03-09 18:41:40 +00001570#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001571 }
1572 }
1573 }
1574 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001575 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001576 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001577}
1578
1579/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001580 * xmlRelaxNGLoadInclude:
1581 * @ctxt: the parser context
1582 * @URL: the normalized URL
1583 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001584 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001585 *
1586 * First lookup if the document is already loaded into the parser context,
1587 * check against recursion. If not found the resource is loaded and
1588 * the content is preprocessed before being returned back to the caller.
1589 *
1590 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1591 */
1592static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001593xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1594 xmlNodePtr node, const xmlChar * ns)
1595{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001596 xmlRelaxNGIncludePtr ret = NULL;
1597 xmlDocPtr doc;
1598 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001599 xmlNodePtr root, cur;
1600
1601#ifdef DEBUG_INCLUDE
1602 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001603 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001604#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001605
1606 /*
1607 * check against recursion in the stack
1608 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001609 for (i = 0; i < ctxt->incNr; i++) {
1610 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1611 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1612 "Detected an Include recursion for %s\n", URL,
1613 NULL);
1614 return (NULL);
1615 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001616 }
1617
1618 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001619 * load the document
1620 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001621 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001622 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001623 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1624 "xmlRelaxNG: could not load %s\n", URL, NULL);
1625 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001626 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001627#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001628 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001629#endif
1630
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001631 /*
1632 * Allocate the document structures and register it first.
1633 */
1634 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1635 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001636 xmlRngPErrMemory(ctxt, "allocating include\n");
1637 xmlFreeDoc(doc);
1638 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001639 }
1640 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1641 ret->doc = doc;
1642 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001643 ret->next = ctxt->includes;
1644 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001645
1646 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001647 * transmit the ns if needed
1648 */
1649 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001650 root = xmlDocGetRootElement(doc);
1651 if (root != NULL) {
1652 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1653 xmlSetProp(root, BAD_CAST "ns", ns);
1654 }
1655 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001656 }
1657
1658 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001659 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001660 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001661 xmlRelaxNGIncludePush(ctxt, ret);
1662
1663 /*
1664 * Some preprocessing of the document content, this include recursing
1665 * in the include stack.
1666 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001667#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001668 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001669#endif
1670
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001671 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1672 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001673 ctxt->inc = NULL;
1674 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001675 }
1676
1677 /*
1678 * Pop up the include from the stack
1679 */
1680 xmlRelaxNGIncludePop(ctxt);
1681
Daniel Veillard5add8682003-03-10 13:13:58 +00001682#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001683 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001684#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001685 /*
1686 * Check that the top element is a grammar
1687 */
1688 root = xmlDocGetRootElement(doc);
1689 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001690 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1691 "xmlRelaxNG: included document is empty %s\n", URL,
1692 NULL);
1693 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001694 }
1695 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001696 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1697 "xmlRelaxNG: included document %s root is not a grammar\n",
1698 URL, NULL);
1699 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001700 }
1701
1702 /*
1703 * Elimination of redefined rules in the include.
1704 */
1705 cur = node->children;
1706 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001707 if (IS_RELAXNG(cur, "start")) {
1708 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001709
Daniel Veillard4c004142003-10-07 11:33:24 +00001710 found =
1711 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1712 if (!found) {
1713 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1714 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1715 URL, NULL);
1716 }
1717 } else if (IS_RELAXNG(cur, "define")) {
1718 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001719
Daniel Veillard4c004142003-10-07 11:33:24 +00001720 name = xmlGetProp(cur, BAD_CAST "name");
1721 if (name == NULL) {
1722 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1723 "xmlRelaxNG: include %s has define without name\n",
1724 URL, NULL);
1725 } else {
1726 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001727
Daniel Veillard4c004142003-10-07 11:33:24 +00001728 xmlRelaxNGNormExtSpace(name);
1729 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1730 root->children, name);
1731 if (!found) {
1732 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1733 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1734 URL, name);
1735 }
1736 xmlFree(name);
1737 }
1738 }
1739 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001740 }
1741
1742
Daniel Veillard4c004142003-10-07 11:33:24 +00001743 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001744}
1745
1746/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001747 * xmlRelaxNGValidErrorPush:
1748 * @ctxt: the validation context
1749 * @err: the error code
1750 * @arg1: the first string argument
1751 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001752 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001753 *
1754 * Pushes a new error on top of the error stack
1755 *
1756 * Returns 0 in case of error, the index in the stack otherwise
1757 */
1758static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001759xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1760 xmlRelaxNGValidErr err, const xmlChar * arg1,
1761 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001762{
1763 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001764
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001765#ifdef DEBUG_ERROR
1766 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001767 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001768#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001769 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001770 ctxt->errMax = 8;
1771 ctxt->errNr = 0;
1772 ctxt->errTab =
1773 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1774 sizeof
1775 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001776 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001777 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001778 return (0);
1779 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001780 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001781 }
1782 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001783 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001784 ctxt->errTab =
1785 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001786 ctxt->errMax *
1787 sizeof
1788 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001789 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001790 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001791 return (0);
1792 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001793 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001794 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001795 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001796 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1797 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001798 cur = &ctxt->errTab[ctxt->errNr];
1799 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001800 if (dup) {
1801 cur->arg1 = xmlStrdup(arg1);
1802 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001803 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001804 } else {
1805 cur->arg1 = arg1;
1806 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001807 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001808 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001809 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001810 cur->node = ctxt->state->node;
1811 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001812 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001813 cur->node = NULL;
1814 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001815 }
1816 ctxt->err = cur;
1817 return (ctxt->errNr++);
1818}
1819
1820/**
1821 * xmlRelaxNGValidErrorPop:
1822 * @ctxt: the validation context
1823 *
1824 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001825 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001826static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001827xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1828{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001829 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001830
Daniel Veillard580ced82003-03-21 21:22:48 +00001831 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001832 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001833 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001834 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001835 ctxt->errNr--;
1836 if (ctxt->errNr > 0)
1837 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1838 else
1839 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001840 cur = &ctxt->errTab[ctxt->errNr];
1841 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001842 if (cur->arg1 != NULL)
1843 xmlFree((xmlChar *) cur->arg1);
1844 cur->arg1 = NULL;
1845 if (cur->arg2 != NULL)
1846 xmlFree((xmlChar *) cur->arg2);
1847 cur->arg2 = NULL;
1848 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001849 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001850}
1851
Daniel Veillard42f12e92003-03-07 18:32:59 +00001852/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001853 * xmlRelaxNGDocumentPush:
1854 * @ctxt: the parser context
1855 * @value: the element doc
1856 *
1857 * Pushes a new doc on top of the doc stack
1858 *
1859 * Returns 0 in case of error, the index in the stack otherwise
1860 */
1861static int
1862xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001863 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001864{
1865 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001866 ctxt->docMax = 4;
1867 ctxt->docNr = 0;
1868 ctxt->docTab =
1869 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1870 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001871 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001872 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001873 return (0);
1874 }
1875 }
1876 if (ctxt->docNr >= ctxt->docMax) {
1877 ctxt->docMax *= 2;
1878 ctxt->docTab =
1879 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001880 ctxt->docMax *
1881 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001882 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001883 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001884 return (0);
1885 }
1886 }
1887 ctxt->docTab[ctxt->docNr] = value;
1888 ctxt->doc = value;
1889 return (ctxt->docNr++);
1890}
1891
1892/**
1893 * xmlRelaxNGDocumentPop:
1894 * @ctxt: the parser context
1895 *
1896 * Pops the top doc from the doc stack
1897 *
1898 * Returns the doc just removed
1899 */
1900static xmlRelaxNGDocumentPtr
1901xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1902{
1903 xmlRelaxNGDocumentPtr ret;
1904
1905 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001906 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001907 ctxt->docNr--;
1908 if (ctxt->docNr > 0)
1909 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1910 else
1911 ctxt->doc = NULL;
1912 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001913 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001914 return (ret);
1915}
1916
1917/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001918 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001919 * @ctxt: the parser context
1920 * @URL: the normalized URL
1921 * @ns: the inherited ns if any
1922 *
1923 * First lookup if the document is already loaded into the parser context,
1924 * check against recursion. If not found the resource is loaded and
1925 * the content is preprocessed before being returned back to the caller.
1926 *
1927 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1928 */
1929static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001930xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1931 const xmlChar * URL, const xmlChar * ns)
1932{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001933 xmlRelaxNGDocumentPtr ret = NULL;
1934 xmlDocPtr doc;
1935 xmlNodePtr root;
1936 int i;
1937
1938 /*
1939 * check against recursion in the stack
1940 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001941 for (i = 0; i < ctxt->docNr; i++) {
1942 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1943 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1944 "Detected an externalRef recursion for %s\n", URL,
1945 NULL);
1946 return (NULL);
1947 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001948 }
1949
1950 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001951 * load the document
1952 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001953 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001954 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001955 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1956 "xmlRelaxNG: could not load %s\n", URL, NULL);
1957 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001958 }
1959
1960 /*
1961 * Allocate the document structures and register it first.
1962 */
1963 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1964 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001965 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1966 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1967 xmlFreeDoc(doc);
1968 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001969 }
1970 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1971 ret->doc = doc;
1972 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001973 ret->next = ctxt->documents;
Daniel Veillard81c51e12009-08-14 18:52:10 +02001974 ret->externalRef = 1;
Daniel Veillardc482e262003-02-26 14:48:48 +00001975 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001976
1977 /*
1978 * transmit the ns if needed
1979 */
1980 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001981 root = xmlDocGetRootElement(doc);
1982 if (root != NULL) {
1983 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1984 xmlSetProp(root, BAD_CAST "ns", ns);
1985 }
1986 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001987 }
1988
1989 /*
1990 * push it on the stack and register it in the hash table
1991 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001992 xmlRelaxNGDocumentPush(ctxt, ret);
1993
1994 /*
1995 * Some preprocessing of the document content
1996 */
1997 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1998 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001999 ctxt->doc = NULL;
2000 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002001 }
2002
2003 xmlRelaxNGDocumentPop(ctxt);
2004
Daniel Veillard4c004142003-10-07 11:33:24 +00002005 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002006}
2007
2008/************************************************************************
2009 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002010 * Error functions *
2011 * *
2012 ************************************************************************/
2013
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002014#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2015#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2016#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2017#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2018#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002019
Daniel Veillard231d7912003-02-09 14:22:17 +00002020static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002021xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2022{
Daniel Veillard231d7912003-02-09 14:22:17 +00002023 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002024 return ("none");
2025 switch (def->type) {
2026 case XML_RELAXNG_EMPTY:
2027 return ("empty");
2028 case XML_RELAXNG_NOT_ALLOWED:
2029 return ("notAllowed");
2030 case XML_RELAXNG_EXCEPT:
2031 return ("except");
2032 case XML_RELAXNG_TEXT:
2033 return ("text");
2034 case XML_RELAXNG_ELEMENT:
2035 return ("element");
2036 case XML_RELAXNG_DATATYPE:
2037 return ("datatype");
2038 case XML_RELAXNG_VALUE:
2039 return ("value");
2040 case XML_RELAXNG_LIST:
2041 return ("list");
2042 case XML_RELAXNG_ATTRIBUTE:
2043 return ("attribute");
2044 case XML_RELAXNG_DEF:
2045 return ("def");
2046 case XML_RELAXNG_REF:
2047 return ("ref");
2048 case XML_RELAXNG_EXTERNALREF:
2049 return ("externalRef");
2050 case XML_RELAXNG_PARENTREF:
2051 return ("parentRef");
2052 case XML_RELAXNG_OPTIONAL:
2053 return ("optional");
2054 case XML_RELAXNG_ZEROORMORE:
2055 return ("zeroOrMore");
2056 case XML_RELAXNG_ONEORMORE:
2057 return ("oneOrMore");
2058 case XML_RELAXNG_CHOICE:
2059 return ("choice");
2060 case XML_RELAXNG_GROUP:
2061 return ("group");
2062 case XML_RELAXNG_INTERLEAVE:
2063 return ("interleave");
2064 case XML_RELAXNG_START:
2065 return ("start");
2066 case XML_RELAXNG_NOOP:
2067 return ("noop");
2068 case XML_RELAXNG_PARAM:
2069 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002070 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002071 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002072}
Daniel Veillardd2298792003-02-14 16:54:11 +00002073
Daniel Veillard6eadf632003-01-23 18:29:16 +00002074/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002075 * xmlRelaxNGGetErrorString:
2076 * @err: the error code
2077 * @arg1: the first string argument
2078 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002079 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002080 * computes a formatted error string for the given error code and args
2081 *
2082 * Returns the error string, it must be deallocated by the caller
2083 */
2084static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002085xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2086 const xmlChar * arg2)
2087{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002088 char msg[1000];
2089
2090 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002091 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002092 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002093 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002094
2095 msg[0] = 0;
2096 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002097 case XML_RELAXNG_OK:
2098 return (NULL);
2099 case XML_RELAXNG_ERR_MEMORY:
2100 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002101 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002102 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2103 break;
2104 case XML_RELAXNG_ERR_TYPEVAL:
2105 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2106 arg2);
2107 break;
2108 case XML_RELAXNG_ERR_DUPID:
2109 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2110 break;
2111 case XML_RELAXNG_ERR_TYPECMP:
2112 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2113 break;
2114 case XML_RELAXNG_ERR_NOSTATE:
2115 return (xmlCharStrdup("Internal error: no state\n"));
2116 case XML_RELAXNG_ERR_NODEFINE:
2117 return (xmlCharStrdup("Internal error: no define\n"));
2118 case XML_RELAXNG_ERR_INTERNAL:
2119 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2120 break;
2121 case XML_RELAXNG_ERR_LISTEXTRA:
2122 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2123 break;
2124 case XML_RELAXNG_ERR_INTERNODATA:
2125 return (xmlCharStrdup
2126 ("Internal: interleave block has no data\n"));
2127 case XML_RELAXNG_ERR_INTERSEQ:
2128 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2129 case XML_RELAXNG_ERR_INTEREXTRA:
2130 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2131 break;
2132 case XML_RELAXNG_ERR_ELEMNAME:
2133 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2134 arg2);
2135 break;
2136 case XML_RELAXNG_ERR_ELEMNONS:
2137 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2138 arg1);
2139 break;
2140 case XML_RELAXNG_ERR_ELEMWRONGNS:
2141 snprintf(msg, 1000,
2142 "Element %s has wrong namespace: expecting %s\n", arg1,
2143 arg2);
2144 break;
2145 case XML_RELAXNG_ERR_ELEMWRONG:
2146 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2147 break;
2148 case XML_RELAXNG_ERR_TEXTWRONG:
2149 snprintf(msg, 1000,
2150 "Did not expect text in element %s content\n", arg1);
2151 break;
2152 case XML_RELAXNG_ERR_ELEMEXTRANS:
2153 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2154 arg1);
2155 break;
2156 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2157 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2158 break;
2159 case XML_RELAXNG_ERR_NOELEM:
2160 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2161 arg1);
2162 break;
2163 case XML_RELAXNG_ERR_NOTELEM:
2164 return (xmlCharStrdup("Expecting an element got text\n"));
2165 case XML_RELAXNG_ERR_ATTRVALID:
2166 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2167 arg1);
2168 break;
2169 case XML_RELAXNG_ERR_CONTENTVALID:
2170 snprintf(msg, 1000, "Element %s failed to validate content\n",
2171 arg1);
2172 break;
2173 case XML_RELAXNG_ERR_EXTRACONTENT:
2174 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2175 arg1, arg2);
2176 break;
2177 case XML_RELAXNG_ERR_INVALIDATTR:
2178 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2179 arg1, arg2);
2180 break;
2181 case XML_RELAXNG_ERR_LACKDATA:
2182 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2183 arg1);
2184 break;
2185 case XML_RELAXNG_ERR_DATAELEM:
2186 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2187 arg1);
2188 break;
2189 case XML_RELAXNG_ERR_VALELEM:
2190 snprintf(msg, 1000, "Value element %s has child elements\n",
2191 arg1);
2192 break;
2193 case XML_RELAXNG_ERR_LISTELEM:
2194 snprintf(msg, 1000, "List element %s has child elements\n",
2195 arg1);
2196 break;
2197 case XML_RELAXNG_ERR_DATATYPE:
2198 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2199 break;
2200 case XML_RELAXNG_ERR_VALUE:
2201 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2202 break;
2203 case XML_RELAXNG_ERR_LIST:
2204 return (xmlCharStrdup("Error validating list\n"));
2205 case XML_RELAXNG_ERR_NOGRAMMAR:
2206 return (xmlCharStrdup("No top grammar defined\n"));
2207 case XML_RELAXNG_ERR_EXTRADATA:
2208 return (xmlCharStrdup("Extra data in the document\n"));
2209 default:
2210 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002211 }
2212 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002213 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002214 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002215 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002216 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002217}
2218
2219/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002220 * xmlRelaxNGShowValidError:
2221 * @ctxt: the validation context
2222 * @err: the error number
2223 * @node: the node
2224 * @child: the node child generating the problem.
2225 * @arg1: the first argument
2226 * @arg2: the second argument
2227 *
2228 * Show a validation error.
2229 */
2230static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002231xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2232 xmlRelaxNGValidErr err, xmlNodePtr node,
2233 xmlNodePtr child, const xmlChar * arg1,
2234 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002235{
2236 xmlChar *msg;
2237
Daniel Veillardb30ca312005-09-04 13:50:03 +00002238 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002239 return;
2240
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002241#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002242 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002243#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002244 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2245 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002246 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002247
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002248 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002249 ctxt->errNo = err;
2250 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2251 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002252 xmlFree(msg);
2253}
2254
2255/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002256 * xmlRelaxNGPopErrors:
2257 * @ctxt: the validation context
2258 * @level: the error level in the stack
2259 *
2260 * pop and discard all errors until the given level is reached
2261 */
2262static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002263xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2264{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002265 int i;
2266 xmlRelaxNGValidErrorPtr err;
2267
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002268#ifdef DEBUG_ERROR
2269 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002270 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002271#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002272 for (i = level; i < ctxt->errNr; i++) {
2273 err = &ctxt->errTab[i];
2274 if (err->flags & ERROR_IS_DUP) {
2275 if (err->arg1 != NULL)
2276 xmlFree((xmlChar *) err->arg1);
2277 err->arg1 = NULL;
2278 if (err->arg2 != NULL)
2279 xmlFree((xmlChar *) err->arg2);
2280 err->arg2 = NULL;
2281 err->flags = 0;
2282 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002283 }
2284 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002285 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002286 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002287}
Daniel Veillard4c004142003-10-07 11:33:24 +00002288
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002289/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002290 * xmlRelaxNGDumpValidError:
2291 * @ctxt: the validation context
2292 *
2293 * Show all validation error over a given index.
2294 */
2295static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002296xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2297{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002298 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002299 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002300
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002301#ifdef DEBUG_ERROR
2302 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002303 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002304#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002305 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2306 err = &ctxt->errTab[i];
2307 if (k < MAX_ERROR) {
2308 for (j = 0; j < i; j++) {
2309 dup = &ctxt->errTab[j];
2310 if ((err->err == dup->err) && (err->node == dup->node) &&
2311 (xmlStrEqual(err->arg1, dup->arg1)) &&
2312 (xmlStrEqual(err->arg2, dup->arg2))) {
2313 goto skip;
2314 }
2315 }
2316 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2317 err->arg1, err->arg2);
2318 k++;
2319 }
2320 skip:
2321 if (err->flags & ERROR_IS_DUP) {
2322 if (err->arg1 != NULL)
2323 xmlFree((xmlChar *) err->arg1);
2324 err->arg1 = NULL;
2325 if (err->arg2 != NULL)
2326 xmlFree((xmlChar *) err->arg2);
2327 err->arg2 = NULL;
2328 err->flags = 0;
2329 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002330 }
2331 ctxt->errNr = 0;
2332}
Daniel Veillard4c004142003-10-07 11:33:24 +00002333
Daniel Veillard42f12e92003-03-07 18:32:59 +00002334/**
2335 * xmlRelaxNGAddValidError:
2336 * @ctxt: the validation context
2337 * @err: the error number
2338 * @arg1: the first argument
2339 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002340 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002341 *
2342 * Register a validation error, either generating it if it's sure
2343 * or stacking it for later handling if unsure.
2344 */
2345static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002346xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2347 xmlRelaxNGValidErr err, const xmlChar * arg1,
2348 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002349{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002350 if (ctxt == NULL)
2351 return;
2352 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002353 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002354
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002355#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002356 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002357#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002358 /*
2359 * generate the error directly
2360 */
William M. Brack60929622004-03-27 17:54:18 +00002361 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2362 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002363 xmlNodePtr node, seq;
2364
2365 /*
2366 * Flush first any stacked error which might be the
2367 * real cause of the problem.
2368 */
2369 if (ctxt->errNr != 0)
2370 xmlRelaxNGDumpValidError(ctxt);
2371 if (ctxt->state != NULL) {
2372 node = ctxt->state->node;
2373 seq = ctxt->state->seq;
2374 } else {
2375 node = seq = NULL;
2376 }
Daniel Veillardec18c962009-08-26 18:37:43 +02002377 if ((node == NULL) && (seq == NULL)) {
2378 node = ctxt->pnode;
2379 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002380 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002381 }
2382 /*
2383 * Stack the error for later processing if needed
2384 */
2385 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002386 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002387 }
2388}
2389
Daniel Veillard6eadf632003-01-23 18:29:16 +00002390
2391/************************************************************************
2392 * *
2393 * Type library hooks *
2394 * *
2395 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002396static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002397 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002398
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002399/**
2400 * xmlRelaxNGSchemaTypeHave:
2401 * @data: data needed for the library
2402 * @type: the type name
2403 *
2404 * Check if the given type is provided by
2405 * the W3C XMLSchema Datatype library.
2406 *
2407 * Returns 1 if yes, 0 if no and -1 in case of error.
2408 */
2409static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002410xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2411{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002412 xmlSchemaTypePtr typ;
2413
2414 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002415 return (-1);
2416 typ = xmlSchemaGetPredefinedType(type,
2417 BAD_CAST
2418 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002419 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002420 return (0);
2421 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002422}
2423
2424/**
2425 * xmlRelaxNGSchemaTypeCheck:
2426 * @data: data needed for the library
2427 * @type: the type name
2428 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002429 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002430 *
2431 * Check if the given type and value are validated by
2432 * the W3C XMLSchema Datatype library.
2433 *
2434 * Returns 1 if yes, 0 if no and -1 in case of error.
2435 */
2436static int
2437xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002438 const xmlChar * type,
2439 const xmlChar * value,
2440 void **result, xmlNodePtr node)
2441{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002442 xmlSchemaTypePtr typ;
2443 int ret;
2444
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002445 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002446 return (-1);
2447 typ = xmlSchemaGetPredefinedType(type,
2448 BAD_CAST
2449 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002450 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002451 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002452 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002453 (xmlSchemaValPtr *) result, node);
2454 if (ret == 2) /* special ID error code */
2455 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002456 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002457 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002458 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002459 return (0);
2460 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002461}
2462
2463/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002464 * xmlRelaxNGSchemaFacetCheck:
2465 * @data: data needed for the library
2466 * @type: the type name
2467 * @facet: the facet name
2468 * @val: the facet value
2469 * @strval: the string value
2470 * @value: the value to check
2471 *
2472 * Function provided by a type library to check a value facet
2473 *
2474 * Returns 1 if yes, 0 if no and -1 in case of error.
2475 */
2476static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002477xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2478 const xmlChar * type, const xmlChar * facetname,
2479 const xmlChar * val, const xmlChar * strval,
2480 void *value)
2481{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002482 xmlSchemaFacetPtr facet;
2483 xmlSchemaTypePtr typ;
2484 int ret;
2485
2486 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002487 return (-1);
2488 typ = xmlSchemaGetPredefinedType(type,
2489 BAD_CAST
2490 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002491 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002492 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002493
2494 facet = xmlSchemaNewFacet();
2495 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002496 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002497
Daniel Veillard4c004142003-10-07 11:33:24 +00002498 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002499 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002500 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002501 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002502 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002503 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002504 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002505 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002506 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002507 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002508 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002509 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002510 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002511 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002512 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002513 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002514 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002515 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002516 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002517 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002518 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002519 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2520 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2521 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2522 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002523 xmlSchemaFreeFacet(facet);
2524 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002525 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002526 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002527 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2528 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002529 xmlSchemaFreeFacet(facet);
2530 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002531 }
2532 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2533 xmlSchemaFreeFacet(facet);
2534 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002535 return (-1);
2536 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002537}
2538
2539/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002540 * xmlRelaxNGSchemaFreeValue:
2541 * @data: data needed for the library
2542 * @value: the value to free
2543 *
2544 * Function provided by a type library to free a Schemas value
2545 *
2546 * Returns 1 if yes, 0 if no and -1 in case of error.
2547 */
2548static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002549xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2550{
Daniel Veillard80b19092003-03-28 13:29:53 +00002551 xmlSchemaFreeValue(value);
2552}
2553
2554/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002555 * xmlRelaxNGSchemaTypeCompare:
2556 * @data: data needed for the library
2557 * @type: the type name
2558 * @value1: the first value
2559 * @value2: the second value
2560 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002561 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002562 * Datatype library.
2563 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002564 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002565 */
2566static int
2567xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002568 const xmlChar * type,
2569 const xmlChar * value1,
2570 xmlNodePtr ctxt1,
2571 void *comp1,
2572 const xmlChar * value2, xmlNodePtr ctxt2)
2573{
Daniel Veillard80b19092003-03-28 13:29:53 +00002574 int ret;
2575 xmlSchemaTypePtr typ;
2576 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2577
2578 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002579 return (-1);
2580 typ = xmlSchemaGetPredefinedType(type,
2581 BAD_CAST
2582 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002583 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002584 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002585 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002586 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2587 if (ret != 0)
2588 return (-1);
2589 if (res1 == NULL)
2590 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002591 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002592 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002593 }
2594 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002595 if (ret != 0) {
Daniel Veillardf4644032005-06-13 11:41:31 +00002596 if ((comp1 == NULL) && (res1 != NULL))
2597 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002598 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002599 }
2600 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002601 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002602 }
2603 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002604 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002605 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002606 xmlSchemaFreeValue(res2);
2607 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002608 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002609 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002610 return (1);
2611 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002612}
Daniel Veillard4c004142003-10-07 11:33:24 +00002613
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002614/**
2615 * xmlRelaxNGDefaultTypeHave:
2616 * @data: data needed for the library
2617 * @type: the type name
2618 *
2619 * Check if the given type is provided by
2620 * the default datatype library.
2621 *
2622 * Returns 1 if yes, 0 if no and -1 in case of error.
2623 */
2624static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002625xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2626 const xmlChar * type)
2627{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002628 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002629 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002630 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002631 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002632 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002633 return (1);
2634 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002635}
2636
2637/**
2638 * xmlRelaxNGDefaultTypeCheck:
2639 * @data: data needed for the library
2640 * @type: the type name
2641 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002642 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002643 *
2644 * Check if the given type and value are validated by
2645 * the default datatype library.
2646 *
2647 * Returns 1 if yes, 0 if no and -1 in case of error.
2648 */
2649static int
2650xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002651 const xmlChar * type ATTRIBUTE_UNUSED,
2652 const xmlChar * value ATTRIBUTE_UNUSED,
2653 void **result ATTRIBUTE_UNUSED,
2654 xmlNodePtr node ATTRIBUTE_UNUSED)
2655{
Daniel Veillardd4310742003-02-18 21:12:46 +00002656 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002657 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002658 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002659 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002660 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002661 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002662 }
2663
Daniel Veillard4c004142003-10-07 11:33:24 +00002664 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002665}
2666
2667/**
2668 * xmlRelaxNGDefaultTypeCompare:
2669 * @data: data needed for the library
2670 * @type: the type name
2671 * @value1: the first value
2672 * @value2: the second value
2673 *
2674 * Compare two values accordingly a type from the default
2675 * datatype library.
2676 *
2677 * Returns 1 if yes, 0 if no and -1 in case of error.
2678 */
2679static int
2680xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002681 const xmlChar * type,
2682 const xmlChar * value1,
2683 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2684 void *comp1 ATTRIBUTE_UNUSED,
2685 const xmlChar * value2,
2686 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2687{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002688 int ret = -1;
2689
2690 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002691 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002692 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002693 if (!xmlStrEqual(value1, value2)) {
2694 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002695
Daniel Veillard4c004142003-10-07 11:33:24 +00002696 /*
2697 * TODO: trivial optimizations are possible by
2698 * computing at compile-time
2699 */
2700 nval = xmlRelaxNGNormalize(NULL, value1);
2701 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002702
Daniel Veillard4c004142003-10-07 11:33:24 +00002703 if ((nval == NULL) || (nvalue == NULL))
2704 ret = -1;
2705 else if (xmlStrEqual(nval, nvalue))
2706 ret = 1;
2707 else
2708 ret = 0;
2709 if (nval != NULL)
2710 xmlFree(nval);
2711 if (nvalue != NULL)
2712 xmlFree(nvalue);
2713 } else
2714 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002715 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002716 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002717}
Daniel Veillard4c004142003-10-07 11:33:24 +00002718
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002719static int xmlRelaxNGTypeInitialized = 0;
2720static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2721
2722/**
2723 * xmlRelaxNGFreeTypeLibrary:
2724 * @lib: the type library structure
2725 * @namespace: the URI bound to the library
2726 *
2727 * Free the structure associated to the type library
2728 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002729static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002730xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002731 const xmlChar * namespace ATTRIBUTE_UNUSED)
2732{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002733 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002734 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002735 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002736 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002737 xmlFree(lib);
2738}
2739
2740/**
2741 * xmlRelaxNGRegisterTypeLibrary:
2742 * @namespace: the URI bound to the library
2743 * @data: data associated to the library
2744 * @have: the provide function
2745 * @check: the checking function
2746 * @comp: the comparison function
2747 *
2748 * Register a new type library
2749 *
2750 * Returns 0 in case of success and -1 in case of error.
2751 */
2752static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002753xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2754 xmlRelaxNGTypeHave have,
2755 xmlRelaxNGTypeCheck check,
2756 xmlRelaxNGTypeCompare comp,
2757 xmlRelaxNGFacetCheck facet,
2758 xmlRelaxNGTypeFree freef)
2759{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002760 xmlRelaxNGTypeLibraryPtr lib;
2761 int ret;
2762
2763 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002764 (check == NULL) || (comp == NULL))
2765 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002766 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002767 xmlGenericError(xmlGenericErrorContext,
2768 "Relax-NG types library '%s' already registered\n",
2769 namespace);
2770 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002771 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002772 lib =
2773 (xmlRelaxNGTypeLibraryPtr)
2774 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002775 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002776 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002777 return (-1);
2778 }
2779 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2780 lib->namespace = xmlStrdup(namespace);
2781 lib->data = data;
2782 lib->have = have;
2783 lib->comp = comp;
2784 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002785 lib->facet = facet;
2786 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002787 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2788 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002789 xmlGenericError(xmlGenericErrorContext,
2790 "Relax-NG types library failed to register '%s'\n",
2791 namespace);
2792 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2793 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002794 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002795 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002796}
2797
2798/**
2799 * xmlRelaxNGInitTypes:
2800 *
2801 * Initilize the default type libraries.
2802 *
2803 * Returns 0 in case of success and -1 in case of error.
2804 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002805int
Daniel Veillard4c004142003-10-07 11:33:24 +00002806xmlRelaxNGInitTypes(void)
2807{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002808 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002809 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002810 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2811 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002812 xmlGenericError(xmlGenericErrorContext,
2813 "Failed to allocate sh table for Relax-NG types\n");
2814 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002815 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002816 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2817 "http://www.w3.org/2001/XMLSchema-datatypes",
2818 NULL, xmlRelaxNGSchemaTypeHave,
2819 xmlRelaxNGSchemaTypeCheck,
2820 xmlRelaxNGSchemaTypeCompare,
2821 xmlRelaxNGSchemaFacetCheck,
2822 xmlRelaxNGSchemaFreeValue);
2823 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2824 xmlRelaxNGDefaultTypeHave,
2825 xmlRelaxNGDefaultTypeCheck,
2826 xmlRelaxNGDefaultTypeCompare, NULL,
2827 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002828 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002829 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002830}
2831
2832/**
2833 * xmlRelaxNGCleanupTypes:
2834 *
2835 * Cleanup the default Schemas type library associated to RelaxNG
2836 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002837void
2838xmlRelaxNGCleanupTypes(void)
2839{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002840 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002841 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002842 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002843 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002844 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002845 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002846}
2847
2848/************************************************************************
2849 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002850 * Compiling element content into regexp *
2851 * *
2852 * Sometime the element content can be compiled into a pure regexp, *
2853 * This allows a faster execution and streamability at that level *
2854 * *
2855 ************************************************************************/
2856
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002857/* from automata.c but not exported */
2858void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2859
2860
Daniel Veillard52b48c72003-04-13 19:53:42 +00002861static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2862 xmlRelaxNGDefinePtr def);
2863
Daniel Veillard952379b2003-03-17 15:37:12 +00002864/**
2865 * xmlRelaxNGIsCompileable:
2866 * @define: the definition to check
2867 *
2868 * Check if a definition is nullable.
2869 *
2870 * Returns 1 if yes, 0 if no and -1 in case of error
2871 */
2872static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002873xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2874{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002875 int ret = -1;
2876
Daniel Veillard952379b2003-03-17 15:37:12 +00002877 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002878 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002879 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002880 if ((def->type != XML_RELAXNG_ELEMENT) &&
2881 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002882 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002883 if ((def->type != XML_RELAXNG_ELEMENT) &&
2884 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002885 return (0);
2886 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002887 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002888 ret = xmlRelaxNGIsCompileable(def->content);
2889 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002890 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002891 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002892 ret = 1;
2893 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002894 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002895 /*
2896 * Check if the element content is compileable
2897 */
2898 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2899 ((def->dflags & IS_COMPILABLE) == 0)) {
2900 xmlRelaxNGDefinePtr list;
2901
2902 list = def->content;
2903 while (list != NULL) {
2904 ret = xmlRelaxNGIsCompileable(list);
2905 if (ret != 1)
2906 break;
2907 list = list->next;
2908 }
William M. Brack60929622004-03-27 17:54:18 +00002909 /*
2910 * Because the routine is recursive, we must guard against
2911 * discovering both COMPILABLE and NOT_COMPILABLE
2912 */
2913 if (ret == 0) {
2914 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002915 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002916 }
2917 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002918 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002919#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002920 if (ret == 1) {
2921 xmlGenericError(xmlGenericErrorContext,
2922 "element content for %s is compilable\n",
2923 def->name);
2924 } else if (ret == 0) {
2925 xmlGenericError(xmlGenericErrorContext,
2926 "element content for %s is not compilable\n",
2927 def->name);
2928 } else {
2929 xmlGenericError(xmlGenericErrorContext,
2930 "Problem in RelaxNGIsCompileable for element %s\n",
2931 def->name);
2932 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002933#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002934 }
2935 /*
2936 * All elements return a compileable status unless they
2937 * are generic like anyName
2938 */
2939 if ((def->nameClass != NULL) || (def->name == NULL))
2940 ret = 0;
2941 else
2942 ret = 1;
2943 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002944 case XML_RELAXNG_REF:
2945 case XML_RELAXNG_EXTERNALREF:
2946 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002947 if (def->depth == -20) {
2948 return (1);
2949 } else {
2950 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002951
Daniel Veillard4c004142003-10-07 11:33:24 +00002952 def->depth = -20;
2953 list = def->content;
2954 while (list != NULL) {
2955 ret = xmlRelaxNGIsCompileable(list);
2956 if (ret != 1)
2957 break;
2958 list = list->next;
2959 }
2960 }
2961 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002962 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002963 case XML_RELAXNG_OPTIONAL:
2964 case XML_RELAXNG_ZEROORMORE:
2965 case XML_RELAXNG_ONEORMORE:
2966 case XML_RELAXNG_CHOICE:
2967 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002968 case XML_RELAXNG_DEF:{
2969 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002970
Daniel Veillard4c004142003-10-07 11:33:24 +00002971 list = def->content;
2972 while (list != NULL) {
2973 ret = xmlRelaxNGIsCompileable(list);
2974 if (ret != 1)
2975 break;
2976 list = list->next;
2977 }
2978 break;
2979 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002980 case XML_RELAXNG_EXCEPT:
2981 case XML_RELAXNG_ATTRIBUTE:
2982 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002983 case XML_RELAXNG_DATATYPE:
2984 case XML_RELAXNG_LIST:
2985 case XML_RELAXNG_PARAM:
2986 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002987 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002988 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002989 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002990 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002991 if (ret == 0)
2992 def->dflags |= IS_NOT_COMPILABLE;
2993 if (ret == 1)
2994 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002995#ifdef DEBUG_COMPILE
2996 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002997 xmlGenericError(xmlGenericErrorContext,
2998 "RelaxNGIsCompileable %s : true\n",
2999 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003000 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003001 xmlGenericError(xmlGenericErrorContext,
3002 "RelaxNGIsCompileable %s : false\n",
3003 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003004 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003005 xmlGenericError(xmlGenericErrorContext,
3006 "Problem in RelaxNGIsCompileable %s\n",
3007 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003008 }
3009#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003010 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003011}
3012
3013/**
3014 * xmlRelaxNGCompile:
3015 * ctxt: the RelaxNG parser context
3016 * @define: the definition tree to compile
3017 *
3018 * Compile the set of definitions, it works recursively, till the
3019 * element boundaries, where it tries to compile the content if possible
3020 *
3021 * Returns 0 if success and -1 in case of error
3022 */
3023static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003024xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3025{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003026 int ret = 0;
3027 xmlRelaxNGDefinePtr list;
3028
Daniel Veillard4c004142003-10-07 11:33:24 +00003029 if ((ctxt == NULL) || (def == NULL))
3030 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003031
Daniel Veillard4c004142003-10-07 11:33:24 +00003032 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003033 case XML_RELAXNG_START:
3034 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003035 xmlAutomataPtr oldam = ctxt->am;
3036 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003037
3038 def->depth = -25;
3039
Daniel Veillard4c004142003-10-07 11:33:24 +00003040 list = def->content;
3041 ctxt->am = xmlNewAutomata();
3042 if (ctxt->am == NULL)
3043 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003044
3045 /*
3046 * assume identical strings but not same pointer are different
3047 * atoms, needed for non-determinism detection
3048 * That way if 2 elements with the same name are in a choice
3049 * branch the automata is found non-deterministic and
3050 * we fallback to the normal validation which does the right
3051 * thing of exploring both choices.
3052 */
3053 xmlAutomataSetFlags(ctxt->am, 1);
3054
Daniel Veillard4c004142003-10-07 11:33:24 +00003055 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3056 while (list != NULL) {
3057 xmlRelaxNGCompile(ctxt, list);
3058 list = list->next;
3059 }
3060 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3061 def->contModel = xmlAutomataCompile(ctxt->am);
3062 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003063
Daniel Veillard4c004142003-10-07 11:33:24 +00003064 xmlFreeAutomata(ctxt->am);
3065 ctxt->state = oldstate;
3066 ctxt->am = oldam;
3067 }
3068 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003069 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003070 if ((ctxt->am != NULL) && (def->name != NULL)) {
3071 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3072 ctxt->state, NULL,
3073 def->name, def->ns,
3074 def);
3075 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003076 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003077 xmlAutomataPtr oldam = ctxt->am;
3078 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003079
3080 def->depth = -25;
3081
Daniel Veillard4c004142003-10-07 11:33:24 +00003082 list = def->content;
3083 ctxt->am = xmlNewAutomata();
3084 if (ctxt->am == NULL)
3085 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003086 xmlAutomataSetFlags(ctxt->am, 1);
Daniel Veillard4c004142003-10-07 11:33:24 +00003087 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3088 while (list != NULL) {
3089 xmlRelaxNGCompile(ctxt, list);
3090 list = list->next;
3091 }
3092 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3093 def->contModel = xmlAutomataCompile(ctxt->am);
3094 if (!xmlRegexpIsDeterminist(def->contModel)) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003095#ifdef DEBUG_COMPILE
3096 xmlGenericError(xmlGenericErrorContext,
3097 "Content model not determinist %s\n",
3098 def->name);
3099#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003100 /*
3101 * we can only use the automata if it is determinist
3102 */
3103 xmlRegFreeRegexp(def->contModel);
3104 def->contModel = NULL;
3105 }
3106 xmlFreeAutomata(ctxt->am);
3107 ctxt->state = oldstate;
3108 ctxt->am = oldam;
3109 } else {
3110 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003111
Daniel Veillard4c004142003-10-07 11:33:24 +00003112 /*
3113 * we can't build the content model for this element content
3114 * but it still might be possible to build it for some of its
3115 * children, recurse.
3116 */
3117 ret = xmlRelaxNGTryCompile(ctxt, def);
3118 ctxt->am = oldam;
3119 }
3120 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003121 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003122 ret = xmlRelaxNGCompile(ctxt, def->content);
3123 break;
3124 case XML_RELAXNG_OPTIONAL:{
3125 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003126
Daniel Veillardfd780772009-08-26 18:35:29 +02003127 list = def->content;
3128 while (list != NULL) {
3129 xmlRelaxNGCompile(ctxt, list);
3130 list = list->next;
3131 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003132 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3133 break;
3134 }
3135 case XML_RELAXNG_ZEROORMORE:{
3136 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003137
Daniel Veillard4c004142003-10-07 11:33:24 +00003138 ctxt->state =
3139 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3140 oldstate = ctxt->state;
3141 list = def->content;
3142 while (list != NULL) {
3143 xmlRelaxNGCompile(ctxt, list);
3144 list = list->next;
3145 }
3146 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3147 ctxt->state =
3148 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3149 break;
3150 }
3151 case XML_RELAXNG_ONEORMORE:{
3152 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003153
Daniel Veillard4c004142003-10-07 11:33:24 +00003154 list = def->content;
3155 while (list != NULL) {
3156 xmlRelaxNGCompile(ctxt, list);
3157 list = list->next;
3158 }
3159 oldstate = ctxt->state;
3160 list = def->content;
3161 while (list != NULL) {
3162 xmlRelaxNGCompile(ctxt, list);
3163 list = list->next;
3164 }
3165 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3166 ctxt->state =
3167 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3168 break;
3169 }
3170 case XML_RELAXNG_CHOICE:{
3171 xmlAutomataStatePtr target = NULL;
3172 xmlAutomataStatePtr oldstate = ctxt->state;
3173
3174 list = def->content;
3175 while (list != NULL) {
3176 ctxt->state = oldstate;
3177 ret = xmlRelaxNGCompile(ctxt, list);
3178 if (ret != 0)
3179 break;
3180 if (target == NULL)
3181 target = ctxt->state;
3182 else {
3183 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3184 target);
3185 }
3186 list = list->next;
3187 }
3188 ctxt->state = target;
3189
3190 break;
3191 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003192 case XML_RELAXNG_REF:
3193 case XML_RELAXNG_EXTERNALREF:
3194 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003195 case XML_RELAXNG_GROUP:
3196 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003197 list = def->content;
3198 while (list != NULL) {
3199 ret = xmlRelaxNGCompile(ctxt, list);
3200 if (ret != 0)
3201 break;
3202 list = list->next;
3203 }
3204 break;
3205 case XML_RELAXNG_TEXT:{
3206 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003207
Daniel Veillard4c004142003-10-07 11:33:24 +00003208 ctxt->state =
3209 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3210 oldstate = ctxt->state;
3211 xmlRelaxNGCompile(ctxt, def->content);
3212 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3213 ctxt->state, BAD_CAST "#text",
3214 NULL);
3215 ctxt->state =
3216 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3217 break;
3218 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003219 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003220 ctxt->state =
3221 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3222 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003223 case XML_RELAXNG_EXCEPT:
3224 case XML_RELAXNG_ATTRIBUTE:
3225 case XML_RELAXNG_INTERLEAVE:
3226 case XML_RELAXNG_NOT_ALLOWED:
3227 case XML_RELAXNG_DATATYPE:
3228 case XML_RELAXNG_LIST:
3229 case XML_RELAXNG_PARAM:
3230 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003231 /* This should not happen and generate an internal error */
3232 fprintf(stderr, "RNG internal error trying to compile %s\n",
3233 xmlRelaxNGDefName(def));
3234 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003235 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003236 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003237}
3238
3239/**
3240 * xmlRelaxNGTryCompile:
3241 * ctxt: the RelaxNG parser context
3242 * @define: the definition tree to compile
3243 *
3244 * Try to compile the set of definitions, it works recursively,
3245 * possibly ignoring parts which cannot be compiled.
3246 *
3247 * Returns 0 if success and -1 in case of error
3248 */
3249static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003250xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3251{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003252 int ret = 0;
3253 xmlRelaxNGDefinePtr list;
3254
Daniel Veillard4c004142003-10-07 11:33:24 +00003255 if ((ctxt == NULL) || (def == NULL))
3256 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003257
3258 if ((def->type == XML_RELAXNG_START) ||
3259 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003260 ret = xmlRelaxNGIsCompileable(def);
3261 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3262 ctxt->am = NULL;
3263 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003264#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003265 if (ret == 0) {
3266 if (def->type == XML_RELAXNG_START)
3267 xmlGenericError(xmlGenericErrorContext,
3268 "compiled the start\n");
3269 else
3270 xmlGenericError(xmlGenericErrorContext,
3271 "compiled element %s\n", def->name);
3272 } else {
3273 if (def->type == XML_RELAXNG_START)
3274 xmlGenericError(xmlGenericErrorContext,
3275 "failed to compile the start\n");
3276 else
3277 xmlGenericError(xmlGenericErrorContext,
3278 "failed to compile element %s\n",
3279 def->name);
3280 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003281#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003282 return (ret);
3283 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003284 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003285 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003286 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003287 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3288 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003289 case XML_RELAXNG_TEXT:
3290 case XML_RELAXNG_DATATYPE:
3291 case XML_RELAXNG_LIST:
3292 case XML_RELAXNG_PARAM:
3293 case XML_RELAXNG_VALUE:
3294 case XML_RELAXNG_EMPTY:
3295 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003296 ret = 0;
3297 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003298 case XML_RELAXNG_OPTIONAL:
3299 case XML_RELAXNG_ZEROORMORE:
3300 case XML_RELAXNG_ONEORMORE:
3301 case XML_RELAXNG_CHOICE:
3302 case XML_RELAXNG_GROUP:
3303 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003304 case XML_RELAXNG_START:
3305 case XML_RELAXNG_REF:
3306 case XML_RELAXNG_EXTERNALREF:
3307 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003308 list = def->content;
3309 while (list != NULL) {
3310 ret = xmlRelaxNGTryCompile(ctxt, list);
3311 if (ret != 0)
3312 break;
3313 list = list->next;
3314 }
3315 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003316 case XML_RELAXNG_EXCEPT:
3317 case XML_RELAXNG_ATTRIBUTE:
3318 case XML_RELAXNG_INTERLEAVE:
3319 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003320 ret = 0;
3321 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003322 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003323 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003324}
3325
3326/************************************************************************
3327 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003328 * Parsing functions *
3329 * *
3330 ************************************************************************/
3331
Daniel Veillard4c004142003-10-07 11:33:24 +00003332static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3333 ctxt, xmlNodePtr node);
3334static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3335 ctxt, xmlNodePtr node);
3336static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3337 ctxt, xmlNodePtr nodes,
3338 int group);
3339static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3340 ctxt, xmlNodePtr node);
3341static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3342 xmlNodePtr node);
3343static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3344 xmlNodePtr nodes);
3345static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3346 ctxt, xmlNodePtr node,
3347 xmlRelaxNGDefinePtr
3348 def);
3349static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3350 ctxt, xmlNodePtr nodes);
3351static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3352 xmlRelaxNGDefinePtr define,
3353 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003354
3355
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003356#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003357
3358/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003359 * xmlRelaxNGIsNullable:
3360 * @define: the definition to verify
3361 *
3362 * Check if a definition is nullable.
3363 *
3364 * Returns 1 if yes, 0 if no and -1 in case of error
3365 */
3366static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003367xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3368{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003369 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003370
Daniel Veillardfd573f12003-03-16 17:52:32 +00003371 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003372 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003373
Daniel Veillarde063f482003-03-21 16:53:17 +00003374 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003375 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003376 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003377 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003378 switch (define->type) {
3379 case XML_RELAXNG_EMPTY:
3380 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003381 ret = 1;
3382 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003383 case XML_RELAXNG_NOOP:
3384 case XML_RELAXNG_DEF:
3385 case XML_RELAXNG_REF:
3386 case XML_RELAXNG_EXTERNALREF:
3387 case XML_RELAXNG_PARENTREF:
3388 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003389 ret = xmlRelaxNGIsNullable(define->content);
3390 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003391 case XML_RELAXNG_EXCEPT:
3392 case XML_RELAXNG_NOT_ALLOWED:
3393 case XML_RELAXNG_ELEMENT:
3394 case XML_RELAXNG_DATATYPE:
3395 case XML_RELAXNG_PARAM:
3396 case XML_RELAXNG_VALUE:
3397 case XML_RELAXNG_LIST:
3398 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003399 ret = 0;
3400 break;
3401 case XML_RELAXNG_CHOICE:{
3402 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003403
Daniel Veillard4c004142003-10-07 11:33:24 +00003404 while (list != NULL) {
3405 ret = xmlRelaxNGIsNullable(list);
3406 if (ret != 0)
3407 goto done;
3408 list = list->next;
3409 }
3410 ret = 0;
3411 break;
3412 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003413 case XML_RELAXNG_START:
3414 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003415 case XML_RELAXNG_GROUP:{
3416 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003417
Daniel Veillard4c004142003-10-07 11:33:24 +00003418 while (list != NULL) {
3419 ret = xmlRelaxNGIsNullable(list);
3420 if (ret != 1)
3421 goto done;
3422 list = list->next;
3423 }
3424 return (1);
3425 }
3426 default:
3427 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003428 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003429 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003430 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003431 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003432 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003433 define->dflags |= IS_NULLABLE;
3434 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003435}
3436
3437/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003438 * xmlRelaxNGIsBlank:
3439 * @str: a string
3440 *
3441 * Check if a string is ignorable c.f. 4.2. Whitespace
3442 *
3443 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3444 */
3445static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003446xmlRelaxNGIsBlank(xmlChar * str)
3447{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003448 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003449 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003450 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003451 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003452 return (0);
3453 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003454 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003455 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003456}
3457
Daniel Veillard6eadf632003-01-23 18:29:16 +00003458/**
3459 * xmlRelaxNGGetDataTypeLibrary:
3460 * @ctxt: a Relax-NG parser context
3461 * @node: the current data or value element
3462 *
3463 * Applies algorithm from 4.3. datatypeLibrary attribute
3464 *
3465 * Returns the datatypeLibary value or NULL if not found
3466 */
3467static xmlChar *
3468xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003469 xmlNodePtr node)
3470{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003471 xmlChar *ret, *escape;
3472
Daniel Veillardd44b9362009-09-07 12:15:08 +02003473 if (node == NULL)
3474 return(NULL);
3475
Daniel Veillard6eadf632003-01-23 18:29:16 +00003476 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003477 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3478 if (ret != NULL) {
3479 if (ret[0] == 0) {
3480 xmlFree(ret);
3481 return (NULL);
3482 }
3483 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3484 if (escape == NULL) {
3485 return (ret);
3486 }
3487 xmlFree(ret);
3488 return (escape);
3489 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003490 }
3491 node = node->parent;
3492 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003493 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3494 if (ret != NULL) {
3495 if (ret[0] == 0) {
3496 xmlFree(ret);
3497 return (NULL);
3498 }
3499 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3500 if (escape == NULL) {
3501 return (ret);
3502 }
3503 xmlFree(ret);
3504 return (escape);
3505 }
3506 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003507 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003508 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003509}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003510
3511/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003512 * xmlRelaxNGParseValue:
3513 * @ctxt: a Relax-NG parser context
3514 * @node: the data node.
3515 *
3516 * parse the content of a RelaxNG value node.
3517 *
3518 * Returns the definition pointer or NULL in case of error
3519 */
3520static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003521xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3522{
Daniel Veillardedc91922003-01-26 00:52:04 +00003523 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003524 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003525 xmlChar *type;
3526 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003527 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003528
Daniel Veillardfd573f12003-03-16 17:52:32 +00003529 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003530 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003531 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003532 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003533
3534 type = xmlGetProp(node, BAD_CAST "type");
3535 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003536 xmlRelaxNGNormExtSpace(type);
3537 if (xmlValidateNCName(type, 0)) {
3538 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3539 "value type '%s' is not an NCName\n", type, NULL);
3540 }
3541 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3542 if (library == NULL)
3543 library =
3544 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003545
Daniel Veillard4c004142003-10-07 11:33:24 +00003546 def->name = type;
3547 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003548
Daniel Veillard4c004142003-10-07 11:33:24 +00003549 lib = (xmlRelaxNGTypeLibraryPtr)
3550 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3551 if (lib == NULL) {
3552 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3553 "Use of unregistered type library '%s'\n", library,
3554 NULL);
3555 def->data = NULL;
3556 } else {
3557 def->data = lib;
3558 if (lib->have == NULL) {
3559 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3560 "Internal error with type library '%s': no 'have'\n",
3561 library, NULL);
3562 } else {
3563 success = lib->have(lib->data, def->name);
3564 if (success != 1) {
3565 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3566 "Error type '%s' is not exported by type library '%s'\n",
3567 def->name, library);
3568 }
3569 }
3570 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003571 }
3572 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003573 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003574 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003575 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3576 (node->children->next != NULL)) {
3577 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3578 "Expecting a single text value for <value>content\n",
3579 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003580 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003581 def->value = xmlNodeGetContent(node);
3582 if (def->value == NULL) {
3583 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3584 "Element <value> has no content\n", NULL, NULL);
3585 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3586 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003587
Daniel Veillard4c004142003-10-07 11:33:24 +00003588 success =
3589 lib->check(lib->data, def->name, def->value, &val, node);
3590 if (success != 1) {
3591 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3592 "Value '%s' is not acceptable for type '%s'\n",
3593 def->value, def->name);
3594 } else {
3595 if (val != NULL)
3596 def->attrs = val;
3597 }
3598 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003599 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003600 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003601}
3602
3603/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003604 * xmlRelaxNGParseData:
3605 * @ctxt: a Relax-NG parser context
3606 * @node: the data node.
3607 *
3608 * parse the content of a RelaxNG data node.
3609 *
3610 * Returns the definition pointer or NULL in case of error
3611 */
3612static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003613xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3614{
Daniel Veillard14b56432006-03-09 18:41:40 +00003615 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003616 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003617 xmlRelaxNGTypeLibraryPtr lib;
3618 xmlChar *type;
3619 xmlChar *library;
3620 xmlNodePtr content;
3621 int tmp;
3622
3623 type = xmlGetProp(node, BAD_CAST "type");
3624 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003625 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3626 NULL);
3627 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003628 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003629 xmlRelaxNGNormExtSpace(type);
3630 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003631 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3632 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003633 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003634 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3635 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003636 library =
3637 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003638
Daniel Veillardfd573f12003-03-16 17:52:32 +00003639 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003640 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003641 xmlFree(type);
3642 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003643 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003644 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003645 def->name = type;
3646 def->ns = library;
3647
3648 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003649 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003650 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003651 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3652 "Use of unregistered type library '%s'\n", library,
3653 NULL);
3654 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003655 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003656 def->data = lib;
3657 if (lib->have == NULL) {
3658 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3659 "Internal error with type library '%s': no 'have'\n",
3660 library, NULL);
3661 } else {
3662 tmp = lib->have(lib->data, def->name);
3663 if (tmp != 1) {
3664 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3665 "Error type '%s' is not exported by type library '%s'\n",
3666 def->name, library);
3667 } else
3668 if ((xmlStrEqual
3669 (library,
3670 BAD_CAST
3671 "http://www.w3.org/2001/XMLSchema-datatypes"))
3672 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3673 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3674 ctxt->idref = 1;
3675 }
3676 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003677 }
3678 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003679
3680 /*
3681 * Handle optional params
3682 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003683 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003684 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3685 break;
3686 if (xmlStrEqual(library,
3687 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3688 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3689 "Type library '%s' does not allow type parameters\n",
3690 library, NULL);
3691 content = content->next;
3692 while ((content != NULL) &&
3693 (xmlStrEqual(content->name, BAD_CAST "param")))
3694 content = content->next;
3695 } else {
3696 param = xmlRelaxNGNewDefine(ctxt, node);
3697 if (param != NULL) {
3698 param->type = XML_RELAXNG_PARAM;
3699 param->name = xmlGetProp(content, BAD_CAST "name");
3700 if (param->name == NULL) {
3701 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3702 "param has no name\n", NULL, NULL);
3703 }
3704 param->value = xmlNodeGetContent(content);
3705 if (lastparam == NULL) {
3706 def->attrs = lastparam = param;
3707 } else {
3708 lastparam->next = param;
3709 lastparam = param;
3710 }
3711 if (lib != NULL) {
3712 }
3713 }
3714 content = content->next;
3715 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003716 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003717 /*
3718 * Handle optional except
3719 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003720 if ((content != NULL)
3721 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3722 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003723 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003724
Daniel Veillard4c004142003-10-07 11:33:24 +00003725 except = xmlRelaxNGNewDefine(ctxt, node);
3726 if (except == NULL) {
3727 return (def);
3728 }
3729 except->type = XML_RELAXNG_EXCEPT;
3730 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003731 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003732 if (child == NULL) {
3733 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3734 "except has no content\n", NULL, NULL);
3735 }
3736 while (child != NULL) {
3737 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3738 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003739 if (last == NULL) {
3740 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003741 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003742 last->next = tmp2;
3743 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003744 }
3745 }
3746 child = child->next;
3747 }
3748 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003749 }
3750 /*
3751 * Check there is no unhandled data
3752 */
3753 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003754 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3755 "Element data has unexpected content %s\n",
3756 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003757 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003758
Daniel Veillard4c004142003-10-07 11:33:24 +00003759 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003760}
3761
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003762static const xmlChar *invalidName = BAD_CAST "\1";
3763
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003764/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003765 * xmlRelaxNGCompareNameClasses:
3766 * @defs1: the first element/attribute defs
3767 * @defs2: the second element/attribute defs
3768 * @name: the restriction on the name
3769 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003770 *
3771 * Compare the 2 lists of element definitions. The comparison is
3772 * that if both lists do not accept the same QNames, it returns 1
3773 * If the 2 lists can accept the same QName the comparison returns 0
3774 *
3775 * Returns 1 disttinct, 0 if equal
3776 */
3777static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003778xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003779 xmlRelaxNGDefinePtr def2)
3780{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003781 int ret = 1;
3782 xmlNode node;
3783 xmlNs ns;
3784 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003785
Daniel Veillard42f12e92003-03-07 18:32:59 +00003786 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3787
Daniel Veillardb30ca312005-09-04 13:50:03 +00003788 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3789
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003790 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003791 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3792 if (def2->type == XML_RELAXNG_TEXT)
3793 return (1);
3794 if (def1->name != NULL) {
3795 node.name = def1->name;
3796 } else {
3797 node.name = invalidName;
3798 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003799 if (def1->ns != NULL) {
3800 if (def1->ns[0] == 0) {
3801 node.ns = NULL;
3802 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003803 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003804 ns.href = def1->ns;
3805 }
3806 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003807 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003808 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003809 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003810 if (def1->nameClass != NULL) {
3811 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3812 } else {
3813 ret = 0;
3814 }
3815 } else {
3816 ret = 1;
3817 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003818 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003819 if (def2->type == XML_RELAXNG_TEXT)
3820 return (0);
3821 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003822 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003823 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003824 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003825 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003826 }
3827 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003828 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003829 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003830 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3831 if (def2->name != NULL) {
3832 node.name = def2->name;
3833 } else {
3834 node.name = invalidName;
3835 }
3836 node.ns = &ns;
3837 if (def2->ns != NULL) {
3838 if (def2->ns[0] == 0) {
3839 node.ns = NULL;
3840 } else {
3841 ns.href = def2->ns;
3842 }
3843 } else {
3844 ns.href = invalidName;
3845 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003846 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003847 if (def2->nameClass != NULL) {
3848 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3849 } else {
3850 ret = 0;
3851 }
3852 } else {
3853 ret = 1;
3854 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003855 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003856 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003857 }
3858
Daniel Veillard4c004142003-10-07 11:33:24 +00003859 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003860}
3861
3862/**
3863 * xmlRelaxNGCompareElemDefLists:
3864 * @ctxt: a Relax-NG parser context
3865 * @defs1: the first list of element/attribute defs
3866 * @defs2: the second list of element/attribute defs
3867 *
3868 * Compare the 2 lists of element or attribute definitions. The comparison
3869 * is that if both lists do not accept the same QNames, it returns 1
3870 * If the 2 lists can accept the same QName the comparison returns 0
3871 *
3872 * Returns 1 disttinct, 0 if equal
3873 */
3874static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003875xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3876 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3877 xmlRelaxNGDefinePtr * def2)
3878{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003879 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003880
Daniel Veillard154877e2003-01-30 12:17:05 +00003881 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003882 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003883 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003884 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003885 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003886 while ((*def2) != NULL) {
3887 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3888 return (0);
3889 def2++;
3890 }
3891 def2 = basedef2;
3892 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003893 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003894 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003895}
3896
3897/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003898 * xmlRelaxNGGenerateAttributes:
3899 * @ctxt: a Relax-NG parser context
3900 * @def: the definition definition
3901 *
3902 * Check if the definition can only generate attributes
3903 *
3904 * Returns 1 if yes, 0 if no and -1 in case of error.
3905 */
3906static int
3907xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003908 xmlRelaxNGDefinePtr def)
3909{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003910 xmlRelaxNGDefinePtr parent, cur, tmp;
3911
3912 /*
3913 * Don't run that check in case of error. Infinite recursion
3914 * becomes possible.
3915 */
3916 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003917 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003918
3919 parent = NULL;
3920 cur = def;
3921 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003922 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3923 (cur->type == XML_RELAXNG_TEXT) ||
3924 (cur->type == XML_RELAXNG_DATATYPE) ||
3925 (cur->type == XML_RELAXNG_PARAM) ||
3926 (cur->type == XML_RELAXNG_LIST) ||
3927 (cur->type == XML_RELAXNG_VALUE) ||
3928 (cur->type == XML_RELAXNG_EMPTY))
3929 return (0);
3930 if ((cur->type == XML_RELAXNG_CHOICE) ||
3931 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3932 (cur->type == XML_RELAXNG_GROUP) ||
3933 (cur->type == XML_RELAXNG_ONEORMORE) ||
3934 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3935 (cur->type == XML_RELAXNG_OPTIONAL) ||
3936 (cur->type == XML_RELAXNG_PARENTREF) ||
3937 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3938 (cur->type == XML_RELAXNG_REF) ||
3939 (cur->type == XML_RELAXNG_DEF)) {
3940 if (cur->content != NULL) {
3941 parent = cur;
3942 cur = cur->content;
3943 tmp = cur;
3944 while (tmp != NULL) {
3945 tmp->parent = parent;
3946 tmp = tmp->next;
3947 }
3948 continue;
3949 }
3950 }
3951 if (cur == def)
3952 break;
3953 if (cur->next != NULL) {
3954 cur = cur->next;
3955 continue;
3956 }
3957 do {
3958 cur = cur->parent;
3959 if (cur == NULL)
3960 break;
3961 if (cur == def)
3962 return (1);
3963 if (cur->next != NULL) {
3964 cur = cur->next;
3965 break;
3966 }
3967 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003968 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003969 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003970}
Daniel Veillard4c004142003-10-07 11:33:24 +00003971
Daniel Veillardce192eb2003-04-16 15:58:05 +00003972/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003973 * xmlRelaxNGGetElements:
3974 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003975 * @def: the definition definition
3976 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003977 *
3978 * Compute the list of top elements a definition can generate
3979 *
3980 * Returns a list of elements or NULL if none was found.
3981 */
3982static xmlRelaxNGDefinePtr *
3983xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003984 xmlRelaxNGDefinePtr def, int eora)
3985{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003986 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003987 int len = 0;
3988 int max = 0;
3989
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003990 /*
3991 * Don't run that check in case of error. Infinite recursion
3992 * becomes possible.
3993 */
3994 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003995 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003996
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003997 parent = NULL;
3998 cur = def;
3999 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004000 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4001 (cur->type == XML_RELAXNG_TEXT))) ||
4002 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
4003 if (ret == NULL) {
4004 max = 10;
4005 ret = (xmlRelaxNGDefinePtr *)
4006 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4007 if (ret == NULL) {
4008 xmlRngPErrMemory(ctxt, "getting element list\n");
4009 return (NULL);
4010 }
4011 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00004012 xmlRelaxNGDefinePtr *temp;
4013
Daniel Veillard4c004142003-10-07 11:33:24 +00004014 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00004015 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00004016 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00004017 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004018 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00004019 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00004020 return (NULL);
4021 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00004022 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00004023 }
4024 ret[len++] = cur;
4025 ret[len] = NULL;
4026 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4027 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4028 (cur->type == XML_RELAXNG_GROUP) ||
4029 (cur->type == XML_RELAXNG_ONEORMORE) ||
4030 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4031 (cur->type == XML_RELAXNG_OPTIONAL) ||
4032 (cur->type == XML_RELAXNG_PARENTREF) ||
4033 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004034 (cur->type == XML_RELAXNG_DEF) ||
4035 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004036 /*
4037 * Don't go within elements or attributes or string values.
4038 * Just gather the element top list
4039 */
4040 if (cur->content != NULL) {
4041 parent = cur;
4042 cur = cur->content;
4043 tmp = cur;
4044 while (tmp != NULL) {
4045 tmp->parent = parent;
4046 tmp = tmp->next;
4047 }
4048 continue;
4049 }
4050 }
4051 if (cur == def)
4052 break;
4053 if (cur->next != NULL) {
4054 cur = cur->next;
4055 continue;
4056 }
4057 do {
4058 cur = cur->parent;
4059 if (cur == NULL)
4060 break;
4061 if (cur == def)
4062 return (ret);
4063 if (cur->next != NULL) {
4064 cur = cur->next;
4065 break;
4066 }
4067 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004068 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004069 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004070}
Daniel Veillard4c004142003-10-07 11:33:24 +00004071
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004072/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004073 * xmlRelaxNGCheckChoiceDeterminism:
4074 * @ctxt: a Relax-NG parser context
4075 * @def: the choice definition
4076 *
4077 * Also used to find indeterministic pattern in choice
4078 */
4079static void
4080xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004081 xmlRelaxNGDefinePtr def)
4082{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004083 xmlRelaxNGDefinePtr **list;
4084 xmlRelaxNGDefinePtr cur;
4085 int nbchild = 0, i, j, ret;
4086 int is_nullable = 0;
4087 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004088 xmlHashTablePtr triage = NULL;
4089 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004090
Daniel Veillard4c004142003-10-07 11:33:24 +00004091 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4092 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004093
Daniel Veillarde063f482003-03-21 16:53:17 +00004094 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004095 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004096
Daniel Veillardfd573f12003-03-16 17:52:32 +00004097 /*
4098 * Don't run that check in case of error. Infinite recursion
4099 * becomes possible.
4100 */
4101 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004102 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004103
4104 is_nullable = xmlRelaxNGIsNullable(def);
4105
4106 cur = def->content;
4107 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004108 nbchild++;
4109 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004110 }
4111
4112 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004113 sizeof(xmlRelaxNGDefinePtr
4114 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004115 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004116 xmlRngPErrMemory(ctxt, "building choice\n");
4117 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004118 }
4119 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004120 /*
4121 * a bit strong but safe
4122 */
4123 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004124 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004125 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004126 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004127 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004128 cur = def->content;
4129 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004130 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4131 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4132 is_triable = 0;
4133 } else if (is_triable == 1) {
4134 xmlRelaxNGDefinePtr *tmp;
4135 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004136
Daniel Veillard4c004142003-10-07 11:33:24 +00004137 tmp = list[i];
4138 while ((*tmp != NULL) && (is_triable == 1)) {
4139 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4140 res = xmlHashAddEntry2(triage,
4141 BAD_CAST "#text", NULL,
4142 (void *) cur);
4143 if (res != 0)
4144 is_triable = -1;
4145 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4146 ((*tmp)->name != NULL)) {
4147 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4148 res = xmlHashAddEntry2(triage,
4149 (*tmp)->name, NULL,
4150 (void *) cur);
4151 else
4152 res = xmlHashAddEntry2(triage,
4153 (*tmp)->name, (*tmp)->ns,
4154 (void *) cur);
4155 if (res != 0)
4156 is_triable = -1;
4157 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4158 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4159 res = xmlHashAddEntry2(triage,
4160 BAD_CAST "#any", NULL,
4161 (void *) cur);
4162 else
4163 res = xmlHashAddEntry2(triage,
4164 BAD_CAST "#any", (*tmp)->ns,
4165 (void *) cur);
4166 if (res != 0)
4167 is_triable = -1;
4168 } else {
4169 is_triable = -1;
4170 }
4171 tmp++;
4172 }
4173 }
4174 i++;
4175 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004176 }
4177
Daniel Veillard4c004142003-10-07 11:33:24 +00004178 for (i = 0; i < nbchild; i++) {
4179 if (list[i] == NULL)
4180 continue;
4181 for (j = 0; j < i; j++) {
4182 if (list[j] == NULL)
4183 continue;
4184 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4185 if (ret == 0) {
4186 is_indeterminist = 1;
4187 }
4188 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004189 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004190 for (i = 0; i < nbchild; i++) {
4191 if (list[i] != NULL)
4192 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004193 }
4194
4195 xmlFree(list);
4196 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004197 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004198 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004199 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004200 def->dflags |= IS_TRIABLE;
4201 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004202 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004203 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004204 }
4205 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004206}
4207
4208/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004209 * xmlRelaxNGCheckGroupAttrs:
4210 * @ctxt: a Relax-NG parser context
4211 * @def: the group definition
4212 *
4213 * Detects violations of rule 7.3
4214 */
4215static void
4216xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004217 xmlRelaxNGDefinePtr def)
4218{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004219 xmlRelaxNGDefinePtr **list;
4220 xmlRelaxNGDefinePtr cur;
4221 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004222
4223 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004224 ((def->type != XML_RELAXNG_GROUP) &&
4225 (def->type != XML_RELAXNG_ELEMENT)))
4226 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004227
Daniel Veillarde063f482003-03-21 16:53:17 +00004228 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004229 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004230
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004231 /*
4232 * Don't run that check in case of error. Infinite recursion
4233 * becomes possible.
4234 */
4235 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004236 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004237
Daniel Veillardfd573f12003-03-16 17:52:32 +00004238 cur = def->attrs;
4239 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004240 nbchild++;
4241 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004242 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004243 cur = def->content;
4244 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004245 nbchild++;
4246 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004247 }
4248
4249 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004250 sizeof(xmlRelaxNGDefinePtr
4251 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004252 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004253 xmlRngPErrMemory(ctxt, "building group\n");
4254 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004255 }
4256 i = 0;
4257 cur = def->attrs;
4258 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004259 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4260 i++;
4261 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004262 }
4263 cur = def->content;
4264 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004265 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4266 i++;
4267 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004268 }
4269
Daniel Veillard4c004142003-10-07 11:33:24 +00004270 for (i = 0; i < nbchild; i++) {
4271 if (list[i] == NULL)
4272 continue;
4273 for (j = 0; j < i; j++) {
4274 if (list[j] == NULL)
4275 continue;
4276 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4277 if (ret == 0) {
4278 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4279 "Attributes conflicts in group\n", NULL, NULL);
4280 }
4281 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004282 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004283 for (i = 0; i < nbchild; i++) {
4284 if (list[i] != NULL)
4285 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004286 }
4287
4288 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004289 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004290}
4291
4292/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004293 * xmlRelaxNGComputeInterleaves:
4294 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004295 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004296 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004297 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004298 * A lot of work for preprocessing interleave definitions
4299 * is potentially needed to get a decent execution speed at runtime
4300 * - trying to get a total order on the element nodes generated
4301 * by the interleaves, order the list of interleave definitions
4302 * following that order.
4303 * - if <text/> is used to handle mixed content, it is better to
4304 * flag this in the define and simplify the runtime checking
4305 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004306 */
4307static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004308xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004309 xmlRelaxNGParserCtxtPtr ctxt,
4310 xmlChar * name ATTRIBUTE_UNUSED)
4311{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004312 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004313
Daniel Veillardfd573f12003-03-16 17:52:32 +00004314 xmlRelaxNGPartitionPtr partitions = NULL;
4315 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4316 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004317 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004318 int nbgroups = 0;
4319 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004320 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004321 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004322
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004323 /*
4324 * Don't run that check in case of error. Infinite recursion
4325 * becomes possible.
4326 */
4327 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004328 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004329
Daniel Veillardfd573f12003-03-16 17:52:32 +00004330#ifdef DEBUG_INTERLEAVE
4331 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004332 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004333#endif
4334 cur = def->content;
4335 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004336 nbchild++;
4337 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004338 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004339
Daniel Veillardfd573f12003-03-16 17:52:32 +00004340#ifdef DEBUG_INTERLEAVE
4341 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4342#endif
4343 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004344 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004345 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004346 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004347 cur = def->content;
4348 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004349 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4350 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4351 if (groups[nbgroups] == NULL)
4352 goto error;
4353 if (cur->type == XML_RELAXNG_TEXT)
4354 is_mixed++;
4355 groups[nbgroups]->rule = cur;
4356 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4357 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4358 nbgroups++;
4359 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004360 }
4361#ifdef DEBUG_INTERLEAVE
4362 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4363#endif
4364
4365 /*
4366 * Let's check that all rules makes a partitions according to 7.4
4367 */
4368 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004369 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004370 if (partitions == NULL)
4371 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004372 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004373 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004374 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004375 for (i = 0; i < nbgroups; i++) {
4376 group = groups[i];
4377 for (j = i + 1; j < nbgroups; j++) {
4378 if (groups[j] == NULL)
4379 continue;
4380
4381 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4382 groups[j]->defs);
4383 if (ret == 0) {
4384 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4385 "Element or text conflicts in interleave\n",
4386 NULL, NULL);
4387 }
4388 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4389 groups[j]->attrs);
4390 if (ret == 0) {
4391 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4392 "Attributes conflicts in interleave\n", NULL,
4393 NULL);
4394 }
4395 }
4396 tmp = group->defs;
4397 if ((tmp != NULL) && (*tmp != NULL)) {
4398 while (*tmp != NULL) {
4399 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4400 res = xmlHashAddEntry2(partitions->triage,
4401 BAD_CAST "#text", NULL,
4402 (void *) (long) (i + 1));
4403 if (res != 0)
4404 is_determinist = -1;
4405 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4406 ((*tmp)->name != NULL)) {
4407 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4408 res = xmlHashAddEntry2(partitions->triage,
4409 (*tmp)->name, NULL,
4410 (void *) (long) (i + 1));
4411 else
4412 res = xmlHashAddEntry2(partitions->triage,
4413 (*tmp)->name, (*tmp)->ns,
4414 (void *) (long) (i + 1));
4415 if (res != 0)
4416 is_determinist = -1;
4417 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4418 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4419 res = xmlHashAddEntry2(partitions->triage,
4420 BAD_CAST "#any", NULL,
4421 (void *) (long) (i + 1));
4422 else
4423 res = xmlHashAddEntry2(partitions->triage,
4424 BAD_CAST "#any", (*tmp)->ns,
4425 (void *) (long) (i + 1));
4426 if ((*tmp)->nameClass != NULL)
4427 is_determinist = 2;
4428 if (res != 0)
4429 is_determinist = -1;
4430 } else {
4431 is_determinist = -1;
4432 }
4433 tmp++;
4434 }
4435 } else {
4436 is_determinist = 0;
4437 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004438 }
4439 partitions->groups = groups;
4440
4441 /*
4442 * and save the partition list back in the def
4443 */
4444 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004445 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004446 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004447 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004448 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004449 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004450 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004451 return;
4452
Daniel Veillard4c004142003-10-07 11:33:24 +00004453 error:
4454 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004455 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004456 for (i = 0; i < nbgroups; i++)
4457 if (groups[i] != NULL) {
4458 if (groups[i]->defs != NULL)
4459 xmlFree(groups[i]->defs);
4460 xmlFree(groups[i]);
4461 }
4462 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004463 }
4464 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004465}
4466
4467/**
4468 * xmlRelaxNGParseInterleave:
4469 * @ctxt: a Relax-NG parser context
4470 * @node: the data node.
4471 *
4472 * parse the content of a RelaxNG interleave node.
4473 *
4474 * Returns the definition pointer or NULL in case of error
4475 */
4476static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004477xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4478{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004479 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004480 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004481 xmlNodePtr child;
4482
Daniel Veillardfd573f12003-03-16 17:52:32 +00004483 def = xmlRelaxNGNewDefine(ctxt, node);
4484 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004485 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004486 }
4487 def->type = XML_RELAXNG_INTERLEAVE;
4488
4489 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004490 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004491 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004492 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004493 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004494 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004495
Daniel Veillard4c004142003-10-07 11:33:24 +00004496 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4497 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4498 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4499 "Failed to add %s to hash table\n",
4500 (const xmlChar *) name, NULL);
4501 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004502 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004503 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004504 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004505 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4506 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004507 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004508 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004509 if (IS_RELAXNG(child, "element")) {
4510 cur = xmlRelaxNGParseElement(ctxt, child);
4511 } else {
4512 cur = xmlRelaxNGParsePattern(ctxt, child);
4513 }
4514 if (cur != NULL) {
4515 cur->parent = def;
4516 if (last == NULL) {
4517 def->content = last = cur;
4518 } else {
4519 last->next = cur;
4520 last = cur;
4521 }
4522 }
4523 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004524 }
4525
Daniel Veillard4c004142003-10-07 11:33:24 +00004526 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004527}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004528
4529/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004530 * xmlRelaxNGParseInclude:
4531 * @ctxt: a Relax-NG parser context
4532 * @node: the include node
4533 *
4534 * Integrate the content of an include node in the current grammar
4535 *
4536 * Returns 0 in case of success or -1 in case of error
4537 */
4538static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004539xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4540{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004541 xmlRelaxNGIncludePtr incl;
4542 xmlNodePtr root;
4543 int ret = 0, tmp;
4544
Daniel Veillard807daf82004-02-22 22:13:27 +00004545 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004546 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004547 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4548 "Include node has no data\n", NULL, NULL);
4549 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004550 }
4551 root = xmlDocGetRootElement(incl->doc);
4552 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004553 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4554 NULL, NULL);
4555 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004556 }
4557 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004558 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4559 "Include document root is not a grammar\n", NULL, NULL);
4560 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004561 }
4562
4563 /*
4564 * Merge the definition from both the include and the internal list
4565 */
4566 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004567 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4568 if (tmp != 0)
4569 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004570 }
4571 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004572 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4573 if (tmp != 0)
4574 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004575 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004576 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004577}
4578
4579/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004580 * xmlRelaxNGParseDefine:
4581 * @ctxt: a Relax-NG parser context
4582 * @node: the define node
4583 *
4584 * parse the content of a RelaxNG define element node.
4585 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004586 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004587 */
4588static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004589xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4590{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004591 xmlChar *name;
4592 int ret = 0, tmp;
4593 xmlRelaxNGDefinePtr def;
4594 const xmlChar *olddefine;
4595
4596 name = xmlGetProp(node, BAD_CAST "name");
4597 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004598 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4599 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004600 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004601 xmlRelaxNGNormExtSpace(name);
4602 if (xmlValidateNCName(name, 0)) {
4603 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4604 "define name '%s' is not an NCName\n", name, NULL);
4605 }
4606 def = xmlRelaxNGNewDefine(ctxt, node);
4607 if (def == NULL) {
4608 xmlFree(name);
4609 return (-1);
4610 }
4611 def->type = XML_RELAXNG_DEF;
4612 def->name = name;
4613 if (node->children == NULL) {
4614 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4615 "define has no children\n", NULL, NULL);
4616 } else {
4617 olddefine = ctxt->define;
4618 ctxt->define = name;
4619 def->content =
4620 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4621 ctxt->define = olddefine;
4622 }
4623 if (ctxt->grammar->defs == NULL)
4624 ctxt->grammar->defs = xmlHashCreate(10);
4625 if (ctxt->grammar->defs == NULL) {
4626 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4627 "Could not create definition hash\n", NULL, NULL);
4628 ret = -1;
4629 } else {
4630 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4631 if (tmp < 0) {
4632 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004633
Daniel Veillard4c004142003-10-07 11:33:24 +00004634 prev = xmlHashLookup(ctxt->grammar->defs, name);
4635 if (prev == NULL) {
4636 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4637 "Internal error on define aggregation of %s\n",
4638 name, NULL);
4639 ret = -1;
4640 } else {
4641 while (prev->nextHash != NULL)
4642 prev = prev->nextHash;
4643 prev->nextHash = def;
4644 }
4645 }
4646 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004647 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004648 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004649}
4650
4651/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004652 * xmlRelaxNGParseImportRef:
4653 * @payload: the parser context
4654 * @data: the current grammar
4655 * @name: the reference name
4656 *
4657 * Import import one references into the current grammar
4658 */
4659static void
4660xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4661 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4662 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4663 int tmp;
4664
4665 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4666 if (tmp < 0) {
4667 xmlRelaxNGDefinePtr prev;
4668
4669 prev = (xmlRelaxNGDefinePtr)
4670 xmlHashLookup(ctxt->grammar->refs, def->name);
4671 if (prev == NULL) {
4672 if (def->name != NULL) {
4673 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4674 "Error refs definitions '%s'\n",
4675 def->name, NULL);
4676 } else {
4677 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4678 "Error refs definitions\n",
4679 NULL, NULL);
4680 }
4681 } else {
4682 def->nextHash = prev->nextHash;
4683 prev->nextHash = def;
4684 }
4685 }
4686}
4687
4688/**
4689 * xmlRelaxNGParseImportRefs:
4690 * @ctxt: the parser context
4691 * @grammar: the sub grammar
4692 *
4693 * Import references from the subgrammar into the current grammar
4694 *
4695 * Returns 0 in case of success, -1 in case of failure
4696 */
4697static int
4698xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4699 xmlRelaxNGGrammarPtr grammar) {
4700 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4701 return(-1);
4702 if (grammar->refs == NULL)
4703 return(0);
4704 if (ctxt->grammar->refs == NULL)
4705 ctxt->grammar->refs = xmlHashCreate(10);
4706 if (ctxt->grammar->refs == NULL) {
4707 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4708 "Could not create references hash\n", NULL, NULL);
4709 return(-1);
4710 }
4711 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004712 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004713}
4714
4715/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004716 * xmlRelaxNGProcessExternalRef:
4717 * @ctxt: the parser context
4718 * @node: the externlRef node
4719 *
4720 * Process and compile an externlRef node
4721 *
4722 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4723 */
4724static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004725xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4726{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004727 xmlRelaxNGDocumentPtr docu;
4728 xmlNodePtr root, tmp;
4729 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004730 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004731 xmlRelaxNGDefinePtr def;
4732
Daniel Veillard807daf82004-02-22 22:13:27 +00004733 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004734 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004735 def = xmlRelaxNGNewDefine(ctxt, node);
4736 if (def == NULL)
4737 return (NULL);
4738 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004739
Daniel Veillard4c004142003-10-07 11:33:24 +00004740 if (docu->content == NULL) {
4741 /*
4742 * Then do the parsing for good
4743 */
4744 root = xmlDocGetRootElement(docu->doc);
4745 if (root == NULL) {
4746 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4747 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4748 NULL);
4749 return (NULL);
4750 }
4751 /*
4752 * ns transmission rules
4753 */
4754 ns = xmlGetProp(root, BAD_CAST "ns");
4755 if (ns == NULL) {
4756 tmp = node;
4757 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4758 ns = xmlGetProp(tmp, BAD_CAST "ns");
4759 if (ns != NULL) {
4760 break;
4761 }
4762 tmp = tmp->parent;
4763 }
4764 if (ns != NULL) {
4765 xmlSetProp(root, BAD_CAST "ns", ns);
4766 newNs = 1;
4767 xmlFree(ns);
4768 }
4769 } else {
4770 xmlFree(ns);
4771 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004772
Daniel Veillard4c004142003-10-07 11:33:24 +00004773 /*
4774 * Parsing to get a precompiled schemas.
4775 */
4776 oldflags = ctxt->flags;
4777 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4778 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4779 ctxt->flags = oldflags;
4780 if ((docu->schema != NULL) &&
4781 (docu->schema->topgrammar != NULL)) {
4782 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004783 if (docu->schema->topgrammar->refs)
4784 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004785 }
4786
4787 /*
4788 * the externalRef may be reused in a different ns context
4789 */
4790 if (newNs == 1) {
4791 xmlUnsetProp(root, BAD_CAST "ns");
4792 }
4793 }
4794 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004795 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004796 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004797 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004798 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004799}
4800
4801/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004802 * xmlRelaxNGParsePattern:
4803 * @ctxt: a Relax-NG parser context
4804 * @node: the pattern node.
4805 *
4806 * parse the content of a RelaxNG pattern node.
4807 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004808 * Returns the definition pointer or NULL in case of error or if no
4809 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004810 */
4811static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004812xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4813{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004814 xmlRelaxNGDefinePtr def = NULL;
4815
Daniel Veillardd2298792003-02-14 16:54:11 +00004816 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004817 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004818 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004819 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004820 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004821 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004822 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004823 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004824 def = xmlRelaxNGNewDefine(ctxt, node);
4825 if (def == NULL)
4826 return (NULL);
4827 def->type = XML_RELAXNG_EMPTY;
4828 if (node->children != NULL) {
4829 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4830 "empty: had a child node\n", NULL, NULL);
4831 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004832 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004833 def = xmlRelaxNGNewDefine(ctxt, node);
4834 if (def == NULL)
4835 return (NULL);
4836 def->type = XML_RELAXNG_TEXT;
4837 if (node->children != NULL) {
4838 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4839 "text: had a child node\n", NULL, NULL);
4840 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004841 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004842 def = xmlRelaxNGNewDefine(ctxt, node);
4843 if (def == NULL)
4844 return (NULL);
4845 def->type = XML_RELAXNG_ZEROORMORE;
4846 if (node->children == NULL) {
4847 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4848 "Element %s is empty\n", node->name, NULL);
4849 } else {
4850 def->content =
4851 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4852 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004853 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004854 def = xmlRelaxNGNewDefine(ctxt, node);
4855 if (def == NULL)
4856 return (NULL);
4857 def->type = XML_RELAXNG_ONEORMORE;
4858 if (node->children == NULL) {
4859 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4860 "Element %s is empty\n", node->name, NULL);
4861 } else {
4862 def->content =
4863 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4864 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004865 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004866 def = xmlRelaxNGNewDefine(ctxt, node);
4867 if (def == NULL)
4868 return (NULL);
4869 def->type = XML_RELAXNG_OPTIONAL;
4870 if (node->children == NULL) {
4871 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4872 "Element %s is empty\n", node->name, NULL);
4873 } else {
4874 def->content =
4875 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4876 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004877 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004878 def = xmlRelaxNGNewDefine(ctxt, node);
4879 if (def == NULL)
4880 return (NULL);
4881 def->type = XML_RELAXNG_CHOICE;
4882 if (node->children == NULL) {
4883 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4884 "Element %s is empty\n", node->name, NULL);
4885 } else {
4886 def->content =
4887 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4888 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004889 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004890 def = xmlRelaxNGNewDefine(ctxt, node);
4891 if (def == NULL)
4892 return (NULL);
4893 def->type = XML_RELAXNG_GROUP;
4894 if (node->children == NULL) {
4895 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4896 "Element %s is empty\n", node->name, NULL);
4897 } else {
4898 def->content =
4899 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4900 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004901 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004902 def = xmlRelaxNGNewDefine(ctxt, node);
4903 if (def == NULL)
4904 return (NULL);
4905 def->type = XML_RELAXNG_REF;
4906 def->name = xmlGetProp(node, BAD_CAST "name");
4907 if (def->name == NULL) {
4908 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4909 NULL, NULL);
4910 } else {
4911 xmlRelaxNGNormExtSpace(def->name);
4912 if (xmlValidateNCName(def->name, 0)) {
4913 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4914 "ref name '%s' is not an NCName\n", def->name,
4915 NULL);
4916 }
4917 }
4918 if (node->children != NULL) {
4919 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4920 NULL, NULL);
4921 }
4922 if (ctxt->grammar->refs == NULL)
4923 ctxt->grammar->refs = xmlHashCreate(10);
4924 if (ctxt->grammar->refs == NULL) {
4925 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4926 "Could not create references hash\n", NULL, NULL);
4927 def = NULL;
4928 } else {
4929 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004930
Daniel Veillard4c004142003-10-07 11:33:24 +00004931 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4932 if (tmp < 0) {
4933 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004934
Daniel Veillard4c004142003-10-07 11:33:24 +00004935 prev = (xmlRelaxNGDefinePtr)
4936 xmlHashLookup(ctxt->grammar->refs, def->name);
4937 if (prev == NULL) {
4938 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004939 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4940 "Error refs definitions '%s'\n",
4941 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004942 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004943 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4944 "Error refs definitions\n",
4945 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004946 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004947 def = NULL;
4948 } else {
4949 def->nextHash = prev->nextHash;
4950 prev->nextHash = def;
4951 }
4952 }
4953 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004954 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004955 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004956 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004957 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004958 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004959 def = xmlRelaxNGNewDefine(ctxt, node);
4960 if (def == NULL)
4961 return (NULL);
4962 def->type = XML_RELAXNG_LIST;
4963 if (node->children == NULL) {
4964 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4965 "Element %s is empty\n", node->name, NULL);
4966 } else {
4967 def->content =
4968 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4969 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004970 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004971 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004972 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004973 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004974 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004975 def = xmlRelaxNGNewDefine(ctxt, node);
4976 if (def == NULL)
4977 return (NULL);
4978 def->type = XML_RELAXNG_NOT_ALLOWED;
4979 if (node->children != NULL) {
4980 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4981 "xmlRelaxNGParse: notAllowed element is not empty\n",
4982 NULL, NULL);
4983 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004984 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004985 xmlRelaxNGGrammarPtr grammar, old;
4986 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004987
Daniel Veillardc482e262003-02-26 14:48:48 +00004988#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004989 xmlGenericError(xmlGenericErrorContext,
4990 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004991#endif
4992
Daniel Veillard4c004142003-10-07 11:33:24 +00004993 oldparent = ctxt->parentgrammar;
4994 old = ctxt->grammar;
4995 ctxt->parentgrammar = old;
4996 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4997 if (old != NULL) {
4998 ctxt->grammar = old;
4999 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00005000#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00005001 if (grammar != NULL) {
5002 grammar->next = old->next;
5003 old->next = grammar;
5004 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005005#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00005006 }
5007 if (grammar != NULL)
5008 def = grammar->start;
5009 else
5010 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00005011 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005012 if (ctxt->parentgrammar == NULL) {
5013 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5014 "Use of parentRef without a parent grammar\n", NULL,
5015 NULL);
5016 return (NULL);
5017 }
5018 def = xmlRelaxNGNewDefine(ctxt, node);
5019 if (def == NULL)
5020 return (NULL);
5021 def->type = XML_RELAXNG_PARENTREF;
5022 def->name = xmlGetProp(node, BAD_CAST "name");
5023 if (def->name == NULL) {
5024 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5025 "parentRef has no name\n", NULL, NULL);
5026 } else {
5027 xmlRelaxNGNormExtSpace(def->name);
5028 if (xmlValidateNCName(def->name, 0)) {
5029 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5030 "parentRef name '%s' is not an NCName\n",
5031 def->name, NULL);
5032 }
5033 }
5034 if (node->children != NULL) {
5035 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5036 "parentRef is not empty\n", NULL, NULL);
5037 }
5038 if (ctxt->parentgrammar->refs == NULL)
5039 ctxt->parentgrammar->refs = xmlHashCreate(10);
5040 if (ctxt->parentgrammar->refs == NULL) {
5041 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5042 "Could not create references hash\n", NULL, NULL);
5043 def = NULL;
5044 } else if (def->name != NULL) {
5045 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005046
Daniel Veillard4c004142003-10-07 11:33:24 +00005047 tmp =
5048 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5049 if (tmp < 0) {
5050 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005051
Daniel Veillard4c004142003-10-07 11:33:24 +00005052 prev = (xmlRelaxNGDefinePtr)
5053 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5054 if (prev == NULL) {
5055 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5056 "Internal error parentRef definitions '%s'\n",
5057 def->name, NULL);
5058 def = NULL;
5059 } else {
5060 def->nextHash = prev->nextHash;
5061 prev->nextHash = def;
5062 }
5063 }
5064 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005065 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005066 if (node->children == NULL) {
5067 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5068 NULL, NULL);
5069 def = NULL;
5070 } else {
5071 def = xmlRelaxNGParseInterleave(ctxt, node);
5072 if (def != NULL) {
5073 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005074
Daniel Veillard4c004142003-10-07 11:33:24 +00005075 if ((def->content != NULL) && (def->content->next != NULL)) {
5076 tmp = xmlRelaxNGNewDefine(ctxt, node);
5077 if (tmp != NULL) {
5078 tmp->type = XML_RELAXNG_GROUP;
5079 tmp->content = def->content;
5080 def->content = tmp;
5081 }
5082 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005083
Daniel Veillard4c004142003-10-07 11:33:24 +00005084 tmp = xmlRelaxNGNewDefine(ctxt, node);
5085 if (tmp == NULL)
5086 return (def);
5087 tmp->type = XML_RELAXNG_TEXT;
5088 tmp->next = def->content;
5089 def->content = tmp;
5090 }
5091 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005092 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005093 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5094 "Unexpected node %s is not a pattern\n", node->name,
5095 NULL);
5096 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005097 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005098 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005099}
5100
5101/**
5102 * xmlRelaxNGParseAttribute:
5103 * @ctxt: a Relax-NG parser context
5104 * @node: the element node
5105 *
5106 * parse the content of a RelaxNG attribute node.
5107 *
5108 * Returns the definition pointer or NULL in case of error.
5109 */
5110static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005111xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5112{
Daniel Veillardd2298792003-02-14 16:54:11 +00005113 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005114 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005115 int old_flags;
5116
Daniel Veillardfd573f12003-03-16 17:52:32 +00005117 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005118 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005119 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005120 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005121 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005122 child = node->children;
5123 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005124 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5125 "xmlRelaxNGParseattribute: attribute has no children\n",
5126 NULL, NULL);
5127 return (ret);
5128 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005129 old_flags = ctxt->flags;
5130 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005131 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5132 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005133 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005134
Daniel Veillardd2298792003-02-14 16:54:11 +00005135 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005136 cur = xmlRelaxNGParsePattern(ctxt, child);
5137 if (cur != NULL) {
5138 switch (cur->type) {
5139 case XML_RELAXNG_EMPTY:
5140 case XML_RELAXNG_NOT_ALLOWED:
5141 case XML_RELAXNG_TEXT:
5142 case XML_RELAXNG_ELEMENT:
5143 case XML_RELAXNG_DATATYPE:
5144 case XML_RELAXNG_VALUE:
5145 case XML_RELAXNG_LIST:
5146 case XML_RELAXNG_REF:
5147 case XML_RELAXNG_PARENTREF:
5148 case XML_RELAXNG_EXTERNALREF:
5149 case XML_RELAXNG_DEF:
5150 case XML_RELAXNG_ONEORMORE:
5151 case XML_RELAXNG_ZEROORMORE:
5152 case XML_RELAXNG_OPTIONAL:
5153 case XML_RELAXNG_CHOICE:
5154 case XML_RELAXNG_GROUP:
5155 case XML_RELAXNG_INTERLEAVE:
5156 case XML_RELAXNG_ATTRIBUTE:
5157 ret->content = cur;
5158 cur->parent = ret;
5159 break;
5160 case XML_RELAXNG_START:
5161 case XML_RELAXNG_PARAM:
5162 case XML_RELAXNG_EXCEPT:
5163 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5164 "attribute has invalid content\n", NULL,
5165 NULL);
5166 break;
5167 case XML_RELAXNG_NOOP:
5168 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5169 "RNG Internal error, noop found in attribute\n",
5170 NULL, NULL);
5171 break;
5172 }
5173 }
5174 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005175 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005176 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005177 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5178 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005179 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005180 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005181 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005182}
5183
5184/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005185 * xmlRelaxNGParseExceptNameClass:
5186 * @ctxt: a Relax-NG parser context
5187 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005188 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005189 *
5190 * parse the content of a RelaxNG nameClass node.
5191 *
5192 * Returns the definition pointer or NULL in case of error.
5193 */
5194static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005195xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005196 xmlNodePtr node, int attr)
5197{
Daniel Veillard144fae12003-02-03 13:17:57 +00005198 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5199 xmlNodePtr child;
5200
Daniel Veillardd2298792003-02-14 16:54:11 +00005201 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005202 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5203 "Expecting an except node\n", NULL, NULL);
5204 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005205 }
5206 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005207 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5208 "exceptNameClass allows only a single except node\n",
5209 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005210 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005211 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005212 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5213 NULL, NULL);
5214 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005215 }
5216
Daniel Veillardfd573f12003-03-16 17:52:32 +00005217 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005218 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005219 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005220 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005221 child = node->children;
5222 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005223 cur = xmlRelaxNGNewDefine(ctxt, child);
5224 if (cur == NULL)
5225 break;
5226 if (attr)
5227 cur->type = XML_RELAXNG_ATTRIBUTE;
5228 else
5229 cur->type = XML_RELAXNG_ELEMENT;
5230
Daniel Veillard419a7682003-02-03 23:22:49 +00005231 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005232 if (last == NULL) {
5233 ret->content = cur;
5234 } else {
5235 last->next = cur;
5236 }
5237 last = cur;
5238 }
5239 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005240 }
5241
Daniel Veillard4c004142003-10-07 11:33:24 +00005242 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005243}
5244
5245/**
5246 * xmlRelaxNGParseNameClass:
5247 * @ctxt: a Relax-NG parser context
5248 * @node: the nameClass node
5249 * @def: the current definition
5250 *
5251 * parse the content of a RelaxNG nameClass node.
5252 *
5253 * Returns the definition pointer or NULL in case of error.
5254 */
5255static xmlRelaxNGDefinePtr
5256xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005257 xmlRelaxNGDefinePtr def)
5258{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005259 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005260 xmlChar *val;
5261
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005262 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005263 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005264 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005265 if ((def->type != XML_RELAXNG_ELEMENT) &&
5266 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5267 ret = xmlRelaxNGNewDefine(ctxt, node);
5268 if (ret == NULL)
5269 return (NULL);
5270 ret->parent = def;
5271 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5272 ret->type = XML_RELAXNG_ATTRIBUTE;
5273 else
5274 ret->type = XML_RELAXNG_ELEMENT;
5275 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005276 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005277 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005278 val = xmlNodeGetContent(node);
5279 xmlRelaxNGNormExtSpace(val);
5280 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005281 if (node->parent != NULL)
5282 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5283 "Element %s name '%s' is not an NCName\n",
5284 node->parent->name, val);
5285 else
5286 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5287 "name '%s' is not an NCName\n",
5288 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005289 }
5290 ret->name = val;
5291 val = xmlGetProp(node, BAD_CAST "ns");
5292 ret->ns = val;
5293 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5294 (val != NULL) &&
5295 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005296 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005297 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005298 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005299 }
5300 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5301 (val != NULL) &&
5302 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005303 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5304 "Attribute with QName 'xmlns' is not allowed\n",
5305 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005306 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005307 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005308 ret->name = NULL;
5309 ret->ns = NULL;
5310 if (node->children != NULL) {
5311 ret->nameClass =
5312 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5313 (def->type ==
5314 XML_RELAXNG_ATTRIBUTE));
5315 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005316 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005317 ret->name = NULL;
5318 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5319 if (ret->ns == NULL) {
5320 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5321 "nsName has no ns attribute\n", NULL, NULL);
5322 }
5323 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5324 (ret->ns != NULL) &&
5325 (xmlStrEqual
5326 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5327 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5328 "Attribute with namespace '%s' is not allowed\n",
5329 ret->ns, NULL);
5330 }
5331 if (node->children != NULL) {
5332 ret->nameClass =
5333 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5334 (def->type ==
5335 XML_RELAXNG_ATTRIBUTE));
5336 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005337 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005338 xmlNodePtr child;
5339 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005340
Daniel Veillard4c004142003-10-07 11:33:24 +00005341 ret = xmlRelaxNGNewDefine(ctxt, node);
5342 if (ret == NULL)
5343 return (NULL);
5344 ret->parent = def;
5345 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005346
Daniel Veillard4c004142003-10-07 11:33:24 +00005347 if (node->children == NULL) {
5348 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5349 "Element choice is empty\n", NULL, NULL);
5350 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005351
Daniel Veillard4c004142003-10-07 11:33:24 +00005352 child = node->children;
5353 while (child != NULL) {
5354 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5355 if (tmp != NULL) {
5356 if (last == NULL) {
5357 last = ret->nameClass = tmp;
5358 } else {
5359 last->next = tmp;
5360 last = tmp;
5361 }
5362 }
5363 child = child->next;
5364 }
5365 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005366 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005367 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5368 "expecting name, anyName, nsName or choice : got %s\n",
Daniel Veillard76d36452009-09-07 11:19:33 +02005369 (node == NULL ? 'nothing' : node->name), NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005370 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005371 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005372 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005373 if (def->nameClass == NULL) {
5374 def->nameClass = ret;
5375 } else {
5376 tmp = def->nameClass;
5377 while (tmp->next != NULL) {
5378 tmp = tmp->next;
5379 }
5380 tmp->next = ret;
5381 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005382 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005383 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005384}
5385
5386/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005387 * xmlRelaxNGParseElement:
5388 * @ctxt: a Relax-NG parser context
5389 * @node: the element node
5390 *
5391 * parse the content of a RelaxNG element node.
5392 *
5393 * Returns the definition pointer or NULL in case of error.
5394 */
5395static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005396xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5397{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005398 xmlRelaxNGDefinePtr ret, cur, last;
5399 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005400 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005401
Daniel Veillardfd573f12003-03-16 17:52:32 +00005402 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005403 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005404 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005405 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005406 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005407 child = node->children;
5408 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005409 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5410 "xmlRelaxNGParseElement: element has no children\n",
5411 NULL, NULL);
5412 return (ret);
5413 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005414 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5415 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005416 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005417
Daniel Veillard6eadf632003-01-23 18:29:16 +00005418 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005419 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5420 "xmlRelaxNGParseElement: element has no content\n",
5421 NULL, NULL);
5422 return (ret);
5423 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005424 olddefine = ctxt->define;
5425 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005426 last = NULL;
5427 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005428 cur = xmlRelaxNGParsePattern(ctxt, child);
5429 if (cur != NULL) {
5430 cur->parent = ret;
5431 switch (cur->type) {
5432 case XML_RELAXNG_EMPTY:
5433 case XML_RELAXNG_NOT_ALLOWED:
5434 case XML_RELAXNG_TEXT:
5435 case XML_RELAXNG_ELEMENT:
5436 case XML_RELAXNG_DATATYPE:
5437 case XML_RELAXNG_VALUE:
5438 case XML_RELAXNG_LIST:
5439 case XML_RELAXNG_REF:
5440 case XML_RELAXNG_PARENTREF:
5441 case XML_RELAXNG_EXTERNALREF:
5442 case XML_RELAXNG_DEF:
5443 case XML_RELAXNG_ZEROORMORE:
5444 case XML_RELAXNG_ONEORMORE:
5445 case XML_RELAXNG_OPTIONAL:
5446 case XML_RELAXNG_CHOICE:
5447 case XML_RELAXNG_GROUP:
5448 case XML_RELAXNG_INTERLEAVE:
5449 if (last == NULL) {
5450 ret->content = last = cur;
5451 } else {
5452 if ((last->type == XML_RELAXNG_ELEMENT) &&
5453 (ret->content == last)) {
5454 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5455 if (ret->content != NULL) {
5456 ret->content->type = XML_RELAXNG_GROUP;
5457 ret->content->content = last;
5458 } else {
5459 ret->content = last;
5460 }
5461 }
5462 last->next = cur;
5463 last = cur;
5464 }
5465 break;
5466 case XML_RELAXNG_ATTRIBUTE:
5467 cur->next = ret->attrs;
5468 ret->attrs = cur;
5469 break;
5470 case XML_RELAXNG_START:
5471 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5472 "RNG Internal error, start found in element\n",
5473 NULL, NULL);
5474 break;
5475 case XML_RELAXNG_PARAM:
5476 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5477 "RNG Internal error, param found in element\n",
5478 NULL, NULL);
5479 break;
5480 case XML_RELAXNG_EXCEPT:
5481 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5482 "RNG Internal error, except found in element\n",
5483 NULL, NULL);
5484 break;
5485 case XML_RELAXNG_NOOP:
5486 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5487 "RNG Internal error, noop found in element\n",
5488 NULL, NULL);
5489 break;
5490 }
5491 }
5492 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005493 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005494 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005495 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005496}
5497
5498/**
5499 * xmlRelaxNGParsePatterns:
5500 * @ctxt: a Relax-NG parser context
5501 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005502 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005503 *
5504 * parse the content of a RelaxNG start node.
5505 *
5506 * Returns the definition pointer or NULL in case of error.
5507 */
5508static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005509xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005510 int group)
5511{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005512 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005513
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005514 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005515 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005516 if (IS_RELAXNG(nodes, "element")) {
5517 cur = xmlRelaxNGParseElement(ctxt, nodes);
5518 if (def == NULL) {
5519 def = last = cur;
5520 } else {
5521 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5522 (def == last)) {
5523 def = xmlRelaxNGNewDefine(ctxt, nodes);
5524 def->type = XML_RELAXNG_GROUP;
5525 def->content = last;
5526 }
5527 last->next = cur;
5528 last = cur;
5529 }
5530 cur->parent = parent;
5531 } else {
5532 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5533 if (cur != NULL) {
5534 if (def == NULL) {
5535 def = last = cur;
5536 } else {
5537 last->next = cur;
5538 last = cur;
5539 }
5540 }
5541 }
5542 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005543 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005544 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005545}
5546
5547/**
5548 * xmlRelaxNGParseStart:
5549 * @ctxt: a Relax-NG parser context
5550 * @nodes: start children nodes
5551 *
5552 * parse the content of a RelaxNG start node.
5553 *
5554 * Returns 0 in case of success, -1 in case of error
5555 */
5556static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005557xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5558{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005559 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005560 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005561
Daniel Veillardd2298792003-02-14 16:54:11 +00005562 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005563 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5564 NULL, NULL);
5565 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005566 }
5567 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005568 def = xmlRelaxNGNewDefine(ctxt, nodes);
5569 if (def == NULL)
5570 return (-1);
5571 def->type = XML_RELAXNG_EMPTY;
5572 if (nodes->children != NULL) {
5573 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5574 "element empty is not empty\n", NULL, NULL);
5575 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005576 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005577 def = xmlRelaxNGNewDefine(ctxt, nodes);
5578 if (def == NULL)
5579 return (-1);
5580 def->type = XML_RELAXNG_NOT_ALLOWED;
5581 if (nodes->children != NULL) {
5582 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5583 "element notAllowed is not empty\n", NULL, NULL);
5584 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005585 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005586 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005587 }
5588 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005589 last = ctxt->grammar->start;
5590 while (last->next != NULL)
5591 last = last->next;
5592 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005593 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005594 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005595 }
5596 nodes = nodes->next;
5597 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005598 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5599 "start more than one children\n", NULL, NULL);
5600 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005601 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005602 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005603}
5604
5605/**
5606 * xmlRelaxNGParseGrammarContent:
5607 * @ctxt: a Relax-NG parser context
5608 * @nodes: grammar children nodes
5609 *
5610 * parse the content of a RelaxNG grammar node.
5611 *
5612 * Returns 0 in case of success, -1 in case of error
5613 */
5614static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005615xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5616 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005617{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005618 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005619
5620 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005621 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5622 "grammar has no children\n", NULL, NULL);
5623 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005624 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005625 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005626 if (IS_RELAXNG(nodes, "start")) {
5627 if (nodes->children == NULL) {
5628 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5629 "start has no children\n", NULL, NULL);
5630 } else {
5631 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5632 if (tmp != 0)
5633 ret = -1;
5634 }
5635 } else if (IS_RELAXNG(nodes, "define")) {
5636 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5637 if (tmp != 0)
5638 ret = -1;
5639 } else if (IS_RELAXNG(nodes, "include")) {
5640 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5641 if (tmp != 0)
5642 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005643 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005644 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5645 "grammar has unexpected child %s\n", nodes->name,
5646 NULL);
5647 ret = -1;
5648 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005649 nodes = nodes->next;
5650 }
5651 return (ret);
5652}
5653
5654/**
5655 * xmlRelaxNGCheckReference:
5656 * @ref: the ref
5657 * @ctxt: a Relax-NG parser context
5658 * @name: the name associated to the defines
5659 *
5660 * Applies the 4.17. combine attribute rule for all the define
5661 * element of a given grammar using the same name.
5662 */
5663static void
5664xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005665 xmlRelaxNGParserCtxtPtr ctxt,
5666 const xmlChar * name)
5667{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005668 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005669 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005670
5671 grammar = ctxt->grammar;
5672 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005673 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5674 "Internal error: no grammar in CheckReference %s\n",
5675 name, NULL);
5676 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005677 }
5678 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005679 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5680 "Internal error: reference has content in CheckReference %s\n",
5681 name, NULL);
5682 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005683 }
5684 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005685 def = xmlHashLookup(grammar->defs, name);
5686 if (def != NULL) {
5687 cur = ref;
5688 while (cur != NULL) {
5689 cur->content = def;
5690 cur = cur->nextHash;
5691 }
5692 } else {
5693 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5694 "Reference %s has no matching definition\n", name,
5695 NULL);
5696 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005697 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005698 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5699 "Reference %s has no matching definition\n", name,
5700 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005701 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005702}
5703
5704/**
5705 * xmlRelaxNGCheckCombine:
5706 * @define: the define(s) list
5707 * @ctxt: a Relax-NG parser context
5708 * @name: the name associated to the defines
5709 *
5710 * Applies the 4.17. combine attribute rule for all the define
5711 * element of a given grammar using the same name.
5712 */
5713static void
5714xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005715 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5716{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005717 xmlChar *combine;
5718 int choiceOrInterleave = -1;
5719 int missing = 0;
5720 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5721
5722 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005723 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005724 cur = define;
5725 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005726 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5727 if (combine != NULL) {
5728 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5729 if (choiceOrInterleave == -1)
5730 choiceOrInterleave = 1;
5731 else if (choiceOrInterleave == 0) {
5732 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5733 "Defines for %s use both 'choice' and 'interleave'\n",
5734 name, NULL);
5735 }
5736 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5737 if (choiceOrInterleave == -1)
5738 choiceOrInterleave = 0;
5739 else if (choiceOrInterleave == 1) {
5740 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5741 "Defines for %s use both 'choice' and 'interleave'\n",
5742 name, NULL);
5743 }
5744 } else {
5745 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5746 "Defines for %s use unknown combine value '%s''\n",
5747 name, combine);
5748 }
5749 xmlFree(combine);
5750 } else {
5751 if (missing == 0)
5752 missing = 1;
5753 else {
5754 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5755 "Some defines for %s needs the combine attribute\n",
5756 name, NULL);
5757 }
5758 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005759
Daniel Veillard4c004142003-10-07 11:33:24 +00005760 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005761 }
5762#ifdef DEBUG
5763 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005764 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5765 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005766#endif
5767 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005768 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005769 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005770 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005771 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005772 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005773 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005774 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005775 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005776 tmp = define;
5777 last = NULL;
5778 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005779 if (tmp->content != NULL) {
5780 if (tmp->content->next != NULL) {
5781 /*
5782 * we need first to create a wrapper.
5783 */
5784 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5785 if (tmp2 == NULL)
5786 break;
5787 tmp2->type = XML_RELAXNG_GROUP;
5788 tmp2->content = tmp->content;
5789 } else {
5790 tmp2 = tmp->content;
5791 }
5792 if (last == NULL) {
5793 cur->content = tmp2;
5794 } else {
5795 last->next = tmp2;
5796 }
5797 last = tmp2;
5798 }
5799 tmp->content = cur;
5800 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005801 }
5802 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005803 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005804 if (ctxt->interleaves == NULL)
5805 ctxt->interleaves = xmlHashCreate(10);
5806 if (ctxt->interleaves == NULL) {
5807 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5808 "Failed to create interleaves hash table\n", NULL,
5809 NULL);
5810 } else {
5811 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005812
Daniel Veillard4c004142003-10-07 11:33:24 +00005813 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5814 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5815 0) {
5816 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5817 "Failed to add %s to hash table\n",
5818 (const xmlChar *) tmpname, NULL);
5819 }
5820 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005821 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005822}
5823
5824/**
5825 * xmlRelaxNGCombineStart:
5826 * @ctxt: a Relax-NG parser context
5827 * @grammar: the grammar
5828 *
5829 * Applies the 4.17. combine rule for all the start
5830 * element of a given grammar.
5831 */
5832static void
5833xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005834 xmlRelaxNGGrammarPtr grammar)
5835{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005836 xmlRelaxNGDefinePtr starts;
5837 xmlChar *combine;
5838 int choiceOrInterleave = -1;
5839 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005840 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005841
Daniel Veillard2df2de22003-02-17 23:34:33 +00005842 starts = grammar->start;
5843 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005844 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005845 cur = starts;
5846 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005847 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5848 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5849 combine = NULL;
5850 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5851 "Internal error: start element not found\n", NULL,
5852 NULL);
5853 } else {
5854 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5855 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005856
Daniel Veillard4c004142003-10-07 11:33:24 +00005857 if (combine != NULL) {
5858 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5859 if (choiceOrInterleave == -1)
5860 choiceOrInterleave = 1;
5861 else if (choiceOrInterleave == 0) {
5862 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5863 "<start> use both 'choice' and 'interleave'\n",
5864 NULL, NULL);
5865 }
5866 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5867 if (choiceOrInterleave == -1)
5868 choiceOrInterleave = 0;
5869 else if (choiceOrInterleave == 1) {
5870 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5871 "<start> use both 'choice' and 'interleave'\n",
5872 NULL, NULL);
5873 }
5874 } else {
5875 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5876 "<start> uses unknown combine value '%s''\n",
5877 combine, NULL);
5878 }
5879 xmlFree(combine);
5880 } else {
5881 if (missing == 0)
5882 missing = 1;
5883 else {
5884 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5885 "Some <start> element miss the combine attribute\n",
5886 NULL, NULL);
5887 }
5888 }
5889
5890 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005891 }
5892#ifdef DEBUG
5893 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005894 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5895 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005896#endif
5897 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005898 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005899 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005900 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005901 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005902 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005903 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005904 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005905 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005906 cur->content = grammar->start;
5907 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005908 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005909 if (ctxt->interleaves == NULL)
5910 ctxt->interleaves = xmlHashCreate(10);
5911 if (ctxt->interleaves == NULL) {
5912 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5913 "Failed to create interleaves hash table\n", NULL,
5914 NULL);
5915 } else {
5916 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005917
Daniel Veillard4c004142003-10-07 11:33:24 +00005918 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5919 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5920 0) {
5921 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5922 "Failed to add %s to hash table\n",
5923 (const xmlChar *) tmpname, NULL);
5924 }
5925 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005926 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005927}
5928
5929/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005930 * xmlRelaxNGCheckCycles:
5931 * @ctxt: a Relax-NG parser context
5932 * @nodes: grammar children nodes
5933 * @depth: the counter
5934 *
5935 * Check for cycles.
5936 *
5937 * Returns 0 if check passed, and -1 in case of error
5938 */
5939static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005940xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5941 xmlRelaxNGDefinePtr cur, int depth)
5942{
Daniel Veillardd4310742003-02-18 21:12:46 +00005943 int ret = 0;
5944
5945 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005946 if ((cur->type == XML_RELAXNG_REF) ||
5947 (cur->type == XML_RELAXNG_PARENTREF)) {
5948 if (cur->depth == -1) {
5949 cur->depth = depth;
5950 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5951 cur->depth = -2;
5952 } else if (depth == cur->depth) {
5953 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5954 "Detected a cycle in %s references\n",
5955 cur->name, NULL);
5956 return (-1);
5957 }
5958 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5959 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5960 } else {
5961 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5962 }
5963 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005964 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005965 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005966}
5967
5968/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005969 * xmlRelaxNGTryUnlink:
5970 * @ctxt: a Relax-NG parser context
5971 * @cur: the definition to unlink
5972 * @parent: the parent definition
5973 * @prev: the previous sibling definition
5974 *
5975 * Try to unlink a definition. If not possble make it a NOOP
5976 *
5977 * Returns the new prev definition
5978 */
5979static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005980xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5981 xmlRelaxNGDefinePtr cur,
5982 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5983{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005984 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005985 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005986 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005987 if (parent != NULL) {
5988 if (parent->content == cur)
5989 parent->content = cur->next;
5990 else if (parent->attrs == cur)
5991 parent->attrs = cur->next;
5992 else if (parent->nameClass == cur)
5993 parent->nameClass = cur->next;
5994 } else {
5995 cur->type = XML_RELAXNG_NOOP;
5996 prev = cur;
5997 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005998 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005999 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006000}
6001
6002/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006003 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006004 * @ctxt: a Relax-NG parser context
6005 * @nodes: grammar children nodes
6006 *
6007 * Check for simplification of empty and notAllowed
6008 */
6009static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006010xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6011 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6012{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006013 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006014
Daniel Veillardfd573f12003-03-16 17:52:32 +00006015 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006016 if ((cur->type == XML_RELAXNG_REF) ||
6017 (cur->type == XML_RELAXNG_PARENTREF)) {
6018 if (cur->depth != -3) {
6019 cur->depth = -3;
6020 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6021 }
6022 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6023 cur->parent = parent;
6024 if ((parent != NULL) &&
6025 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6026 (parent->type == XML_RELAXNG_LIST) ||
6027 (parent->type == XML_RELAXNG_GROUP) ||
6028 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6029 (parent->type == XML_RELAXNG_ONEORMORE) ||
6030 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6031 parent->type = XML_RELAXNG_NOT_ALLOWED;
6032 break;
6033 }
6034 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6035 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6036 } else
6037 prev = cur;
6038 } else if (cur->type == XML_RELAXNG_EMPTY) {
6039 cur->parent = parent;
6040 if ((parent != NULL) &&
6041 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6042 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6043 parent->type = XML_RELAXNG_EMPTY;
6044 break;
6045 }
6046 if ((parent != NULL) &&
6047 ((parent->type == XML_RELAXNG_GROUP) ||
6048 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6049 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6050 } else
6051 prev = cur;
6052 } else {
6053 cur->parent = parent;
6054 if (cur->content != NULL)
6055 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6056 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6057 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6058 if (cur->nameClass != NULL)
6059 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6060 /*
6061 * On Elements, try to move attribute only generating rules on
6062 * the attrs rules.
6063 */
6064 if (cur->type == XML_RELAXNG_ELEMENT) {
6065 int attronly;
6066 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006067
Daniel Veillard4c004142003-10-07 11:33:24 +00006068 while (cur->content != NULL) {
6069 attronly =
6070 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6071 if (attronly == 1) {
6072 /*
6073 * migrate cur->content to attrs
6074 */
6075 tmp = cur->content;
6076 cur->content = tmp->next;
6077 tmp->next = cur->attrs;
6078 cur->attrs = tmp;
6079 } else {
6080 /*
6081 * cur->content can generate elements or text
6082 */
6083 break;
6084 }
6085 }
6086 pre = cur->content;
6087 while ((pre != NULL) && (pre->next != NULL)) {
6088 tmp = pre->next;
6089 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6090 if (attronly == 1) {
6091 /*
6092 * migrate tmp to attrs
6093 */
6094 pre->next = tmp->next;
6095 tmp->next = cur->attrs;
6096 cur->attrs = tmp;
6097 } else {
6098 pre = tmp;
6099 }
6100 }
6101 }
6102 /*
6103 * This may result in a simplification
6104 */
6105 if ((cur->type == XML_RELAXNG_GROUP) ||
6106 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6107 if (cur->content == NULL)
6108 cur->type = XML_RELAXNG_EMPTY;
6109 else if (cur->content->next == NULL) {
6110 if ((parent == NULL) && (prev == NULL)) {
6111 cur->type = XML_RELAXNG_NOOP;
6112 } else if (prev == NULL) {
6113 parent->content = cur->content;
6114 cur->content->next = cur->next;
6115 cur = cur->content;
6116 } else {
6117 cur->content->next = cur->next;
6118 prev->next = cur->content;
6119 cur = cur->content;
6120 }
6121 }
6122 }
6123 /*
6124 * the current node may have been transformed back
6125 */
6126 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6127 (cur->content != NULL) &&
6128 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6129 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6130 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6131 if ((parent != NULL) &&
6132 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6133 (parent->type == XML_RELAXNG_LIST) ||
6134 (parent->type == XML_RELAXNG_GROUP) ||
6135 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6136 (parent->type == XML_RELAXNG_ONEORMORE) ||
6137 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6138 parent->type = XML_RELAXNG_NOT_ALLOWED;
6139 break;
6140 }
6141 if ((parent != NULL) &&
6142 (parent->type == XML_RELAXNG_CHOICE)) {
6143 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6144 } else
6145 prev = cur;
6146 } else if (cur->type == XML_RELAXNG_EMPTY) {
6147 if ((parent != NULL) &&
6148 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6149 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6150 parent->type = XML_RELAXNG_EMPTY;
6151 break;
6152 }
6153 if ((parent != NULL) &&
6154 ((parent->type == XML_RELAXNG_GROUP) ||
6155 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6156 (parent->type == XML_RELAXNG_CHOICE))) {
6157 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6158 } else
6159 prev = cur;
6160 } else {
6161 prev = cur;
6162 }
6163 }
6164 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006165 }
6166}
6167
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006168/**
6169 * xmlRelaxNGGroupContentType:
6170 * @ct1: the first content type
6171 * @ct2: the second content type
6172 *
6173 * Try to group 2 content types
6174 *
6175 * Returns the content type
6176 */
6177static xmlRelaxNGContentType
6178xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006179 xmlRelaxNGContentType ct2)
6180{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006181 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006182 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6183 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006184 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006185 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006186 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006187 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006188 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006189 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6190 return (XML_RELAXNG_CONTENT_COMPLEX);
6191 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006192}
6193
6194/**
6195 * xmlRelaxNGMaxContentType:
6196 * @ct1: the first content type
6197 * @ct2: the second content type
6198 *
6199 * Compute the max content-type
6200 *
6201 * Returns the content type
6202 */
6203static xmlRelaxNGContentType
6204xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006205 xmlRelaxNGContentType ct2)
6206{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006207 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006208 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6209 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006210 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006211 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6212 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006213 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006214 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6215 return (XML_RELAXNG_CONTENT_COMPLEX);
6216 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006217}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006218
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006219/**
6220 * xmlRelaxNGCheckRules:
6221 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006222 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006223 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006224 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006225 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006226 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006227 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006228 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006229 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006230static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006231xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6232 xmlRelaxNGDefinePtr cur, int flags,
6233 xmlRelaxNGType ptype)
6234{
Daniel Veillardd44b9362009-09-07 12:15:08 +02006235 int nflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006236 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006237
Daniel Veillardfd573f12003-03-16 17:52:32 +00006238 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006239 ret = XML_RELAXNG_CONTENT_EMPTY;
6240 if ((cur->type == XML_RELAXNG_REF) ||
6241 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006242 /*
6243 * This should actually be caught by list//element(ref) at the
6244 * element boundaries, c.f. Bug #159968 local refs are dropped
6245 * in step 4.19.
6246 */
6247#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006248 if (flags & XML_RELAXNG_IN_LIST) {
6249 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6250 "Found forbidden pattern list//ref\n", NULL,
6251 NULL);
6252 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006253#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006254 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6255 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6256 "Found forbidden pattern data/except//ref\n",
6257 NULL, NULL);
6258 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006259 if (cur->content == NULL) {
6260 if (cur->type == XML_RELAXNG_PARENTREF)
6261 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6262 "Internal found no define for parent refs\n",
6263 NULL, NULL);
6264 else
6265 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6266 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006267 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006268 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006269 if (cur->depth > -4) {
6270 cur->depth = -4;
6271 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6272 flags, cur->type);
6273 cur->depth = ret - 15;
6274 } else if (cur->depth == -4) {
6275 ret = XML_RELAXNG_CONTENT_COMPLEX;
6276 } else {
6277 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6278 }
6279 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6280 /*
6281 * The 7.3 Attribute derivation rule for groups is plugged there
6282 */
6283 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6284 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6285 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6286 "Found forbidden pattern data/except//element(ref)\n",
6287 NULL, NULL);
6288 }
6289 if (flags & XML_RELAXNG_IN_LIST) {
6290 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6291 "Found forbidden pattern list//element(ref)\n",
6292 NULL, NULL);
6293 }
6294 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6295 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6296 "Found forbidden pattern attribute//element(ref)\n",
6297 NULL, NULL);
6298 }
6299 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6300 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6301 "Found forbidden pattern attribute//element(ref)\n",
6302 NULL, NULL);
6303 }
6304 /*
6305 * reset since in the simple form elements are only child
6306 * of grammar/define
6307 */
6308 nflags = 0;
6309 ret =
6310 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6311 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6312 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6313 "Element %s attributes have a content type error\n",
6314 cur->name, NULL);
6315 }
6316 ret =
6317 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6318 cur->type);
6319 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6320 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6321 "Element %s has a content type error\n",
6322 cur->name, NULL);
6323 } else {
6324 ret = XML_RELAXNG_CONTENT_COMPLEX;
6325 }
6326 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6327 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6328 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6329 "Found forbidden pattern attribute//attribute\n",
6330 NULL, NULL);
6331 }
6332 if (flags & XML_RELAXNG_IN_LIST) {
6333 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6334 "Found forbidden pattern list//attribute\n",
6335 NULL, NULL);
6336 }
6337 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6338 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6339 "Found forbidden pattern oneOrMore//group//attribute\n",
6340 NULL, NULL);
6341 }
6342 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6343 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6344 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6345 NULL, NULL);
6346 }
6347 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6348 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6349 "Found forbidden pattern data/except//attribute\n",
6350 NULL, NULL);
6351 }
6352 if (flags & XML_RELAXNG_IN_START) {
6353 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6354 "Found forbidden pattern start//attribute\n",
6355 NULL, NULL);
6356 }
6357 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6358 && (cur->name == NULL)) {
6359 if (cur->ns == NULL) {
6360 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6361 "Found anyName attribute without oneOrMore ancestor\n",
6362 NULL, NULL);
6363 } else {
6364 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6365 "Found nsName attribute without oneOrMore ancestor\n",
6366 NULL, NULL);
6367 }
6368 }
6369 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6370 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6371 ret = XML_RELAXNG_CONTENT_EMPTY;
6372 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6373 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6374 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6375 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6376 "Found forbidden pattern data/except//oneOrMore\n",
6377 NULL, NULL);
6378 }
6379 if (flags & XML_RELAXNG_IN_START) {
6380 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6381 "Found forbidden pattern start//oneOrMore\n",
6382 NULL, NULL);
6383 }
6384 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6385 ret =
6386 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6387 cur->type);
6388 ret = xmlRelaxNGGroupContentType(ret, ret);
6389 } else if (cur->type == XML_RELAXNG_LIST) {
6390 if (flags & XML_RELAXNG_IN_LIST) {
6391 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6392 "Found forbidden pattern list//list\n", NULL,
6393 NULL);
6394 }
6395 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6396 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6397 "Found forbidden pattern data/except//list\n",
6398 NULL, NULL);
6399 }
6400 if (flags & XML_RELAXNG_IN_START) {
6401 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6402 "Found forbidden pattern start//list\n", NULL,
6403 NULL);
6404 }
6405 nflags = flags | XML_RELAXNG_IN_LIST;
6406 ret =
6407 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6408 cur->type);
6409 } else if (cur->type == XML_RELAXNG_GROUP) {
6410 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6411 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6412 "Found forbidden pattern data/except//group\n",
6413 NULL, NULL);
6414 }
6415 if (flags & XML_RELAXNG_IN_START) {
6416 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6417 "Found forbidden pattern start//group\n", NULL,
6418 NULL);
6419 }
6420 if (flags & XML_RELAXNG_IN_ONEORMORE)
6421 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6422 else
6423 nflags = flags;
6424 ret =
6425 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6426 cur->type);
6427 /*
6428 * The 7.3 Attribute derivation rule for groups is plugged there
6429 */
6430 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6431 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6432 if (flags & XML_RELAXNG_IN_LIST) {
6433 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6434 "Found forbidden pattern list//interleave\n",
6435 NULL, NULL);
6436 }
6437 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6438 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6439 "Found forbidden pattern data/except//interleave\n",
6440 NULL, NULL);
6441 }
6442 if (flags & XML_RELAXNG_IN_START) {
6443 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6444 "Found forbidden pattern start//interleave\n",
6445 NULL, NULL);
6446 }
6447 if (flags & XML_RELAXNG_IN_ONEORMORE)
6448 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6449 else
6450 nflags = flags;
6451 ret =
6452 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6453 cur->type);
6454 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6455 if ((cur->parent != NULL) &&
6456 (cur->parent->type == XML_RELAXNG_DATATYPE))
6457 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6458 else
6459 nflags = flags;
6460 ret =
6461 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6462 cur->type);
6463 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6464 if (flags & XML_RELAXNG_IN_START) {
6465 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6466 "Found forbidden pattern start//data\n", NULL,
6467 NULL);
6468 }
6469 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6470 ret = XML_RELAXNG_CONTENT_SIMPLE;
6471 } else if (cur->type == XML_RELAXNG_VALUE) {
6472 if (flags & XML_RELAXNG_IN_START) {
6473 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6474 "Found forbidden pattern start//value\n", NULL,
6475 NULL);
6476 }
6477 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6478 ret = XML_RELAXNG_CONTENT_SIMPLE;
6479 } else if (cur->type == XML_RELAXNG_TEXT) {
6480 if (flags & XML_RELAXNG_IN_LIST) {
6481 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6482 "Found forbidden pattern list//text\n", NULL,
6483 NULL);
6484 }
6485 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6486 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6487 "Found forbidden pattern data/except//text\n",
6488 NULL, NULL);
6489 }
6490 if (flags & XML_RELAXNG_IN_START) {
6491 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6492 "Found forbidden pattern start//text\n", NULL,
6493 NULL);
6494 }
6495 ret = XML_RELAXNG_CONTENT_COMPLEX;
6496 } else if (cur->type == XML_RELAXNG_EMPTY) {
6497 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6498 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6499 "Found forbidden pattern data/except//empty\n",
6500 NULL, NULL);
6501 }
6502 if (flags & XML_RELAXNG_IN_START) {
6503 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6504 "Found forbidden pattern start//empty\n", NULL,
6505 NULL);
6506 }
6507 ret = XML_RELAXNG_CONTENT_EMPTY;
6508 } else if (cur->type == XML_RELAXNG_CHOICE) {
6509 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6510 ret =
6511 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6512 } else {
6513 ret =
6514 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6515 }
6516 cur = cur->next;
6517 if (ptype == XML_RELAXNG_GROUP) {
6518 val = xmlRelaxNGGroupContentType(val, ret);
6519 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02006520 /*
6521 * TODO: scan complain that tmp is never used, seems on purpose
6522 * need double-checking
6523 */
Daniel Veillard4c004142003-10-07 11:33:24 +00006524 tmp = xmlRelaxNGGroupContentType(val, ret);
6525 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6526 tmp = xmlRelaxNGMaxContentType(val, ret);
6527 } else if (ptype == XML_RELAXNG_CHOICE) {
6528 val = xmlRelaxNGMaxContentType(val, ret);
6529 } else if (ptype == XML_RELAXNG_LIST) {
6530 val = XML_RELAXNG_CONTENT_SIMPLE;
6531 } else if (ptype == XML_RELAXNG_EXCEPT) {
6532 if (ret == XML_RELAXNG_CONTENT_ERROR)
6533 val = XML_RELAXNG_CONTENT_ERROR;
6534 else
6535 val = XML_RELAXNG_CONTENT_SIMPLE;
6536 } else {
6537 val = xmlRelaxNGGroupContentType(val, ret);
6538 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006539
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006540 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006541 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006542}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006543
6544/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006545 * xmlRelaxNGParseGrammar:
6546 * @ctxt: a Relax-NG parser context
6547 * @nodes: grammar children nodes
6548 *
6549 * parse a Relax-NG <grammar> node
6550 *
6551 * Returns the internal xmlRelaxNGGrammarPtr built or
6552 * NULL in case of error
6553 */
6554static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006555xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6556{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006557 xmlRelaxNGGrammarPtr ret, tmp, old;
6558
Daniel Veillardc482e262003-02-26 14:48:48 +00006559#ifdef DEBUG_GRAMMAR
6560 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6561#endif
6562
Daniel Veillard6eadf632003-01-23 18:29:16 +00006563 ret = xmlRelaxNGNewGrammar(ctxt);
6564 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006565 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006566
6567 /*
6568 * Link the new grammar in the tree
6569 */
6570 ret->parent = ctxt->grammar;
6571 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006572 tmp = ctxt->grammar->children;
6573 if (tmp == NULL) {
6574 ctxt->grammar->children = ret;
6575 } else {
6576 while (tmp->next != NULL)
6577 tmp = tmp->next;
6578 tmp->next = ret;
6579 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006580 }
6581
6582 old = ctxt->grammar;
6583 ctxt->grammar = ret;
6584 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6585 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006586 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006587 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6588 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006589 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006590 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6591 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006592 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006593
6594 /*
6595 * Apply 4.17 mergingd rules to defines and starts
6596 */
6597 xmlRelaxNGCombineStart(ctxt, ret);
6598 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006599 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6600 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006601 }
6602
6603 /*
6604 * link together defines and refs in this grammar
6605 */
6606 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006607 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6608 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006609 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006610
Daniel Veillard81c51e12009-08-14 18:52:10 +02006611
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006612 /* @@@@ */
6613
Daniel Veillard6eadf632003-01-23 18:29:16 +00006614 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006615 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006616}
6617
6618/**
6619 * xmlRelaxNGParseDocument:
6620 * @ctxt: a Relax-NG parser context
6621 * @node: the root node of the RelaxNG schema
6622 *
6623 * parse a Relax-NG definition resource and build an internal
6624 * xmlRelaxNG struture which can be used to validate instances.
6625 *
6626 * Returns the internal XML RelaxNG structure built or
6627 * NULL in case of error
6628 */
6629static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006630xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6631{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006632 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006633 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006634 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006635
6636 if ((ctxt == NULL) || (node == NULL))
6637 return (NULL);
6638
6639 schema = xmlRelaxNGNewRelaxNG(ctxt);
6640 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006641 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006642
Daniel Veillard276be4a2003-01-24 01:03:34 +00006643 olddefine = ctxt->define;
6644 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006645 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006646 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006647 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006648 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006649
Daniel Veillard4c004142003-10-07 11:33:24 +00006650 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6651 if (schema->topgrammar == NULL) {
6652 return (schema);
6653 }
6654 /*
6655 * Link the new grammar in the tree
6656 */
6657 ret->parent = ctxt->grammar;
6658 if (ctxt->grammar != NULL) {
6659 tmp = ctxt->grammar->children;
6660 if (tmp == NULL) {
6661 ctxt->grammar->children = ret;
6662 } else {
6663 while (tmp->next != NULL)
6664 tmp = tmp->next;
6665 tmp->next = ret;
6666 }
6667 }
6668 old = ctxt->grammar;
6669 ctxt->grammar = ret;
6670 xmlRelaxNGParseStart(ctxt, node);
6671 if (old != NULL)
6672 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006673 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006674 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006675 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006676 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6677 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6678 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6679 while ((schema->topgrammar->start != NULL) &&
6680 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6681 (schema->topgrammar->start->next != NULL))
6682 schema->topgrammar->start =
6683 schema->topgrammar->start->content;
6684 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6685 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6686 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006687 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006688#ifdef DEBUG
6689 if (schema == NULL)
6690 xmlGenericError(xmlGenericErrorContext,
6691 "xmlRelaxNGParseDocument() failed\n");
6692#endif
6693
6694 return (schema);
6695}
6696
6697/************************************************************************
6698 * *
6699 * Reading RelaxNGs *
6700 * *
6701 ************************************************************************/
6702
6703/**
6704 * xmlRelaxNGNewParserCtxt:
6705 * @URL: the location of the schema
6706 *
6707 * Create an XML RelaxNGs parse context for that file/resource expected
6708 * to contain an XML RelaxNGs file.
6709 *
6710 * Returns the parser context or NULL in case of error
6711 */
6712xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006713xmlRelaxNGNewParserCtxt(const char *URL)
6714{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006715 xmlRelaxNGParserCtxtPtr ret;
6716
6717 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006718 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006719
Daniel Veillard4c004142003-10-07 11:33:24 +00006720 ret =
6721 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006722 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006723 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006724 return (NULL);
6725 }
6726 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006727 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006728 ret->error = xmlGenericError;
6729 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006730 return (ret);
6731}
6732
6733/**
6734 * xmlRelaxNGNewMemParserCtxt:
6735 * @buffer: a pointer to a char array containing the schemas
6736 * @size: the size of the array
6737 *
6738 * Create an XML RelaxNGs parse context for that memory buffer expected
6739 * to contain an XML RelaxNGs file.
6740 *
6741 * Returns the parser context or NULL in case of error
6742 */
6743xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006744xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6745{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006746 xmlRelaxNGParserCtxtPtr ret;
6747
6748 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006749 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006750
Daniel Veillard4c004142003-10-07 11:33:24 +00006751 ret =
6752 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006753 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006754 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006755 return (NULL);
6756 }
6757 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6758 ret->buffer = buffer;
6759 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006760 ret->error = xmlGenericError;
6761 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006762 return (ret);
6763}
6764
6765/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006766 * xmlRelaxNGNewDocParserCtxt:
6767 * @doc: a preparsed document tree
6768 *
6769 * Create an XML RelaxNGs parser context for that document.
6770 * Note: since the process of compiling a RelaxNG schemas modifies the
6771 * document, the @doc parameter is duplicated internally.
6772 *
6773 * Returns the parser context or NULL in case of error
6774 */
6775xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006776xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6777{
Daniel Veillard33300b42003-04-17 09:09:19 +00006778 xmlRelaxNGParserCtxtPtr ret;
6779 xmlDocPtr copy;
6780
6781 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006782 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006783 copy = xmlCopyDoc(doc, 1);
6784 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006785 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006786
Daniel Veillard4c004142003-10-07 11:33:24 +00006787 ret =
6788 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006789 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006790 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006791 return (NULL);
6792 }
6793 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6794 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006795 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006796 ret->userData = xmlGenericErrorContext;
6797 return (ret);
6798}
6799
6800/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006801 * xmlRelaxNGFreeParserCtxt:
6802 * @ctxt: the schema parser context
6803 *
6804 * Free the resources associated to the schema parser context
6805 */
6806void
Daniel Veillard4c004142003-10-07 11:33:24 +00006807xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6808{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006809 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006810 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006811 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006812 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006813 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006814 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006815 if (ctxt->interleaves != NULL)
6816 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006817 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006818 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006819 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006820 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006821 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006822 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006823 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006824 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006825 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006826 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006827
Daniel Veillard4c004142003-10-07 11:33:24 +00006828 for (i = 0; i < ctxt->defNr; i++)
6829 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6830 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006831 }
Daniel Veillard42595322004-11-08 10:52:06 +00006832 if ((ctxt->document != NULL) && (ctxt->freedoc))
6833 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006834 xmlFree(ctxt);
6835}
6836
Daniel Veillard6eadf632003-01-23 18:29:16 +00006837/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006838 * xmlRelaxNGNormExtSpace:
6839 * @value: a value
6840 *
6841 * Removes the leading and ending spaces of the value
6842 * The string is modified "in situ"
6843 */
6844static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006845xmlRelaxNGNormExtSpace(xmlChar * value)
6846{
Daniel Veillardd2298792003-02-14 16:54:11 +00006847 xmlChar *start = value;
6848 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006849
Daniel Veillard4c004142003-10-07 11:33:24 +00006850 if (value == NULL)
6851 return;
6852
William M. Brack76e95df2003-10-18 16:20:14 +00006853 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006854 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006855 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006856 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006857 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006858 cur++;
6859 if (*cur == 0)
6860 return;
6861 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006862 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006863 cur++;
6864 if (*cur == 0) {
6865 *start = 0;
6866 return;
6867 }
6868 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006869 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006870 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006871 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006872 *start++ = *cur++;
6873 if (*cur == 0) {
6874 *start = 0;
6875 return;
6876 }
6877 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006878 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006879 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006880 if (*cur == 0) {
6881 *start = 0;
6882 return;
6883 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006884 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006885 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006886 }
6887}
6888
6889/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006890 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006891 * @ctxt: a Relax-NG parser context
6892 * @node: a Relax-NG node
6893 *
6894 * Check all the attributes on the given node
6895 */
6896static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006897xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6898{
Daniel Veillardd2298792003-02-14 16:54:11 +00006899 xmlAttrPtr cur, next;
6900
6901 cur = node->properties;
6902 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006903 next = cur->next;
6904 if ((cur->ns == NULL) ||
6905 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6906 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6907 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6908 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6909 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6910 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6911 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6912 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6913 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6914 "Attribute %s is not allowed on %s\n",
6915 cur->name, node->name);
6916 }
6917 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6918 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6919 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6920 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6921 "Attribute %s is not allowed on %s\n",
6922 cur->name, node->name);
6923 }
6924 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6925 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6926 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6927 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6928 "Attribute %s is not allowed on %s\n",
6929 cur->name, node->name);
6930 }
6931 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6932 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6933 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6934 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6935 "Attribute %s is not allowed on %s\n",
6936 cur->name, node->name);
6937 }
6938 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6939 xmlChar *val;
6940 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006941
Daniel Veillard4c004142003-10-07 11:33:24 +00006942 val = xmlNodeListGetString(node->doc, cur->children, 1);
6943 if (val != NULL) {
6944 if (val[0] != 0) {
6945 uri = xmlParseURI((const char *) val);
6946 if (uri == NULL) {
6947 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6948 "Attribute %s contains invalid URI %s\n",
6949 cur->name, val);
6950 } else {
6951 if (uri->scheme == NULL) {
6952 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6953 "Attribute %s URI %s is not absolute\n",
6954 cur->name, val);
6955 }
6956 if (uri->fragment != NULL) {
6957 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6958 "Attribute %s URI %s has a fragment ID\n",
6959 cur->name, val);
6960 }
6961 xmlFreeURI(uri);
6962 }
6963 }
6964 xmlFree(val);
6965 }
6966 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6967 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6968 "Unknown attribute %s on %s\n", cur->name,
6969 node->name);
6970 }
6971 }
6972 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006973 }
6974}
6975
6976/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006977 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006978 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006979 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006980 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006981 * Cleanup the subtree from unwanted nodes for parsing, resolve
6982 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006983 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006984static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006985xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6986{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006987 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006988
Daniel Veillard6eadf632003-01-23 18:29:16 +00006989 delete = NULL;
6990 cur = root;
6991 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006992 if (delete != NULL) {
6993 xmlUnlinkNode(delete);
6994 xmlFreeNode(delete);
6995 delete = NULL;
6996 }
6997 if (cur->type == XML_ELEMENT_NODE) {
6998 /*
6999 * Simplification 4.1. Annotations
7000 */
7001 if ((cur->ns == NULL) ||
7002 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
7003 if ((cur->parent != NULL) &&
7004 (cur->parent->type == XML_ELEMENT_NODE) &&
7005 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7006 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7007 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7008 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7009 "element %s doesn't allow foreign elements\n",
7010 cur->parent->name, NULL);
7011 }
7012 delete = cur;
7013 goto skip_children;
7014 } else {
7015 xmlRelaxNGCleanupAttributes(ctxt, cur);
7016 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7017 xmlChar *href, *ns, *base, *URL;
7018 xmlRelaxNGDocumentPtr docu;
7019 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00007020 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007021
Daniel Veillard4c004142003-10-07 11:33:24 +00007022 ns = xmlGetProp(cur, BAD_CAST "ns");
7023 if (ns == NULL) {
7024 tmp = cur->parent;
7025 while ((tmp != NULL) &&
7026 (tmp->type == XML_ELEMENT_NODE)) {
7027 ns = xmlGetProp(tmp, BAD_CAST "ns");
7028 if (ns != NULL)
7029 break;
7030 tmp = tmp->parent;
7031 }
7032 }
7033 href = xmlGetProp(cur, BAD_CAST "href");
7034 if (href == NULL) {
7035 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7036 "xmlRelaxNGParse: externalRef has no href attribute\n",
7037 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007038 if (ns != NULL)
7039 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007040 delete = cur;
7041 goto skip_children;
7042 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007043 uri = xmlParseURI((const char *) href);
7044 if (uri == NULL) {
7045 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7046 "Incorrect URI for externalRef %s\n",
7047 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007048 if (ns != NULL)
7049 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007050 if (href != NULL)
7051 xmlFree(href);
7052 delete = cur;
7053 goto skip_children;
7054 }
7055 if (uri->fragment != NULL) {
7056 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7057 "Fragment forbidden in URI for externalRef %s\n",
7058 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007059 if (ns != NULL)
7060 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007061 xmlFreeURI(uri);
7062 if (href != NULL)
7063 xmlFree(href);
7064 delete = cur;
7065 goto skip_children;
7066 }
7067 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007068 base = xmlNodeGetBase(cur->doc, cur);
7069 URL = xmlBuildURI(href, base);
7070 if (URL == NULL) {
7071 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7072 "Failed to compute URL for externalRef %s\n",
7073 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007074 if (ns != NULL)
7075 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007076 if (href != NULL)
7077 xmlFree(href);
7078 if (base != NULL)
7079 xmlFree(base);
7080 delete = cur;
7081 goto skip_children;
7082 }
7083 if (href != NULL)
7084 xmlFree(href);
7085 if (base != NULL)
7086 xmlFree(base);
7087 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7088 if (docu == NULL) {
7089 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7090 "Failed to load externalRef %s\n", URL,
7091 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007092 if (ns != NULL)
7093 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007094 xmlFree(URL);
7095 delete = cur;
7096 goto skip_children;
7097 }
7098 if (ns != NULL)
7099 xmlFree(ns);
7100 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007101 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007102 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7103 xmlChar *href, *ns, *base, *URL;
7104 xmlRelaxNGIncludePtr incl;
7105 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007106
Daniel Veillard4c004142003-10-07 11:33:24 +00007107 href = xmlGetProp(cur, BAD_CAST "href");
7108 if (href == NULL) {
7109 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7110 "xmlRelaxNGParse: include has no href attribute\n",
7111 NULL, NULL);
7112 delete = cur;
7113 goto skip_children;
7114 }
7115 base = xmlNodeGetBase(cur->doc, cur);
7116 URL = xmlBuildURI(href, base);
7117 if (URL == NULL) {
7118 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7119 "Failed to compute URL for include %s\n",
7120 href, NULL);
7121 if (href != NULL)
7122 xmlFree(href);
7123 if (base != NULL)
7124 xmlFree(base);
7125 delete = cur;
7126 goto skip_children;
7127 }
7128 if (href != NULL)
7129 xmlFree(href);
7130 if (base != NULL)
7131 xmlFree(base);
7132 ns = xmlGetProp(cur, BAD_CAST "ns");
7133 if (ns == NULL) {
7134 tmp = cur->parent;
7135 while ((tmp != NULL) &&
7136 (tmp->type == XML_ELEMENT_NODE)) {
7137 ns = xmlGetProp(tmp, BAD_CAST "ns");
7138 if (ns != NULL)
7139 break;
7140 tmp = tmp->parent;
7141 }
7142 }
7143 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7144 if (ns != NULL)
7145 xmlFree(ns);
7146 if (incl == NULL) {
7147 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7148 "Failed to load include %s\n", URL,
7149 NULL);
7150 xmlFree(URL);
7151 delete = cur;
7152 goto skip_children;
7153 }
7154 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007155 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007156 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7157 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7158 {
7159 xmlChar *name, *ns;
7160 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007161
Daniel Veillard4c004142003-10-07 11:33:24 +00007162 /*
7163 * Simplification 4.8. name attribute of element
7164 * and attribute elements
7165 */
7166 name = xmlGetProp(cur, BAD_CAST "name");
7167 if (name != NULL) {
7168 if (cur->children == NULL) {
7169 text =
7170 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7171 name);
7172 } else {
7173 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007174
Daniel Veillard03a53c32004-10-26 16:06:51 +00007175 node = xmlNewDocNode(cur->doc, cur->ns,
7176 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007177 if (node != NULL) {
7178 xmlAddPrevSibling(cur->children, node);
7179 text = xmlNewText(name);
7180 xmlAddChild(node, text);
7181 text = node;
7182 }
7183 }
7184 if (text == NULL) {
7185 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7186 "Failed to create a name %s element\n",
7187 name, NULL);
7188 }
7189 xmlUnsetProp(cur, BAD_CAST "name");
7190 xmlFree(name);
7191 ns = xmlGetProp(cur, BAD_CAST "ns");
7192 if (ns != NULL) {
7193 if (text != NULL) {
7194 xmlSetProp(text, BAD_CAST "ns", ns);
7195 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7196 }
7197 xmlFree(ns);
7198 } else if (xmlStrEqual(cur->name,
7199 BAD_CAST "attribute")) {
7200 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7201 }
7202 }
7203 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7204 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7205 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7206 /*
7207 * Simplification 4.8. name attribute of element
7208 * and attribute elements
7209 */
7210 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7211 xmlNodePtr node;
7212 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007213
Daniel Veillard4c004142003-10-07 11:33:24 +00007214 node = cur->parent;
7215 while ((node != NULL) &&
7216 (node->type == XML_ELEMENT_NODE)) {
7217 ns = xmlGetProp(node, BAD_CAST "ns");
7218 if (ns != NULL) {
7219 break;
7220 }
7221 node = node->parent;
7222 }
7223 if (ns == NULL) {
7224 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7225 } else {
7226 xmlSetProp(cur, BAD_CAST "ns", ns);
7227 xmlFree(ns);
7228 }
7229 }
7230 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7231 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007232
Daniel Veillard4c004142003-10-07 11:33:24 +00007233 /*
7234 * Simplification: 4.10. QNames
7235 */
7236 name = xmlNodeGetContent(cur);
7237 if (name != NULL) {
7238 local = xmlSplitQName2(name, &prefix);
7239 if (local != NULL) {
7240 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007241
Daniel Veillard4c004142003-10-07 11:33:24 +00007242 ns = xmlSearchNs(cur->doc, cur, prefix);
7243 if (ns == NULL) {
7244 xmlRngPErr(ctxt, cur,
7245 XML_RNGP_PREFIX_UNDEFINED,
7246 "xmlRelaxNGParse: no namespace for prefix %s\n",
7247 prefix, NULL);
7248 } else {
7249 xmlSetProp(cur, BAD_CAST "ns",
7250 ns->href);
7251 xmlNodeSetContent(cur, local);
7252 }
7253 xmlFree(local);
7254 xmlFree(prefix);
7255 }
7256 xmlFree(name);
7257 }
7258 }
7259 /*
7260 * 4.16
7261 */
7262 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7263 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7264 xmlRngPErr(ctxt, cur,
7265 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7266 "Found nsName/except//nsName forbidden construct\n",
7267 NULL, NULL);
7268 }
7269 }
7270 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7271 (cur != root)) {
7272 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007273
Daniel Veillard4c004142003-10-07 11:33:24 +00007274 /*
7275 * 4.16
7276 */
7277 if ((cur->parent != NULL) &&
7278 (xmlStrEqual
7279 (cur->parent->name, BAD_CAST "anyName"))) {
7280 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7281 xmlRelaxNGCleanupTree(ctxt, cur);
7282 ctxt->flags = oldflags;
7283 goto skip_children;
7284 } else if ((cur->parent != NULL) &&
7285 (xmlStrEqual
7286 (cur->parent->name, BAD_CAST "nsName"))) {
7287 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7288 xmlRelaxNGCleanupTree(ctxt, cur);
7289 ctxt->flags = oldflags;
7290 goto skip_children;
7291 }
7292 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7293 /*
7294 * 4.16
7295 */
7296 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7297 xmlRngPErr(ctxt, cur,
7298 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7299 "Found anyName/except//anyName forbidden construct\n",
7300 NULL, NULL);
7301 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7302 xmlRngPErr(ctxt, cur,
7303 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7304 "Found nsName/except//anyName forbidden construct\n",
7305 NULL, NULL);
7306 }
7307 }
7308 /*
7309 * Thisd is not an else since "include" is transformed
7310 * into a div
7311 */
7312 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7313 xmlChar *ns;
7314 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007315
Daniel Veillard4c004142003-10-07 11:33:24 +00007316 /*
7317 * implements rule 4.11
7318 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007319
Daniel Veillard4c004142003-10-07 11:33:24 +00007320 ns = xmlGetProp(cur, BAD_CAST "ns");
7321
7322 child = cur->children;
7323 ins = cur;
7324 while (child != NULL) {
7325 if (ns != NULL) {
7326 if (!xmlHasProp(child, BAD_CAST "ns")) {
7327 xmlSetProp(child, BAD_CAST "ns", ns);
7328 }
7329 }
7330 tmp = child->next;
7331 xmlUnlinkNode(child);
7332 ins = xmlAddNextSibling(ins, child);
7333 child = tmp;
7334 }
7335 if (ns != NULL)
7336 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007337 /*
7338 * Since we are about to delete cur, if it's nsDef is non-NULL we
7339 * need to preserve it (it contains the ns definitions for the
7340 * children we just moved). We'll just stick it on to the end
7341 * of cur->parent's list, since it's never going to be re-serialized
7342 * (bug 143738).
7343 */
7344 if (cur->nsDef != NULL) {
7345 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7346 while (parDef->next != NULL)
7347 parDef = parDef->next;
7348 parDef->next = cur->nsDef;
7349 cur->nsDef = NULL;
7350 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007351 delete = cur;
7352 goto skip_children;
7353 }
7354 }
7355 }
7356 /*
7357 * Simplification 4.2 whitespaces
7358 */
7359 else if ((cur->type == XML_TEXT_NODE) ||
7360 (cur->type == XML_CDATA_SECTION_NODE)) {
7361 if (IS_BLANK_NODE(cur)) {
7362 if (cur->parent->type == XML_ELEMENT_NODE) {
7363 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7364 &&
7365 (!xmlStrEqual
7366 (cur->parent->name, BAD_CAST "param")))
7367 delete = cur;
7368 } else {
7369 delete = cur;
7370 goto skip_children;
7371 }
7372 }
7373 } else {
7374 delete = cur;
7375 goto skip_children;
7376 }
7377
7378 /*
7379 * Skip to next node
7380 */
7381 if (cur->children != NULL) {
7382 if ((cur->children->type != XML_ENTITY_DECL) &&
7383 (cur->children->type != XML_ENTITY_REF_NODE) &&
7384 (cur->children->type != XML_ENTITY_NODE)) {
7385 cur = cur->children;
7386 continue;
7387 }
7388 }
7389 skip_children:
7390 if (cur->next != NULL) {
7391 cur = cur->next;
7392 continue;
7393 }
7394
7395 do {
7396 cur = cur->parent;
7397 if (cur == NULL)
7398 break;
7399 if (cur == root) {
7400 cur = NULL;
7401 break;
7402 }
7403 if (cur->next != NULL) {
7404 cur = cur->next;
7405 break;
7406 }
7407 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007408 }
7409 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007410 xmlUnlinkNode(delete);
7411 xmlFreeNode(delete);
7412 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007413 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007414}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007415
Daniel Veillardc5312d72003-02-21 17:14:10 +00007416/**
7417 * xmlRelaxNGCleanupDoc:
7418 * @ctxt: a Relax-NG parser context
7419 * @doc: an xmldocPtr document pointer
7420 *
7421 * Cleanup the document from unwanted nodes for parsing, resolve
7422 * Include and externalRef lookups.
7423 *
7424 * Returns the cleaned up document or NULL in case of error
7425 */
7426static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007427xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7428{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007429 xmlNodePtr root;
7430
7431 /*
7432 * Extract the root
7433 */
7434 root = xmlDocGetRootElement(doc);
7435 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007436 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7437 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007438 return (NULL);
7439 }
7440 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007441 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007442}
7443
7444/**
7445 * xmlRelaxNGParse:
7446 * @ctxt: a Relax-NG parser context
7447 *
7448 * parse a schema definition resource and build an internal
7449 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007450 *
7451 * Returns the internal XML RelaxNG structure built from the resource or
7452 * NULL in case of error
7453 */
7454xmlRelaxNGPtr
7455xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7456{
7457 xmlRelaxNGPtr ret = NULL;
7458 xmlDocPtr doc;
7459 xmlNodePtr root;
7460
7461 xmlRelaxNGInitTypes();
7462
7463 if (ctxt == NULL)
7464 return (NULL);
7465
7466 /*
7467 * First step is to parse the input document into an DOM/Infoset
7468 */
7469 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007470 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007471 if (doc == NULL) {
7472 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7473 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7474 NULL);
7475 return (NULL);
7476 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007477 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007478 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007479 if (doc == NULL) {
7480 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7481 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7482 NULL);
7483 return (NULL);
7484 }
7485 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7486 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007487 } else if (ctxt->document != NULL) {
7488 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007489 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007490 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7491 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7492 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007493 }
7494 ctxt->document = doc;
7495
7496 /*
7497 * Some preprocessing of the document content
7498 */
7499 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7500 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007501 xmlFreeDoc(ctxt->document);
7502 ctxt->document = NULL;
7503 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007504 }
7505
Daniel Veillard6eadf632003-01-23 18:29:16 +00007506 /*
7507 * Then do the parsing for good
7508 */
7509 root = xmlDocGetRootElement(doc);
7510 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007511 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7512 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007513 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillard3f845a92006-04-13 07:33:44 +00007514
7515 xmlFreeDoc(ctxt->document);
7516 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007517 return (NULL);
7518 }
7519 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007520 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007521 xmlFreeDoc(ctxt->document);
7522 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007523 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007524 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007525
7526 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007527 * Check the ref/defines links
7528 */
7529 /*
7530 * try to preprocess interleaves
7531 */
7532 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007533 xmlHashScan(ctxt->interleaves,
7534 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007535 }
7536
7537 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007538 * if there was a parsing error return NULL
7539 */
7540 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007541 xmlRelaxNGFree(ret);
7542 ctxt->document = NULL;
7543 xmlFreeDoc(doc);
7544 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007545 }
7546
7547 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007548 * try to compile (parts of) the schemas
7549 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007550 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7551 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007552 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007553
Daniel Veillard4c004142003-10-07 11:33:24 +00007554 def = xmlRelaxNGNewDefine(ctxt, NULL);
7555 if (def != NULL) {
7556 def->type = XML_RELAXNG_START;
7557 def->content = ret->topgrammar->start;
7558 ret->topgrammar->start = def;
7559 }
7560 }
7561 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007562 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007563
7564 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007565 * Transfer the pointer for cleanup at the schema level.
7566 */
7567 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007568 ctxt->document = NULL;
7569 ret->documents = ctxt->documents;
7570 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007571
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007572 ret->includes = ctxt->includes;
7573 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007574 ret->defNr = ctxt->defNr;
7575 ret->defTab = ctxt->defTab;
7576 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007577 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007578 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007579
7580 return (ret);
7581}
Daniel Veillard4c004142003-10-07 11:33:24 +00007582
Daniel Veillard6eadf632003-01-23 18:29:16 +00007583/**
7584 * xmlRelaxNGSetParserErrors:
7585 * @ctxt: a Relax-NG validation context
7586 * @err: the error callback
7587 * @warn: the warning callback
7588 * @ctx: contextual data for the callbacks
7589 *
7590 * Set the callback functions used to handle errors for a validation context
7591 */
7592void
7593xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007594 xmlRelaxNGValidityErrorFunc err,
7595 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7596{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007597 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007598 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007599 ctxt->error = err;
7600 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007601 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007602 ctxt->userData = ctx;
7603}
Daniel Veillard409a8142003-07-18 15:16:57 +00007604
7605/**
7606 * xmlRelaxNGGetParserErrors:
7607 * @ctxt: a Relax-NG validation context
7608 * @err: the error callback result
7609 * @warn: the warning callback result
7610 * @ctx: contextual data for the callbacks result
7611 *
7612 * Get the callback information used to handle errors for a validation context
7613 *
7614 * Returns -1 in case of failure, 0 otherwise.
7615 */
7616int
7617xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007618 xmlRelaxNGValidityErrorFunc * err,
7619 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7620{
Daniel Veillard409a8142003-07-18 15:16:57 +00007621 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007622 return (-1);
7623 if (err != NULL)
7624 *err = ctxt->error;
7625 if (warn != NULL)
7626 *warn = ctxt->warning;
7627 if (ctx != NULL)
7628 *ctx = ctxt->userData;
7629 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007630}
7631
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007632/**
7633 * xmlRelaxNGSetParserStructuredErrors:
7634 * @ctxt: a Relax-NG parser context
7635 * @serror: the error callback
7636 * @ctx: contextual data for the callbacks
7637 *
7638 * Set the callback functions used to handle errors for a parsing context
7639 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007640void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007641xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007642 xmlStructuredErrorFunc serror,
7643 void *ctx)
7644{
7645 if (ctxt == NULL)
7646 return;
7647 ctxt->serror = serror;
7648 ctxt->error = NULL;
7649 ctxt->warning = NULL;
7650 ctxt->userData = ctx;
7651}
7652
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007653#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007654
Daniel Veillard6eadf632003-01-23 18:29:16 +00007655/************************************************************************
7656 * *
7657 * Dump back a compiled form *
7658 * *
7659 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007660static void xmlRelaxNGDumpDefine(FILE * output,
7661 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007662
7663/**
7664 * xmlRelaxNGDumpDefines:
7665 * @output: the file output
7666 * @defines: a list of define structures
7667 *
7668 * Dump a RelaxNG structure back
7669 */
7670static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007671xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7672{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007673 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007674 xmlRelaxNGDumpDefine(output, defines);
7675 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007676 }
7677}
7678
7679/**
7680 * xmlRelaxNGDumpDefine:
7681 * @output: the file output
7682 * @define: a define structure
7683 *
7684 * Dump a RelaxNG structure back
7685 */
7686static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007687xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7688{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007689 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007690 return;
7691 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007692 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007693 fprintf(output, "<empty/>\n");
7694 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007695 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007696 fprintf(output, "<notAllowed/>\n");
7697 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007698 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007699 fprintf(output, "<text/>\n");
7700 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007701 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007702 fprintf(output, "<element>\n");
7703 if (define->name != NULL) {
7704 fprintf(output, "<name");
7705 if (define->ns != NULL)
7706 fprintf(output, " ns=\"%s\"", define->ns);
7707 fprintf(output, ">%s</name>\n", define->name);
7708 }
7709 xmlRelaxNGDumpDefines(output, define->attrs);
7710 xmlRelaxNGDumpDefines(output, define->content);
7711 fprintf(output, "</element>\n");
7712 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007713 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007714 fprintf(output, "<list>\n");
7715 xmlRelaxNGDumpDefines(output, define->content);
7716 fprintf(output, "</list>\n");
7717 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007718 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007719 fprintf(output, "<oneOrMore>\n");
7720 xmlRelaxNGDumpDefines(output, define->content);
7721 fprintf(output, "</oneOrMore>\n");
7722 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007723 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007724 fprintf(output, "<zeroOrMore>\n");
7725 xmlRelaxNGDumpDefines(output, define->content);
7726 fprintf(output, "</zeroOrMore>\n");
7727 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007728 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007729 fprintf(output, "<choice>\n");
7730 xmlRelaxNGDumpDefines(output, define->content);
7731 fprintf(output, "</choice>\n");
7732 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007733 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007734 fprintf(output, "<group>\n");
7735 xmlRelaxNGDumpDefines(output, define->content);
7736 fprintf(output, "</group>\n");
7737 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007738 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007739 fprintf(output, "<interleave>\n");
7740 xmlRelaxNGDumpDefines(output, define->content);
7741 fprintf(output, "</interleave>\n");
7742 break;
7743 case XML_RELAXNG_OPTIONAL:
7744 fprintf(output, "<optional>\n");
7745 xmlRelaxNGDumpDefines(output, define->content);
7746 fprintf(output, "</optional>\n");
7747 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007748 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007749 fprintf(output, "<attribute>\n");
7750 xmlRelaxNGDumpDefines(output, define->content);
7751 fprintf(output, "</attribute>\n");
7752 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007753 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007754 fprintf(output, "<define");
7755 if (define->name != NULL)
7756 fprintf(output, " name=\"%s\"", define->name);
7757 fprintf(output, ">\n");
7758 xmlRelaxNGDumpDefines(output, define->content);
7759 fprintf(output, "</define>\n");
7760 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007761 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007762 fprintf(output, "<ref");
7763 if (define->name != NULL)
7764 fprintf(output, " name=\"%s\"", define->name);
7765 fprintf(output, ">\n");
7766 xmlRelaxNGDumpDefines(output, define->content);
7767 fprintf(output, "</ref>\n");
7768 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007769 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007770 fprintf(output, "<parentRef");
7771 if (define->name != NULL)
7772 fprintf(output, " name=\"%s\"", define->name);
7773 fprintf(output, ">\n");
7774 xmlRelaxNGDumpDefines(output, define->content);
7775 fprintf(output, "</parentRef>\n");
7776 break;
7777 case XML_RELAXNG_EXTERNALREF:
7778 fprintf(output, "<externalRef>");
7779 xmlRelaxNGDumpDefines(output, define->content);
7780 fprintf(output, "</externalRef>\n");
7781 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007782 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007783 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007784 TODO break;
7785 case XML_RELAXNG_START:
7786 case XML_RELAXNG_EXCEPT:
7787 case XML_RELAXNG_PARAM:
7788 TODO break;
7789 case XML_RELAXNG_NOOP:
7790 xmlRelaxNGDumpDefines(output, define->content);
7791 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007792 }
7793}
Daniel Veillard4c004142003-10-07 11:33:24 +00007794
Daniel Veillard6eadf632003-01-23 18:29:16 +00007795/**
7796 * xmlRelaxNGDumpGrammar:
7797 * @output: the file output
7798 * @grammar: a grammar structure
7799 * @top: is this a top grammar
7800 *
7801 * Dump a RelaxNG structure back
7802 */
7803static void
7804xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7805{
7806 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007807 return;
7808
Daniel Veillard6eadf632003-01-23 18:29:16 +00007809 fprintf(output, "<grammar");
7810 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007811 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7812 switch (grammar->combine) {
7813 case XML_RELAXNG_COMBINE_UNDEFINED:
7814 break;
7815 case XML_RELAXNG_COMBINE_CHOICE:
7816 fprintf(output, " combine=\"choice\"");
7817 break;
7818 case XML_RELAXNG_COMBINE_INTERLEAVE:
7819 fprintf(output, " combine=\"interleave\"");
7820 break;
7821 default:
7822 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007823 }
7824 fprintf(output, ">\n");
7825 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007826 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007827 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007828 fprintf(output, "<start>\n");
7829 xmlRelaxNGDumpDefine(output, grammar->start);
7830 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007831 }
7832 /* TODO ? Dump the defines ? */
7833 fprintf(output, "</grammar>\n");
7834}
7835
7836/**
7837 * xmlRelaxNGDump:
7838 * @output: the file output
7839 * @schema: a schema structure
7840 *
7841 * Dump a RelaxNG structure back
7842 */
7843void
7844xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7845{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007846 if (output == NULL)
7847 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007848 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007849 fprintf(output, "RelaxNG empty or failed to compile\n");
7850 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007851 }
7852 fprintf(output, "RelaxNG: ");
7853 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007854 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007855 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007856 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007857 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007858 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007859 }
7860 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007861 fprintf(output, "RelaxNG has no top grammar\n");
7862 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007863 }
7864 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7865}
7866
Daniel Veillardfebcca42003-02-16 15:44:18 +00007867/**
7868 * xmlRelaxNGDumpTree:
7869 * @output: the file output
7870 * @schema: a schema structure
7871 *
7872 * Dump the transformed RelaxNG tree.
7873 */
7874void
7875xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7876{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007877 if (output == NULL)
7878 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007879 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007880 fprintf(output, "RelaxNG empty or failed to compile\n");
7881 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007882 }
7883 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007884 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007885 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007886 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007887 }
7888}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007889#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007890
Daniel Veillard6eadf632003-01-23 18:29:16 +00007891/************************************************************************
7892 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007893 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007894 * *
7895 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007896static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7897 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007898
7899/**
7900 * xmlRelaxNGValidateCompiledCallback:
7901 * @exec: the regular expression instance
7902 * @token: the token which matched
7903 * @transdata: callback data, the define for the subelement if available
7904 @ @inputdata: callback data, the Relax NG validation context
7905 *
7906 * Handle the callback and if needed validate the element children.
7907 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007908static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007909xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007910 const xmlChar * token,
7911 void *transdata, void *inputdata)
7912{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007913 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7914 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7915 int ret;
7916
7917#ifdef DEBUG_COMPILE
7918 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007919 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007920#endif
7921 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007922 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007923 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007924 }
7925 if (define == NULL) {
7926 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007927 return;
7928 fprintf(stderr, "callback on %s missing define\n", token);
7929 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7930 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7931 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007932 }
7933 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007934 fprintf(stderr, "callback on %s missing info\n", token);
7935 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7936 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7937 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007938 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007939 fprintf(stderr, "callback on %s define is not element\n", token);
7940 if (ctxt->errNo == XML_RELAXNG_OK)
7941 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7942 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007943 }
7944 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007945 if (ret != 0)
7946 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007947}
7948
7949/**
7950 * xmlRelaxNGValidateCompiledContent:
7951 * @ctxt: the RelaxNG validation context
7952 * @regexp: the regular expression as compiled
7953 * @content: list of children to test against the regexp
7954 *
7955 * Validate the content model of an element or start using the regexp
7956 *
7957 * Returns 0 in case of success, -1 in case of error.
7958 */
7959static int
7960xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007961 xmlRegexpPtr regexp, xmlNodePtr content)
7962{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007963 xmlRegExecCtxtPtr exec;
7964 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007965 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007966 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007967
7968 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007969 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007970 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007971 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007972 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007973 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007974 cur = content;
7975 while (cur != NULL) {
7976 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007977 switch (cur->type) {
7978 case XML_TEXT_NODE:
7979 case XML_CDATA_SECTION_NODE:
7980 if (xmlIsBlankNode(cur))
7981 break;
7982 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7983 if (ret < 0) {
7984 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7985 cur->parent->name);
7986 }
7987 break;
7988 case XML_ELEMENT_NODE:
7989 if (cur->ns != NULL) {
7990 ret = xmlRegExecPushString2(exec, cur->name,
7991 cur->ns->href, ctxt);
7992 } else {
7993 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7994 }
7995 if (ret < 0) {
7996 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7997 }
7998 break;
7999 default:
8000 break;
8001 }
8002 if (ret < 0)
8003 break;
8004 /*
8005 * Switch to next element
8006 */
8007 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008008 }
8009 ret = xmlRegExecPushString(exec, NULL, NULL);
8010 if (ret == 1) {
8011 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008012 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008013 } else if (ret == 0) {
8014 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008015 * TODO: get some of the names needed to exit the current state of exec
8016 */
8017 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8018 ret = -1;
8019 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8020 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008021 } else {
8022 ret = -1;
8023 }
8024 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008025 /*
8026 * There might be content model errors outside of the pure
8027 * regexp validation, e.g. for attribute values.
8028 */
8029 if ((ret == 0) && (ctxt->perr != 0)) {
8030 ret = ctxt->perr;
8031 }
8032 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008033 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008034}
8035
8036/************************************************************************
8037 * *
8038 * Progressive validation of when possible *
8039 * *
8040 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008041static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8042 xmlRelaxNGDefinePtr defines);
8043static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008044 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008045static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008046
8047/**
8048 * xmlRelaxNGElemPush:
8049 * @ctxt: the validation context
8050 * @exec: the regexp runtime for the new content model
8051 *
8052 * Push a new regexp for the current node content model on the stack
8053 *
8054 * Returns 0 in case of success and -1 in case of error.
8055 */
8056static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008057xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8058{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008059 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008060 ctxt->elemMax = 10;
8061 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8062 sizeof
8063 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008064 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008065 xmlRngVErrMemory(ctxt, "validating\n");
8066 return (-1);
8067 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008068 }
8069 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008070 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008071 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008072 ctxt->elemMax *
8073 sizeof
8074 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008075 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008076 xmlRngVErrMemory(ctxt, "validating\n");
8077 return (-1);
8078 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008079 }
8080 ctxt->elemTab[ctxt->elemNr++] = exec;
8081 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008082 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008083}
8084
8085/**
8086 * xmlRelaxNGElemPop:
8087 * @ctxt: the validation context
8088 *
8089 * Pop the regexp of the current node content model from the stack
8090 *
8091 * Returns the exec or NULL if empty
8092 */
8093static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008094xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8095{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008096 xmlRegExecCtxtPtr ret;
8097
Daniel Veillard4c004142003-10-07 11:33:24 +00008098 if (ctxt->elemNr <= 0)
8099 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008100 ctxt->elemNr--;
8101 ret = ctxt->elemTab[ctxt->elemNr];
8102 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008103 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008104 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8105 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008106 ctxt->elem = NULL;
8107 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008108}
8109
8110/**
8111 * xmlRelaxNGValidateProgressiveCallback:
8112 * @exec: the regular expression instance
8113 * @token: the token which matched
8114 * @transdata: callback data, the define for the subelement if available
8115 @ @inputdata: callback data, the Relax NG validation context
8116 *
8117 * Handle the callback and if needed validate the element children.
8118 * some of the in/out informations are passed via the context in @inputdata.
8119 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008120static void
8121xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8122 ATTRIBUTE_UNUSED,
8123 const xmlChar * token,
8124 void *transdata, void *inputdata)
8125{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008126 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8127 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008128 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008129 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008130 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008131
8132#ifdef DEBUG_PROGRESSIVE
8133 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008134 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008135#endif
8136 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008137 fprintf(stderr, "callback on %s missing context\n", token);
8138 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008139 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008140 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008141 ctxt->pstate = 1;
8142 if (define == NULL) {
8143 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008144 return;
8145 fprintf(stderr, "callback on %s missing define\n", token);
8146 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8147 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8148 ctxt->pstate = -1;
8149 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008150 }
8151 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008152 fprintf(stderr, "callback on %s missing info\n", token);
8153 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8154 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8155 ctxt->pstate = -1;
8156 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008157 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008158 fprintf(stderr, "callback on %s define is not element\n", token);
8159 if (ctxt->errNo == XML_RELAXNG_OK)
8160 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8161 ctxt->pstate = -1;
8162 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008163 }
8164 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008165 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8166 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8167 xmlRelaxNGDumpValidError(ctxt);
8168 ctxt->pstate = -1;
8169 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008170 }
8171 if (define->contModel == NULL) {
8172 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008173 * this node cannot be validated in a streamable fashion
8174 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008175#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008176 xmlGenericError(xmlGenericErrorContext,
8177 "Element '%s' validation is not streamable\n",
8178 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008179#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008180 ctxt->pstate = 0;
8181 ctxt->pdef = define;
8182 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008183 }
8184 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008185 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008186 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008187 ctxt->pstate = -1;
8188 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008189 }
8190 xmlRelaxNGElemPush(ctxt, exec);
8191
8192 /*
8193 * Validate the attributes part of the content.
8194 */
8195 state = xmlRelaxNGNewValidState(ctxt, node);
8196 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008197 ctxt->pstate = -1;
8198 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008199 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008200 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008201 ctxt->state = state;
8202 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008203 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8204 if (ret != 0) {
8205 ctxt->pstate = -1;
8206 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8207 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008208 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008209 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008210 ctxt->state->seq = NULL;
8211 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8212 if (ret != 0) {
8213 ctxt->pstate = -1;
8214 }
8215 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008216 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008217 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008218
8219 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008220
Daniel Veillard4c004142003-10-07 11:33:24 +00008221 for (i = 0; i < ctxt->states->nbState; i++) {
8222 state = ctxt->states->tabState[i];
8223 ctxt->state = state;
8224 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008225
Daniel Veillard4c004142003-10-07 11:33:24 +00008226 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8227 tmp = 0;
8228 break;
8229 }
8230 }
8231 if (tmp != 0) {
8232 /*
8233 * validation error, log the message for the "best" one
8234 */
8235 ctxt->flags |= FLAGS_IGNORABLE;
8236 xmlRelaxNGLogBestError(ctxt);
8237 }
8238 for (i = 0; i < ctxt->states->nbState; i++) {
8239 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8240 }
8241 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8242 ctxt->states = NULL;
8243 if ((ret == 0) && (tmp == -1))
8244 ctxt->pstate = -1;
8245 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008246 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008247 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008248 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8249 xmlRelaxNGDumpValidError(ctxt);
8250 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008251 }
8252 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008253}
8254
8255/**
8256 * xmlRelaxNGValidatePushElement:
8257 * @ctxt: the validation context
8258 * @doc: a document instance
8259 * @elem: an element instance
8260 *
8261 * Push a new element start on the RelaxNG validation stack.
8262 *
8263 * returns 1 if no validation problem was found or 0 if validating the
8264 * element requires a full node, and -1 in case of error.
8265 */
8266int
Daniel Veillard33300b42003-04-17 09:09:19 +00008267xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8268 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008269 xmlNodePtr elem)
8270{
8271 int ret = 1;
8272
8273 if ((ctxt == NULL) || (elem == NULL))
8274 return (-1);
8275
8276#ifdef DEBUG_PROGRESSIVE
8277 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8278#endif
8279 if (ctxt->elem == 0) {
8280 xmlRelaxNGPtr schema;
8281 xmlRelaxNGGrammarPtr grammar;
8282 xmlRegExecCtxtPtr exec;
8283 xmlRelaxNGDefinePtr define;
8284
8285 schema = ctxt->schema;
8286 if (schema == NULL) {
8287 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8288 return (-1);
8289 }
8290 grammar = schema->topgrammar;
8291 if ((grammar == NULL) || (grammar->start == NULL)) {
8292 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8293 return (-1);
8294 }
8295 define = grammar->start;
8296 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008297 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008298 return (0);
8299 }
8300 exec = xmlRegNewExecCtxt(define->contModel,
8301 xmlRelaxNGValidateProgressiveCallback,
8302 ctxt);
8303 if (exec == NULL) {
8304 return (-1);
8305 }
8306 xmlRelaxNGElemPush(ctxt, exec);
8307 }
8308 ctxt->pnode = elem;
8309 ctxt->pstate = 0;
8310 if (elem->ns != NULL) {
8311 ret =
8312 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8313 ctxt);
8314 } else {
8315 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8316 }
8317 if (ret < 0) {
8318 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8319 } else {
8320 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008321 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008322 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008323 ret = -1;
8324 else
8325 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008326 }
8327#ifdef DEBUG_PROGRESSIVE
8328 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008329 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8330 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008331#endif
8332 return (ret);
8333}
8334
8335/**
8336 * xmlRelaxNGValidatePushCData:
8337 * @ctxt: the RelaxNG validation context
8338 * @data: some character data read
8339 * @len: the lenght of the data
8340 *
8341 * check the CData parsed for validation in the current stack
8342 *
8343 * returns 1 if no validation problem was found or -1 otherwise
8344 */
8345int
8346xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008347 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008348{
8349 int ret = 1;
8350
8351 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8352 return (-1);
8353
8354#ifdef DEBUG_PROGRESSIVE
8355 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8356#endif
8357
8358 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008359 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008360 break;
8361 data++;
8362 }
8363 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008364 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008365
8366 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8367 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008368 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008369#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008370 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008371#endif
8372
Daniel Veillard4c004142003-10-07 11:33:24 +00008373 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008374 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008375 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008376}
8377
8378/**
8379 * xmlRelaxNGValidatePopElement:
8380 * @ctxt: the RelaxNG validation context
8381 * @doc: a document instance
8382 * @elem: an element instance
8383 *
8384 * Pop the element end from the RelaxNG validation stack.
8385 *
8386 * returns 1 if no validation problem was found or 0 otherwise
8387 */
8388int
8389xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8390 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008391 xmlNodePtr elem)
8392{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008393 int ret;
8394 xmlRegExecCtxtPtr exec;
8395
Daniel Veillard4c004142003-10-07 11:33:24 +00008396 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8397 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008398#ifdef DEBUG_PROGRESSIVE
8399 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8400#endif
8401 /*
8402 * verify that we reached a terminal state of the content model.
8403 */
8404 exec = xmlRelaxNGElemPop(ctxt);
8405 ret = xmlRegExecPushString(exec, NULL, NULL);
8406 if (ret == 0) {
8407 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008408 * TODO: get some of the names needed to exit the current state of exec
8409 */
8410 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8411 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008412 } else if (ret < 0) {
8413 ret = -1;
8414 } else {
8415 ret = 1;
8416 }
8417 xmlRegFreeExecCtxt(exec);
8418#ifdef DEBUG_PROGRESSIVE
8419 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008420 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8421 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008422#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008423 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008424}
8425
8426/**
8427 * xmlRelaxNGValidateFullElement:
8428 * @ctxt: the validation context
8429 * @doc: a document instance
8430 * @elem: an element instance
8431 *
8432 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8433 * 0 and the content of the node has been expanded.
8434 *
8435 * returns 1 if no validation problem was found or -1 in case of error.
8436 */
8437int
Daniel Veillard33300b42003-04-17 09:09:19 +00008438xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8439 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008440 xmlNodePtr elem)
8441{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008442 int ret;
8443 xmlRelaxNGValidStatePtr state;
8444
Daniel Veillard4c004142003-10-07 11:33:24 +00008445 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8446 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008447#ifdef DEBUG_PROGRESSIVE
8448 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8449#endif
8450 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8451 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008452 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008453 }
8454 state->seq = elem;
8455 ctxt->state = state;
8456 ctxt->errNo = XML_RELAXNG_OK;
8457 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008458 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8459 ret = -1;
8460 else
8461 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008462 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008463 ctxt->state = NULL;
8464#ifdef DEBUG_PROGRESSIVE
8465 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008466 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8467 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008468#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008469 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008470}
8471
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008472/************************************************************************
8473 * *
8474 * Generic interpreted validation implementation *
8475 * *
8476 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008477static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8478 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008479
8480/**
8481 * xmlRelaxNGSkipIgnored:
8482 * @ctxt: a schema validation context
8483 * @node: the top node.
8484 *
8485 * Skip ignorable nodes in that context
8486 *
8487 * Returns the new sibling or NULL in case of error.
8488 */
8489static xmlNodePtr
8490xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008491 xmlNodePtr node)
8492{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008493 /*
8494 * TODO complete and handle entities
8495 */
8496 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008497 ((node->type == XML_COMMENT_NODE) ||
8498 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008499 (node->type == XML_XINCLUDE_START) ||
8500 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008501 (((node->type == XML_TEXT_NODE) ||
8502 (node->type == XML_CDATA_SECTION_NODE)) &&
8503 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8504 (IS_BLANK_NODE(node)))))) {
8505 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008506 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008507 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008508}
8509
8510/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008511 * xmlRelaxNGNormalize:
8512 * @ctxt: a schema validation context
8513 * @str: the string to normalize
8514 *
8515 * Implements the normalizeWhiteSpace( s ) function from
8516 * section 6.2.9 of the spec
8517 *
8518 * Returns the new string or NULL in case of error.
8519 */
8520static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008521xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8522{
Daniel Veillardedc91922003-01-26 00:52:04 +00008523 xmlChar *ret, *p;
8524 const xmlChar *tmp;
8525 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008526
Daniel Veillardedc91922003-01-26 00:52:04 +00008527 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008528 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008529 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008530 while (*tmp != 0)
8531 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008532 len = tmp - str;
8533
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008534 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008535 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008536 xmlRngVErrMemory(ctxt, "validating\n");
8537 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008538 }
8539 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008540 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008541 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008542 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008543 if (IS_BLANK_CH(*str)) {
8544 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008545 str++;
8546 if (*str == 0)
8547 break;
8548 *p++ = ' ';
8549 } else
8550 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008551 }
8552 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008553 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008554}
8555
8556/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008557 * xmlRelaxNGValidateDatatype:
8558 * @ctxt: a Relax-NG validation context
8559 * @value: the string value
8560 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008561 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008562 *
8563 * Validate the given value against the dataype
8564 *
8565 * Returns 0 if the validation succeeded or an error code.
8566 */
8567static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008568xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8569 const xmlChar * value,
8570 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8571{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008572 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008573 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008574 void *result = NULL;
8575 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008576
8577 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008578 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008579 }
8580 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008581 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008582 if ((define->attrs != NULL) &&
8583 (define->attrs->type == XML_RELAXNG_PARAM)) {
8584 ret =
8585 lib->check(lib->data, define->name, value, &result, node);
8586 } else {
8587 ret = lib->check(lib->data, define->name, value, NULL, node);
8588 }
8589 } else
8590 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008591 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008592 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8593 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8594 lib->freef(lib->data, result);
8595 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008596 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008597 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008598 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008599 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008600 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008601 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8602 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008603 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008604 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008605 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008606 if (lib->facet != NULL) {
8607 tmp = lib->facet(lib->data, define->name, cur->name,
8608 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008609 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008610 ret = -1;
8611 }
8612 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008613 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008614 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008615 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008616
Daniel Veillard4c004142003-10-07 11:33:24 +00008617 oldvalue = ctxt->state->value;
8618 oldendvalue = ctxt->state->endvalue;
8619 ctxt->state->value = (xmlChar *) value;
8620 ctxt->state->endvalue = NULL;
8621 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8622 ctxt->state->value = (xmlChar *) oldvalue;
8623 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008624 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008625 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008626 lib->freef(lib->data, result);
8627 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008628}
8629
8630/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008631 * xmlRelaxNGNextValue:
8632 * @ctxt: a Relax-NG validation context
8633 *
8634 * Skip to the next value when validating within a list
8635 *
8636 * Returns 0 if the operation succeeded or an error code.
8637 */
8638static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008639xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8640{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008641 xmlChar *cur;
8642
8643 cur = ctxt->state->value;
8644 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008645 ctxt->state->value = NULL;
8646 ctxt->state->endvalue = NULL;
8647 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008648 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008649 while (*cur != 0)
8650 cur++;
8651 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8652 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008653 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008654 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008655 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008656 ctxt->state->value = cur;
8657 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008658}
8659
8660/**
8661 * xmlRelaxNGValidateValueList:
8662 * @ctxt: a Relax-NG validation context
8663 * @defines: the list of definitions to verify
8664 *
8665 * Validate the given set of definitions for the current value
8666 *
8667 * Returns 0 if the validation succeeded or an error code.
8668 */
8669static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008670xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8671 xmlRelaxNGDefinePtr defines)
8672{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008673 int ret = 0;
8674
8675 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008676 ret = xmlRelaxNGValidateValue(ctxt, defines);
8677 if (ret != 0)
8678 break;
8679 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008680 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008681 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008682}
8683
8684/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008685 * xmlRelaxNGValidateValue:
8686 * @ctxt: a Relax-NG validation context
8687 * @define: the definition to verify
8688 *
8689 * Validate the given definition for the current value
8690 *
8691 * Returns 0 if the validation succeeded or an error code.
8692 */
8693static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008694xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8695 xmlRelaxNGDefinePtr define)
8696{
Daniel Veillardedc91922003-01-26 00:52:04 +00008697 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008698 xmlChar *value;
8699
8700 value = ctxt->state->value;
8701 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008702 case XML_RELAXNG_EMPTY:{
8703 if ((value != NULL) && (value[0] != 0)) {
8704 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008705
William M. Brack76e95df2003-10-18 16:20:14 +00008706 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008707 idx++;
8708 if (value[idx] != 0)
8709 ret = -1;
8710 }
8711 break;
8712 }
8713 case XML_RELAXNG_TEXT:
8714 break;
8715 case XML_RELAXNG_VALUE:{
8716 if (!xmlStrEqual(value, define->value)) {
8717 if (define->name != NULL) {
8718 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008719
Daniel Veillard4c004142003-10-07 11:33:24 +00008720 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8721 if ((lib != NULL) && (lib->comp != NULL)) {
8722 ret = lib->comp(lib->data, define->name,
8723 define->value, define->node,
8724 (void *) define->attrs,
8725 value, ctxt->state->node);
8726 } else
8727 ret = -1;
8728 if (ret < 0) {
8729 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8730 define->name);
8731 return (-1);
8732 } else if (ret == 1) {
8733 ret = 0;
8734 } else {
8735 ret = -1;
8736 }
8737 } else {
8738 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008739
Daniel Veillard4c004142003-10-07 11:33:24 +00008740 /*
8741 * TODO: trivial optimizations are possible by
8742 * computing at compile-time
8743 */
8744 nval = xmlRelaxNGNormalize(ctxt, define->value);
8745 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008746
Daniel Veillard4c004142003-10-07 11:33:24 +00008747 if ((nval == NULL) || (nvalue == NULL) ||
8748 (!xmlStrEqual(nval, nvalue)))
8749 ret = -1;
8750 if (nval != NULL)
8751 xmlFree(nval);
8752 if (nvalue != NULL)
8753 xmlFree(nvalue);
8754 }
8755 }
8756 if (ret == 0)
8757 xmlRelaxNGNextValue(ctxt);
8758 break;
8759 }
8760 case XML_RELAXNG_DATATYPE:{
8761 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8762 ctxt->state->seq);
8763 if (ret == 0)
8764 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008765
Daniel Veillard4c004142003-10-07 11:33:24 +00008766 break;
8767 }
8768 case XML_RELAXNG_CHOICE:{
8769 xmlRelaxNGDefinePtr list = define->content;
8770 xmlChar *oldvalue;
8771
8772 oldflags = ctxt->flags;
8773 ctxt->flags |= FLAGS_IGNORABLE;
8774
8775 oldvalue = ctxt->state->value;
8776 while (list != NULL) {
8777 ret = xmlRelaxNGValidateValue(ctxt, list);
8778 if (ret == 0) {
8779 break;
8780 }
8781 ctxt->state->value = oldvalue;
8782 list = list->next;
8783 }
8784 ctxt->flags = oldflags;
8785 if (ret != 0) {
8786 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8787 xmlRelaxNGDumpValidError(ctxt);
8788 } else {
8789 if (ctxt->errNr > 0)
8790 xmlRelaxNGPopErrors(ctxt, 0);
8791 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008792 break;
8793 }
8794 case XML_RELAXNG_LIST:{
8795 xmlRelaxNGDefinePtr list = define->content;
8796 xmlChar *oldvalue, *oldend, *val, *cur;
8797
Daniel Veillard416589a2003-02-17 17:25:42 +00008798#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008799 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008800#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008801
Daniel Veillard4c004142003-10-07 11:33:24 +00008802 oldvalue = ctxt->state->value;
8803 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008804
Daniel Veillard4c004142003-10-07 11:33:24 +00008805 val = xmlStrdup(oldvalue);
8806 if (val == NULL) {
8807 val = xmlStrdup(BAD_CAST "");
8808 }
8809 if (val == NULL) {
8810 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8811 return (-1);
8812 }
8813 cur = val;
8814 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008815 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008816 *cur = 0;
8817 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008818#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008819 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008820#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008821 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008822 *cur++ = 0;
8823 } else
8824 cur++;
8825 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008826#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008827 xmlGenericError(xmlGenericErrorContext,
8828 "list value: '%s' found %d items\n",
8829 oldvalue, nb_values);
8830 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008831#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008832 ctxt->state->endvalue = cur;
8833 cur = val;
8834 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8835 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008836
Daniel Veillard4c004142003-10-07 11:33:24 +00008837 ctxt->state->value = cur;
8838
8839 while (list != NULL) {
8840 if (ctxt->state->value == ctxt->state->endvalue)
8841 ctxt->state->value = NULL;
8842 ret = xmlRelaxNGValidateValue(ctxt, list);
8843 if (ret != 0) {
8844#ifdef DEBUG_LIST
8845 xmlGenericError(xmlGenericErrorContext,
8846 "Failed to validate value: '%s' with %d rule\n",
8847 ctxt->state->value, nb_values);
8848#endif
8849 break;
8850 }
8851#ifdef DEBUG_LIST
8852 nb_values++;
8853#endif
8854 list = list->next;
8855 }
8856
8857 if ((ret == 0) && (ctxt->state->value != NULL) &&
8858 (ctxt->state->value != ctxt->state->endvalue)) {
8859 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8860 ctxt->state->value);
8861 ret = -1;
8862 }
8863 xmlFree(val);
8864 ctxt->state->value = oldvalue;
8865 ctxt->state->endvalue = oldend;
8866 break;
8867 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008868 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008869 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8870 if (ret != 0) {
8871 break;
8872 }
8873 /* no break on purpose */
8874 case XML_RELAXNG_ZEROORMORE:{
8875 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008876
Daniel Veillard4c004142003-10-07 11:33:24 +00008877 oldflags = ctxt->flags;
8878 ctxt->flags |= FLAGS_IGNORABLE;
8879 cur = ctxt->state->value;
8880 temp = NULL;
8881 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8882 (temp != cur)) {
8883 temp = cur;
8884 ret =
8885 xmlRelaxNGValidateValueList(ctxt, define->content);
8886 if (ret != 0) {
8887 ctxt->state->value = temp;
8888 ret = 0;
8889 break;
8890 }
8891 cur = ctxt->state->value;
8892 }
8893 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008894 if (ctxt->errNr > 0)
8895 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008896 break;
8897 }
8898 case XML_RELAXNG_EXCEPT:{
8899 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008900
Daniel Veillard4c004142003-10-07 11:33:24 +00008901 list = define->content;
8902 while (list != NULL) {
8903 ret = xmlRelaxNGValidateValue(ctxt, list);
8904 if (ret == 0) {
8905 ret = -1;
8906 break;
8907 } else
8908 ret = 0;
8909 list = list->next;
8910 }
8911 break;
8912 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008913 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008914 case XML_RELAXNG_GROUP:{
8915 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008916
Daniel Veillard4c004142003-10-07 11:33:24 +00008917 list = define->content;
8918 while (list != NULL) {
8919 ret = xmlRelaxNGValidateValue(ctxt, list);
8920 if (ret != 0) {
8921 ret = -1;
8922 break;
8923 } else
8924 ret = 0;
8925 list = list->next;
8926 }
8927 break;
8928 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008929 case XML_RELAXNG_REF:
8930 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008931 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008932 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8933 ret = -1;
8934 } else {
8935 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8936 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008937 break;
8938 default:
8939 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008940 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008941 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008942}
8943
8944/**
8945 * xmlRelaxNGValidateValueContent:
8946 * @ctxt: a Relax-NG validation context
8947 * @defines: the list of definitions to verify
8948 *
8949 * Validate the given definitions for the current value
8950 *
8951 * Returns 0 if the validation succeeded or an error code.
8952 */
8953static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008954xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8955 xmlRelaxNGDefinePtr defines)
8956{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008957 int ret = 0;
8958
8959 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008960 ret = xmlRelaxNGValidateValue(ctxt, defines);
8961 if (ret != 0)
8962 break;
8963 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008964 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008965 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008966}
8967
8968/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008969 * xmlRelaxNGAttributeMatch:
8970 * @ctxt: a Relax-NG validation context
8971 * @define: the definition to check
8972 * @prop: the attribute
8973 *
8974 * Check if the attribute matches the definition nameClass
8975 *
8976 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8977 */
8978static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008979xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8980 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8981{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008982 int ret;
8983
8984 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008985 if (!xmlStrEqual(define->name, prop->name))
8986 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008987 }
8988 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008989 if (define->ns[0] == 0) {
8990 if (prop->ns != NULL)
8991 return (0);
8992 } else {
8993 if ((prop->ns == NULL) ||
8994 (!xmlStrEqual(define->ns, prop->ns->href)))
8995 return (0);
8996 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008997 }
8998 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008999 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009000 define = define->nameClass;
9001 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009002 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009003
Daniel Veillard4c004142003-10-07 11:33:24 +00009004 list = define->content;
9005 while (list != NULL) {
9006 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9007 if (ret == 1)
9008 return (0);
9009 if (ret < 0)
9010 return (ret);
9011 list = list->next;
9012 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009013 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009014 TODO}
9015 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009016}
9017
9018/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009019 * xmlRelaxNGValidateAttribute:
9020 * @ctxt: a Relax-NG validation context
9021 * @define: the definition to verify
9022 *
9023 * Validate the given attribute definition for that node
9024 *
9025 * Returns 0 if the validation succeeded or an error code.
9026 */
9027static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009028xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9029 xmlRelaxNGDefinePtr define)
9030{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009031 int ret = 0, i;
9032 xmlChar *value, *oldvalue;
9033 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009034 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009035
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009036 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009037 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009038 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009039 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9040 tmp = ctxt->state->attrs[i];
9041 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9042 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9043 (tmp->ns == NULL)) ||
9044 ((tmp->ns != NULL) &&
9045 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9046 prop = tmp;
9047 break;
9048 }
9049 }
9050 }
9051 if (prop != NULL) {
9052 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9053 oldvalue = ctxt->state->value;
9054 oldseq = ctxt->state->seq;
9055 ctxt->state->seq = (xmlNodePtr) prop;
9056 ctxt->state->value = value;
9057 ctxt->state->endvalue = NULL;
9058 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9059 if (ctxt->state->value != NULL)
9060 value = ctxt->state->value;
9061 if (value != NULL)
9062 xmlFree(value);
9063 ctxt->state->value = oldvalue;
9064 ctxt->state->seq = oldseq;
9065 if (ret == 0) {
9066 /*
9067 * flag the attribute as processed
9068 */
9069 ctxt->state->attrs[i] = NULL;
9070 ctxt->state->nbAttrLeft--;
9071 }
9072 } else {
9073 ret = -1;
9074 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009075#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009076 xmlGenericError(xmlGenericErrorContext,
9077 "xmlRelaxNGValidateAttribute(%s): %d\n",
9078 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009079#endif
9080 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009081 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9082 tmp = ctxt->state->attrs[i];
9083 if ((tmp != NULL) &&
9084 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9085 prop = tmp;
9086 break;
9087 }
9088 }
9089 if (prop != NULL) {
9090 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9091 oldvalue = ctxt->state->value;
9092 oldseq = ctxt->state->seq;
9093 ctxt->state->seq = (xmlNodePtr) prop;
9094 ctxt->state->value = value;
9095 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9096 if (ctxt->state->value != NULL)
9097 value = ctxt->state->value;
9098 if (value != NULL)
9099 xmlFree(value);
9100 ctxt->state->value = oldvalue;
9101 ctxt->state->seq = oldseq;
9102 if (ret == 0) {
9103 /*
9104 * flag the attribute as processed
9105 */
9106 ctxt->state->attrs[i] = NULL;
9107 ctxt->state->nbAttrLeft--;
9108 }
9109 } else {
9110 ret = -1;
9111 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009112#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009113 if (define->ns != NULL) {
9114 xmlGenericError(xmlGenericErrorContext,
9115 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9116 define->ns, ret);
9117 } else {
9118 xmlGenericError(xmlGenericErrorContext,
9119 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9120 ret);
9121 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009122#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009123 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009124
9125 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009126}
9127
9128/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009129 * xmlRelaxNGValidateAttributeList:
9130 * @ctxt: a Relax-NG validation context
9131 * @define: the list of definition to verify
9132 *
9133 * Validate the given node against the list of attribute definitions
9134 *
9135 * Returns 0 if the validation succeeded or an error code.
9136 */
9137static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009138xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9139 xmlRelaxNGDefinePtr defines)
9140{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009141 int ret = 0, res;
9142 int needmore = 0;
9143 xmlRelaxNGDefinePtr cur;
9144
9145 cur = defines;
9146 while (cur != NULL) {
9147 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009148 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9149 ret = -1;
9150 } else
9151 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009152 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009153 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009154 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009155 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009156 cur = defines;
9157 while (cur != NULL) {
9158 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009159 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9160 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9161 if (res < 0)
9162 ret = -1;
9163 } else {
9164 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9165 return (-1);
9166 }
9167 if (res == -1) /* continues on -2 */
9168 break;
9169 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009170 cur = cur->next;
9171 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009172
9173 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009174}
9175
9176/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009177 * xmlRelaxNGNodeMatchesList:
9178 * @node: the node
9179 * @list: a NULL terminated array of definitions
9180 *
9181 * Check if a node can be matched by one of the definitions
9182 *
9183 * Returns 1 if matches 0 otherwise
9184 */
9185static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009186xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9187{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009188 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009189 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009190
9191 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009192 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009193
9194 cur = list[i++];
9195 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009196 if ((node->type == XML_ELEMENT_NODE) &&
9197 (cur->type == XML_RELAXNG_ELEMENT)) {
9198 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9199 if (tmp == 1)
9200 return (1);
9201 } else if (((node->type == XML_TEXT_NODE) ||
9202 (node->type == XML_CDATA_SECTION_NODE)) &&
9203 (cur->type == XML_RELAXNG_TEXT)) {
9204 return (1);
9205 }
9206 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009207 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009208 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009209}
9210
9211/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009212 * xmlRelaxNGValidateInterleave:
9213 * @ctxt: a Relax-NG validation context
9214 * @define: the definition to verify
9215 *
9216 * Validate an interleave definition for a node.
9217 *
9218 * Returns 0 if the validation succeeded or an error code.
9219 */
9220static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009221xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9222 xmlRelaxNGDefinePtr define)
9223{
William M. Brack779af002003-08-01 15:55:39 +00009224 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009225 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009226 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009227
9228 xmlRelaxNGValidStatePtr oldstate;
9229 xmlRelaxNGPartitionPtr partitions;
9230 xmlRelaxNGInterleaveGroupPtr group = NULL;
9231 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9232 xmlNodePtr *list = NULL, *lasts = NULL;
9233
9234 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009235 partitions = (xmlRelaxNGPartitionPtr) define->data;
9236 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009237 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009238 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9239 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009240 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009241 /*
9242 * Optimizations for MIXED
9243 */
9244 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009245 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009246 ctxt->flags |= FLAGS_MIXED_CONTENT;
9247 if (nbgroups == 2) {
9248 /*
9249 * this is a pure <mixed> case
9250 */
9251 if (ctxt->state != NULL)
9252 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9253 ctxt->state->seq);
9254 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9255 ret = xmlRelaxNGValidateDefinition(ctxt,
9256 partitions->groups[1]->
9257 rule);
9258 else
9259 ret = xmlRelaxNGValidateDefinition(ctxt,
9260 partitions->groups[0]->
9261 rule);
9262 if (ret == 0) {
9263 if (ctxt->state != NULL)
9264 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9265 ctxt->state->
9266 seq);
9267 }
9268 ctxt->flags = oldflags;
9269 return (ret);
9270 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009271 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009272
9273 /*
9274 * Build arrays to store the first and last node of the chain
9275 * pertaining to each group
9276 */
9277 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9278 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009279 xmlRngVErrMemory(ctxt, "validating\n");
9280 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009281 }
9282 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9283 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9284 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009285 xmlRngVErrMemory(ctxt, "validating\n");
9286 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009287 }
9288 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9289
9290 /*
9291 * Walk the sequence of children finding the right group and
9292 * sorting them in sequences.
9293 */
9294 cur = ctxt->state->seq;
9295 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9296 start = cur;
9297 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009298 ctxt->state->seq = cur;
9299 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009300 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009301 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009302
Daniel Veillard4c004142003-10-07 11:33:24 +00009303 if ((cur->type == XML_TEXT_NODE) ||
9304 (cur->type == XML_CDATA_SECTION_NODE)) {
9305 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9306 NULL);
9307 } else if (cur->type == XML_ELEMENT_NODE) {
9308 if (cur->ns != NULL) {
9309 tmp = xmlHashLookup2(partitions->triage, cur->name,
9310 cur->ns->href);
9311 if (tmp == NULL)
9312 tmp = xmlHashLookup2(partitions->triage,
9313 BAD_CAST "#any",
9314 cur->ns->href);
9315 } else
9316 tmp =
9317 xmlHashLookup2(partitions->triage, cur->name,
9318 NULL);
9319 if (tmp == NULL)
9320 tmp =
9321 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9322 NULL);
9323 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009324
Daniel Veillard4c004142003-10-07 11:33:24 +00009325 if (tmp == NULL) {
9326 i = nbgroups;
9327 } else {
9328 i = ((long) tmp) - 1;
9329 if (partitions->flags & IS_NEEDCHECK) {
9330 group = partitions->groups[i];
9331 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9332 i = nbgroups;
9333 }
9334 }
9335 } else {
9336 for (i = 0; i < nbgroups; i++) {
9337 group = partitions->groups[i];
9338 if (group == NULL)
9339 continue;
9340 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9341 break;
9342 }
9343 }
9344 /*
9345 * We break as soon as an element not matched is found
9346 */
9347 if (i >= nbgroups) {
9348 break;
9349 }
9350 if (lasts[i] != NULL) {
9351 lasts[i]->next = cur;
9352 lasts[i] = cur;
9353 } else {
9354 list[i] = cur;
9355 lasts[i] = cur;
9356 }
9357 if (cur->next != NULL)
9358 lastchg = cur->next;
9359 else
9360 lastchg = cur;
9361 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009362 }
9363 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009364 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9365 ret = -1;
9366 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009367 }
9368 lastelem = cur;
9369 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009370 for (i = 0; i < nbgroups; i++) {
9371 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9372 group = partitions->groups[i];
9373 if (lasts[i] != NULL) {
9374 last = lasts[i]->next;
9375 lasts[i]->next = NULL;
9376 }
9377 ctxt->state->seq = list[i];
9378 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9379 if (ret != 0)
9380 break;
9381 if (ctxt->state != NULL) {
9382 cur = ctxt->state->seq;
9383 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9384 xmlRelaxNGFreeValidState(ctxt, oldstate);
9385 oldstate = ctxt->state;
9386 ctxt->state = NULL;
9387 if (cur != NULL) {
9388 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9389 ret = -1;
9390 ctxt->state = oldstate;
9391 goto done;
9392 }
9393 } else if (ctxt->states != NULL) {
9394 int j;
9395 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009396 int best = -1;
9397 int lowattr = -1;
9398
9399 /*
9400 * PBM: what happen if there is attributes checks in the interleaves
9401 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009402
Daniel Veillard4c004142003-10-07 11:33:24 +00009403 for (j = 0; j < ctxt->states->nbState; j++) {
9404 cur = ctxt->states->tabState[j]->seq;
9405 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9406 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009407 if (found == 0) {
9408 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9409 best = j;
9410 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009411 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009412 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9413 /* try to keep the latest one to mach old heuristic */
9414 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9415 best = j;
9416 }
9417 if (lowattr == 0)
9418 break;
9419 } else if (found == 0) {
9420 if (lowattr == -1) {
9421 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9422 best = j;
9423 } else
9424 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9425 /* try to keep the latest one to mach old heuristic */
9426 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9427 best = j;
9428 }
9429 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009430 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009431 /*
9432 * BIG PBM: here we pick only one restarting point :-(
9433 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009434 if (ctxt->states->nbState > 0) {
9435 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009436 if (best != -1) {
9437 oldstate = ctxt->states->tabState[best];
9438 ctxt->states->tabState[best] = NULL;
9439 } else {
9440 oldstate =
9441 ctxt->states->tabState[ctxt->states->nbState - 1];
9442 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009443 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009444 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009445 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009446 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009447 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9448 }
9449 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9450 ctxt->states = NULL;
9451 if (found == 0) {
Daniel Veillard76d36452009-09-07 11:19:33 +02009452 if (cur == NULL) {
9453 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, 'noname');
9454 } else {
9455 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9456 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009457 ret = -1;
9458 ctxt->state = oldstate;
9459 goto done;
9460 }
9461 } else {
9462 ret = -1;
9463 break;
9464 }
9465 if (lasts[i] != NULL) {
9466 lasts[i]->next = last;
9467 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009468 }
9469 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009470 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009471 ctxt->state = oldstate;
9472 ctxt->state->seq = lastelem;
9473 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009474 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9475 ret = -1;
9476 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009477 }
9478
Daniel Veillard4c004142003-10-07 11:33:24 +00009479 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009480 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009481 /*
9482 * builds the next links chain from the prev one
9483 */
9484 cur = lastchg;
9485 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009486 if ((cur == start) || (cur->prev == NULL))
9487 break;
9488 cur->prev->next = cur;
9489 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009490 }
9491 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009492 if (ctxt->errNr > errNr)
9493 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009494 }
9495
9496 xmlFree(list);
9497 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009498 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009499}
9500
9501/**
9502 * xmlRelaxNGValidateDefinitionList:
9503 * @ctxt: a Relax-NG validation context
9504 * @define: the list of definition to verify
9505 *
9506 * Validate the given node content against the (list) of definitions
9507 *
9508 * Returns 0 if the validation succeeded or an error code.
9509 */
9510static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009511xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9512 xmlRelaxNGDefinePtr defines)
9513{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009514 int ret = 0, res;
9515
9516
Daniel Veillard952379b2003-03-17 15:37:12 +00009517 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009518 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9519 BAD_CAST "NULL definition list");
9520 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009521 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009522 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009523 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9524 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9525 if (res < 0)
9526 ret = -1;
9527 } else {
9528 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9529 return (-1);
9530 }
9531 if (res == -1) /* continues on -2 */
9532 break;
9533 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009534 }
9535
Daniel Veillard4c004142003-10-07 11:33:24 +00009536 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009537}
9538
9539/**
9540 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009541 * @ctxt: a Relax-NG validation context
9542 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009543 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009544 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009545 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009546 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009547 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009548 */
9549static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009550xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9551 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9552{
Daniel Veillard580ced82003-03-21 21:22:48 +00009553 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009554
Daniel Veillardfd573f12003-03-16 17:52:32 +00009555 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009556 if (!xmlStrEqual(elem->name, define->name)) {
9557 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9558 return (0);
9559 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009560 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009561 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009562 if (elem->ns == NULL) {
9563 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9564 return (0);
9565 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9566 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9567 elem->name, define->ns);
9568 return (0);
9569 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009570 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009571 (define->name == NULL)) {
9572 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9573 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009574 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009575 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9576 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009577 }
9578
9579 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009580 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009581
9582 define = define->nameClass;
9583 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009584 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009585
Daniel Veillard4c004142003-10-07 11:33:24 +00009586 if (ctxt != NULL) {
9587 oldflags = ctxt->flags;
9588 ctxt->flags |= FLAGS_IGNORABLE;
9589 }
9590
9591 list = define->content;
9592 while (list != NULL) {
9593 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9594 if (ret == 1) {
9595 if (ctxt != NULL)
9596 ctxt->flags = oldflags;
9597 return (0);
9598 }
9599 if (ret < 0) {
9600 if (ctxt != NULL)
9601 ctxt->flags = oldflags;
9602 return (ret);
9603 }
9604 list = list->next;
9605 }
9606 ret = 1;
9607 if (ctxt != NULL) {
9608 ctxt->flags = oldflags;
9609 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009610 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009611 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009612
Daniel Veillard4c004142003-10-07 11:33:24 +00009613 if (ctxt != NULL) {
9614 oldflags = ctxt->flags;
9615 ctxt->flags |= FLAGS_IGNORABLE;
9616 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009617
Daniel Veillard4c004142003-10-07 11:33:24 +00009618 list = define->nameClass;
9619 while (list != NULL) {
9620 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9621 if (ret == 1) {
9622 if (ctxt != NULL)
9623 ctxt->flags = oldflags;
9624 return (1);
9625 }
9626 if (ret < 0) {
9627 if (ctxt != NULL)
9628 ctxt->flags = oldflags;
9629 return (ret);
9630 }
9631 list = list->next;
9632 }
9633 if (ctxt != NULL) {
9634 if (ret != 0) {
9635 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9636 xmlRelaxNGDumpValidError(ctxt);
9637 } else {
9638 if (ctxt->errNr > 0)
9639 xmlRelaxNGPopErrors(ctxt, 0);
9640 }
9641 }
9642 ret = 0;
9643 if (ctxt != NULL) {
9644 ctxt->flags = oldflags;
9645 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009646 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009647 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009648 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009649 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009650}
9651
9652/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009653 * xmlRelaxNGBestState:
9654 * @ctxt: a Relax-NG validation context
9655 *
9656 * Find the "best" state in the ctxt->states list of states to report
9657 * errors about. I.e. a state with no element left in the child list
9658 * or the one with the less attributes left.
9659 * This is called only if a falidation error was detected
9660 *
9661 * Returns the index of the "best" state or -1 in case of error
9662 */
9663static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009664xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9665{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009666 xmlRelaxNGValidStatePtr state;
9667 int i, tmp;
9668 int best = -1;
9669 int value = 1000000;
9670
9671 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9672 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009673 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009674
Daniel Veillard4c004142003-10-07 11:33:24 +00009675 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009676 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009677 if (state == NULL)
9678 continue;
9679 if (state->seq != NULL) {
9680 if ((best == -1) || (value > 100000)) {
9681 value = 100000;
9682 best = i;
9683 }
9684 } else {
9685 tmp = state->nbAttrLeft;
9686 if ((best == -1) || (value > tmp)) {
9687 value = tmp;
9688 best = i;
9689 }
9690 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009691 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009692 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009693}
9694
9695/**
9696 * xmlRelaxNGLogBestError:
9697 * @ctxt: a Relax-NG validation context
9698 *
9699 * Find the "best" state in the ctxt->states list of states to report
9700 * errors about and log it.
9701 */
9702static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009703xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9704{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009705 int best;
9706
9707 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9708 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009709 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009710
9711 best = xmlRelaxNGBestState(ctxt);
9712 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009713 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009714
Daniel Veillard4c004142003-10-07 11:33:24 +00009715 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009716 }
9717}
9718
9719/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009720 * xmlRelaxNGValidateElementEnd:
9721 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009722 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009723 *
9724 * Validate the end of the element, implements check that
9725 * there is nothing left not consumed in the element content
9726 * or in the attribute list.
9727 *
9728 * Returns 0 if the validation succeeded or an error code.
9729 */
9730static int
William M. Brack272693c2003-11-14 16:20:34 +00009731xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009732{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009733 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009734 xmlRelaxNGValidStatePtr state;
9735
9736 state = ctxt->state;
9737 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009738 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9739 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009740 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009741 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9742 state->node->name, state->seq->name);
9743 }
9744 return (-1);
9745 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009746 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009747 for (i = 0; i < state->nbAttrs; i++) {
9748 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009749 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009750 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9751 state->attrs[i]->name, state->node->name);
9752 }
9753 return (-1 - i);
9754 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009755 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009756 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009757}
9758
9759/**
9760 * xmlRelaxNGValidateState:
9761 * @ctxt: a Relax-NG validation context
9762 * @define: the definition to verify
9763 *
9764 * Validate the current state against the definition
9765 *
9766 * Returns 0 if the validation succeeded or an error code.
9767 */
9768static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009769xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9770 xmlRelaxNGDefinePtr define)
9771{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009772 xmlNodePtr node;
9773 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009774 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009775
9776 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009777 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9778 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009779 }
9780
9781 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009782 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009783 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009784 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009785 }
9786#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009787 for (i = 0; i < ctxt->depth; i++)
9788 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009789 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009790 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009791 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009792 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009793 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009794 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009795 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009796 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009797#endif
9798 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009799 switch (define->type) {
9800 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009801 node = xmlRelaxNGSkipIgnored(ctxt, node);
9802 ret = 0;
9803 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009804 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009805 ret = -1;
9806 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009807 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009808 while ((node != NULL) &&
9809 ((node->type == XML_TEXT_NODE) ||
9810 (node->type == XML_COMMENT_NODE) ||
9811 (node->type == XML_PI_NODE) ||
9812 (node->type == XML_CDATA_SECTION_NODE)))
9813 node = node->next;
9814 ctxt->state->seq = node;
9815 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009816 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009817 errNr = ctxt->errNr;
9818 node = xmlRelaxNGSkipIgnored(ctxt, node);
9819 if (node == NULL) {
9820 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9821 ret = -1;
9822 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9823 xmlRelaxNGDumpValidError(ctxt);
9824 break;
9825 }
9826 if (node->type != XML_ELEMENT_NODE) {
9827 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9828 ret = -1;
9829 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9830 xmlRelaxNGDumpValidError(ctxt);
9831 break;
9832 }
9833 /*
9834 * This node was already validated successfully against
9835 * this definition.
9836 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009837 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009838 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9839 if (ctxt->errNr > errNr)
9840 xmlRelaxNGPopErrors(ctxt, errNr);
9841 if (ctxt->errNr != 0) {
9842 while ((ctxt->err != NULL) &&
9843 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9844 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9845 ||
9846 ((ctxt->err->err ==
9847 XML_RELAXNG_ERR_ELEMEXTRANS)
9848 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9849 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9850 || (ctxt->err->err ==
9851 XML_RELAXNG_ERR_NOTELEM)))
9852 xmlRelaxNGValidErrorPop(ctxt);
9853 }
9854 break;
9855 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009856
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009857 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9858 if (ret <= 0) {
9859 ret = -1;
9860 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9861 xmlRelaxNGDumpValidError(ctxt);
9862 break;
9863 }
9864 ret = 0;
9865 if (ctxt->errNr != 0) {
9866 if (ctxt->errNr > errNr)
9867 xmlRelaxNGPopErrors(ctxt, errNr);
9868 while ((ctxt->err != NULL) &&
9869 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9870 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9871 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9872 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9873 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9874 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9875 xmlRelaxNGValidErrorPop(ctxt);
9876 }
9877 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009878
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009879 oldflags = ctxt->flags;
9880 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9881 ctxt->flags -= FLAGS_MIXED_CONTENT;
9882 }
9883 state = xmlRelaxNGNewValidState(ctxt, node);
9884 if (state == NULL) {
9885 ret = -1;
9886 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9887 xmlRelaxNGDumpValidError(ctxt);
9888 break;
9889 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009890
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009891 oldstate = ctxt->state;
9892 ctxt->state = state;
9893 if (define->attrs != NULL) {
9894 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9895 if (tmp != 0) {
9896 ret = -1;
9897 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9898 }
9899 }
9900 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009901 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9902 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9903 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009904
Daniel Veillard4c004142003-10-07 11:33:24 +00009905 nstate = xmlRelaxNGNewValidState(ctxt, node);
9906 ctxt->state = nstate;
9907 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009908
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009909 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9910 define->contModel,
9911 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009912 nseq = ctxt->state->seq;
9913 ctxt->state = tmpstate;
9914 ctxt->states = tmpstates;
9915 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009916
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009917#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009918 xmlGenericError(xmlGenericErrorContext,
9919 "Validating content of '%s' : %d\n",
9920 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009921#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009922 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009923 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009924
9925 if (ctxt->states != NULL) {
9926 tmp = -1;
9927
Daniel Veillardce192eb2003-04-16 15:58:05 +00009928 for (i = 0; i < ctxt->states->nbState; i++) {
9929 state = ctxt->states->tabState[i];
9930 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009931 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009932
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009933 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009934 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009935 break;
9936 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009937 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009938 if (tmp != 0) {
9939 /*
9940 * validation error, log the message for the "best" one
9941 */
9942 ctxt->flags |= FLAGS_IGNORABLE;
9943 xmlRelaxNGLogBestError(ctxt);
9944 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009945 for (i = 0; i < ctxt->states->nbState; i++) {
9946 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009947 ctxt->states->
9948 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009949 }
9950 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9951 ctxt->flags = oldflags;
9952 ctxt->states = NULL;
9953 if ((ret == 0) && (tmp == -1))
9954 ret = -1;
9955 } else {
9956 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +00009957 if (ctxt->state != NULL)
9958 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009959 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009960 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009961 xmlRelaxNGFreeValidState(ctxt, state);
9962 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009963 } else {
9964 if (define->content != NULL) {
9965 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009966 define->
9967 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009968 if (tmp != 0) {
9969 ret = -1;
9970 if (ctxt->state == NULL) {
9971 ctxt->state = oldstate;
9972 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9973 node->name);
9974 ctxt->state = NULL;
9975 } else {
9976 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9977 node->name);
9978 }
9979
9980 }
9981 }
9982 if (ctxt->states != NULL) {
9983 tmp = -1;
9984
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009985 for (i = 0; i < ctxt->states->nbState; i++) {
9986 state = ctxt->states->tabState[i];
9987 ctxt->state = state;
9988
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009989 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009990 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009991 break;
9992 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009993 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009994 if (tmp != 0) {
9995 /*
9996 * validation error, log the message for the "best" one
9997 */
9998 ctxt->flags |= FLAGS_IGNORABLE;
9999 xmlRelaxNGLogBestError(ctxt);
10000 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010001 for (i = 0; i < ctxt->states->nbState; i++) {
10002 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010003 ctxt->states->tabState[i]);
10004 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010005 }
10006 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10007 ctxt->flags = oldflags;
10008 ctxt->states = NULL;
10009 if ((ret == 0) && (tmp == -1))
10010 ret = -1;
10011 } else {
10012 state = ctxt->state;
10013 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010014 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010015 xmlRelaxNGFreeValidState(ctxt, state);
10016 }
10017 }
10018 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +000010019 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010020 }
10021 ctxt->flags = oldflags;
10022 ctxt->state = oldstate;
10023 if (oldstate != NULL)
10024 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10025 if (ret != 0) {
10026 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10027 xmlRelaxNGDumpValidError(ctxt);
10028 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010029#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010030 } else {
10031 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010032#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010033 }
10034 } else {
10035 if (ctxt->errNr > errNr)
10036 xmlRelaxNGPopErrors(ctxt, errNr);
10037 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010038
10039#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010040 xmlGenericError(xmlGenericErrorContext,
10041 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10042 node->name, ret);
10043 if (oldstate == NULL)
10044 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10045 else if (oldstate->seq == NULL)
10046 xmlGenericError(xmlGenericErrorContext, ": done\n");
10047 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10048 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10049 oldstate->seq->name);
10050 else
10051 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10052 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010053#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010054 break;
10055 case XML_RELAXNG_OPTIONAL:{
10056 errNr = ctxt->errNr;
10057 oldflags = ctxt->flags;
10058 ctxt->flags |= FLAGS_IGNORABLE;
10059 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10060 ret =
10061 xmlRelaxNGValidateDefinitionList(ctxt,
10062 define->content);
10063 if (ret != 0) {
10064 if (ctxt->state != NULL)
10065 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10066 ctxt->state = oldstate;
10067 ctxt->flags = oldflags;
10068 ret = 0;
10069 if (ctxt->errNr > errNr)
10070 xmlRelaxNGPopErrors(ctxt, errNr);
10071 break;
10072 }
10073 if (ctxt->states != NULL) {
10074 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10075 } else {
10076 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10077 if (ctxt->states == NULL) {
10078 xmlRelaxNGFreeValidState(ctxt, oldstate);
10079 ctxt->flags = oldflags;
10080 ret = -1;
10081 if (ctxt->errNr > errNr)
10082 xmlRelaxNGPopErrors(ctxt, errNr);
10083 break;
10084 }
10085 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10086 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10087 ctxt->state = NULL;
10088 }
10089 ctxt->flags = oldflags;
10090 ret = 0;
10091 if (ctxt->errNr > errNr)
10092 xmlRelaxNGPopErrors(ctxt, errNr);
10093 break;
10094 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010095 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010096 errNr = ctxt->errNr;
10097 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10098 if (ret != 0) {
10099 break;
10100 }
10101 if (ctxt->errNr > errNr)
10102 xmlRelaxNGPopErrors(ctxt, errNr);
10103 /* no break on purpose */
10104 case XML_RELAXNG_ZEROORMORE:{
10105 int progress;
10106 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10107 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010108
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010109 errNr = ctxt->errNr;
10110 res = xmlRelaxNGNewStates(ctxt, 1);
10111 if (res == NULL) {
10112 ret = -1;
10113 break;
10114 }
10115 /*
10116 * All the input states are also exit states
10117 */
10118 if (ctxt->state != NULL) {
10119 xmlRelaxNGAddStates(ctxt, res,
10120 xmlRelaxNGCopyValidState(ctxt,
10121 ctxt->
10122 state));
10123 } else {
10124 for (j = 0; j < ctxt->states->nbState; j++) {
10125 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010126 xmlRelaxNGCopyValidState(ctxt,
10127 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010128 }
10129 }
10130 oldflags = ctxt->flags;
10131 ctxt->flags |= FLAGS_IGNORABLE;
10132 do {
10133 progress = 0;
10134 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010135
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010136 if (ctxt->states != NULL) {
10137 states = ctxt->states;
10138 for (i = 0; i < states->nbState; i++) {
10139 ctxt->state = states->tabState[i];
10140 ctxt->states = NULL;
10141 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10142 define->
10143 content);
10144 if (ret == 0) {
10145 if (ctxt->state != NULL) {
10146 tmp = xmlRelaxNGAddStates(ctxt, res,
10147 ctxt->state);
10148 ctxt->state = NULL;
10149 if (tmp == 1)
10150 progress = 1;
10151 } else if (ctxt->states != NULL) {
10152 for (j = 0; j < ctxt->states->nbState;
10153 j++) {
10154 tmp =
10155 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010156 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010157 if (tmp == 1)
10158 progress = 1;
10159 }
10160 xmlRelaxNGFreeStates(ctxt,
10161 ctxt->states);
10162 ctxt->states = NULL;
10163 }
10164 } else {
10165 if (ctxt->state != NULL) {
10166 xmlRelaxNGFreeValidState(ctxt,
10167 ctxt->state);
10168 ctxt->state = NULL;
10169 }
10170 }
10171 }
10172 } else {
10173 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10174 define->
10175 content);
10176 if (ret != 0) {
10177 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10178 ctxt->state = NULL;
10179 } else {
10180 base = res->nbState;
10181 if (ctxt->state != NULL) {
10182 tmp = xmlRelaxNGAddStates(ctxt, res,
10183 ctxt->state);
10184 ctxt->state = NULL;
10185 if (tmp == 1)
10186 progress = 1;
10187 } else if (ctxt->states != NULL) {
10188 for (j = 0; j < ctxt->states->nbState; j++) {
10189 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010190 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010191 if (tmp == 1)
10192 progress = 1;
10193 }
10194 if (states == NULL) {
10195 states = ctxt->states;
10196 } else {
10197 xmlRelaxNGFreeStates(ctxt,
10198 ctxt->states);
10199 }
10200 ctxt->states = NULL;
10201 }
10202 }
10203 }
10204 if (progress) {
10205 /*
10206 * Collect all the new nodes added at that step
10207 * and make them the new node set
10208 */
10209 if (res->nbState - base == 1) {
10210 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10211 res->
10212 tabState
10213 [base]);
10214 } else {
10215 if (states == NULL) {
10216 xmlRelaxNGNewStates(ctxt,
10217 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010218 states = ctxt->states;
10219 if (states == NULL) {
10220 progress = 0;
10221 break;
10222 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010223 }
10224 states->nbState = 0;
10225 for (i = base; i < res->nbState; i++)
10226 xmlRelaxNGAddStates(ctxt, states,
10227 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010228 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010229 ctxt->states = states;
10230 }
10231 }
10232 } while (progress == 1);
10233 if (states != NULL) {
10234 xmlRelaxNGFreeStates(ctxt, states);
10235 }
10236 ctxt->states = res;
10237 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010238#if 0
10239 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010240 * errors may have to be propagated back...
10241 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010242 if (ctxt->errNr > errNr)
10243 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010244#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010245 ret = 0;
10246 break;
10247 }
10248 case XML_RELAXNG_CHOICE:{
10249 xmlRelaxNGDefinePtr list = NULL;
10250 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010251
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010252 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010253
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010254 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010255 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10256 (node != NULL)) {
10257 /*
10258 * node == NULL can't be optimized since IS_TRIABLE
10259 * doesn't account for choice which may lead to
10260 * only attributes.
10261 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010262 xmlHashTablePtr triage =
10263 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010264
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010265 /*
10266 * Something we can optimize cleanly there is only one
10267 * possble branch out !
10268 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010269 if ((node->type == XML_TEXT_NODE) ||
10270 (node->type == XML_CDATA_SECTION_NODE)) {
10271 list =
10272 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10273 } else if (node->type == XML_ELEMENT_NODE) {
10274 if (node->ns != NULL) {
10275 list = xmlHashLookup2(triage, node->name,
10276 node->ns->href);
10277 if (list == NULL)
10278 list =
10279 xmlHashLookup2(triage, BAD_CAST "#any",
10280 node->ns->href);
10281 } else
10282 list =
10283 xmlHashLookup2(triage, node->name, NULL);
10284 if (list == NULL)
10285 list =
10286 xmlHashLookup2(triage, BAD_CAST "#any",
10287 NULL);
10288 }
10289 if (list == NULL) {
10290 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010291 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010292 break;
10293 }
10294 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10295 if (ret == 0) {
10296 }
10297 break;
10298 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010299
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010300 list = define->content;
10301 oldflags = ctxt->flags;
10302 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010303
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010304 while (list != NULL) {
10305 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10306 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10307 if (ret == 0) {
10308 if (states == NULL) {
10309 states = xmlRelaxNGNewStates(ctxt, 1);
10310 }
10311 if (ctxt->state != NULL) {
10312 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10313 } else if (ctxt->states != NULL) {
10314 for (i = 0; i < ctxt->states->nbState; i++) {
10315 xmlRelaxNGAddStates(ctxt, states,
10316 ctxt->states->
10317 tabState[i]);
10318 }
10319 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10320 ctxt->states = NULL;
10321 }
10322 } else {
10323 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10324 }
10325 ctxt->state = oldstate;
10326 list = list->next;
10327 }
10328 if (states != NULL) {
10329 xmlRelaxNGFreeValidState(ctxt, oldstate);
10330 ctxt->states = states;
10331 ctxt->state = NULL;
10332 ret = 0;
10333 } else {
10334 ctxt->states = NULL;
10335 }
10336 ctxt->flags = oldflags;
10337 if (ret != 0) {
10338 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10339 xmlRelaxNGDumpValidError(ctxt);
10340 }
10341 } else {
10342 if (ctxt->errNr > errNr)
10343 xmlRelaxNGPopErrors(ctxt, errNr);
10344 }
10345 break;
10346 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010347 case XML_RELAXNG_DEF:
10348 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010349 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10350 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010351 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010352 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10353 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010354 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010355 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10356 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010357 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010358 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010359 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010360 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010361 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010362 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10363 break;
10364 case XML_RELAXNG_DATATYPE:{
10365 xmlNodePtr child;
10366 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010367
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010368 child = node;
10369 while (child != NULL) {
10370 if (child->type == XML_ELEMENT_NODE) {
10371 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10372 node->parent->name);
10373 ret = -1;
10374 break;
10375 } else if ((child->type == XML_TEXT_NODE) ||
10376 (child->type == XML_CDATA_SECTION_NODE)) {
10377 content = xmlStrcat(content, child->content);
10378 }
10379 /* TODO: handle entities ... */
10380 child = child->next;
10381 }
10382 if (ret == -1) {
10383 if (content != NULL)
10384 xmlFree(content);
10385 break;
10386 }
10387 if (content == NULL) {
10388 content = xmlStrdup(BAD_CAST "");
10389 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010390 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010391 ret = -1;
10392 break;
10393 }
10394 }
10395 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10396 ctxt->state->seq);
10397 if (ret == -1) {
10398 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10399 } else if (ret == 0) {
10400 ctxt->state->seq = NULL;
10401 }
10402 if (content != NULL)
10403 xmlFree(content);
10404 break;
10405 }
10406 case XML_RELAXNG_VALUE:{
10407 xmlChar *content = NULL;
10408 xmlChar *oldvalue;
10409 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010410
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010411 child = node;
10412 while (child != NULL) {
10413 if (child->type == XML_ELEMENT_NODE) {
10414 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10415 node->parent->name);
10416 ret = -1;
10417 break;
10418 } else if ((child->type == XML_TEXT_NODE) ||
10419 (child->type == XML_CDATA_SECTION_NODE)) {
10420 content = xmlStrcat(content, child->content);
10421 }
10422 /* TODO: handle entities ... */
10423 child = child->next;
10424 }
10425 if (ret == -1) {
10426 if (content != NULL)
10427 xmlFree(content);
10428 break;
10429 }
10430 if (content == NULL) {
10431 content = xmlStrdup(BAD_CAST "");
10432 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010433 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010434 ret = -1;
10435 break;
10436 }
10437 }
10438 oldvalue = ctxt->state->value;
10439 ctxt->state->value = content;
10440 ret = xmlRelaxNGValidateValue(ctxt, define);
10441 ctxt->state->value = oldvalue;
10442 if (ret == -1) {
10443 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10444 } else if (ret == 0) {
10445 ctxt->state->seq = NULL;
10446 }
10447 if (content != NULL)
10448 xmlFree(content);
10449 break;
10450 }
10451 case XML_RELAXNG_LIST:{
10452 xmlChar *content;
10453 xmlNodePtr child;
10454 xmlChar *oldvalue, *oldendvalue;
10455 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010456
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010457 /*
10458 * Make sure it's only text nodes
10459 */
10460
10461 content = NULL;
10462 child = node;
10463 while (child != NULL) {
10464 if (child->type == XML_ELEMENT_NODE) {
10465 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10466 node->parent->name);
10467 ret = -1;
10468 break;
10469 } else if ((child->type == XML_TEXT_NODE) ||
10470 (child->type == XML_CDATA_SECTION_NODE)) {
10471 content = xmlStrcat(content, child->content);
10472 }
10473 /* TODO: handle entities ... */
10474 child = child->next;
10475 }
10476 if (ret == -1) {
10477 if (content != NULL)
10478 xmlFree(content);
10479 break;
10480 }
10481 if (content == NULL) {
10482 content = xmlStrdup(BAD_CAST "");
10483 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010484 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010485 ret = -1;
10486 break;
10487 }
10488 }
10489 len = xmlStrlen(content);
10490 oldvalue = ctxt->state->value;
10491 oldendvalue = ctxt->state->endvalue;
10492 ctxt->state->value = content;
10493 ctxt->state->endvalue = content + len;
10494 ret = xmlRelaxNGValidateValue(ctxt, define);
10495 ctxt->state->value = oldvalue;
10496 ctxt->state->endvalue = oldendvalue;
10497 if (ret == -1) {
10498 VALID_ERR(XML_RELAXNG_ERR_LIST);
10499 } else if ((ret == 0) && (node != NULL)) {
10500 ctxt->state->seq = node->next;
10501 }
10502 if (content != NULL)
10503 xmlFree(content);
10504 break;
10505 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010506 case XML_RELAXNG_EXCEPT:
10507 case XML_RELAXNG_PARAM:
10508 TODO ret = -1;
10509 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010510 }
10511 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010512#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010513 for (i = 0; i < ctxt->depth; i++)
10514 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010515 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010516 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010517 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010518 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010519 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010520 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010521 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010522 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010523#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010524 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010525}
10526
10527/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010528 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010529 * @ctxt: a Relax-NG validation context
10530 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010531 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010532 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010533 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010534 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010535 */
10536static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010537xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10538 xmlRelaxNGDefinePtr define)
10539{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010540 xmlRelaxNGStatesPtr states, res;
10541 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010542
Daniel Veillardfd573f12003-03-16 17:52:32 +000010543 /*
10544 * We should NOT have both ctxt->state and ctxt->states
10545 */
10546 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010547 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10548 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010549 }
10550
10551 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010552 if (ctxt->states != NULL) {
10553 ctxt->state = ctxt->states->tabState[0];
10554 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10555 ctxt->states = NULL;
10556 }
10557 ret = xmlRelaxNGValidateState(ctxt, define);
10558 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10559 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10560 ctxt->state = NULL;
10561 }
10562 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10563 ctxt->state = ctxt->states->tabState[0];
10564 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10565 ctxt->states = NULL;
10566 }
10567 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010568 }
10569
10570 states = ctxt->states;
10571 ctxt->states = NULL;
10572 res = NULL;
10573 j = 0;
10574 oldflags = ctxt->flags;
10575 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010576 for (i = 0; i < states->nbState; i++) {
10577 ctxt->state = states->tabState[i];
10578 ctxt->states = NULL;
10579 ret = xmlRelaxNGValidateState(ctxt, define);
10580 /*
10581 * We should NOT have both ctxt->state and ctxt->states
10582 */
10583 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10584 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10585 ctxt->state = NULL;
10586 }
10587 if (ret == 0) {
10588 if (ctxt->states == NULL) {
10589 if (res != NULL) {
10590 /* add the state to the container */
10591 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10592 ctxt->state = NULL;
10593 } else {
10594 /* add the state directly in states */
10595 states->tabState[j++] = ctxt->state;
10596 ctxt->state = NULL;
10597 }
10598 } else {
10599 if (res == NULL) {
10600 /* make it the new container and copy other results */
10601 res = ctxt->states;
10602 ctxt->states = NULL;
10603 for (k = 0; k < j; k++)
10604 xmlRelaxNGAddStates(ctxt, res,
10605 states->tabState[k]);
10606 } else {
10607 /* add all the new results to res and reff the container */
10608 for (k = 0; k < ctxt->states->nbState; k++)
10609 xmlRelaxNGAddStates(ctxt, res,
10610 ctxt->states->tabState[k]);
10611 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10612 ctxt->states = NULL;
10613 }
10614 }
10615 } else {
10616 if (ctxt->state != NULL) {
10617 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10618 ctxt->state = NULL;
10619 } else if (ctxt->states != NULL) {
10620 for (k = 0; k < ctxt->states->nbState; k++)
10621 xmlRelaxNGFreeValidState(ctxt,
10622 ctxt->states->tabState[k]);
10623 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10624 ctxt->states = NULL;
10625 }
10626 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010627 }
10628 ctxt->flags = oldflags;
10629 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010630 xmlRelaxNGFreeStates(ctxt, states);
10631 ctxt->states = res;
10632 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010633 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010634 states->nbState = j;
10635 ctxt->states = states;
10636 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010637 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010638 ctxt->state = states->tabState[0];
10639 xmlRelaxNGFreeStates(ctxt, states);
10640 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010641 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010642 ret = -1;
10643 xmlRelaxNGFreeStates(ctxt, states);
10644 if (ctxt->states != NULL) {
10645 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10646 ctxt->states = NULL;
10647 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010648 }
10649 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010650 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10651 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010652 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010653 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010654}
10655
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010656/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010657 * xmlRelaxNGValidateDocument:
10658 * @ctxt: a Relax-NG validation context
10659 * @doc: the document
10660 *
10661 * Validate the given document
10662 *
10663 * Returns 0 if the validation succeeded or an error code.
10664 */
10665static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010666xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10667{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010668 int ret;
10669 xmlRelaxNGPtr schema;
10670 xmlRelaxNGGrammarPtr grammar;
10671 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010672 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010673
10674 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010675 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010676
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010677 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010678 schema = ctxt->schema;
10679 grammar = schema->topgrammar;
10680 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010681 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10682 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010683 }
10684 state = xmlRelaxNGNewValidState(ctxt, NULL);
10685 ctxt->state = state;
10686 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010687 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010688 state = ctxt->state;
10689 node = state->seq;
10690 node = xmlRelaxNGSkipIgnored(ctxt, node);
10691 if (node != NULL) {
10692 if (ret != -1) {
10693 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10694 ret = -1;
10695 }
10696 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010697 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010698 int i;
10699 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010700
Daniel Veillard4c004142003-10-07 11:33:24 +000010701 for (i = 0; i < ctxt->states->nbState; i++) {
10702 state = ctxt->states->tabState[i];
10703 node = state->seq;
10704 node = xmlRelaxNGSkipIgnored(ctxt, node);
10705 if (node == NULL)
10706 tmp = 0;
10707 xmlRelaxNGFreeValidState(ctxt, state);
10708 }
10709 if (tmp == -1) {
10710 if (ret != -1) {
10711 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10712 ret = -1;
10713 }
10714 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010715 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010716 if (ctxt->state != NULL) {
10717 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010718 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010719 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010720 if (ret != 0)
10721 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010722#ifdef DEBUG
10723 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010724 ctxt->error(ctxt->userData,
10725 "%d Extra error messages left on stack !\n",
10726 ctxt->errNr);
10727 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010728 }
10729#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010730#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010731 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010732 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010733
Daniel Veillard4c004142003-10-07 11:33:24 +000010734 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10735 vctxt.valid = 1;
10736 vctxt.error = ctxt->error;
10737 vctxt.warning = ctxt->warning;
10738 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010739
Daniel Veillard4c004142003-10-07 11:33:24 +000010740 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10741 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010742 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010743#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010744 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010745 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010746
Daniel Veillard4c004142003-10-07 11:33:24 +000010747 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010748}
10749
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010750/**
10751 * xmlRelaxNGCleanPSVI:
10752 * @node: an input element or document
10753 *
10754 * Call this routine to speed up XPath computation on static documents.
10755 * This stamps all the element nodes with the document order
10756 * Like for line information, the order is kept in the element->content
10757 * field, the value stored is actually - the node number (starting at -1)
10758 * to be able to differentiate from line numbers.
10759 *
10760 * Returns the number of elements found in the document or -1 in case
10761 * of error.
10762 */
10763static void
10764xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10765 xmlNodePtr cur;
10766
10767 if ((node == NULL) ||
10768 ((node->type != XML_ELEMENT_NODE) &&
10769 (node->type != XML_DOCUMENT_NODE) &&
10770 (node->type != XML_HTML_DOCUMENT_NODE)))
10771 return;
10772 if (node->type == XML_ELEMENT_NODE)
10773 node->psvi = NULL;
10774
10775 cur = node->children;
10776 while (cur != NULL) {
10777 if (cur->type == XML_ELEMENT_NODE) {
10778 cur->psvi = NULL;
10779 if (cur->children != NULL) {
10780 cur = cur->children;
10781 continue;
10782 }
10783 }
10784 if (cur->next != NULL) {
10785 cur = cur->next;
10786 continue;
10787 }
10788 do {
10789 cur = cur->parent;
10790 if (cur == NULL)
10791 break;
10792 if (cur == node) {
10793 cur = NULL;
10794 break;
10795 }
10796 if (cur->next != NULL) {
10797 cur = cur->next;
10798 break;
10799 }
10800 } while (cur != NULL);
10801 }
10802 return;
10803}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010804/************************************************************************
10805 * *
10806 * Validation interfaces *
10807 * *
10808 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010809
Daniel Veillard6eadf632003-01-23 18:29:16 +000010810/**
10811 * xmlRelaxNGNewValidCtxt:
10812 * @schema: a precompiled XML RelaxNGs
10813 *
10814 * Create an XML RelaxNGs validation context based on the given schema
10815 *
10816 * Returns the validation context or NULL in case of error
10817 */
10818xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010819xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10820{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010821 xmlRelaxNGValidCtxtPtr ret;
10822
10823 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10824 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010825 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010826 return (NULL);
10827 }
10828 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10829 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010830 ret->error = xmlGenericError;
10831 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010832 ret->errNr = 0;
10833 ret->errMax = 0;
10834 ret->err = NULL;
10835 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010836 if (schema != NULL)
10837 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010838 ret->states = NULL;
10839 ret->freeState = NULL;
10840 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010841 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010842 return (ret);
10843}
10844
10845/**
10846 * xmlRelaxNGFreeValidCtxt:
10847 * @ctxt: the schema validation context
10848 *
10849 * Free the resources associated to the schema validation context
10850 */
10851void
Daniel Veillard4c004142003-10-07 11:33:24 +000010852xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10853{
Daniel Veillard798024a2003-03-19 10:36:09 +000010854 int k;
10855
Daniel Veillard6eadf632003-01-23 18:29:16 +000010856 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010857 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010858 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010859 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010860 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010861 for (k = 0; k < ctxt->freeState->nbState; k++) {
10862 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10863 }
10864 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010865 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010866 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010867 for (k = 0; k < ctxt->freeStatesNr; k++) {
10868 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10869 }
10870 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010871 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010872 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010873 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010874 if (ctxt->elemTab != NULL) {
10875 xmlRegExecCtxtPtr exec;
10876
Daniel Veillard4c004142003-10-07 11:33:24 +000010877 exec = xmlRelaxNGElemPop(ctxt);
10878 while (exec != NULL) {
10879 xmlRegFreeExecCtxt(exec);
10880 exec = xmlRelaxNGElemPop(ctxt);
10881 }
10882 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010883 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010884 xmlFree(ctxt);
10885}
10886
10887/**
10888 * xmlRelaxNGSetValidErrors:
10889 * @ctxt: a Relax-NG validation context
10890 * @err: the error function
10891 * @warn: the warning function
10892 * @ctx: the functions context
10893 *
10894 * Set the error and warning callback informations
10895 */
10896void
10897xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010898 xmlRelaxNGValidityErrorFunc err,
10899 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10900{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010901 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010902 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010903 ctxt->error = err;
10904 ctxt->warning = warn;
10905 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010906 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010907}
10908
10909/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010910 * xmlRelaxNGSetValidStructuredErrors:
10911 * @ctxt: a Relax-NG validation context
10912 * @serror: the structured error function
10913 * @ctx: the functions context
10914 *
10915 * Set the structured error callback
10916 */
10917void
10918xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010919 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010920{
10921 if (ctxt == NULL)
10922 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010923 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010924 ctxt->error = NULL;
10925 ctxt->warning = NULL;
10926 ctxt->userData = ctx;
10927}
10928
10929/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010930 * xmlRelaxNGGetValidErrors:
10931 * @ctxt: a Relax-NG validation context
10932 * @err: the error function result
10933 * @warn: the warning function result
10934 * @ctx: the functions context result
10935 *
10936 * Get the error and warning callback informations
10937 *
10938 * Returns -1 in case of error and 0 otherwise
10939 */
10940int
10941xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010942 xmlRelaxNGValidityErrorFunc * err,
10943 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10944{
Daniel Veillard409a8142003-07-18 15:16:57 +000010945 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010946 return (-1);
10947 if (err != NULL)
10948 *err = ctxt->error;
10949 if (warn != NULL)
10950 *warn = ctxt->warning;
10951 if (ctx != NULL)
10952 *ctx = ctxt->userData;
10953 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010954}
10955
10956/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010957 * xmlRelaxNGValidateDoc:
10958 * @ctxt: a Relax-NG validation context
10959 * @doc: a parsed document tree
10960 *
10961 * Validate a document tree in memory.
10962 *
10963 * Returns 0 if the document is valid, a positive error code
10964 * number otherwise and -1 in case of internal or API error.
10965 */
10966int
Daniel Veillard4c004142003-10-07 11:33:24 +000010967xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10968{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010969 int ret;
10970
10971 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010972 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010973
10974 ctxt->doc = doc;
10975
10976 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010977 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010978 * Remove all left PSVI
10979 */
10980 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
10981
10982 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000010983 * TODO: build error codes
10984 */
10985 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010986 return (1);
10987 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010988}
10989
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010990#define bottom_relaxng
10991#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000010992#endif /* LIBXML_SCHEMAS_ENABLED */