blob: 075fc4a246173cb72c7a765fbf97be2ce27566ff [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 Veillardaa422d92009-09-24 11:31:48 +0200152#define IS_EXTERNAL_REF (1 << 8)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000153
Daniel Veillard6eadf632003-01-23 18:29:16 +0000154struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000155 xmlRelaxNGType type; /* the type of definition */
156 xmlNodePtr node; /* the node in the source */
157 xmlChar *name; /* the element local name if present */
158 xmlChar *ns; /* the namespace local name if present */
159 xmlChar *value; /* value when available */
160 void *data; /* data lib or specific pointer */
161 xmlRelaxNGDefinePtr content; /* the expected content */
162 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
163 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
164 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
165 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
166 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
167 short depth; /* used for the cycle detection */
168 short dflags; /* define related flags */
169 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000170};
171
172/**
173 * _xmlRelaxNG:
174 *
175 * A RelaxNGs definition
176 */
177struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000178 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000179 xmlRelaxNGGrammarPtr topgrammar;
180 xmlDocPtr doc;
181
Daniel Veillard4c004142003-10-07 11:33:24 +0000182 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000183
Daniel Veillard4c004142003-10-07 11:33:24 +0000184 xmlHashTablePtr defs; /* define */
185 xmlHashTablePtr refs; /* references */
186 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
187 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
188 int defNr; /* number of defines used */
189 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000190
Daniel Veillard6eadf632003-01-23 18:29:16 +0000191};
192
Daniel Veillard77648bb2003-02-20 15:03:22 +0000193#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
194#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
195#define XML_RELAXNG_IN_LIST (1 << 2)
196#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
197#define XML_RELAXNG_IN_START (1 << 4)
198#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
199#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
200#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000201#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
202#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000203
204struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000205 void *userData; /* user specific data block */
206 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
207 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000208 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000209 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000210
Daniel Veillard4c004142003-10-07 11:33:24 +0000211 xmlRelaxNGPtr schema; /* The schema in use */
212 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
213 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
214 int flags; /* parser flags */
215 int nbErrors; /* number of errors at parse time */
216 int nbWarnings; /* number of warnings at parse time */
217 const xmlChar *define; /* the current define scope */
218 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000219
Daniel Veillard4c004142003-10-07 11:33:24 +0000220 int nbInterleaves;
221 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000222
Daniel Veillard4c004142003-10-07 11:33:24 +0000223 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
224 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
225 xmlChar *URL;
226 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000227
Daniel Veillard4c004142003-10-07 11:33:24 +0000228 int defNr; /* number of defines used */
229 int defMax; /* number of defines aloocated */
230 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000231
Daniel Veillard4c004142003-10-07 11:33:24 +0000232 const char *buffer;
233 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000234
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000235 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000236 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
237 int docNr; /* Depth of the parsing stack */
238 int docMax; /* Max depth of the parsing stack */
239 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000240
241 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000242 xmlRelaxNGIncludePtr inc; /* Current parsed include */
243 int incNr; /* Depth of the include parsing stack */
244 int incMax; /* Max depth of the parsing stack */
245 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000246
Daniel Veillard4c004142003-10-07 11:33:24 +0000247 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000248
249 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000250 xmlAutomataPtr am; /* the automata */
251 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000252
253 int crng; /* compact syntax and other flags */
Daniel Veillard42595322004-11-08 10:52:06 +0000254 int freedoc; /* need to free the document */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000255};
256
257#define FLAGS_IGNORABLE 1
258#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000259#define FLAGS_MIXED_CONTENT 4
Daniel Veillardb30ca312005-09-04 13:50:03 +0000260#define FLAGS_NOERROR 8
Daniel Veillard6eadf632003-01-23 18:29:16 +0000261
262/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000263 * xmlRelaxNGInterleaveGroup:
264 *
265 * A RelaxNGs partition set associated to lists of definitions
266 */
267typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
268typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
269struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000270 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
271 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
272 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000273};
274
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000275#define IS_DETERMINIST 1
276#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000277
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000278/**
279 * xmlRelaxNGPartitions:
280 *
281 * A RelaxNGs partition associated to an interleave group
282 */
283typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
284typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
285struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000286 int nbgroups; /* number of groups in the partitions */
287 xmlHashTablePtr triage; /* hash table used to direct nodes to the
288 * right group when possible */
289 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000290 xmlRelaxNGInterleaveGroupPtr *groups;
291};
292
293/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000294 * xmlRelaxNGValidState:
295 *
296 * A RelaxNGs validation state
297 */
298#define MAX_ATTR 20
299typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
300typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
301struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000302 xmlNodePtr node; /* the current node */
303 xmlNodePtr seq; /* the sequence of children left to validate */
304 int nbAttrs; /* the number of attributes */
305 int maxAttrs; /* the size of attrs */
306 int nbAttrLeft; /* the number of attributes left to validate */
307 xmlChar *value; /* the value when operating on string */
308 xmlChar *endvalue; /* the end value when operating on string */
309 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000310};
311
312/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000313 * xmlRelaxNGStates:
314 *
315 * A RelaxNGs container for validation state
316 */
317typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
318typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
319struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000320 int nbState; /* the number of states */
321 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000322 xmlRelaxNGValidStatePtr *tabState;
323};
324
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000325#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000326
Daniel Veillardfd573f12003-03-16 17:52:32 +0000327/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000328 * xmlRelaxNGValidError:
329 *
330 * A RelaxNGs validation error
331 */
332typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
333typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
334struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000335 xmlRelaxNGValidErr err; /* the error number */
336 int flags; /* flags */
337 xmlNodePtr node; /* the current node */
338 xmlNodePtr seq; /* the current child */
339 const xmlChar *arg1; /* first arg */
340 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000341};
342
343/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000344 * xmlRelaxNGValidCtxt:
345 *
346 * A RelaxNGs validation context
347 */
348
349struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000350 void *userData; /* user specific data block */
351 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
352 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000353 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000354 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000355
Daniel Veillard4c004142003-10-07 11:33:24 +0000356 xmlRelaxNGPtr schema; /* The schema in use */
357 xmlDocPtr doc; /* the document being validated */
358 int flags; /* validation flags */
359 int depth; /* validation depth */
360 int idref; /* requires idref checking */
361 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000362
363 /*
364 * Errors accumulated in branches may have to be stacked to be
365 * provided back when it's sure they affect validation.
366 */
367 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000368 int errNr; /* Depth of the error stack */
369 int errMax; /* Max depth of the error stack */
370 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000371
Daniel Veillard4c004142003-10-07 11:33:24 +0000372 xmlRelaxNGValidStatePtr state; /* the current validation state */
373 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000374
Daniel Veillard4c004142003-10-07 11:33:24 +0000375 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
376 int freeStatesNr;
377 int freeStatesMax;
378 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000379
380 /*
381 * This is used for "progressive" validation
382 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000383 xmlRegExecCtxtPtr elem; /* the current element regexp */
384 int elemNr; /* the number of element validated */
385 int elemMax; /* the max depth of elements */
386 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
387 int pstate; /* progressive state */
388 xmlNodePtr pnode; /* the current node */
389 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
390 int perr; /* signal error in content model
391 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000392};
393
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000394/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000395 * xmlRelaxNGInclude:
396 *
397 * Structure associated to a RelaxNGs document element
398 */
399struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000400 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
401 xmlChar *href; /* the normalized href value */
402 xmlDocPtr doc; /* the associated XML document */
403 xmlRelaxNGDefinePtr content; /* the definitions */
404 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000405};
406
407/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000408 * xmlRelaxNGDocument:
409 *
410 * Structure associated to a RelaxNGs document element
411 */
412struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000413 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000414 xmlChar *href; /* the normalized href value */
415 xmlDocPtr doc; /* the associated XML document */
416 xmlRelaxNGDefinePtr content; /* the definitions */
417 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillard81c51e12009-08-14 18:52:10 +0200418 int externalRef; /* 1 if an external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000419};
420
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000421
Daniel Veillard6eadf632003-01-23 18:29:16 +0000422/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000423 * *
424 * Some factorized error routines *
425 * *
426 ************************************************************************/
427
428/**
429 * xmlRngPErrMemory:
430 * @ctxt: an Relax-NG parser context
431 * @extra: extra informations
432 *
433 * Handle a redefinition of attribute error
434 */
435static void
436xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
437{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000438 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000439 xmlGenericErrorFunc channel = NULL;
440 void *data = NULL;
441
442 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000443 if (ctxt->serror != NULL)
444 schannel = ctxt->serror;
445 else
446 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000447 data = ctxt->userData;
448 ctxt->nbErrors++;
449 }
450 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000451 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000452 NULL, NULL, XML_FROM_RELAXNGP,
453 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
454 NULL, NULL, 0, 0,
455 "Memory allocation failed : %s\n", extra);
456 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000457 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000458 NULL, NULL, XML_FROM_RELAXNGP,
459 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
460 NULL, NULL, 0, 0, "Memory allocation failed\n");
461}
462
463/**
464 * xmlRngVErrMemory:
465 * @ctxt: a Relax-NG validation context
466 * @extra: extra informations
467 *
468 * Handle a redefinition of attribute error
469 */
470static void
471xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
472{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000473 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000474 xmlGenericErrorFunc channel = NULL;
475 void *data = NULL;
476
477 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000478 if (ctxt->serror != NULL)
479 schannel = ctxt->serror;
480 else
481 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000482 data = ctxt->userData;
483 ctxt->nbErrors++;
484 }
485 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000486 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000487 NULL, NULL, XML_FROM_RELAXNGV,
488 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
489 NULL, NULL, 0, 0,
490 "Memory allocation failed : %s\n", extra);
491 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000492 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000493 NULL, NULL, XML_FROM_RELAXNGV,
494 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
495 NULL, NULL, 0, 0, "Memory allocation failed\n");
496}
497
498/**
499 * xmlRngPErr:
500 * @ctxt: a Relax-NG parser context
501 * @node: the node raising the error
502 * @error: the error code
503 * @msg: message
504 * @str1: extra info
505 * @str2: extra info
506 *
507 * Handle a Relax NG Parsing error
508 */
509static void
510xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
511 const char *msg, const xmlChar * str1, const xmlChar * str2)
512{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000513 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000514 xmlGenericErrorFunc channel = NULL;
515 void *data = NULL;
516
517 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000518 if (ctxt->serror != NULL)
519 schannel = ctxt->serror;
520 else
521 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000522 data = ctxt->userData;
523 ctxt->nbErrors++;
524 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000525 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000526 NULL, node, XML_FROM_RELAXNGP,
527 error, XML_ERR_ERROR, NULL, 0,
528 (const char *) str1, (const char *) str2, NULL, 0, 0,
529 msg, str1, str2);
530}
531
532/**
533 * xmlRngVErr:
534 * @ctxt: a Relax-NG validation context
535 * @node: the node raising the error
536 * @error: the error code
537 * @msg: message
538 * @str1: extra info
539 * @str2: extra info
540 *
541 * Handle a Relax NG Validation error
542 */
543static void
544xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
545 const char *msg, const xmlChar * str1, const xmlChar * str2)
546{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000547 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000548 xmlGenericErrorFunc channel = NULL;
549 void *data = NULL;
550
551 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000552 if (ctxt->serror != NULL)
553 schannel = ctxt->serror;
554 else
555 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000556 data = ctxt->userData;
557 ctxt->nbErrors++;
558 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000559 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000560 NULL, node, XML_FROM_RELAXNGV,
561 error, XML_ERR_ERROR, NULL, 0,
562 (const char *) str1, (const char *) str2, NULL, 0, 0,
563 msg, str1, str2);
564}
565
566/************************************************************************
Daniel Veillard6eadf632003-01-23 18:29:16 +0000567 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000568 * Preliminary type checking interfaces *
569 * *
570 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000571
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000572/**
573 * xmlRelaxNGTypeHave:
574 * @data: data needed for the library
575 * @type: the type name
576 * @value: the value to check
577 *
578 * Function provided by a type library to check if a type is exported
579 *
580 * Returns 1 if yes, 0 if no and -1 in case of error.
581 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000582typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000583
584/**
585 * xmlRelaxNGTypeCheck:
586 * @data: data needed for the library
587 * @type: the type name
588 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000589 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000590 *
591 * Function provided by a type library to check if a value match a type
592 *
593 * Returns 1 if yes, 0 if no and -1 in case of error.
594 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000595typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
596 const xmlChar * value, void **result,
597 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000598
599/**
600 * xmlRelaxNGFacetCheck:
601 * @data: data needed for the library
602 * @type: the type name
603 * @facet: the facet name
604 * @val: the facet value
605 * @strval: the string value
606 * @value: the value to check
607 *
608 * Function provided by a type library to check a value facet
609 *
610 * Returns 1 if yes, 0 if no and -1 in case of error.
611 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000612typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
613 const xmlChar * facet,
614 const xmlChar * val,
615 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000616
617/**
618 * xmlRelaxNGTypeFree:
619 * @data: data needed for the library
620 * @result: the value to free
621 *
622 * Function provided by a type library to free a returned result
623 */
624typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000625
626/**
627 * xmlRelaxNGTypeCompare:
628 * @data: data needed for the library
629 * @type: the type name
630 * @value1: the first value
631 * @value2: the second value
632 *
633 * Function provided by a type library to compare two values accordingly
634 * to a type.
635 *
636 * Returns 1 if yes, 0 if no and -1 in case of error.
637 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000638typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
639 const xmlChar * value1,
640 xmlNodePtr ctxt1,
641 void *comp1,
642 const xmlChar * value2,
643 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000644typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
645typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
646struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000647 const xmlChar *namespace; /* the datatypeLibrary value */
648 void *data; /* data needed for the library */
649 xmlRelaxNGTypeHave have; /* the export function */
650 xmlRelaxNGTypeCheck check; /* the checking function */
651 xmlRelaxNGTypeCompare comp; /* the compare function */
652 xmlRelaxNGFacetCheck facet; /* the facet check function */
653 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000654};
655
656/************************************************************************
657 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000658 * Allocation functions *
659 * *
660 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000661static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
662static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000663static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000664static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000665static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
666 ATTRIBUTE_UNUSED,
667 xmlRelaxNGValidStatePtr state1,
668 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000669static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000670 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000671
672/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000673 * xmlRelaxNGFreeDocument:
674 * @docu: a document structure
675 *
676 * Deallocate a RelaxNG document structure.
677 */
678static void
679xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
680{
681 if (docu == NULL)
682 return;
683
684 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000685 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000686 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000687 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000688 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000689 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000690 xmlFree(docu);
691}
692
693/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000694 * xmlRelaxNGFreeDocumentList:
695 * @docu: a list of document structure
696 *
697 * Deallocate a RelaxNG document structures.
698 */
699static void
700xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
701{
702 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000703
Daniel Veillardc482e262003-02-26 14:48:48 +0000704 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000705 next = docu->next;
706 xmlRelaxNGFreeDocument(docu);
707 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000708 }
709}
710
711/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000712 * xmlRelaxNGFreeInclude:
713 * @incl: a include structure
714 *
715 * Deallocate a RelaxNG include structure.
716 */
717static void
718xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
719{
720 if (incl == NULL)
721 return;
722
723 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000724 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000725 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000726 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000727 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000728 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000729 xmlFree(incl);
730}
731
732/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000733 * xmlRelaxNGFreeIncludeList:
734 * @incl: a include structure list
735 *
736 * Deallocate a RelaxNG include structure.
737 */
738static void
739xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
740{
741 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000742
Daniel Veillardc482e262003-02-26 14:48:48 +0000743 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000744 next = incl->next;
745 xmlRelaxNGFreeInclude(incl);
746 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000747 }
748}
749
750/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000751 * xmlRelaxNGNewRelaxNG:
752 * @ctxt: a Relax-NG validation context (optional)
753 *
754 * Allocate a new RelaxNG structure.
755 *
756 * Returns the newly allocated structure or NULL in case or error
757 */
758static xmlRelaxNGPtr
759xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
760{
761 xmlRelaxNGPtr ret;
762
763 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
764 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000765 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000766 return (NULL);
767 }
768 memset(ret, 0, sizeof(xmlRelaxNG));
769
770 return (ret);
771}
772
773/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000774 * xmlRelaxNGFreeInnerSchema:
775 * @schema: a schema structure
776 *
777 * Deallocate a RelaxNG schema structure.
778 */
779static void
780xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
781{
782 if (schema == NULL)
783 return;
784
785 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000786 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000787 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000788 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000789
Daniel Veillard4c004142003-10-07 11:33:24 +0000790 for (i = 0; i < schema->defNr; i++)
791 xmlRelaxNGFreeDefine(schema->defTab[i]);
792 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000793 }
794
795 xmlFree(schema);
796}
797
798/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000799 * xmlRelaxNGFree:
800 * @schema: a schema structure
801 *
802 * Deallocate a RelaxNG structure.
803 */
804void
805xmlRelaxNGFree(xmlRelaxNGPtr schema)
806{
807 if (schema == NULL)
808 return;
809
Daniel Veillard6eadf632003-01-23 18:29:16 +0000810 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000811 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000812 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000813 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000814 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000815 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000816 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000817 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000818 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000819 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000820
Daniel Veillard4c004142003-10-07 11:33:24 +0000821 for (i = 0; i < schema->defNr; i++)
822 xmlRelaxNGFreeDefine(schema->defTab[i]);
823 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000824 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000825
826 xmlFree(schema);
827}
828
829/**
830 * xmlRelaxNGNewGrammar:
831 * @ctxt: a Relax-NG validation context (optional)
832 *
833 * Allocate a new RelaxNG grammar.
834 *
835 * Returns the newly allocated structure or NULL in case or error
836 */
837static xmlRelaxNGGrammarPtr
838xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
839{
840 xmlRelaxNGGrammarPtr ret;
841
842 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
843 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000844 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000845 return (NULL);
846 }
847 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
848
849 return (ret);
850}
851
852/**
853 * xmlRelaxNGFreeGrammar:
854 * @grammar: a grammar structure
855 *
856 * Deallocate a RelaxNG grammar structure.
857 */
858static void
859xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
860{
861 if (grammar == NULL)
862 return;
863
Daniel Veillardc482e262003-02-26 14:48:48 +0000864 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000865 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000866 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000867 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000868 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000869 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000870 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000871 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000872 }
873 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000874 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000875 }
876
877 xmlFree(grammar);
878}
879
880/**
881 * xmlRelaxNGNewDefine:
882 * @ctxt: a Relax-NG validation context
883 * @node: the node in the input document.
884 *
885 * Allocate a new RelaxNG define.
886 *
887 * Returns the newly allocated structure or NULL in case or error
888 */
889static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000890xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000891{
892 xmlRelaxNGDefinePtr ret;
893
Daniel Veillard419a7682003-02-03 23:22:49 +0000894 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000895 ctxt->defMax = 16;
896 ctxt->defNr = 0;
897 ctxt->defTab = (xmlRelaxNGDefinePtr *)
898 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
899 if (ctxt->defTab == NULL) {
900 xmlRngPErrMemory(ctxt, "allocating define\n");
901 return (NULL);
902 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000903 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000904 xmlRelaxNGDefinePtr *tmp;
905
906 ctxt->defMax *= 2;
907 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
908 ctxt->defMax *
909 sizeof
910 (xmlRelaxNGDefinePtr));
911 if (tmp == NULL) {
912 xmlRngPErrMemory(ctxt, "allocating define\n");
913 return (NULL);
914 }
915 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000916 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000917 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
918 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000919 xmlRngPErrMemory(ctxt, "allocating define\n");
920 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000921 }
922 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000923 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000924 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000925 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000926 return (ret);
927}
928
929/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000930 * xmlRelaxNGFreePartition:
931 * @partitions: a partition set structure
932 *
933 * Deallocate RelaxNG partition set structures.
934 */
935static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000936xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
937{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000938 xmlRelaxNGInterleaveGroupPtr group;
939 int j;
940
941 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000942 if (partitions->groups != NULL) {
943 for (j = 0; j < partitions->nbgroups; j++) {
944 group = partitions->groups[j];
945 if (group != NULL) {
946 if (group->defs != NULL)
947 xmlFree(group->defs);
948 if (group->attrs != NULL)
949 xmlFree(group->attrs);
950 xmlFree(group);
951 }
952 }
953 xmlFree(partitions->groups);
954 }
955 if (partitions->triage != NULL) {
956 xmlHashFree(partitions->triage, NULL);
957 }
958 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000959 }
960}
Daniel Veillard4c004142003-10-07 11:33:24 +0000961
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000962/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000963 * xmlRelaxNGFreeDefine:
964 * @define: a define structure
965 *
966 * Deallocate a RelaxNG define structure.
967 */
968static void
969xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
970{
971 if (define == NULL)
972 return;
973
Daniel Veillard4c004142003-10-07 11:33:24 +0000974 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
975 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000976
Daniel Veillard4c004142003-10-07 11:33:24 +0000977 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
978 if ((lib != NULL) && (lib->freef != NULL))
979 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000980 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000981 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
982 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
983 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
984 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000985 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000986 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000987 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000988 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000989 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000990 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000991 if (define->contModel != NULL)
992 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000993 xmlFree(define);
994}
995
996/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000997 * xmlRelaxNGNewStates:
998 * @ctxt: a Relax-NG validation context
999 * @size: the default size for the container
1000 *
1001 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001002 *
1003 * Returns the newly allocated structure or NULL in case or error
1004 */
1005static xmlRelaxNGStatesPtr
1006xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1007{
1008 xmlRelaxNGStatesPtr ret;
1009
Daniel Veillard798024a2003-03-19 10:36:09 +00001010 if ((ctxt != NULL) &&
Daniel Veillard9fcd4622009-08-14 16:16:31 +02001011 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001012 ctxt->freeStatesNr--;
1013 ret = ctxt->freeStates[ctxt->freeStatesNr];
1014 ret->nbState = 0;
1015 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001016 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001017 if (size < 16)
1018 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001019
1020 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001021 (size -
1022 1) *
1023 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001024 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001025 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001026 return (NULL);
1027 }
1028 ret->nbState = 0;
1029 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001030 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1031 sizeof
1032 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001033 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001034 xmlRngVErrMemory(ctxt, "allocating states\n");
1035 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001036 return (NULL);
1037 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001038 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001039}
1040
1041/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001042 * xmlRelaxNGAddStateUniq:
1043 * @ctxt: a Relax-NG validation context
1044 * @states: the states container
1045 * @state: the validation state
1046 *
1047 * Add a RelaxNG validation state to the container without checking
1048 * for unicity.
1049 *
1050 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1051 */
1052static int
1053xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001054 xmlRelaxNGStatesPtr states,
1055 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001056{
1057 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001058 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001059 }
1060 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001061 xmlRelaxNGValidStatePtr *tmp;
1062 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001063
Daniel Veillard4c004142003-10-07 11:33:24 +00001064 size = states->maxState * 2;
1065 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1066 (size) *
1067 sizeof
1068 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001069 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001070 xmlRngVErrMemory(ctxt, "adding states\n");
1071 return (-1);
1072 }
1073 states->tabState = tmp;
1074 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001075 }
1076 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001077 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001078}
1079
1080/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001081 * xmlRelaxNGAddState:
1082 * @ctxt: a Relax-NG validation context
1083 * @states: the states container
1084 * @state: the validation state
1085 *
1086 * Add a RelaxNG validation state to the container
1087 *
1088 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1089 */
1090static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001091xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1092 xmlRelaxNGStatesPtr states,
1093 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001094{
1095 int i;
1096
1097 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001098 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001099 }
1100 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001101 xmlRelaxNGValidStatePtr *tmp;
1102 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001103
Daniel Veillard4c004142003-10-07 11:33:24 +00001104 size = states->maxState * 2;
1105 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1106 (size) *
1107 sizeof
1108 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001109 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001110 xmlRngVErrMemory(ctxt, "adding states\n");
1111 return (-1);
1112 }
1113 states->tabState = tmp;
1114 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001115 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001116 for (i = 0; i < states->nbState; i++) {
1117 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1118 xmlRelaxNGFreeValidState(ctxt, state);
1119 return (0);
1120 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001121 }
1122 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001123 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001124}
1125
1126/**
1127 * xmlRelaxNGFreeStates:
1128 * @ctxt: a Relax-NG validation context
1129 * @states: teh container
1130 *
1131 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001132 */
1133static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001134xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001135 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001136{
Daniel Veillard798024a2003-03-19 10:36:09 +00001137 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001138 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001139 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001140 ctxt->freeStatesMax = 40;
1141 ctxt->freeStatesNr = 0;
1142 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1143 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1144 if (ctxt->freeStates == NULL) {
1145 xmlRngVErrMemory(ctxt, "storing states\n");
1146 }
1147 } else if ((ctxt != NULL)
1148 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1149 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001150
Daniel Veillard4c004142003-10-07 11:33:24 +00001151 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1152 2 * ctxt->freeStatesMax *
1153 sizeof
1154 (xmlRelaxNGStatesPtr));
1155 if (tmp == NULL) {
1156 xmlRngVErrMemory(ctxt, "storing states\n");
1157 xmlFree(states->tabState);
1158 xmlFree(states);
1159 return;
1160 }
1161 ctxt->freeStates = tmp;
1162 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001163 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001164 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001165 xmlFree(states->tabState);
1166 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001167 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001168 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001169 }
1170}
1171
1172/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001173 * xmlRelaxNGNewValidState:
1174 * @ctxt: a Relax-NG validation context
1175 * @node: the current node or NULL for the document
1176 *
1177 * Allocate a new RelaxNG validation state
1178 *
1179 * Returns the newly allocated structure or NULL in case or error
1180 */
1181static xmlRelaxNGValidStatePtr
1182xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1183{
1184 xmlRelaxNGValidStatePtr ret;
1185 xmlAttrPtr attr;
1186 xmlAttrPtr attrs[MAX_ATTR];
1187 int nbAttrs = 0;
1188 xmlNodePtr root = NULL;
1189
1190 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001191 root = xmlDocGetRootElement(ctxt->doc);
1192 if (root == NULL)
1193 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001194 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001195 attr = node->properties;
1196 while (attr != NULL) {
1197 if (nbAttrs < MAX_ATTR)
1198 attrs[nbAttrs++] = attr;
1199 else
1200 nbAttrs++;
1201 attr = attr->next;
1202 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001203 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001204 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1205 ctxt->freeState->nbState--;
1206 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001207 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001208 ret =
1209 (xmlRelaxNGValidStatePtr)
1210 xmlMalloc(sizeof(xmlRelaxNGValidState));
1211 if (ret == NULL) {
1212 xmlRngVErrMemory(ctxt, "allocating states\n");
1213 return (NULL);
1214 }
1215 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001216 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001217 ret->value = NULL;
1218 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001219 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001220 ret->node = (xmlNodePtr) ctxt->doc;
1221 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001222 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001223 ret->node = node;
1224 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001225 }
1226 ret->nbAttrs = 0;
1227 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001228 if (ret->attrs == NULL) {
1229 if (nbAttrs < 4)
1230 ret->maxAttrs = 4;
1231 else
1232 ret->maxAttrs = nbAttrs;
1233 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1234 sizeof(xmlAttrPtr));
1235 if (ret->attrs == NULL) {
1236 xmlRngVErrMemory(ctxt, "allocating states\n");
1237 return (ret);
1238 }
1239 } else if (ret->maxAttrs < nbAttrs) {
1240 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001241
Daniel Veillard4c004142003-10-07 11:33:24 +00001242 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1243 sizeof(xmlAttrPtr));
1244 if (tmp == NULL) {
1245 xmlRngVErrMemory(ctxt, "allocating states\n");
1246 return (ret);
1247 }
1248 ret->attrs = tmp;
1249 ret->maxAttrs = nbAttrs;
1250 }
1251 ret->nbAttrs = nbAttrs;
1252 if (nbAttrs < MAX_ATTR) {
1253 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1254 } else {
1255 attr = node->properties;
1256 nbAttrs = 0;
1257 while (attr != NULL) {
1258 ret->attrs[nbAttrs++] = attr;
1259 attr = attr->next;
1260 }
1261 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001262 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001263 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001264 return (ret);
1265}
1266
1267/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001268 * xmlRelaxNGCopyValidState:
1269 * @ctxt: a Relax-NG validation context
1270 * @state: a validation state
1271 *
1272 * Copy the validation state
1273 *
1274 * Returns the newly allocated structure or NULL in case or error
1275 */
1276static xmlRelaxNGValidStatePtr
1277xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001278 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001279{
1280 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001281 unsigned int maxAttrs;
1282 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001283
1284 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001285 return (NULL);
1286 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1287 ctxt->freeState->nbState--;
1288 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001289 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001290 ret =
1291 (xmlRelaxNGValidStatePtr)
1292 xmlMalloc(sizeof(xmlRelaxNGValidState));
1293 if (ret == NULL) {
1294 xmlRngVErrMemory(ctxt, "allocating states\n");
1295 return (NULL);
1296 }
1297 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001298 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001299 attrs = ret->attrs;
1300 maxAttrs = ret->maxAttrs;
1301 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1302 ret->attrs = attrs;
1303 ret->maxAttrs = maxAttrs;
1304 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001305 if (ret->attrs == NULL) {
1306 ret->maxAttrs = state->maxAttrs;
1307 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1308 sizeof(xmlAttrPtr));
1309 if (ret->attrs == NULL) {
1310 xmlRngVErrMemory(ctxt, "allocating states\n");
1311 ret->nbAttrs = 0;
1312 return (ret);
1313 }
1314 } else if (ret->maxAttrs < state->nbAttrs) {
1315 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001316
Daniel Veillard4c004142003-10-07 11:33:24 +00001317 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1318 sizeof(xmlAttrPtr));
1319 if (tmp == NULL) {
1320 xmlRngVErrMemory(ctxt, "allocating states\n");
1321 ret->nbAttrs = 0;
1322 return (ret);
1323 }
1324 ret->maxAttrs = state->maxAttrs;
1325 ret->attrs = tmp;
1326 }
1327 memcpy(ret->attrs, state->attrs,
1328 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001329 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001330 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001331}
1332
1333/**
1334 * xmlRelaxNGEqualValidState:
1335 * @ctxt: a Relax-NG validation context
1336 * @state1: a validation state
1337 * @state2: a validation state
1338 *
1339 * Compare the validation states for equality
1340 *
1341 * Returns 1 if equald, 0 otherwise
1342 */
1343static int
1344xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001345 xmlRelaxNGValidStatePtr state1,
1346 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001347{
1348 int i;
1349
1350 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001351 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001352 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001353 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001354 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001355 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001356 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001357 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001358 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001359 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001360 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001361 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001362 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001363 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001364 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001365 (!xmlStrEqual(state1->value, state2->value)))
1366 return (0);
1367 for (i = 0; i < state1->nbAttrs; i++) {
1368 if (state1->attrs[i] != state2->attrs[i])
1369 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001370 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001371 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001372}
1373
1374/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001375 * xmlRelaxNGFreeValidState:
1376 * @state: a validation state structure
1377 *
1378 * Deallocate a RelaxNG validation state structure.
1379 */
1380static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001381xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001382 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001383{
1384 if (state == NULL)
1385 return;
1386
Daniel Veillard798024a2003-03-19 10:36:09 +00001387 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001388 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001389 }
1390 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001391 if (state->attrs != NULL)
1392 xmlFree(state->attrs);
1393 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001394 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001395 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001396 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001397}
1398
1399/************************************************************************
1400 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001401 * Semi internal functions *
1402 * *
1403 ************************************************************************/
1404
1405/**
1406 * xmlRelaxParserSetFlag:
1407 * @ctxt: a RelaxNG parser context
1408 * @flags: a set of flags values
1409 *
1410 * Semi private function used to pass informations to a parser context
1411 * which are a combination of xmlRelaxNGParserFlag .
1412 *
1413 * Returns 0 if success and -1 in case of error
1414 */
1415int
1416xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1417{
1418 if (ctxt == NULL) return(-1);
1419 if (flags & XML_RELAXNGP_FREE_DOC) {
1420 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1421 flags -= XML_RELAXNGP_FREE_DOC;
1422 }
1423 if (flags & XML_RELAXNGP_CRNG) {
1424 ctxt->crng |= XML_RELAXNGP_CRNG;
1425 flags -= XML_RELAXNGP_CRNG;
1426 }
1427 if (flags != 0) return(-1);
1428 return(0);
1429}
1430
1431/************************************************************************
1432 * *
1433 * Document functions *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001434 * *
1435 ************************************************************************/
1436static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001437 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001438
1439/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001440 * xmlRelaxNGIncludePush:
1441 * @ctxt: the parser context
1442 * @value: the element doc
1443 *
1444 * Pushes a new include on top of the include stack
1445 *
1446 * Returns 0 in case of error, the index in the stack otherwise
1447 */
1448static int
1449xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001450 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001451{
1452 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001453 ctxt->incMax = 4;
1454 ctxt->incNr = 0;
1455 ctxt->incTab =
1456 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1457 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001458 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001459 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001460 return (0);
1461 }
1462 }
1463 if (ctxt->incNr >= ctxt->incMax) {
1464 ctxt->incMax *= 2;
1465 ctxt->incTab =
1466 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001467 ctxt->incMax *
1468 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001469 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001470 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001471 return (0);
1472 }
1473 }
1474 ctxt->incTab[ctxt->incNr] = value;
1475 ctxt->inc = value;
1476 return (ctxt->incNr++);
1477}
1478
1479/**
1480 * xmlRelaxNGIncludePop:
1481 * @ctxt: the parser context
1482 *
1483 * Pops the top include from the include stack
1484 *
1485 * Returns the include just removed
1486 */
1487static xmlRelaxNGIncludePtr
1488xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1489{
1490 xmlRelaxNGIncludePtr ret;
1491
1492 if (ctxt->incNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001493 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001494 ctxt->incNr--;
1495 if (ctxt->incNr > 0)
1496 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1497 else
1498 ctxt->inc = NULL;
1499 ret = ctxt->incTab[ctxt->incNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001500 ctxt->incTab[ctxt->incNr] = NULL;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001501 return (ret);
1502}
1503
1504/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001505 * xmlRelaxNGRemoveRedefine:
1506 * @ctxt: the parser context
1507 * @URL: the normalized URL
1508 * @target: the included target
1509 * @name: the define name to eliminate
1510 *
1511 * Applies the elimination algorithm of 4.7
1512 *
1513 * Returns 0 in case of error, 1 in case of success.
1514 */
1515static int
1516xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001517 const xmlChar * URL ATTRIBUTE_UNUSED,
1518 xmlNodePtr target, const xmlChar * name)
1519{
Daniel Veillard5add8682003-03-10 13:13:58 +00001520 int found = 0;
1521 xmlNodePtr tmp, tmp2;
1522 xmlChar *name2;
1523
1524#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001525 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001526 xmlGenericError(xmlGenericErrorContext,
1527 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001528 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001529 xmlGenericError(xmlGenericErrorContext,
1530 "Elimination of <include> define %s from %s\n",
1531 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001532#endif
1533 tmp = target;
1534 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001535 tmp2 = tmp->next;
1536 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1537 found = 1;
1538 xmlUnlinkNode(tmp);
1539 xmlFreeNode(tmp);
1540 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1541 name2 = xmlGetProp(tmp, BAD_CAST "name");
1542 xmlRelaxNGNormExtSpace(name2);
1543 if (name2 != NULL) {
1544 if (xmlStrEqual(name, name2)) {
1545 found = 1;
1546 xmlUnlinkNode(tmp);
1547 xmlFreeNode(tmp);
1548 }
1549 xmlFree(name2);
1550 }
1551 } else if (IS_RELAXNG(tmp, "include")) {
1552 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001553 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001554
Daniel Veillard4c004142003-10-07 11:33:24 +00001555 if ((inc != NULL) && (inc->doc != NULL) &&
1556 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001557
Daniel Veillard4c004142003-10-07 11:33:24 +00001558 if (xmlStrEqual
1559 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001560#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001561 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001562#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001563 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1564 inc->doc->children->
1565 children, name) == 1) {
1566 found = 1;
1567 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001568#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001569 if (href != NULL)
1570 xmlFree(href);
Daniel Veillard14b56432006-03-09 18:41:40 +00001571#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001572 }
1573 }
1574 }
1575 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001576 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001577 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001578}
1579
1580/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001581 * xmlRelaxNGLoadInclude:
1582 * @ctxt: the parser context
1583 * @URL: the normalized URL
1584 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001585 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001586 *
1587 * First lookup if the document is already loaded into the parser context,
1588 * check against recursion. If not found the resource is loaded and
1589 * the content is preprocessed before being returned back to the caller.
1590 *
1591 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1592 */
1593static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001594xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1595 xmlNodePtr node, const xmlChar * ns)
1596{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001597 xmlRelaxNGIncludePtr ret = NULL;
1598 xmlDocPtr doc;
1599 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001600 xmlNodePtr root, cur;
1601
1602#ifdef DEBUG_INCLUDE
1603 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001604 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001605#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001606
1607 /*
1608 * check against recursion in the stack
1609 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001610 for (i = 0; i < ctxt->incNr; i++) {
1611 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1612 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1613 "Detected an Include recursion for %s\n", URL,
1614 NULL);
1615 return (NULL);
1616 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001617 }
1618
1619 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001620 * load the document
1621 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001622 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001623 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001624 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1625 "xmlRelaxNG: could not load %s\n", URL, NULL);
1626 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001627 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001628#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001629 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001630#endif
1631
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001632 /*
1633 * Allocate the document structures and register it first.
1634 */
1635 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1636 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001637 xmlRngPErrMemory(ctxt, "allocating include\n");
1638 xmlFreeDoc(doc);
1639 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001640 }
1641 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1642 ret->doc = doc;
1643 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001644 ret->next = ctxt->includes;
1645 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001646
1647 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001648 * transmit the ns if needed
1649 */
1650 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001651 root = xmlDocGetRootElement(doc);
1652 if (root != NULL) {
1653 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1654 xmlSetProp(root, BAD_CAST "ns", ns);
1655 }
1656 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001657 }
1658
1659 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001660 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001661 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001662 xmlRelaxNGIncludePush(ctxt, ret);
1663
1664 /*
1665 * Some preprocessing of the document content, this include recursing
1666 * in the include stack.
1667 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001668#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001669 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001670#endif
1671
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001672 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1673 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001674 ctxt->inc = NULL;
1675 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001676 }
1677
1678 /*
1679 * Pop up the include from the stack
1680 */
1681 xmlRelaxNGIncludePop(ctxt);
1682
Daniel Veillard5add8682003-03-10 13:13:58 +00001683#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001684 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001685#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001686 /*
1687 * Check that the top element is a grammar
1688 */
1689 root = xmlDocGetRootElement(doc);
1690 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001691 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1692 "xmlRelaxNG: included document is empty %s\n", URL,
1693 NULL);
1694 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001695 }
1696 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001697 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1698 "xmlRelaxNG: included document %s root is not a grammar\n",
1699 URL, NULL);
1700 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001701 }
1702
1703 /*
1704 * Elimination of redefined rules in the include.
1705 */
1706 cur = node->children;
1707 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001708 if (IS_RELAXNG(cur, "start")) {
1709 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001710
Daniel Veillard4c004142003-10-07 11:33:24 +00001711 found =
1712 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1713 if (!found) {
1714 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1715 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1716 URL, NULL);
1717 }
1718 } else if (IS_RELAXNG(cur, "define")) {
1719 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001720
Daniel Veillard4c004142003-10-07 11:33:24 +00001721 name = xmlGetProp(cur, BAD_CAST "name");
1722 if (name == NULL) {
1723 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1724 "xmlRelaxNG: include %s has define without name\n",
1725 URL, NULL);
1726 } else {
1727 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001728
Daniel Veillard4c004142003-10-07 11:33:24 +00001729 xmlRelaxNGNormExtSpace(name);
1730 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1731 root->children, name);
1732 if (!found) {
1733 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1734 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1735 URL, name);
1736 }
1737 xmlFree(name);
1738 }
1739 }
1740 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001741 }
1742
1743
Daniel Veillard4c004142003-10-07 11:33:24 +00001744 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001745}
1746
1747/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001748 * xmlRelaxNGValidErrorPush:
1749 * @ctxt: the validation context
1750 * @err: the error code
1751 * @arg1: the first string argument
1752 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001753 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001754 *
1755 * Pushes a new error on top of the error stack
1756 *
1757 * Returns 0 in case of error, the index in the stack otherwise
1758 */
1759static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001760xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1761 xmlRelaxNGValidErr err, const xmlChar * arg1,
1762 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001763{
1764 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001765
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001766#ifdef DEBUG_ERROR
1767 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001768 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001769#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001770 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001771 ctxt->errMax = 8;
1772 ctxt->errNr = 0;
1773 ctxt->errTab =
1774 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1775 sizeof
1776 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001777 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001778 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001779 return (0);
1780 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001781 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001782 }
1783 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001784 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001785 ctxt->errTab =
1786 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001787 ctxt->errMax *
1788 sizeof
1789 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001790 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001791 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001792 return (0);
1793 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001794 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001795 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001796 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001797 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1798 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001799 cur = &ctxt->errTab[ctxt->errNr];
1800 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001801 if (dup) {
1802 cur->arg1 = xmlStrdup(arg1);
1803 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001804 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001805 } else {
1806 cur->arg1 = arg1;
1807 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001808 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001809 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001810 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001811 cur->node = ctxt->state->node;
1812 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001813 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001814 cur->node = NULL;
1815 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001816 }
1817 ctxt->err = cur;
1818 return (ctxt->errNr++);
1819}
1820
1821/**
1822 * xmlRelaxNGValidErrorPop:
1823 * @ctxt: the validation context
1824 *
1825 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001826 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001827static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001828xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1829{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001830 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001831
Daniel Veillard580ced82003-03-21 21:22:48 +00001832 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001833 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001834 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001835 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001836 ctxt->errNr--;
1837 if (ctxt->errNr > 0)
1838 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1839 else
1840 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001841 cur = &ctxt->errTab[ctxt->errNr];
1842 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001843 if (cur->arg1 != NULL)
1844 xmlFree((xmlChar *) cur->arg1);
1845 cur->arg1 = NULL;
1846 if (cur->arg2 != NULL)
1847 xmlFree((xmlChar *) cur->arg2);
1848 cur->arg2 = NULL;
1849 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001850 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001851}
1852
Daniel Veillard42f12e92003-03-07 18:32:59 +00001853/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001854 * xmlRelaxNGDocumentPush:
1855 * @ctxt: the parser context
1856 * @value: the element doc
1857 *
1858 * Pushes a new doc on top of the doc stack
1859 *
1860 * Returns 0 in case of error, the index in the stack otherwise
1861 */
1862static int
1863xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001864 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001865{
1866 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001867 ctxt->docMax = 4;
1868 ctxt->docNr = 0;
1869 ctxt->docTab =
1870 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1871 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001872 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001873 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001874 return (0);
1875 }
1876 }
1877 if (ctxt->docNr >= ctxt->docMax) {
1878 ctxt->docMax *= 2;
1879 ctxt->docTab =
1880 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001881 ctxt->docMax *
1882 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001883 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001884 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001885 return (0);
1886 }
1887 }
1888 ctxt->docTab[ctxt->docNr] = value;
1889 ctxt->doc = value;
1890 return (ctxt->docNr++);
1891}
1892
1893/**
1894 * xmlRelaxNGDocumentPop:
1895 * @ctxt: the parser context
1896 *
1897 * Pops the top doc from the doc stack
1898 *
1899 * Returns the doc just removed
1900 */
1901static xmlRelaxNGDocumentPtr
1902xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1903{
1904 xmlRelaxNGDocumentPtr ret;
1905
1906 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001907 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001908 ctxt->docNr--;
1909 if (ctxt->docNr > 0)
1910 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1911 else
1912 ctxt->doc = NULL;
1913 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001914 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001915 return (ret);
1916}
1917
1918/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001919 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001920 * @ctxt: the parser context
1921 * @URL: the normalized URL
1922 * @ns: the inherited ns if any
1923 *
1924 * First lookup if the document is already loaded into the parser context,
1925 * check against recursion. If not found the resource is loaded and
1926 * the content is preprocessed before being returned back to the caller.
1927 *
1928 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1929 */
1930static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001931xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1932 const xmlChar * URL, const xmlChar * ns)
1933{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001934 xmlRelaxNGDocumentPtr ret = NULL;
1935 xmlDocPtr doc;
1936 xmlNodePtr root;
1937 int i;
1938
1939 /*
1940 * check against recursion in the stack
1941 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001942 for (i = 0; i < ctxt->docNr; i++) {
1943 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1944 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1945 "Detected an externalRef recursion for %s\n", URL,
1946 NULL);
1947 return (NULL);
1948 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001949 }
1950
1951 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001952 * load the document
1953 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001954 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001955 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001956 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1957 "xmlRelaxNG: could not load %s\n", URL, NULL);
1958 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001959 }
1960
1961 /*
1962 * Allocate the document structures and register it first.
1963 */
1964 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1965 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001966 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1967 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1968 xmlFreeDoc(doc);
1969 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001970 }
1971 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1972 ret->doc = doc;
1973 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001974 ret->next = ctxt->documents;
Daniel Veillard81c51e12009-08-14 18:52:10 +02001975 ret->externalRef = 1;
Daniel Veillardc482e262003-02-26 14:48:48 +00001976 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001977
1978 /*
1979 * transmit the ns if needed
1980 */
1981 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001982 root = xmlDocGetRootElement(doc);
1983 if (root != NULL) {
1984 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1985 xmlSetProp(root, BAD_CAST "ns", ns);
1986 }
1987 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001988 }
1989
1990 /*
1991 * push it on the stack and register it in the hash table
1992 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001993 xmlRelaxNGDocumentPush(ctxt, ret);
1994
1995 /*
1996 * Some preprocessing of the document content
1997 */
1998 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1999 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002000 ctxt->doc = NULL;
2001 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002002 }
2003
2004 xmlRelaxNGDocumentPop(ctxt);
2005
Daniel Veillard4c004142003-10-07 11:33:24 +00002006 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002007}
2008
2009/************************************************************************
2010 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002011 * Error functions *
2012 * *
2013 ************************************************************************/
2014
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002015#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2016#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2017#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2018#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2019#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002020
Daniel Veillard231d7912003-02-09 14:22:17 +00002021static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002022xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2023{
Daniel Veillard231d7912003-02-09 14:22:17 +00002024 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002025 return ("none");
2026 switch (def->type) {
2027 case XML_RELAXNG_EMPTY:
2028 return ("empty");
2029 case XML_RELAXNG_NOT_ALLOWED:
2030 return ("notAllowed");
2031 case XML_RELAXNG_EXCEPT:
2032 return ("except");
2033 case XML_RELAXNG_TEXT:
2034 return ("text");
2035 case XML_RELAXNG_ELEMENT:
2036 return ("element");
2037 case XML_RELAXNG_DATATYPE:
2038 return ("datatype");
2039 case XML_RELAXNG_VALUE:
2040 return ("value");
2041 case XML_RELAXNG_LIST:
2042 return ("list");
2043 case XML_RELAXNG_ATTRIBUTE:
2044 return ("attribute");
2045 case XML_RELAXNG_DEF:
2046 return ("def");
2047 case XML_RELAXNG_REF:
2048 return ("ref");
2049 case XML_RELAXNG_EXTERNALREF:
2050 return ("externalRef");
2051 case XML_RELAXNG_PARENTREF:
2052 return ("parentRef");
2053 case XML_RELAXNG_OPTIONAL:
2054 return ("optional");
2055 case XML_RELAXNG_ZEROORMORE:
2056 return ("zeroOrMore");
2057 case XML_RELAXNG_ONEORMORE:
2058 return ("oneOrMore");
2059 case XML_RELAXNG_CHOICE:
2060 return ("choice");
2061 case XML_RELAXNG_GROUP:
2062 return ("group");
2063 case XML_RELAXNG_INTERLEAVE:
2064 return ("interleave");
2065 case XML_RELAXNG_START:
2066 return ("start");
2067 case XML_RELAXNG_NOOP:
2068 return ("noop");
2069 case XML_RELAXNG_PARAM:
2070 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002071 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002072 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002073}
Daniel Veillardd2298792003-02-14 16:54:11 +00002074
Daniel Veillard6eadf632003-01-23 18:29:16 +00002075/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002076 * xmlRelaxNGGetErrorString:
2077 * @err: the error code
2078 * @arg1: the first string argument
2079 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002080 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002081 * computes a formatted error string for the given error code and args
2082 *
2083 * Returns the error string, it must be deallocated by the caller
2084 */
2085static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002086xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2087 const xmlChar * arg2)
2088{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002089 char msg[1000];
2090
2091 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002092 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002093 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002094 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002095
2096 msg[0] = 0;
2097 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002098 case XML_RELAXNG_OK:
2099 return (NULL);
2100 case XML_RELAXNG_ERR_MEMORY:
2101 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002102 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002103 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2104 break;
2105 case XML_RELAXNG_ERR_TYPEVAL:
2106 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2107 arg2);
2108 break;
2109 case XML_RELAXNG_ERR_DUPID:
2110 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2111 break;
2112 case XML_RELAXNG_ERR_TYPECMP:
2113 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2114 break;
2115 case XML_RELAXNG_ERR_NOSTATE:
2116 return (xmlCharStrdup("Internal error: no state\n"));
2117 case XML_RELAXNG_ERR_NODEFINE:
2118 return (xmlCharStrdup("Internal error: no define\n"));
2119 case XML_RELAXNG_ERR_INTERNAL:
2120 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2121 break;
2122 case XML_RELAXNG_ERR_LISTEXTRA:
2123 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2124 break;
2125 case XML_RELAXNG_ERR_INTERNODATA:
2126 return (xmlCharStrdup
2127 ("Internal: interleave block has no data\n"));
2128 case XML_RELAXNG_ERR_INTERSEQ:
2129 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2130 case XML_RELAXNG_ERR_INTEREXTRA:
2131 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2132 break;
2133 case XML_RELAXNG_ERR_ELEMNAME:
2134 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2135 arg2);
2136 break;
2137 case XML_RELAXNG_ERR_ELEMNONS:
2138 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2139 arg1);
2140 break;
2141 case XML_RELAXNG_ERR_ELEMWRONGNS:
2142 snprintf(msg, 1000,
2143 "Element %s has wrong namespace: expecting %s\n", arg1,
2144 arg2);
2145 break;
2146 case XML_RELAXNG_ERR_ELEMWRONG:
2147 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2148 break;
2149 case XML_RELAXNG_ERR_TEXTWRONG:
2150 snprintf(msg, 1000,
2151 "Did not expect text in element %s content\n", arg1);
2152 break;
2153 case XML_RELAXNG_ERR_ELEMEXTRANS:
2154 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2155 arg1);
2156 break;
2157 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2158 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2159 break;
2160 case XML_RELAXNG_ERR_NOELEM:
2161 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2162 arg1);
2163 break;
2164 case XML_RELAXNG_ERR_NOTELEM:
2165 return (xmlCharStrdup("Expecting an element got text\n"));
2166 case XML_RELAXNG_ERR_ATTRVALID:
2167 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2168 arg1);
2169 break;
2170 case XML_RELAXNG_ERR_CONTENTVALID:
2171 snprintf(msg, 1000, "Element %s failed to validate content\n",
2172 arg1);
2173 break;
2174 case XML_RELAXNG_ERR_EXTRACONTENT:
2175 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2176 arg1, arg2);
2177 break;
2178 case XML_RELAXNG_ERR_INVALIDATTR:
2179 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2180 arg1, arg2);
2181 break;
2182 case XML_RELAXNG_ERR_LACKDATA:
2183 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2184 arg1);
2185 break;
2186 case XML_RELAXNG_ERR_DATAELEM:
2187 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2188 arg1);
2189 break;
2190 case XML_RELAXNG_ERR_VALELEM:
2191 snprintf(msg, 1000, "Value element %s has child elements\n",
2192 arg1);
2193 break;
2194 case XML_RELAXNG_ERR_LISTELEM:
2195 snprintf(msg, 1000, "List element %s has child elements\n",
2196 arg1);
2197 break;
2198 case XML_RELAXNG_ERR_DATATYPE:
2199 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2200 break;
2201 case XML_RELAXNG_ERR_VALUE:
2202 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2203 break;
2204 case XML_RELAXNG_ERR_LIST:
2205 return (xmlCharStrdup("Error validating list\n"));
2206 case XML_RELAXNG_ERR_NOGRAMMAR:
2207 return (xmlCharStrdup("No top grammar defined\n"));
2208 case XML_RELAXNG_ERR_EXTRADATA:
2209 return (xmlCharStrdup("Extra data in the document\n"));
2210 default:
2211 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002212 }
2213 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002214 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002215 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002216 msg[1000 - 1] = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002217 return (xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002218}
2219
2220/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002221 * xmlRelaxNGShowValidError:
2222 * @ctxt: the validation context
2223 * @err: the error number
2224 * @node: the node
2225 * @child: the node child generating the problem.
2226 * @arg1: the first argument
2227 * @arg2: the second argument
2228 *
2229 * Show a validation error.
2230 */
2231static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002232xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2233 xmlRelaxNGValidErr err, xmlNodePtr node,
2234 xmlNodePtr child, const xmlChar * arg1,
2235 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002236{
2237 xmlChar *msg;
2238
Daniel Veillardb30ca312005-09-04 13:50:03 +00002239 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002240 return;
2241
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002242#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002243 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002244#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002245 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2246 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002247 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002248
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002249 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002250 ctxt->errNo = err;
2251 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2252 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002253 xmlFree(msg);
2254}
2255
2256/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002257 * xmlRelaxNGPopErrors:
2258 * @ctxt: the validation context
2259 * @level: the error level in the stack
2260 *
2261 * pop and discard all errors until the given level is reached
2262 */
2263static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002264xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2265{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002266 int i;
2267 xmlRelaxNGValidErrorPtr err;
2268
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002269#ifdef DEBUG_ERROR
2270 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002271 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002272#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002273 for (i = level; i < ctxt->errNr; i++) {
2274 err = &ctxt->errTab[i];
2275 if (err->flags & ERROR_IS_DUP) {
2276 if (err->arg1 != NULL)
2277 xmlFree((xmlChar *) err->arg1);
2278 err->arg1 = NULL;
2279 if (err->arg2 != NULL)
2280 xmlFree((xmlChar *) err->arg2);
2281 err->arg2 = NULL;
2282 err->flags = 0;
2283 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002284 }
2285 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002286 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002287 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002288}
Daniel Veillard4c004142003-10-07 11:33:24 +00002289
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002290/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002291 * xmlRelaxNGDumpValidError:
2292 * @ctxt: the validation context
2293 *
2294 * Show all validation error over a given index.
2295 */
2296static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002297xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2298{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002299 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002300 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002301
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002302#ifdef DEBUG_ERROR
2303 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002304 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002305#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002306 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2307 err = &ctxt->errTab[i];
2308 if (k < MAX_ERROR) {
2309 for (j = 0; j < i; j++) {
2310 dup = &ctxt->errTab[j];
2311 if ((err->err == dup->err) && (err->node == dup->node) &&
2312 (xmlStrEqual(err->arg1, dup->arg1)) &&
2313 (xmlStrEqual(err->arg2, dup->arg2))) {
2314 goto skip;
2315 }
2316 }
2317 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2318 err->arg1, err->arg2);
2319 k++;
2320 }
2321 skip:
2322 if (err->flags & ERROR_IS_DUP) {
2323 if (err->arg1 != NULL)
2324 xmlFree((xmlChar *) err->arg1);
2325 err->arg1 = NULL;
2326 if (err->arg2 != NULL)
2327 xmlFree((xmlChar *) err->arg2);
2328 err->arg2 = NULL;
2329 err->flags = 0;
2330 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002331 }
2332 ctxt->errNr = 0;
2333}
Daniel Veillard4c004142003-10-07 11:33:24 +00002334
Daniel Veillard42f12e92003-03-07 18:32:59 +00002335/**
2336 * xmlRelaxNGAddValidError:
2337 * @ctxt: the validation context
2338 * @err: the error number
2339 * @arg1: the first argument
2340 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002341 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002342 *
2343 * Register a validation error, either generating it if it's sure
2344 * or stacking it for later handling if unsure.
2345 */
2346static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002347xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2348 xmlRelaxNGValidErr err, const xmlChar * arg1,
2349 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002350{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002351 if (ctxt == NULL)
2352 return;
2353 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002354 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002355
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002356#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002357 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002358#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002359 /*
2360 * generate the error directly
2361 */
William M. Brack60929622004-03-27 17:54:18 +00002362 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2363 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002364 xmlNodePtr node, seq;
2365
2366 /*
2367 * Flush first any stacked error which might be the
2368 * real cause of the problem.
2369 */
2370 if (ctxt->errNr != 0)
2371 xmlRelaxNGDumpValidError(ctxt);
2372 if (ctxt->state != NULL) {
2373 node = ctxt->state->node;
2374 seq = ctxt->state->seq;
2375 } else {
2376 node = seq = NULL;
2377 }
Daniel Veillardec18c962009-08-26 18:37:43 +02002378 if ((node == NULL) && (seq == NULL)) {
2379 node = ctxt->pnode;
2380 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002381 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002382 }
2383 /*
2384 * Stack the error for later processing if needed
2385 */
2386 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002387 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002388 }
2389}
2390
Daniel Veillard6eadf632003-01-23 18:29:16 +00002391
2392/************************************************************************
2393 * *
2394 * Type library hooks *
2395 * *
2396 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002397static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002398 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002399
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002400/**
2401 * xmlRelaxNGSchemaTypeHave:
2402 * @data: data needed for the library
2403 * @type: the type name
2404 *
2405 * Check if the given type is provided by
2406 * the W3C XMLSchema Datatype library.
2407 *
2408 * Returns 1 if yes, 0 if no and -1 in case of error.
2409 */
2410static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002411xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2412{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002413 xmlSchemaTypePtr typ;
2414
2415 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002416 return (-1);
2417 typ = xmlSchemaGetPredefinedType(type,
2418 BAD_CAST
2419 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002420 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002421 return (0);
2422 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002423}
2424
2425/**
2426 * xmlRelaxNGSchemaTypeCheck:
2427 * @data: data needed for the library
2428 * @type: the type name
2429 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002430 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002431 *
2432 * Check if the given type and value are validated by
2433 * the W3C XMLSchema Datatype library.
2434 *
2435 * Returns 1 if yes, 0 if no and -1 in case of error.
2436 */
2437static int
2438xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002439 const xmlChar * type,
2440 const xmlChar * value,
2441 void **result, xmlNodePtr node)
2442{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002443 xmlSchemaTypePtr typ;
2444 int ret;
2445
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002446 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002447 return (-1);
2448 typ = xmlSchemaGetPredefinedType(type,
2449 BAD_CAST
2450 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002451 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002452 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002453 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002454 (xmlSchemaValPtr *) result, node);
2455 if (ret == 2) /* special ID error code */
2456 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002457 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002458 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002459 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002460 return (0);
2461 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002462}
2463
2464/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002465 * xmlRelaxNGSchemaFacetCheck:
2466 * @data: data needed for the library
2467 * @type: the type name
2468 * @facet: the facet name
2469 * @val: the facet value
2470 * @strval: the string value
2471 * @value: the value to check
2472 *
2473 * Function provided by a type library to check a value facet
2474 *
2475 * Returns 1 if yes, 0 if no and -1 in case of error.
2476 */
2477static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002478xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2479 const xmlChar * type, const xmlChar * facetname,
2480 const xmlChar * val, const xmlChar * strval,
2481 void *value)
2482{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002483 xmlSchemaFacetPtr facet;
2484 xmlSchemaTypePtr typ;
2485 int ret;
2486
2487 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002488 return (-1);
2489 typ = xmlSchemaGetPredefinedType(type,
2490 BAD_CAST
2491 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002492 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002493 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002494
2495 facet = xmlSchemaNewFacet();
2496 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002497 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002498
Daniel Veillard4c004142003-10-07 11:33:24 +00002499 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002500 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002501 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002502 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002503 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002504 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002505 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002506 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002507 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002508 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002509 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002510 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002511 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002512 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002513 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002514 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002515 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002516 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002517 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002518 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002519 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002520 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2521 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2522 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2523 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002524 xmlSchemaFreeFacet(facet);
2525 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002526 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002527 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002528 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2529 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002530 xmlSchemaFreeFacet(facet);
2531 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002532 }
2533 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2534 xmlSchemaFreeFacet(facet);
2535 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002536 return (-1);
2537 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002538}
2539
2540/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002541 * xmlRelaxNGSchemaFreeValue:
2542 * @data: data needed for the library
2543 * @value: the value to free
2544 *
2545 * Function provided by a type library to free a Schemas value
2546 *
2547 * Returns 1 if yes, 0 if no and -1 in case of error.
2548 */
2549static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002550xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2551{
Daniel Veillard80b19092003-03-28 13:29:53 +00002552 xmlSchemaFreeValue(value);
2553}
2554
2555/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002556 * xmlRelaxNGSchemaTypeCompare:
2557 * @data: data needed for the library
2558 * @type: the type name
2559 * @value1: the first value
2560 * @value2: the second value
2561 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002562 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002563 * Datatype library.
2564 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002565 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002566 */
2567static int
2568xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002569 const xmlChar * type,
2570 const xmlChar * value1,
2571 xmlNodePtr ctxt1,
2572 void *comp1,
2573 const xmlChar * value2, xmlNodePtr ctxt2)
2574{
Daniel Veillard80b19092003-03-28 13:29:53 +00002575 int ret;
2576 xmlSchemaTypePtr typ;
2577 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2578
2579 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002580 return (-1);
2581 typ = xmlSchemaGetPredefinedType(type,
2582 BAD_CAST
2583 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002584 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002585 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002586 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002587 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2588 if (ret != 0)
2589 return (-1);
2590 if (res1 == NULL)
2591 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002592 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002593 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002594 }
2595 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002596 if (ret != 0) {
Daniel Veillardf4644032005-06-13 11:41:31 +00002597 if ((comp1 == NULL) && (res1 != NULL))
2598 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002599 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002600 }
2601 if (res1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002602 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002603 }
2604 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002605 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002606 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002607 xmlSchemaFreeValue(res2);
2608 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002609 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002610 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002611 return (1);
2612 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002613}
Daniel Veillard4c004142003-10-07 11:33:24 +00002614
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002615/**
2616 * xmlRelaxNGDefaultTypeHave:
2617 * @data: data needed for the library
2618 * @type: the type name
2619 *
2620 * Check if the given type is provided by
2621 * the default datatype library.
2622 *
2623 * Returns 1 if yes, 0 if no and -1 in case of error.
2624 */
2625static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002626xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2627 const xmlChar * type)
2628{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002629 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002630 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002631 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002632 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002633 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002634 return (1);
2635 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002636}
2637
2638/**
2639 * xmlRelaxNGDefaultTypeCheck:
2640 * @data: data needed for the library
2641 * @type: the type name
2642 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002643 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002644 *
2645 * Check if the given type and value are validated by
2646 * the default datatype library.
2647 *
2648 * Returns 1 if yes, 0 if no and -1 in case of error.
2649 */
2650static int
2651xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002652 const xmlChar * type ATTRIBUTE_UNUSED,
2653 const xmlChar * value ATTRIBUTE_UNUSED,
2654 void **result ATTRIBUTE_UNUSED,
2655 xmlNodePtr node ATTRIBUTE_UNUSED)
2656{
Daniel Veillardd4310742003-02-18 21:12:46 +00002657 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002658 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002659 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002660 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002661 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002662 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002663 }
2664
Daniel Veillard4c004142003-10-07 11:33:24 +00002665 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002666}
2667
2668/**
2669 * xmlRelaxNGDefaultTypeCompare:
2670 * @data: data needed for the library
2671 * @type: the type name
2672 * @value1: the first value
2673 * @value2: the second value
2674 *
2675 * Compare two values accordingly a type from the default
2676 * datatype library.
2677 *
2678 * Returns 1 if yes, 0 if no and -1 in case of error.
2679 */
2680static int
2681xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002682 const xmlChar * type,
2683 const xmlChar * value1,
2684 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2685 void *comp1 ATTRIBUTE_UNUSED,
2686 const xmlChar * value2,
2687 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2688{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002689 int ret = -1;
2690
2691 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002692 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002693 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002694 if (!xmlStrEqual(value1, value2)) {
2695 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002696
Daniel Veillard4c004142003-10-07 11:33:24 +00002697 /*
2698 * TODO: trivial optimizations are possible by
2699 * computing at compile-time
2700 */
2701 nval = xmlRelaxNGNormalize(NULL, value1);
2702 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002703
Daniel Veillard4c004142003-10-07 11:33:24 +00002704 if ((nval == NULL) || (nvalue == NULL))
2705 ret = -1;
2706 else if (xmlStrEqual(nval, nvalue))
2707 ret = 1;
2708 else
2709 ret = 0;
2710 if (nval != NULL)
2711 xmlFree(nval);
2712 if (nvalue != NULL)
2713 xmlFree(nvalue);
2714 } else
2715 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002716 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002717 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002718}
Daniel Veillard4c004142003-10-07 11:33:24 +00002719
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002720static int xmlRelaxNGTypeInitialized = 0;
2721static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2722
2723/**
2724 * xmlRelaxNGFreeTypeLibrary:
2725 * @lib: the type library structure
2726 * @namespace: the URI bound to the library
2727 *
2728 * Free the structure associated to the type library
2729 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002730static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002731xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002732 const xmlChar * namespace ATTRIBUTE_UNUSED)
2733{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002734 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002735 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002736 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002737 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002738 xmlFree(lib);
2739}
2740
2741/**
2742 * xmlRelaxNGRegisterTypeLibrary:
2743 * @namespace: the URI bound to the library
2744 * @data: data associated to the library
2745 * @have: the provide function
2746 * @check: the checking function
2747 * @comp: the comparison function
2748 *
2749 * Register a new type library
2750 *
2751 * Returns 0 in case of success and -1 in case of error.
2752 */
2753static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002754xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2755 xmlRelaxNGTypeHave have,
2756 xmlRelaxNGTypeCheck check,
2757 xmlRelaxNGTypeCompare comp,
2758 xmlRelaxNGFacetCheck facet,
2759 xmlRelaxNGTypeFree freef)
2760{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002761 xmlRelaxNGTypeLibraryPtr lib;
2762 int ret;
2763
2764 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002765 (check == NULL) || (comp == NULL))
2766 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002767 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002768 xmlGenericError(xmlGenericErrorContext,
2769 "Relax-NG types library '%s' already registered\n",
2770 namespace);
2771 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002772 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002773 lib =
2774 (xmlRelaxNGTypeLibraryPtr)
2775 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002776 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002777 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002778 return (-1);
2779 }
2780 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2781 lib->namespace = xmlStrdup(namespace);
2782 lib->data = data;
2783 lib->have = have;
2784 lib->comp = comp;
2785 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002786 lib->facet = facet;
2787 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002788 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2789 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002790 xmlGenericError(xmlGenericErrorContext,
2791 "Relax-NG types library failed to register '%s'\n",
2792 namespace);
2793 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2794 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002795 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002796 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002797}
2798
2799/**
2800 * xmlRelaxNGInitTypes:
2801 *
2802 * Initilize the default type libraries.
2803 *
2804 * Returns 0 in case of success and -1 in case of error.
2805 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002806int
Daniel Veillard4c004142003-10-07 11:33:24 +00002807xmlRelaxNGInitTypes(void)
2808{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002809 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002810 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002811 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2812 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002813 xmlGenericError(xmlGenericErrorContext,
2814 "Failed to allocate sh table for Relax-NG types\n");
2815 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002816 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002817 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2818 "http://www.w3.org/2001/XMLSchema-datatypes",
2819 NULL, xmlRelaxNGSchemaTypeHave,
2820 xmlRelaxNGSchemaTypeCheck,
2821 xmlRelaxNGSchemaTypeCompare,
2822 xmlRelaxNGSchemaFacetCheck,
2823 xmlRelaxNGSchemaFreeValue);
2824 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2825 xmlRelaxNGDefaultTypeHave,
2826 xmlRelaxNGDefaultTypeCheck,
2827 xmlRelaxNGDefaultTypeCompare, NULL,
2828 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002829 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002830 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002831}
2832
2833/**
2834 * xmlRelaxNGCleanupTypes:
2835 *
2836 * Cleanup the default Schemas type library associated to RelaxNG
2837 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002838void
2839xmlRelaxNGCleanupTypes(void)
2840{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002841 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002842 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002843 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002844 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002845 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002846 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002847}
2848
2849/************************************************************************
2850 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002851 * Compiling element content into regexp *
2852 * *
2853 * Sometime the element content can be compiled into a pure regexp, *
2854 * This allows a faster execution and streamability at that level *
2855 * *
2856 ************************************************************************/
2857
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002858/* from automata.c but not exported */
2859void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2860
2861
Daniel Veillard52b48c72003-04-13 19:53:42 +00002862static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2863 xmlRelaxNGDefinePtr def);
2864
Daniel Veillard952379b2003-03-17 15:37:12 +00002865/**
2866 * xmlRelaxNGIsCompileable:
2867 * @define: the definition to check
2868 *
2869 * Check if a definition is nullable.
2870 *
2871 * Returns 1 if yes, 0 if no and -1 in case of error
2872 */
2873static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002874xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2875{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002876 int ret = -1;
2877
Daniel Veillard952379b2003-03-17 15:37:12 +00002878 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002879 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002880 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002881 if ((def->type != XML_RELAXNG_ELEMENT) &&
2882 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002883 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002884 if ((def->type != XML_RELAXNG_ELEMENT) &&
2885 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002886 return (0);
2887 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002888 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002889 ret = xmlRelaxNGIsCompileable(def->content);
2890 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002891 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002892 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002893 ret = 1;
2894 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002895 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002896 /*
2897 * Check if the element content is compileable
2898 */
2899 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2900 ((def->dflags & IS_COMPILABLE) == 0)) {
2901 xmlRelaxNGDefinePtr list;
2902
2903 list = def->content;
2904 while (list != NULL) {
2905 ret = xmlRelaxNGIsCompileable(list);
2906 if (ret != 1)
2907 break;
2908 list = list->next;
2909 }
William M. Brack60929622004-03-27 17:54:18 +00002910 /*
2911 * Because the routine is recursive, we must guard against
2912 * discovering both COMPILABLE and NOT_COMPILABLE
2913 */
2914 if (ret == 0) {
2915 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002916 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002917 }
2918 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002919 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002920#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002921 if (ret == 1) {
2922 xmlGenericError(xmlGenericErrorContext,
2923 "element content for %s is compilable\n",
2924 def->name);
2925 } else if (ret == 0) {
2926 xmlGenericError(xmlGenericErrorContext,
2927 "element content for %s is not compilable\n",
2928 def->name);
2929 } else {
2930 xmlGenericError(xmlGenericErrorContext,
2931 "Problem in RelaxNGIsCompileable for element %s\n",
2932 def->name);
2933 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002934#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002935 }
2936 /*
2937 * All elements return a compileable status unless they
2938 * are generic like anyName
2939 */
2940 if ((def->nameClass != NULL) || (def->name == NULL))
2941 ret = 0;
2942 else
2943 ret = 1;
2944 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002945 case XML_RELAXNG_REF:
2946 case XML_RELAXNG_EXTERNALREF:
2947 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002948 if (def->depth == -20) {
2949 return (1);
2950 } else {
2951 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002952
Daniel Veillard4c004142003-10-07 11:33:24 +00002953 def->depth = -20;
2954 list = def->content;
2955 while (list != NULL) {
2956 ret = xmlRelaxNGIsCompileable(list);
2957 if (ret != 1)
2958 break;
2959 list = list->next;
2960 }
2961 }
2962 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002963 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002964 case XML_RELAXNG_OPTIONAL:
2965 case XML_RELAXNG_ZEROORMORE:
2966 case XML_RELAXNG_ONEORMORE:
2967 case XML_RELAXNG_CHOICE:
2968 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002969 case XML_RELAXNG_DEF:{
2970 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002971
Daniel Veillard4c004142003-10-07 11:33:24 +00002972 list = def->content;
2973 while (list != NULL) {
2974 ret = xmlRelaxNGIsCompileable(list);
2975 if (ret != 1)
2976 break;
2977 list = list->next;
2978 }
2979 break;
2980 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002981 case XML_RELAXNG_EXCEPT:
2982 case XML_RELAXNG_ATTRIBUTE:
2983 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002984 case XML_RELAXNG_DATATYPE:
2985 case XML_RELAXNG_LIST:
2986 case XML_RELAXNG_PARAM:
2987 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002988 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002989 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002990 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002991 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002992 if (ret == 0)
2993 def->dflags |= IS_NOT_COMPILABLE;
2994 if (ret == 1)
2995 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002996#ifdef DEBUG_COMPILE
2997 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002998 xmlGenericError(xmlGenericErrorContext,
2999 "RelaxNGIsCompileable %s : true\n",
3000 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003001 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003002 xmlGenericError(xmlGenericErrorContext,
3003 "RelaxNGIsCompileable %s : false\n",
3004 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003005 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003006 xmlGenericError(xmlGenericErrorContext,
3007 "Problem in RelaxNGIsCompileable %s\n",
3008 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003009 }
3010#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003011 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003012}
3013
3014/**
3015 * xmlRelaxNGCompile:
3016 * ctxt: the RelaxNG parser context
3017 * @define: the definition tree to compile
3018 *
3019 * Compile the set of definitions, it works recursively, till the
3020 * element boundaries, where it tries to compile the content if possible
3021 *
3022 * Returns 0 if success and -1 in case of error
3023 */
3024static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003025xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3026{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003027 int ret = 0;
3028 xmlRelaxNGDefinePtr list;
3029
Daniel Veillard4c004142003-10-07 11:33:24 +00003030 if ((ctxt == NULL) || (def == NULL))
3031 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003032
Daniel Veillard4c004142003-10-07 11:33:24 +00003033 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003034 case XML_RELAXNG_START:
3035 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003036 xmlAutomataPtr oldam = ctxt->am;
3037 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003038
3039 def->depth = -25;
3040
Daniel Veillard4c004142003-10-07 11:33:24 +00003041 list = def->content;
3042 ctxt->am = xmlNewAutomata();
3043 if (ctxt->am == NULL)
3044 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003045
3046 /*
3047 * assume identical strings but not same pointer are different
3048 * atoms, needed for non-determinism detection
3049 * That way if 2 elements with the same name are in a choice
3050 * branch the automata is found non-deterministic and
3051 * we fallback to the normal validation which does the right
3052 * thing of exploring both choices.
3053 */
3054 xmlAutomataSetFlags(ctxt->am, 1);
3055
Daniel Veillard4c004142003-10-07 11:33:24 +00003056 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3057 while (list != NULL) {
3058 xmlRelaxNGCompile(ctxt, list);
3059 list = list->next;
3060 }
3061 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3062 def->contModel = xmlAutomataCompile(ctxt->am);
3063 xmlRegexpIsDeterminist(def->contModel);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003064
Daniel Veillard4c004142003-10-07 11:33:24 +00003065 xmlFreeAutomata(ctxt->am);
3066 ctxt->state = oldstate;
3067 ctxt->am = oldam;
3068 }
3069 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003070 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003071 if ((ctxt->am != NULL) && (def->name != NULL)) {
3072 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3073 ctxt->state, NULL,
3074 def->name, def->ns,
3075 def);
3076 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003077 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003078 xmlAutomataPtr oldam = ctxt->am;
3079 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003080
3081 def->depth = -25;
3082
Daniel Veillard4c004142003-10-07 11:33:24 +00003083 list = def->content;
3084 ctxt->am = xmlNewAutomata();
3085 if (ctxt->am == NULL)
3086 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003087 xmlAutomataSetFlags(ctxt->am, 1);
Daniel Veillard4c004142003-10-07 11:33:24 +00003088 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3089 while (list != NULL) {
3090 xmlRelaxNGCompile(ctxt, list);
3091 list = list->next;
3092 }
3093 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3094 def->contModel = xmlAutomataCompile(ctxt->am);
3095 if (!xmlRegexpIsDeterminist(def->contModel)) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003096#ifdef DEBUG_COMPILE
3097 xmlGenericError(xmlGenericErrorContext,
3098 "Content model not determinist %s\n",
3099 def->name);
3100#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003101 /*
3102 * we can only use the automata if it is determinist
3103 */
3104 xmlRegFreeRegexp(def->contModel);
3105 def->contModel = NULL;
3106 }
3107 xmlFreeAutomata(ctxt->am);
3108 ctxt->state = oldstate;
3109 ctxt->am = oldam;
3110 } else {
3111 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003112
Daniel Veillard4c004142003-10-07 11:33:24 +00003113 /*
3114 * we can't build the content model for this element content
3115 * but it still might be possible to build it for some of its
3116 * children, recurse.
3117 */
3118 ret = xmlRelaxNGTryCompile(ctxt, def);
3119 ctxt->am = oldam;
3120 }
3121 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003122 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003123 ret = xmlRelaxNGCompile(ctxt, def->content);
3124 break;
3125 case XML_RELAXNG_OPTIONAL:{
3126 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003127
Daniel Veillardfd780772009-08-26 18:35:29 +02003128 list = def->content;
3129 while (list != NULL) {
3130 xmlRelaxNGCompile(ctxt, list);
3131 list = list->next;
3132 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003133 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3134 break;
3135 }
3136 case XML_RELAXNG_ZEROORMORE:{
3137 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003138
Daniel Veillard4c004142003-10-07 11:33:24 +00003139 ctxt->state =
3140 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3141 oldstate = ctxt->state;
3142 list = def->content;
3143 while (list != NULL) {
3144 xmlRelaxNGCompile(ctxt, list);
3145 list = list->next;
3146 }
3147 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3148 ctxt->state =
3149 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3150 break;
3151 }
3152 case XML_RELAXNG_ONEORMORE:{
3153 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003154
Daniel Veillard4c004142003-10-07 11:33:24 +00003155 list = def->content;
3156 while (list != NULL) {
3157 xmlRelaxNGCompile(ctxt, list);
3158 list = list->next;
3159 }
3160 oldstate = ctxt->state;
3161 list = def->content;
3162 while (list != NULL) {
3163 xmlRelaxNGCompile(ctxt, list);
3164 list = list->next;
3165 }
3166 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3167 ctxt->state =
3168 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3169 break;
3170 }
3171 case XML_RELAXNG_CHOICE:{
3172 xmlAutomataStatePtr target = NULL;
3173 xmlAutomataStatePtr oldstate = ctxt->state;
3174
3175 list = def->content;
3176 while (list != NULL) {
3177 ctxt->state = oldstate;
3178 ret = xmlRelaxNGCompile(ctxt, list);
3179 if (ret != 0)
3180 break;
3181 if (target == NULL)
3182 target = ctxt->state;
3183 else {
3184 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3185 target);
3186 }
3187 list = list->next;
3188 }
3189 ctxt->state = target;
3190
3191 break;
3192 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003193 case XML_RELAXNG_REF:
3194 case XML_RELAXNG_EXTERNALREF:
3195 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003196 case XML_RELAXNG_GROUP:
3197 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003198 list = def->content;
3199 while (list != NULL) {
3200 ret = xmlRelaxNGCompile(ctxt, list);
3201 if (ret != 0)
3202 break;
3203 list = list->next;
3204 }
3205 break;
3206 case XML_RELAXNG_TEXT:{
3207 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003208
Daniel Veillard4c004142003-10-07 11:33:24 +00003209 ctxt->state =
3210 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3211 oldstate = ctxt->state;
3212 xmlRelaxNGCompile(ctxt, def->content);
3213 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3214 ctxt->state, BAD_CAST "#text",
3215 NULL);
3216 ctxt->state =
3217 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3218 break;
3219 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003220 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003221 ctxt->state =
3222 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3223 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003224 case XML_RELAXNG_EXCEPT:
3225 case XML_RELAXNG_ATTRIBUTE:
3226 case XML_RELAXNG_INTERLEAVE:
3227 case XML_RELAXNG_NOT_ALLOWED:
3228 case XML_RELAXNG_DATATYPE:
3229 case XML_RELAXNG_LIST:
3230 case XML_RELAXNG_PARAM:
3231 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003232 /* This should not happen and generate an internal error */
3233 fprintf(stderr, "RNG internal error trying to compile %s\n",
3234 xmlRelaxNGDefName(def));
3235 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003236 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003237 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003238}
3239
3240/**
3241 * xmlRelaxNGTryCompile:
3242 * ctxt: the RelaxNG parser context
3243 * @define: the definition tree to compile
3244 *
3245 * Try to compile the set of definitions, it works recursively,
3246 * possibly ignoring parts which cannot be compiled.
3247 *
3248 * Returns 0 if success and -1 in case of error
3249 */
3250static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003251xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3252{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003253 int ret = 0;
3254 xmlRelaxNGDefinePtr list;
3255
Daniel Veillard4c004142003-10-07 11:33:24 +00003256 if ((ctxt == NULL) || (def == NULL))
3257 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003258
3259 if ((def->type == XML_RELAXNG_START) ||
3260 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003261 ret = xmlRelaxNGIsCompileable(def);
3262 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3263 ctxt->am = NULL;
3264 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003265#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003266 if (ret == 0) {
3267 if (def->type == XML_RELAXNG_START)
3268 xmlGenericError(xmlGenericErrorContext,
3269 "compiled the start\n");
3270 else
3271 xmlGenericError(xmlGenericErrorContext,
3272 "compiled element %s\n", def->name);
3273 } else {
3274 if (def->type == XML_RELAXNG_START)
3275 xmlGenericError(xmlGenericErrorContext,
3276 "failed to compile the start\n");
3277 else
3278 xmlGenericError(xmlGenericErrorContext,
3279 "failed to compile element %s\n",
3280 def->name);
3281 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003282#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003283 return (ret);
3284 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003285 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003286 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003287 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003288 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3289 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003290 case XML_RELAXNG_TEXT:
3291 case XML_RELAXNG_DATATYPE:
3292 case XML_RELAXNG_LIST:
3293 case XML_RELAXNG_PARAM:
3294 case XML_RELAXNG_VALUE:
3295 case XML_RELAXNG_EMPTY:
3296 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003297 ret = 0;
3298 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003299 case XML_RELAXNG_OPTIONAL:
3300 case XML_RELAXNG_ZEROORMORE:
3301 case XML_RELAXNG_ONEORMORE:
3302 case XML_RELAXNG_CHOICE:
3303 case XML_RELAXNG_GROUP:
3304 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003305 case XML_RELAXNG_START:
3306 case XML_RELAXNG_REF:
3307 case XML_RELAXNG_EXTERNALREF:
3308 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003309 list = def->content;
3310 while (list != NULL) {
3311 ret = xmlRelaxNGTryCompile(ctxt, list);
3312 if (ret != 0)
3313 break;
3314 list = list->next;
3315 }
3316 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003317 case XML_RELAXNG_EXCEPT:
3318 case XML_RELAXNG_ATTRIBUTE:
3319 case XML_RELAXNG_INTERLEAVE:
3320 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003321 ret = 0;
3322 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003323 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003324 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003325}
3326
3327/************************************************************************
3328 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003329 * Parsing functions *
3330 * *
3331 ************************************************************************/
3332
Daniel Veillard4c004142003-10-07 11:33:24 +00003333static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3334 ctxt, xmlNodePtr node);
3335static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3336 ctxt, xmlNodePtr node);
3337static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3338 ctxt, xmlNodePtr nodes,
3339 int group);
3340static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3341 ctxt, xmlNodePtr node);
3342static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3343 xmlNodePtr node);
3344static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3345 xmlNodePtr nodes);
3346static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3347 ctxt, xmlNodePtr node,
3348 xmlRelaxNGDefinePtr
3349 def);
3350static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3351 ctxt, xmlNodePtr nodes);
3352static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3353 xmlRelaxNGDefinePtr define,
3354 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003355
3356
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003357#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003358
3359/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003360 * xmlRelaxNGIsNullable:
3361 * @define: the definition to verify
3362 *
3363 * Check if a definition is nullable.
3364 *
3365 * Returns 1 if yes, 0 if no and -1 in case of error
3366 */
3367static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003368xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3369{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003370 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003371
Daniel Veillardfd573f12003-03-16 17:52:32 +00003372 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003373 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003374
Daniel Veillarde063f482003-03-21 16:53:17 +00003375 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003376 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003377 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003378 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003379 switch (define->type) {
3380 case XML_RELAXNG_EMPTY:
3381 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003382 ret = 1;
3383 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003384 case XML_RELAXNG_NOOP:
3385 case XML_RELAXNG_DEF:
3386 case XML_RELAXNG_REF:
3387 case XML_RELAXNG_EXTERNALREF:
3388 case XML_RELAXNG_PARENTREF:
3389 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003390 ret = xmlRelaxNGIsNullable(define->content);
3391 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003392 case XML_RELAXNG_EXCEPT:
3393 case XML_RELAXNG_NOT_ALLOWED:
3394 case XML_RELAXNG_ELEMENT:
3395 case XML_RELAXNG_DATATYPE:
3396 case XML_RELAXNG_PARAM:
3397 case XML_RELAXNG_VALUE:
3398 case XML_RELAXNG_LIST:
3399 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003400 ret = 0;
3401 break;
3402 case XML_RELAXNG_CHOICE:{
3403 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003404
Daniel Veillard4c004142003-10-07 11:33:24 +00003405 while (list != NULL) {
3406 ret = xmlRelaxNGIsNullable(list);
3407 if (ret != 0)
3408 goto done;
3409 list = list->next;
3410 }
3411 ret = 0;
3412 break;
3413 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003414 case XML_RELAXNG_START:
3415 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003416 case XML_RELAXNG_GROUP:{
3417 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003418
Daniel Veillard4c004142003-10-07 11:33:24 +00003419 while (list != NULL) {
3420 ret = xmlRelaxNGIsNullable(list);
3421 if (ret != 1)
3422 goto done;
3423 list = list->next;
3424 }
3425 return (1);
3426 }
3427 default:
3428 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003429 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003430 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003431 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003432 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003433 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003434 define->dflags |= IS_NULLABLE;
3435 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003436}
3437
3438/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003439 * xmlRelaxNGIsBlank:
3440 * @str: a string
3441 *
3442 * Check if a string is ignorable c.f. 4.2. Whitespace
3443 *
3444 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3445 */
3446static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003447xmlRelaxNGIsBlank(xmlChar * str)
3448{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003449 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003450 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003451 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003452 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003453 return (0);
3454 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003455 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003456 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003457}
3458
Daniel Veillard6eadf632003-01-23 18:29:16 +00003459/**
3460 * xmlRelaxNGGetDataTypeLibrary:
3461 * @ctxt: a Relax-NG parser context
3462 * @node: the current data or value element
3463 *
3464 * Applies algorithm from 4.3. datatypeLibrary attribute
3465 *
3466 * Returns the datatypeLibary value or NULL if not found
3467 */
3468static xmlChar *
3469xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003470 xmlNodePtr node)
3471{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003472 xmlChar *ret, *escape;
3473
Daniel Veillardd44b9362009-09-07 12:15:08 +02003474 if (node == NULL)
3475 return(NULL);
3476
Daniel Veillard6eadf632003-01-23 18:29:16 +00003477 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003478 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3479 if (ret != NULL) {
3480 if (ret[0] == 0) {
3481 xmlFree(ret);
3482 return (NULL);
3483 }
3484 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3485 if (escape == NULL) {
3486 return (ret);
3487 }
3488 xmlFree(ret);
3489 return (escape);
3490 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003491 }
3492 node = node->parent;
3493 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003494 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3495 if (ret != NULL) {
3496 if (ret[0] == 0) {
3497 xmlFree(ret);
3498 return (NULL);
3499 }
3500 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3501 if (escape == NULL) {
3502 return (ret);
3503 }
3504 xmlFree(ret);
3505 return (escape);
3506 }
3507 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003508 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003509 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003510}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003511
3512/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003513 * xmlRelaxNGParseValue:
3514 * @ctxt: a Relax-NG parser context
3515 * @node: the data node.
3516 *
3517 * parse the content of a RelaxNG value node.
3518 *
3519 * Returns the definition pointer or NULL in case of error
3520 */
3521static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003522xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3523{
Daniel Veillardedc91922003-01-26 00:52:04 +00003524 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003525 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003526 xmlChar *type;
3527 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003528 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003529
Daniel Veillardfd573f12003-03-16 17:52:32 +00003530 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003531 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003532 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003533 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003534
3535 type = xmlGetProp(node, BAD_CAST "type");
3536 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003537 xmlRelaxNGNormExtSpace(type);
3538 if (xmlValidateNCName(type, 0)) {
3539 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3540 "value type '%s' is not an NCName\n", type, NULL);
3541 }
3542 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3543 if (library == NULL)
3544 library =
3545 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003546
Daniel Veillard4c004142003-10-07 11:33:24 +00003547 def->name = type;
3548 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003549
Daniel Veillard4c004142003-10-07 11:33:24 +00003550 lib = (xmlRelaxNGTypeLibraryPtr)
3551 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3552 if (lib == NULL) {
3553 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3554 "Use of unregistered type library '%s'\n", library,
3555 NULL);
3556 def->data = NULL;
3557 } else {
3558 def->data = lib;
3559 if (lib->have == NULL) {
3560 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3561 "Internal error with type library '%s': no 'have'\n",
3562 library, NULL);
3563 } else {
3564 success = lib->have(lib->data, def->name);
3565 if (success != 1) {
3566 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3567 "Error type '%s' is not exported by type library '%s'\n",
3568 def->name, library);
3569 }
3570 }
3571 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003572 }
3573 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003574 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003575 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003576 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3577 (node->children->next != NULL)) {
3578 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3579 "Expecting a single text value for <value>content\n",
3580 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003581 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003582 def->value = xmlNodeGetContent(node);
3583 if (def->value == NULL) {
3584 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3585 "Element <value> has no content\n", NULL, NULL);
3586 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3587 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003588
Daniel Veillard4c004142003-10-07 11:33:24 +00003589 success =
3590 lib->check(lib->data, def->name, def->value, &val, node);
3591 if (success != 1) {
3592 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3593 "Value '%s' is not acceptable for type '%s'\n",
3594 def->value, def->name);
3595 } else {
3596 if (val != NULL)
3597 def->attrs = val;
3598 }
3599 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003600 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003601 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003602}
3603
3604/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003605 * xmlRelaxNGParseData:
3606 * @ctxt: a Relax-NG parser context
3607 * @node: the data node.
3608 *
3609 * parse the content of a RelaxNG data node.
3610 *
3611 * Returns the definition pointer or NULL in case of error
3612 */
3613static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003614xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3615{
Daniel Veillard14b56432006-03-09 18:41:40 +00003616 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003617 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003618 xmlRelaxNGTypeLibraryPtr lib;
3619 xmlChar *type;
3620 xmlChar *library;
3621 xmlNodePtr content;
3622 int tmp;
3623
3624 type = xmlGetProp(node, BAD_CAST "type");
3625 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003626 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3627 NULL);
3628 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003629 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003630 xmlRelaxNGNormExtSpace(type);
3631 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003632 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3633 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003634 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003635 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3636 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003637 library =
3638 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003639
Daniel Veillardfd573f12003-03-16 17:52:32 +00003640 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003641 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003642 xmlFree(type);
3643 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003644 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003645 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003646 def->name = type;
3647 def->ns = library;
3648
3649 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003650 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003651 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003652 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3653 "Use of unregistered type library '%s'\n", library,
3654 NULL);
3655 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003656 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003657 def->data = lib;
3658 if (lib->have == NULL) {
3659 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3660 "Internal error with type library '%s': no 'have'\n",
3661 library, NULL);
3662 } else {
3663 tmp = lib->have(lib->data, def->name);
3664 if (tmp != 1) {
3665 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3666 "Error type '%s' is not exported by type library '%s'\n",
3667 def->name, library);
3668 } else
3669 if ((xmlStrEqual
3670 (library,
3671 BAD_CAST
3672 "http://www.w3.org/2001/XMLSchema-datatypes"))
3673 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3674 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3675 ctxt->idref = 1;
3676 }
3677 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003678 }
3679 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003680
3681 /*
3682 * Handle optional params
3683 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003684 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003685 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3686 break;
3687 if (xmlStrEqual(library,
3688 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3689 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3690 "Type library '%s' does not allow type parameters\n",
3691 library, NULL);
3692 content = content->next;
3693 while ((content != NULL) &&
3694 (xmlStrEqual(content->name, BAD_CAST "param")))
3695 content = content->next;
3696 } else {
3697 param = xmlRelaxNGNewDefine(ctxt, node);
3698 if (param != NULL) {
3699 param->type = XML_RELAXNG_PARAM;
3700 param->name = xmlGetProp(content, BAD_CAST "name");
3701 if (param->name == NULL) {
3702 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3703 "param has no name\n", NULL, NULL);
3704 }
3705 param->value = xmlNodeGetContent(content);
3706 if (lastparam == NULL) {
3707 def->attrs = lastparam = param;
3708 } else {
3709 lastparam->next = param;
3710 lastparam = param;
3711 }
3712 if (lib != NULL) {
3713 }
3714 }
3715 content = content->next;
3716 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003717 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003718 /*
3719 * Handle optional except
3720 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003721 if ((content != NULL)
3722 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3723 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003724 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003725
Daniel Veillard4c004142003-10-07 11:33:24 +00003726 except = xmlRelaxNGNewDefine(ctxt, node);
3727 if (except == NULL) {
3728 return (def);
3729 }
3730 except->type = XML_RELAXNG_EXCEPT;
3731 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003732 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003733 if (child == NULL) {
3734 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3735 "except has no content\n", NULL, NULL);
3736 }
3737 while (child != NULL) {
3738 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3739 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003740 if (last == NULL) {
3741 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003742 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003743 last->next = tmp2;
3744 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003745 }
3746 }
3747 child = child->next;
3748 }
3749 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003750 }
3751 /*
3752 * Check there is no unhandled data
3753 */
3754 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003755 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3756 "Element data has unexpected content %s\n",
3757 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003758 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003759
Daniel Veillard4c004142003-10-07 11:33:24 +00003760 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003761}
3762
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003763static const xmlChar *invalidName = BAD_CAST "\1";
3764
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003765/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003766 * xmlRelaxNGCompareNameClasses:
3767 * @defs1: the first element/attribute defs
3768 * @defs2: the second element/attribute defs
3769 * @name: the restriction on the name
3770 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003771 *
3772 * Compare the 2 lists of element definitions. The comparison is
3773 * that if both lists do not accept the same QNames, it returns 1
3774 * If the 2 lists can accept the same QName the comparison returns 0
3775 *
3776 * Returns 1 disttinct, 0 if equal
3777 */
3778static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003779xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003780 xmlRelaxNGDefinePtr def2)
3781{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003782 int ret = 1;
3783 xmlNode node;
3784 xmlNs ns;
3785 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003786
Daniel Veillard42f12e92003-03-07 18:32:59 +00003787 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3788
Daniel Veillardb30ca312005-09-04 13:50:03 +00003789 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3790
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003791 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003792 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3793 if (def2->type == XML_RELAXNG_TEXT)
3794 return (1);
3795 if (def1->name != NULL) {
3796 node.name = def1->name;
3797 } else {
3798 node.name = invalidName;
3799 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003800 if (def1->ns != NULL) {
3801 if (def1->ns[0] == 0) {
3802 node.ns = NULL;
3803 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003804 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003805 ns.href = def1->ns;
3806 }
3807 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003808 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003809 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003810 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003811 if (def1->nameClass != NULL) {
3812 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3813 } else {
3814 ret = 0;
3815 }
3816 } else {
3817 ret = 1;
3818 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003819 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003820 if (def2->type == XML_RELAXNG_TEXT)
3821 return (0);
3822 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003823 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003824 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003825 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003826 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003827 }
3828 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003829 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003830 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003831 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3832 if (def2->name != NULL) {
3833 node.name = def2->name;
3834 } else {
3835 node.name = invalidName;
3836 }
3837 node.ns = &ns;
3838 if (def2->ns != NULL) {
3839 if (def2->ns[0] == 0) {
3840 node.ns = NULL;
3841 } else {
3842 ns.href = def2->ns;
3843 }
3844 } else {
3845 ns.href = invalidName;
3846 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003847 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003848 if (def2->nameClass != NULL) {
3849 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3850 } else {
3851 ret = 0;
3852 }
3853 } else {
3854 ret = 1;
3855 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003856 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003857 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003858 }
3859
Daniel Veillard4c004142003-10-07 11:33:24 +00003860 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003861}
3862
3863/**
3864 * xmlRelaxNGCompareElemDefLists:
3865 * @ctxt: a Relax-NG parser context
3866 * @defs1: the first list of element/attribute defs
3867 * @defs2: the second list of element/attribute defs
3868 *
3869 * Compare the 2 lists of element or attribute definitions. The comparison
3870 * is that if both lists do not accept the same QNames, it returns 1
3871 * If the 2 lists can accept the same QName the comparison returns 0
3872 *
3873 * Returns 1 disttinct, 0 if equal
3874 */
3875static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003876xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3877 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3878 xmlRelaxNGDefinePtr * def2)
3879{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003880 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003881
Daniel Veillard154877e2003-01-30 12:17:05 +00003882 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003883 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003884 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003885 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003886 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003887 while ((*def2) != NULL) {
3888 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3889 return (0);
3890 def2++;
3891 }
3892 def2 = basedef2;
3893 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003894 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003895 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003896}
3897
3898/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003899 * xmlRelaxNGGenerateAttributes:
3900 * @ctxt: a Relax-NG parser context
3901 * @def: the definition definition
3902 *
3903 * Check if the definition can only generate attributes
3904 *
3905 * Returns 1 if yes, 0 if no and -1 in case of error.
3906 */
3907static int
3908xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003909 xmlRelaxNGDefinePtr def)
3910{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003911 xmlRelaxNGDefinePtr parent, cur, tmp;
3912
3913 /*
3914 * Don't run that check in case of error. Infinite recursion
3915 * becomes possible.
3916 */
3917 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003918 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003919
3920 parent = NULL;
3921 cur = def;
3922 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003923 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3924 (cur->type == XML_RELAXNG_TEXT) ||
3925 (cur->type == XML_RELAXNG_DATATYPE) ||
3926 (cur->type == XML_RELAXNG_PARAM) ||
3927 (cur->type == XML_RELAXNG_LIST) ||
3928 (cur->type == XML_RELAXNG_VALUE) ||
3929 (cur->type == XML_RELAXNG_EMPTY))
3930 return (0);
3931 if ((cur->type == XML_RELAXNG_CHOICE) ||
3932 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3933 (cur->type == XML_RELAXNG_GROUP) ||
3934 (cur->type == XML_RELAXNG_ONEORMORE) ||
3935 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3936 (cur->type == XML_RELAXNG_OPTIONAL) ||
3937 (cur->type == XML_RELAXNG_PARENTREF) ||
3938 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3939 (cur->type == XML_RELAXNG_REF) ||
3940 (cur->type == XML_RELAXNG_DEF)) {
3941 if (cur->content != NULL) {
3942 parent = cur;
3943 cur = cur->content;
3944 tmp = cur;
3945 while (tmp != NULL) {
3946 tmp->parent = parent;
3947 tmp = tmp->next;
3948 }
3949 continue;
3950 }
3951 }
3952 if (cur == def)
3953 break;
3954 if (cur->next != NULL) {
3955 cur = cur->next;
3956 continue;
3957 }
3958 do {
3959 cur = cur->parent;
3960 if (cur == NULL)
3961 break;
3962 if (cur == def)
3963 return (1);
3964 if (cur->next != NULL) {
3965 cur = cur->next;
3966 break;
3967 }
3968 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003969 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003970 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003971}
Daniel Veillard4c004142003-10-07 11:33:24 +00003972
Daniel Veillardce192eb2003-04-16 15:58:05 +00003973/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003974 * xmlRelaxNGGetElements:
3975 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003976 * @def: the definition definition
3977 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003978 *
3979 * Compute the list of top elements a definition can generate
3980 *
3981 * Returns a list of elements or NULL if none was found.
3982 */
3983static xmlRelaxNGDefinePtr *
3984xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003985 xmlRelaxNGDefinePtr def, int eora)
3986{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003987 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003988 int len = 0;
3989 int max = 0;
3990
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003991 /*
3992 * Don't run that check in case of error. Infinite recursion
3993 * becomes possible.
3994 */
3995 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003996 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003997
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003998 parent = NULL;
3999 cur = def;
4000 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004001 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4002 (cur->type == XML_RELAXNG_TEXT))) ||
4003 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
4004 if (ret == NULL) {
4005 max = 10;
4006 ret = (xmlRelaxNGDefinePtr *)
4007 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4008 if (ret == NULL) {
4009 xmlRngPErrMemory(ctxt, "getting element list\n");
4010 return (NULL);
4011 }
4012 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00004013 xmlRelaxNGDefinePtr *temp;
4014
Daniel Veillard4c004142003-10-07 11:33:24 +00004015 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00004016 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00004017 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00004018 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004019 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00004020 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00004021 return (NULL);
4022 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00004023 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00004024 }
4025 ret[len++] = cur;
4026 ret[len] = NULL;
4027 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4028 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4029 (cur->type == XML_RELAXNG_GROUP) ||
4030 (cur->type == XML_RELAXNG_ONEORMORE) ||
4031 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4032 (cur->type == XML_RELAXNG_OPTIONAL) ||
4033 (cur->type == XML_RELAXNG_PARENTREF) ||
4034 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004035 (cur->type == XML_RELAXNG_DEF) ||
4036 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004037 /*
4038 * Don't go within elements or attributes or string values.
4039 * Just gather the element top list
4040 */
4041 if (cur->content != NULL) {
4042 parent = cur;
4043 cur = cur->content;
4044 tmp = cur;
4045 while (tmp != NULL) {
4046 tmp->parent = parent;
4047 tmp = tmp->next;
4048 }
4049 continue;
4050 }
4051 }
4052 if (cur == def)
4053 break;
4054 if (cur->next != NULL) {
4055 cur = cur->next;
4056 continue;
4057 }
4058 do {
4059 cur = cur->parent;
4060 if (cur == NULL)
4061 break;
4062 if (cur == def)
4063 return (ret);
4064 if (cur->next != NULL) {
4065 cur = cur->next;
4066 break;
4067 }
4068 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004069 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004070 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004071}
Daniel Veillard4c004142003-10-07 11:33:24 +00004072
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004073/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004074 * xmlRelaxNGCheckChoiceDeterminism:
4075 * @ctxt: a Relax-NG parser context
4076 * @def: the choice definition
4077 *
4078 * Also used to find indeterministic pattern in choice
4079 */
4080static void
4081xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004082 xmlRelaxNGDefinePtr def)
4083{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004084 xmlRelaxNGDefinePtr **list;
4085 xmlRelaxNGDefinePtr cur;
4086 int nbchild = 0, i, j, ret;
4087 int is_nullable = 0;
4088 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004089 xmlHashTablePtr triage = NULL;
4090 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004091
Daniel Veillard4c004142003-10-07 11:33:24 +00004092 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4093 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004094
Daniel Veillarde063f482003-03-21 16:53:17 +00004095 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004096 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004097
Daniel Veillardfd573f12003-03-16 17:52:32 +00004098 /*
4099 * Don't run that check in case of error. Infinite recursion
4100 * becomes possible.
4101 */
4102 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004103 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004104
4105 is_nullable = xmlRelaxNGIsNullable(def);
4106
4107 cur = def->content;
4108 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004109 nbchild++;
4110 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004111 }
4112
4113 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004114 sizeof(xmlRelaxNGDefinePtr
4115 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004116 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004117 xmlRngPErrMemory(ctxt, "building choice\n");
4118 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004119 }
4120 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004121 /*
4122 * a bit strong but safe
4123 */
4124 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004125 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004126 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004127 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004128 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004129 cur = def->content;
4130 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004131 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4132 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4133 is_triable = 0;
4134 } else if (is_triable == 1) {
4135 xmlRelaxNGDefinePtr *tmp;
4136 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004137
Daniel Veillard4c004142003-10-07 11:33:24 +00004138 tmp = list[i];
4139 while ((*tmp != NULL) && (is_triable == 1)) {
4140 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4141 res = xmlHashAddEntry2(triage,
4142 BAD_CAST "#text", NULL,
4143 (void *) cur);
4144 if (res != 0)
4145 is_triable = -1;
4146 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4147 ((*tmp)->name != NULL)) {
4148 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4149 res = xmlHashAddEntry2(triage,
4150 (*tmp)->name, NULL,
4151 (void *) cur);
4152 else
4153 res = xmlHashAddEntry2(triage,
4154 (*tmp)->name, (*tmp)->ns,
4155 (void *) cur);
4156 if (res != 0)
4157 is_triable = -1;
4158 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4159 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4160 res = xmlHashAddEntry2(triage,
4161 BAD_CAST "#any", NULL,
4162 (void *) cur);
4163 else
4164 res = xmlHashAddEntry2(triage,
4165 BAD_CAST "#any", (*tmp)->ns,
4166 (void *) cur);
4167 if (res != 0)
4168 is_triable = -1;
4169 } else {
4170 is_triable = -1;
4171 }
4172 tmp++;
4173 }
4174 }
4175 i++;
4176 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004177 }
4178
Daniel Veillard4c004142003-10-07 11:33:24 +00004179 for (i = 0; i < nbchild; i++) {
4180 if (list[i] == NULL)
4181 continue;
4182 for (j = 0; j < i; j++) {
4183 if (list[j] == NULL)
4184 continue;
4185 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4186 if (ret == 0) {
4187 is_indeterminist = 1;
4188 }
4189 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004190 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004191 for (i = 0; i < nbchild; i++) {
4192 if (list[i] != NULL)
4193 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004194 }
4195
4196 xmlFree(list);
4197 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004198 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004199 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004200 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004201 def->dflags |= IS_TRIABLE;
4202 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004203 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004204 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004205 }
4206 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004207}
4208
4209/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004210 * xmlRelaxNGCheckGroupAttrs:
4211 * @ctxt: a Relax-NG parser context
4212 * @def: the group definition
4213 *
4214 * Detects violations of rule 7.3
4215 */
4216static void
4217xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004218 xmlRelaxNGDefinePtr def)
4219{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004220 xmlRelaxNGDefinePtr **list;
4221 xmlRelaxNGDefinePtr cur;
4222 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004223
4224 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004225 ((def->type != XML_RELAXNG_GROUP) &&
4226 (def->type != XML_RELAXNG_ELEMENT)))
4227 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004228
Daniel Veillarde063f482003-03-21 16:53:17 +00004229 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004230 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004231
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004232 /*
4233 * Don't run that check in case of error. Infinite recursion
4234 * becomes possible.
4235 */
4236 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004237 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004238
Daniel Veillardfd573f12003-03-16 17:52:32 +00004239 cur = def->attrs;
4240 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004241 nbchild++;
4242 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004243 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004244 cur = def->content;
4245 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004246 nbchild++;
4247 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004248 }
4249
4250 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004251 sizeof(xmlRelaxNGDefinePtr
4252 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004253 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004254 xmlRngPErrMemory(ctxt, "building group\n");
4255 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004256 }
4257 i = 0;
4258 cur = def->attrs;
4259 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004260 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4261 i++;
4262 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004263 }
4264 cur = def->content;
4265 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004266 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4267 i++;
4268 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004269 }
4270
Daniel Veillard4c004142003-10-07 11:33:24 +00004271 for (i = 0; i < nbchild; i++) {
4272 if (list[i] == NULL)
4273 continue;
4274 for (j = 0; j < i; j++) {
4275 if (list[j] == NULL)
4276 continue;
4277 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4278 if (ret == 0) {
4279 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4280 "Attributes conflicts in group\n", NULL, NULL);
4281 }
4282 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004283 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004284 for (i = 0; i < nbchild; i++) {
4285 if (list[i] != NULL)
4286 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004287 }
4288
4289 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004290 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004291}
4292
4293/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004294 * xmlRelaxNGComputeInterleaves:
4295 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004296 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004297 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004298 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004299 * A lot of work for preprocessing interleave definitions
4300 * is potentially needed to get a decent execution speed at runtime
4301 * - trying to get a total order on the element nodes generated
4302 * by the interleaves, order the list of interleave definitions
4303 * following that order.
4304 * - if <text/> is used to handle mixed content, it is better to
4305 * flag this in the define and simplify the runtime checking
4306 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004307 */
4308static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004309xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004310 xmlRelaxNGParserCtxtPtr ctxt,
4311 xmlChar * name ATTRIBUTE_UNUSED)
4312{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004313 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004314
Daniel Veillardfd573f12003-03-16 17:52:32 +00004315 xmlRelaxNGPartitionPtr partitions = NULL;
4316 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4317 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004318 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004319 int nbgroups = 0;
4320 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004321 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004322 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004323
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004324 /*
4325 * Don't run that check in case of error. Infinite recursion
4326 * becomes possible.
4327 */
4328 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004329 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004330
Daniel Veillardfd573f12003-03-16 17:52:32 +00004331#ifdef DEBUG_INTERLEAVE
4332 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004333 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004334#endif
4335 cur = def->content;
4336 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004337 nbchild++;
4338 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004339 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004340
Daniel Veillardfd573f12003-03-16 17:52:32 +00004341#ifdef DEBUG_INTERLEAVE
4342 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4343#endif
4344 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004345 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004346 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004347 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004348 cur = def->content;
4349 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004350 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4351 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4352 if (groups[nbgroups] == NULL)
4353 goto error;
4354 if (cur->type == XML_RELAXNG_TEXT)
4355 is_mixed++;
4356 groups[nbgroups]->rule = cur;
4357 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4358 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4359 nbgroups++;
4360 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004361 }
4362#ifdef DEBUG_INTERLEAVE
4363 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4364#endif
4365
4366 /*
4367 * Let's check that all rules makes a partitions according to 7.4
4368 */
4369 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004370 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004371 if (partitions == NULL)
4372 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004373 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004374 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004375 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004376 for (i = 0; i < nbgroups; i++) {
4377 group = groups[i];
4378 for (j = i + 1; j < nbgroups; j++) {
4379 if (groups[j] == NULL)
4380 continue;
4381
4382 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4383 groups[j]->defs);
4384 if (ret == 0) {
4385 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4386 "Element or text conflicts in interleave\n",
4387 NULL, NULL);
4388 }
4389 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4390 groups[j]->attrs);
4391 if (ret == 0) {
4392 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4393 "Attributes conflicts in interleave\n", NULL,
4394 NULL);
4395 }
4396 }
4397 tmp = group->defs;
4398 if ((tmp != NULL) && (*tmp != NULL)) {
4399 while (*tmp != NULL) {
4400 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4401 res = xmlHashAddEntry2(partitions->triage,
4402 BAD_CAST "#text", NULL,
4403 (void *) (long) (i + 1));
4404 if (res != 0)
4405 is_determinist = -1;
4406 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4407 ((*tmp)->name != NULL)) {
4408 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4409 res = xmlHashAddEntry2(partitions->triage,
4410 (*tmp)->name, NULL,
4411 (void *) (long) (i + 1));
4412 else
4413 res = xmlHashAddEntry2(partitions->triage,
4414 (*tmp)->name, (*tmp)->ns,
4415 (void *) (long) (i + 1));
4416 if (res != 0)
4417 is_determinist = -1;
4418 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4419 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4420 res = xmlHashAddEntry2(partitions->triage,
4421 BAD_CAST "#any", NULL,
4422 (void *) (long) (i + 1));
4423 else
4424 res = xmlHashAddEntry2(partitions->triage,
4425 BAD_CAST "#any", (*tmp)->ns,
4426 (void *) (long) (i + 1));
4427 if ((*tmp)->nameClass != NULL)
4428 is_determinist = 2;
4429 if (res != 0)
4430 is_determinist = -1;
4431 } else {
4432 is_determinist = -1;
4433 }
4434 tmp++;
4435 }
4436 } else {
4437 is_determinist = 0;
4438 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004439 }
4440 partitions->groups = groups;
4441
4442 /*
4443 * and save the partition list back in the def
4444 */
4445 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004446 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004447 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004448 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004449 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004450 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004451 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004452 return;
4453
Daniel Veillard4c004142003-10-07 11:33:24 +00004454 error:
4455 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004456 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004457 for (i = 0; i < nbgroups; i++)
4458 if (groups[i] != NULL) {
4459 if (groups[i]->defs != NULL)
4460 xmlFree(groups[i]->defs);
4461 xmlFree(groups[i]);
4462 }
4463 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004464 }
4465 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004466}
4467
4468/**
4469 * xmlRelaxNGParseInterleave:
4470 * @ctxt: a Relax-NG parser context
4471 * @node: the data node.
4472 *
4473 * parse the content of a RelaxNG interleave node.
4474 *
4475 * Returns the definition pointer or NULL in case of error
4476 */
4477static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004478xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4479{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004480 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004481 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004482 xmlNodePtr child;
4483
Daniel Veillardfd573f12003-03-16 17:52:32 +00004484 def = xmlRelaxNGNewDefine(ctxt, node);
4485 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004486 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004487 }
4488 def->type = XML_RELAXNG_INTERLEAVE;
4489
4490 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004491 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004492 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004493 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004494 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004495 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004496
Daniel Veillard4c004142003-10-07 11:33:24 +00004497 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4498 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4499 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4500 "Failed to add %s to hash table\n",
4501 (const xmlChar *) name, NULL);
4502 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004503 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004504 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004505 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004506 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4507 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004508 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004509 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004510 if (IS_RELAXNG(child, "element")) {
4511 cur = xmlRelaxNGParseElement(ctxt, child);
4512 } else {
4513 cur = xmlRelaxNGParsePattern(ctxt, child);
4514 }
4515 if (cur != NULL) {
4516 cur->parent = def;
4517 if (last == NULL) {
4518 def->content = last = cur;
4519 } else {
4520 last->next = cur;
4521 last = cur;
4522 }
4523 }
4524 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004525 }
4526
Daniel Veillard4c004142003-10-07 11:33:24 +00004527 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004528}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004529
4530/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004531 * xmlRelaxNGParseInclude:
4532 * @ctxt: a Relax-NG parser context
4533 * @node: the include node
4534 *
4535 * Integrate the content of an include node in the current grammar
4536 *
4537 * Returns 0 in case of success or -1 in case of error
4538 */
4539static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004540xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4541{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004542 xmlRelaxNGIncludePtr incl;
4543 xmlNodePtr root;
4544 int ret = 0, tmp;
4545
Daniel Veillard807daf82004-02-22 22:13:27 +00004546 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004547 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004548 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4549 "Include node has no data\n", NULL, NULL);
4550 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004551 }
4552 root = xmlDocGetRootElement(incl->doc);
4553 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004554 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4555 NULL, NULL);
4556 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004557 }
4558 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004559 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4560 "Include document root is not a grammar\n", NULL, NULL);
4561 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004562 }
4563
4564 /*
4565 * Merge the definition from both the include and the internal list
4566 */
4567 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004568 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4569 if (tmp != 0)
4570 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004571 }
4572 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004573 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4574 if (tmp != 0)
4575 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004576 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004577 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004578}
4579
4580/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004581 * xmlRelaxNGParseDefine:
4582 * @ctxt: a Relax-NG parser context
4583 * @node: the define node
4584 *
4585 * parse the content of a RelaxNG define element node.
4586 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004587 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004588 */
4589static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004590xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4591{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004592 xmlChar *name;
4593 int ret = 0, tmp;
4594 xmlRelaxNGDefinePtr def;
4595 const xmlChar *olddefine;
4596
4597 name = xmlGetProp(node, BAD_CAST "name");
4598 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004599 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4600 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004601 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004602 xmlRelaxNGNormExtSpace(name);
4603 if (xmlValidateNCName(name, 0)) {
4604 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4605 "define name '%s' is not an NCName\n", name, NULL);
4606 }
4607 def = xmlRelaxNGNewDefine(ctxt, node);
4608 if (def == NULL) {
4609 xmlFree(name);
4610 return (-1);
4611 }
4612 def->type = XML_RELAXNG_DEF;
4613 def->name = name;
4614 if (node->children == NULL) {
4615 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4616 "define has no children\n", NULL, NULL);
4617 } else {
4618 olddefine = ctxt->define;
4619 ctxt->define = name;
4620 def->content =
4621 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4622 ctxt->define = olddefine;
4623 }
4624 if (ctxt->grammar->defs == NULL)
4625 ctxt->grammar->defs = xmlHashCreate(10);
4626 if (ctxt->grammar->defs == NULL) {
4627 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4628 "Could not create definition hash\n", NULL, NULL);
4629 ret = -1;
4630 } else {
4631 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4632 if (tmp < 0) {
4633 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004634
Daniel Veillard4c004142003-10-07 11:33:24 +00004635 prev = xmlHashLookup(ctxt->grammar->defs, name);
4636 if (prev == NULL) {
4637 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4638 "Internal error on define aggregation of %s\n",
4639 name, NULL);
4640 ret = -1;
4641 } else {
4642 while (prev->nextHash != NULL)
4643 prev = prev->nextHash;
4644 prev->nextHash = def;
4645 }
4646 }
4647 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004648 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004649 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004650}
4651
4652/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004653 * xmlRelaxNGParseImportRef:
4654 * @payload: the parser context
4655 * @data: the current grammar
4656 * @name: the reference name
4657 *
4658 * Import import one references into the current grammar
4659 */
4660static void
4661xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4662 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4663 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4664 int tmp;
4665
Daniel Veillardaa422d92009-09-24 11:31:48 +02004666 def->dflags |= IS_EXTERNAL_REF;
4667
Daniel Veillard81c51e12009-08-14 18:52:10 +02004668 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4669 if (tmp < 0) {
4670 xmlRelaxNGDefinePtr prev;
4671
4672 prev = (xmlRelaxNGDefinePtr)
4673 xmlHashLookup(ctxt->grammar->refs, def->name);
4674 if (prev == NULL) {
4675 if (def->name != NULL) {
4676 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4677 "Error refs definitions '%s'\n",
4678 def->name, NULL);
4679 } else {
4680 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4681 "Error refs definitions\n",
4682 NULL, NULL);
4683 }
4684 } else {
4685 def->nextHash = prev->nextHash;
4686 prev->nextHash = def;
4687 }
4688 }
4689}
4690
4691/**
4692 * xmlRelaxNGParseImportRefs:
4693 * @ctxt: the parser context
4694 * @grammar: the sub grammar
4695 *
4696 * Import references from the subgrammar into the current grammar
4697 *
4698 * Returns 0 in case of success, -1 in case of failure
4699 */
4700static int
4701xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4702 xmlRelaxNGGrammarPtr grammar) {
4703 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4704 return(-1);
4705 if (grammar->refs == NULL)
4706 return(0);
4707 if (ctxt->grammar->refs == NULL)
4708 ctxt->grammar->refs = xmlHashCreate(10);
4709 if (ctxt->grammar->refs == NULL) {
4710 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4711 "Could not create references hash\n", NULL, NULL);
4712 return(-1);
4713 }
4714 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004715 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004716}
4717
4718/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004719 * xmlRelaxNGProcessExternalRef:
4720 * @ctxt: the parser context
4721 * @node: the externlRef node
4722 *
4723 * Process and compile an externlRef node
4724 *
4725 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4726 */
4727static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004728xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4729{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004730 xmlRelaxNGDocumentPtr docu;
4731 xmlNodePtr root, tmp;
4732 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004733 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004734 xmlRelaxNGDefinePtr def;
4735
Daniel Veillard807daf82004-02-22 22:13:27 +00004736 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004737 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004738 def = xmlRelaxNGNewDefine(ctxt, node);
4739 if (def == NULL)
4740 return (NULL);
4741 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004742
Daniel Veillard4c004142003-10-07 11:33:24 +00004743 if (docu->content == NULL) {
4744 /*
4745 * Then do the parsing for good
4746 */
4747 root = xmlDocGetRootElement(docu->doc);
4748 if (root == NULL) {
4749 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4750 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4751 NULL);
4752 return (NULL);
4753 }
4754 /*
4755 * ns transmission rules
4756 */
4757 ns = xmlGetProp(root, BAD_CAST "ns");
4758 if (ns == NULL) {
4759 tmp = node;
4760 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4761 ns = xmlGetProp(tmp, BAD_CAST "ns");
4762 if (ns != NULL) {
4763 break;
4764 }
4765 tmp = tmp->parent;
4766 }
4767 if (ns != NULL) {
4768 xmlSetProp(root, BAD_CAST "ns", ns);
4769 newNs = 1;
4770 xmlFree(ns);
4771 }
4772 } else {
4773 xmlFree(ns);
4774 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004775
Daniel Veillard4c004142003-10-07 11:33:24 +00004776 /*
4777 * Parsing to get a precompiled schemas.
4778 */
4779 oldflags = ctxt->flags;
4780 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4781 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4782 ctxt->flags = oldflags;
4783 if ((docu->schema != NULL) &&
4784 (docu->schema->topgrammar != NULL)) {
4785 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004786 if (docu->schema->topgrammar->refs)
4787 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004788 }
4789
4790 /*
4791 * the externalRef may be reused in a different ns context
4792 */
4793 if (newNs == 1) {
4794 xmlUnsetProp(root, BAD_CAST "ns");
4795 }
4796 }
4797 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004798 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004799 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004800 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004801 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004802}
4803
4804/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004805 * xmlRelaxNGParsePattern:
4806 * @ctxt: a Relax-NG parser context
4807 * @node: the pattern node.
4808 *
4809 * parse the content of a RelaxNG pattern node.
4810 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004811 * Returns the definition pointer or NULL in case of error or if no
4812 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004813 */
4814static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004815xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4816{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004817 xmlRelaxNGDefinePtr def = NULL;
4818
Daniel Veillardd2298792003-02-14 16:54:11 +00004819 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004820 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004821 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004822 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004823 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004824 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004825 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004826 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004827 def = xmlRelaxNGNewDefine(ctxt, node);
4828 if (def == NULL)
4829 return (NULL);
4830 def->type = XML_RELAXNG_EMPTY;
4831 if (node->children != NULL) {
4832 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4833 "empty: had a child node\n", NULL, NULL);
4834 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004835 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004836 def = xmlRelaxNGNewDefine(ctxt, node);
4837 if (def == NULL)
4838 return (NULL);
4839 def->type = XML_RELAXNG_TEXT;
4840 if (node->children != NULL) {
4841 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4842 "text: had a child node\n", NULL, NULL);
4843 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004844 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004845 def = xmlRelaxNGNewDefine(ctxt, node);
4846 if (def == NULL)
4847 return (NULL);
4848 def->type = XML_RELAXNG_ZEROORMORE;
4849 if (node->children == NULL) {
4850 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4851 "Element %s is empty\n", node->name, NULL);
4852 } else {
4853 def->content =
4854 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4855 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004856 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004857 def = xmlRelaxNGNewDefine(ctxt, node);
4858 if (def == NULL)
4859 return (NULL);
4860 def->type = XML_RELAXNG_ONEORMORE;
4861 if (node->children == NULL) {
4862 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4863 "Element %s is empty\n", node->name, NULL);
4864 } else {
4865 def->content =
4866 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4867 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004868 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004869 def = xmlRelaxNGNewDefine(ctxt, node);
4870 if (def == NULL)
4871 return (NULL);
4872 def->type = XML_RELAXNG_OPTIONAL;
4873 if (node->children == NULL) {
4874 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4875 "Element %s is empty\n", node->name, NULL);
4876 } else {
4877 def->content =
4878 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4879 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004880 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004881 def = xmlRelaxNGNewDefine(ctxt, node);
4882 if (def == NULL)
4883 return (NULL);
4884 def->type = XML_RELAXNG_CHOICE;
4885 if (node->children == NULL) {
4886 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4887 "Element %s is empty\n", node->name, NULL);
4888 } else {
4889 def->content =
4890 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4891 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004892 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004893 def = xmlRelaxNGNewDefine(ctxt, node);
4894 if (def == NULL)
4895 return (NULL);
4896 def->type = XML_RELAXNG_GROUP;
4897 if (node->children == NULL) {
4898 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4899 "Element %s is empty\n", node->name, NULL);
4900 } else {
4901 def->content =
4902 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4903 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004904 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004905 def = xmlRelaxNGNewDefine(ctxt, node);
4906 if (def == NULL)
4907 return (NULL);
4908 def->type = XML_RELAXNG_REF;
4909 def->name = xmlGetProp(node, BAD_CAST "name");
4910 if (def->name == NULL) {
4911 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4912 NULL, NULL);
4913 } else {
4914 xmlRelaxNGNormExtSpace(def->name);
4915 if (xmlValidateNCName(def->name, 0)) {
4916 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4917 "ref name '%s' is not an NCName\n", def->name,
4918 NULL);
4919 }
4920 }
4921 if (node->children != NULL) {
4922 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4923 NULL, NULL);
4924 }
4925 if (ctxt->grammar->refs == NULL)
4926 ctxt->grammar->refs = xmlHashCreate(10);
4927 if (ctxt->grammar->refs == NULL) {
4928 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4929 "Could not create references hash\n", NULL, NULL);
4930 def = NULL;
4931 } else {
4932 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004933
Daniel Veillard4c004142003-10-07 11:33:24 +00004934 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4935 if (tmp < 0) {
4936 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004937
Daniel Veillard4c004142003-10-07 11:33:24 +00004938 prev = (xmlRelaxNGDefinePtr)
4939 xmlHashLookup(ctxt->grammar->refs, def->name);
4940 if (prev == NULL) {
4941 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004942 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4943 "Error refs definitions '%s'\n",
4944 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004945 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004946 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4947 "Error refs definitions\n",
4948 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004949 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004950 def = NULL;
4951 } else {
4952 def->nextHash = prev->nextHash;
4953 prev->nextHash = def;
4954 }
4955 }
4956 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004957 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004958 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004959 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004960 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004961 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004962 def = xmlRelaxNGNewDefine(ctxt, node);
4963 if (def == NULL)
4964 return (NULL);
4965 def->type = XML_RELAXNG_LIST;
4966 if (node->children == NULL) {
4967 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4968 "Element %s is empty\n", node->name, NULL);
4969 } else {
4970 def->content =
4971 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4972 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004973 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004974 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004975 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004976 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004977 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004978 def = xmlRelaxNGNewDefine(ctxt, node);
4979 if (def == NULL)
4980 return (NULL);
4981 def->type = XML_RELAXNG_NOT_ALLOWED;
4982 if (node->children != NULL) {
4983 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4984 "xmlRelaxNGParse: notAllowed element is not empty\n",
4985 NULL, NULL);
4986 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004987 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004988 xmlRelaxNGGrammarPtr grammar, old;
4989 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004990
Daniel Veillardc482e262003-02-26 14:48:48 +00004991#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004992 xmlGenericError(xmlGenericErrorContext,
4993 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004994#endif
4995
Daniel Veillard4c004142003-10-07 11:33:24 +00004996 oldparent = ctxt->parentgrammar;
4997 old = ctxt->grammar;
4998 ctxt->parentgrammar = old;
4999 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5000 if (old != NULL) {
5001 ctxt->grammar = old;
5002 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00005003#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00005004 if (grammar != NULL) {
5005 grammar->next = old->next;
5006 old->next = grammar;
5007 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005008#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00005009 }
5010 if (grammar != NULL)
5011 def = grammar->start;
5012 else
5013 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00005014 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005015 if (ctxt->parentgrammar == NULL) {
5016 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5017 "Use of parentRef without a parent grammar\n", NULL,
5018 NULL);
5019 return (NULL);
5020 }
5021 def = xmlRelaxNGNewDefine(ctxt, node);
5022 if (def == NULL)
5023 return (NULL);
5024 def->type = XML_RELAXNG_PARENTREF;
5025 def->name = xmlGetProp(node, BAD_CAST "name");
5026 if (def->name == NULL) {
5027 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5028 "parentRef has no name\n", NULL, NULL);
5029 } else {
5030 xmlRelaxNGNormExtSpace(def->name);
5031 if (xmlValidateNCName(def->name, 0)) {
5032 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5033 "parentRef name '%s' is not an NCName\n",
5034 def->name, NULL);
5035 }
5036 }
5037 if (node->children != NULL) {
5038 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5039 "parentRef is not empty\n", NULL, NULL);
5040 }
5041 if (ctxt->parentgrammar->refs == NULL)
5042 ctxt->parentgrammar->refs = xmlHashCreate(10);
5043 if (ctxt->parentgrammar->refs == NULL) {
5044 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5045 "Could not create references hash\n", NULL, NULL);
5046 def = NULL;
5047 } else if (def->name != NULL) {
5048 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005049
Daniel Veillard4c004142003-10-07 11:33:24 +00005050 tmp =
5051 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5052 if (tmp < 0) {
5053 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005054
Daniel Veillard4c004142003-10-07 11:33:24 +00005055 prev = (xmlRelaxNGDefinePtr)
5056 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5057 if (prev == NULL) {
5058 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5059 "Internal error parentRef definitions '%s'\n",
5060 def->name, NULL);
5061 def = NULL;
5062 } else {
5063 def->nextHash = prev->nextHash;
5064 prev->nextHash = def;
5065 }
5066 }
5067 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005068 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005069 if (node->children == NULL) {
5070 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5071 NULL, NULL);
5072 def = NULL;
5073 } else {
5074 def = xmlRelaxNGParseInterleave(ctxt, node);
5075 if (def != NULL) {
5076 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005077
Daniel Veillard4c004142003-10-07 11:33:24 +00005078 if ((def->content != NULL) && (def->content->next != NULL)) {
5079 tmp = xmlRelaxNGNewDefine(ctxt, node);
5080 if (tmp != NULL) {
5081 tmp->type = XML_RELAXNG_GROUP;
5082 tmp->content = def->content;
5083 def->content = tmp;
5084 }
5085 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005086
Daniel Veillard4c004142003-10-07 11:33:24 +00005087 tmp = xmlRelaxNGNewDefine(ctxt, node);
5088 if (tmp == NULL)
5089 return (def);
5090 tmp->type = XML_RELAXNG_TEXT;
5091 tmp->next = def->content;
5092 def->content = tmp;
5093 }
5094 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005095 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005096 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5097 "Unexpected node %s is not a pattern\n", node->name,
5098 NULL);
5099 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005100 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005101 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005102}
5103
5104/**
5105 * xmlRelaxNGParseAttribute:
5106 * @ctxt: a Relax-NG parser context
5107 * @node: the element node
5108 *
5109 * parse the content of a RelaxNG attribute node.
5110 *
5111 * Returns the definition pointer or NULL in case of error.
5112 */
5113static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005114xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5115{
Daniel Veillardd2298792003-02-14 16:54:11 +00005116 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005117 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005118 int old_flags;
5119
Daniel Veillardfd573f12003-03-16 17:52:32 +00005120 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005121 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005122 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005123 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005124 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005125 child = node->children;
5126 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005127 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5128 "xmlRelaxNGParseattribute: attribute has no children\n",
5129 NULL, NULL);
5130 return (ret);
5131 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005132 old_flags = ctxt->flags;
5133 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005134 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5135 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005136 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005137
Daniel Veillardd2298792003-02-14 16:54:11 +00005138 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005139 cur = xmlRelaxNGParsePattern(ctxt, child);
5140 if (cur != NULL) {
5141 switch (cur->type) {
5142 case XML_RELAXNG_EMPTY:
5143 case XML_RELAXNG_NOT_ALLOWED:
5144 case XML_RELAXNG_TEXT:
5145 case XML_RELAXNG_ELEMENT:
5146 case XML_RELAXNG_DATATYPE:
5147 case XML_RELAXNG_VALUE:
5148 case XML_RELAXNG_LIST:
5149 case XML_RELAXNG_REF:
5150 case XML_RELAXNG_PARENTREF:
5151 case XML_RELAXNG_EXTERNALREF:
5152 case XML_RELAXNG_DEF:
5153 case XML_RELAXNG_ONEORMORE:
5154 case XML_RELAXNG_ZEROORMORE:
5155 case XML_RELAXNG_OPTIONAL:
5156 case XML_RELAXNG_CHOICE:
5157 case XML_RELAXNG_GROUP:
5158 case XML_RELAXNG_INTERLEAVE:
5159 case XML_RELAXNG_ATTRIBUTE:
5160 ret->content = cur;
5161 cur->parent = ret;
5162 break;
5163 case XML_RELAXNG_START:
5164 case XML_RELAXNG_PARAM:
5165 case XML_RELAXNG_EXCEPT:
5166 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5167 "attribute has invalid content\n", NULL,
5168 NULL);
5169 break;
5170 case XML_RELAXNG_NOOP:
5171 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5172 "RNG Internal error, noop found in attribute\n",
5173 NULL, NULL);
5174 break;
5175 }
5176 }
5177 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005178 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005179 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005180 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5181 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005182 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005183 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005184 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005185}
5186
5187/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005188 * xmlRelaxNGParseExceptNameClass:
5189 * @ctxt: a Relax-NG parser context
5190 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005191 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005192 *
5193 * parse the content of a RelaxNG nameClass node.
5194 *
5195 * Returns the definition pointer or NULL in case of error.
5196 */
5197static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005198xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005199 xmlNodePtr node, int attr)
5200{
Daniel Veillard144fae12003-02-03 13:17:57 +00005201 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5202 xmlNodePtr child;
5203
Daniel Veillardd2298792003-02-14 16:54:11 +00005204 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005205 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5206 "Expecting an except node\n", NULL, NULL);
5207 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005208 }
5209 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005210 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5211 "exceptNameClass allows only a single except node\n",
5212 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005213 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005214 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005215 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5216 NULL, NULL);
5217 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005218 }
5219
Daniel Veillardfd573f12003-03-16 17:52:32 +00005220 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005221 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005222 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005223 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005224 child = node->children;
5225 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005226 cur = xmlRelaxNGNewDefine(ctxt, child);
5227 if (cur == NULL)
5228 break;
5229 if (attr)
5230 cur->type = XML_RELAXNG_ATTRIBUTE;
5231 else
5232 cur->type = XML_RELAXNG_ELEMENT;
5233
Daniel Veillard419a7682003-02-03 23:22:49 +00005234 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005235 if (last == NULL) {
5236 ret->content = cur;
5237 } else {
5238 last->next = cur;
5239 }
5240 last = cur;
5241 }
5242 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005243 }
5244
Daniel Veillard4c004142003-10-07 11:33:24 +00005245 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005246}
5247
5248/**
5249 * xmlRelaxNGParseNameClass:
5250 * @ctxt: a Relax-NG parser context
5251 * @node: the nameClass node
5252 * @def: the current definition
5253 *
5254 * parse the content of a RelaxNG nameClass node.
5255 *
5256 * Returns the definition pointer or NULL in case of error.
5257 */
5258static xmlRelaxNGDefinePtr
5259xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005260 xmlRelaxNGDefinePtr def)
5261{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005262 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005263 xmlChar *val;
5264
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005265 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005266 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005267 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005268 if ((def->type != XML_RELAXNG_ELEMENT) &&
5269 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5270 ret = xmlRelaxNGNewDefine(ctxt, node);
5271 if (ret == NULL)
5272 return (NULL);
5273 ret->parent = def;
5274 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5275 ret->type = XML_RELAXNG_ATTRIBUTE;
5276 else
5277 ret->type = XML_RELAXNG_ELEMENT;
5278 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005279 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005280 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005281 val = xmlNodeGetContent(node);
5282 xmlRelaxNGNormExtSpace(val);
5283 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005284 if (node->parent != NULL)
5285 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5286 "Element %s name '%s' is not an NCName\n",
5287 node->parent->name, val);
5288 else
5289 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5290 "name '%s' is not an NCName\n",
5291 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005292 }
5293 ret->name = val;
5294 val = xmlGetProp(node, BAD_CAST "ns");
5295 ret->ns = val;
5296 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5297 (val != NULL) &&
5298 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005299 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005300 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005301 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005302 }
5303 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5304 (val != NULL) &&
5305 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005306 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5307 "Attribute with QName 'xmlns' is not allowed\n",
5308 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005309 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005310 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005311 ret->name = NULL;
5312 ret->ns = NULL;
5313 if (node->children != NULL) {
5314 ret->nameClass =
5315 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5316 (def->type ==
5317 XML_RELAXNG_ATTRIBUTE));
5318 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005319 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005320 ret->name = NULL;
5321 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5322 if (ret->ns == NULL) {
5323 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5324 "nsName has no ns attribute\n", NULL, NULL);
5325 }
5326 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5327 (ret->ns != NULL) &&
5328 (xmlStrEqual
5329 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5330 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5331 "Attribute with namespace '%s' is not allowed\n",
5332 ret->ns, NULL);
5333 }
5334 if (node->children != NULL) {
5335 ret->nameClass =
5336 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5337 (def->type ==
5338 XML_RELAXNG_ATTRIBUTE));
5339 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005340 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005341 xmlNodePtr child;
5342 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005343
Daniel Veillard4c004142003-10-07 11:33:24 +00005344 ret = xmlRelaxNGNewDefine(ctxt, node);
5345 if (ret == NULL)
5346 return (NULL);
5347 ret->parent = def;
5348 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005349
Daniel Veillard4c004142003-10-07 11:33:24 +00005350 if (node->children == NULL) {
5351 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5352 "Element choice is empty\n", NULL, NULL);
5353 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005354
Daniel Veillard4c004142003-10-07 11:33:24 +00005355 child = node->children;
5356 while (child != NULL) {
5357 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5358 if (tmp != NULL) {
5359 if (last == NULL) {
5360 last = ret->nameClass = tmp;
5361 } else {
5362 last->next = tmp;
5363 last = tmp;
5364 }
5365 }
5366 child = child->next;
5367 }
5368 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005369 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005370 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5371 "expecting name, anyName, nsName or choice : got %s\n",
Rob Richards848e5cf2009-09-09 12:13:58 -04005372 (node == NULL ? "nothing" : node->name), NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005373 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005374 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005375 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005376 if (def->nameClass == NULL) {
5377 def->nameClass = ret;
5378 } else {
5379 tmp = def->nameClass;
5380 while (tmp->next != NULL) {
5381 tmp = tmp->next;
5382 }
5383 tmp->next = ret;
5384 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005385 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005386 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005387}
5388
5389/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005390 * xmlRelaxNGParseElement:
5391 * @ctxt: a Relax-NG parser context
5392 * @node: the element node
5393 *
5394 * parse the content of a RelaxNG element node.
5395 *
5396 * Returns the definition pointer or NULL in case of error.
5397 */
5398static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005399xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5400{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005401 xmlRelaxNGDefinePtr ret, cur, last;
5402 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005403 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005404
Daniel Veillardfd573f12003-03-16 17:52:32 +00005405 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005406 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005407 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005408 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005409 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005410 child = node->children;
5411 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005412 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5413 "xmlRelaxNGParseElement: element has no children\n",
5414 NULL, NULL);
5415 return (ret);
5416 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005417 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5418 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005419 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005420
Daniel Veillard6eadf632003-01-23 18:29:16 +00005421 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005422 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5423 "xmlRelaxNGParseElement: element has no content\n",
5424 NULL, NULL);
5425 return (ret);
5426 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005427 olddefine = ctxt->define;
5428 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005429 last = NULL;
5430 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005431 cur = xmlRelaxNGParsePattern(ctxt, child);
5432 if (cur != NULL) {
5433 cur->parent = ret;
5434 switch (cur->type) {
5435 case XML_RELAXNG_EMPTY:
5436 case XML_RELAXNG_NOT_ALLOWED:
5437 case XML_RELAXNG_TEXT:
5438 case XML_RELAXNG_ELEMENT:
5439 case XML_RELAXNG_DATATYPE:
5440 case XML_RELAXNG_VALUE:
5441 case XML_RELAXNG_LIST:
5442 case XML_RELAXNG_REF:
5443 case XML_RELAXNG_PARENTREF:
5444 case XML_RELAXNG_EXTERNALREF:
5445 case XML_RELAXNG_DEF:
5446 case XML_RELAXNG_ZEROORMORE:
5447 case XML_RELAXNG_ONEORMORE:
5448 case XML_RELAXNG_OPTIONAL:
5449 case XML_RELAXNG_CHOICE:
5450 case XML_RELAXNG_GROUP:
5451 case XML_RELAXNG_INTERLEAVE:
5452 if (last == NULL) {
5453 ret->content = last = cur;
5454 } else {
5455 if ((last->type == XML_RELAXNG_ELEMENT) &&
5456 (ret->content == last)) {
5457 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5458 if (ret->content != NULL) {
5459 ret->content->type = XML_RELAXNG_GROUP;
5460 ret->content->content = last;
5461 } else {
5462 ret->content = last;
5463 }
5464 }
5465 last->next = cur;
5466 last = cur;
5467 }
5468 break;
5469 case XML_RELAXNG_ATTRIBUTE:
5470 cur->next = ret->attrs;
5471 ret->attrs = cur;
5472 break;
5473 case XML_RELAXNG_START:
5474 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5475 "RNG Internal error, start found in element\n",
5476 NULL, NULL);
5477 break;
5478 case XML_RELAXNG_PARAM:
5479 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5480 "RNG Internal error, param found in element\n",
5481 NULL, NULL);
5482 break;
5483 case XML_RELAXNG_EXCEPT:
5484 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5485 "RNG Internal error, except found in element\n",
5486 NULL, NULL);
5487 break;
5488 case XML_RELAXNG_NOOP:
5489 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5490 "RNG Internal error, noop found in element\n",
5491 NULL, NULL);
5492 break;
5493 }
5494 }
5495 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005496 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005497 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005498 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005499}
5500
5501/**
5502 * xmlRelaxNGParsePatterns:
5503 * @ctxt: a Relax-NG parser context
5504 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005505 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005506 *
5507 * parse the content of a RelaxNG start node.
5508 *
5509 * Returns the definition pointer or NULL in case of error.
5510 */
5511static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005512xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005513 int group)
5514{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005515 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005516
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005517 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005518 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005519 if (IS_RELAXNG(nodes, "element")) {
5520 cur = xmlRelaxNGParseElement(ctxt, nodes);
5521 if (def == NULL) {
5522 def = last = cur;
5523 } else {
5524 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5525 (def == last)) {
5526 def = xmlRelaxNGNewDefine(ctxt, nodes);
5527 def->type = XML_RELAXNG_GROUP;
5528 def->content = last;
5529 }
5530 last->next = cur;
5531 last = cur;
5532 }
5533 cur->parent = parent;
5534 } else {
5535 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5536 if (cur != NULL) {
5537 if (def == NULL) {
5538 def = last = cur;
5539 } else {
5540 last->next = cur;
5541 last = cur;
5542 }
5543 }
5544 }
5545 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005546 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005547 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005548}
5549
5550/**
5551 * xmlRelaxNGParseStart:
5552 * @ctxt: a Relax-NG parser context
5553 * @nodes: start children nodes
5554 *
5555 * parse the content of a RelaxNG start node.
5556 *
5557 * Returns 0 in case of success, -1 in case of error
5558 */
5559static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005560xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5561{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005562 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005563 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005564
Daniel Veillardd2298792003-02-14 16:54:11 +00005565 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005566 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5567 NULL, NULL);
5568 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005569 }
5570 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005571 def = xmlRelaxNGNewDefine(ctxt, nodes);
5572 if (def == NULL)
5573 return (-1);
5574 def->type = XML_RELAXNG_EMPTY;
5575 if (nodes->children != NULL) {
5576 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5577 "element empty is not empty\n", NULL, NULL);
5578 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005579 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005580 def = xmlRelaxNGNewDefine(ctxt, nodes);
5581 if (def == NULL)
5582 return (-1);
5583 def->type = XML_RELAXNG_NOT_ALLOWED;
5584 if (nodes->children != NULL) {
5585 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5586 "element notAllowed is not empty\n", NULL, NULL);
5587 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005588 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005589 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005590 }
5591 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005592 last = ctxt->grammar->start;
5593 while (last->next != NULL)
5594 last = last->next;
5595 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005596 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005597 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005598 }
5599 nodes = nodes->next;
5600 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005601 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5602 "start more than one children\n", NULL, NULL);
5603 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005604 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005605 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005606}
5607
5608/**
5609 * xmlRelaxNGParseGrammarContent:
5610 * @ctxt: a Relax-NG parser context
5611 * @nodes: grammar children nodes
5612 *
5613 * parse the content of a RelaxNG grammar node.
5614 *
5615 * Returns 0 in case of success, -1 in case of error
5616 */
5617static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005618xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5619 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005620{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005621 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005622
5623 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005624 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5625 "grammar has no children\n", NULL, NULL);
5626 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005627 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005628 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005629 if (IS_RELAXNG(nodes, "start")) {
5630 if (nodes->children == NULL) {
5631 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5632 "start has no children\n", NULL, NULL);
5633 } else {
5634 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5635 if (tmp != 0)
5636 ret = -1;
5637 }
5638 } else if (IS_RELAXNG(nodes, "define")) {
5639 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5640 if (tmp != 0)
5641 ret = -1;
5642 } else if (IS_RELAXNG(nodes, "include")) {
5643 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5644 if (tmp != 0)
5645 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005646 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005647 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5648 "grammar has unexpected child %s\n", nodes->name,
5649 NULL);
5650 ret = -1;
5651 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005652 nodes = nodes->next;
5653 }
5654 return (ret);
5655}
5656
5657/**
5658 * xmlRelaxNGCheckReference:
5659 * @ref: the ref
5660 * @ctxt: a Relax-NG parser context
5661 * @name: the name associated to the defines
5662 *
5663 * Applies the 4.17. combine attribute rule for all the define
5664 * element of a given grammar using the same name.
5665 */
5666static void
5667xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005668 xmlRelaxNGParserCtxtPtr ctxt,
5669 const xmlChar * name)
5670{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005671 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005672 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005673
Daniel Veillardaa422d92009-09-24 11:31:48 +02005674 /*
5675 * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5676 */
5677 if (ref->dflags & IS_EXTERNAL_REF)
5678 return;
5679
Daniel Veillard6eadf632003-01-23 18:29:16 +00005680 grammar = ctxt->grammar;
5681 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005682 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5683 "Internal error: no grammar in CheckReference %s\n",
5684 name, NULL);
5685 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005686 }
5687 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005688 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5689 "Internal error: reference has content in CheckReference %s\n",
5690 name, NULL);
5691 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005692 }
5693 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005694 def = xmlHashLookup(grammar->defs, name);
5695 if (def != NULL) {
5696 cur = ref;
5697 while (cur != NULL) {
5698 cur->content = def;
5699 cur = cur->nextHash;
5700 }
5701 } else {
5702 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5703 "Reference %s has no matching definition\n", name,
5704 NULL);
5705 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005706 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005707 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5708 "Reference %s has no matching definition\n", name,
5709 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005710 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005711}
5712
5713/**
5714 * xmlRelaxNGCheckCombine:
5715 * @define: the define(s) list
5716 * @ctxt: a Relax-NG parser context
5717 * @name: the name associated to the defines
5718 *
5719 * Applies the 4.17. combine attribute rule for all the define
5720 * element of a given grammar using the same name.
5721 */
5722static void
5723xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005724 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5725{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005726 xmlChar *combine;
5727 int choiceOrInterleave = -1;
5728 int missing = 0;
5729 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5730
5731 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005732 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005733 cur = define;
5734 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005735 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5736 if (combine != NULL) {
5737 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5738 if (choiceOrInterleave == -1)
5739 choiceOrInterleave = 1;
5740 else if (choiceOrInterleave == 0) {
5741 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5742 "Defines for %s use both 'choice' and 'interleave'\n",
5743 name, NULL);
5744 }
5745 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5746 if (choiceOrInterleave == -1)
5747 choiceOrInterleave = 0;
5748 else if (choiceOrInterleave == 1) {
5749 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5750 "Defines for %s use both 'choice' and 'interleave'\n",
5751 name, NULL);
5752 }
5753 } else {
5754 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5755 "Defines for %s use unknown combine value '%s''\n",
5756 name, combine);
5757 }
5758 xmlFree(combine);
5759 } else {
5760 if (missing == 0)
5761 missing = 1;
5762 else {
5763 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5764 "Some defines for %s needs the combine attribute\n",
5765 name, NULL);
5766 }
5767 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005768
Daniel Veillard4c004142003-10-07 11:33:24 +00005769 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005770 }
5771#ifdef DEBUG
5772 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005773 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5774 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005775#endif
5776 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005777 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005778 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005779 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005780 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005781 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005782 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005783 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005784 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005785 tmp = define;
5786 last = NULL;
5787 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005788 if (tmp->content != NULL) {
5789 if (tmp->content->next != NULL) {
5790 /*
5791 * we need first to create a wrapper.
5792 */
5793 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5794 if (tmp2 == NULL)
5795 break;
5796 tmp2->type = XML_RELAXNG_GROUP;
5797 tmp2->content = tmp->content;
5798 } else {
5799 tmp2 = tmp->content;
5800 }
5801 if (last == NULL) {
5802 cur->content = tmp2;
5803 } else {
5804 last->next = tmp2;
5805 }
5806 last = tmp2;
5807 }
5808 tmp->content = cur;
5809 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005810 }
5811 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005812 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005813 if (ctxt->interleaves == NULL)
5814 ctxt->interleaves = xmlHashCreate(10);
5815 if (ctxt->interleaves == NULL) {
5816 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5817 "Failed to create interleaves hash table\n", NULL,
5818 NULL);
5819 } else {
5820 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005821
Daniel Veillard4c004142003-10-07 11:33:24 +00005822 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5823 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5824 0) {
5825 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5826 "Failed to add %s to hash table\n",
5827 (const xmlChar *) tmpname, NULL);
5828 }
5829 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005830 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005831}
5832
5833/**
5834 * xmlRelaxNGCombineStart:
5835 * @ctxt: a Relax-NG parser context
5836 * @grammar: the grammar
5837 *
5838 * Applies the 4.17. combine rule for all the start
5839 * element of a given grammar.
5840 */
5841static void
5842xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005843 xmlRelaxNGGrammarPtr grammar)
5844{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005845 xmlRelaxNGDefinePtr starts;
5846 xmlChar *combine;
5847 int choiceOrInterleave = -1;
5848 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005849 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005850
Daniel Veillard2df2de22003-02-17 23:34:33 +00005851 starts = grammar->start;
5852 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005853 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005854 cur = starts;
5855 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005856 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5857 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5858 combine = NULL;
5859 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5860 "Internal error: start element not found\n", NULL,
5861 NULL);
5862 } else {
5863 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5864 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005865
Daniel Veillard4c004142003-10-07 11:33:24 +00005866 if (combine != NULL) {
5867 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5868 if (choiceOrInterleave == -1)
5869 choiceOrInterleave = 1;
5870 else if (choiceOrInterleave == 0) {
5871 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5872 "<start> use both 'choice' and 'interleave'\n",
5873 NULL, NULL);
5874 }
5875 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5876 if (choiceOrInterleave == -1)
5877 choiceOrInterleave = 0;
5878 else if (choiceOrInterleave == 1) {
5879 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5880 "<start> use both 'choice' and 'interleave'\n",
5881 NULL, NULL);
5882 }
5883 } else {
5884 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5885 "<start> uses unknown combine value '%s''\n",
5886 combine, NULL);
5887 }
5888 xmlFree(combine);
5889 } else {
5890 if (missing == 0)
5891 missing = 1;
5892 else {
5893 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5894 "Some <start> element miss the combine attribute\n",
5895 NULL, NULL);
5896 }
5897 }
5898
5899 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005900 }
5901#ifdef DEBUG
5902 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005903 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5904 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005905#endif
5906 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005907 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005908 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005909 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005910 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005911 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005912 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005913 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005914 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005915 cur->content = grammar->start;
5916 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005917 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005918 if (ctxt->interleaves == NULL)
5919 ctxt->interleaves = xmlHashCreate(10);
5920 if (ctxt->interleaves == NULL) {
5921 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5922 "Failed to create interleaves hash table\n", NULL,
5923 NULL);
5924 } else {
5925 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005926
Daniel Veillard4c004142003-10-07 11:33:24 +00005927 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5928 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5929 0) {
5930 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5931 "Failed to add %s to hash table\n",
5932 (const xmlChar *) tmpname, NULL);
5933 }
5934 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005935 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005936}
5937
5938/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005939 * xmlRelaxNGCheckCycles:
5940 * @ctxt: a Relax-NG parser context
5941 * @nodes: grammar children nodes
5942 * @depth: the counter
5943 *
5944 * Check for cycles.
5945 *
5946 * Returns 0 if check passed, and -1 in case of error
5947 */
5948static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005949xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5950 xmlRelaxNGDefinePtr cur, int depth)
5951{
Daniel Veillardd4310742003-02-18 21:12:46 +00005952 int ret = 0;
5953
5954 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005955 if ((cur->type == XML_RELAXNG_REF) ||
5956 (cur->type == XML_RELAXNG_PARENTREF)) {
5957 if (cur->depth == -1) {
5958 cur->depth = depth;
5959 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5960 cur->depth = -2;
5961 } else if (depth == cur->depth) {
5962 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5963 "Detected a cycle in %s references\n",
5964 cur->name, NULL);
5965 return (-1);
5966 }
5967 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5968 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5969 } else {
5970 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5971 }
5972 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005973 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005974 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005975}
5976
5977/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005978 * xmlRelaxNGTryUnlink:
5979 * @ctxt: a Relax-NG parser context
5980 * @cur: the definition to unlink
5981 * @parent: the parent definition
5982 * @prev: the previous sibling definition
5983 *
5984 * Try to unlink a definition. If not possble make it a NOOP
5985 *
5986 * Returns the new prev definition
5987 */
5988static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005989xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5990 xmlRelaxNGDefinePtr cur,
5991 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5992{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005993 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005994 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005995 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005996 if (parent != NULL) {
5997 if (parent->content == cur)
5998 parent->content = cur->next;
5999 else if (parent->attrs == cur)
6000 parent->attrs = cur->next;
6001 else if (parent->nameClass == cur)
6002 parent->nameClass = cur->next;
6003 } else {
6004 cur->type = XML_RELAXNG_NOOP;
6005 prev = cur;
6006 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006007 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006008 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006009}
6010
6011/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006012 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006013 * @ctxt: a Relax-NG parser context
6014 * @nodes: grammar children nodes
6015 *
6016 * Check for simplification of empty and notAllowed
6017 */
6018static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006019xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6020 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6021{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006022 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006023
Daniel Veillardfd573f12003-03-16 17:52:32 +00006024 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006025 if ((cur->type == XML_RELAXNG_REF) ||
6026 (cur->type == XML_RELAXNG_PARENTREF)) {
6027 if (cur->depth != -3) {
6028 cur->depth = -3;
6029 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6030 }
6031 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6032 cur->parent = parent;
6033 if ((parent != NULL) &&
6034 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6035 (parent->type == XML_RELAXNG_LIST) ||
6036 (parent->type == XML_RELAXNG_GROUP) ||
6037 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6038 (parent->type == XML_RELAXNG_ONEORMORE) ||
6039 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6040 parent->type = XML_RELAXNG_NOT_ALLOWED;
6041 break;
6042 }
6043 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6044 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6045 } else
6046 prev = cur;
6047 } else if (cur->type == XML_RELAXNG_EMPTY) {
6048 cur->parent = parent;
6049 if ((parent != NULL) &&
6050 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6051 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6052 parent->type = XML_RELAXNG_EMPTY;
6053 break;
6054 }
6055 if ((parent != NULL) &&
6056 ((parent->type == XML_RELAXNG_GROUP) ||
6057 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6058 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6059 } else
6060 prev = cur;
6061 } else {
6062 cur->parent = parent;
6063 if (cur->content != NULL)
6064 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6065 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6066 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6067 if (cur->nameClass != NULL)
6068 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6069 /*
6070 * On Elements, try to move attribute only generating rules on
6071 * the attrs rules.
6072 */
6073 if (cur->type == XML_RELAXNG_ELEMENT) {
6074 int attronly;
6075 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006076
Daniel Veillard4c004142003-10-07 11:33:24 +00006077 while (cur->content != NULL) {
6078 attronly =
6079 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6080 if (attronly == 1) {
6081 /*
6082 * migrate cur->content to attrs
6083 */
6084 tmp = cur->content;
6085 cur->content = tmp->next;
6086 tmp->next = cur->attrs;
6087 cur->attrs = tmp;
6088 } else {
6089 /*
6090 * cur->content can generate elements or text
6091 */
6092 break;
6093 }
6094 }
6095 pre = cur->content;
6096 while ((pre != NULL) && (pre->next != NULL)) {
6097 tmp = pre->next;
6098 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6099 if (attronly == 1) {
6100 /*
6101 * migrate tmp to attrs
6102 */
6103 pre->next = tmp->next;
6104 tmp->next = cur->attrs;
6105 cur->attrs = tmp;
6106 } else {
6107 pre = tmp;
6108 }
6109 }
6110 }
6111 /*
6112 * This may result in a simplification
6113 */
6114 if ((cur->type == XML_RELAXNG_GROUP) ||
6115 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6116 if (cur->content == NULL)
6117 cur->type = XML_RELAXNG_EMPTY;
6118 else if (cur->content->next == NULL) {
6119 if ((parent == NULL) && (prev == NULL)) {
6120 cur->type = XML_RELAXNG_NOOP;
6121 } else if (prev == NULL) {
6122 parent->content = cur->content;
6123 cur->content->next = cur->next;
6124 cur = cur->content;
6125 } else {
6126 cur->content->next = cur->next;
6127 prev->next = cur->content;
6128 cur = cur->content;
6129 }
6130 }
6131 }
6132 /*
6133 * the current node may have been transformed back
6134 */
6135 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6136 (cur->content != NULL) &&
6137 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6138 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6139 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6140 if ((parent != NULL) &&
6141 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6142 (parent->type == XML_RELAXNG_LIST) ||
6143 (parent->type == XML_RELAXNG_GROUP) ||
6144 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6145 (parent->type == XML_RELAXNG_ONEORMORE) ||
6146 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6147 parent->type = XML_RELAXNG_NOT_ALLOWED;
6148 break;
6149 }
6150 if ((parent != NULL) &&
6151 (parent->type == XML_RELAXNG_CHOICE)) {
6152 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6153 } else
6154 prev = cur;
6155 } else if (cur->type == XML_RELAXNG_EMPTY) {
6156 if ((parent != NULL) &&
6157 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6158 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6159 parent->type = XML_RELAXNG_EMPTY;
6160 break;
6161 }
6162 if ((parent != NULL) &&
6163 ((parent->type == XML_RELAXNG_GROUP) ||
6164 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6165 (parent->type == XML_RELAXNG_CHOICE))) {
6166 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6167 } else
6168 prev = cur;
6169 } else {
6170 prev = cur;
6171 }
6172 }
6173 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006174 }
6175}
6176
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006177/**
6178 * xmlRelaxNGGroupContentType:
6179 * @ct1: the first content type
6180 * @ct2: the second content type
6181 *
6182 * Try to group 2 content types
6183 *
6184 * Returns the content type
6185 */
6186static xmlRelaxNGContentType
6187xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006188 xmlRelaxNGContentType ct2)
6189{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006190 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006191 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6192 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006193 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006194 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006195 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006196 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006197 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006198 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6199 return (XML_RELAXNG_CONTENT_COMPLEX);
6200 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006201}
6202
6203/**
6204 * xmlRelaxNGMaxContentType:
6205 * @ct1: the first content type
6206 * @ct2: the second content type
6207 *
6208 * Compute the max content-type
6209 *
6210 * Returns the content type
6211 */
6212static xmlRelaxNGContentType
6213xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006214 xmlRelaxNGContentType ct2)
6215{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006216 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006217 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6218 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006219 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006220 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6221 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006222 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006223 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6224 return (XML_RELAXNG_CONTENT_COMPLEX);
6225 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006226}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006227
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006228/**
6229 * xmlRelaxNGCheckRules:
6230 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006231 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006232 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006233 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006234 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006235 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006236 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006237 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006238 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006239static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006240xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6241 xmlRelaxNGDefinePtr cur, int flags,
6242 xmlRelaxNGType ptype)
6243{
Daniel Veillardd44b9362009-09-07 12:15:08 +02006244 int nflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006245 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006246
Daniel Veillardfd573f12003-03-16 17:52:32 +00006247 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006248 ret = XML_RELAXNG_CONTENT_EMPTY;
6249 if ((cur->type == XML_RELAXNG_REF) ||
6250 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006251 /*
6252 * This should actually be caught by list//element(ref) at the
6253 * element boundaries, c.f. Bug #159968 local refs are dropped
6254 * in step 4.19.
6255 */
6256#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006257 if (flags & XML_RELAXNG_IN_LIST) {
6258 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6259 "Found forbidden pattern list//ref\n", NULL,
6260 NULL);
6261 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006262#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006263 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6264 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6265 "Found forbidden pattern data/except//ref\n",
6266 NULL, NULL);
6267 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006268 if (cur->content == NULL) {
6269 if (cur->type == XML_RELAXNG_PARENTREF)
6270 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6271 "Internal found no define for parent refs\n",
6272 NULL, NULL);
6273 else
6274 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6275 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006276 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006277 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006278 if (cur->depth > -4) {
6279 cur->depth = -4;
6280 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6281 flags, cur->type);
6282 cur->depth = ret - 15;
6283 } else if (cur->depth == -4) {
6284 ret = XML_RELAXNG_CONTENT_COMPLEX;
6285 } else {
6286 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6287 }
6288 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6289 /*
6290 * The 7.3 Attribute derivation rule for groups is plugged there
6291 */
6292 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6293 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6294 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6295 "Found forbidden pattern data/except//element(ref)\n",
6296 NULL, NULL);
6297 }
6298 if (flags & XML_RELAXNG_IN_LIST) {
6299 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6300 "Found forbidden pattern list//element(ref)\n",
6301 NULL, NULL);
6302 }
6303 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6304 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6305 "Found forbidden pattern attribute//element(ref)\n",
6306 NULL, NULL);
6307 }
6308 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6309 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6310 "Found forbidden pattern attribute//element(ref)\n",
6311 NULL, NULL);
6312 }
6313 /*
6314 * reset since in the simple form elements are only child
6315 * of grammar/define
6316 */
6317 nflags = 0;
6318 ret =
6319 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6320 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6321 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6322 "Element %s attributes have a content type error\n",
6323 cur->name, NULL);
6324 }
6325 ret =
6326 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6327 cur->type);
6328 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6329 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6330 "Element %s has a content type error\n",
6331 cur->name, NULL);
6332 } else {
6333 ret = XML_RELAXNG_CONTENT_COMPLEX;
6334 }
6335 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6336 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6337 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6338 "Found forbidden pattern attribute//attribute\n",
6339 NULL, NULL);
6340 }
6341 if (flags & XML_RELAXNG_IN_LIST) {
6342 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6343 "Found forbidden pattern list//attribute\n",
6344 NULL, NULL);
6345 }
6346 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6347 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6348 "Found forbidden pattern oneOrMore//group//attribute\n",
6349 NULL, NULL);
6350 }
6351 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6352 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6353 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6354 NULL, NULL);
6355 }
6356 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6357 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6358 "Found forbidden pattern data/except//attribute\n",
6359 NULL, NULL);
6360 }
6361 if (flags & XML_RELAXNG_IN_START) {
6362 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6363 "Found forbidden pattern start//attribute\n",
6364 NULL, NULL);
6365 }
6366 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6367 && (cur->name == NULL)) {
6368 if (cur->ns == NULL) {
6369 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6370 "Found anyName attribute without oneOrMore ancestor\n",
6371 NULL, NULL);
6372 } else {
6373 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6374 "Found nsName attribute without oneOrMore ancestor\n",
6375 NULL, NULL);
6376 }
6377 }
6378 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6379 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6380 ret = XML_RELAXNG_CONTENT_EMPTY;
6381 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6382 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6383 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6384 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6385 "Found forbidden pattern data/except//oneOrMore\n",
6386 NULL, NULL);
6387 }
6388 if (flags & XML_RELAXNG_IN_START) {
6389 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6390 "Found forbidden pattern start//oneOrMore\n",
6391 NULL, NULL);
6392 }
6393 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6394 ret =
6395 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6396 cur->type);
6397 ret = xmlRelaxNGGroupContentType(ret, ret);
6398 } else if (cur->type == XML_RELAXNG_LIST) {
6399 if (flags & XML_RELAXNG_IN_LIST) {
6400 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6401 "Found forbidden pattern list//list\n", NULL,
6402 NULL);
6403 }
6404 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6405 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6406 "Found forbidden pattern data/except//list\n",
6407 NULL, NULL);
6408 }
6409 if (flags & XML_RELAXNG_IN_START) {
6410 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6411 "Found forbidden pattern start//list\n", NULL,
6412 NULL);
6413 }
6414 nflags = flags | XML_RELAXNG_IN_LIST;
6415 ret =
6416 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6417 cur->type);
6418 } else if (cur->type == XML_RELAXNG_GROUP) {
6419 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6420 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6421 "Found forbidden pattern data/except//group\n",
6422 NULL, NULL);
6423 }
6424 if (flags & XML_RELAXNG_IN_START) {
6425 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6426 "Found forbidden pattern start//group\n", NULL,
6427 NULL);
6428 }
6429 if (flags & XML_RELAXNG_IN_ONEORMORE)
6430 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6431 else
6432 nflags = flags;
6433 ret =
6434 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6435 cur->type);
6436 /*
6437 * The 7.3 Attribute derivation rule for groups is plugged there
6438 */
6439 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6440 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6441 if (flags & XML_RELAXNG_IN_LIST) {
6442 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6443 "Found forbidden pattern list//interleave\n",
6444 NULL, NULL);
6445 }
6446 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6447 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6448 "Found forbidden pattern data/except//interleave\n",
6449 NULL, NULL);
6450 }
6451 if (flags & XML_RELAXNG_IN_START) {
6452 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6453 "Found forbidden pattern start//interleave\n",
6454 NULL, NULL);
6455 }
6456 if (flags & XML_RELAXNG_IN_ONEORMORE)
6457 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6458 else
6459 nflags = flags;
6460 ret =
6461 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6462 cur->type);
6463 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6464 if ((cur->parent != NULL) &&
6465 (cur->parent->type == XML_RELAXNG_DATATYPE))
6466 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6467 else
6468 nflags = flags;
6469 ret =
6470 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6471 cur->type);
6472 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6473 if (flags & XML_RELAXNG_IN_START) {
6474 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6475 "Found forbidden pattern start//data\n", NULL,
6476 NULL);
6477 }
6478 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6479 ret = XML_RELAXNG_CONTENT_SIMPLE;
6480 } else if (cur->type == XML_RELAXNG_VALUE) {
6481 if (flags & XML_RELAXNG_IN_START) {
6482 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6483 "Found forbidden pattern start//value\n", NULL,
6484 NULL);
6485 }
6486 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6487 ret = XML_RELAXNG_CONTENT_SIMPLE;
6488 } else if (cur->type == XML_RELAXNG_TEXT) {
6489 if (flags & XML_RELAXNG_IN_LIST) {
6490 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6491 "Found forbidden pattern list//text\n", NULL,
6492 NULL);
6493 }
6494 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6495 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6496 "Found forbidden pattern data/except//text\n",
6497 NULL, NULL);
6498 }
6499 if (flags & XML_RELAXNG_IN_START) {
6500 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6501 "Found forbidden pattern start//text\n", NULL,
6502 NULL);
6503 }
6504 ret = XML_RELAXNG_CONTENT_COMPLEX;
6505 } else if (cur->type == XML_RELAXNG_EMPTY) {
6506 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6507 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6508 "Found forbidden pattern data/except//empty\n",
6509 NULL, NULL);
6510 }
6511 if (flags & XML_RELAXNG_IN_START) {
6512 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6513 "Found forbidden pattern start//empty\n", NULL,
6514 NULL);
6515 }
6516 ret = XML_RELAXNG_CONTENT_EMPTY;
6517 } else if (cur->type == XML_RELAXNG_CHOICE) {
6518 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6519 ret =
6520 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6521 } else {
6522 ret =
6523 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6524 }
6525 cur = cur->next;
6526 if (ptype == XML_RELAXNG_GROUP) {
6527 val = xmlRelaxNGGroupContentType(val, ret);
6528 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02006529 /*
6530 * TODO: scan complain that tmp is never used, seems on purpose
6531 * need double-checking
6532 */
Daniel Veillard4c004142003-10-07 11:33:24 +00006533 tmp = xmlRelaxNGGroupContentType(val, ret);
6534 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6535 tmp = xmlRelaxNGMaxContentType(val, ret);
6536 } else if (ptype == XML_RELAXNG_CHOICE) {
6537 val = xmlRelaxNGMaxContentType(val, ret);
6538 } else if (ptype == XML_RELAXNG_LIST) {
6539 val = XML_RELAXNG_CONTENT_SIMPLE;
6540 } else if (ptype == XML_RELAXNG_EXCEPT) {
6541 if (ret == XML_RELAXNG_CONTENT_ERROR)
6542 val = XML_RELAXNG_CONTENT_ERROR;
6543 else
6544 val = XML_RELAXNG_CONTENT_SIMPLE;
6545 } else {
6546 val = xmlRelaxNGGroupContentType(val, ret);
6547 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006548
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006549 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006550 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006551}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006552
6553/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006554 * xmlRelaxNGParseGrammar:
6555 * @ctxt: a Relax-NG parser context
6556 * @nodes: grammar children nodes
6557 *
6558 * parse a Relax-NG <grammar> node
6559 *
6560 * Returns the internal xmlRelaxNGGrammarPtr built or
6561 * NULL in case of error
6562 */
6563static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006564xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6565{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006566 xmlRelaxNGGrammarPtr ret, tmp, old;
6567
Daniel Veillardc482e262003-02-26 14:48:48 +00006568#ifdef DEBUG_GRAMMAR
6569 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6570#endif
6571
Daniel Veillard6eadf632003-01-23 18:29:16 +00006572 ret = xmlRelaxNGNewGrammar(ctxt);
6573 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006574 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006575
6576 /*
6577 * Link the new grammar in the tree
6578 */
6579 ret->parent = ctxt->grammar;
6580 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006581 tmp = ctxt->grammar->children;
6582 if (tmp == NULL) {
6583 ctxt->grammar->children = ret;
6584 } else {
6585 while (tmp->next != NULL)
6586 tmp = tmp->next;
6587 tmp->next = ret;
6588 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006589 }
6590
6591 old = ctxt->grammar;
6592 ctxt->grammar = ret;
6593 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6594 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006595 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006596 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6597 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006598 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006599 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6600 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006601 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006602
6603 /*
6604 * Apply 4.17 mergingd rules to defines and starts
6605 */
6606 xmlRelaxNGCombineStart(ctxt, ret);
6607 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006608 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6609 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006610 }
6611
6612 /*
6613 * link together defines and refs in this grammar
6614 */
6615 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006616 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6617 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006618 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006619
Daniel Veillard81c51e12009-08-14 18:52:10 +02006620
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006621 /* @@@@ */
6622
Daniel Veillard6eadf632003-01-23 18:29:16 +00006623 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006624 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006625}
6626
6627/**
6628 * xmlRelaxNGParseDocument:
6629 * @ctxt: a Relax-NG parser context
6630 * @node: the root node of the RelaxNG schema
6631 *
6632 * parse a Relax-NG definition resource and build an internal
6633 * xmlRelaxNG struture which can be used to validate instances.
6634 *
6635 * Returns the internal XML RelaxNG structure built or
6636 * NULL in case of error
6637 */
6638static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006639xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6640{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006641 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006642 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006643 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006644
6645 if ((ctxt == NULL) || (node == NULL))
6646 return (NULL);
6647
6648 schema = xmlRelaxNGNewRelaxNG(ctxt);
6649 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006650 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006651
Daniel Veillard276be4a2003-01-24 01:03:34 +00006652 olddefine = ctxt->define;
6653 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006654 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006655 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006656 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006657 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006658
Daniel Veillard4c004142003-10-07 11:33:24 +00006659 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6660 if (schema->topgrammar == NULL) {
6661 return (schema);
6662 }
6663 /*
6664 * Link the new grammar in the tree
6665 */
6666 ret->parent = ctxt->grammar;
6667 if (ctxt->grammar != NULL) {
6668 tmp = ctxt->grammar->children;
6669 if (tmp == NULL) {
6670 ctxt->grammar->children = ret;
6671 } else {
6672 while (tmp->next != NULL)
6673 tmp = tmp->next;
6674 tmp->next = ret;
6675 }
6676 }
6677 old = ctxt->grammar;
6678 ctxt->grammar = ret;
6679 xmlRelaxNGParseStart(ctxt, node);
6680 if (old != NULL)
6681 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006682 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006683 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006684 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006685 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6686 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6687 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6688 while ((schema->topgrammar->start != NULL) &&
6689 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6690 (schema->topgrammar->start->next != NULL))
6691 schema->topgrammar->start =
6692 schema->topgrammar->start->content;
6693 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6694 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6695 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006696 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006697#ifdef DEBUG
6698 if (schema == NULL)
6699 xmlGenericError(xmlGenericErrorContext,
6700 "xmlRelaxNGParseDocument() failed\n");
6701#endif
6702
6703 return (schema);
6704}
6705
6706/************************************************************************
6707 * *
6708 * Reading RelaxNGs *
6709 * *
6710 ************************************************************************/
6711
6712/**
6713 * xmlRelaxNGNewParserCtxt:
6714 * @URL: the location of the schema
6715 *
6716 * Create an XML RelaxNGs parse context for that file/resource expected
6717 * to contain an XML RelaxNGs file.
6718 *
6719 * Returns the parser context or NULL in case of error
6720 */
6721xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006722xmlRelaxNGNewParserCtxt(const char *URL)
6723{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006724 xmlRelaxNGParserCtxtPtr ret;
6725
6726 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006727 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006728
Daniel Veillard4c004142003-10-07 11:33:24 +00006729 ret =
6730 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006731 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006732 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006733 return (NULL);
6734 }
6735 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006736 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006737 ret->error = xmlGenericError;
6738 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006739 return (ret);
6740}
6741
6742/**
6743 * xmlRelaxNGNewMemParserCtxt:
6744 * @buffer: a pointer to a char array containing the schemas
6745 * @size: the size of the array
6746 *
6747 * Create an XML RelaxNGs parse context for that memory buffer expected
6748 * to contain an XML RelaxNGs file.
6749 *
6750 * Returns the parser context or NULL in case of error
6751 */
6752xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006753xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6754{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006755 xmlRelaxNGParserCtxtPtr ret;
6756
6757 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006758 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006759
Daniel Veillard4c004142003-10-07 11:33:24 +00006760 ret =
6761 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006762 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006763 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006764 return (NULL);
6765 }
6766 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6767 ret->buffer = buffer;
6768 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006769 ret->error = xmlGenericError;
6770 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006771 return (ret);
6772}
6773
6774/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006775 * xmlRelaxNGNewDocParserCtxt:
6776 * @doc: a preparsed document tree
6777 *
6778 * Create an XML RelaxNGs parser context for that document.
6779 * Note: since the process of compiling a RelaxNG schemas modifies the
6780 * document, the @doc parameter is duplicated internally.
6781 *
6782 * Returns the parser context or NULL in case of error
6783 */
6784xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006785xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6786{
Daniel Veillard33300b42003-04-17 09:09:19 +00006787 xmlRelaxNGParserCtxtPtr ret;
6788 xmlDocPtr copy;
6789
6790 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006791 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006792 copy = xmlCopyDoc(doc, 1);
6793 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006794 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006795
Daniel Veillard4c004142003-10-07 11:33:24 +00006796 ret =
6797 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006798 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006799 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006800 return (NULL);
6801 }
6802 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6803 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006804 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006805 ret->userData = xmlGenericErrorContext;
6806 return (ret);
6807}
6808
6809/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006810 * xmlRelaxNGFreeParserCtxt:
6811 * @ctxt: the schema parser context
6812 *
6813 * Free the resources associated to the schema parser context
6814 */
6815void
Daniel Veillard4c004142003-10-07 11:33:24 +00006816xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6817{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006818 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006819 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006820 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006821 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006822 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006823 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006824 if (ctxt->interleaves != NULL)
6825 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006826 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006827 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006828 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006829 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006830 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006831 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006832 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006833 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006834 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006835 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006836
Daniel Veillard4c004142003-10-07 11:33:24 +00006837 for (i = 0; i < ctxt->defNr; i++)
6838 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6839 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006840 }
Daniel Veillard42595322004-11-08 10:52:06 +00006841 if ((ctxt->document != NULL) && (ctxt->freedoc))
6842 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006843 xmlFree(ctxt);
6844}
6845
Daniel Veillard6eadf632003-01-23 18:29:16 +00006846/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006847 * xmlRelaxNGNormExtSpace:
6848 * @value: a value
6849 *
6850 * Removes the leading and ending spaces of the value
6851 * The string is modified "in situ"
6852 */
6853static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006854xmlRelaxNGNormExtSpace(xmlChar * value)
6855{
Daniel Veillardd2298792003-02-14 16:54:11 +00006856 xmlChar *start = value;
6857 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006858
Daniel Veillard4c004142003-10-07 11:33:24 +00006859 if (value == NULL)
6860 return;
6861
William M. Brack76e95df2003-10-18 16:20:14 +00006862 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006863 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006864 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006865 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006866 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006867 cur++;
6868 if (*cur == 0)
6869 return;
6870 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006871 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006872 cur++;
6873 if (*cur == 0) {
6874 *start = 0;
6875 return;
6876 }
6877 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006878 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006879 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006880 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006881 *start++ = *cur++;
6882 if (*cur == 0) {
6883 *start = 0;
6884 return;
6885 }
6886 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006887 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006888 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006889 if (*cur == 0) {
6890 *start = 0;
6891 return;
6892 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006893 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006894 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006895 }
6896}
6897
6898/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006899 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006900 * @ctxt: a Relax-NG parser context
6901 * @node: a Relax-NG node
6902 *
6903 * Check all the attributes on the given node
6904 */
6905static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006906xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6907{
Daniel Veillardd2298792003-02-14 16:54:11 +00006908 xmlAttrPtr cur, next;
6909
6910 cur = node->properties;
6911 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006912 next = cur->next;
6913 if ((cur->ns == NULL) ||
6914 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6915 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6916 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6917 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6918 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6919 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6920 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6921 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6922 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6923 "Attribute %s is not allowed on %s\n",
6924 cur->name, node->name);
6925 }
6926 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6927 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6928 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6929 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6930 "Attribute %s is not allowed on %s\n",
6931 cur->name, node->name);
6932 }
6933 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6934 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6935 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6936 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6937 "Attribute %s is not allowed on %s\n",
6938 cur->name, node->name);
6939 }
6940 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6941 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6942 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6943 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6944 "Attribute %s is not allowed on %s\n",
6945 cur->name, node->name);
6946 }
6947 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6948 xmlChar *val;
6949 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006950
Daniel Veillard4c004142003-10-07 11:33:24 +00006951 val = xmlNodeListGetString(node->doc, cur->children, 1);
6952 if (val != NULL) {
6953 if (val[0] != 0) {
6954 uri = xmlParseURI((const char *) val);
6955 if (uri == NULL) {
6956 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6957 "Attribute %s contains invalid URI %s\n",
6958 cur->name, val);
6959 } else {
6960 if (uri->scheme == NULL) {
6961 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6962 "Attribute %s URI %s is not absolute\n",
6963 cur->name, val);
6964 }
6965 if (uri->fragment != NULL) {
6966 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6967 "Attribute %s URI %s has a fragment ID\n",
6968 cur->name, val);
6969 }
6970 xmlFreeURI(uri);
6971 }
6972 }
6973 xmlFree(val);
6974 }
6975 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6976 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6977 "Unknown attribute %s on %s\n", cur->name,
6978 node->name);
6979 }
6980 }
6981 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006982 }
6983}
6984
6985/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006986 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006987 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006988 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006989 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006990 * Cleanup the subtree from unwanted nodes for parsing, resolve
6991 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006992 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006993static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006994xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6995{
Daniel Veillardc5312d72003-02-21 17:14:10 +00006996 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006997
Daniel Veillard6eadf632003-01-23 18:29:16 +00006998 delete = NULL;
6999 cur = root;
7000 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007001 if (delete != NULL) {
7002 xmlUnlinkNode(delete);
7003 xmlFreeNode(delete);
7004 delete = NULL;
7005 }
7006 if (cur->type == XML_ELEMENT_NODE) {
7007 /*
7008 * Simplification 4.1. Annotations
7009 */
7010 if ((cur->ns == NULL) ||
7011 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
7012 if ((cur->parent != NULL) &&
7013 (cur->parent->type == XML_ELEMENT_NODE) &&
7014 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7015 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7016 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7017 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7018 "element %s doesn't allow foreign elements\n",
7019 cur->parent->name, NULL);
7020 }
7021 delete = cur;
7022 goto skip_children;
7023 } else {
7024 xmlRelaxNGCleanupAttributes(ctxt, cur);
7025 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7026 xmlChar *href, *ns, *base, *URL;
7027 xmlRelaxNGDocumentPtr docu;
7028 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00007029 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007030
Daniel Veillard4c004142003-10-07 11:33:24 +00007031 ns = xmlGetProp(cur, BAD_CAST "ns");
7032 if (ns == NULL) {
7033 tmp = cur->parent;
7034 while ((tmp != NULL) &&
7035 (tmp->type == XML_ELEMENT_NODE)) {
7036 ns = xmlGetProp(tmp, BAD_CAST "ns");
7037 if (ns != NULL)
7038 break;
7039 tmp = tmp->parent;
7040 }
7041 }
7042 href = xmlGetProp(cur, BAD_CAST "href");
7043 if (href == NULL) {
7044 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7045 "xmlRelaxNGParse: externalRef has no href attribute\n",
7046 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007047 if (ns != NULL)
7048 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007049 delete = cur;
7050 goto skip_children;
7051 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007052 uri = xmlParseURI((const char *) href);
7053 if (uri == NULL) {
7054 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7055 "Incorrect URI for externalRef %s\n",
7056 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007057 if (ns != NULL)
7058 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007059 if (href != NULL)
7060 xmlFree(href);
7061 delete = cur;
7062 goto skip_children;
7063 }
7064 if (uri->fragment != NULL) {
7065 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7066 "Fragment forbidden in URI for externalRef %s\n",
7067 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007068 if (ns != NULL)
7069 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007070 xmlFreeURI(uri);
7071 if (href != NULL)
7072 xmlFree(href);
7073 delete = cur;
7074 goto skip_children;
7075 }
7076 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007077 base = xmlNodeGetBase(cur->doc, cur);
7078 URL = xmlBuildURI(href, base);
7079 if (URL == NULL) {
7080 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7081 "Failed to compute URL for externalRef %s\n",
7082 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007083 if (ns != NULL)
7084 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007085 if (href != NULL)
7086 xmlFree(href);
7087 if (base != NULL)
7088 xmlFree(base);
7089 delete = cur;
7090 goto skip_children;
7091 }
7092 if (href != NULL)
7093 xmlFree(href);
7094 if (base != NULL)
7095 xmlFree(base);
7096 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7097 if (docu == NULL) {
7098 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7099 "Failed to load externalRef %s\n", URL,
7100 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007101 if (ns != NULL)
7102 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007103 xmlFree(URL);
7104 delete = cur;
7105 goto skip_children;
7106 }
7107 if (ns != NULL)
7108 xmlFree(ns);
7109 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007110 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007111 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7112 xmlChar *href, *ns, *base, *URL;
7113 xmlRelaxNGIncludePtr incl;
7114 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007115
Daniel Veillard4c004142003-10-07 11:33:24 +00007116 href = xmlGetProp(cur, BAD_CAST "href");
7117 if (href == NULL) {
7118 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7119 "xmlRelaxNGParse: include has no href attribute\n",
7120 NULL, NULL);
7121 delete = cur;
7122 goto skip_children;
7123 }
7124 base = xmlNodeGetBase(cur->doc, cur);
7125 URL = xmlBuildURI(href, base);
7126 if (URL == NULL) {
7127 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7128 "Failed to compute URL for include %s\n",
7129 href, NULL);
7130 if (href != NULL)
7131 xmlFree(href);
7132 if (base != NULL)
7133 xmlFree(base);
7134 delete = cur;
7135 goto skip_children;
7136 }
7137 if (href != NULL)
7138 xmlFree(href);
7139 if (base != NULL)
7140 xmlFree(base);
7141 ns = xmlGetProp(cur, BAD_CAST "ns");
7142 if (ns == NULL) {
7143 tmp = cur->parent;
7144 while ((tmp != NULL) &&
7145 (tmp->type == XML_ELEMENT_NODE)) {
7146 ns = xmlGetProp(tmp, BAD_CAST "ns");
7147 if (ns != NULL)
7148 break;
7149 tmp = tmp->parent;
7150 }
7151 }
7152 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7153 if (ns != NULL)
7154 xmlFree(ns);
7155 if (incl == NULL) {
7156 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7157 "Failed to load include %s\n", URL,
7158 NULL);
7159 xmlFree(URL);
7160 delete = cur;
7161 goto skip_children;
7162 }
7163 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007164 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007165 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7166 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7167 {
7168 xmlChar *name, *ns;
7169 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007170
Daniel Veillard4c004142003-10-07 11:33:24 +00007171 /*
7172 * Simplification 4.8. name attribute of element
7173 * and attribute elements
7174 */
7175 name = xmlGetProp(cur, BAD_CAST "name");
7176 if (name != NULL) {
7177 if (cur->children == NULL) {
7178 text =
7179 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7180 name);
7181 } else {
7182 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007183
Daniel Veillard03a53c32004-10-26 16:06:51 +00007184 node = xmlNewDocNode(cur->doc, cur->ns,
7185 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007186 if (node != NULL) {
7187 xmlAddPrevSibling(cur->children, node);
7188 text = xmlNewText(name);
7189 xmlAddChild(node, text);
7190 text = node;
7191 }
7192 }
7193 if (text == NULL) {
7194 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7195 "Failed to create a name %s element\n",
7196 name, NULL);
7197 }
7198 xmlUnsetProp(cur, BAD_CAST "name");
7199 xmlFree(name);
7200 ns = xmlGetProp(cur, BAD_CAST "ns");
7201 if (ns != NULL) {
7202 if (text != NULL) {
7203 xmlSetProp(text, BAD_CAST "ns", ns);
7204 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7205 }
7206 xmlFree(ns);
7207 } else if (xmlStrEqual(cur->name,
7208 BAD_CAST "attribute")) {
7209 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7210 }
7211 }
7212 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7213 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7214 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7215 /*
7216 * Simplification 4.8. name attribute of element
7217 * and attribute elements
7218 */
7219 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7220 xmlNodePtr node;
7221 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007222
Daniel Veillard4c004142003-10-07 11:33:24 +00007223 node = cur->parent;
7224 while ((node != NULL) &&
7225 (node->type == XML_ELEMENT_NODE)) {
7226 ns = xmlGetProp(node, BAD_CAST "ns");
7227 if (ns != NULL) {
7228 break;
7229 }
7230 node = node->parent;
7231 }
7232 if (ns == NULL) {
7233 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7234 } else {
7235 xmlSetProp(cur, BAD_CAST "ns", ns);
7236 xmlFree(ns);
7237 }
7238 }
7239 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7240 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007241
Daniel Veillard4c004142003-10-07 11:33:24 +00007242 /*
7243 * Simplification: 4.10. QNames
7244 */
7245 name = xmlNodeGetContent(cur);
7246 if (name != NULL) {
7247 local = xmlSplitQName2(name, &prefix);
7248 if (local != NULL) {
7249 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007250
Daniel Veillard4c004142003-10-07 11:33:24 +00007251 ns = xmlSearchNs(cur->doc, cur, prefix);
7252 if (ns == NULL) {
7253 xmlRngPErr(ctxt, cur,
7254 XML_RNGP_PREFIX_UNDEFINED,
7255 "xmlRelaxNGParse: no namespace for prefix %s\n",
7256 prefix, NULL);
7257 } else {
7258 xmlSetProp(cur, BAD_CAST "ns",
7259 ns->href);
7260 xmlNodeSetContent(cur, local);
7261 }
7262 xmlFree(local);
7263 xmlFree(prefix);
7264 }
7265 xmlFree(name);
7266 }
7267 }
7268 /*
7269 * 4.16
7270 */
7271 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7272 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7273 xmlRngPErr(ctxt, cur,
7274 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7275 "Found nsName/except//nsName forbidden construct\n",
7276 NULL, NULL);
7277 }
7278 }
7279 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7280 (cur != root)) {
7281 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007282
Daniel Veillard4c004142003-10-07 11:33:24 +00007283 /*
7284 * 4.16
7285 */
7286 if ((cur->parent != NULL) &&
7287 (xmlStrEqual
7288 (cur->parent->name, BAD_CAST "anyName"))) {
7289 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7290 xmlRelaxNGCleanupTree(ctxt, cur);
7291 ctxt->flags = oldflags;
7292 goto skip_children;
7293 } else if ((cur->parent != NULL) &&
7294 (xmlStrEqual
7295 (cur->parent->name, BAD_CAST "nsName"))) {
7296 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7297 xmlRelaxNGCleanupTree(ctxt, cur);
7298 ctxt->flags = oldflags;
7299 goto skip_children;
7300 }
7301 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7302 /*
7303 * 4.16
7304 */
7305 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7306 xmlRngPErr(ctxt, cur,
7307 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7308 "Found anyName/except//anyName forbidden construct\n",
7309 NULL, NULL);
7310 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7311 xmlRngPErr(ctxt, cur,
7312 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7313 "Found nsName/except//anyName forbidden construct\n",
7314 NULL, NULL);
7315 }
7316 }
7317 /*
7318 * Thisd is not an else since "include" is transformed
7319 * into a div
7320 */
7321 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7322 xmlChar *ns;
7323 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007324
Daniel Veillard4c004142003-10-07 11:33:24 +00007325 /*
7326 * implements rule 4.11
7327 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007328
Daniel Veillard4c004142003-10-07 11:33:24 +00007329 ns = xmlGetProp(cur, BAD_CAST "ns");
7330
7331 child = cur->children;
7332 ins = cur;
7333 while (child != NULL) {
7334 if (ns != NULL) {
7335 if (!xmlHasProp(child, BAD_CAST "ns")) {
7336 xmlSetProp(child, BAD_CAST "ns", ns);
7337 }
7338 }
7339 tmp = child->next;
7340 xmlUnlinkNode(child);
7341 ins = xmlAddNextSibling(ins, child);
7342 child = tmp;
7343 }
7344 if (ns != NULL)
7345 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007346 /*
7347 * Since we are about to delete cur, if it's nsDef is non-NULL we
7348 * need to preserve it (it contains the ns definitions for the
7349 * children we just moved). We'll just stick it on to the end
7350 * of cur->parent's list, since it's never going to be re-serialized
7351 * (bug 143738).
7352 */
7353 if (cur->nsDef != NULL) {
7354 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7355 while (parDef->next != NULL)
7356 parDef = parDef->next;
7357 parDef->next = cur->nsDef;
7358 cur->nsDef = NULL;
7359 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007360 delete = cur;
7361 goto skip_children;
7362 }
7363 }
7364 }
7365 /*
7366 * Simplification 4.2 whitespaces
7367 */
7368 else if ((cur->type == XML_TEXT_NODE) ||
7369 (cur->type == XML_CDATA_SECTION_NODE)) {
7370 if (IS_BLANK_NODE(cur)) {
7371 if (cur->parent->type == XML_ELEMENT_NODE) {
7372 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7373 &&
7374 (!xmlStrEqual
7375 (cur->parent->name, BAD_CAST "param")))
7376 delete = cur;
7377 } else {
7378 delete = cur;
7379 goto skip_children;
7380 }
7381 }
7382 } else {
7383 delete = cur;
7384 goto skip_children;
7385 }
7386
7387 /*
7388 * Skip to next node
7389 */
7390 if (cur->children != NULL) {
7391 if ((cur->children->type != XML_ENTITY_DECL) &&
7392 (cur->children->type != XML_ENTITY_REF_NODE) &&
7393 (cur->children->type != XML_ENTITY_NODE)) {
7394 cur = cur->children;
7395 continue;
7396 }
7397 }
7398 skip_children:
7399 if (cur->next != NULL) {
7400 cur = cur->next;
7401 continue;
7402 }
7403
7404 do {
7405 cur = cur->parent;
7406 if (cur == NULL)
7407 break;
7408 if (cur == root) {
7409 cur = NULL;
7410 break;
7411 }
7412 if (cur->next != NULL) {
7413 cur = cur->next;
7414 break;
7415 }
7416 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007417 }
7418 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007419 xmlUnlinkNode(delete);
7420 xmlFreeNode(delete);
7421 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007422 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007423}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007424
Daniel Veillardc5312d72003-02-21 17:14:10 +00007425/**
7426 * xmlRelaxNGCleanupDoc:
7427 * @ctxt: a Relax-NG parser context
7428 * @doc: an xmldocPtr document pointer
7429 *
7430 * Cleanup the document from unwanted nodes for parsing, resolve
7431 * Include and externalRef lookups.
7432 *
7433 * Returns the cleaned up document or NULL in case of error
7434 */
7435static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007436xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7437{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007438 xmlNodePtr root;
7439
7440 /*
7441 * Extract the root
7442 */
7443 root = xmlDocGetRootElement(doc);
7444 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007445 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7446 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007447 return (NULL);
7448 }
7449 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007450 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007451}
7452
7453/**
7454 * xmlRelaxNGParse:
7455 * @ctxt: a Relax-NG parser context
7456 *
7457 * parse a schema definition resource and build an internal
7458 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007459 *
7460 * Returns the internal XML RelaxNG structure built from the resource or
7461 * NULL in case of error
7462 */
7463xmlRelaxNGPtr
7464xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7465{
7466 xmlRelaxNGPtr ret = NULL;
7467 xmlDocPtr doc;
7468 xmlNodePtr root;
7469
7470 xmlRelaxNGInitTypes();
7471
7472 if (ctxt == NULL)
7473 return (NULL);
7474
7475 /*
7476 * First step is to parse the input document into an DOM/Infoset
7477 */
7478 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007479 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007480 if (doc == NULL) {
7481 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7482 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7483 NULL);
7484 return (NULL);
7485 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007486 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007487 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007488 if (doc == NULL) {
7489 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7490 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7491 NULL);
7492 return (NULL);
7493 }
7494 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7495 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007496 } else if (ctxt->document != NULL) {
7497 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007498 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007499 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7500 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7501 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007502 }
7503 ctxt->document = doc;
7504
7505 /*
7506 * Some preprocessing of the document content
7507 */
7508 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7509 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007510 xmlFreeDoc(ctxt->document);
7511 ctxt->document = NULL;
7512 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007513 }
7514
Daniel Veillard6eadf632003-01-23 18:29:16 +00007515 /*
7516 * Then do the parsing for good
7517 */
7518 root = xmlDocGetRootElement(doc);
7519 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007520 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7521 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007522 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillard3f845a92006-04-13 07:33:44 +00007523
7524 xmlFreeDoc(ctxt->document);
7525 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007526 return (NULL);
7527 }
7528 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007529 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007530 xmlFreeDoc(ctxt->document);
7531 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007532 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007533 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007534
7535 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007536 * Check the ref/defines links
7537 */
7538 /*
7539 * try to preprocess interleaves
7540 */
7541 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007542 xmlHashScan(ctxt->interleaves,
7543 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007544 }
7545
7546 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007547 * if there was a parsing error return NULL
7548 */
7549 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007550 xmlRelaxNGFree(ret);
7551 ctxt->document = NULL;
7552 xmlFreeDoc(doc);
7553 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007554 }
7555
7556 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007557 * try to compile (parts of) the schemas
7558 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007559 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7560 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007561 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007562
Daniel Veillard4c004142003-10-07 11:33:24 +00007563 def = xmlRelaxNGNewDefine(ctxt, NULL);
7564 if (def != NULL) {
7565 def->type = XML_RELAXNG_START;
7566 def->content = ret->topgrammar->start;
7567 ret->topgrammar->start = def;
7568 }
7569 }
7570 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007571 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007572
7573 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007574 * Transfer the pointer for cleanup at the schema level.
7575 */
7576 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007577 ctxt->document = NULL;
7578 ret->documents = ctxt->documents;
7579 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007580
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007581 ret->includes = ctxt->includes;
7582 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007583 ret->defNr = ctxt->defNr;
7584 ret->defTab = ctxt->defTab;
7585 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007586 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007587 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007588
7589 return (ret);
7590}
Daniel Veillard4c004142003-10-07 11:33:24 +00007591
Daniel Veillard6eadf632003-01-23 18:29:16 +00007592/**
7593 * xmlRelaxNGSetParserErrors:
7594 * @ctxt: a Relax-NG validation context
7595 * @err: the error callback
7596 * @warn: the warning callback
7597 * @ctx: contextual data for the callbacks
7598 *
7599 * Set the callback functions used to handle errors for a validation context
7600 */
7601void
7602xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007603 xmlRelaxNGValidityErrorFunc err,
7604 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7605{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007606 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007607 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007608 ctxt->error = err;
7609 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007610 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007611 ctxt->userData = ctx;
7612}
Daniel Veillard409a8142003-07-18 15:16:57 +00007613
7614/**
7615 * xmlRelaxNGGetParserErrors:
7616 * @ctxt: a Relax-NG validation context
7617 * @err: the error callback result
7618 * @warn: the warning callback result
7619 * @ctx: contextual data for the callbacks result
7620 *
7621 * Get the callback information used to handle errors for a validation context
7622 *
7623 * Returns -1 in case of failure, 0 otherwise.
7624 */
7625int
7626xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007627 xmlRelaxNGValidityErrorFunc * err,
7628 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7629{
Daniel Veillard409a8142003-07-18 15:16:57 +00007630 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007631 return (-1);
7632 if (err != NULL)
7633 *err = ctxt->error;
7634 if (warn != NULL)
7635 *warn = ctxt->warning;
7636 if (ctx != NULL)
7637 *ctx = ctxt->userData;
7638 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007639}
7640
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007641/**
7642 * xmlRelaxNGSetParserStructuredErrors:
7643 * @ctxt: a Relax-NG parser context
7644 * @serror: the error callback
7645 * @ctx: contextual data for the callbacks
7646 *
7647 * Set the callback functions used to handle errors for a parsing context
7648 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007649void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007650xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007651 xmlStructuredErrorFunc serror,
7652 void *ctx)
7653{
7654 if (ctxt == NULL)
7655 return;
7656 ctxt->serror = serror;
7657 ctxt->error = NULL;
7658 ctxt->warning = NULL;
7659 ctxt->userData = ctx;
7660}
7661
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007662#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007663
Daniel Veillard6eadf632003-01-23 18:29:16 +00007664/************************************************************************
7665 * *
7666 * Dump back a compiled form *
7667 * *
7668 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007669static void xmlRelaxNGDumpDefine(FILE * output,
7670 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007671
7672/**
7673 * xmlRelaxNGDumpDefines:
7674 * @output: the file output
7675 * @defines: a list of define structures
7676 *
7677 * Dump a RelaxNG structure back
7678 */
7679static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007680xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7681{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007682 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007683 xmlRelaxNGDumpDefine(output, defines);
7684 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007685 }
7686}
7687
7688/**
7689 * xmlRelaxNGDumpDefine:
7690 * @output: the file output
7691 * @define: a define structure
7692 *
7693 * Dump a RelaxNG structure back
7694 */
7695static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007696xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7697{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007698 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007699 return;
7700 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007701 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007702 fprintf(output, "<empty/>\n");
7703 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007704 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007705 fprintf(output, "<notAllowed/>\n");
7706 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007707 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007708 fprintf(output, "<text/>\n");
7709 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007710 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007711 fprintf(output, "<element>\n");
7712 if (define->name != NULL) {
7713 fprintf(output, "<name");
7714 if (define->ns != NULL)
7715 fprintf(output, " ns=\"%s\"", define->ns);
7716 fprintf(output, ">%s</name>\n", define->name);
7717 }
7718 xmlRelaxNGDumpDefines(output, define->attrs);
7719 xmlRelaxNGDumpDefines(output, define->content);
7720 fprintf(output, "</element>\n");
7721 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007722 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007723 fprintf(output, "<list>\n");
7724 xmlRelaxNGDumpDefines(output, define->content);
7725 fprintf(output, "</list>\n");
7726 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007727 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007728 fprintf(output, "<oneOrMore>\n");
7729 xmlRelaxNGDumpDefines(output, define->content);
7730 fprintf(output, "</oneOrMore>\n");
7731 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007732 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007733 fprintf(output, "<zeroOrMore>\n");
7734 xmlRelaxNGDumpDefines(output, define->content);
7735 fprintf(output, "</zeroOrMore>\n");
7736 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007737 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007738 fprintf(output, "<choice>\n");
7739 xmlRelaxNGDumpDefines(output, define->content);
7740 fprintf(output, "</choice>\n");
7741 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007742 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007743 fprintf(output, "<group>\n");
7744 xmlRelaxNGDumpDefines(output, define->content);
7745 fprintf(output, "</group>\n");
7746 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007747 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007748 fprintf(output, "<interleave>\n");
7749 xmlRelaxNGDumpDefines(output, define->content);
7750 fprintf(output, "</interleave>\n");
7751 break;
7752 case XML_RELAXNG_OPTIONAL:
7753 fprintf(output, "<optional>\n");
7754 xmlRelaxNGDumpDefines(output, define->content);
7755 fprintf(output, "</optional>\n");
7756 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007757 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007758 fprintf(output, "<attribute>\n");
7759 xmlRelaxNGDumpDefines(output, define->content);
7760 fprintf(output, "</attribute>\n");
7761 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007762 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007763 fprintf(output, "<define");
7764 if (define->name != NULL)
7765 fprintf(output, " name=\"%s\"", define->name);
7766 fprintf(output, ">\n");
7767 xmlRelaxNGDumpDefines(output, define->content);
7768 fprintf(output, "</define>\n");
7769 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007770 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007771 fprintf(output, "<ref");
7772 if (define->name != NULL)
7773 fprintf(output, " name=\"%s\"", define->name);
7774 fprintf(output, ">\n");
7775 xmlRelaxNGDumpDefines(output, define->content);
7776 fprintf(output, "</ref>\n");
7777 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007778 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007779 fprintf(output, "<parentRef");
7780 if (define->name != NULL)
7781 fprintf(output, " name=\"%s\"", define->name);
7782 fprintf(output, ">\n");
7783 xmlRelaxNGDumpDefines(output, define->content);
7784 fprintf(output, "</parentRef>\n");
7785 break;
7786 case XML_RELAXNG_EXTERNALREF:
7787 fprintf(output, "<externalRef>");
7788 xmlRelaxNGDumpDefines(output, define->content);
7789 fprintf(output, "</externalRef>\n");
7790 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007791 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007792 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007793 TODO break;
7794 case XML_RELAXNG_START:
7795 case XML_RELAXNG_EXCEPT:
7796 case XML_RELAXNG_PARAM:
7797 TODO break;
7798 case XML_RELAXNG_NOOP:
7799 xmlRelaxNGDumpDefines(output, define->content);
7800 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007801 }
7802}
Daniel Veillard4c004142003-10-07 11:33:24 +00007803
Daniel Veillard6eadf632003-01-23 18:29:16 +00007804/**
7805 * xmlRelaxNGDumpGrammar:
7806 * @output: the file output
7807 * @grammar: a grammar structure
7808 * @top: is this a top grammar
7809 *
7810 * Dump a RelaxNG structure back
7811 */
7812static void
7813xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7814{
7815 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007816 return;
7817
Daniel Veillard6eadf632003-01-23 18:29:16 +00007818 fprintf(output, "<grammar");
7819 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007820 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7821 switch (grammar->combine) {
7822 case XML_RELAXNG_COMBINE_UNDEFINED:
7823 break;
7824 case XML_RELAXNG_COMBINE_CHOICE:
7825 fprintf(output, " combine=\"choice\"");
7826 break;
7827 case XML_RELAXNG_COMBINE_INTERLEAVE:
7828 fprintf(output, " combine=\"interleave\"");
7829 break;
7830 default:
7831 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007832 }
7833 fprintf(output, ">\n");
7834 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007835 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007836 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007837 fprintf(output, "<start>\n");
7838 xmlRelaxNGDumpDefine(output, grammar->start);
7839 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007840 }
7841 /* TODO ? Dump the defines ? */
7842 fprintf(output, "</grammar>\n");
7843}
7844
7845/**
7846 * xmlRelaxNGDump:
7847 * @output: the file output
7848 * @schema: a schema structure
7849 *
7850 * Dump a RelaxNG structure back
7851 */
7852void
7853xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7854{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007855 if (output == NULL)
7856 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007857 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007858 fprintf(output, "RelaxNG empty or failed to compile\n");
7859 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007860 }
7861 fprintf(output, "RelaxNG: ");
7862 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007863 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007864 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007865 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007866 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007867 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007868 }
7869 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007870 fprintf(output, "RelaxNG has no top grammar\n");
7871 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007872 }
7873 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7874}
7875
Daniel Veillardfebcca42003-02-16 15:44:18 +00007876/**
7877 * xmlRelaxNGDumpTree:
7878 * @output: the file output
7879 * @schema: a schema structure
7880 *
7881 * Dump the transformed RelaxNG tree.
7882 */
7883void
7884xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7885{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007886 if (output == NULL)
7887 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007888 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007889 fprintf(output, "RelaxNG empty or failed to compile\n");
7890 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007891 }
7892 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007893 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007894 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007895 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007896 }
7897}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007898#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007899
Daniel Veillard6eadf632003-01-23 18:29:16 +00007900/************************************************************************
7901 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007902 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007903 * *
7904 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007905static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7906 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007907
7908/**
7909 * xmlRelaxNGValidateCompiledCallback:
7910 * @exec: the regular expression instance
7911 * @token: the token which matched
7912 * @transdata: callback data, the define for the subelement if available
7913 @ @inputdata: callback data, the Relax NG validation context
7914 *
7915 * Handle the callback and if needed validate the element children.
7916 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007917static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007918xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007919 const xmlChar * token,
7920 void *transdata, void *inputdata)
7921{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007922 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7923 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7924 int ret;
7925
7926#ifdef DEBUG_COMPILE
7927 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007928 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007929#endif
7930 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007931 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007932 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007933 }
7934 if (define == NULL) {
7935 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007936 return;
7937 fprintf(stderr, "callback on %s missing define\n", token);
7938 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7939 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7940 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007941 }
7942 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007943 fprintf(stderr, "callback on %s missing info\n", token);
7944 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7945 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7946 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007947 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007948 fprintf(stderr, "callback on %s define is not element\n", token);
7949 if (ctxt->errNo == XML_RELAXNG_OK)
7950 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7951 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007952 }
7953 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007954 if (ret != 0)
7955 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007956}
7957
7958/**
7959 * xmlRelaxNGValidateCompiledContent:
7960 * @ctxt: the RelaxNG validation context
7961 * @regexp: the regular expression as compiled
7962 * @content: list of children to test against the regexp
7963 *
7964 * Validate the content model of an element or start using the regexp
7965 *
7966 * Returns 0 in case of success, -1 in case of error.
7967 */
7968static int
7969xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007970 xmlRegexpPtr regexp, xmlNodePtr content)
7971{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007972 xmlRegExecCtxtPtr exec;
7973 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007974 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007975 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007976
7977 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007978 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007979 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007980 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007981 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007982 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007983 cur = content;
7984 while (cur != NULL) {
7985 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007986 switch (cur->type) {
7987 case XML_TEXT_NODE:
7988 case XML_CDATA_SECTION_NODE:
7989 if (xmlIsBlankNode(cur))
7990 break;
7991 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7992 if (ret < 0) {
7993 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7994 cur->parent->name);
7995 }
7996 break;
7997 case XML_ELEMENT_NODE:
7998 if (cur->ns != NULL) {
7999 ret = xmlRegExecPushString2(exec, cur->name,
8000 cur->ns->href, ctxt);
8001 } else {
8002 ret = xmlRegExecPushString(exec, cur->name, ctxt);
8003 }
8004 if (ret < 0) {
8005 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
8006 }
8007 break;
8008 default:
8009 break;
8010 }
8011 if (ret < 0)
8012 break;
8013 /*
8014 * Switch to next element
8015 */
8016 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008017 }
8018 ret = xmlRegExecPushString(exec, NULL, NULL);
8019 if (ret == 1) {
8020 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008021 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008022 } else if (ret == 0) {
8023 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008024 * TODO: get some of the names needed to exit the current state of exec
8025 */
8026 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8027 ret = -1;
8028 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8029 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008030 } else {
8031 ret = -1;
8032 }
8033 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008034 /*
8035 * There might be content model errors outside of the pure
8036 * regexp validation, e.g. for attribute values.
8037 */
8038 if ((ret == 0) && (ctxt->perr != 0)) {
8039 ret = ctxt->perr;
8040 }
8041 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008042 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008043}
8044
8045/************************************************************************
8046 * *
8047 * Progressive validation of when possible *
8048 * *
8049 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008050static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8051 xmlRelaxNGDefinePtr defines);
8052static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008053 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008054static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008055
8056/**
8057 * xmlRelaxNGElemPush:
8058 * @ctxt: the validation context
8059 * @exec: the regexp runtime for the new content model
8060 *
8061 * Push a new regexp for the current node content model on the stack
8062 *
8063 * Returns 0 in case of success and -1 in case of error.
8064 */
8065static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008066xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8067{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008068 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008069 ctxt->elemMax = 10;
8070 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8071 sizeof
8072 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008073 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008074 xmlRngVErrMemory(ctxt, "validating\n");
8075 return (-1);
8076 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008077 }
8078 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008079 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008080 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008081 ctxt->elemMax *
8082 sizeof
8083 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008084 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008085 xmlRngVErrMemory(ctxt, "validating\n");
8086 return (-1);
8087 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008088 }
8089 ctxt->elemTab[ctxt->elemNr++] = exec;
8090 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008091 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008092}
8093
8094/**
8095 * xmlRelaxNGElemPop:
8096 * @ctxt: the validation context
8097 *
8098 * Pop the regexp of the current node content model from the stack
8099 *
8100 * Returns the exec or NULL if empty
8101 */
8102static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008103xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8104{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008105 xmlRegExecCtxtPtr ret;
8106
Daniel Veillard4c004142003-10-07 11:33:24 +00008107 if (ctxt->elemNr <= 0)
8108 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008109 ctxt->elemNr--;
8110 ret = ctxt->elemTab[ctxt->elemNr];
8111 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008112 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008113 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8114 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008115 ctxt->elem = NULL;
8116 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008117}
8118
8119/**
8120 * xmlRelaxNGValidateProgressiveCallback:
8121 * @exec: the regular expression instance
8122 * @token: the token which matched
8123 * @transdata: callback data, the define for the subelement if available
8124 @ @inputdata: callback data, the Relax NG validation context
8125 *
8126 * Handle the callback and if needed validate the element children.
8127 * some of the in/out informations are passed via the context in @inputdata.
8128 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008129static void
8130xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8131 ATTRIBUTE_UNUSED,
8132 const xmlChar * token,
8133 void *transdata, void *inputdata)
8134{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008135 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8136 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008137 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008138 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008139 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008140
8141#ifdef DEBUG_PROGRESSIVE
8142 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008143 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008144#endif
8145 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008146 fprintf(stderr, "callback on %s missing context\n", token);
8147 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008148 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008149 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008150 ctxt->pstate = 1;
8151 if (define == NULL) {
8152 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008153 return;
8154 fprintf(stderr, "callback on %s missing define\n", token);
8155 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8156 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8157 ctxt->pstate = -1;
8158 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008159 }
8160 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008161 fprintf(stderr, "callback on %s missing info\n", token);
8162 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8163 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8164 ctxt->pstate = -1;
8165 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008166 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008167 fprintf(stderr, "callback on %s define is not element\n", token);
8168 if (ctxt->errNo == XML_RELAXNG_OK)
8169 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8170 ctxt->pstate = -1;
8171 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008172 }
8173 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008174 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8175 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8176 xmlRelaxNGDumpValidError(ctxt);
8177 ctxt->pstate = -1;
8178 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008179 }
8180 if (define->contModel == NULL) {
8181 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008182 * this node cannot be validated in a streamable fashion
8183 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008184#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008185 xmlGenericError(xmlGenericErrorContext,
8186 "Element '%s' validation is not streamable\n",
8187 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008188#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008189 ctxt->pstate = 0;
8190 ctxt->pdef = define;
8191 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008192 }
8193 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008194 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008195 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008196 ctxt->pstate = -1;
8197 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008198 }
8199 xmlRelaxNGElemPush(ctxt, exec);
8200
8201 /*
8202 * Validate the attributes part of the content.
8203 */
8204 state = xmlRelaxNGNewValidState(ctxt, node);
8205 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008206 ctxt->pstate = -1;
8207 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008208 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008209 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008210 ctxt->state = state;
8211 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008212 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8213 if (ret != 0) {
8214 ctxt->pstate = -1;
8215 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8216 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008217 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008218 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008219 ctxt->state->seq = NULL;
8220 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8221 if (ret != 0) {
8222 ctxt->pstate = -1;
8223 }
8224 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008225 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008226 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008227
8228 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008229
Daniel Veillard4c004142003-10-07 11:33:24 +00008230 for (i = 0; i < ctxt->states->nbState; i++) {
8231 state = ctxt->states->tabState[i];
8232 ctxt->state = state;
8233 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008234
Daniel Veillard4c004142003-10-07 11:33:24 +00008235 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8236 tmp = 0;
8237 break;
8238 }
8239 }
8240 if (tmp != 0) {
8241 /*
8242 * validation error, log the message for the "best" one
8243 */
8244 ctxt->flags |= FLAGS_IGNORABLE;
8245 xmlRelaxNGLogBestError(ctxt);
8246 }
8247 for (i = 0; i < ctxt->states->nbState; i++) {
8248 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8249 }
8250 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8251 ctxt->states = NULL;
8252 if ((ret == 0) && (tmp == -1))
8253 ctxt->pstate = -1;
8254 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008255 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008256 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008257 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8258 xmlRelaxNGDumpValidError(ctxt);
8259 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008260 }
8261 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008262}
8263
8264/**
8265 * xmlRelaxNGValidatePushElement:
8266 * @ctxt: the validation context
8267 * @doc: a document instance
8268 * @elem: an element instance
8269 *
8270 * Push a new element start on the RelaxNG validation stack.
8271 *
8272 * returns 1 if no validation problem was found or 0 if validating the
8273 * element requires a full node, and -1 in case of error.
8274 */
8275int
Daniel Veillard33300b42003-04-17 09:09:19 +00008276xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8277 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008278 xmlNodePtr elem)
8279{
8280 int ret = 1;
8281
8282 if ((ctxt == NULL) || (elem == NULL))
8283 return (-1);
8284
8285#ifdef DEBUG_PROGRESSIVE
8286 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8287#endif
8288 if (ctxt->elem == 0) {
8289 xmlRelaxNGPtr schema;
8290 xmlRelaxNGGrammarPtr grammar;
8291 xmlRegExecCtxtPtr exec;
8292 xmlRelaxNGDefinePtr define;
8293
8294 schema = ctxt->schema;
8295 if (schema == NULL) {
8296 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8297 return (-1);
8298 }
8299 grammar = schema->topgrammar;
8300 if ((grammar == NULL) || (grammar->start == NULL)) {
8301 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8302 return (-1);
8303 }
8304 define = grammar->start;
8305 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008306 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008307 return (0);
8308 }
8309 exec = xmlRegNewExecCtxt(define->contModel,
8310 xmlRelaxNGValidateProgressiveCallback,
8311 ctxt);
8312 if (exec == NULL) {
8313 return (-1);
8314 }
8315 xmlRelaxNGElemPush(ctxt, exec);
8316 }
8317 ctxt->pnode = elem;
8318 ctxt->pstate = 0;
8319 if (elem->ns != NULL) {
8320 ret =
8321 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8322 ctxt);
8323 } else {
8324 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8325 }
8326 if (ret < 0) {
8327 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8328 } else {
8329 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008330 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008331 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008332 ret = -1;
8333 else
8334 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008335 }
8336#ifdef DEBUG_PROGRESSIVE
8337 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008338 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8339 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008340#endif
8341 return (ret);
8342}
8343
8344/**
8345 * xmlRelaxNGValidatePushCData:
8346 * @ctxt: the RelaxNG validation context
8347 * @data: some character data read
8348 * @len: the lenght of the data
8349 *
8350 * check the CData parsed for validation in the current stack
8351 *
8352 * returns 1 if no validation problem was found or -1 otherwise
8353 */
8354int
8355xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008356 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008357{
8358 int ret = 1;
8359
8360 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8361 return (-1);
8362
8363#ifdef DEBUG_PROGRESSIVE
8364 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8365#endif
8366
8367 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008368 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008369 break;
8370 data++;
8371 }
8372 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008373 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008374
8375 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8376 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008377 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008378#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008379 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008380#endif
8381
Daniel Veillard4c004142003-10-07 11:33:24 +00008382 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008383 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008384 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008385}
8386
8387/**
8388 * xmlRelaxNGValidatePopElement:
8389 * @ctxt: the RelaxNG validation context
8390 * @doc: a document instance
8391 * @elem: an element instance
8392 *
8393 * Pop the element end from the RelaxNG validation stack.
8394 *
8395 * returns 1 if no validation problem was found or 0 otherwise
8396 */
8397int
8398xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8399 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008400 xmlNodePtr elem)
8401{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008402 int ret;
8403 xmlRegExecCtxtPtr exec;
8404
Daniel Veillard4c004142003-10-07 11:33:24 +00008405 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8406 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008407#ifdef DEBUG_PROGRESSIVE
8408 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8409#endif
8410 /*
8411 * verify that we reached a terminal state of the content model.
8412 */
8413 exec = xmlRelaxNGElemPop(ctxt);
8414 ret = xmlRegExecPushString(exec, NULL, NULL);
8415 if (ret == 0) {
8416 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008417 * TODO: get some of the names needed to exit the current state of exec
8418 */
8419 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8420 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008421 } else if (ret < 0) {
8422 ret = -1;
8423 } else {
8424 ret = 1;
8425 }
8426 xmlRegFreeExecCtxt(exec);
8427#ifdef DEBUG_PROGRESSIVE
8428 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008429 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8430 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008431#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008432 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008433}
8434
8435/**
8436 * xmlRelaxNGValidateFullElement:
8437 * @ctxt: the validation context
8438 * @doc: a document instance
8439 * @elem: an element instance
8440 *
8441 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8442 * 0 and the content of the node has been expanded.
8443 *
8444 * returns 1 if no validation problem was found or -1 in case of error.
8445 */
8446int
Daniel Veillard33300b42003-04-17 09:09:19 +00008447xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8448 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008449 xmlNodePtr elem)
8450{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008451 int ret;
8452 xmlRelaxNGValidStatePtr state;
8453
Daniel Veillard4c004142003-10-07 11:33:24 +00008454 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8455 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008456#ifdef DEBUG_PROGRESSIVE
8457 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8458#endif
8459 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8460 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008461 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008462 }
8463 state->seq = elem;
8464 ctxt->state = state;
8465 ctxt->errNo = XML_RELAXNG_OK;
8466 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008467 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8468 ret = -1;
8469 else
8470 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008471 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008472 ctxt->state = NULL;
8473#ifdef DEBUG_PROGRESSIVE
8474 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008475 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8476 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008477#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008478 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008479}
8480
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008481/************************************************************************
8482 * *
8483 * Generic interpreted validation implementation *
8484 * *
8485 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008486static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8487 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008488
8489/**
8490 * xmlRelaxNGSkipIgnored:
8491 * @ctxt: a schema validation context
8492 * @node: the top node.
8493 *
8494 * Skip ignorable nodes in that context
8495 *
8496 * Returns the new sibling or NULL in case of error.
8497 */
8498static xmlNodePtr
8499xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008500 xmlNodePtr node)
8501{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008502 /*
8503 * TODO complete and handle entities
8504 */
8505 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008506 ((node->type == XML_COMMENT_NODE) ||
8507 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008508 (node->type == XML_XINCLUDE_START) ||
8509 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008510 (((node->type == XML_TEXT_NODE) ||
8511 (node->type == XML_CDATA_SECTION_NODE)) &&
8512 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8513 (IS_BLANK_NODE(node)))))) {
8514 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008515 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008516 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008517}
8518
8519/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008520 * xmlRelaxNGNormalize:
8521 * @ctxt: a schema validation context
8522 * @str: the string to normalize
8523 *
8524 * Implements the normalizeWhiteSpace( s ) function from
8525 * section 6.2.9 of the spec
8526 *
8527 * Returns the new string or NULL in case of error.
8528 */
8529static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008530xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8531{
Daniel Veillardedc91922003-01-26 00:52:04 +00008532 xmlChar *ret, *p;
8533 const xmlChar *tmp;
8534 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008535
Daniel Veillardedc91922003-01-26 00:52:04 +00008536 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008537 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008538 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008539 while (*tmp != 0)
8540 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008541 len = tmp - str;
8542
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008543 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008544 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008545 xmlRngVErrMemory(ctxt, "validating\n");
8546 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008547 }
8548 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008549 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008550 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008551 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008552 if (IS_BLANK_CH(*str)) {
8553 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008554 str++;
8555 if (*str == 0)
8556 break;
8557 *p++ = ' ';
8558 } else
8559 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008560 }
8561 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008562 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008563}
8564
8565/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008566 * xmlRelaxNGValidateDatatype:
8567 * @ctxt: a Relax-NG validation context
8568 * @value: the string value
8569 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008570 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008571 *
8572 * Validate the given value against the dataype
8573 *
8574 * Returns 0 if the validation succeeded or an error code.
8575 */
8576static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008577xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8578 const xmlChar * value,
8579 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8580{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008581 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008582 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008583 void *result = NULL;
8584 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008585
8586 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008587 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008588 }
8589 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008590 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008591 if ((define->attrs != NULL) &&
8592 (define->attrs->type == XML_RELAXNG_PARAM)) {
8593 ret =
8594 lib->check(lib->data, define->name, value, &result, node);
8595 } else {
8596 ret = lib->check(lib->data, define->name, value, NULL, node);
8597 }
8598 } else
8599 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008600 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008601 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8602 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8603 lib->freef(lib->data, result);
8604 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008605 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008606 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008607 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008608 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008609 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008610 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8611 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008612 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008613 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008614 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008615 if (lib->facet != NULL) {
8616 tmp = lib->facet(lib->data, define->name, cur->name,
8617 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008618 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008619 ret = -1;
8620 }
8621 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008622 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008623 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008624 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008625
Daniel Veillard4c004142003-10-07 11:33:24 +00008626 oldvalue = ctxt->state->value;
8627 oldendvalue = ctxt->state->endvalue;
8628 ctxt->state->value = (xmlChar *) value;
8629 ctxt->state->endvalue = NULL;
8630 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8631 ctxt->state->value = (xmlChar *) oldvalue;
8632 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008633 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008634 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008635 lib->freef(lib->data, result);
8636 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008637}
8638
8639/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008640 * xmlRelaxNGNextValue:
8641 * @ctxt: a Relax-NG validation context
8642 *
8643 * Skip to the next value when validating within a list
8644 *
8645 * Returns 0 if the operation succeeded or an error code.
8646 */
8647static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008648xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8649{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008650 xmlChar *cur;
8651
8652 cur = ctxt->state->value;
8653 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008654 ctxt->state->value = NULL;
8655 ctxt->state->endvalue = NULL;
8656 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008657 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008658 while (*cur != 0)
8659 cur++;
8660 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8661 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008662 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008663 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008664 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008665 ctxt->state->value = cur;
8666 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008667}
8668
8669/**
8670 * xmlRelaxNGValidateValueList:
8671 * @ctxt: a Relax-NG validation context
8672 * @defines: the list of definitions to verify
8673 *
8674 * Validate the given set of definitions for the current value
8675 *
8676 * Returns 0 if the validation succeeded or an error code.
8677 */
8678static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008679xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8680 xmlRelaxNGDefinePtr defines)
8681{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008682 int ret = 0;
8683
8684 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008685 ret = xmlRelaxNGValidateValue(ctxt, defines);
8686 if (ret != 0)
8687 break;
8688 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008689 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008690 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008691}
8692
8693/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008694 * xmlRelaxNGValidateValue:
8695 * @ctxt: a Relax-NG validation context
8696 * @define: the definition to verify
8697 *
8698 * Validate the given definition for the current value
8699 *
8700 * Returns 0 if the validation succeeded or an error code.
8701 */
8702static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008703xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8704 xmlRelaxNGDefinePtr define)
8705{
Daniel Veillardedc91922003-01-26 00:52:04 +00008706 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008707 xmlChar *value;
8708
8709 value = ctxt->state->value;
8710 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008711 case XML_RELAXNG_EMPTY:{
8712 if ((value != NULL) && (value[0] != 0)) {
8713 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008714
William M. Brack76e95df2003-10-18 16:20:14 +00008715 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008716 idx++;
8717 if (value[idx] != 0)
8718 ret = -1;
8719 }
8720 break;
8721 }
8722 case XML_RELAXNG_TEXT:
8723 break;
8724 case XML_RELAXNG_VALUE:{
8725 if (!xmlStrEqual(value, define->value)) {
8726 if (define->name != NULL) {
8727 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008728
Daniel Veillard4c004142003-10-07 11:33:24 +00008729 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8730 if ((lib != NULL) && (lib->comp != NULL)) {
8731 ret = lib->comp(lib->data, define->name,
8732 define->value, define->node,
8733 (void *) define->attrs,
8734 value, ctxt->state->node);
8735 } else
8736 ret = -1;
8737 if (ret < 0) {
8738 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8739 define->name);
8740 return (-1);
8741 } else if (ret == 1) {
8742 ret = 0;
8743 } else {
8744 ret = -1;
8745 }
8746 } else {
8747 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008748
Daniel Veillard4c004142003-10-07 11:33:24 +00008749 /*
8750 * TODO: trivial optimizations are possible by
8751 * computing at compile-time
8752 */
8753 nval = xmlRelaxNGNormalize(ctxt, define->value);
8754 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008755
Daniel Veillard4c004142003-10-07 11:33:24 +00008756 if ((nval == NULL) || (nvalue == NULL) ||
8757 (!xmlStrEqual(nval, nvalue)))
8758 ret = -1;
8759 if (nval != NULL)
8760 xmlFree(nval);
8761 if (nvalue != NULL)
8762 xmlFree(nvalue);
8763 }
8764 }
8765 if (ret == 0)
8766 xmlRelaxNGNextValue(ctxt);
8767 break;
8768 }
8769 case XML_RELAXNG_DATATYPE:{
8770 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8771 ctxt->state->seq);
8772 if (ret == 0)
8773 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008774
Daniel Veillard4c004142003-10-07 11:33:24 +00008775 break;
8776 }
8777 case XML_RELAXNG_CHOICE:{
8778 xmlRelaxNGDefinePtr list = define->content;
8779 xmlChar *oldvalue;
8780
8781 oldflags = ctxt->flags;
8782 ctxt->flags |= FLAGS_IGNORABLE;
8783
8784 oldvalue = ctxt->state->value;
8785 while (list != NULL) {
8786 ret = xmlRelaxNGValidateValue(ctxt, list);
8787 if (ret == 0) {
8788 break;
8789 }
8790 ctxt->state->value = oldvalue;
8791 list = list->next;
8792 }
8793 ctxt->flags = oldflags;
8794 if (ret != 0) {
8795 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8796 xmlRelaxNGDumpValidError(ctxt);
8797 } else {
8798 if (ctxt->errNr > 0)
8799 xmlRelaxNGPopErrors(ctxt, 0);
8800 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008801 break;
8802 }
8803 case XML_RELAXNG_LIST:{
8804 xmlRelaxNGDefinePtr list = define->content;
8805 xmlChar *oldvalue, *oldend, *val, *cur;
8806
Daniel Veillard416589a2003-02-17 17:25:42 +00008807#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008808 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008809#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008810
Daniel Veillard4c004142003-10-07 11:33:24 +00008811 oldvalue = ctxt->state->value;
8812 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008813
Daniel Veillard4c004142003-10-07 11:33:24 +00008814 val = xmlStrdup(oldvalue);
8815 if (val == NULL) {
8816 val = xmlStrdup(BAD_CAST "");
8817 }
8818 if (val == NULL) {
8819 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8820 return (-1);
8821 }
8822 cur = val;
8823 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008824 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008825 *cur = 0;
8826 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008827#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008828 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008829#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008830 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008831 *cur++ = 0;
8832 } else
8833 cur++;
8834 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008835#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008836 xmlGenericError(xmlGenericErrorContext,
8837 "list value: '%s' found %d items\n",
8838 oldvalue, nb_values);
8839 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008840#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008841 ctxt->state->endvalue = cur;
8842 cur = val;
8843 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8844 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008845
Daniel Veillard4c004142003-10-07 11:33:24 +00008846 ctxt->state->value = cur;
8847
8848 while (list != NULL) {
8849 if (ctxt->state->value == ctxt->state->endvalue)
8850 ctxt->state->value = NULL;
8851 ret = xmlRelaxNGValidateValue(ctxt, list);
8852 if (ret != 0) {
8853#ifdef DEBUG_LIST
8854 xmlGenericError(xmlGenericErrorContext,
8855 "Failed to validate value: '%s' with %d rule\n",
8856 ctxt->state->value, nb_values);
8857#endif
8858 break;
8859 }
8860#ifdef DEBUG_LIST
8861 nb_values++;
8862#endif
8863 list = list->next;
8864 }
8865
8866 if ((ret == 0) && (ctxt->state->value != NULL) &&
8867 (ctxt->state->value != ctxt->state->endvalue)) {
8868 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8869 ctxt->state->value);
8870 ret = -1;
8871 }
8872 xmlFree(val);
8873 ctxt->state->value = oldvalue;
8874 ctxt->state->endvalue = oldend;
8875 break;
8876 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008877 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008878 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8879 if (ret != 0) {
8880 break;
8881 }
8882 /* no break on purpose */
8883 case XML_RELAXNG_ZEROORMORE:{
8884 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008885
Daniel Veillard4c004142003-10-07 11:33:24 +00008886 oldflags = ctxt->flags;
8887 ctxt->flags |= FLAGS_IGNORABLE;
8888 cur = ctxt->state->value;
8889 temp = NULL;
8890 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8891 (temp != cur)) {
8892 temp = cur;
8893 ret =
8894 xmlRelaxNGValidateValueList(ctxt, define->content);
8895 if (ret != 0) {
8896 ctxt->state->value = temp;
8897 ret = 0;
8898 break;
8899 }
8900 cur = ctxt->state->value;
8901 }
8902 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008903 if (ctxt->errNr > 0)
8904 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008905 break;
8906 }
8907 case XML_RELAXNG_EXCEPT:{
8908 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008909
Daniel Veillard4c004142003-10-07 11:33:24 +00008910 list = define->content;
8911 while (list != NULL) {
8912 ret = xmlRelaxNGValidateValue(ctxt, list);
8913 if (ret == 0) {
8914 ret = -1;
8915 break;
8916 } else
8917 ret = 0;
8918 list = list->next;
8919 }
8920 break;
8921 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008922 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008923 case XML_RELAXNG_GROUP:{
8924 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008925
Daniel Veillard4c004142003-10-07 11:33:24 +00008926 list = define->content;
8927 while (list != NULL) {
8928 ret = xmlRelaxNGValidateValue(ctxt, list);
8929 if (ret != 0) {
8930 ret = -1;
8931 break;
8932 } else
8933 ret = 0;
8934 list = list->next;
8935 }
8936 break;
8937 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008938 case XML_RELAXNG_REF:
8939 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008940 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008941 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8942 ret = -1;
8943 } else {
8944 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8945 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008946 break;
8947 default:
8948 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008949 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008950 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008951}
8952
8953/**
8954 * xmlRelaxNGValidateValueContent:
8955 * @ctxt: a Relax-NG validation context
8956 * @defines: the list of definitions to verify
8957 *
8958 * Validate the given definitions for the current value
8959 *
8960 * Returns 0 if the validation succeeded or an error code.
8961 */
8962static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008963xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8964 xmlRelaxNGDefinePtr defines)
8965{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008966 int ret = 0;
8967
8968 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008969 ret = xmlRelaxNGValidateValue(ctxt, defines);
8970 if (ret != 0)
8971 break;
8972 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008973 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008974 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008975}
8976
8977/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008978 * xmlRelaxNGAttributeMatch:
8979 * @ctxt: a Relax-NG validation context
8980 * @define: the definition to check
8981 * @prop: the attribute
8982 *
8983 * Check if the attribute matches the definition nameClass
8984 *
8985 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8986 */
8987static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008988xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8989 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8990{
Daniel Veillardfd573f12003-03-16 17:52:32 +00008991 int ret;
8992
8993 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008994 if (!xmlStrEqual(define->name, prop->name))
8995 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008996 }
8997 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008998 if (define->ns[0] == 0) {
8999 if (prop->ns != NULL)
9000 return (0);
9001 } else {
9002 if ((prop->ns == NULL) ||
9003 (!xmlStrEqual(define->ns, prop->ns->href)))
9004 return (0);
9005 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009006 }
9007 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009008 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009009 define = define->nameClass;
9010 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009011 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009012
Daniel Veillard4c004142003-10-07 11:33:24 +00009013 list = define->content;
9014 while (list != NULL) {
9015 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9016 if (ret == 1)
9017 return (0);
9018 if (ret < 0)
9019 return (ret);
9020 list = list->next;
9021 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009022 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009023 TODO}
9024 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009025}
9026
9027/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009028 * xmlRelaxNGValidateAttribute:
9029 * @ctxt: a Relax-NG validation context
9030 * @define: the definition to verify
9031 *
9032 * Validate the given attribute definition for that node
9033 *
9034 * Returns 0 if the validation succeeded or an error code.
9035 */
9036static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009037xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9038 xmlRelaxNGDefinePtr define)
9039{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009040 int ret = 0, i;
9041 xmlChar *value, *oldvalue;
9042 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009043 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009044
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009045 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009046 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009047 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009048 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9049 tmp = ctxt->state->attrs[i];
9050 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9051 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9052 (tmp->ns == NULL)) ||
9053 ((tmp->ns != NULL) &&
9054 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9055 prop = tmp;
9056 break;
9057 }
9058 }
9059 }
9060 if (prop != NULL) {
9061 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9062 oldvalue = ctxt->state->value;
9063 oldseq = ctxt->state->seq;
9064 ctxt->state->seq = (xmlNodePtr) prop;
9065 ctxt->state->value = value;
9066 ctxt->state->endvalue = NULL;
9067 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9068 if (ctxt->state->value != NULL)
9069 value = ctxt->state->value;
9070 if (value != NULL)
9071 xmlFree(value);
9072 ctxt->state->value = oldvalue;
9073 ctxt->state->seq = oldseq;
9074 if (ret == 0) {
9075 /*
9076 * flag the attribute as processed
9077 */
9078 ctxt->state->attrs[i] = NULL;
9079 ctxt->state->nbAttrLeft--;
9080 }
9081 } else {
9082 ret = -1;
9083 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009084#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009085 xmlGenericError(xmlGenericErrorContext,
9086 "xmlRelaxNGValidateAttribute(%s): %d\n",
9087 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009088#endif
9089 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009090 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9091 tmp = ctxt->state->attrs[i];
9092 if ((tmp != NULL) &&
9093 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9094 prop = tmp;
9095 break;
9096 }
9097 }
9098 if (prop != NULL) {
9099 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9100 oldvalue = ctxt->state->value;
9101 oldseq = ctxt->state->seq;
9102 ctxt->state->seq = (xmlNodePtr) prop;
9103 ctxt->state->value = value;
9104 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9105 if (ctxt->state->value != NULL)
9106 value = ctxt->state->value;
9107 if (value != NULL)
9108 xmlFree(value);
9109 ctxt->state->value = oldvalue;
9110 ctxt->state->seq = oldseq;
9111 if (ret == 0) {
9112 /*
9113 * flag the attribute as processed
9114 */
9115 ctxt->state->attrs[i] = NULL;
9116 ctxt->state->nbAttrLeft--;
9117 }
9118 } else {
9119 ret = -1;
9120 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009121#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009122 if (define->ns != NULL) {
9123 xmlGenericError(xmlGenericErrorContext,
9124 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9125 define->ns, ret);
9126 } else {
9127 xmlGenericError(xmlGenericErrorContext,
9128 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9129 ret);
9130 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009131#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009132 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009133
9134 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009135}
9136
9137/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009138 * xmlRelaxNGValidateAttributeList:
9139 * @ctxt: a Relax-NG validation context
9140 * @define: the list of definition to verify
9141 *
9142 * Validate the given node against the list of attribute definitions
9143 *
9144 * Returns 0 if the validation succeeded or an error code.
9145 */
9146static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009147xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9148 xmlRelaxNGDefinePtr defines)
9149{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009150 int ret = 0, res;
9151 int needmore = 0;
9152 xmlRelaxNGDefinePtr cur;
9153
9154 cur = defines;
9155 while (cur != NULL) {
9156 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009157 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9158 ret = -1;
9159 } else
9160 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009161 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009162 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009163 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009164 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009165 cur = defines;
9166 while (cur != NULL) {
9167 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009168 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9169 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9170 if (res < 0)
9171 ret = -1;
9172 } else {
9173 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9174 return (-1);
9175 }
9176 if (res == -1) /* continues on -2 */
9177 break;
9178 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009179 cur = cur->next;
9180 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009181
9182 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009183}
9184
9185/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009186 * xmlRelaxNGNodeMatchesList:
9187 * @node: the node
9188 * @list: a NULL terminated array of definitions
9189 *
9190 * Check if a node can be matched by one of the definitions
9191 *
9192 * Returns 1 if matches 0 otherwise
9193 */
9194static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009195xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9196{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009197 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009198 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009199
9200 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009201 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009202
9203 cur = list[i++];
9204 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009205 if ((node->type == XML_ELEMENT_NODE) &&
9206 (cur->type == XML_RELAXNG_ELEMENT)) {
9207 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9208 if (tmp == 1)
9209 return (1);
9210 } else if (((node->type == XML_TEXT_NODE) ||
9211 (node->type == XML_CDATA_SECTION_NODE)) &&
9212 (cur->type == XML_RELAXNG_TEXT)) {
9213 return (1);
9214 }
9215 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009216 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009217 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009218}
9219
9220/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009221 * xmlRelaxNGValidateInterleave:
9222 * @ctxt: a Relax-NG validation context
9223 * @define: the definition to verify
9224 *
9225 * Validate an interleave definition for a node.
9226 *
9227 * Returns 0 if the validation succeeded or an error code.
9228 */
9229static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009230xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9231 xmlRelaxNGDefinePtr define)
9232{
William M. Brack779af002003-08-01 15:55:39 +00009233 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009234 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009235 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009236
9237 xmlRelaxNGValidStatePtr oldstate;
9238 xmlRelaxNGPartitionPtr partitions;
9239 xmlRelaxNGInterleaveGroupPtr group = NULL;
9240 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9241 xmlNodePtr *list = NULL, *lasts = NULL;
9242
9243 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009244 partitions = (xmlRelaxNGPartitionPtr) define->data;
9245 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009246 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009247 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9248 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009249 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009250 /*
9251 * Optimizations for MIXED
9252 */
9253 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009254 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009255 ctxt->flags |= FLAGS_MIXED_CONTENT;
9256 if (nbgroups == 2) {
9257 /*
9258 * this is a pure <mixed> case
9259 */
9260 if (ctxt->state != NULL)
9261 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9262 ctxt->state->seq);
9263 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9264 ret = xmlRelaxNGValidateDefinition(ctxt,
9265 partitions->groups[1]->
9266 rule);
9267 else
9268 ret = xmlRelaxNGValidateDefinition(ctxt,
9269 partitions->groups[0]->
9270 rule);
9271 if (ret == 0) {
9272 if (ctxt->state != NULL)
9273 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9274 ctxt->state->
9275 seq);
9276 }
9277 ctxt->flags = oldflags;
9278 return (ret);
9279 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009280 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009281
9282 /*
9283 * Build arrays to store the first and last node of the chain
9284 * pertaining to each group
9285 */
9286 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9287 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009288 xmlRngVErrMemory(ctxt, "validating\n");
9289 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009290 }
9291 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9292 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9293 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009294 xmlRngVErrMemory(ctxt, "validating\n");
9295 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009296 }
9297 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9298
9299 /*
9300 * Walk the sequence of children finding the right group and
9301 * sorting them in sequences.
9302 */
9303 cur = ctxt->state->seq;
9304 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9305 start = cur;
9306 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009307 ctxt->state->seq = cur;
9308 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009309 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009310 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009311
Daniel Veillard4c004142003-10-07 11:33:24 +00009312 if ((cur->type == XML_TEXT_NODE) ||
9313 (cur->type == XML_CDATA_SECTION_NODE)) {
9314 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9315 NULL);
9316 } else if (cur->type == XML_ELEMENT_NODE) {
9317 if (cur->ns != NULL) {
9318 tmp = xmlHashLookup2(partitions->triage, cur->name,
9319 cur->ns->href);
9320 if (tmp == NULL)
9321 tmp = xmlHashLookup2(partitions->triage,
9322 BAD_CAST "#any",
9323 cur->ns->href);
9324 } else
9325 tmp =
9326 xmlHashLookup2(partitions->triage, cur->name,
9327 NULL);
9328 if (tmp == NULL)
9329 tmp =
9330 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9331 NULL);
9332 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009333
Daniel Veillard4c004142003-10-07 11:33:24 +00009334 if (tmp == NULL) {
9335 i = nbgroups;
9336 } else {
9337 i = ((long) tmp) - 1;
9338 if (partitions->flags & IS_NEEDCHECK) {
9339 group = partitions->groups[i];
9340 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9341 i = nbgroups;
9342 }
9343 }
9344 } else {
9345 for (i = 0; i < nbgroups; i++) {
9346 group = partitions->groups[i];
9347 if (group == NULL)
9348 continue;
9349 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9350 break;
9351 }
9352 }
9353 /*
9354 * We break as soon as an element not matched is found
9355 */
9356 if (i >= nbgroups) {
9357 break;
9358 }
9359 if (lasts[i] != NULL) {
9360 lasts[i]->next = cur;
9361 lasts[i] = cur;
9362 } else {
9363 list[i] = cur;
9364 lasts[i] = cur;
9365 }
9366 if (cur->next != NULL)
9367 lastchg = cur->next;
9368 else
9369 lastchg = cur;
9370 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009371 }
9372 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009373 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9374 ret = -1;
9375 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009376 }
9377 lastelem = cur;
9378 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009379 for (i = 0; i < nbgroups; i++) {
9380 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9381 group = partitions->groups[i];
9382 if (lasts[i] != NULL) {
9383 last = lasts[i]->next;
9384 lasts[i]->next = NULL;
9385 }
9386 ctxt->state->seq = list[i];
9387 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9388 if (ret != 0)
9389 break;
9390 if (ctxt->state != NULL) {
9391 cur = ctxt->state->seq;
9392 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9393 xmlRelaxNGFreeValidState(ctxt, oldstate);
9394 oldstate = ctxt->state;
9395 ctxt->state = NULL;
9396 if (cur != NULL) {
9397 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9398 ret = -1;
9399 ctxt->state = oldstate;
9400 goto done;
9401 }
9402 } else if (ctxt->states != NULL) {
9403 int j;
9404 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009405 int best = -1;
9406 int lowattr = -1;
9407
9408 /*
9409 * PBM: what happen if there is attributes checks in the interleaves
9410 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009411
Daniel Veillard4c004142003-10-07 11:33:24 +00009412 for (j = 0; j < ctxt->states->nbState; j++) {
9413 cur = ctxt->states->tabState[j]->seq;
9414 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9415 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009416 if (found == 0) {
9417 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9418 best = j;
9419 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009420 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009421 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9422 /* try to keep the latest one to mach old heuristic */
9423 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9424 best = j;
9425 }
9426 if (lowattr == 0)
9427 break;
9428 } else if (found == 0) {
9429 if (lowattr == -1) {
9430 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9431 best = j;
9432 } else
9433 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9434 /* try to keep the latest one to mach old heuristic */
9435 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9436 best = j;
9437 }
9438 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009439 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009440 /*
9441 * BIG PBM: here we pick only one restarting point :-(
9442 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009443 if (ctxt->states->nbState > 0) {
9444 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009445 if (best != -1) {
9446 oldstate = ctxt->states->tabState[best];
9447 ctxt->states->tabState[best] = NULL;
9448 } else {
9449 oldstate =
9450 ctxt->states->tabState[ctxt->states->nbState - 1];
9451 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009452 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009453 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009454 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009455 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009456 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9457 }
9458 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9459 ctxt->states = NULL;
9460 if (found == 0) {
Daniel Veillard76d36452009-09-07 11:19:33 +02009461 if (cur == NULL) {
Rob Richards848e5cf2009-09-09 12:13:58 -04009462 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, "noname");
Daniel Veillard76d36452009-09-07 11:19:33 +02009463 } else {
9464 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9465 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009466 ret = -1;
9467 ctxt->state = oldstate;
9468 goto done;
9469 }
9470 } else {
9471 ret = -1;
9472 break;
9473 }
9474 if (lasts[i] != NULL) {
9475 lasts[i]->next = last;
9476 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009477 }
9478 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009479 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009480 ctxt->state = oldstate;
9481 ctxt->state->seq = lastelem;
9482 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009483 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9484 ret = -1;
9485 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009486 }
9487
Daniel Veillard4c004142003-10-07 11:33:24 +00009488 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009489 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009490 /*
9491 * builds the next links chain from the prev one
9492 */
9493 cur = lastchg;
9494 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009495 if ((cur == start) || (cur->prev == NULL))
9496 break;
9497 cur->prev->next = cur;
9498 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009499 }
9500 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009501 if (ctxt->errNr > errNr)
9502 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009503 }
9504
9505 xmlFree(list);
9506 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009507 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009508}
9509
9510/**
9511 * xmlRelaxNGValidateDefinitionList:
9512 * @ctxt: a Relax-NG validation context
9513 * @define: the list of definition to verify
9514 *
9515 * Validate the given node content against the (list) of definitions
9516 *
9517 * Returns 0 if the validation succeeded or an error code.
9518 */
9519static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009520xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9521 xmlRelaxNGDefinePtr defines)
9522{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009523 int ret = 0, res;
9524
9525
Daniel Veillard952379b2003-03-17 15:37:12 +00009526 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009527 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9528 BAD_CAST "NULL definition list");
9529 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009530 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009531 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009532 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9533 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9534 if (res < 0)
9535 ret = -1;
9536 } else {
9537 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9538 return (-1);
9539 }
9540 if (res == -1) /* continues on -2 */
9541 break;
9542 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009543 }
9544
Daniel Veillard4c004142003-10-07 11:33:24 +00009545 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009546}
9547
9548/**
9549 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009550 * @ctxt: a Relax-NG validation context
9551 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009552 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009553 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009554 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009555 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009556 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009557 */
9558static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009559xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9560 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9561{
Daniel Veillard580ced82003-03-21 21:22:48 +00009562 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009563
Daniel Veillardfd573f12003-03-16 17:52:32 +00009564 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009565 if (!xmlStrEqual(elem->name, define->name)) {
9566 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9567 return (0);
9568 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009569 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009570 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009571 if (elem->ns == NULL) {
9572 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9573 return (0);
9574 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9575 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9576 elem->name, define->ns);
9577 return (0);
9578 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009579 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009580 (define->name == NULL)) {
9581 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9582 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009583 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009584 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9585 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009586 }
9587
9588 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009589 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009590
9591 define = define->nameClass;
9592 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009593 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009594
Daniel Veillard4c004142003-10-07 11:33:24 +00009595 if (ctxt != NULL) {
9596 oldflags = ctxt->flags;
9597 ctxt->flags |= FLAGS_IGNORABLE;
9598 }
9599
9600 list = define->content;
9601 while (list != NULL) {
9602 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9603 if (ret == 1) {
9604 if (ctxt != NULL)
9605 ctxt->flags = oldflags;
9606 return (0);
9607 }
9608 if (ret < 0) {
9609 if (ctxt != NULL)
9610 ctxt->flags = oldflags;
9611 return (ret);
9612 }
9613 list = list->next;
9614 }
9615 ret = 1;
9616 if (ctxt != NULL) {
9617 ctxt->flags = oldflags;
9618 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009619 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009620 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009621
Daniel Veillard4c004142003-10-07 11:33:24 +00009622 if (ctxt != NULL) {
9623 oldflags = ctxt->flags;
9624 ctxt->flags |= FLAGS_IGNORABLE;
9625 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009626
Daniel Veillard4c004142003-10-07 11:33:24 +00009627 list = define->nameClass;
9628 while (list != NULL) {
9629 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9630 if (ret == 1) {
9631 if (ctxt != NULL)
9632 ctxt->flags = oldflags;
9633 return (1);
9634 }
9635 if (ret < 0) {
9636 if (ctxt != NULL)
9637 ctxt->flags = oldflags;
9638 return (ret);
9639 }
9640 list = list->next;
9641 }
9642 if (ctxt != NULL) {
9643 if (ret != 0) {
9644 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9645 xmlRelaxNGDumpValidError(ctxt);
9646 } else {
9647 if (ctxt->errNr > 0)
9648 xmlRelaxNGPopErrors(ctxt, 0);
9649 }
9650 }
9651 ret = 0;
9652 if (ctxt != NULL) {
9653 ctxt->flags = oldflags;
9654 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009655 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009656 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009657 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009658 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009659}
9660
9661/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009662 * xmlRelaxNGBestState:
9663 * @ctxt: a Relax-NG validation context
9664 *
9665 * Find the "best" state in the ctxt->states list of states to report
9666 * errors about. I.e. a state with no element left in the child list
9667 * or the one with the less attributes left.
9668 * This is called only if a falidation error was detected
9669 *
9670 * Returns the index of the "best" state or -1 in case of error
9671 */
9672static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009673xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9674{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009675 xmlRelaxNGValidStatePtr state;
9676 int i, tmp;
9677 int best = -1;
9678 int value = 1000000;
9679
9680 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9681 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009682 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009683
Daniel Veillard4c004142003-10-07 11:33:24 +00009684 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009685 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009686 if (state == NULL)
9687 continue;
9688 if (state->seq != NULL) {
9689 if ((best == -1) || (value > 100000)) {
9690 value = 100000;
9691 best = i;
9692 }
9693 } else {
9694 tmp = state->nbAttrLeft;
9695 if ((best == -1) || (value > tmp)) {
9696 value = tmp;
9697 best = i;
9698 }
9699 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009700 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009701 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009702}
9703
9704/**
9705 * xmlRelaxNGLogBestError:
9706 * @ctxt: a Relax-NG validation context
9707 *
9708 * Find the "best" state in the ctxt->states list of states to report
9709 * errors about and log it.
9710 */
9711static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009712xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9713{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009714 int best;
9715
9716 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9717 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009718 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009719
9720 best = xmlRelaxNGBestState(ctxt);
9721 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009722 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009723
Daniel Veillard4c004142003-10-07 11:33:24 +00009724 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009725 }
9726}
9727
9728/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009729 * xmlRelaxNGValidateElementEnd:
9730 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009731 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009732 *
9733 * Validate the end of the element, implements check that
9734 * there is nothing left not consumed in the element content
9735 * or in the attribute list.
9736 *
9737 * Returns 0 if the validation succeeded or an error code.
9738 */
9739static int
William M. Brack272693c2003-11-14 16:20:34 +00009740xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009741{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009742 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009743 xmlRelaxNGValidStatePtr state;
9744
9745 state = ctxt->state;
9746 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009747 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9748 if (state->seq != 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_EXTRACONTENT,
9751 state->node->name, state->seq->name);
9752 }
9753 return (-1);
9754 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009755 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009756 for (i = 0; i < state->nbAttrs; i++) {
9757 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009758 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009759 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9760 state->attrs[i]->name, state->node->name);
9761 }
9762 return (-1 - i);
9763 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009764 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009765 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009766}
9767
9768/**
9769 * xmlRelaxNGValidateState:
9770 * @ctxt: a Relax-NG validation context
9771 * @define: the definition to verify
9772 *
9773 * Validate the current state against the definition
9774 *
9775 * Returns 0 if the validation succeeded or an error code.
9776 */
9777static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009778xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9779 xmlRelaxNGDefinePtr define)
9780{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009781 xmlNodePtr node;
9782 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009783 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009784
9785 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009786 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9787 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009788 }
9789
9790 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009791 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009792 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009793 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009794 }
9795#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009796 for (i = 0; i < ctxt->depth; i++)
9797 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009798 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009799 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009800 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009801 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009802 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009803 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009804 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009805 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009806#endif
9807 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009808 switch (define->type) {
9809 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009810 node = xmlRelaxNGSkipIgnored(ctxt, node);
9811 ret = 0;
9812 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009813 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009814 ret = -1;
9815 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009816 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009817 while ((node != NULL) &&
9818 ((node->type == XML_TEXT_NODE) ||
9819 (node->type == XML_COMMENT_NODE) ||
9820 (node->type == XML_PI_NODE) ||
9821 (node->type == XML_CDATA_SECTION_NODE)))
9822 node = node->next;
9823 ctxt->state->seq = node;
9824 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009825 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009826 errNr = ctxt->errNr;
9827 node = xmlRelaxNGSkipIgnored(ctxt, node);
9828 if (node == NULL) {
9829 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9830 ret = -1;
9831 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9832 xmlRelaxNGDumpValidError(ctxt);
9833 break;
9834 }
9835 if (node->type != XML_ELEMENT_NODE) {
9836 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9837 ret = -1;
9838 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9839 xmlRelaxNGDumpValidError(ctxt);
9840 break;
9841 }
9842 /*
9843 * This node was already validated successfully against
9844 * this definition.
9845 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009846 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009847 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9848 if (ctxt->errNr > errNr)
9849 xmlRelaxNGPopErrors(ctxt, errNr);
9850 if (ctxt->errNr != 0) {
9851 while ((ctxt->err != NULL) &&
9852 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9853 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9854 ||
9855 ((ctxt->err->err ==
9856 XML_RELAXNG_ERR_ELEMEXTRANS)
9857 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9858 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9859 || (ctxt->err->err ==
9860 XML_RELAXNG_ERR_NOTELEM)))
9861 xmlRelaxNGValidErrorPop(ctxt);
9862 }
9863 break;
9864 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009865
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009866 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9867 if (ret <= 0) {
9868 ret = -1;
9869 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9870 xmlRelaxNGDumpValidError(ctxt);
9871 break;
9872 }
9873 ret = 0;
9874 if (ctxt->errNr != 0) {
9875 if (ctxt->errNr > errNr)
9876 xmlRelaxNGPopErrors(ctxt, errNr);
9877 while ((ctxt->err != NULL) &&
9878 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9879 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9880 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9881 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9882 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9883 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9884 xmlRelaxNGValidErrorPop(ctxt);
9885 }
9886 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009887
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009888 oldflags = ctxt->flags;
9889 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9890 ctxt->flags -= FLAGS_MIXED_CONTENT;
9891 }
9892 state = xmlRelaxNGNewValidState(ctxt, node);
9893 if (state == NULL) {
9894 ret = -1;
9895 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9896 xmlRelaxNGDumpValidError(ctxt);
9897 break;
9898 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009899
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009900 oldstate = ctxt->state;
9901 ctxt->state = state;
9902 if (define->attrs != NULL) {
9903 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9904 if (tmp != 0) {
9905 ret = -1;
9906 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9907 }
9908 }
9909 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009910 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9911 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9912 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009913
Daniel Veillard4c004142003-10-07 11:33:24 +00009914 nstate = xmlRelaxNGNewValidState(ctxt, node);
9915 ctxt->state = nstate;
9916 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009917
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009918 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9919 define->contModel,
9920 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009921 nseq = ctxt->state->seq;
9922 ctxt->state = tmpstate;
9923 ctxt->states = tmpstates;
9924 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009925
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009926#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009927 xmlGenericError(xmlGenericErrorContext,
9928 "Validating content of '%s' : %d\n",
9929 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009930#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009931 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009932 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009933
9934 if (ctxt->states != NULL) {
9935 tmp = -1;
9936
Daniel Veillardce192eb2003-04-16 15:58:05 +00009937 for (i = 0; i < ctxt->states->nbState; i++) {
9938 state = ctxt->states->tabState[i];
9939 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009940 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009941
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009942 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009943 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00009944 break;
9945 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009946 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009947 if (tmp != 0) {
9948 /*
9949 * validation error, log the message for the "best" one
9950 */
9951 ctxt->flags |= FLAGS_IGNORABLE;
9952 xmlRelaxNGLogBestError(ctxt);
9953 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009954 for (i = 0; i < ctxt->states->nbState; i++) {
9955 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009956 ctxt->states->
9957 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009958 }
9959 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9960 ctxt->flags = oldflags;
9961 ctxt->states = NULL;
9962 if ((ret == 0) && (tmp == -1))
9963 ret = -1;
9964 } else {
9965 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +00009966 if (ctxt->state != NULL)
9967 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009968 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009969 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009970 xmlRelaxNGFreeValidState(ctxt, state);
9971 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009972 } else {
9973 if (define->content != NULL) {
9974 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00009975 define->
9976 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009977 if (tmp != 0) {
9978 ret = -1;
9979 if (ctxt->state == NULL) {
9980 ctxt->state = oldstate;
9981 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9982 node->name);
9983 ctxt->state = NULL;
9984 } else {
9985 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9986 node->name);
9987 }
9988
9989 }
9990 }
9991 if (ctxt->states != NULL) {
9992 tmp = -1;
9993
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009994 for (i = 0; i < ctxt->states->nbState; i++) {
9995 state = ctxt->states->tabState[i];
9996 ctxt->state = state;
9997
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009998 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009999 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010000 break;
10001 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010002 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010003 if (tmp != 0) {
10004 /*
10005 * validation error, log the message for the "best" one
10006 */
10007 ctxt->flags |= FLAGS_IGNORABLE;
10008 xmlRelaxNGLogBestError(ctxt);
10009 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010010 for (i = 0; i < ctxt->states->nbState; i++) {
10011 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010012 ctxt->states->tabState[i]);
10013 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010014 }
10015 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10016 ctxt->flags = oldflags;
10017 ctxt->states = NULL;
10018 if ((ret == 0) && (tmp == -1))
10019 ret = -1;
10020 } else {
10021 state = ctxt->state;
10022 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010023 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010024 xmlRelaxNGFreeValidState(ctxt, state);
10025 }
10026 }
10027 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +000010028 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010029 }
10030 ctxt->flags = oldflags;
10031 ctxt->state = oldstate;
10032 if (oldstate != NULL)
10033 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10034 if (ret != 0) {
10035 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10036 xmlRelaxNGDumpValidError(ctxt);
10037 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010038#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010039 } else {
10040 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010041#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010042 }
10043 } else {
10044 if (ctxt->errNr > errNr)
10045 xmlRelaxNGPopErrors(ctxt, errNr);
10046 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010047
10048#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010049 xmlGenericError(xmlGenericErrorContext,
10050 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10051 node->name, ret);
10052 if (oldstate == NULL)
10053 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10054 else if (oldstate->seq == NULL)
10055 xmlGenericError(xmlGenericErrorContext, ": done\n");
10056 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10057 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10058 oldstate->seq->name);
10059 else
10060 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10061 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010062#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010063 break;
10064 case XML_RELAXNG_OPTIONAL:{
10065 errNr = ctxt->errNr;
10066 oldflags = ctxt->flags;
10067 ctxt->flags |= FLAGS_IGNORABLE;
10068 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10069 ret =
10070 xmlRelaxNGValidateDefinitionList(ctxt,
10071 define->content);
10072 if (ret != 0) {
10073 if (ctxt->state != NULL)
10074 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10075 ctxt->state = oldstate;
10076 ctxt->flags = oldflags;
10077 ret = 0;
10078 if (ctxt->errNr > errNr)
10079 xmlRelaxNGPopErrors(ctxt, errNr);
10080 break;
10081 }
10082 if (ctxt->states != NULL) {
10083 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10084 } else {
10085 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10086 if (ctxt->states == NULL) {
10087 xmlRelaxNGFreeValidState(ctxt, oldstate);
10088 ctxt->flags = oldflags;
10089 ret = -1;
10090 if (ctxt->errNr > errNr)
10091 xmlRelaxNGPopErrors(ctxt, errNr);
10092 break;
10093 }
10094 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10095 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10096 ctxt->state = NULL;
10097 }
10098 ctxt->flags = oldflags;
10099 ret = 0;
10100 if (ctxt->errNr > errNr)
10101 xmlRelaxNGPopErrors(ctxt, errNr);
10102 break;
10103 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010104 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010105 errNr = ctxt->errNr;
10106 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10107 if (ret != 0) {
10108 break;
10109 }
10110 if (ctxt->errNr > errNr)
10111 xmlRelaxNGPopErrors(ctxt, errNr);
10112 /* no break on purpose */
10113 case XML_RELAXNG_ZEROORMORE:{
10114 int progress;
10115 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10116 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010117
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010118 errNr = ctxt->errNr;
10119 res = xmlRelaxNGNewStates(ctxt, 1);
10120 if (res == NULL) {
10121 ret = -1;
10122 break;
10123 }
10124 /*
10125 * All the input states are also exit states
10126 */
10127 if (ctxt->state != NULL) {
10128 xmlRelaxNGAddStates(ctxt, res,
10129 xmlRelaxNGCopyValidState(ctxt,
10130 ctxt->
10131 state));
10132 } else {
10133 for (j = 0; j < ctxt->states->nbState; j++) {
10134 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010135 xmlRelaxNGCopyValidState(ctxt,
10136 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010137 }
10138 }
10139 oldflags = ctxt->flags;
10140 ctxt->flags |= FLAGS_IGNORABLE;
10141 do {
10142 progress = 0;
10143 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010144
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010145 if (ctxt->states != NULL) {
10146 states = ctxt->states;
10147 for (i = 0; i < states->nbState; i++) {
10148 ctxt->state = states->tabState[i];
10149 ctxt->states = NULL;
10150 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10151 define->
10152 content);
10153 if (ret == 0) {
10154 if (ctxt->state != NULL) {
10155 tmp = xmlRelaxNGAddStates(ctxt, res,
10156 ctxt->state);
10157 ctxt->state = NULL;
10158 if (tmp == 1)
10159 progress = 1;
10160 } else if (ctxt->states != NULL) {
10161 for (j = 0; j < ctxt->states->nbState;
10162 j++) {
10163 tmp =
10164 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010165 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010166 if (tmp == 1)
10167 progress = 1;
10168 }
10169 xmlRelaxNGFreeStates(ctxt,
10170 ctxt->states);
10171 ctxt->states = NULL;
10172 }
10173 } else {
10174 if (ctxt->state != NULL) {
10175 xmlRelaxNGFreeValidState(ctxt,
10176 ctxt->state);
10177 ctxt->state = NULL;
10178 }
10179 }
10180 }
10181 } else {
10182 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10183 define->
10184 content);
10185 if (ret != 0) {
10186 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10187 ctxt->state = NULL;
10188 } else {
10189 base = res->nbState;
10190 if (ctxt->state != NULL) {
10191 tmp = xmlRelaxNGAddStates(ctxt, res,
10192 ctxt->state);
10193 ctxt->state = NULL;
10194 if (tmp == 1)
10195 progress = 1;
10196 } else if (ctxt->states != NULL) {
10197 for (j = 0; j < ctxt->states->nbState; j++) {
10198 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010199 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010200 if (tmp == 1)
10201 progress = 1;
10202 }
10203 if (states == NULL) {
10204 states = ctxt->states;
10205 } else {
10206 xmlRelaxNGFreeStates(ctxt,
10207 ctxt->states);
10208 }
10209 ctxt->states = NULL;
10210 }
10211 }
10212 }
10213 if (progress) {
10214 /*
10215 * Collect all the new nodes added at that step
10216 * and make them the new node set
10217 */
10218 if (res->nbState - base == 1) {
10219 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10220 res->
10221 tabState
10222 [base]);
10223 } else {
10224 if (states == NULL) {
10225 xmlRelaxNGNewStates(ctxt,
10226 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010227 states = ctxt->states;
10228 if (states == NULL) {
10229 progress = 0;
10230 break;
10231 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010232 }
10233 states->nbState = 0;
10234 for (i = base; i < res->nbState; i++)
10235 xmlRelaxNGAddStates(ctxt, states,
10236 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010237 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010238 ctxt->states = states;
10239 }
10240 }
10241 } while (progress == 1);
10242 if (states != NULL) {
10243 xmlRelaxNGFreeStates(ctxt, states);
10244 }
10245 ctxt->states = res;
10246 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010247#if 0
10248 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010249 * errors may have to be propagated back...
10250 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010251 if (ctxt->errNr > errNr)
10252 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010253#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010254 ret = 0;
10255 break;
10256 }
10257 case XML_RELAXNG_CHOICE:{
10258 xmlRelaxNGDefinePtr list = NULL;
10259 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010260
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010261 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010262
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010263 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010264 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10265 (node != NULL)) {
10266 /*
10267 * node == NULL can't be optimized since IS_TRIABLE
10268 * doesn't account for choice which may lead to
10269 * only attributes.
10270 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010271 xmlHashTablePtr triage =
10272 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010273
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010274 /*
10275 * Something we can optimize cleanly there is only one
10276 * possble branch out !
10277 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010278 if ((node->type == XML_TEXT_NODE) ||
10279 (node->type == XML_CDATA_SECTION_NODE)) {
10280 list =
10281 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10282 } else if (node->type == XML_ELEMENT_NODE) {
10283 if (node->ns != NULL) {
10284 list = xmlHashLookup2(triage, node->name,
10285 node->ns->href);
10286 if (list == NULL)
10287 list =
10288 xmlHashLookup2(triage, BAD_CAST "#any",
10289 node->ns->href);
10290 } else
10291 list =
10292 xmlHashLookup2(triage, node->name, NULL);
10293 if (list == NULL)
10294 list =
10295 xmlHashLookup2(triage, BAD_CAST "#any",
10296 NULL);
10297 }
10298 if (list == NULL) {
10299 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010300 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010301 break;
10302 }
10303 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10304 if (ret == 0) {
10305 }
10306 break;
10307 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010308
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010309 list = define->content;
10310 oldflags = ctxt->flags;
10311 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010312
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010313 while (list != NULL) {
10314 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10315 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10316 if (ret == 0) {
10317 if (states == NULL) {
10318 states = xmlRelaxNGNewStates(ctxt, 1);
10319 }
10320 if (ctxt->state != NULL) {
10321 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10322 } else if (ctxt->states != NULL) {
10323 for (i = 0; i < ctxt->states->nbState; i++) {
10324 xmlRelaxNGAddStates(ctxt, states,
10325 ctxt->states->
10326 tabState[i]);
10327 }
10328 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10329 ctxt->states = NULL;
10330 }
10331 } else {
10332 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10333 }
10334 ctxt->state = oldstate;
10335 list = list->next;
10336 }
10337 if (states != NULL) {
10338 xmlRelaxNGFreeValidState(ctxt, oldstate);
10339 ctxt->states = states;
10340 ctxt->state = NULL;
10341 ret = 0;
10342 } else {
10343 ctxt->states = NULL;
10344 }
10345 ctxt->flags = oldflags;
10346 if (ret != 0) {
10347 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10348 xmlRelaxNGDumpValidError(ctxt);
10349 }
10350 } else {
10351 if (ctxt->errNr > errNr)
10352 xmlRelaxNGPopErrors(ctxt, errNr);
10353 }
10354 break;
10355 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010356 case XML_RELAXNG_DEF:
10357 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010358 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10359 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010360 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010361 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10362 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010363 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010364 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10365 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010366 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010367 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010368 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010369 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010370 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010371 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10372 break;
10373 case XML_RELAXNG_DATATYPE:{
10374 xmlNodePtr child;
10375 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010376
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010377 child = node;
10378 while (child != NULL) {
10379 if (child->type == XML_ELEMENT_NODE) {
10380 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10381 node->parent->name);
10382 ret = -1;
10383 break;
10384 } else if ((child->type == XML_TEXT_NODE) ||
10385 (child->type == XML_CDATA_SECTION_NODE)) {
10386 content = xmlStrcat(content, child->content);
10387 }
10388 /* TODO: handle entities ... */
10389 child = child->next;
10390 }
10391 if (ret == -1) {
10392 if (content != NULL)
10393 xmlFree(content);
10394 break;
10395 }
10396 if (content == NULL) {
10397 content = xmlStrdup(BAD_CAST "");
10398 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010399 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010400 ret = -1;
10401 break;
10402 }
10403 }
10404 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10405 ctxt->state->seq);
10406 if (ret == -1) {
10407 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10408 } else if (ret == 0) {
10409 ctxt->state->seq = NULL;
10410 }
10411 if (content != NULL)
10412 xmlFree(content);
10413 break;
10414 }
10415 case XML_RELAXNG_VALUE:{
10416 xmlChar *content = NULL;
10417 xmlChar *oldvalue;
10418 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010419
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010420 child = node;
10421 while (child != NULL) {
10422 if (child->type == XML_ELEMENT_NODE) {
10423 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10424 node->parent->name);
10425 ret = -1;
10426 break;
10427 } else if ((child->type == XML_TEXT_NODE) ||
10428 (child->type == XML_CDATA_SECTION_NODE)) {
10429 content = xmlStrcat(content, child->content);
10430 }
10431 /* TODO: handle entities ... */
10432 child = child->next;
10433 }
10434 if (ret == -1) {
10435 if (content != NULL)
10436 xmlFree(content);
10437 break;
10438 }
10439 if (content == NULL) {
10440 content = xmlStrdup(BAD_CAST "");
10441 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010442 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010443 ret = -1;
10444 break;
10445 }
10446 }
10447 oldvalue = ctxt->state->value;
10448 ctxt->state->value = content;
10449 ret = xmlRelaxNGValidateValue(ctxt, define);
10450 ctxt->state->value = oldvalue;
10451 if (ret == -1) {
10452 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10453 } else if (ret == 0) {
10454 ctxt->state->seq = NULL;
10455 }
10456 if (content != NULL)
10457 xmlFree(content);
10458 break;
10459 }
10460 case XML_RELAXNG_LIST:{
10461 xmlChar *content;
10462 xmlNodePtr child;
10463 xmlChar *oldvalue, *oldendvalue;
10464 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010465
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010466 /*
10467 * Make sure it's only text nodes
10468 */
10469
10470 content = NULL;
10471 child = node;
10472 while (child != NULL) {
10473 if (child->type == XML_ELEMENT_NODE) {
10474 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10475 node->parent->name);
10476 ret = -1;
10477 break;
10478 } else if ((child->type == XML_TEXT_NODE) ||
10479 (child->type == XML_CDATA_SECTION_NODE)) {
10480 content = xmlStrcat(content, child->content);
10481 }
10482 /* TODO: handle entities ... */
10483 child = child->next;
10484 }
10485 if (ret == -1) {
10486 if (content != NULL)
10487 xmlFree(content);
10488 break;
10489 }
10490 if (content == NULL) {
10491 content = xmlStrdup(BAD_CAST "");
10492 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010493 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010494 ret = -1;
10495 break;
10496 }
10497 }
10498 len = xmlStrlen(content);
10499 oldvalue = ctxt->state->value;
10500 oldendvalue = ctxt->state->endvalue;
10501 ctxt->state->value = content;
10502 ctxt->state->endvalue = content + len;
10503 ret = xmlRelaxNGValidateValue(ctxt, define);
10504 ctxt->state->value = oldvalue;
10505 ctxt->state->endvalue = oldendvalue;
10506 if (ret == -1) {
10507 VALID_ERR(XML_RELAXNG_ERR_LIST);
10508 } else if ((ret == 0) && (node != NULL)) {
10509 ctxt->state->seq = node->next;
10510 }
10511 if (content != NULL)
10512 xmlFree(content);
10513 break;
10514 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010515 case XML_RELAXNG_EXCEPT:
10516 case XML_RELAXNG_PARAM:
10517 TODO ret = -1;
10518 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010519 }
10520 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010521#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010522 for (i = 0; i < ctxt->depth; i++)
10523 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010524 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010525 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010526 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010527 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010528 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010529 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010530 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010531 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010532#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010533 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010534}
10535
10536/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010537 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010538 * @ctxt: a Relax-NG validation context
10539 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010540 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010541 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010542 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010543 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010544 */
10545static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010546xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10547 xmlRelaxNGDefinePtr define)
10548{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010549 xmlRelaxNGStatesPtr states, res;
10550 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010551
Daniel Veillardfd573f12003-03-16 17:52:32 +000010552 /*
10553 * We should NOT have both ctxt->state and ctxt->states
10554 */
10555 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010556 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10557 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010558 }
10559
10560 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010561 if (ctxt->states != NULL) {
10562 ctxt->state = ctxt->states->tabState[0];
10563 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10564 ctxt->states = NULL;
10565 }
10566 ret = xmlRelaxNGValidateState(ctxt, define);
10567 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10568 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10569 ctxt->state = NULL;
10570 }
10571 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10572 ctxt->state = ctxt->states->tabState[0];
10573 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10574 ctxt->states = NULL;
10575 }
10576 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010577 }
10578
10579 states = ctxt->states;
10580 ctxt->states = NULL;
10581 res = NULL;
10582 j = 0;
10583 oldflags = ctxt->flags;
10584 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010585 for (i = 0; i < states->nbState; i++) {
10586 ctxt->state = states->tabState[i];
10587 ctxt->states = NULL;
10588 ret = xmlRelaxNGValidateState(ctxt, define);
10589 /*
10590 * We should NOT have both ctxt->state and ctxt->states
10591 */
10592 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10593 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10594 ctxt->state = NULL;
10595 }
10596 if (ret == 0) {
10597 if (ctxt->states == NULL) {
10598 if (res != NULL) {
10599 /* add the state to the container */
10600 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10601 ctxt->state = NULL;
10602 } else {
10603 /* add the state directly in states */
10604 states->tabState[j++] = ctxt->state;
10605 ctxt->state = NULL;
10606 }
10607 } else {
10608 if (res == NULL) {
10609 /* make it the new container and copy other results */
10610 res = ctxt->states;
10611 ctxt->states = NULL;
10612 for (k = 0; k < j; k++)
10613 xmlRelaxNGAddStates(ctxt, res,
10614 states->tabState[k]);
10615 } else {
10616 /* add all the new results to res and reff the container */
10617 for (k = 0; k < ctxt->states->nbState; k++)
10618 xmlRelaxNGAddStates(ctxt, res,
10619 ctxt->states->tabState[k]);
10620 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10621 ctxt->states = NULL;
10622 }
10623 }
10624 } else {
10625 if (ctxt->state != NULL) {
10626 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10627 ctxt->state = NULL;
10628 } else if (ctxt->states != NULL) {
10629 for (k = 0; k < ctxt->states->nbState; k++)
10630 xmlRelaxNGFreeValidState(ctxt,
10631 ctxt->states->tabState[k]);
10632 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10633 ctxt->states = NULL;
10634 }
10635 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010636 }
10637 ctxt->flags = oldflags;
10638 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010639 xmlRelaxNGFreeStates(ctxt, states);
10640 ctxt->states = res;
10641 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010642 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010643 states->nbState = j;
10644 ctxt->states = states;
10645 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010646 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010647 ctxt->state = states->tabState[0];
10648 xmlRelaxNGFreeStates(ctxt, states);
10649 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010650 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010651 ret = -1;
10652 xmlRelaxNGFreeStates(ctxt, states);
10653 if (ctxt->states != NULL) {
10654 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10655 ctxt->states = NULL;
10656 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010657 }
10658 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010659 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10660 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010661 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010662 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010663}
10664
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010665/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010666 * xmlRelaxNGValidateDocument:
10667 * @ctxt: a Relax-NG validation context
10668 * @doc: the document
10669 *
10670 * Validate the given document
10671 *
10672 * Returns 0 if the validation succeeded or an error code.
10673 */
10674static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010675xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10676{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010677 int ret;
10678 xmlRelaxNGPtr schema;
10679 xmlRelaxNGGrammarPtr grammar;
10680 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010681 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010682
10683 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010684 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010685
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010686 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010687 schema = ctxt->schema;
10688 grammar = schema->topgrammar;
10689 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010690 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10691 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010692 }
10693 state = xmlRelaxNGNewValidState(ctxt, NULL);
10694 ctxt->state = state;
10695 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010696 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010697 state = ctxt->state;
10698 node = state->seq;
10699 node = xmlRelaxNGSkipIgnored(ctxt, node);
10700 if (node != NULL) {
10701 if (ret != -1) {
10702 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10703 ret = -1;
10704 }
10705 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010706 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010707 int i;
10708 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010709
Daniel Veillard4c004142003-10-07 11:33:24 +000010710 for (i = 0; i < ctxt->states->nbState; i++) {
10711 state = ctxt->states->tabState[i];
10712 node = state->seq;
10713 node = xmlRelaxNGSkipIgnored(ctxt, node);
10714 if (node == NULL)
10715 tmp = 0;
10716 xmlRelaxNGFreeValidState(ctxt, state);
10717 }
10718 if (tmp == -1) {
10719 if (ret != -1) {
10720 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10721 ret = -1;
10722 }
10723 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010724 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010725 if (ctxt->state != NULL) {
10726 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010727 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010728 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010729 if (ret != 0)
10730 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010731#ifdef DEBUG
10732 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010733 ctxt->error(ctxt->userData,
10734 "%d Extra error messages left on stack !\n",
10735 ctxt->errNr);
10736 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010737 }
10738#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010739#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010740 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010741 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010742
Daniel Veillard4c004142003-10-07 11:33:24 +000010743 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10744 vctxt.valid = 1;
10745 vctxt.error = ctxt->error;
10746 vctxt.warning = ctxt->warning;
10747 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010748
Daniel Veillard4c004142003-10-07 11:33:24 +000010749 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10750 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010751 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010752#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010753 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010754 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010755
Daniel Veillard4c004142003-10-07 11:33:24 +000010756 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010757}
10758
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010759/**
10760 * xmlRelaxNGCleanPSVI:
10761 * @node: an input element or document
10762 *
10763 * Call this routine to speed up XPath computation on static documents.
10764 * This stamps all the element nodes with the document order
10765 * Like for line information, the order is kept in the element->content
10766 * field, the value stored is actually - the node number (starting at -1)
10767 * to be able to differentiate from line numbers.
10768 *
10769 * Returns the number of elements found in the document or -1 in case
10770 * of error.
10771 */
10772static void
10773xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10774 xmlNodePtr cur;
10775
10776 if ((node == NULL) ||
10777 ((node->type != XML_ELEMENT_NODE) &&
10778 (node->type != XML_DOCUMENT_NODE) &&
10779 (node->type != XML_HTML_DOCUMENT_NODE)))
10780 return;
10781 if (node->type == XML_ELEMENT_NODE)
10782 node->psvi = NULL;
10783
10784 cur = node->children;
10785 while (cur != NULL) {
10786 if (cur->type == XML_ELEMENT_NODE) {
10787 cur->psvi = NULL;
10788 if (cur->children != NULL) {
10789 cur = cur->children;
10790 continue;
10791 }
10792 }
10793 if (cur->next != NULL) {
10794 cur = cur->next;
10795 continue;
10796 }
10797 do {
10798 cur = cur->parent;
10799 if (cur == NULL)
10800 break;
10801 if (cur == node) {
10802 cur = NULL;
10803 break;
10804 }
10805 if (cur->next != NULL) {
10806 cur = cur->next;
10807 break;
10808 }
10809 } while (cur != NULL);
10810 }
10811 return;
10812}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010813/************************************************************************
10814 * *
10815 * Validation interfaces *
10816 * *
10817 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010818
Daniel Veillard6eadf632003-01-23 18:29:16 +000010819/**
10820 * xmlRelaxNGNewValidCtxt:
10821 * @schema: a precompiled XML RelaxNGs
10822 *
10823 * Create an XML RelaxNGs validation context based on the given schema
10824 *
10825 * Returns the validation context or NULL in case of error
10826 */
10827xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010828xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10829{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010830 xmlRelaxNGValidCtxtPtr ret;
10831
10832 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10833 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010834 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010835 return (NULL);
10836 }
10837 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10838 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010839 ret->error = xmlGenericError;
10840 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010841 ret->errNr = 0;
10842 ret->errMax = 0;
10843 ret->err = NULL;
10844 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010845 if (schema != NULL)
10846 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010847 ret->states = NULL;
10848 ret->freeState = NULL;
10849 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010850 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010851 return (ret);
10852}
10853
10854/**
10855 * xmlRelaxNGFreeValidCtxt:
10856 * @ctxt: the schema validation context
10857 *
10858 * Free the resources associated to the schema validation context
10859 */
10860void
Daniel Veillard4c004142003-10-07 11:33:24 +000010861xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10862{
Daniel Veillard798024a2003-03-19 10:36:09 +000010863 int k;
10864
Daniel Veillard6eadf632003-01-23 18:29:16 +000010865 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010866 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010867 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010868 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010869 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010870 for (k = 0; k < ctxt->freeState->nbState; k++) {
10871 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10872 }
10873 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010874 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010875 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010876 for (k = 0; k < ctxt->freeStatesNr; k++) {
10877 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10878 }
10879 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010880 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010881 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010882 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010883 if (ctxt->elemTab != NULL) {
10884 xmlRegExecCtxtPtr exec;
10885
Daniel Veillard4c004142003-10-07 11:33:24 +000010886 exec = xmlRelaxNGElemPop(ctxt);
10887 while (exec != NULL) {
10888 xmlRegFreeExecCtxt(exec);
10889 exec = xmlRelaxNGElemPop(ctxt);
10890 }
10891 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010892 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010893 xmlFree(ctxt);
10894}
10895
10896/**
10897 * xmlRelaxNGSetValidErrors:
10898 * @ctxt: a Relax-NG validation context
10899 * @err: the error function
10900 * @warn: the warning function
10901 * @ctx: the functions context
10902 *
10903 * Set the error and warning callback informations
10904 */
10905void
10906xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010907 xmlRelaxNGValidityErrorFunc err,
10908 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10909{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010910 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010911 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010912 ctxt->error = err;
10913 ctxt->warning = warn;
10914 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010915 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010916}
10917
10918/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010919 * xmlRelaxNGSetValidStructuredErrors:
10920 * @ctxt: a Relax-NG validation context
10921 * @serror: the structured error function
10922 * @ctx: the functions context
10923 *
10924 * Set the structured error callback
10925 */
10926void
10927xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010928 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010929{
10930 if (ctxt == NULL)
10931 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010932 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010933 ctxt->error = NULL;
10934 ctxt->warning = NULL;
10935 ctxt->userData = ctx;
10936}
10937
10938/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010939 * xmlRelaxNGGetValidErrors:
10940 * @ctxt: a Relax-NG validation context
10941 * @err: the error function result
10942 * @warn: the warning function result
10943 * @ctx: the functions context result
10944 *
10945 * Get the error and warning callback informations
10946 *
10947 * Returns -1 in case of error and 0 otherwise
10948 */
10949int
10950xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010951 xmlRelaxNGValidityErrorFunc * err,
10952 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10953{
Daniel Veillard409a8142003-07-18 15:16:57 +000010954 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010955 return (-1);
10956 if (err != NULL)
10957 *err = ctxt->error;
10958 if (warn != NULL)
10959 *warn = ctxt->warning;
10960 if (ctx != NULL)
10961 *ctx = ctxt->userData;
10962 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000010963}
10964
10965/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010966 * xmlRelaxNGValidateDoc:
10967 * @ctxt: a Relax-NG validation context
10968 * @doc: a parsed document tree
10969 *
10970 * Validate a document tree in memory.
10971 *
10972 * Returns 0 if the document is valid, a positive error code
10973 * number otherwise and -1 in case of internal or API error.
10974 */
10975int
Daniel Veillard4c004142003-10-07 11:33:24 +000010976xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10977{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010978 int ret;
10979
10980 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010981 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010982
10983 ctxt->doc = doc;
10984
10985 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010986 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010987 * Remove all left PSVI
10988 */
10989 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
10990
10991 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000010992 * TODO: build error codes
10993 */
10994 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000010995 return (1);
10996 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010997}
10998
Daniel Veillard5d4644e2005-04-01 13:11:58 +000010999#define bottom_relaxng
11000#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000011001#endif /* LIBXML_SCHEMAS_ENABLED */