blob: ad1571885d10d3613bd09aa9d2c8ab367b375e6a [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 Veillard952379b2003-03-17 15:37:12 +000048/* #define DEBUG 1 */
Daniel Veillardc482e262003-02-26 14:48:48 +000049/* #define DEBUG_GRAMMAR 1 */
Daniel Veillard71531f32003-02-05 13:19:53 +000050/* #define DEBUG_CONTENT 1 */
51/* #define DEBUG_TYPE 1 */
52/* #define DEBUG_VALID 1 */
Daniel Veillarde5b110b2003-02-04 14:43:39 +000053/* #define DEBUG_INTERLEAVE 1 */
Daniel Veillard416589a2003-02-17 17:25:42 +000054/* #define DEBUG_LIST 1 */
Daniel Veillard5add8682003-03-10 13:13:58 +000055/* #define DEBUG_INCLUDE */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000056/* #define DEBUG_ERROR 1 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000057/* #define DEBUG_COMPILE 1 */
Daniel Veillardf4e55762003-04-15 23:32:22 +000058/* #define DEBUG_PROGRESSIVE 1 */
Daniel Veillard6eadf632003-01-23 18:29:16 +000059
Daniel Veillard5f1946a2003-03-31 16:38:16 +000060#define MAX_ERROR 5
61
Daniel Veillard6eadf632003-01-23 18:29:16 +000062#define TODO \
63 xmlGenericError(xmlGenericErrorContext, \
64 "Unimplemented block at %s:%d\n", \
65 __FILE__, __LINE__);
66
67typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
68typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
69
70typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
71typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
72
Daniel Veillardd41f4f42003-01-29 21:07:52 +000073typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
74typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
75
Daniel Veillarda9d912d2003-02-01 17:43:10 +000076typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
77typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
78
Daniel Veillard6eadf632003-01-23 18:29:16 +000079typedef enum {
80 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
81 XML_RELAXNG_COMBINE_CHOICE, /* choice */
82 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
83} xmlRelaxNGCombine;
84
Daniel Veillard4c5cf702003-02-21 15:40:34 +000085typedef enum {
86 XML_RELAXNG_CONTENT_ERROR = -1,
87 XML_RELAXNG_CONTENT_EMPTY = 0,
88 XML_RELAXNG_CONTENT_SIMPLE,
89 XML_RELAXNG_CONTENT_COMPLEX
90} xmlRelaxNGContentType;
91
Daniel Veillard6eadf632003-01-23 18:29:16 +000092typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
93typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
94
95struct _xmlRelaxNGGrammar {
96 xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */
97 xmlRelaxNGGrammarPtr children;/* the children grammar if any */
98 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
99 xmlRelaxNGDefinePtr start; /* <start> content */
100 xmlRelaxNGCombine combine; /* the default combine value */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000101 xmlRelaxNGDefinePtr startList;/* list of <start> definitions */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000102 xmlHashTablePtr defs; /* define* */
103 xmlHashTablePtr refs; /* references */
104};
105
106
Daniel Veillard6eadf632003-01-23 18:29:16 +0000107typedef enum {
Daniel Veillard77648bb2003-02-20 15:03:22 +0000108 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000109 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
110 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard144fae12003-02-03 13:17:57 +0000111 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000112 XML_RELAXNG_TEXT, /* textual content */
113 XML_RELAXNG_ELEMENT, /* an element */
114 XML_RELAXNG_DATATYPE, /* extenal data type definition */
Daniel Veillard8fe98712003-02-19 00:19:14 +0000115 XML_RELAXNG_PARAM, /* extenal data type parameter */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000116 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
117 XML_RELAXNG_LIST, /* a list of patterns */
118 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
119 XML_RELAXNG_DEF, /* a definition */
120 XML_RELAXNG_REF, /* reference to a definition */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000121 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
Daniel Veillard419a7682003-02-03 23:22:49 +0000122 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000123 XML_RELAXNG_OPTIONAL, /* optional patterns */
124 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000125 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
126 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
127 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000128 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000129 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000130} xmlRelaxNGType;
131
Daniel Veillard52b48c72003-04-13 19:53:42 +0000132#define IS_NULLABLE (1 << 0)
133#define IS_NOT_NULLABLE (1 << 1)
134#define IS_INDETERMINIST (1 << 2)
135#define IS_MIXED (1 << 3)
136#define IS_TRIABLE (1 << 4)
137#define IS_PROCESSED (1 << 5)
138#define IS_COMPILABLE (1 << 6)
139#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000140
Daniel Veillard6eadf632003-01-23 18:29:16 +0000141struct _xmlRelaxNGDefine {
142 xmlRelaxNGType type; /* the type of definition */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000143 xmlNodePtr node; /* the node in the source */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000144 xmlChar *name; /* the element local name if present */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000145 xmlChar *ns; /* the namespace local name if present */
Daniel Veillardedc91922003-01-26 00:52:04 +0000146 xmlChar *value; /* value when available */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000147 void *data; /* data lib or specific pointer */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000148 xmlRelaxNGDefinePtr content;/* the expected content */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000149 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000150 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000151 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
Daniel Veillard3b2e4e12003-02-03 08:52:58 +0000152 xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000153 xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000154 short depth; /* used for the cycle detection */
Daniel Veillarde063f482003-03-21 16:53:17 +0000155 short dflags; /* define related flags */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000156 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000157};
158
159/**
160 * _xmlRelaxNG:
161 *
162 * A RelaxNGs definition
163 */
164struct _xmlRelaxNG {
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000165 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000166 xmlRelaxNGGrammarPtr topgrammar;
167 xmlDocPtr doc;
168
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000169 int idref; /* requires idref checking */
170
Daniel Veillard6eadf632003-01-23 18:29:16 +0000171 xmlHashTablePtr defs; /* define */
172 xmlHashTablePtr refs; /* references */
Daniel Veillardc482e262003-02-26 14:48:48 +0000173 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
174 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard419a7682003-02-03 23:22:49 +0000175 int defNr; /* number of defines used */
176 xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000177
Daniel Veillard6eadf632003-01-23 18:29:16 +0000178};
179
Daniel Veillard77648bb2003-02-20 15:03:22 +0000180#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
181#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
182#define XML_RELAXNG_IN_LIST (1 << 2)
183#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
184#define XML_RELAXNG_IN_START (1 << 4)
185#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
186#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
187#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000188#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
189#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000190
191struct _xmlRelaxNGParserCtxt {
192 void *userData; /* user specific data block */
193 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
194 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000195 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000196
197 xmlRelaxNGPtr schema; /* The schema in use */
198 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
Daniel Veillard419a7682003-02-03 23:22:49 +0000199 xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000200 int flags; /* parser flags */
201 int nbErrors; /* number of errors at parse time */
202 int nbWarnings; /* number of warnings at parse time */
Daniel Veillard276be4a2003-01-24 01:03:34 +0000203 const xmlChar *define; /* the current define scope */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000204 xmlRelaxNGDefinePtr def; /* the current define */
205
206 int nbInterleaves;
207 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000208
Daniel Veillardc482e262003-02-26 14:48:48 +0000209 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
210 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000211 xmlChar *URL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000212 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000213
Daniel Veillard419a7682003-02-03 23:22:49 +0000214 int defNr; /* number of defines used */
215 int defMax; /* number of defines aloocated */
216 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
217
Daniel Veillard6eadf632003-01-23 18:29:16 +0000218 const char *buffer;
219 int size;
220
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000221 /* the document stack */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000222 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000223 int docNr; /* Depth of the parsing stack */
224 int docMax; /* Max depth of the parsing stack */
225 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000226
227 /* the include stack */
Daniel Veillardd4310742003-02-18 21:12:46 +0000228 xmlRelaxNGIncludePtr inc; /* Current parsed include */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000229 int incNr; /* Depth of the include parsing stack */
230 int incMax; /* Max depth of the parsing stack */
231 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000232
233 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000234
235 /* used to compile content models */
236 xmlAutomataPtr am; /* the automata */
237 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000238};
239
240#define FLAGS_IGNORABLE 1
241#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000242#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000243
244/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000245 * xmlRelaxNGInterleaveGroup:
246 *
247 * A RelaxNGs partition set associated to lists of definitions
248 */
249typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
250typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
251struct _xmlRelaxNGInterleaveGroup {
252 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
253 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000254 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000255};
256
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000257#define IS_DETERMINIST 1
258#define IS_NEEDCHECK 2
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000259/**
260 * xmlRelaxNGPartitions:
261 *
262 * A RelaxNGs partition associated to an interleave group
263 */
264typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
265typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
266struct _xmlRelaxNGPartition {
267 int nbgroups; /* number of groups in the partitions */
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000268 xmlHashTablePtr triage; /* hash table used to direct nodes to the
269 right group when possible */
270 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000271 xmlRelaxNGInterleaveGroupPtr *groups;
272};
273
274/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000275 * xmlRelaxNGValidState:
276 *
277 * A RelaxNGs validation state
278 */
279#define MAX_ATTR 20
280typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
281typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
282struct _xmlRelaxNGValidState {
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000283 xmlNodePtr node; /* the current node */
284 xmlNodePtr seq; /* the sequence of children left to validate */
285 int nbAttrs; /* the number of attributes */
Daniel Veillard798024a2003-03-19 10:36:09 +0000286 int maxAttrs; /* the size of attrs */
Daniel Veillard1ed7f362003-02-03 10:57:45 +0000287 int nbAttrLeft; /* the number of attributes left to validate */
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000288 xmlChar *value; /* the value when operating on string */
289 xmlChar *endvalue; /* the end value when operating on string */
Daniel Veillard798024a2003-03-19 10:36:09 +0000290 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000291};
292
293/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000294 * xmlRelaxNGStates:
295 *
296 * A RelaxNGs container for validation state
297 */
298typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
299typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
300struct _xmlRelaxNGStates {
301 int nbState; /* the number of states */
302 int maxState; /* the size of the array */
303 xmlRelaxNGValidStatePtr *tabState;
304};
305
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000306#define ERROR_IS_DUP 1
Daniel Veillardfd573f12003-03-16 17:52:32 +0000307/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000308 * xmlRelaxNGValidError:
309 *
310 * A RelaxNGs validation error
311 */
312typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
313typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
314struct _xmlRelaxNGValidError {
315 xmlRelaxNGValidErr err; /* the error number */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000316 int flags; /* flags */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000317 xmlNodePtr node; /* the current node */
318 xmlNodePtr seq; /* the current child */
319 const xmlChar * arg1; /* first arg */
320 const xmlChar * arg2; /* second arg */
321};
322
323/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000324 * xmlRelaxNGValidCtxt:
325 *
326 * A RelaxNGs validation context
327 */
328
329struct _xmlRelaxNGValidCtxt {
330 void *userData; /* user specific data block */
331 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
332 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
333
334 xmlRelaxNGPtr schema; /* The schema in use */
335 xmlDocPtr doc; /* the document being validated */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000336 int flags; /* validation flags */
Daniel Veillard231d7912003-02-09 14:22:17 +0000337 int depth; /* validation depth */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000338 int idref; /* requires idref checking */
Daniel Veillarda507fbf2003-03-31 16:09:37 +0000339 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000340
341 /*
342 * Errors accumulated in branches may have to be stacked to be
343 * provided back when it's sure they affect validation.
344 */
345 xmlRelaxNGValidErrorPtr err; /* Last error */
346 int errNr; /* Depth of the error stack */
347 int errMax; /* Max depth of the error stack */
348 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000349
Daniel Veillardfd573f12003-03-16 17:52:32 +0000350 xmlRelaxNGValidStatePtr state; /* the current validation state */
351 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000352
353 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
354 int freeStatesNr;
355 int freeStatesMax;
356 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000357
358 /*
359 * This is used for "progressive" validation
360 */
361 xmlRegExecCtxtPtr elem; /* the current element regexp */
362 int elemNr; /* the number of element validated */
363 int elemMax; /* the max depth of elements */
364 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
365 int pstate; /* progressive state */
366 xmlNodePtr pnode; /* the current node */
367 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +0000368 int perr; /* signal error in content model
369 outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000370};
371
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000372/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000373 * xmlRelaxNGInclude:
374 *
375 * Structure associated to a RelaxNGs document element
376 */
377struct _xmlRelaxNGInclude {
Daniel Veillardc482e262003-02-26 14:48:48 +0000378 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000379 xmlChar *href; /* the normalized href value */
380 xmlDocPtr doc; /* the associated XML document */
381 xmlRelaxNGDefinePtr content;/* the definitions */
382 xmlRelaxNGPtr schema; /* the schema */
383};
384
385/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000386 * xmlRelaxNGDocument:
387 *
388 * Structure associated to a RelaxNGs document element
389 */
390struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000391 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000392 xmlChar *href; /* the normalized href value */
393 xmlDocPtr doc; /* the associated XML document */
394 xmlRelaxNGDefinePtr content;/* the definitions */
395 xmlRelaxNGPtr schema; /* the schema */
396};
397
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000398
Daniel Veillard6eadf632003-01-23 18:29:16 +0000399/************************************************************************
400 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000401 * Preliminary type checking interfaces *
402 * *
403 ************************************************************************/
404/**
405 * xmlRelaxNGTypeHave:
406 * @data: data needed for the library
407 * @type: the type name
408 * @value: the value to check
409 *
410 * Function provided by a type library to check if a type is exported
411 *
412 * Returns 1 if yes, 0 if no and -1 in case of error.
413 */
414typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type);
415
416/**
417 * xmlRelaxNGTypeCheck:
418 * @data: data needed for the library
419 * @type: the type name
420 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000421 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000422 *
423 * Function provided by a type library to check if a value match a type
424 *
425 * Returns 1 if yes, 0 if no and -1 in case of error.
426 */
427typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type,
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000428 const xmlChar *value, void **result,
429 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000430
431/**
432 * xmlRelaxNGFacetCheck:
433 * @data: data needed for the library
434 * @type: the type name
435 * @facet: the facet name
436 * @val: the facet value
437 * @strval: the string value
438 * @value: the value to check
439 *
440 * Function provided by a type library to check a value facet
441 *
442 * Returns 1 if yes, 0 if no and -1 in case of error.
443 */
444typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type,
445 const xmlChar *facet, const xmlChar *val,
446 const xmlChar *strval, void *value);
447
448/**
449 * xmlRelaxNGTypeFree:
450 * @data: data needed for the library
451 * @result: the value to free
452 *
453 * Function provided by a type library to free a returned result
454 */
455typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000456
457/**
458 * xmlRelaxNGTypeCompare:
459 * @data: data needed for the library
460 * @type: the type name
461 * @value1: the first value
462 * @value2: the second value
463 *
464 * Function provided by a type library to compare two values accordingly
465 * to a type.
466 *
467 * Returns 1 if yes, 0 if no and -1 in case of error.
468 */
469typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type,
470 const xmlChar *value1,
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000471 xmlNodePtr ctxt1,
472 void *comp1,
473 const xmlChar *value2,
474 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000475typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
476typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
477struct _xmlRelaxNGTypeLibrary {
478 const xmlChar *namespace; /* the datatypeLibrary value */
479 void *data; /* data needed for the library */
480 xmlRelaxNGTypeHave have; /* the export function */
481 xmlRelaxNGTypeCheck check; /* the checking function */
482 xmlRelaxNGTypeCompare comp; /* the compare function */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000483 xmlRelaxNGFacetCheck facet; /* the facet check function */
484 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000485};
486
487/************************************************************************
488 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000489 * Allocation functions *
490 * *
491 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000492static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
493static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillardd2298792003-02-14 16:54:11 +0000494static void xmlRelaxNGNormExtSpace(xmlChar *value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000495static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000496static int xmlRelaxNGEqualValidState(
497 xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
498 xmlRelaxNGValidStatePtr state1,
499 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000500static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
501 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000502
503/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000504 * xmlRelaxNGFreeDocument:
505 * @docu: a document structure
506 *
507 * Deallocate a RelaxNG document structure.
508 */
509static void
510xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
511{
512 if (docu == NULL)
513 return;
514
515 if (docu->href != NULL)
516 xmlFree(docu->href);
517 if (docu->doc != NULL)
518 xmlFreeDoc(docu->doc);
519 if (docu->schema != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000520 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000521 xmlFree(docu);
522}
523
524/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000525 * xmlRelaxNGFreeDocumentList:
526 * @docu: a list of document structure
527 *
528 * Deallocate a RelaxNG document structures.
529 */
530static void
531xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
532{
533 xmlRelaxNGDocumentPtr next;
534 while (docu != NULL) {
535 next = docu->next;
536 xmlRelaxNGFreeDocument(docu);
537 docu = next;
538 }
539}
540
541/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000542 * xmlRelaxNGFreeInclude:
543 * @incl: a include structure
544 *
545 * Deallocate a RelaxNG include structure.
546 */
547static void
548xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
549{
550 if (incl == NULL)
551 return;
552
553 if (incl->href != NULL)
554 xmlFree(incl->href);
555 if (incl->doc != NULL)
556 xmlFreeDoc(incl->doc);
557 if (incl->schema != NULL)
558 xmlRelaxNGFree(incl->schema);
559 xmlFree(incl);
560}
561
562/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000563 * xmlRelaxNGFreeIncludeList:
564 * @incl: a include structure list
565 *
566 * Deallocate a RelaxNG include structure.
567 */
568static void
569xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
570{
571 xmlRelaxNGIncludePtr next;
572 while (incl != NULL) {
573 next = incl->next;
574 xmlRelaxNGFreeInclude(incl);
575 incl = next;
576 }
577}
578
579/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000580 * xmlRelaxNGNewRelaxNG:
581 * @ctxt: a Relax-NG validation context (optional)
582 *
583 * Allocate a new RelaxNG structure.
584 *
585 * Returns the newly allocated structure or NULL in case or error
586 */
587static xmlRelaxNGPtr
588xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
589{
590 xmlRelaxNGPtr ret;
591
592 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
593 if (ret == NULL) {
594 if ((ctxt != NULL) && (ctxt->error != NULL))
595 ctxt->error(ctxt->userData, "Out of memory\n");
596 ctxt->nbErrors++;
597 return (NULL);
598 }
599 memset(ret, 0, sizeof(xmlRelaxNG));
600
601 return (ret);
602}
603
604/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000605 * xmlRelaxNGFreeInnerSchema:
606 * @schema: a schema structure
607 *
608 * Deallocate a RelaxNG schema structure.
609 */
610static void
611xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
612{
613 if (schema == NULL)
614 return;
615
616 if (schema->doc != NULL)
617 xmlFreeDoc(schema->doc);
618 if (schema->defTab != NULL) {
619 int i;
620
621 for (i = 0;i < schema->defNr;i++)
622 xmlRelaxNGFreeDefine(schema->defTab[i]);
623 xmlFree(schema->defTab);
624 }
625
626 xmlFree(schema);
627}
628
629/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000630 * xmlRelaxNGFree:
631 * @schema: a schema structure
632 *
633 * Deallocate a RelaxNG structure.
634 */
635void
636xmlRelaxNGFree(xmlRelaxNGPtr schema)
637{
638 if (schema == NULL)
639 return;
640
Daniel Veillard6eadf632003-01-23 18:29:16 +0000641 if (schema->topgrammar != NULL)
642 xmlRelaxNGFreeGrammar(schema->topgrammar);
643 if (schema->doc != NULL)
644 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000645 if (schema->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000646 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000647 if (schema->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000648 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000649 if (schema->defTab != NULL) {
650 int i;
651
652 for (i = 0;i < schema->defNr;i++)
653 xmlRelaxNGFreeDefine(schema->defTab[i]);
654 xmlFree(schema->defTab);
655 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000656
657 xmlFree(schema);
658}
659
660/**
661 * xmlRelaxNGNewGrammar:
662 * @ctxt: a Relax-NG validation context (optional)
663 *
664 * Allocate a new RelaxNG grammar.
665 *
666 * Returns the newly allocated structure or NULL in case or error
667 */
668static xmlRelaxNGGrammarPtr
669xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
670{
671 xmlRelaxNGGrammarPtr ret;
672
673 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
674 if (ret == NULL) {
675 if ((ctxt != NULL) && (ctxt->error != NULL))
676 ctxt->error(ctxt->userData, "Out of memory\n");
677 ctxt->nbErrors++;
678 return (NULL);
679 }
680 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
681
682 return (ret);
683}
684
685/**
686 * xmlRelaxNGFreeGrammar:
687 * @grammar: a grammar structure
688 *
689 * Deallocate a RelaxNG grammar structure.
690 */
691static void
692xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
693{
694 if (grammar == NULL)
695 return;
696
Daniel Veillardc482e262003-02-26 14:48:48 +0000697 if (grammar->children != NULL) {
698 xmlRelaxNGFreeGrammar(grammar->children);
699 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000700 if (grammar->next != NULL) {
701 xmlRelaxNGFreeGrammar(grammar->next);
702 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000703 if (grammar->refs != NULL) {
704 xmlHashFree(grammar->refs, NULL);
705 }
706 if (grammar->defs != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000707 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000708 }
709
710 xmlFree(grammar);
711}
712
713/**
714 * xmlRelaxNGNewDefine:
715 * @ctxt: a Relax-NG validation context
716 * @node: the node in the input document.
717 *
718 * Allocate a new RelaxNG define.
719 *
720 * Returns the newly allocated structure or NULL in case or error
721 */
722static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000723xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000724{
725 xmlRelaxNGDefinePtr ret;
726
Daniel Veillard419a7682003-02-03 23:22:49 +0000727 if (ctxt->defMax == 0) {
728 ctxt->defMax = 16;
729 ctxt->defNr = 0;
730 ctxt->defTab = (xmlRelaxNGDefinePtr *)
731 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
732 if (ctxt->defTab == NULL) {
733 if ((ctxt != NULL) && (ctxt->error != NULL))
734 ctxt->error(ctxt->userData, "Out of memory\n");
735 ctxt->nbErrors++;
736 return (NULL);
737 }
738 } else if (ctxt->defMax <= ctxt->defNr) {
739 xmlRelaxNGDefinePtr *tmp;
740 ctxt->defMax *= 2;
741 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
742 ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
743 if (tmp == NULL) {
744 if ((ctxt != NULL) && (ctxt->error != NULL))
745 ctxt->error(ctxt->userData, "Out of memory\n");
746 ctxt->nbErrors++;
747 return (NULL);
748 }
749 ctxt->defTab = tmp;
750 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000751 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
752 if (ret == NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000753 if ((ctxt != NULL) && (ctxt->error != NULL))
754 ctxt->error(ctxt->userData, "Out of memory\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +0000755 ctxt->nbErrors++;
Daniel Veillard419a7682003-02-03 23:22:49 +0000756 return(NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000757 }
758 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000759 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000760 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000761 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000762 return (ret);
763}
764
765/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000766 * xmlRelaxNGFreePartition:
767 * @partitions: a partition set structure
768 *
769 * Deallocate RelaxNG partition set structures.
770 */
771static void
772xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) {
773 xmlRelaxNGInterleaveGroupPtr group;
774 int j;
775
776 if (partitions != NULL) {
777 if (partitions->groups != NULL) {
778 for (j = 0;j < partitions->nbgroups;j++) {
779 group = partitions->groups[j];
780 if (group != NULL) {
781 if (group->defs != NULL)
782 xmlFree(group->defs);
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000783 if (group->attrs != NULL)
784 xmlFree(group->attrs);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000785 xmlFree(group);
786 }
787 }
788 xmlFree(partitions->groups);
789 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000790 if (partitions->triage != NULL) {
791 xmlHashFree(partitions->triage, NULL);
792 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000793 xmlFree(partitions);
794 }
795}
796/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000797 * xmlRelaxNGFreeDefine:
798 * @define: a define structure
799 *
800 * Deallocate a RelaxNG define structure.
801 */
802static void
803xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
804{
805 if (define == NULL)
806 return;
807
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000808 if ((define->type == XML_RELAXNG_VALUE) &&
809 (define->attrs != NULL)) {
810 xmlRelaxNGTypeLibraryPtr lib;
811
812 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
813 if ((lib != NULL) && (lib->freef != NULL))
814 lib->freef(lib->data, (void *) define->attrs);
815 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000816 if ((define->data != NULL) &&
817 (define->type == XML_RELAXNG_INTERLEAVE))
818 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
Daniel Veillarde063f482003-03-21 16:53:17 +0000819 if ((define->data != NULL) &&
820 (define->type == XML_RELAXNG_CHOICE))
821 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000822 if (define->name != NULL)
823 xmlFree(define->name);
824 if (define->ns != NULL)
825 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000826 if (define->value != NULL)
827 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000828 if (define->contModel != NULL)
829 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000830 xmlFree(define);
831}
832
833/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000834 * xmlRelaxNGNewStates:
835 * @ctxt: a Relax-NG validation context
836 * @size: the default size for the container
837 *
838 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000839 *
840 * Returns the newly allocated structure or NULL in case or error
841 */
842static xmlRelaxNGStatesPtr
843xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
844{
845 xmlRelaxNGStatesPtr ret;
846
Daniel Veillard798024a2003-03-19 10:36:09 +0000847 if ((ctxt != NULL) &&
848 (ctxt->freeState != NULL) &&
849 (ctxt->freeStatesNr > 0)) {
850 ctxt->freeStatesNr--;
851 ret = ctxt->freeStates[ctxt->freeStatesNr];
852 ret->nbState = 0;
853 return(ret);
854 }
Daniel Veillardfd573f12003-03-16 17:52:32 +0000855 if (size < 16) size = 16;
856
857 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
858 (size - 1) * sizeof(xmlRelaxNGValidStatePtr));
859 if (ret == NULL) {
860 if ((ctxt != NULL) && (ctxt->error != NULL))
861 ctxt->error(ctxt->userData, "Out of memory\n");
862 return (NULL);
863 }
864 ret->nbState = 0;
865 ret->maxState = size;
866 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc(
867 (size) * sizeof(xmlRelaxNGValidStatePtr));
868 if (ret->tabState == NULL) {
869 if ((ctxt != NULL) && (ctxt->error != NULL))
870 ctxt->error(ctxt->userData, "Out of memory\n");
871 xmlFree(ret->tabState);
872 return (NULL);
873 }
874 return(ret);
875}
876
877/**
Daniel Veillard798024a2003-03-19 10:36:09 +0000878 * xmlRelaxNGAddStateUniq:
879 * @ctxt: a Relax-NG validation context
880 * @states: the states container
881 * @state: the validation state
882 *
883 * Add a RelaxNG validation state to the container without checking
884 * for unicity.
885 *
886 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
887 */
888static int
889xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
890 xmlRelaxNGStatesPtr states,
891 xmlRelaxNGValidStatePtr state)
892{
893 if (state == NULL) {
894 return(-1);
895 }
896 if (states->nbState >= states->maxState) {
897 xmlRelaxNGValidStatePtr *tmp;
898 int size;
899
900 size = states->maxState * 2;
901 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
902 (size) * sizeof(xmlRelaxNGValidStatePtr));
903 if (tmp == NULL) {
904 if ((ctxt != NULL) && (ctxt->error != NULL))
905 ctxt->error(ctxt->userData, "Out of memory\n");
906 return(-1);
907 }
908 states->tabState = tmp;
909 states->maxState = size;
910 }
911 states->tabState[states->nbState++] = state;
912 return(1);
913}
914
915/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000916 * xmlRelaxNGAddState:
917 * @ctxt: a Relax-NG validation context
918 * @states: the states container
919 * @state: the validation state
920 *
921 * Add a RelaxNG validation state to the container
922 *
923 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
924 */
925static int
926xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states,
927 xmlRelaxNGValidStatePtr state)
928{
929 int i;
930
931 if (state == NULL) {
932 return(-1);
933 }
934 if (states->nbState >= states->maxState) {
935 xmlRelaxNGValidStatePtr *tmp;
936 int size;
937
938 size = states->maxState * 2;
939 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
940 (size) * sizeof(xmlRelaxNGValidStatePtr));
941 if (tmp == NULL) {
942 if ((ctxt != NULL) && (ctxt->error != NULL))
943 ctxt->error(ctxt->userData, "Out of memory\n");
944 return(-1);
945 }
946 states->tabState = tmp;
947 states->maxState = size;
948 }
949 for (i = 0;i < states->nbState;i++) {
950 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
Daniel Veillard798024a2003-03-19 10:36:09 +0000951 xmlRelaxNGFreeValidState(ctxt, state);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000952 return(0);
953 }
954 }
955 states->tabState[states->nbState++] = state;
956 return(1);
957}
958
959/**
960 * xmlRelaxNGFreeStates:
961 * @ctxt: a Relax-NG validation context
962 * @states: teh container
963 *
964 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000965 */
966static void
Daniel Veillard798024a2003-03-19 10:36:09 +0000967xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardfd573f12003-03-16 17:52:32 +0000968 xmlRelaxNGStatesPtr states)
969{
Daniel Veillard798024a2003-03-19 10:36:09 +0000970 if (states == NULL)
971 return;
Daniel Veillard798024a2003-03-19 10:36:09 +0000972 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
973 ctxt->freeStatesMax = 40;
974 ctxt->freeStatesNr = 0;
975 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
976 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
977 if (ctxt->freeStates == NULL) {
978 if ((ctxt != NULL) && (ctxt->error != NULL))
979 ctxt->error(ctxt->userData, "Out of memory\n");
980 }
981 } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
982 xmlRelaxNGStatesPtr *tmp;
983
984 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
985 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
986 if (tmp == NULL) {
987 if ((ctxt != NULL) && (ctxt->error != NULL))
988 ctxt->error(ctxt->userData, "Out of memory\n");
989 xmlFree(states->tabState);
990 xmlFree(states);
991 return;
992 }
993 ctxt->freeStates = tmp;
994 ctxt->freeStatesMax *= 2;
995 }
996 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +0000997 xmlFree(states->tabState);
998 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +0000999 } else {
1000 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001001 }
1002}
1003
1004/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001005 * xmlRelaxNGNewValidState:
1006 * @ctxt: a Relax-NG validation context
1007 * @node: the current node or NULL for the document
1008 *
1009 * Allocate a new RelaxNG validation state
1010 *
1011 * Returns the newly allocated structure or NULL in case or error
1012 */
1013static xmlRelaxNGValidStatePtr
1014xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1015{
1016 xmlRelaxNGValidStatePtr ret;
1017 xmlAttrPtr attr;
1018 xmlAttrPtr attrs[MAX_ATTR];
1019 int nbAttrs = 0;
1020 xmlNodePtr root = NULL;
1021
1022 if (node == NULL) {
1023 root = xmlDocGetRootElement(ctxt->doc);
1024 if (root == NULL)
1025 return(NULL);
1026 } else {
1027 attr = node->properties;
1028 while (attr != NULL) {
1029 if (nbAttrs < MAX_ATTR)
1030 attrs[nbAttrs++] = attr;
1031 else
1032 nbAttrs++;
1033 attr = attr->next;
1034 }
1035 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001036 if ((ctxt->freeState != NULL) &&
1037 (ctxt->freeState->nbState > 0)) {
1038 ctxt->freeState->nbState--;
1039 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1040 } else {
1041 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1042 if (ret == NULL) {
1043 if ((ctxt != NULL) && (ctxt->error != NULL))
1044 ctxt->error(ctxt->userData, "Out of memory\n");
1045 return (NULL);
1046 }
1047 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001048 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001049 ret->value = NULL;
1050 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001051 if (node == NULL) {
1052 ret->node = (xmlNodePtr) ctxt->doc;
1053 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001054 } else {
1055 ret->node = node;
1056 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001057 }
1058 ret->nbAttrs = 0;
1059 if (nbAttrs > 0) {
1060 if (ret->attrs == NULL) {
1061 if (nbAttrs < 4) ret->maxAttrs = 4;
1062 else ret->maxAttrs = nbAttrs;
1063 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1064 sizeof(xmlAttrPtr));
1065 if (ret->attrs == NULL) {
1066 if ((ctxt != NULL) && (ctxt->error != NULL))
1067 ctxt->error(ctxt->userData, "Out of memory\n");
1068 return (ret);
1069 }
1070 } else if (ret->maxAttrs < nbAttrs) {
1071 xmlAttrPtr *tmp;
1072
1073 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1074 sizeof(xmlAttrPtr));
1075 if (tmp == NULL) {
1076 if ((ctxt != NULL) && (ctxt->error != NULL))
1077 ctxt->error(ctxt->userData, "Out of memory\n");
1078 return (ret);
1079 }
1080 ret->attrs = tmp;
Daniel Veillard73827cb2003-08-25 10:57:27 +00001081 ret->maxAttrs = nbAttrs;
Daniel Veillard798024a2003-03-19 10:36:09 +00001082 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001083 ret->nbAttrs = nbAttrs;
Daniel Veillard798024a2003-03-19 10:36:09 +00001084 if (nbAttrs < MAX_ATTR) {
1085 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1086 } else {
1087 attr = node->properties;
1088 nbAttrs = 0;
1089 while (attr != NULL) {
1090 ret->attrs[nbAttrs++] = attr;
1091 attr = attr->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001092 }
1093 }
1094 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001095 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001096 return (ret);
1097}
1098
1099/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001100 * xmlRelaxNGCopyValidState:
1101 * @ctxt: a Relax-NG validation context
1102 * @state: a validation state
1103 *
1104 * Copy the validation state
1105 *
1106 * Returns the newly allocated structure or NULL in case or error
1107 */
1108static xmlRelaxNGValidStatePtr
1109xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1110 xmlRelaxNGValidStatePtr state)
1111{
1112 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001113 unsigned int maxAttrs;
1114 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001115
1116 if (state == NULL)
1117 return(NULL);
Daniel Veillard798024a2003-03-19 10:36:09 +00001118 if ((ctxt->freeState != NULL) &&
1119 (ctxt->freeState->nbState > 0)) {
1120 ctxt->freeState->nbState--;
1121 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1122 } else {
1123 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1124 if (ret == NULL) {
1125 if ((ctxt != NULL) && (ctxt->error != NULL))
1126 ctxt->error(ctxt->userData, "Out of memory\n");
1127 return (NULL);
1128 }
1129 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001130 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001131 attrs = ret->attrs;
1132 maxAttrs = ret->maxAttrs;
1133 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1134 ret->attrs = attrs;
1135 ret->maxAttrs = maxAttrs;
1136 if (state->nbAttrs > 0) {
1137 if (ret->attrs == NULL) {
1138 ret->maxAttrs = state->maxAttrs;
1139 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1140 sizeof(xmlAttrPtr));
1141 if (ret->attrs == NULL) {
1142 if ((ctxt != NULL) && (ctxt->error != NULL))
1143 ctxt->error(ctxt->userData, "Out of memory\n");
1144 ret->nbAttrs = 0;
1145 return (ret);
1146 }
1147 } else if (ret->maxAttrs < state->nbAttrs) {
1148 xmlAttrPtr *tmp;
1149
1150 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1151 sizeof(xmlAttrPtr));
1152 if (tmp == NULL) {
1153 if ((ctxt != NULL) && (ctxt->error != NULL))
1154 ctxt->error(ctxt->userData, "Out of memory\n");
1155 ret->nbAttrs = 0;
1156 return (ret);
1157 }
1158 ret->maxAttrs = state->maxAttrs;
Daniel Veillard73827cb2003-08-25 10:57:27 +00001159 ret->attrs = tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001160 }
1161 memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr));
1162 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001163 return(ret);
1164}
1165
1166/**
1167 * xmlRelaxNGEqualValidState:
1168 * @ctxt: a Relax-NG validation context
1169 * @state1: a validation state
1170 * @state2: a validation state
1171 *
1172 * Compare the validation states for equality
1173 *
1174 * Returns 1 if equald, 0 otherwise
1175 */
1176static int
1177xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1178 xmlRelaxNGValidStatePtr state1,
1179 xmlRelaxNGValidStatePtr state2)
1180{
1181 int i;
1182
1183 if ((state1 == NULL) || (state2 == NULL))
1184 return(0);
1185 if (state1 == state2)
1186 return(1);
1187 if (state1->node != state2->node)
1188 return(0);
1189 if (state1->seq != state2->seq)
1190 return(0);
1191 if (state1->nbAttrLeft != state2->nbAttrLeft)
1192 return(0);
1193 if (state1->nbAttrs != state2->nbAttrs)
1194 return(0);
1195 if (state1->endvalue != state2->endvalue)
1196 return(0);
1197 if ((state1->value != state2->value) &&
1198 (!xmlStrEqual(state1->value, state2->value)))
1199 return(0);
1200 for (i = 0;i < state1->nbAttrs;i++) {
1201 if (state1->attrs[i] != state2->attrs[i])
1202 return(0);
1203 }
1204 return(1);
1205}
1206
1207/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001208 * xmlRelaxNGFreeValidState:
1209 * @state: a validation state structure
1210 *
1211 * Deallocate a RelaxNG validation state structure.
1212 */
1213static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001214xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1215 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001216{
1217 if (state == NULL)
1218 return;
1219
Daniel Veillard798024a2003-03-19 10:36:09 +00001220 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1221 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1222 }
1223 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1224 if (state->attrs != NULL)
1225 xmlFree(state->attrs);
1226 xmlFree(state);
1227 } else {
1228 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1229 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001230}
1231
1232/************************************************************************
1233 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001234 * Document functions *
1235 * *
1236 ************************************************************************/
1237static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1238 xmlDocPtr doc);
1239
1240/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001241 * xmlRelaxNGIncludePush:
1242 * @ctxt: the parser context
1243 * @value: the element doc
1244 *
1245 * Pushes a new include on top of the include stack
1246 *
1247 * Returns 0 in case of error, the index in the stack otherwise
1248 */
1249static int
1250xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1251 xmlRelaxNGIncludePtr value)
1252{
1253 if (ctxt->incTab == NULL) {
1254 ctxt->incMax = 4;
1255 ctxt->incNr = 0;
1256 ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc(
1257 ctxt->incMax * sizeof(ctxt->incTab[0]));
1258 if (ctxt->incTab == NULL) {
1259 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1260 return (0);
1261 }
1262 }
1263 if (ctxt->incNr >= ctxt->incMax) {
1264 ctxt->incMax *= 2;
1265 ctxt->incTab =
1266 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1267 ctxt->incMax *
1268 sizeof(ctxt->incTab[0]));
1269 if (ctxt->incTab == NULL) {
1270 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1271 return (0);
1272 }
1273 }
1274 ctxt->incTab[ctxt->incNr] = value;
1275 ctxt->inc = value;
1276 return (ctxt->incNr++);
1277}
1278
1279/**
1280 * xmlRelaxNGIncludePop:
1281 * @ctxt: the parser context
1282 *
1283 * Pops the top include from the include stack
1284 *
1285 * Returns the include just removed
1286 */
1287static xmlRelaxNGIncludePtr
1288xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1289{
1290 xmlRelaxNGIncludePtr ret;
1291
1292 if (ctxt->incNr <= 0)
1293 return (0);
1294 ctxt->incNr--;
1295 if (ctxt->incNr > 0)
1296 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1297 else
1298 ctxt->inc = NULL;
1299 ret = ctxt->incTab[ctxt->incNr];
1300 ctxt->incTab[ctxt->incNr] = 0;
1301 return (ret);
1302}
1303
1304/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001305 * xmlRelaxNGRemoveRedefine:
1306 * @ctxt: the parser context
1307 * @URL: the normalized URL
1308 * @target: the included target
1309 * @name: the define name to eliminate
1310 *
1311 * Applies the elimination algorithm of 4.7
1312 *
1313 * Returns 0 in case of error, 1 in case of success.
1314 */
1315static int
1316xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1317 const xmlChar *URL ATTRIBUTE_UNUSED,
1318 xmlNodePtr target, const xmlChar *name) {
1319 int found = 0;
1320 xmlNodePtr tmp, tmp2;
1321 xmlChar *name2;
1322
1323#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001324 if (name == NULL)
1325 xmlGenericError(xmlGenericErrorContext,
1326 "Elimination of <include> start from %s\n", URL);
1327 else
1328 xmlGenericError(xmlGenericErrorContext,
1329 "Elimination of <include> define %s from %s\n", name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001330#endif
1331 tmp = target;
1332 while (tmp != NULL) {
1333 tmp2 = tmp->next;
1334 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1335 found = 1;
1336 xmlUnlinkNode(tmp);
1337 xmlFreeNode(tmp);
1338 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1339 name2 = xmlGetProp(tmp, BAD_CAST "name");
1340 xmlRelaxNGNormExtSpace(name2);
1341 if (name2 != NULL) {
1342 if (xmlStrEqual(name, name2)) {
1343 found = 1;
1344 xmlUnlinkNode(tmp);
1345 xmlFreeNode(tmp);
1346 }
1347 xmlFree(name2);
1348 }
1349 } else if (IS_RELAXNG(tmp, "include")) {
1350 xmlChar *href = NULL;
1351 xmlRelaxNGDocumentPtr inc = tmp->_private;
1352
1353 if ((inc != NULL) && (inc->doc != NULL) &&
1354 (inc->doc->children != NULL)) {
1355
1356 if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) {
1357#ifdef DEBUG_INCLUDE
1358 href = xmlGetProp(tmp, BAD_CAST "href");
1359#endif
1360 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1361 inc->doc->children->children, name) == 1) {
1362 found = 1;
1363 }
1364 if (href != NULL)
1365 xmlFree(href);
1366 }
1367 }
1368 }
1369 tmp = tmp2;
1370 }
1371 return(found);
1372}
1373
1374/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001375 * xmlRelaxNGLoadInclude:
1376 * @ctxt: the parser context
1377 * @URL: the normalized URL
1378 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001379 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001380 *
1381 * First lookup if the document is already loaded into the parser context,
1382 * check against recursion. If not found the resource is loaded and
1383 * the content is preprocessed before being returned back to the caller.
1384 *
1385 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1386 */
1387static xmlRelaxNGIncludePtr
1388xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillard416589a2003-02-17 17:25:42 +00001389 xmlNodePtr node, const xmlChar *ns) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001390 xmlRelaxNGIncludePtr ret = NULL;
1391 xmlDocPtr doc;
1392 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001393 xmlNodePtr root, cur;
1394
1395#ifdef DEBUG_INCLUDE
1396 xmlGenericError(xmlGenericErrorContext,
1397 "xmlRelaxNGLoadInclude(%s)\n", URL);
1398#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001399
1400 /*
1401 * check against recursion in the stack
1402 */
1403 for (i = 0;i < ctxt->incNr;i++) {
1404 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1405 if (ctxt->error != NULL)
1406 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00001407 "Detected an Include recursion for %s\n",
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001408 URL);
1409 ctxt->nbErrors++;
1410 return(NULL);
1411 }
1412 }
1413
1414 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001415 * load the document
1416 */
1417 doc = xmlParseFile((const char *) URL);
1418 if (doc == NULL) {
1419 if (ctxt->error != NULL)
1420 ctxt->error(ctxt->userData,
1421 "xmlRelaxNG: could not load %s\n", URL);
1422 ctxt->nbErrors++;
1423 return (NULL);
1424 }
1425
Daniel Veillard5add8682003-03-10 13:13:58 +00001426#ifdef DEBUG_INCLUDE
1427 xmlGenericError(xmlGenericErrorContext,
1428 "Parsed %s Okay\n", URL);
1429#endif
1430
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001431 /*
1432 * Allocate the document structures and register it first.
1433 */
1434 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1435 if (ret == NULL) {
1436 if (ctxt->error != NULL)
1437 ctxt->error(ctxt->userData,
1438 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1439 ctxt->nbErrors++;
1440 xmlFreeDoc(doc);
1441 return (NULL);
1442 }
1443 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1444 ret->doc = doc;
1445 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001446 ret->next = ctxt->includes;
1447 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001448
1449 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001450 * transmit the ns if needed
1451 */
1452 if (ns != NULL) {
1453 root = xmlDocGetRootElement(doc);
1454 if (root != NULL) {
1455 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1456 xmlSetProp(root, BAD_CAST"ns", ns);
1457 }
1458 }
1459 }
1460
1461 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001462 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001463 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001464 xmlRelaxNGIncludePush(ctxt, ret);
1465
1466 /*
1467 * Some preprocessing of the document content, this include recursing
1468 * in the include stack.
1469 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001470#ifdef DEBUG_INCLUDE
1471 xmlGenericError(xmlGenericErrorContext,
1472 "cleanup of %s\n", URL);
1473#endif
1474
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001475 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1476 if (doc == NULL) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001477 ctxt->inc = NULL;
1478 return(NULL);
1479 }
1480
1481 /*
1482 * Pop up the include from the stack
1483 */
1484 xmlRelaxNGIncludePop(ctxt);
1485
Daniel Veillard5add8682003-03-10 13:13:58 +00001486#ifdef DEBUG_INCLUDE
1487 xmlGenericError(xmlGenericErrorContext,
1488 "Checking of %s\n", URL);
1489#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001490 /*
1491 * Check that the top element is a grammar
1492 */
1493 root = xmlDocGetRootElement(doc);
1494 if (root == NULL) {
1495 if (ctxt->error != NULL)
1496 ctxt->error(ctxt->userData,
1497 "xmlRelaxNG: included document is empty %s\n", URL);
1498 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001499 return (NULL);
1500 }
1501 if (!IS_RELAXNG(root, "grammar")) {
1502 if (ctxt->error != NULL)
1503 ctxt->error(ctxt->userData,
1504 "xmlRelaxNG: included document %s root is not a grammar\n",
1505 URL);
1506 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001507 return (NULL);
1508 }
1509
1510 /*
1511 * Elimination of redefined rules in the include.
1512 */
1513 cur = node->children;
1514 while (cur != NULL) {
1515 if (IS_RELAXNG(cur, "start")) {
1516 int found = 0;
1517
Daniel Veillard5add8682003-03-10 13:13:58 +00001518 found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001519 if (!found) {
1520 if (ctxt->error != NULL)
1521 ctxt->error(ctxt->userData,
1522 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1523 URL);
1524 ctxt->nbErrors++;
1525 }
1526 } else if (IS_RELAXNG(cur, "define")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001527 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001528
1529 name = xmlGetProp(cur, BAD_CAST "name");
1530 if (name == NULL) {
1531 if (ctxt->error != NULL)
1532 ctxt->error(ctxt->userData,
1533 "xmlRelaxNG: include %s has define without name\n",
1534 URL);
1535 ctxt->nbErrors++;
1536 } else {
Daniel Veillard5add8682003-03-10 13:13:58 +00001537 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001538
Daniel Veillardd2298792003-02-14 16:54:11 +00001539 xmlRelaxNGNormExtSpace(name);
Daniel Veillard5add8682003-03-10 13:13:58 +00001540 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1541 root->children, name);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001542 if (!found) {
1543 if (ctxt->error != NULL)
1544 ctxt->error(ctxt->userData,
1545 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1546 URL, name);
1547 ctxt->nbErrors++;
1548 }
1549 xmlFree(name);
1550 }
1551 }
1552 cur = cur->next;
1553 }
1554
1555
1556 return(ret);
1557}
1558
1559/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001560 * xmlRelaxNGValidErrorPush:
1561 * @ctxt: the validation context
1562 * @err: the error code
1563 * @arg1: the first string argument
1564 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001565 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001566 *
1567 * Pushes a new error on top of the error stack
1568 *
1569 * Returns 0 in case of error, the index in the stack otherwise
1570 */
1571static int
1572xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001573 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001574{
1575 xmlRelaxNGValidErrorPtr cur;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001576#ifdef DEBUG_ERROR
1577 xmlGenericError(xmlGenericErrorContext,
1578 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
1579#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001580 if (ctxt->errTab == NULL) {
1581 ctxt->errMax = 8;
1582 ctxt->errNr = 0;
1583 ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc(
1584 ctxt->errMax * sizeof(xmlRelaxNGValidError));
1585 if (ctxt->errTab == NULL) {
1586 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1587 return (0);
1588 }
Daniel Veillard20863822003-03-22 17:51:47 +00001589 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001590 }
1591 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard20863822003-03-22 17:51:47 +00001592 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001593 ctxt->errTab =
1594 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard20863822003-03-22 17:51:47 +00001595 ctxt->errMax * sizeof(xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001596 if (ctxt->errTab == NULL) {
1597 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1598 return (0);
1599 }
Daniel Veillard20863822003-03-22 17:51:47 +00001600 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001601 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001602 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00001603 (ctxt->err->node == ctxt->state->node) &&
1604 (ctxt->err->err == err))
1605 return(ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001606 cur = &ctxt->errTab[ctxt->errNr];
1607 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001608 if (dup) {
1609 cur->arg1 = xmlStrdup(arg1);
1610 cur->arg2 = xmlStrdup(arg2);
1611 cur->flags = ERROR_IS_DUP;
1612 } else {
1613 cur->arg1 = arg1;
1614 cur->arg2 = arg2;
1615 cur->flags = 0;
1616 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001617 if (ctxt->state != NULL) {
1618 cur->node = ctxt->state->node;
1619 cur->seq = ctxt->state->seq;
1620 } else {
1621 cur->node = NULL;
1622 cur->seq = NULL;
1623 }
1624 ctxt->err = cur;
1625 return (ctxt->errNr++);
1626}
1627
1628/**
1629 * xmlRelaxNGValidErrorPop:
1630 * @ctxt: the validation context
1631 *
1632 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001633 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001634static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001635xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1636{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001637 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001638
Daniel Veillard580ced82003-03-21 21:22:48 +00001639 if (ctxt->errNr <= 0) {
1640 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001641 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001642 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001643 ctxt->errNr--;
1644 if (ctxt->errNr > 0)
1645 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1646 else
1647 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001648 cur = &ctxt->errTab[ctxt->errNr];
1649 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001650 if (cur->arg1 != NULL)
1651 xmlFree((xmlChar *)cur->arg1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001652 cur->arg1 = NULL;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001653 if (cur->arg2 != NULL)
1654 xmlFree((xmlChar *)cur->arg2);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001655 cur->arg2 = NULL;
1656 cur->flags = 0;
1657 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001658}
1659
Daniel Veillard42f12e92003-03-07 18:32:59 +00001660/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001661 * xmlRelaxNGDocumentPush:
1662 * @ctxt: the parser context
1663 * @value: the element doc
1664 *
1665 * Pushes a new doc on top of the doc stack
1666 *
1667 * Returns 0 in case of error, the index in the stack otherwise
1668 */
1669static int
1670xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1671 xmlRelaxNGDocumentPtr value)
1672{
1673 if (ctxt->docTab == NULL) {
1674 ctxt->docMax = 4;
1675 ctxt->docNr = 0;
1676 ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc(
1677 ctxt->docMax * sizeof(ctxt->docTab[0]));
1678 if (ctxt->docTab == NULL) {
1679 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1680 return (0);
1681 }
1682 }
1683 if (ctxt->docNr >= ctxt->docMax) {
1684 ctxt->docMax *= 2;
1685 ctxt->docTab =
1686 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1687 ctxt->docMax *
1688 sizeof(ctxt->docTab[0]));
1689 if (ctxt->docTab == NULL) {
1690 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1691 return (0);
1692 }
1693 }
1694 ctxt->docTab[ctxt->docNr] = value;
1695 ctxt->doc = value;
1696 return (ctxt->docNr++);
1697}
1698
1699/**
1700 * xmlRelaxNGDocumentPop:
1701 * @ctxt: the parser context
1702 *
1703 * Pops the top doc from the doc stack
1704 *
1705 * Returns the doc just removed
1706 */
1707static xmlRelaxNGDocumentPtr
1708xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1709{
1710 xmlRelaxNGDocumentPtr ret;
1711
1712 if (ctxt->docNr <= 0)
1713 return (0);
1714 ctxt->docNr--;
1715 if (ctxt->docNr > 0)
1716 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1717 else
1718 ctxt->doc = NULL;
1719 ret = ctxt->docTab[ctxt->docNr];
1720 ctxt->docTab[ctxt->docNr] = 0;
1721 return (ret);
1722}
1723
1724/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001725 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001726 * @ctxt: the parser context
1727 * @URL: the normalized URL
1728 * @ns: the inherited ns if any
1729 *
1730 * First lookup if the document is already loaded into the parser context,
1731 * check against recursion. If not found the resource is loaded and
1732 * the content is preprocessed before being returned back to the caller.
1733 *
1734 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1735 */
1736static xmlRelaxNGDocumentPtr
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001737xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001738 const xmlChar *ns) {
1739 xmlRelaxNGDocumentPtr ret = NULL;
1740 xmlDocPtr doc;
1741 xmlNodePtr root;
1742 int i;
1743
1744 /*
1745 * check against recursion in the stack
1746 */
1747 for (i = 0;i < ctxt->docNr;i++) {
1748 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1749 if (ctxt->error != NULL)
1750 ctxt->error(ctxt->userData,
1751 "Detected an externalRef recursion for %s\n",
1752 URL);
1753 ctxt->nbErrors++;
1754 return(NULL);
1755 }
1756 }
1757
1758 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001759 * load the document
1760 */
1761 doc = xmlParseFile((const char *) URL);
1762 if (doc == NULL) {
1763 if (ctxt->error != NULL)
1764 ctxt->error(ctxt->userData,
1765 "xmlRelaxNG: could not load %s\n", URL);
1766 ctxt->nbErrors++;
1767 return (NULL);
1768 }
1769
1770 /*
1771 * Allocate the document structures and register it first.
1772 */
1773 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1774 if (ret == NULL) {
1775 if (ctxt->error != NULL)
1776 ctxt->error(ctxt->userData,
1777 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1778 ctxt->nbErrors++;
1779 xmlFreeDoc(doc);
1780 return (NULL);
1781 }
1782 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1783 ret->doc = doc;
1784 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001785 ret->next = ctxt->documents;
1786 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001787
1788 /*
1789 * transmit the ns if needed
1790 */
1791 if (ns != NULL) {
1792 root = xmlDocGetRootElement(doc);
1793 if (root != NULL) {
1794 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1795 xmlSetProp(root, BAD_CAST"ns", ns);
1796 }
1797 }
1798 }
1799
1800 /*
1801 * push it on the stack and register it in the hash table
1802 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001803 xmlRelaxNGDocumentPush(ctxt, ret);
1804
1805 /*
1806 * Some preprocessing of the document content
1807 */
1808 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1809 if (doc == NULL) {
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001810 ctxt->doc = NULL;
1811 return(NULL);
1812 }
1813
1814 xmlRelaxNGDocumentPop(ctxt);
1815
1816 return(ret);
1817}
1818
1819/************************************************************************
1820 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001821 * Error functions *
1822 * *
1823 ************************************************************************/
1824
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001825#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1826#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
1827#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
1828#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
1829#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001830
Daniel Veillard231d7912003-02-09 14:22:17 +00001831static const char *
1832xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) {
1833 if (def == NULL)
1834 return("none");
1835 switch(def->type) {
1836 case XML_RELAXNG_EMPTY: return("empty");
1837 case XML_RELAXNG_NOT_ALLOWED: return("notAllowed");
1838 case XML_RELAXNG_EXCEPT: return("except");
1839 case XML_RELAXNG_TEXT: return("text");
1840 case XML_RELAXNG_ELEMENT: return("element");
1841 case XML_RELAXNG_DATATYPE: return("datatype");
1842 case XML_RELAXNG_VALUE: return("value");
1843 case XML_RELAXNG_LIST: return("list");
1844 case XML_RELAXNG_ATTRIBUTE: return("attribute");
1845 case XML_RELAXNG_DEF: return("def");
1846 case XML_RELAXNG_REF: return("ref");
1847 case XML_RELAXNG_EXTERNALREF: return("externalRef");
1848 case XML_RELAXNG_PARENTREF: return("parentRef");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001849 case XML_RELAXNG_OPTIONAL: return("optional");
1850 case XML_RELAXNG_ZEROORMORE: return("zeroOrMore");
Daniel Veillard231d7912003-02-09 14:22:17 +00001851 case XML_RELAXNG_ONEORMORE: return("oneOrMore");
1852 case XML_RELAXNG_CHOICE: return("choice");
1853 case XML_RELAXNG_GROUP: return("group");
1854 case XML_RELAXNG_INTERLEAVE: return("interleave");
1855 case XML_RELAXNG_START: return("start");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001856 case XML_RELAXNG_NOOP: return("noop");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001857 case XML_RELAXNG_PARAM: return("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00001858 }
1859 return("unknown");
1860}
Daniel Veillardd2298792003-02-14 16:54:11 +00001861
Daniel Veillard6eadf632003-01-23 18:29:16 +00001862/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001863 * xmlRelaxNGGetErrorString:
1864 * @err: the error code
1865 * @arg1: the first string argument
1866 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00001867 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00001868 * computes a formatted error string for the given error code and args
1869 *
1870 * Returns the error string, it must be deallocated by the caller
1871 */
1872static xmlChar *
Daniel Veillardfd573f12003-03-16 17:52:32 +00001873xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1,
1874 const xmlChar *arg2) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00001875 char msg[1000];
1876
1877 if (arg1 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001878 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001879 if (arg2 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001880 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001881
1882 msg[0] = 0;
1883 switch (err) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001884 case XML_RELAXNG_OK:
1885 return(NULL);
1886 case XML_RELAXNG_ERR_MEMORY:
1887 return(xmlCharStrdup("out of memory"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001888 case XML_RELAXNG_ERR_TYPE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00001889 snprintf(msg, 1000, "failed to validate type %s", arg1);
1890 break;
1891 case XML_RELAXNG_ERR_TYPEVAL:
Daniel Veillardc4c21552003-03-29 10:53:38 +00001892 snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001893 break;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001894 case XML_RELAXNG_ERR_DUPID:
1895 snprintf(msg, 1000, "ID %s redefined", arg1);
1896 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001897 case XML_RELAXNG_ERR_TYPECMP:
1898 snprintf(msg, 1000, "failed to compare type %s", arg1);
1899 break;
1900 case XML_RELAXNG_ERR_NOSTATE:
1901 return(xmlCharStrdup("Internal error: no state"));
1902 case XML_RELAXNG_ERR_NODEFINE:
1903 return(xmlCharStrdup("Internal error: no define"));
Daniel Veillard952379b2003-03-17 15:37:12 +00001904 case XML_RELAXNG_ERR_INTERNAL:
1905 snprintf(msg, 1000, "Internal error: %s", arg1);
1906 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001907 case XML_RELAXNG_ERR_LISTEXTRA:
1908 snprintf(msg, 1000, "Extra data in list: %s", arg1);
1909 break;
1910 case XML_RELAXNG_ERR_INTERNODATA:
1911 return(xmlCharStrdup("Internal: interleave block has no data"));
1912 case XML_RELAXNG_ERR_INTERSEQ:
1913 return(xmlCharStrdup("Invalid sequence in interleave"));
1914 case XML_RELAXNG_ERR_INTEREXTRA:
1915 snprintf(msg, 1000, "Extra element %s in interleave", arg1);
1916 break;
1917 case XML_RELAXNG_ERR_ELEMNAME:
1918 snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2);
1919 break;
1920 case XML_RELAXNG_ERR_ELEMNONS:
1921 snprintf(msg, 1000, "Expecting a namespace for element %s", arg1);
1922 break;
1923 case XML_RELAXNG_ERR_ELEMWRONGNS:
1924 snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s",
1925 arg1, arg2);
1926 break;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00001927 case XML_RELAXNG_ERR_ELEMWRONG:
1928 snprintf(msg, 1000, "Did not expect element %s there",
1929 arg1);
1930 break;
1931 case XML_RELAXNG_ERR_TEXTWRONG:
1932 snprintf(msg, 1000, "Did not expect text in element %s content",
1933 arg1);
1934 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001935 case XML_RELAXNG_ERR_ELEMEXTRANS:
1936 snprintf(msg, 1000, "Expecting no namespace for element %s", arg1);
1937 break;
1938 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
1939 snprintf(msg, 1000, "Expecting element %s to be empty", arg1);
1940 break;
1941 case XML_RELAXNG_ERR_NOELEM:
1942 snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1);
1943 break;
1944 case XML_RELAXNG_ERR_NOTELEM:
1945 return(xmlCharStrdup("Expecting an element got text"));
1946 case XML_RELAXNG_ERR_ATTRVALID:
1947 snprintf(msg, 1000, "Element %s failed to validate attributes",
1948 arg1);
1949 break;
1950 case XML_RELAXNG_ERR_CONTENTVALID:
1951 snprintf(msg, 1000, "Element %s failed to validate content",
1952 arg1);
1953 break;
1954 case XML_RELAXNG_ERR_EXTRACONTENT:
1955 snprintf(msg, 1000, "Element %s has extra content: %s",
1956 arg1, arg2);
1957 break;
1958 case XML_RELAXNG_ERR_INVALIDATTR:
1959 snprintf(msg, 1000, "Invalid attribute %s for element %s",
1960 arg1, arg2);
1961 break;
Daniel Veillardc4c21552003-03-29 10:53:38 +00001962 case XML_RELAXNG_ERR_LACKDATA:
1963 snprintf(msg, 1000, "Datatype element %s contains no data",
1964 arg1);
1965 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001966 case XML_RELAXNG_ERR_DATAELEM:
1967 snprintf(msg, 1000, "Datatype element %s has child elements",
1968 arg1);
1969 break;
1970 case XML_RELAXNG_ERR_VALELEM:
1971 snprintf(msg, 1000, "Value element %s has child elements",
1972 arg1);
1973 break;
1974 case XML_RELAXNG_ERR_LISTELEM:
1975 snprintf(msg, 1000, "List element %s has child elements",
1976 arg1);
1977 break;
1978 case XML_RELAXNG_ERR_DATATYPE:
1979 snprintf(msg, 1000, "Error validating datatype %s",
1980 arg1);
1981 break;
1982 case XML_RELAXNG_ERR_VALUE:
1983 snprintf(msg, 1000, "Error validating value %s",
1984 arg1);
1985 break;
1986 case XML_RELAXNG_ERR_LIST:
1987 return(xmlCharStrdup("Error validating list"));
1988 case XML_RELAXNG_ERR_NOGRAMMAR:
1989 return(xmlCharStrdup("No top grammar defined"));
1990 case XML_RELAXNG_ERR_EXTRADATA:
1991 return(xmlCharStrdup("Extra data in the document"));
1992 default:
Daniel Veillardac297932003-04-17 12:55:35 +00001993 return(xmlCharStrdup("Unknown error !"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001994 }
1995 if (msg[0] == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001996 snprintf(msg, 1000, "Unknown error code %d", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001997 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00001998 msg[1000 - 1] = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001999 return(xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002000}
2001
2002/**
2003 * xmlRelaxNGValidErrorContext:
2004 * @ctxt: the validation context
2005 * @node: the node
2006 * @child: the node child generating the problem.
2007 *
2008 * Dump informations about the kocation of the error in the instance
Daniel Veillard6eadf632003-01-23 18:29:16 +00002009 */
2010static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00002011xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node,
2012 xmlNodePtr child)
Daniel Veillard6eadf632003-01-23 18:29:16 +00002013{
2014 int line = 0;
2015 const xmlChar *file = NULL;
2016 const xmlChar *name = NULL;
2017 const char *type = "error";
2018
2019 if ((ctxt == NULL) || (ctxt->error == NULL))
2020 return;
2021
2022 if (child != NULL)
2023 node = child;
2024
2025 if (node != NULL) {
2026 if ((node->type == XML_DOCUMENT_NODE) ||
2027 (node->type == XML_HTML_DOCUMENT_NODE)) {
2028 xmlDocPtr doc = (xmlDocPtr) node;
2029
2030 file = doc->URL;
2031 } else {
2032 /*
2033 * Try to find contextual informations to report
2034 */
2035 if (node->type == XML_ELEMENT_NODE) {
Daniel Veillard34ba3872003-07-15 13:34:05 +00002036 line = (long) node->content;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002037 } else if ((node->prev != NULL) &&
2038 (node->prev->type == XML_ELEMENT_NODE)) {
Daniel Veillard34ba3872003-07-15 13:34:05 +00002039 line = (long) node->prev->content;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002040 } else if ((node->parent != NULL) &&
2041 (node->parent->type == XML_ELEMENT_NODE)) {
Daniel Veillard34ba3872003-07-15 13:34:05 +00002042 line = (long) node->parent->content;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002043 }
2044 if ((node->doc != NULL) && (node->doc->URL != NULL))
2045 file = node->doc->URL;
2046 if (node->name != NULL)
2047 name = node->name;
2048 }
2049 }
2050
Daniel Veillard42f12e92003-03-07 18:32:59 +00002051 type = "RNG validity error";
Daniel Veillard6eadf632003-01-23 18:29:16 +00002052
2053 if ((file != NULL) && (line != 0) && (name != NULL))
2054 ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n",
2055 type, file, line, name);
2056 else if ((file != NULL) && (name != NULL))
2057 ctxt->error(ctxt->userData, "%s: file %s element %s\n",
2058 type, file, name);
2059 else if ((file != NULL) && (line != 0))
2060 ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line);
2061 else if (file != NULL)
2062 ctxt->error(ctxt->userData, "%s: file %s\n", type, file);
2063 else if (name != NULL)
2064 ctxt->error(ctxt->userData, "%s: element %s\n", type, name);
2065 else
2066 ctxt->error(ctxt->userData, "%s\n", type);
2067}
Daniel Veillard42f12e92003-03-07 18:32:59 +00002068
2069/**
2070 * xmlRelaxNGShowValidError:
2071 * @ctxt: the validation context
2072 * @err: the error number
2073 * @node: the node
2074 * @child: the node child generating the problem.
2075 * @arg1: the first argument
2076 * @arg2: the second argument
2077 *
2078 * Show a validation error.
2079 */
2080static void
2081xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
2082 xmlNodePtr node, xmlNodePtr child,
2083 const xmlChar *arg1, const xmlChar *arg2)
2084{
2085 xmlChar *msg;
2086
2087 if (ctxt->error == NULL)
2088 return;
2089
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002090#ifdef DEBUG_ERROR
2091 xmlGenericError(xmlGenericErrorContext,
2092 "Show error %d\n", err);
2093#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002094 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2095 if (msg == NULL)
2096 return;
2097
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002098 if (ctxt->errNo == XML_RELAXNG_OK)
2099 ctxt->errNo = err;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002100 xmlRelaxNGValidErrorContext(ctxt, node, child);
2101 ctxt->error(ctxt->userData, "%s\n", msg);
2102 xmlFree(msg);
2103}
2104
2105/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002106 * xmlRelaxNGPopErrors:
2107 * @ctxt: the validation context
2108 * @level: the error level in the stack
2109 *
2110 * pop and discard all errors until the given level is reached
2111 */
2112static void
2113xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) {
2114 int i;
2115 xmlRelaxNGValidErrorPtr err;
2116
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002117#ifdef DEBUG_ERROR
2118 xmlGenericError(xmlGenericErrorContext,
2119 "Pop errors till level %d\n", level);
2120#endif
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002121 for (i = level;i < ctxt->errNr;i++) {
2122 err = &ctxt->errTab[i];
2123 if (err->flags & ERROR_IS_DUP) {
2124 if (err->arg1 != NULL)
2125 xmlFree((xmlChar *)err->arg1);
2126 err->arg1 = NULL;
2127 if (err->arg2 != NULL)
2128 xmlFree((xmlChar *)err->arg2);
2129 err->arg2 = NULL;
2130 err->flags = 0;
2131 }
2132 }
2133 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002134 if (ctxt->errNr <= 0)
2135 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002136}
2137/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002138 * xmlRelaxNGDumpValidError:
2139 * @ctxt: the validation context
2140 *
2141 * Show all validation error over a given index.
2142 */
2143static void
2144xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002145 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002146 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002147
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002148#ifdef DEBUG_ERROR
2149 xmlGenericError(xmlGenericErrorContext,
2150 "Dumping error stack %d errors\n", ctxt->errNr);
2151#endif
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002152 for (i = 0, k = 0;i < ctxt->errNr;i++) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00002153 err = &ctxt->errTab[i];
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002154 if (k < MAX_ERROR) {
2155 for (j = 0;j < i;j++) {
2156 dup = &ctxt->errTab[j];
2157 if ((err->err == dup->err) && (err->node == dup->node) &&
2158 (xmlStrEqual(err->arg1, dup->arg1)) &&
2159 (xmlStrEqual(err->arg2, dup->arg2))) {
2160 goto skip;
2161 }
Daniel Veillard580ced82003-03-21 21:22:48 +00002162 }
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002163 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2164 err->arg1, err->arg2);
2165 k++;
Daniel Veillard580ced82003-03-21 21:22:48 +00002166 }
Daniel Veillard580ced82003-03-21 21:22:48 +00002167skip:
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002168 if (err->flags & ERROR_IS_DUP) {
2169 if (err->arg1 != NULL)
2170 xmlFree((xmlChar *)err->arg1);
2171 err->arg1 = NULL;
2172 if (err->arg2 != NULL)
2173 xmlFree((xmlChar *)err->arg2);
2174 err->arg2 = NULL;
2175 err->flags = 0;
2176 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002177 }
2178 ctxt->errNr = 0;
2179}
2180/**
2181 * xmlRelaxNGAddValidError:
2182 * @ctxt: the validation context
2183 * @err: the error number
2184 * @arg1: the first argument
2185 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002186 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002187 *
2188 * Register a validation error, either generating it if it's sure
2189 * or stacking it for later handling if unsure.
2190 */
2191static void
2192xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002193 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002194{
2195 if ((ctxt == NULL) || (ctxt->error == NULL))
2196 return;
2197
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002198#ifdef DEBUG_ERROR
2199 xmlGenericError(xmlGenericErrorContext,
2200 "Adding error %d\n", err);
2201#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002202 /*
2203 * generate the error directly
2204 */
2205 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
2206 xmlNodePtr node, seq;
2207 /*
2208 * Flush first any stacked error which might be the
2209 * real cause of the problem.
2210 */
2211 if (ctxt->errNr != 0)
2212 xmlRelaxNGDumpValidError(ctxt);
2213 if (ctxt->state != NULL) {
2214 node = ctxt->state->node;
2215 seq = ctxt->state->seq;
2216 } else {
2217 node = seq = NULL;
2218 }
2219 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2220 }
2221 /*
2222 * Stack the error for later processing if needed
2223 */
2224 else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002225 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002226 }
2227}
2228
Daniel Veillard6eadf632003-01-23 18:29:16 +00002229
2230/************************************************************************
2231 * *
2232 * Type library hooks *
2233 * *
2234 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002235static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2236 const xmlChar *str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002237
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002238/**
2239 * xmlRelaxNGSchemaTypeHave:
2240 * @data: data needed for the library
2241 * @type: the type name
2242 *
2243 * Check if the given type is provided by
2244 * the W3C XMLSchema Datatype library.
2245 *
2246 * Returns 1 if yes, 0 if no and -1 in case of error.
2247 */
2248static int
2249xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002250 const xmlChar *type) {
2251 xmlSchemaTypePtr typ;
2252
2253 if (type == NULL)
2254 return(-1);
2255 typ = xmlSchemaGetPredefinedType(type,
2256 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2257 if (typ == NULL)
2258 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002259 return(1);
2260}
2261
2262/**
2263 * xmlRelaxNGSchemaTypeCheck:
2264 * @data: data needed for the library
2265 * @type: the type name
2266 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002267 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002268 *
2269 * Check if the given type and value are validated by
2270 * the W3C XMLSchema Datatype library.
2271 *
2272 * Returns 1 if yes, 0 if no and -1 in case of error.
2273 */
2274static int
2275xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002276 const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002277 const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002278 void **result,
2279 xmlNodePtr node) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002280 xmlSchemaTypePtr typ;
2281 int ret;
2282
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002283 if ((type == NULL) || (value == NULL))
2284 return(-1);
2285 typ = xmlSchemaGetPredefinedType(type,
2286 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2287 if (typ == NULL)
2288 return(-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002289 ret = xmlSchemaValPredefTypeNode(typ, value,
2290 (xmlSchemaValPtr *) result, node);
2291 if (ret == 2) /* special ID error code */
2292 return(2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002293 if (ret == 0)
2294 return(1);
2295 if (ret > 0)
2296 return(0);
2297 return(-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002298}
2299
2300/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002301 * xmlRelaxNGSchemaFacetCheck:
2302 * @data: data needed for the library
2303 * @type: the type name
2304 * @facet: the facet name
2305 * @val: the facet value
2306 * @strval: the string value
2307 * @value: the value to check
2308 *
2309 * Function provided by a type library to check a value facet
2310 *
2311 * Returns 1 if yes, 0 if no and -1 in case of error.
2312 */
2313static int
Daniel Veillard42f12e92003-03-07 18:32:59 +00002314xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002315 const xmlChar *facetname, const xmlChar *val,
2316 const xmlChar *strval, void *value) {
2317 xmlSchemaFacetPtr facet;
2318 xmlSchemaTypePtr typ;
2319 int ret;
2320
2321 if ((type == NULL) || (strval == NULL))
2322 return(-1);
2323 typ = xmlSchemaGetPredefinedType(type,
2324 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2325 if (typ == NULL)
2326 return(-1);
2327
2328 facet = xmlSchemaNewFacet();
2329 if (facet == NULL)
2330 return(-1);
2331
2332 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2333 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2334 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2335 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2336 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2337 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2338 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2339 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2340 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2341 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2342 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2343 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2344 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2345 facet->type = XML_SCHEMA_FACET_PATTERN;
2346 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2347 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2348 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2349 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2350 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2351 facet->type = XML_SCHEMA_FACET_LENGTH;
2352 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2353 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2354 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2355 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2356 } else {
2357 xmlSchemaFreeFacet(facet);
2358 return(-1);
2359 }
2360 facet->value = xmlStrdup(val);
2361 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2362 if (ret != 0) {
2363 xmlSchemaFreeFacet(facet);
2364 return(-1);
2365 }
2366 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2367 xmlSchemaFreeFacet(facet);
2368 if (ret != 0)
2369 return(-1);
2370 return(0);
2371}
2372
2373/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002374 * xmlRelaxNGSchemaFreeValue:
2375 * @data: data needed for the library
2376 * @value: the value to free
2377 *
2378 * Function provided by a type library to free a Schemas value
2379 *
2380 * Returns 1 if yes, 0 if no and -1 in case of error.
2381 */
2382static void
2383xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) {
2384 xmlSchemaFreeValue(value);
2385}
2386
2387/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002388 * xmlRelaxNGSchemaTypeCompare:
2389 * @data: data needed for the library
2390 * @type: the type name
2391 * @value1: the first value
2392 * @value2: the second value
2393 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002394 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002395 * Datatype library.
2396 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002397 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002398 */
2399static int
2400xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002401 const xmlChar *type,
2402 const xmlChar *value1,
2403 xmlNodePtr ctxt1,
2404 void *comp1,
2405 const xmlChar *value2,
2406 xmlNodePtr ctxt2) {
Daniel Veillard80b19092003-03-28 13:29:53 +00002407 int ret;
2408 xmlSchemaTypePtr typ;
2409 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2410
2411 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2412 return(-1);
2413 typ = xmlSchemaGetPredefinedType(type,
2414 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2415 if (typ == NULL)
2416 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002417 if (comp1 == NULL) {
2418 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2419 if (ret != 0)
2420 return(-1);
2421 if (res1 == NULL)
2422 return(-1);
2423 } else {
2424 res1 = (xmlSchemaValPtr) comp1;
2425 }
2426 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002427 if (ret != 0) {
2428 xmlSchemaFreeValue(res1);
2429 return(-1);
2430 }
2431 if (res1 == NULL) {
2432 xmlSchemaFreeValue(res1);
2433 return(-1);
2434 }
2435 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002436 if (res1 != (xmlSchemaValPtr) comp1)
2437 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002438 xmlSchemaFreeValue(res2);
2439 if (ret == -2)
2440 return(-1);
2441 if (ret == 0)
2442 return(1);
2443 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002444}
2445
2446/**
2447 * xmlRelaxNGDefaultTypeHave:
2448 * @data: data needed for the library
2449 * @type: the type name
2450 *
2451 * Check if the given type is provided by
2452 * the default datatype library.
2453 *
2454 * Returns 1 if yes, 0 if no and -1 in case of error.
2455 */
2456static int
2457xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) {
2458 if (type == NULL)
2459 return(-1);
2460 if (xmlStrEqual(type, BAD_CAST "string"))
2461 return(1);
2462 if (xmlStrEqual(type, BAD_CAST "token"))
2463 return(1);
2464 return(0);
2465}
2466
2467/**
2468 * xmlRelaxNGDefaultTypeCheck:
2469 * @data: data needed for the library
2470 * @type: the type name
2471 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002472 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002473 *
2474 * Check if the given type and value are validated by
2475 * the default datatype library.
2476 *
2477 * Returns 1 if yes, 0 if no and -1 in case of error.
2478 */
2479static int
2480xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2481 const xmlChar *type ATTRIBUTE_UNUSED,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002482 const xmlChar *value ATTRIBUTE_UNUSED,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002483 void **result ATTRIBUTE_UNUSED,
2484 xmlNodePtr node ATTRIBUTE_UNUSED) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002485 if (value == NULL)
2486 return(-1);
2487 if (xmlStrEqual(type, BAD_CAST "string"))
2488 return(1);
2489 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002490 return(1);
2491 }
2492
2493 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002494}
2495
2496/**
2497 * xmlRelaxNGDefaultTypeCompare:
2498 * @data: data needed for the library
2499 * @type: the type name
2500 * @value1: the first value
2501 * @value2: the second value
2502 *
2503 * Compare two values accordingly a type from the default
2504 * datatype library.
2505 *
2506 * Returns 1 if yes, 0 if no and -1 in case of error.
2507 */
2508static int
2509xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002510 const xmlChar *type,
2511 const xmlChar *value1,
2512 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2513 void *comp1 ATTRIBUTE_UNUSED,
2514 const xmlChar *value2,
2515 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00002516 int ret = -1;
2517
2518 if (xmlStrEqual(type, BAD_CAST "string")) {
2519 ret = xmlStrEqual(value1, value2);
2520 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2521 if (!xmlStrEqual(value1, value2)) {
2522 xmlChar *nval, *nvalue;
2523
2524 /*
2525 * TODO: trivial optimizations are possible by
2526 * computing at compile-time
2527 */
2528 nval = xmlRelaxNGNormalize(NULL, value1);
2529 nvalue = xmlRelaxNGNormalize(NULL, value2);
2530
Daniel Veillardd4310742003-02-18 21:12:46 +00002531 if ((nval == NULL) || (nvalue == NULL))
Daniel Veillardea3f3982003-01-26 19:45:18 +00002532 ret = -1;
Daniel Veillardd4310742003-02-18 21:12:46 +00002533 else if (xmlStrEqual(nval, nvalue))
2534 ret = 1;
2535 else
2536 ret = 0;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002537 if (nval != NULL)
2538 xmlFree(nval);
2539 if (nvalue != NULL)
2540 xmlFree(nvalue);
Daniel Veillardd4310742003-02-18 21:12:46 +00002541 } else
2542 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002543 }
2544 return(ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002545}
2546
2547static int xmlRelaxNGTypeInitialized = 0;
2548static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2549
2550/**
2551 * xmlRelaxNGFreeTypeLibrary:
2552 * @lib: the type library structure
2553 * @namespace: the URI bound to the library
2554 *
2555 * Free the structure associated to the type library
2556 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002557static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002558xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2559 const xmlChar *namespace ATTRIBUTE_UNUSED) {
2560 if (lib == NULL)
2561 return;
2562 if (lib->namespace != NULL)
2563 xmlFree((xmlChar *)lib->namespace);
2564 xmlFree(lib);
2565}
2566
2567/**
2568 * xmlRelaxNGRegisterTypeLibrary:
2569 * @namespace: the URI bound to the library
2570 * @data: data associated to the library
2571 * @have: the provide function
2572 * @check: the checking function
2573 * @comp: the comparison function
2574 *
2575 * Register a new type library
2576 *
2577 * Returns 0 in case of success and -1 in case of error.
2578 */
2579static int
2580xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data,
2581 xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002582 xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet,
2583 xmlRelaxNGTypeFree freef) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002584 xmlRelaxNGTypeLibraryPtr lib;
2585 int ret;
2586
2587 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2588 (check == NULL) || (comp == NULL))
2589 return(-1);
2590 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2591 xmlGenericError(xmlGenericErrorContext,
2592 "Relax-NG types library '%s' already registered\n",
2593 namespace);
2594 return(-1);
2595 }
2596 lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2597 if (lib == NULL) {
2598 xmlGenericError(xmlGenericErrorContext,
2599 "Relax-NG types library '%s' malloc() failed\n",
2600 namespace);
2601 return (-1);
2602 }
2603 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2604 lib->namespace = xmlStrdup(namespace);
2605 lib->data = data;
2606 lib->have = have;
2607 lib->comp = comp;
2608 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002609 lib->facet = facet;
2610 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002611 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2612 if (ret < 0) {
2613 xmlGenericError(xmlGenericErrorContext,
2614 "Relax-NG types library failed to register '%s'\n",
2615 namespace);
2616 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2617 return(-1);
2618 }
2619 return(0);
2620}
2621
2622/**
2623 * xmlRelaxNGInitTypes:
2624 *
2625 * Initilize the default type libraries.
2626 *
2627 * Returns 0 in case of success and -1 in case of error.
2628 */
2629static int
Daniel Veillard6eadf632003-01-23 18:29:16 +00002630xmlRelaxNGInitTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002631 if (xmlRelaxNGTypeInitialized != 0)
2632 return(0);
2633 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2634 if (xmlRelaxNGRegisteredTypes == NULL) {
2635 xmlGenericError(xmlGenericErrorContext,
2636 "Failed to allocate sh table for Relax-NG types\n");
2637 return(-1);
2638 }
2639 xmlRelaxNGRegisterTypeLibrary(
2640 BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes",
2641 NULL,
2642 xmlRelaxNGSchemaTypeHave,
2643 xmlRelaxNGSchemaTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002644 xmlRelaxNGSchemaTypeCompare,
2645 xmlRelaxNGSchemaFacetCheck,
Daniel Veillard80b19092003-03-28 13:29:53 +00002646 xmlRelaxNGSchemaFreeValue);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002647 xmlRelaxNGRegisterTypeLibrary(
2648 xmlRelaxNGNs,
2649 NULL,
2650 xmlRelaxNGDefaultTypeHave,
2651 xmlRelaxNGDefaultTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002652 xmlRelaxNGDefaultTypeCompare,
2653 NULL,
2654 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002655 xmlRelaxNGTypeInitialized = 1;
2656 return(0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002657}
2658
2659/**
2660 * xmlRelaxNGCleanupTypes:
2661 *
2662 * Cleanup the default Schemas type library associated to RelaxNG
2663 */
2664void
2665xmlRelaxNGCleanupTypes(void) {
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002666 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002667 if (xmlRelaxNGTypeInitialized == 0)
2668 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002669 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2670 xmlRelaxNGFreeTypeLibrary);
2671 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002672}
2673
2674/************************************************************************
2675 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002676 * Compiling element content into regexp *
2677 * *
2678 * Sometime the element content can be compiled into a pure regexp, *
2679 * This allows a faster execution and streamability at that level *
2680 * *
2681 ************************************************************************/
2682
Daniel Veillard52b48c72003-04-13 19:53:42 +00002683static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2684 xmlRelaxNGDefinePtr def);
2685
Daniel Veillard952379b2003-03-17 15:37:12 +00002686/**
2687 * xmlRelaxNGIsCompileable:
2688 * @define: the definition to check
2689 *
2690 * Check if a definition is nullable.
2691 *
2692 * Returns 1 if yes, 0 if no and -1 in case of error
2693 */
2694static int
2695xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00002696 int ret = -1;
2697
Daniel Veillard952379b2003-03-17 15:37:12 +00002698 if (def == NULL) {
2699 return(-1);
2700 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002701 if ((def->type != XML_RELAXNG_ELEMENT) &&
2702 (def->dflags & IS_COMPILABLE))
2703 return(1);
2704 if ((def->type != XML_RELAXNG_ELEMENT) &&
2705 (def->dflags & IS_NOT_COMPILABLE))
2706 return(0);
Daniel Veillard952379b2003-03-17 15:37:12 +00002707 switch(def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002708 case XML_RELAXNG_NOOP:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002709 ret = xmlRelaxNGIsCompileable(def->content);
2710 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002711 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002712 case XML_RELAXNG_EMPTY:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002713 ret = 1;
2714 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002715 case XML_RELAXNG_ELEMENT:
Daniel Veillardd94849b2003-07-28 13:02:24 +00002716 /*
2717 * Check if the element content is compileable
2718 */
Daniel Veillard52b48c72003-04-13 19:53:42 +00002719 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2720 ((def->dflags & IS_COMPILABLE) == 0)) {
Daniel Veillard2134ab12003-07-23 19:56:29 +00002721 xmlRelaxNGDefinePtr list;
2722 list = def->content;
2723 while (list != NULL) {
2724 ret = xmlRelaxNGIsCompileable(list);
2725 if (ret != 1)
2726 break;
2727 list = list->next;
2728 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002729 if (ret == 0) def->dflags |= IS_NOT_COMPILABLE;
2730 if (ret == 1) def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002731#ifdef DEBUG_COMPILE
2732 if (ret == 1) {
2733 xmlGenericError(xmlGenericErrorContext,
2734 "element content for %s is compilable\n",
2735 def->name);
2736 } else if (ret == 0) {
2737 xmlGenericError(xmlGenericErrorContext,
2738 "element content for %s is not compilable\n",
2739 def->name);
2740 } else {
2741 xmlGenericError(xmlGenericErrorContext,
2742 "Problem in RelaxNGIsCompileable for element %s\n",
2743 def->name);
2744 }
2745#endif
Daniel Veillard52b48c72003-04-13 19:53:42 +00002746 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002747 /*
2748 * All elements return a compileable status unless they
2749 * are generic like anyName
2750 */
2751 if ((def->nameClass != NULL) || (def->name == NULL))
2752 ret = 0;
2753 else
2754 ret = 1;
2755 return(ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002756 case XML_RELAXNG_REF:
2757 case XML_RELAXNG_EXTERNALREF:
2758 case XML_RELAXNG_PARENTREF:
2759 if (def->depth == -20) {
2760 return(1);
2761 } else {
2762 xmlRelaxNGDefinePtr list;
2763
2764 def->depth = -20;
2765 list = def->content;
2766 while (list != NULL) {
2767 ret = xmlRelaxNGIsCompileable(list);
2768 if (ret != 1)
2769 break;
2770 list = list->next;
2771 }
2772 }
2773 break;
2774 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002775 case XML_RELAXNG_OPTIONAL:
2776 case XML_RELAXNG_ZEROORMORE:
2777 case XML_RELAXNG_ONEORMORE:
2778 case XML_RELAXNG_CHOICE:
2779 case XML_RELAXNG_GROUP:
2780 case XML_RELAXNG_DEF: {
2781 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002782
2783 list = def->content;
2784 while (list != NULL) {
2785 ret = xmlRelaxNGIsCompileable(list);
2786 if (ret != 1)
Daniel Veillard52b48c72003-04-13 19:53:42 +00002787 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002788 list = list->next;
2789 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002790 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002791 }
2792 case XML_RELAXNG_EXCEPT:
2793 case XML_RELAXNG_ATTRIBUTE:
2794 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002795 case XML_RELAXNG_DATATYPE:
2796 case XML_RELAXNG_LIST:
2797 case XML_RELAXNG_PARAM:
2798 case XML_RELAXNG_VALUE:
2799 ret = 0;
2800 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002801 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002802 ret = -1;
2803 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002804 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002805 if (ret == 0) def->dflags |= IS_NOT_COMPILABLE;
2806 if (ret == 1) def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002807#ifdef DEBUG_COMPILE
2808 if (ret == 1) {
2809 xmlGenericError(xmlGenericErrorContext,
2810 "RelaxNGIsCompileable %s : true\n",
2811 xmlRelaxNGDefName(def));
2812 } else if (ret == 0) {
2813 xmlGenericError(xmlGenericErrorContext,
2814 "RelaxNGIsCompileable %s : false\n",
2815 xmlRelaxNGDefName(def));
2816 } else {
2817 xmlGenericError(xmlGenericErrorContext,
2818 "Problem in RelaxNGIsCompileable %s\n",
2819 xmlRelaxNGDefName(def));
2820 }
2821#endif
Daniel Veillard52b48c72003-04-13 19:53:42 +00002822 return(ret);
2823}
2824
2825/**
2826 * xmlRelaxNGCompile:
2827 * ctxt: the RelaxNG parser context
2828 * @define: the definition tree to compile
2829 *
2830 * Compile the set of definitions, it works recursively, till the
2831 * element boundaries, where it tries to compile the content if possible
2832 *
2833 * Returns 0 if success and -1 in case of error
2834 */
2835static int
2836xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) {
2837 int ret = 0;
2838 xmlRelaxNGDefinePtr list;
2839
2840 if ((ctxt == NULL) || (def == NULL)) return(-1);
2841
2842 switch(def->type) {
2843 case XML_RELAXNG_START:
2844 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
2845 xmlAutomataPtr oldam = ctxt->am;
2846 xmlAutomataStatePtr oldstate = ctxt->state;
2847
2848 def->depth = -25;
2849
2850 list = def->content;
2851 ctxt->am = xmlNewAutomata();
2852 if (ctxt->am == NULL)
2853 return(-1);
2854 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2855 while (list != NULL) {
2856 xmlRelaxNGCompile(ctxt, list);
2857 list = list->next;
2858 }
2859 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2860 def->contModel = xmlAutomataCompile(ctxt->am);
2861 xmlRegexpIsDeterminist(def->contModel);
2862
2863 xmlFreeAutomata(ctxt->am);
2864 ctxt->state = oldstate;
2865 ctxt->am = oldam;
2866 }
2867 break;
2868 case XML_RELAXNG_ELEMENT:
2869 if ((ctxt->am != NULL) && (def->name != NULL)) {
2870 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00002871 ctxt->state, NULL, def->name, def->ns, def);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002872 }
2873 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
2874 xmlAutomataPtr oldam = ctxt->am;
2875 xmlAutomataStatePtr oldstate = ctxt->state;
2876
2877 def->depth = -25;
2878
2879 list = def->content;
2880 ctxt->am = xmlNewAutomata();
2881 if (ctxt->am == NULL)
2882 return(-1);
2883 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2884 while (list != NULL) {
2885 xmlRelaxNGCompile(ctxt, list);
2886 list = list->next;
2887 }
2888 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2889 def->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00002890 if (!xmlRegexpIsDeterminist(def->contModel)) {
2891 /*
2892 * we can only use the automata if it is determinist
2893 */
2894 xmlRegFreeRegexp(def->contModel);
2895 def->contModel = NULL;
2896 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002897 xmlFreeAutomata(ctxt->am);
2898 ctxt->state = oldstate;
2899 ctxt->am = oldam;
2900 } else {
2901 xmlAutomataPtr oldam = ctxt->am;
2902
2903 /*
2904 * we can't build the content model for this element content
2905 * but it still might be possible to build it for some of its
2906 * children, recurse.
2907 */
2908 ret = xmlRelaxNGTryCompile(ctxt, def);
2909 ctxt->am = oldam;
2910 }
2911 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002912 case XML_RELAXNG_NOOP:
2913 ret = xmlRelaxNGCompile(ctxt, def->content);
2914 break;
2915 case XML_RELAXNG_OPTIONAL: {
2916 xmlAutomataStatePtr oldstate = ctxt->state;
2917
2918 xmlRelaxNGCompile(ctxt, def->content);
2919 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
2920 break;
2921 }
2922 case XML_RELAXNG_ZEROORMORE: {
2923 xmlAutomataStatePtr oldstate;
2924
2925 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
2926 oldstate = ctxt->state;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00002927 list = def->content;
2928 while (list != NULL) {
2929 xmlRelaxNGCompile(ctxt, list);
2930 list = list->next;
2931 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002932 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
2933 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2934 break;
2935 }
2936 case XML_RELAXNG_ONEORMORE: {
2937 xmlAutomataStatePtr oldstate;
2938
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00002939 list = def->content;
2940 while (list != NULL) {
2941 xmlRelaxNGCompile(ctxt, list);
2942 list = list->next;
2943 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002944 oldstate = ctxt->state;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00002945 list = def->content;
2946 while (list != NULL) {
2947 xmlRelaxNGCompile(ctxt, list);
2948 list = list->next;
2949 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002950 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
2951 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2952 break;
2953 }
2954 case XML_RELAXNG_CHOICE: {
2955 xmlAutomataStatePtr target = NULL;
2956 xmlAutomataStatePtr oldstate = ctxt->state;
2957
2958 list = def->content;
2959 while (list != NULL) {
2960 ctxt->state = oldstate;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002961 ret = xmlRelaxNGCompile(ctxt, list);
2962 if (ret != 0)
2963 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002964 if (target == NULL)
2965 target = ctxt->state;
2966 else {
2967 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target);
2968 }
2969 list = list->next;
2970 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00002971 ctxt->state = target;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002972
2973 break;
2974 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00002975 case XML_RELAXNG_REF:
2976 case XML_RELAXNG_EXTERNALREF:
2977 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002978 case XML_RELAXNG_GROUP:
2979 case XML_RELAXNG_DEF:
2980 list = def->content;
2981 while (list != NULL) {
Daniel Veillard2134ab12003-07-23 19:56:29 +00002982 ret = xmlRelaxNGCompile(ctxt, list);
2983 if (ret != 0)
2984 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00002985 list = list->next;
2986 }
2987 break;
2988 case XML_RELAXNG_TEXT: {
2989 xmlAutomataStatePtr oldstate;
2990
2991 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
2992 oldstate = ctxt->state;
2993 xmlRelaxNGCompile(ctxt, def->content);
2994 xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state,
2995 BAD_CAST "#text", NULL);
2996 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2997 break;
2998 }
2999 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00003000 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003001 break;
3002 case XML_RELAXNG_EXCEPT:
3003 case XML_RELAXNG_ATTRIBUTE:
3004 case XML_RELAXNG_INTERLEAVE:
3005 case XML_RELAXNG_NOT_ALLOWED:
3006 case XML_RELAXNG_DATATYPE:
3007 case XML_RELAXNG_LIST:
3008 case XML_RELAXNG_PARAM:
3009 case XML_RELAXNG_VALUE:
Daniel Veillardac297932003-04-17 12:55:35 +00003010 /* This should not happen and generate an internal error */
3011 fprintf(stderr, "RNG internal error trying to compile %s\n",
3012 xmlRelaxNGDefName(def));
Daniel Veillard52b48c72003-04-13 19:53:42 +00003013 break;
3014 }
3015 return(ret);
3016}
3017
3018/**
3019 * xmlRelaxNGTryCompile:
3020 * ctxt: the RelaxNG parser context
3021 * @define: the definition tree to compile
3022 *
3023 * Try to compile the set of definitions, it works recursively,
3024 * possibly ignoring parts which cannot be compiled.
3025 *
3026 * Returns 0 if success and -1 in case of error
3027 */
3028static int
3029xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) {
3030 int ret = 0;
3031 xmlRelaxNGDefinePtr list;
3032
3033 if ((ctxt == NULL) || (def == NULL)) return(-1);
3034
3035 if ((def->type == XML_RELAXNG_START) ||
3036 (def->type == XML_RELAXNG_ELEMENT)) {
3037 ret = xmlRelaxNGIsCompileable(def);
3038 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3039 ctxt->am = NULL;
3040 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003041#ifdef DEBUG_PROGRESSIVE
3042 if (ret == 0) {
3043 if (def->type == XML_RELAXNG_START)
3044 xmlGenericError(xmlGenericErrorContext,
3045 "compiled the start\n");
3046 else
3047 xmlGenericError(xmlGenericErrorContext,
3048 "compiled element %s\n", def->name);
3049 } else {
3050 if (def->type == XML_RELAXNG_START)
3051 xmlGenericError(xmlGenericErrorContext,
3052 "failed to compile the start\n");
3053 else
3054 xmlGenericError(xmlGenericErrorContext,
3055 "failed to compile element %s\n", def->name);
3056 }
3057#endif
Daniel Veillard52b48c72003-04-13 19:53:42 +00003058 return(ret);
3059 }
3060 }
3061 switch(def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003062 case XML_RELAXNG_NOOP:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003063 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3064 break;
3065 case XML_RELAXNG_TEXT:
3066 case XML_RELAXNG_DATATYPE:
3067 case XML_RELAXNG_LIST:
3068 case XML_RELAXNG_PARAM:
3069 case XML_RELAXNG_VALUE:
3070 case XML_RELAXNG_EMPTY:
3071 case XML_RELAXNG_ELEMENT:
3072 ret = 0;
3073 break;
3074 case XML_RELAXNG_OPTIONAL:
3075 case XML_RELAXNG_ZEROORMORE:
3076 case XML_RELAXNG_ONEORMORE:
3077 case XML_RELAXNG_CHOICE:
3078 case XML_RELAXNG_GROUP:
3079 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003080 case XML_RELAXNG_START:
3081 case XML_RELAXNG_REF:
3082 case XML_RELAXNG_EXTERNALREF:
3083 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003084 list = def->content;
3085 while (list != NULL) {
3086 ret = xmlRelaxNGTryCompile(ctxt, list);
3087 if (ret != 0)
3088 break;
3089 list = list->next;
3090 }
3091 break;
3092 case XML_RELAXNG_EXCEPT:
3093 case XML_RELAXNG_ATTRIBUTE:
3094 case XML_RELAXNG_INTERLEAVE:
3095 case XML_RELAXNG_NOT_ALLOWED:
3096 ret = 0;
3097 break;
3098 }
3099 return(ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003100}
3101
3102/************************************************************************
3103 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003104 * Parsing functions *
3105 * *
3106 ************************************************************************/
3107
3108static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(
3109 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3110static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(
3111 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3112static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(
Daniel Veillard154877e2003-01-30 12:17:05 +00003113 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003114static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(
3115 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003116static xmlRelaxNGPtr xmlRelaxNGParseDocument(
3117 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00003118static int xmlRelaxNGParseGrammarContent(
3119 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00003120static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(
3121 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
3122 xmlRelaxNGDefinePtr def);
Daniel Veillard419a7682003-02-03 23:22:49 +00003123static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(
3124 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003125static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3126 xmlRelaxNGDefinePtr define, xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003127
3128
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003129#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003130
3131/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003132 * xmlRelaxNGIsNullable:
3133 * @define: the definition to verify
3134 *
3135 * Check if a definition is nullable.
3136 *
3137 * Returns 1 if yes, 0 if no and -1 in case of error
3138 */
3139static int
3140xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) {
3141 int ret;
3142 if (define == NULL)
3143 return(-1);
3144
Daniel Veillarde063f482003-03-21 16:53:17 +00003145 if (define->dflags & IS_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00003146 return(1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003147 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00003148 return(0);
3149 switch (define->type) {
3150 case XML_RELAXNG_EMPTY:
3151 case XML_RELAXNG_TEXT:
3152 ret = 1; break;
3153 case XML_RELAXNG_NOOP:
3154 case XML_RELAXNG_DEF:
3155 case XML_RELAXNG_REF:
3156 case XML_RELAXNG_EXTERNALREF:
3157 case XML_RELAXNG_PARENTREF:
3158 case XML_RELAXNG_ONEORMORE:
3159 ret = xmlRelaxNGIsNullable(define->content);
3160 break;
3161 case XML_RELAXNG_EXCEPT:
3162 case XML_RELAXNG_NOT_ALLOWED:
3163 case XML_RELAXNG_ELEMENT:
3164 case XML_RELAXNG_DATATYPE:
3165 case XML_RELAXNG_PARAM:
3166 case XML_RELAXNG_VALUE:
3167 case XML_RELAXNG_LIST:
3168 case XML_RELAXNG_ATTRIBUTE:
3169 ret = 0; break;
3170 case XML_RELAXNG_CHOICE: {
3171 xmlRelaxNGDefinePtr list = define->content;
3172
3173 while (list != NULL) {
3174 ret = xmlRelaxNGIsNullable(list);
3175 if (ret != 0)
3176 goto done;
3177 list = list->next;
3178 }
3179 ret = 0; break;
3180 }
3181 case XML_RELAXNG_START:
3182 case XML_RELAXNG_INTERLEAVE:
3183 case XML_RELAXNG_GROUP: {
3184 xmlRelaxNGDefinePtr list = define->content;
3185
3186 while (list != NULL) {
3187 ret = xmlRelaxNGIsNullable(list);
3188 if (ret != 1)
3189 goto done;
3190 list = list->next;
3191 }
3192 return(1);
3193 }
3194 default:
3195 return(-1);
3196 }
3197done:
3198 if (ret == 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00003199 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003200 if (ret == 1)
Daniel Veillarde063f482003-03-21 16:53:17 +00003201 define->dflags |= IS_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003202 return(ret);
3203}
3204
3205/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003206 * xmlRelaxNGIsBlank:
3207 * @str: a string
3208 *
3209 * Check if a string is ignorable c.f. 4.2. Whitespace
3210 *
3211 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3212 */
3213static int
3214xmlRelaxNGIsBlank(xmlChar *str) {
3215 if (str == NULL)
3216 return(1);
3217 while (*str != 0) {
3218 if (!(IS_BLANK(*str))) return(0);
3219 str++;
3220 }
3221 return(1);
3222}
3223
Daniel Veillard6eadf632003-01-23 18:29:16 +00003224/**
3225 * xmlRelaxNGGetDataTypeLibrary:
3226 * @ctxt: a Relax-NG parser context
3227 * @node: the current data or value element
3228 *
3229 * Applies algorithm from 4.3. datatypeLibrary attribute
3230 *
3231 * Returns the datatypeLibary value or NULL if not found
3232 */
3233static xmlChar *
3234xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3235 xmlNodePtr node) {
3236 xmlChar *ret, *escape;
3237
Daniel Veillard6eadf632003-01-23 18:29:16 +00003238 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3239 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3240 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00003241 if (ret[0] == 0) {
3242 xmlFree(ret);
3243 return(NULL);
3244 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003245 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
Daniel Veillard6eadf632003-01-23 18:29:16 +00003246 if (escape == NULL) {
3247 return(ret);
3248 }
3249 xmlFree(ret);
3250 return(escape);
3251 }
3252 }
3253 node = node->parent;
3254 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillarde5b110b2003-02-04 14:43:39 +00003255 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3256 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00003257 if (ret[0] == 0) {
3258 xmlFree(ret);
3259 return(NULL);
3260 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00003261 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3262 if (escape == NULL) {
3263 return(ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003264 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00003265 xmlFree(ret);
3266 return(escape);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003267 }
3268 node = node->parent;
3269 }
3270 return(NULL);
3271}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003272
3273/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003274 * xmlRelaxNGParseValue:
3275 * @ctxt: a Relax-NG parser context
3276 * @node: the data node.
3277 *
3278 * parse the content of a RelaxNG value node.
3279 *
3280 * Returns the definition pointer or NULL in case of error
3281 */
3282static xmlRelaxNGDefinePtr
3283xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3284 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003285 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003286 xmlChar *type;
3287 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003288 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003289
Daniel Veillardfd573f12003-03-16 17:52:32 +00003290 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003291 if (def == NULL)
3292 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003293 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003294
3295 type = xmlGetProp(node, BAD_CAST "type");
3296 if (type != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00003297 xmlRelaxNGNormExtSpace(type);
3298 if (xmlValidateNCName(type, 0)) {
3299 if (ctxt->error != NULL)
3300 ctxt->error(ctxt->userData,
3301 "value type '%s' is not an NCName\n",
3302 type);
3303 ctxt->nbErrors++;
3304 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003305 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3306 if (library == NULL)
3307 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3308
3309 def->name = type;
3310 def->ns = library;
3311
3312 lib = (xmlRelaxNGTypeLibraryPtr)
3313 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3314 if (lib == NULL) {
3315 if (ctxt->error != NULL)
3316 ctxt->error(ctxt->userData,
3317 "Use of unregistered type library '%s'\n",
3318 library);
3319 ctxt->nbErrors++;
3320 def->data = NULL;
3321 } else {
3322 def->data = lib;
3323 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003324 if (ctxt->error != NULL)
3325 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00003326 "Internal error with type library '%s': no 'have'\n",
3327 library);
3328 ctxt->nbErrors++;
3329 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003330 success = lib->have(lib->data, def->name);
3331 if (success != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003332 if (ctxt->error != NULL)
3333 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00003334 "Error type '%s' is not exported by type library '%s'\n",
3335 def->name, library);
3336 ctxt->nbErrors++;
3337 }
3338 }
3339 }
3340 }
3341 if (node->children == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00003342 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003343 } else if (((node->children->type != XML_TEXT_NODE) &&
3344 (node->children->type != XML_CDATA_SECTION_NODE)) ||
Daniel Veillardedc91922003-01-26 00:52:04 +00003345 (node->children->next != NULL)) {
3346 if (ctxt->error != NULL)
3347 ctxt->error(ctxt->userData,
3348 "Expecting a single text value for <value>content\n");
3349 ctxt->nbErrors++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003350 } else if (def != NULL) {
Daniel Veillardedc91922003-01-26 00:52:04 +00003351 def->value = xmlNodeGetContent(node);
3352 if (def->value == NULL) {
3353 if (ctxt->error != NULL)
3354 ctxt->error(ctxt->userData,
3355 "Element <value> has no content\n");
3356 ctxt->nbErrors++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003357 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3358 void *val = NULL;
3359
3360 success = lib->check(lib->data, def->name, def->value, &val, node);
3361 if (success != 1) {
3362 if (ctxt->error != NULL)
3363 ctxt->error(ctxt->userData,
3364 "Value '%s' is not acceptable for type '%s'\n",
3365 def->value, def->name);
3366 ctxt->nbErrors++;
3367 } else {
3368 if (val != NULL)
3369 def->attrs = val;
3370 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003371 }
3372 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003373 return(def);
3374}
3375
3376/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003377 * xmlRelaxNGParseData:
3378 * @ctxt: a Relax-NG parser context
3379 * @node: the data node.
3380 *
3381 * parse the content of a RelaxNG data node.
3382 *
3383 * Returns the definition pointer or NULL in case of error
3384 */
3385static xmlRelaxNGDefinePtr
3386xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard416589a2003-02-17 17:25:42 +00003387 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003388 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003389 xmlRelaxNGTypeLibraryPtr lib;
3390 xmlChar *type;
3391 xmlChar *library;
3392 xmlNodePtr content;
3393 int tmp;
3394
3395 type = xmlGetProp(node, BAD_CAST "type");
3396 if (type == NULL) {
3397 if (ctxt->error != NULL)
3398 ctxt->error(ctxt->userData,
3399 "data has no type\n");
3400 ctxt->nbErrors++;
3401 return(NULL);
3402 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003403 xmlRelaxNGNormExtSpace(type);
3404 if (xmlValidateNCName(type, 0)) {
3405 if (ctxt->error != NULL)
3406 ctxt->error(ctxt->userData,
3407 "data type '%s' is not an NCName\n",
3408 type);
3409 ctxt->nbErrors++;
3410 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003411 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3412 if (library == NULL)
3413 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3414
Daniel Veillardfd573f12003-03-16 17:52:32 +00003415 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003416 if (def == NULL) {
3417 xmlFree(type);
3418 return(NULL);
3419 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003420 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003421 def->name = type;
3422 def->ns = library;
3423
3424 lib = (xmlRelaxNGTypeLibraryPtr)
3425 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3426 if (lib == NULL) {
3427 if (ctxt->error != NULL)
3428 ctxt->error(ctxt->userData,
3429 "Use of unregistered type library '%s'\n",
3430 library);
3431 ctxt->nbErrors++;
3432 def->data = NULL;
3433 } else {
3434 def->data = lib;
3435 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003436 if (ctxt->error != NULL)
3437 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003438 "Internal error with type library '%s': no 'have'\n",
3439 library);
3440 ctxt->nbErrors++;
3441 } else {
3442 tmp = lib->have(lib->data, def->name);
3443 if (tmp != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003444 if (ctxt->error != NULL)
3445 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003446 "Error type '%s' is not exported by type library '%s'\n",
3447 def->name, library);
3448 ctxt->nbErrors++;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003449 } else if ((xmlStrEqual(library, BAD_CAST
3450 "http://www.w3.org/2001/XMLSchema-datatypes")) &&
3451 ((xmlStrEqual(def->name, BAD_CAST "IDREF")) ||
3452 (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3453 ctxt->idref = 1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003454 }
3455 }
3456 }
3457 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003458
3459 /*
3460 * Handle optional params
3461 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003462 while (content != NULL) {
Daniel Veillard416589a2003-02-17 17:25:42 +00003463 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3464 break;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003465 if (xmlStrEqual(library,
3466 BAD_CAST"http://relaxng.org/ns/structure/1.0")) {
3467 if (ctxt->error != NULL)
3468 ctxt->error(ctxt->userData,
3469 "Type library '%s' does not allow type parameters\n",
3470 library);
3471 ctxt->nbErrors++;
3472 content = content->next;
3473 while ((content != NULL) &&
3474 (xmlStrEqual(content->name, BAD_CAST "param")))
3475 content = content->next;
3476 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003477 param = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003478 if (param != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003479 param->type = XML_RELAXNG_PARAM;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003480 param->name = xmlGetProp(content, BAD_CAST "name");
3481 if (param->name == NULL) {
3482 if (ctxt->error != NULL)
3483 ctxt->error(ctxt->userData,
3484 "param has no name\n");
3485 ctxt->nbErrors++;
3486 }
3487 param->value = xmlNodeGetContent(content);
3488 if (lastparam == NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003489 def->attrs = lastparam = param;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003490 } else {
3491 lastparam->next = param;
3492 lastparam = param;
3493 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003494 if (lib != NULL) {
3495 }
Daniel Veillard8fe98712003-02-19 00:19:14 +00003496 }
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003497 content = content->next;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003498 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003499 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003500 /*
3501 * Handle optional except
3502 */
3503 if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3504 xmlNodePtr child;
3505 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
3506
Daniel Veillardfd573f12003-03-16 17:52:32 +00003507 except = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard416589a2003-02-17 17:25:42 +00003508 if (except == NULL) {
3509 return(def);
3510 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003511 except->type = XML_RELAXNG_EXCEPT;
Daniel Veillard416589a2003-02-17 17:25:42 +00003512 child = content->children;
3513 if (last == NULL) {
3514 def->content = except;
3515 } else {
3516 last->next = except;
3517 }
3518 if (child == NULL) {
3519 if (ctxt->error != NULL)
3520 ctxt->error(ctxt->userData,
3521 "except has no content\n");
3522 ctxt->nbErrors++;
3523 }
3524 while (child != NULL) {
3525 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3526 if (tmp2 != NULL) {
3527 if (last2 == NULL) {
3528 except->content = last2 = tmp2;
3529 } else {
3530 last2->next = tmp2;
3531 last2 = tmp2;
3532 }
3533 }
3534 child = child->next;
3535 }
3536 content = content->next;
3537 }
3538 /*
3539 * Check there is no unhandled data
3540 */
3541 if (content != NULL) {
3542 if (ctxt->error != NULL)
3543 ctxt->error(ctxt->userData,
3544 "Element data has unexpected content %s\n", content->name);
3545 ctxt->nbErrors++;
3546 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003547
3548 return(def);
3549}
3550
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003551static const xmlChar *invalidName = BAD_CAST "\1";
3552
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003553/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003554 * xmlRelaxNGCompareNameClasses:
3555 * @defs1: the first element/attribute defs
3556 * @defs2: the second element/attribute defs
3557 * @name: the restriction on the name
3558 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003559 *
3560 * Compare the 2 lists of element definitions. The comparison is
3561 * that if both lists do not accept the same QNames, it returns 1
3562 * If the 2 lists can accept the same QName the comparison returns 0
3563 *
3564 * Returns 1 disttinct, 0 if equal
3565 */
3566static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003567xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3568 xmlRelaxNGDefinePtr def2) {
3569 int ret = 1;
3570 xmlNode node;
3571 xmlNs ns;
3572 xmlRelaxNGValidCtxt ctxt;
3573 ctxt.flags = FLAGS_IGNORABLE;
3574
Daniel Veillard42f12e92003-03-07 18:32:59 +00003575 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3576
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003577 if ((def1->type == XML_RELAXNG_ELEMENT) ||
3578 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3579 if (def2->type == XML_RELAXNG_TEXT)
3580 return(1);
3581 if (def1->name != NULL) {
3582 node.name = def1->name;
3583 } else {
3584 node.name = invalidName;
3585 }
3586 node.ns = &ns;
3587 if (def1->ns != NULL) {
3588 if (def1->ns[0] == 0) {
3589 node.ns = NULL;
3590 } else {
3591 ns.href = def1->ns;
3592 }
3593 } else {
3594 ns.href = invalidName;
3595 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003596 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003597 if (def1->nameClass != NULL) {
3598 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3599 } else {
3600 ret = 0;
3601 }
3602 } else {
3603 ret = 1;
3604 }
3605 } else if (def1->type == XML_RELAXNG_TEXT) {
3606 if (def2->type == XML_RELAXNG_TEXT)
3607 return(0);
3608 return(1);
3609 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003610 TODO
3611 ret = 0;
3612 } else {
3613 TODO
3614 ret = 0;
3615 }
3616 if (ret == 0)
3617 return(ret);
3618 if ((def2->type == XML_RELAXNG_ELEMENT) ||
3619 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3620 if (def2->name != NULL) {
3621 node.name = def2->name;
3622 } else {
3623 node.name = invalidName;
3624 }
3625 node.ns = &ns;
3626 if (def2->ns != NULL) {
3627 if (def2->ns[0] == 0) {
3628 node.ns = NULL;
3629 } else {
3630 ns.href = def2->ns;
3631 }
3632 } else {
3633 ns.href = invalidName;
3634 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003635 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003636 if (def2->nameClass != NULL) {
3637 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3638 } else {
3639 ret = 0;
3640 }
3641 } else {
3642 ret = 1;
3643 }
3644 } else {
3645 TODO
3646 ret = 0;
3647 }
3648
3649 return(ret);
3650}
3651
3652/**
3653 * xmlRelaxNGCompareElemDefLists:
3654 * @ctxt: a Relax-NG parser context
3655 * @defs1: the first list of element/attribute defs
3656 * @defs2: the second list of element/attribute defs
3657 *
3658 * Compare the 2 lists of element or attribute definitions. The comparison
3659 * is that if both lists do not accept the same QNames, it returns 1
3660 * If the 2 lists can accept the same QName the comparison returns 0
3661 *
3662 * Returns 1 disttinct, 0 if equal
3663 */
3664static int
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003665xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3666 xmlRelaxNGDefinePtr *def1,
3667 xmlRelaxNGDefinePtr *def2) {
3668 xmlRelaxNGDefinePtr *basedef2 = def2;
3669
Daniel Veillard154877e2003-01-30 12:17:05 +00003670 if ((def1 == NULL) || (def2 == NULL))
3671 return(1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003672 if ((*def1 == NULL) || (*def2 == NULL))
3673 return(1);
3674 while (*def1 != NULL) {
3675 while ((*def2) != NULL) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003676 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3677 return(0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003678 def2++;
3679 }
3680 def2 = basedef2;
3681 def1++;
3682 }
3683 return(1);
3684}
3685
3686/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003687 * xmlRelaxNGGenerateAttributes:
3688 * @ctxt: a Relax-NG parser context
3689 * @def: the definition definition
3690 *
3691 * Check if the definition can only generate attributes
3692 *
3693 * Returns 1 if yes, 0 if no and -1 in case of error.
3694 */
3695static int
3696xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
3697 xmlRelaxNGDefinePtr def) {
3698 xmlRelaxNGDefinePtr parent, cur, tmp;
3699
3700 /*
3701 * Don't run that check in case of error. Infinite recursion
3702 * becomes possible.
3703 */
3704 if (ctxt->nbErrors != 0)
3705 return(-1);
3706
3707 parent = NULL;
3708 cur = def;
3709 while (cur != NULL) {
3710 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3711 (cur->type == XML_RELAXNG_TEXT) ||
3712 (cur->type == XML_RELAXNG_DATATYPE) ||
3713 (cur->type == XML_RELAXNG_PARAM) ||
3714 (cur->type == XML_RELAXNG_LIST) ||
3715 (cur->type == XML_RELAXNG_VALUE) ||
3716 (cur->type == XML_RELAXNG_EMPTY))
3717 return(0);
3718 if ((cur->type == XML_RELAXNG_CHOICE) ||
3719 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3720 (cur->type == XML_RELAXNG_GROUP) ||
3721 (cur->type == XML_RELAXNG_ONEORMORE) ||
3722 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3723 (cur->type == XML_RELAXNG_OPTIONAL) ||
3724 (cur->type == XML_RELAXNG_PARENTREF) ||
3725 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3726 (cur->type == XML_RELAXNG_REF) ||
3727 (cur->type == XML_RELAXNG_DEF)) {
3728 if (cur->content != NULL) {
3729 parent = cur;
3730 cur = cur->content;
3731 tmp = cur;
3732 while (tmp != NULL) {
3733 tmp->parent = parent;
3734 tmp = tmp->next;
3735 }
3736 continue;
3737 }
3738 }
3739 if (cur == def)
3740 break;
3741 if (cur->next != NULL) {
3742 cur = cur->next;
3743 continue;
3744 }
3745 do {
3746 cur = cur->parent;
3747 if (cur == NULL) break;
3748 if (cur == def) return(1);
3749 if (cur->next != NULL) {
3750 cur = cur->next;
3751 break;
3752 }
3753 } while (cur != NULL);
3754 }
3755 return(1);
3756}
3757
3758/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003759 * xmlRelaxNGGetElements:
3760 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003761 * @def: the definition definition
3762 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003763 *
3764 * Compute the list of top elements a definition can generate
3765 *
3766 * Returns a list of elements or NULL if none was found.
3767 */
3768static xmlRelaxNGDefinePtr *
3769xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003770 xmlRelaxNGDefinePtr def,
3771 int eora) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003772 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003773 int len = 0;
3774 int max = 0;
3775
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003776 /*
3777 * Don't run that check in case of error. Infinite recursion
3778 * becomes possible.
3779 */
3780 if (ctxt->nbErrors != 0)
3781 return(NULL);
3782
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003783 parent = NULL;
3784 cur = def;
3785 while (cur != NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003786 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3787 (cur->type == XML_RELAXNG_TEXT))) ||
3788 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003789 if (ret == NULL) {
3790 max = 10;
3791 ret = (xmlRelaxNGDefinePtr *)
3792 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3793 if (ret == NULL) {
3794 if (ctxt->error != NULL)
3795 ctxt->error(ctxt->userData,
3796 "Out of memory in element search\n");
3797 ctxt->nbErrors++;
3798 return(NULL);
3799 }
3800 } else if (max <= len) {
3801 max *= 2;
3802 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3803 if (ret == NULL) {
3804 if (ctxt->error != NULL)
3805 ctxt->error(ctxt->userData,
3806 "Out of memory in element search\n");
3807 ctxt->nbErrors++;
3808 return(NULL);
3809 }
3810 }
Daniel Veillardb08c9812003-01-28 23:09:49 +00003811 ret[len++] = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003812 ret[len] = NULL;
3813 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3814 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3815 (cur->type == XML_RELAXNG_GROUP) ||
3816 (cur->type == XML_RELAXNG_ONEORMORE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00003817 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3818 (cur->type == XML_RELAXNG_OPTIONAL) ||
Daniel Veillard952379b2003-03-17 15:37:12 +00003819 (cur->type == XML_RELAXNG_PARENTREF) ||
Daniel Veillardb08c9812003-01-28 23:09:49 +00003820 (cur->type == XML_RELAXNG_REF) ||
3821 (cur->type == XML_RELAXNG_DEF)) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003822 /*
3823 * Don't go within elements or attributes or string values.
3824 * Just gather the element top list
3825 */
3826 if (cur->content != NULL) {
3827 parent = cur;
3828 cur = cur->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003829 tmp = cur;
3830 while (tmp != NULL) {
3831 tmp->parent = parent;
3832 tmp = tmp->next;
3833 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003834 continue;
3835 }
3836 }
Daniel Veillard154877e2003-01-30 12:17:05 +00003837 if (cur == def)
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003838 break;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003839 if (cur->next != NULL) {
3840 cur = cur->next;
3841 continue;
3842 }
3843 do {
3844 cur = cur->parent;
3845 if (cur == NULL) break;
3846 if (cur == def) return(ret);
3847 if (cur->next != NULL) {
3848 cur = cur->next;
3849 break;
3850 }
3851 } while (cur != NULL);
3852 }
3853 return(ret);
3854}
3855
3856/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003857 * xmlRelaxNGCheckChoiceDeterminism:
3858 * @ctxt: a Relax-NG parser context
3859 * @def: the choice definition
3860 *
3861 * Also used to find indeterministic pattern in choice
3862 */
3863static void
3864xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
3865 xmlRelaxNGDefinePtr def) {
3866 xmlRelaxNGDefinePtr **list;
3867 xmlRelaxNGDefinePtr cur;
3868 int nbchild = 0, i, j, ret;
3869 int is_nullable = 0;
3870 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003871 xmlHashTablePtr triage = NULL;
3872 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003873
3874 if ((def == NULL) ||
3875 (def->type != XML_RELAXNG_CHOICE))
3876 return;
3877
Daniel Veillarde063f482003-03-21 16:53:17 +00003878 if (def->dflags & IS_PROCESSED)
3879 return;
3880
Daniel Veillardfd573f12003-03-16 17:52:32 +00003881 /*
3882 * Don't run that check in case of error. Infinite recursion
3883 * becomes possible.
3884 */
3885 if (ctxt->nbErrors != 0)
3886 return;
3887
3888 is_nullable = xmlRelaxNGIsNullable(def);
3889
3890 cur = def->content;
3891 while (cur != NULL) {
3892 nbchild++;
3893 cur = cur->next;
3894 }
3895
3896 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3897 sizeof(xmlRelaxNGDefinePtr *));
3898 if (list == NULL) {
3899 if (ctxt->error != NULL)
3900 ctxt->error(ctxt->userData,
3901 "Out of memory in choice computation\n");
3902 ctxt->nbErrors++;
3903 return;
3904 }
3905 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003906 /*
3907 * a bit strong but safe
3908 */
3909 if (is_nullable == 0) {
3910 triage = xmlHashCreate(10);
3911 } else {
3912 is_triable = 0;
3913 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003914 cur = def->content;
3915 while (cur != NULL) {
3916 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
Daniel Veillarde063f482003-03-21 16:53:17 +00003917 if ((list[i] == NULL) || (list[i][0] == NULL)) {
3918 is_triable = 0;
3919 } else if (is_triable == 1) {
3920 xmlRelaxNGDefinePtr *tmp;
3921 int res;
3922
3923 tmp = list[i];
3924 while ((*tmp != NULL) && (is_triable == 1)) {
3925 if ((*tmp)->type == XML_RELAXNG_TEXT) {
3926 res = xmlHashAddEntry2(triage,
3927 BAD_CAST "#text", NULL,
3928 (void *)cur);
3929 if (res != 0)
3930 is_triable = -1;
3931 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
3932 ((*tmp)->name != NULL)) {
3933 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3934 res = xmlHashAddEntry2(triage,
3935 (*tmp)->name, NULL,
3936 (void *)cur);
3937 else
3938 res = xmlHashAddEntry2(triage,
3939 (*tmp)->name, (*tmp)->ns,
3940 (void *)cur);
3941 if (res != 0)
3942 is_triable = -1;
3943 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
3944 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3945 res = xmlHashAddEntry2(triage,
3946 BAD_CAST "#any", NULL,
3947 (void *)cur);
3948 else
3949 res = xmlHashAddEntry2(triage,
3950 BAD_CAST "#any", (*tmp)->ns,
3951 (void *)cur);
3952 if (res != 0)
3953 is_triable = -1;
3954 } else {
3955 is_triable = -1;
3956 }
3957 tmp++;
3958 }
3959 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003960 i++;
3961 cur = cur->next;
3962 }
3963
3964 for (i = 0;i < nbchild;i++) {
3965 if (list[i] == NULL)
3966 continue;
3967 for (j = 0;j < i;j++) {
3968 if (list[j] == NULL)
3969 continue;
3970 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3971 if (ret == 0) {
3972 is_indeterminist = 1;
3973 }
3974 }
3975 }
3976 for (i = 0;i < nbchild;i++) {
3977 if (list[i] != NULL)
3978 xmlFree(list[i]);
3979 }
3980
3981 xmlFree(list);
3982 if (is_indeterminist) {
Daniel Veillarde063f482003-03-21 16:53:17 +00003983 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003984 }
Daniel Veillarde063f482003-03-21 16:53:17 +00003985 if (is_triable == 1) {
3986 def->dflags |= IS_TRIABLE;
3987 def->data = triage;
3988 } else if (triage != NULL) {
3989 xmlHashFree(triage, NULL);
3990 }
3991 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003992}
3993
3994/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003995 * xmlRelaxNGCheckGroupAttrs:
3996 * @ctxt: a Relax-NG parser context
3997 * @def: the group definition
3998 *
3999 * Detects violations of rule 7.3
4000 */
4001static void
4002xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
4003 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004004 xmlRelaxNGDefinePtr **list;
4005 xmlRelaxNGDefinePtr cur;
4006 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004007
4008 if ((def == NULL) ||
4009 ((def->type != XML_RELAXNG_GROUP) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00004010 (def->type != XML_RELAXNG_ELEMENT)))
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004011 return;
4012
Daniel Veillarde063f482003-03-21 16:53:17 +00004013 if (def->dflags & IS_PROCESSED)
4014 return;
4015
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004016 /*
4017 * Don't run that check in case of error. Infinite recursion
4018 * becomes possible.
4019 */
4020 if (ctxt->nbErrors != 0)
4021 return;
4022
Daniel Veillardfd573f12003-03-16 17:52:32 +00004023 cur = def->attrs;
4024 while (cur != NULL) {
4025 nbchild++;
4026 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004027 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004028 cur = def->content;
4029 while (cur != NULL) {
4030 nbchild++;
4031 cur = cur->next;
4032 }
4033
4034 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4035 sizeof(xmlRelaxNGDefinePtr *));
4036 if (list == NULL) {
4037 if (ctxt->error != NULL)
4038 ctxt->error(ctxt->userData,
4039 "Out of memory in group computation\n");
4040 ctxt->nbErrors++;
4041 return;
4042 }
4043 i = 0;
4044 cur = def->attrs;
4045 while (cur != NULL) {
4046 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4047 i++;
4048 cur = cur->next;
4049 }
4050 cur = def->content;
4051 while (cur != NULL) {
4052 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4053 i++;
4054 cur = cur->next;
4055 }
4056
4057 for (i = 0;i < nbchild;i++) {
4058 if (list[i] == NULL)
4059 continue;
4060 for (j = 0;j < i;j++) {
4061 if (list[j] == NULL)
4062 continue;
4063 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4064 if (ret == 0) {
4065 if (ctxt->error != NULL)
4066 ctxt->error(ctxt->userData,
4067 "Attributes conflicts in group\n");
4068 ctxt->nbErrors++;
4069 }
4070 }
4071 }
4072 for (i = 0;i < nbchild;i++) {
4073 if (list[i] != NULL)
4074 xmlFree(list[i]);
4075 }
4076
4077 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004078 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004079}
4080
4081/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004082 * xmlRelaxNGComputeInterleaves:
4083 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004084 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004085 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004086 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004087 * A lot of work for preprocessing interleave definitions
4088 * is potentially needed to get a decent execution speed at runtime
4089 * - trying to get a total order on the element nodes generated
4090 * by the interleaves, order the list of interleave definitions
4091 * following that order.
4092 * - if <text/> is used to handle mixed content, it is better to
4093 * flag this in the define and simplify the runtime checking
4094 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004095 */
4096static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004097xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
4098 xmlRelaxNGParserCtxtPtr ctxt,
4099 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004100 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004101
Daniel Veillardfd573f12003-03-16 17:52:32 +00004102 xmlRelaxNGPartitionPtr partitions = NULL;
4103 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4104 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004105 int i,j,ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004106 int nbgroups = 0;
4107 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004108 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004109 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004110
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004111 /*
4112 * Don't run that check in case of error. Infinite recursion
4113 * becomes possible.
4114 */
4115 if (ctxt->nbErrors != 0)
4116 return;
4117
Daniel Veillardfd573f12003-03-16 17:52:32 +00004118#ifdef DEBUG_INTERLEAVE
4119 xmlGenericError(xmlGenericErrorContext,
4120 "xmlRelaxNGComputeInterleaves(%s)\n",
4121 name);
4122#endif
4123 cur = def->content;
4124 while (cur != NULL) {
4125 nbchild++;
4126 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004127 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004128
4129#ifdef DEBUG_INTERLEAVE
4130 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4131#endif
4132 groups = (xmlRelaxNGInterleaveGroupPtr *)
4133 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
4134 if (groups == NULL)
4135 goto error;
4136 cur = def->content;
4137 while (cur != NULL) {
4138 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4139 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4140 if (groups[nbgroups] == NULL)
4141 goto error;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004142 if (cur->type == XML_RELAXNG_TEXT)
4143 is_mixed++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004144 groups[nbgroups]->rule = cur;
4145 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4146 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4147 nbgroups++;
4148 cur = cur->next;
4149 }
4150#ifdef DEBUG_INTERLEAVE
4151 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4152#endif
4153
4154 /*
4155 * Let's check that all rules makes a partitions according to 7.4
4156 */
4157 partitions = (xmlRelaxNGPartitionPtr)
4158 xmlMalloc(sizeof(xmlRelaxNGPartition));
4159 if (partitions == NULL)
4160 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004161 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004162 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004163 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004164 for (i = 0;i < nbgroups;i++) {
4165 group = groups[i];
4166 for (j = i+1;j < nbgroups;j++) {
4167 if (groups[j] == NULL)
4168 continue;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004169
Daniel Veillardfd573f12003-03-16 17:52:32 +00004170 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4171 groups[j]->defs);
4172 if (ret == 0) {
4173 if (ctxt->error != NULL)
4174 ctxt->error(ctxt->userData,
4175 "Element or text conflicts in interleave\n");
4176 ctxt->nbErrors++;
4177 }
4178 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4179 groups[j]->attrs);
4180 if (ret == 0) {
4181 if (ctxt->error != NULL)
4182 ctxt->error(ctxt->userData,
4183 "Attributes conflicts in interleave\n");
4184 ctxt->nbErrors++;
4185 }
4186 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004187 tmp = group->defs;
4188 if ((tmp != NULL) && (*tmp != NULL)) {
4189 while (*tmp != NULL) {
4190 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4191 res = xmlHashAddEntry2(partitions->triage,
4192 BAD_CAST "#text", NULL,
Daniel Veillard34ba3872003-07-15 13:34:05 +00004193 (void *)(long)(i + 1));
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004194 if (res != 0)
4195 is_determinist = -1;
4196 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4197 ((*tmp)->name != NULL)) {
4198 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4199 res = xmlHashAddEntry2(partitions->triage,
4200 (*tmp)->name, NULL,
Daniel Veillard34ba3872003-07-15 13:34:05 +00004201 (void *)(long)(i + 1));
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004202 else
4203 res = xmlHashAddEntry2(partitions->triage,
4204 (*tmp)->name, (*tmp)->ns,
Daniel Veillard34ba3872003-07-15 13:34:05 +00004205 (void *)(long)(i + 1));
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004206 if (res != 0)
4207 is_determinist = -1;
4208 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4209 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4210 res = xmlHashAddEntry2(partitions->triage,
4211 BAD_CAST "#any", NULL,
Daniel Veillard34ba3872003-07-15 13:34:05 +00004212 (void *)(long)(i + 1));
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004213 else
4214 res = xmlHashAddEntry2(partitions->triage,
4215 BAD_CAST "#any", (*tmp)->ns,
Daniel Veillard34ba3872003-07-15 13:34:05 +00004216 (void *)(long)(i + 1));
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004217 if ((*tmp)->nameClass != NULL)
4218 is_determinist = 2;
4219 if (res != 0)
4220 is_determinist = -1;
4221 } else {
4222 is_determinist = -1;
4223 }
4224 tmp++;
4225 }
4226 } else {
4227 is_determinist = 0;
4228 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004229 }
4230 partitions->groups = groups;
4231
4232 /*
4233 * and save the partition list back in the def
4234 */
4235 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004236 if (is_mixed != 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00004237 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004238 if (is_determinist == 1)
4239 partitions->flags = IS_DETERMINIST;
4240 if (is_determinist == 2)
4241 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004242 return;
4243
4244error:
4245 if (ctxt->error != NULL)
4246 ctxt->error(ctxt->userData,
4247 "Out of memory in interleave computation\n");
4248 ctxt->nbErrors++;
4249 if (groups != NULL) {
4250 for (i = 0;i < nbgroups;i++)
4251 if (groups[i] != NULL) {
4252 if (groups[i]->defs != NULL)
4253 xmlFree(groups[i]->defs);
4254 xmlFree(groups[i]);
4255 }
4256 xmlFree(groups);
4257 }
4258 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004259}
4260
4261/**
4262 * xmlRelaxNGParseInterleave:
4263 * @ctxt: a Relax-NG parser context
4264 * @node: the data node.
4265 *
4266 * parse the content of a RelaxNG interleave node.
4267 *
4268 * Returns the definition pointer or NULL in case of error
4269 */
4270static xmlRelaxNGDefinePtr
4271xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4272 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004273 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004274 xmlNodePtr child;
4275
Daniel Veillardfd573f12003-03-16 17:52:32 +00004276 def = xmlRelaxNGNewDefine(ctxt, node);
4277 if (def == NULL) {
4278 return(NULL);
4279 }
4280 def->type = XML_RELAXNG_INTERLEAVE;
4281
4282 if (ctxt->interleaves == NULL)
4283 ctxt->interleaves = xmlHashCreate(10);
4284 if (ctxt->interleaves == NULL) {
4285 if (ctxt->error != NULL)
4286 ctxt->error(ctxt->userData,
4287 "Failed to create interleaves hash table\n");
4288 ctxt->nbErrors++;
4289 } else {
4290 char name[32];
4291
4292 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4293 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4294 if (ctxt->error != NULL)
4295 ctxt->error(ctxt->userData,
4296 "Failed to add %s to hash table\n", name);
4297 ctxt->nbErrors++;
4298 }
4299 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004300 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004301 if (child == NULL) {
4302 if (ctxt->error != NULL)
4303 ctxt->error(ctxt->userData, "Element interleave is empty\n");
4304 ctxt->nbErrors++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004305 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004306 while (child != NULL) {
4307 if (IS_RELAXNG(child, "element")) {
4308 cur = xmlRelaxNGParseElement(ctxt, child);
4309 } else {
4310 cur = xmlRelaxNGParsePattern(ctxt, child);
4311 }
4312 if (cur != NULL) {
4313 cur->parent = def;
4314 if (last == NULL) {
4315 def->content = last = cur;
4316 } else {
4317 last->next = cur;
4318 last = cur;
4319 }
4320 }
4321 child = child->next;
4322 }
4323
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004324 return(def);
4325}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004326
4327/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004328 * xmlRelaxNGParseInclude:
4329 * @ctxt: a Relax-NG parser context
4330 * @node: the include node
4331 *
4332 * Integrate the content of an include node in the current grammar
4333 *
4334 * Returns 0 in case of success or -1 in case of error
4335 */
4336static int
4337xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4338 xmlRelaxNGIncludePtr incl;
4339 xmlNodePtr root;
4340 int ret = 0, tmp;
4341
4342 incl = node->_private;
4343 if (incl == NULL) {
4344 if (ctxt->error != NULL)
4345 ctxt->error(ctxt->userData,
4346 "Include node has no data\n");
4347 ctxt->nbErrors++;
4348 return(-1);
4349 }
4350 root = xmlDocGetRootElement(incl->doc);
4351 if (root == NULL) {
4352 if (ctxt->error != NULL)
4353 ctxt->error(ctxt->userData,
4354 "Include document is empty\n");
4355 ctxt->nbErrors++;
4356 return(-1);
4357 }
4358 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
4359 if (ctxt->error != NULL)
4360 ctxt->error(ctxt->userData,
4361 "Include document root is not a grammar\n");
4362 ctxt->nbErrors++;
4363 return(-1);
4364 }
4365
4366 /*
4367 * Merge the definition from both the include and the internal list
4368 */
4369 if (root->children != NULL) {
4370 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4371 if (tmp != 0)
4372 ret = -1;
4373 }
4374 if (node->children != NULL) {
4375 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4376 if (tmp != 0)
4377 ret = -1;
4378 }
4379 return(ret);
4380}
4381
4382/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004383 * xmlRelaxNGParseDefine:
4384 * @ctxt: a Relax-NG parser context
4385 * @node: the define node
4386 *
4387 * parse the content of a RelaxNG define element node.
4388 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004389 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004390 */
4391static int
4392xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4393 xmlChar *name;
4394 int ret = 0, tmp;
4395 xmlRelaxNGDefinePtr def;
4396 const xmlChar *olddefine;
4397
4398 name = xmlGetProp(node, BAD_CAST "name");
4399 if (name == NULL) {
4400 if (ctxt->error != NULL)
4401 ctxt->error(ctxt->userData,
4402 "define has no name\n");
4403 ctxt->nbErrors++;
4404 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004405 xmlRelaxNGNormExtSpace(name);
4406 if (xmlValidateNCName(name, 0)) {
4407 if (ctxt->error != NULL)
4408 ctxt->error(ctxt->userData,
4409 "define name '%s' is not an NCName\n",
4410 name);
4411 ctxt->nbErrors++;
4412 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004413 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004414 if (def == NULL) {
4415 xmlFree(name);
4416 return(-1);
4417 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004418 def->type = XML_RELAXNG_DEF;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004419 def->name = name;
4420 if (node->children == NULL) {
4421 if (ctxt->error != NULL)
4422 ctxt->error(ctxt->userData,
4423 "define has no children\n");
4424 ctxt->nbErrors++;
4425 } else {
4426 olddefine = ctxt->define;
4427 ctxt->define = name;
Daniel Veillard154877e2003-01-30 12:17:05 +00004428 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004429 ctxt->define = olddefine;
4430 }
4431 if (ctxt->grammar->defs == NULL)
4432 ctxt->grammar->defs = xmlHashCreate(10);
4433 if (ctxt->grammar->defs == NULL) {
4434 if (ctxt->error != NULL)
4435 ctxt->error(ctxt->userData,
4436 "Could not create definition hash\n");
4437 ctxt->nbErrors++;
4438 ret = -1;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004439 } else {
4440 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4441 if (tmp < 0) {
Daniel Veillard154877e2003-01-30 12:17:05 +00004442 xmlRelaxNGDefinePtr prev;
4443
4444 prev = xmlHashLookup(ctxt->grammar->defs, name);
4445 if (prev == NULL) {
4446 if (ctxt->error != NULL)
4447 ctxt->error(ctxt->userData,
4448 "Internal error on define aggregation of %s\n",
4449 name);
4450 ctxt->nbErrors++;
4451 ret = -1;
Daniel Veillard154877e2003-01-30 12:17:05 +00004452 } else {
4453 while (prev->nextHash != NULL)
4454 prev = prev->nextHash;
4455 prev->nextHash = def;
4456 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004457 }
4458 }
4459 }
4460 return(ret);
4461}
4462
4463/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004464 * xmlRelaxNGProcessExternalRef:
4465 * @ctxt: the parser context
4466 * @node: the externlRef node
4467 *
4468 * Process and compile an externlRef node
4469 *
4470 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4471 */
4472static xmlRelaxNGDefinePtr
4473xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4474 xmlRelaxNGDocumentPtr docu;
4475 xmlNodePtr root, tmp;
4476 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004477 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004478 xmlRelaxNGDefinePtr def;
4479
4480 docu = node->_private;
4481 if (docu != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004482 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004483 if (def == NULL)
4484 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004485 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004486
4487 if (docu->content == NULL) {
4488 /*
4489 * Then do the parsing for good
4490 */
4491 root = xmlDocGetRootElement(docu->doc);
4492 if (root == NULL) {
4493 if (ctxt->error != NULL)
4494 ctxt->error(ctxt->userData,
4495 "xmlRelaxNGParse: %s is empty\n",
4496 ctxt->URL);
4497 ctxt->nbErrors++;
4498 return (NULL);
4499 }
4500 /*
4501 * ns transmission rules
4502 */
4503 ns = xmlGetProp(root, BAD_CAST "ns");
4504 if (ns == NULL) {
4505 tmp = node;
4506 while ((tmp != NULL) &&
4507 (tmp->type == XML_ELEMENT_NODE)) {
4508 ns = xmlGetProp(tmp, BAD_CAST "ns");
4509 if (ns != NULL) {
4510 break;
4511 }
4512 tmp = tmp->parent;
4513 }
4514 if (ns != NULL) {
4515 xmlSetProp(root, BAD_CAST "ns", ns);
4516 newNs = 1;
4517 xmlFree(ns);
4518 }
4519 } else {
4520 xmlFree(ns);
4521 }
4522
4523 /*
4524 * Parsing to get a precompiled schemas.
4525 */
Daniel Veillard77648bb2003-02-20 15:03:22 +00004526 oldflags = ctxt->flags;
4527 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004528 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard77648bb2003-02-20 15:03:22 +00004529 ctxt->flags = oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004530 if ((docu->schema != NULL) &&
4531 (docu->schema->topgrammar != NULL)) {
4532 docu->content = docu->schema->topgrammar->start;
4533 }
4534
4535 /*
4536 * the externalRef may be reused in a different ns context
4537 */
4538 if (newNs == 1) {
4539 xmlUnsetProp(root, BAD_CAST "ns");
4540 }
4541 }
4542 def->content = docu->content;
4543 } else {
4544 def = NULL;
4545 }
4546 return(def);
4547}
4548
4549/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004550 * xmlRelaxNGParsePattern:
4551 * @ctxt: a Relax-NG parser context
4552 * @node: the pattern node.
4553 *
4554 * parse the content of a RelaxNG pattern node.
4555 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004556 * Returns the definition pointer or NULL in case of error or if no
4557 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004558 */
4559static xmlRelaxNGDefinePtr
4560xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4561 xmlRelaxNGDefinePtr def = NULL;
4562
Daniel Veillardd2298792003-02-14 16:54:11 +00004563 if (node == NULL) {
4564 return(NULL);
4565 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004566 if (IS_RELAXNG(node, "element")) {
4567 def = xmlRelaxNGParseElement(ctxt, node);
4568 } else if (IS_RELAXNG(node, "attribute")) {
4569 def = xmlRelaxNGParseAttribute(ctxt, node);
4570 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004571 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004572 if (def == NULL)
4573 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004574 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00004575 if (node->children != NULL) {
4576 if (ctxt->error != NULL)
4577 ctxt->error(ctxt->userData, "empty: had a child node\n");
4578 ctxt->nbErrors++;
4579 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004580 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004581 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004582 if (def == NULL)
4583 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004584 def->type = XML_RELAXNG_TEXT;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004585 if (node->children != NULL) {
4586 if (ctxt->error != NULL)
4587 ctxt->error(ctxt->userData, "text: had a child node\n");
4588 ctxt->nbErrors++;
4589 }
4590 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004591 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004592 if (def == NULL)
4593 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004594 def->type = XML_RELAXNG_ZEROORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004595 if (node->children == NULL) {
4596 if (ctxt->error != NULL)
4597 ctxt->error(ctxt->userData,
4598 "Element %s is empty\n", node->name);
4599 ctxt->nbErrors++;
4600 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004601 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
Daniel Veillardd2298792003-02-14 16:54:11 +00004602 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004603 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004604 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004605 if (def == NULL)
4606 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004607 def->type = XML_RELAXNG_ONEORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004608 if (node->children == NULL) {
4609 if (ctxt->error != NULL)
4610 ctxt->error(ctxt->userData,
4611 "Element %s is empty\n", node->name);
4612 ctxt->nbErrors++;
4613 } else {
4614 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4615 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004616 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004617 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004618 if (def == NULL)
4619 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004620 def->type = XML_RELAXNG_OPTIONAL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004621 if (node->children == NULL) {
4622 if (ctxt->error != NULL)
4623 ctxt->error(ctxt->userData,
4624 "Element %s is empty\n", node->name);
4625 ctxt->nbErrors++;
4626 } else {
4627 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4628 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004629 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004630 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004631 if (def == NULL)
4632 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004633 def->type = XML_RELAXNG_CHOICE;
4634 if (node->children == NULL) {
4635 if (ctxt->error != NULL)
4636 ctxt->error(ctxt->userData,
4637 "Element %s is empty\n", node->name);
4638 ctxt->nbErrors++;
4639 } else {
4640 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4641 }
4642 } else if (IS_RELAXNG(node, "group")) {
4643 def = xmlRelaxNGNewDefine(ctxt, node);
4644 if (def == NULL)
4645 return(NULL);
4646 def->type = XML_RELAXNG_GROUP;
4647 if (node->children == NULL) {
4648 if (ctxt->error != NULL)
4649 ctxt->error(ctxt->userData,
4650 "Element %s is empty\n", node->name);
4651 ctxt->nbErrors++;
4652 } else {
4653 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4654 }
4655 } else if (IS_RELAXNG(node, "ref")) {
4656 def = xmlRelaxNGNewDefine(ctxt, node);
4657 if (def == NULL)
4658 return(NULL);
4659 def->type = XML_RELAXNG_REF;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004660 def->name = xmlGetProp(node, BAD_CAST "name");
4661 if (def->name == NULL) {
4662 if (ctxt->error != NULL)
4663 ctxt->error(ctxt->userData,
4664 "ref has no name\n");
4665 ctxt->nbErrors++;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004666 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004667 xmlRelaxNGNormExtSpace(def->name);
4668 if (xmlValidateNCName(def->name, 0)) {
4669 if (ctxt->error != NULL)
4670 ctxt->error(ctxt->userData,
4671 "ref name '%s' is not an NCName\n",
4672 def->name);
4673 ctxt->nbErrors++;
4674 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004675 }
4676 if (node->children != NULL) {
4677 if (ctxt->error != NULL)
4678 ctxt->error(ctxt->userData,
4679 "ref is not empty\n");
4680 ctxt->nbErrors++;
4681 }
4682 if (ctxt->grammar->refs == NULL)
4683 ctxt->grammar->refs = xmlHashCreate(10);
4684 if (ctxt->grammar->refs == NULL) {
4685 if (ctxt->error != NULL)
4686 ctxt->error(ctxt->userData,
4687 "Could not create references hash\n");
4688 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004689 def = NULL;
4690 } else {
4691 int tmp;
4692
4693 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4694 if (tmp < 0) {
4695 xmlRelaxNGDefinePtr prev;
4696
4697 prev = (xmlRelaxNGDefinePtr)
4698 xmlHashLookup(ctxt->grammar->refs, def->name);
4699 if (prev == NULL) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004700 if (def->name != NULL) {
4701 if (ctxt->error != NULL)
4702 ctxt->error(ctxt->userData,
4703 "Error refs definitions '%s'\n",
4704 def->name);
4705 } else {
4706 if (ctxt->error != NULL)
4707 ctxt->error(ctxt->userData,
4708 "Error refs definitions\n");
4709 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004710 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004711 def = NULL;
4712 } else {
4713 def->nextHash = prev->nextHash;
4714 prev->nextHash = def;
4715 }
4716 }
4717 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004718 } else if (IS_RELAXNG(node, "data")) {
4719 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004720 } else if (IS_RELAXNG(node, "value")) {
4721 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004722 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004723 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004724 if (def == NULL)
4725 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004726 def->type = XML_RELAXNG_LIST;
Daniel Veillardd2298792003-02-14 16:54:11 +00004727 if (node->children == NULL) {
4728 if (ctxt->error != NULL)
4729 ctxt->error(ctxt->userData,
4730 "Element %s is empty\n", node->name);
4731 ctxt->nbErrors++;
4732 } else {
4733 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4734 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004735 } else if (IS_RELAXNG(node, "interleave")) {
4736 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004737 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004738 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004739 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004740 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004741 if (def == NULL)
4742 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004743 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004744 if (node->children != NULL) {
4745 if (ctxt->error != NULL)
4746 ctxt->error(ctxt->userData,
4747 "xmlRelaxNGParse: notAllowed element is not empty\n");
4748 ctxt->nbErrors++;
4749 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004750 } else if (IS_RELAXNG(node, "grammar")) {
4751 xmlRelaxNGGrammarPtr grammar, old;
4752 xmlRelaxNGGrammarPtr oldparent;
4753
Daniel Veillardc482e262003-02-26 14:48:48 +00004754#ifdef DEBUG_GRAMMAR
4755 xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n");
4756#endif
4757
Daniel Veillard419a7682003-02-03 23:22:49 +00004758 oldparent = ctxt->parentgrammar;
4759 old = ctxt->grammar;
4760 ctxt->parentgrammar = old;
4761 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4762 if (old != NULL) {
4763 ctxt->grammar = old;
4764 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004765#if 0
Daniel Veillard419a7682003-02-03 23:22:49 +00004766 if (grammar != NULL) {
4767 grammar->next = old->next;
4768 old->next = grammar;
4769 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004770#endif
Daniel Veillard419a7682003-02-03 23:22:49 +00004771 }
4772 if (grammar != NULL)
4773 def = grammar->start;
4774 else
4775 def = NULL;
4776 } else if (IS_RELAXNG(node, "parentRef")) {
4777 if (ctxt->parentgrammar == NULL) {
4778 if (ctxt->error != NULL)
4779 ctxt->error(ctxt->userData,
4780 "Use of parentRef without a parent grammar\n");
4781 ctxt->nbErrors++;
4782 return(NULL);
4783 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004784 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard419a7682003-02-03 23:22:49 +00004785 if (def == NULL)
4786 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004787 def->type = XML_RELAXNG_PARENTREF;
Daniel Veillard419a7682003-02-03 23:22:49 +00004788 def->name = xmlGetProp(node, BAD_CAST "name");
4789 if (def->name == NULL) {
4790 if (ctxt->error != NULL)
4791 ctxt->error(ctxt->userData,
4792 "parentRef has no name\n");
4793 ctxt->nbErrors++;
Daniel Veillardd2298792003-02-14 16:54:11 +00004794 } else {
4795 xmlRelaxNGNormExtSpace(def->name);
4796 if (xmlValidateNCName(def->name, 0)) {
4797 if (ctxt->error != NULL)
4798 ctxt->error(ctxt->userData,
4799 "parentRef name '%s' is not an NCName\n",
4800 def->name);
4801 ctxt->nbErrors++;
4802 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004803 }
4804 if (node->children != NULL) {
4805 if (ctxt->error != NULL)
4806 ctxt->error(ctxt->userData,
4807 "parentRef is not empty\n");
4808 ctxt->nbErrors++;
4809 }
4810 if (ctxt->parentgrammar->refs == NULL)
4811 ctxt->parentgrammar->refs = xmlHashCreate(10);
4812 if (ctxt->parentgrammar->refs == NULL) {
4813 if (ctxt->error != NULL)
4814 ctxt->error(ctxt->userData,
4815 "Could not create references hash\n");
4816 ctxt->nbErrors++;
4817 def = NULL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004818 } else if (def->name != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +00004819 int tmp;
4820
4821 tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4822 if (tmp < 0) {
4823 xmlRelaxNGDefinePtr prev;
4824
4825 prev = (xmlRelaxNGDefinePtr)
4826 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4827 if (prev == NULL) {
4828 if (ctxt->error != NULL)
4829 ctxt->error(ctxt->userData,
4830 "Internal error parentRef definitions '%s'\n",
4831 def->name);
4832 ctxt->nbErrors++;
4833 def = NULL;
4834 } else {
4835 def->nextHash = prev->nextHash;
4836 prev->nextHash = def;
4837 }
4838 }
4839 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004840 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004841 if (node->children == NULL) {
4842 if (ctxt->error != NULL)
4843 ctxt->error(ctxt->userData,
4844 "Mixed is empty\n");
4845 ctxt->nbErrors++;
4846 def = NULL;
4847 } else {
4848 def = xmlRelaxNGParseInterleave(ctxt, node);
4849 if (def != NULL) {
4850 xmlRelaxNGDefinePtr tmp;
4851
4852 if ((def->content != NULL) && (def->content->next != NULL)) {
4853 tmp = xmlRelaxNGNewDefine(ctxt, node);
4854 if (tmp != NULL) {
4855 tmp->type = XML_RELAXNG_GROUP;
4856 tmp->content = def->content;
4857 def->content = tmp;
4858 }
4859 }
4860
4861 tmp = xmlRelaxNGNewDefine(ctxt, node);
4862 if (tmp == NULL)
4863 return(def);
4864 tmp->type = XML_RELAXNG_TEXT;
4865 tmp->next = def->content;
4866 def->content = tmp;
4867 }
4868 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004869 } else {
4870 if (ctxt->error != NULL)
4871 ctxt->error(ctxt->userData,
4872 "Unexpected node %s is not a pattern\n",
4873 node->name);
4874 ctxt->nbErrors++;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004875 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004876 }
4877 return(def);
4878}
4879
4880/**
4881 * xmlRelaxNGParseAttribute:
4882 * @ctxt: a Relax-NG parser context
4883 * @node: the element node
4884 *
4885 * parse the content of a RelaxNG attribute node.
4886 *
4887 * Returns the definition pointer or NULL in case of error.
4888 */
4889static xmlRelaxNGDefinePtr
4890xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillardd2298792003-02-14 16:54:11 +00004891 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004892 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004893 int old_flags;
4894
Daniel Veillardfd573f12003-03-16 17:52:32 +00004895 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004896 if (ret == NULL)
4897 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004898 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004899 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004900 child = node->children;
4901 if (child == NULL) {
4902 if (ctxt->error != NULL)
4903 ctxt->error(ctxt->userData,
4904 "xmlRelaxNGParseattribute: attribute has no children\n");
4905 ctxt->nbErrors++;
4906 return(ret);
4907 }
4908 old_flags = ctxt->flags;
4909 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004910 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4911 if (cur != NULL)
4912 child = child->next;
4913
Daniel Veillardd2298792003-02-14 16:54:11 +00004914 if (child != NULL) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00004915 cur = xmlRelaxNGParsePattern(ctxt, child);
4916 if (cur != NULL) {
4917 switch (cur->type) {
4918 case XML_RELAXNG_EMPTY:
4919 case XML_RELAXNG_NOT_ALLOWED:
4920 case XML_RELAXNG_TEXT:
4921 case XML_RELAXNG_ELEMENT:
4922 case XML_RELAXNG_DATATYPE:
4923 case XML_RELAXNG_VALUE:
4924 case XML_RELAXNG_LIST:
4925 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00004926 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004927 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004928 case XML_RELAXNG_DEF:
4929 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004930 case XML_RELAXNG_ZEROORMORE:
4931 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004932 case XML_RELAXNG_CHOICE:
4933 case XML_RELAXNG_GROUP:
4934 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard77648bb2003-02-20 15:03:22 +00004935 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardd2298792003-02-14 16:54:11 +00004936 ret->content = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004937 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004938 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004939 case XML_RELAXNG_START:
Daniel Veillard8fe98712003-02-19 00:19:14 +00004940 case XML_RELAXNG_PARAM:
Daniel Veillard144fae12003-02-03 13:17:57 +00004941 case XML_RELAXNG_EXCEPT:
Daniel Veillardd2298792003-02-14 16:54:11 +00004942 if (ctxt->error != NULL)
4943 ctxt->error(ctxt->userData,
4944 "attribute has invalid content\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004945 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004946 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004947 case XML_RELAXNG_NOOP:
Daniel Veillard77648bb2003-02-20 15:03:22 +00004948 if (ctxt->error != NULL)
4949 ctxt->error(ctxt->userData,
Daniel Veillardac297932003-04-17 12:55:35 +00004950 "RNG Internal error, noop found in attribute\n");
Daniel Veillard77648bb2003-02-20 15:03:22 +00004951 ctxt->nbErrors++;
4952 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004953 }
4954 }
4955 child = child->next;
4956 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004957 if (child != NULL) {
4958 if (ctxt->error != NULL)
4959 ctxt->error(ctxt->userData, "attribute has multiple children\n");
4960 ctxt->nbErrors++;
4961 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004962 ctxt->flags = old_flags;
4963 return(ret);
4964}
4965
4966/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004967 * xmlRelaxNGParseExceptNameClass:
4968 * @ctxt: a Relax-NG parser context
4969 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00004970 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004971 *
4972 * parse the content of a RelaxNG nameClass node.
4973 *
4974 * Returns the definition pointer or NULL in case of error.
4975 */
4976static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00004977xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
4978 xmlNodePtr node, int attr) {
4979 xmlRelaxNGDefinePtr ret, cur, last = NULL;
4980 xmlNodePtr child;
4981
Daniel Veillardd2298792003-02-14 16:54:11 +00004982 if (!IS_RELAXNG(node, "except")) {
4983 if (ctxt->error != NULL)
4984 ctxt->error(ctxt->userData,
4985 "Expecting an except node\n");
4986 ctxt->nbErrors++;
Daniel Veillard144fae12003-02-03 13:17:57 +00004987 return(NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004988 }
4989 if (node->next != NULL) {
4990 if (ctxt->error != NULL)
4991 ctxt->error(ctxt->userData,
4992 "exceptNameClass allows only a single except node\n");
4993 ctxt->nbErrors++;
4994 }
Daniel Veillard144fae12003-02-03 13:17:57 +00004995 if (node->children == NULL) {
4996 if (ctxt->error != NULL)
4997 ctxt->error(ctxt->userData,
4998 "except has no content\n");
4999 ctxt->nbErrors++;
5000 return(NULL);
5001 }
5002
Daniel Veillardfd573f12003-03-16 17:52:32 +00005003 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005004 if (ret == NULL)
5005 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005006 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005007 child = node->children;
5008 while (child != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005009 cur = xmlRelaxNGNewDefine(ctxt, child);
Daniel Veillard144fae12003-02-03 13:17:57 +00005010 if (cur == NULL)
5011 break;
5012 if (attr)
5013 cur->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005014 else
5015 cur->type = XML_RELAXNG_ELEMENT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005016
Daniel Veillard419a7682003-02-03 23:22:49 +00005017 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard144fae12003-02-03 13:17:57 +00005018 if (last == NULL) {
5019 ret->content = cur;
5020 } else {
5021 last->next = cur;
5022 }
5023 last = cur;
5024 }
5025 child = child->next;
5026 }
5027
5028 return(ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005029}
5030
5031/**
5032 * xmlRelaxNGParseNameClass:
5033 * @ctxt: a Relax-NG parser context
5034 * @node: the nameClass node
5035 * @def: the current definition
5036 *
5037 * parse the content of a RelaxNG nameClass node.
5038 *
5039 * Returns the definition pointer or NULL in case of error.
5040 */
5041static xmlRelaxNGDefinePtr
5042xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
5043 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005044 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005045 xmlChar *val;
5046
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005047 ret = def;
5048 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
5049 (IS_RELAXNG(node, "nsName"))) {
5050 if ((def->type != XML_RELAXNG_ELEMENT) &&
5051 (def->type != XML_RELAXNG_ATTRIBUTE)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005052 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005053 if (ret == NULL)
5054 return(NULL);
5055 ret->parent = def;
5056 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5057 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005058 else
5059 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005060 }
5061 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005062 if (IS_RELAXNG(node, "name")) {
5063 val = xmlNodeGetContent(node);
Daniel Veillardd2298792003-02-14 16:54:11 +00005064 xmlRelaxNGNormExtSpace(val);
5065 if (xmlValidateNCName(val, 0)) {
5066 if (ctxt->error != NULL) {
5067 if (node->parent != NULL)
5068 ctxt->error(ctxt->userData,
5069 "Element %s name '%s' is not an NCName\n",
5070 node->parent->name, val);
5071 else
5072 ctxt->error(ctxt->userData,
5073 "name '%s' is not an NCName\n",
5074 val);
5075 }
5076 ctxt->nbErrors++;
5077 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005078 ret->name = val;
5079 val = xmlGetProp(node, BAD_CAST "ns");
5080 ret->ns = val;
Daniel Veillard416589a2003-02-17 17:25:42 +00005081 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5082 (val != NULL) &&
5083 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5084 ctxt->error(ctxt->userData,
5085 "Attribute with namespace '%s' is not allowed\n",
5086 val);
5087 ctxt->nbErrors++;
5088 }
5089 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5090 (val != NULL) &&
5091 (val[0] == 0) &&
5092 (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
5093 ctxt->error(ctxt->userData,
5094 "Attribute with QName 'xmlns' is not allowed\n",
5095 val);
5096 ctxt->nbErrors++;
5097 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005098 } else if (IS_RELAXNG(node, "anyName")) {
5099 ret->name = NULL;
5100 ret->ns = NULL;
5101 if (node->children != NULL) {
5102 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00005103 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5104 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005105 }
5106 } else if (IS_RELAXNG(node, "nsName")) {
5107 ret->name = NULL;
5108 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5109 if (ret->ns == NULL) {
5110 if (ctxt->error != NULL)
5111 ctxt->error(ctxt->userData,
5112 "nsName has no ns attribute\n");
5113 ctxt->nbErrors++;
5114 }
Daniel Veillard416589a2003-02-17 17:25:42 +00005115 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5116 (ret->ns != NULL) &&
5117 (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5118 ctxt->error(ctxt->userData,
5119 "Attribute with namespace '%s' is not allowed\n",
5120 ret->ns);
5121 ctxt->nbErrors++;
5122 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005123 if (node->children != NULL) {
5124 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00005125 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5126 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005127 }
5128 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005129 xmlNodePtr child;
5130 xmlRelaxNGDefinePtr last = NULL;
5131
5132 ret = xmlRelaxNGNewDefine(ctxt, node);
5133 if (ret == NULL)
5134 return(NULL);
5135 ret->parent = def;
5136 ret->type = XML_RELAXNG_CHOICE;
5137
Daniel Veillardd2298792003-02-14 16:54:11 +00005138 if (node->children == NULL) {
5139 if (ctxt->error != NULL)
5140 ctxt->error(ctxt->userData,
5141 "Element choice is empty\n");
5142 ctxt->nbErrors++;
5143 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005144
5145 child = node->children;
5146 while (child != NULL) {
5147 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5148 if (tmp != NULL) {
5149 if (last == NULL) {
5150 last = ret->nameClass = tmp;
5151 } else {
5152 last->next = tmp;
5153 last = tmp;
5154 }
5155 }
5156 child = child->next;
5157 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005158 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005159 } else {
5160 if (ctxt->error != NULL)
5161 ctxt->error(ctxt->userData,
Daniel Veillardfd573f12003-03-16 17:52:32 +00005162 "expecting name, anyName, nsName or choice : got %s\n",
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005163 node->name);
5164 ctxt->nbErrors++;
5165 return(NULL);
5166 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005167 if (ret != def) {
5168 if (def->nameClass == NULL) {
5169 def->nameClass = ret;
5170 } else {
5171 tmp = def->nameClass;
5172 while (tmp->next != NULL) {
5173 tmp = tmp->next;
5174 }
5175 tmp->next = ret;
5176 }
5177 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005178 return(ret);
5179}
5180
5181/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005182 * xmlRelaxNGParseElement:
5183 * @ctxt: a Relax-NG parser context
5184 * @node: the element node
5185 *
5186 * parse the content of a RelaxNG element node.
5187 *
5188 * Returns the definition pointer or NULL in case of error.
5189 */
5190static xmlRelaxNGDefinePtr
5191xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
5192 xmlRelaxNGDefinePtr ret, cur, last;
5193 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005194 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005195
Daniel Veillardfd573f12003-03-16 17:52:32 +00005196 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005197 if (ret == NULL)
5198 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005199 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005200 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005201 child = node->children;
5202 if (child == NULL) {
5203 if (ctxt->error != NULL)
5204 ctxt->error(ctxt->userData,
5205 "xmlRelaxNGParseElement: element has no children\n");
5206 ctxt->nbErrors++;
5207 return(ret);
5208 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005209 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5210 if (cur != NULL)
5211 child = child->next;
5212
Daniel Veillard6eadf632003-01-23 18:29:16 +00005213 if (child == NULL) {
5214 if (ctxt->error != NULL)
5215 ctxt->error(ctxt->userData,
5216 "xmlRelaxNGParseElement: element has no content\n");
5217 ctxt->nbErrors++;
5218 return(ret);
5219 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005220 olddefine = ctxt->define;
5221 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005222 last = NULL;
5223 while (child != NULL) {
5224 cur = xmlRelaxNGParsePattern(ctxt, child);
5225 if (cur != NULL) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005226 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005227 switch (cur->type) {
5228 case XML_RELAXNG_EMPTY:
5229 case XML_RELAXNG_NOT_ALLOWED:
5230 case XML_RELAXNG_TEXT:
5231 case XML_RELAXNG_ELEMENT:
5232 case XML_RELAXNG_DATATYPE:
5233 case XML_RELAXNG_VALUE:
5234 case XML_RELAXNG_LIST:
5235 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00005236 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005237 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00005238 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00005239 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00005240 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00005241 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00005242 case XML_RELAXNG_CHOICE:
5243 case XML_RELAXNG_GROUP:
5244 case XML_RELAXNG_INTERLEAVE:
5245 if (last == NULL) {
5246 ret->content = last = cur;
5247 } else {
5248 if ((last->type == XML_RELAXNG_ELEMENT) &&
5249 (ret->content == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005250 ret->content = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005251 if (ret->content != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005252 ret->content->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005253 ret->content->content = last;
5254 } else {
5255 ret->content = last;
5256 }
5257 }
5258 last->next = cur;
5259 last = cur;
5260 }
5261 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005262 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardce192eb2003-04-16 15:58:05 +00005263 /* HERE !!! */
Daniel Veillardfd573f12003-03-16 17:52:32 +00005264 cur->next = ret->attrs;
5265 ret->attrs = cur;
5266 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005267 case XML_RELAXNG_START:
Daniel Veillardac297932003-04-17 12:55:35 +00005268 if (ctxt->error != NULL)
5269 ctxt->error(ctxt->userData,
5270 "RNG Internal error, start found in element\n");
5271 ctxt->nbErrors++;
5272 break;
Daniel Veillard8fe98712003-02-19 00:19:14 +00005273 case XML_RELAXNG_PARAM:
Daniel Veillardac297932003-04-17 12:55:35 +00005274 if (ctxt->error != NULL)
5275 ctxt->error(ctxt->userData,
5276 "RNG Internal error, param found in element\n");
5277 ctxt->nbErrors++;
5278 break;
Daniel Veillard144fae12003-02-03 13:17:57 +00005279 case XML_RELAXNG_EXCEPT:
Daniel Veillardac297932003-04-17 12:55:35 +00005280 if (ctxt->error != NULL)
5281 ctxt->error(ctxt->userData,
5282 "RNG Internal error, except found in element\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005283 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005284 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00005285 case XML_RELAXNG_NOOP:
Daniel Veillard77648bb2003-02-20 15:03:22 +00005286 if (ctxt->error != NULL)
5287 ctxt->error(ctxt->userData,
Daniel Veillardac297932003-04-17 12:55:35 +00005288 "RNG Internal error, noop found in element\n");
Daniel Veillard77648bb2003-02-20 15:03:22 +00005289 ctxt->nbErrors++;
5290 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005291 }
5292 }
5293 child = child->next;
5294 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005295 ctxt->define = olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005296 return(ret);
5297}
5298
5299/**
5300 * xmlRelaxNGParsePatterns:
5301 * @ctxt: a Relax-NG parser context
5302 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005303 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005304 *
5305 * parse the content of a RelaxNG start node.
5306 *
5307 * Returns the definition pointer or NULL in case of error.
5308 */
5309static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005310xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
5311 int group) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005312 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005313
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005314 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005315 while (nodes != NULL) {
5316 if (IS_RELAXNG(nodes, "element")) {
5317 cur = xmlRelaxNGParseElement(ctxt, nodes);
5318 if (def == NULL) {
5319 def = last = cur;
5320 } else {
Daniel Veillard154877e2003-01-30 12:17:05 +00005321 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5322 (def == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005323 def = xmlRelaxNGNewDefine(ctxt, nodes);
5324 def->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005325 def->content = last;
5326 }
5327 last->next = cur;
5328 last = cur;
5329 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005330 cur->parent = parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005331 } else {
5332 cur = xmlRelaxNGParsePattern(ctxt, nodes);
Daniel Veillard419a7682003-02-03 23:22:49 +00005333 if (cur != NULL) {
5334 if (def == NULL) {
5335 def = last = cur;
5336 } else {
5337 last->next = cur;
5338 last = cur;
5339 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005340 }
5341 }
5342 nodes = nodes->next;
5343 }
5344 return(def);
5345}
5346
5347/**
5348 * xmlRelaxNGParseStart:
5349 * @ctxt: a Relax-NG parser context
5350 * @nodes: start children nodes
5351 *
5352 * parse the content of a RelaxNG start node.
5353 *
5354 * Returns 0 in case of success, -1 in case of error
5355 */
5356static int
5357xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
5358 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005359 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005360
Daniel Veillardd2298792003-02-14 16:54:11 +00005361 if (nodes == NULL) {
5362 if (ctxt->error != NULL)
5363 ctxt->error(ctxt->userData,
5364 "start has no children\n");
5365 ctxt->nbErrors++;
5366 return(-1);
5367 }
5368 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005369 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00005370 if (def == NULL)
5371 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005372 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00005373 if (nodes->children != NULL) {
5374 if (ctxt->error != NULL)
5375 ctxt->error(ctxt->userData, "element empty is not empty\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005376 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005377 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005378 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005379 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00005380 if (def == NULL)
5381 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005382 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardd2298792003-02-14 16:54:11 +00005383 if (nodes->children != NULL) {
5384 if (ctxt->error != NULL)
5385 ctxt->error(ctxt->userData,
5386 "element notAllowed is not empty\n");
5387 ctxt->nbErrors++;
5388 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005389 } else {
5390 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005391 }
5392 if (ctxt->grammar->start != NULL) {
5393 last = ctxt->grammar->start;
5394 while (last->next != NULL)
5395 last = last->next;
5396 last->next = def;
5397 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00005398 ctxt->grammar->start = def;
5399 }
5400 nodes = nodes->next;
5401 if (nodes != NULL) {
5402 if (ctxt->error != NULL)
5403 ctxt->error(ctxt->userData,
5404 "start more than one children\n");
5405 ctxt->nbErrors++;
5406 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005407 }
5408 return(ret);
5409}
5410
5411/**
5412 * xmlRelaxNGParseGrammarContent:
5413 * @ctxt: a Relax-NG parser context
5414 * @nodes: grammar children nodes
5415 *
5416 * parse the content of a RelaxNG grammar node.
5417 *
5418 * Returns 0 in case of success, -1 in case of error
5419 */
5420static int
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005421xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005422{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005423 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005424
5425 if (nodes == NULL) {
5426 if (ctxt->error != NULL)
5427 ctxt->error(ctxt->userData,
5428 "grammar has no children\n");
5429 ctxt->nbErrors++;
5430 return(-1);
5431 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005432 while (nodes != NULL) {
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005433 if (IS_RELAXNG(nodes, "start")) {
5434 if (nodes->children == NULL) {
5435 if (ctxt->error != NULL)
5436 ctxt->error(ctxt->userData,
Daniel Veillardd2298792003-02-14 16:54:11 +00005437 "start has no children\n");
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005438 ctxt->nbErrors++;
5439 } else {
5440 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5441 if (tmp != 0)
5442 ret = -1;
5443 }
5444 } else if (IS_RELAXNG(nodes, "define")) {
5445 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5446 if (tmp != 0)
5447 ret = -1;
5448 } else if (IS_RELAXNG(nodes, "include")) {
5449 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5450 if (tmp != 0)
5451 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005452 } else {
5453 if (ctxt->error != NULL)
5454 ctxt->error(ctxt->userData,
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005455 "grammar has unexpected child %s\n", nodes->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005456 ctxt->nbErrors++;
5457 ret = -1;
5458 }
5459 nodes = nodes->next;
5460 }
5461 return (ret);
5462}
5463
5464/**
5465 * xmlRelaxNGCheckReference:
5466 * @ref: the ref
5467 * @ctxt: a Relax-NG parser context
5468 * @name: the name associated to the defines
5469 *
5470 * Applies the 4.17. combine attribute rule for all the define
5471 * element of a given grammar using the same name.
5472 */
5473static void
5474xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
5475 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5476 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005477 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005478
5479 grammar = ctxt->grammar;
5480 if (grammar == NULL) {
5481 if (ctxt->error != NULL)
5482 ctxt->error(ctxt->userData,
5483 "Internal error: no grammar in CheckReference %s\n",
5484 name);
5485 ctxt->nbErrors++;
5486 return;
5487 }
5488 if (ref->content != NULL) {
5489 if (ctxt->error != NULL)
5490 ctxt->error(ctxt->userData,
5491 "Internal error: reference has content in CheckReference %s\n",
5492 name);
5493 ctxt->nbErrors++;
5494 return;
5495 }
5496 if (grammar->defs != NULL) {
5497 def = xmlHashLookup(grammar->defs, name);
5498 if (def != NULL) {
Daniel Veillard276be4a2003-01-24 01:03:34 +00005499 cur = ref;
5500 while (cur != NULL) {
5501 cur->content = def;
5502 cur = cur->nextHash;
5503 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005504 } else {
Daniel Veillardd4310742003-02-18 21:12:46 +00005505 if (ctxt->error != NULL)
5506 ctxt->error(ctxt->userData,
5507 "Reference %s has no matching definition\n",
5508 name);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005509 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005510 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005511 } else {
5512 if (ctxt->error != NULL)
5513 ctxt->error(ctxt->userData,
5514 "Reference %s has no matching definition\n",
5515 name);
5516 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005517 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005518}
5519
5520/**
5521 * xmlRelaxNGCheckCombine:
5522 * @define: the define(s) list
5523 * @ctxt: a Relax-NG parser context
5524 * @name: the name associated to the defines
5525 *
5526 * Applies the 4.17. combine attribute rule for all the define
5527 * element of a given grammar using the same name.
5528 */
5529static void
5530xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
5531 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5532 xmlChar *combine;
5533 int choiceOrInterleave = -1;
5534 int missing = 0;
5535 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5536
5537 if (define->nextHash == NULL)
5538 return;
5539 cur = define;
5540 while (cur != NULL) {
5541 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5542 if (combine != NULL) {
5543 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5544 if (choiceOrInterleave == -1)
5545 choiceOrInterleave = 1;
5546 else if (choiceOrInterleave == 0) {
5547 if (ctxt->error != NULL)
5548 ctxt->error(ctxt->userData,
5549 "Defines for %s use both 'choice' and 'interleave'\n",
5550 name);
5551 ctxt->nbErrors++;
5552 }
Daniel Veillard154877e2003-01-30 12:17:05 +00005553 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005554 if (choiceOrInterleave == -1)
5555 choiceOrInterleave = 0;
5556 else if (choiceOrInterleave == 1) {
5557 if (ctxt->error != NULL)
5558 ctxt->error(ctxt->userData,
5559 "Defines for %s use both 'choice' and 'interleave'\n",
5560 name);
5561 ctxt->nbErrors++;
5562 }
5563 } else {
5564 if (ctxt->error != NULL)
5565 ctxt->error(ctxt->userData,
5566 "Defines for %s use unknown combine value '%s''\n",
5567 name, combine);
5568 ctxt->nbErrors++;
5569 }
5570 xmlFree(combine);
5571 } else {
5572 if (missing == 0)
5573 missing = 1;
5574 else {
5575 if (ctxt->error != NULL)
5576 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005577 "Some defines for %s needs the combine attribute\n",
Daniel Veillard6eadf632003-01-23 18:29:16 +00005578 name);
5579 ctxt->nbErrors++;
5580 }
5581 }
5582
5583 cur = cur->nextHash;
5584 }
5585#ifdef DEBUG
5586 xmlGenericError(xmlGenericErrorContext,
5587 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5588 name, choiceOrInterleave);
5589#endif
5590 if (choiceOrInterleave == -1)
5591 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005592 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005593 if (cur == NULL)
5594 return;
5595 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005596 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005597 else
5598 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005599 tmp = define;
5600 last = NULL;
5601 while (tmp != NULL) {
5602 if (tmp->content != NULL) {
5603 if (tmp->content->next != NULL) {
5604 /*
5605 * we need first to create a wrapper.
5606 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00005607 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005608 if (tmp2 == NULL)
5609 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005610 tmp2->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005611 tmp2->content = tmp->content;
5612 } else {
5613 tmp2 = tmp->content;
5614 }
5615 if (last == NULL) {
5616 cur->content = tmp2;
5617 } else {
5618 last->next = tmp2;
5619 }
5620 last = tmp2;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005621 }
Daniel Veillard952379b2003-03-17 15:37:12 +00005622 tmp->content = cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005623 tmp = tmp->nextHash;
5624 }
5625 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005626 if (choiceOrInterleave == 0) {
5627 if (ctxt->interleaves == NULL)
5628 ctxt->interleaves = xmlHashCreate(10);
5629 if (ctxt->interleaves == NULL) {
5630 if (ctxt->error != NULL)
5631 ctxt->error(ctxt->userData,
5632 "Failed to create interleaves hash table\n");
5633 ctxt->nbErrors++;
5634 } else {
5635 char tmpname[32];
5636
5637 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5638 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5639 if (ctxt->error != NULL)
5640 ctxt->error(ctxt->userData,
5641 "Failed to add %s to hash table\n", tmpname);
5642 ctxt->nbErrors++;
5643 }
5644 }
5645 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005646}
5647
5648/**
5649 * xmlRelaxNGCombineStart:
5650 * @ctxt: a Relax-NG parser context
5651 * @grammar: the grammar
5652 *
5653 * Applies the 4.17. combine rule for all the start
5654 * element of a given grammar.
5655 */
5656static void
5657xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5658 xmlRelaxNGGrammarPtr grammar) {
5659 xmlRelaxNGDefinePtr starts;
5660 xmlChar *combine;
5661 int choiceOrInterleave = -1;
5662 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005663 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005664
Daniel Veillard2df2de22003-02-17 23:34:33 +00005665 starts = grammar->start;
5666 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard6eadf632003-01-23 18:29:16 +00005667 return;
5668 cur = starts;
5669 while (cur != NULL) {
Daniel Veillard2df2de22003-02-17 23:34:33 +00005670 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5671 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5672 combine = NULL;
5673 if (ctxt->error != NULL)
5674 ctxt->error(ctxt->userData,
5675 "Internal error: start element not found\n");
5676 ctxt->nbErrors++;
5677 } else {
5678 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5679 }
5680
Daniel Veillard6eadf632003-01-23 18:29:16 +00005681 if (combine != NULL) {
5682 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5683 if (choiceOrInterleave == -1)
5684 choiceOrInterleave = 1;
5685 else if (choiceOrInterleave == 0) {
5686 if (ctxt->error != NULL)
5687 ctxt->error(ctxt->userData,
5688 "<start> use both 'choice' and 'interleave'\n");
5689 ctxt->nbErrors++;
5690 }
Daniel Veillard2df2de22003-02-17 23:34:33 +00005691 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005692 if (choiceOrInterleave == -1)
5693 choiceOrInterleave = 0;
5694 else if (choiceOrInterleave == 1) {
5695 if (ctxt->error != NULL)
5696 ctxt->error(ctxt->userData,
5697 "<start> use both 'choice' and 'interleave'\n");
5698 ctxt->nbErrors++;
5699 }
5700 } else {
5701 if (ctxt->error != NULL)
5702 ctxt->error(ctxt->userData,
5703 "<start> uses unknown combine value '%s''\n", combine);
5704 ctxt->nbErrors++;
5705 }
5706 xmlFree(combine);
5707 } else {
5708 if (missing == 0)
5709 missing = 1;
5710 else {
5711 if (ctxt->error != NULL)
5712 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005713 "Some <start> element miss the combine attribute\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00005714 ctxt->nbErrors++;
5715 }
5716 }
5717
Daniel Veillard2df2de22003-02-17 23:34:33 +00005718 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005719 }
5720#ifdef DEBUG
5721 xmlGenericError(xmlGenericErrorContext,
5722 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5723 choiceOrInterleave);
5724#endif
5725 if (choiceOrInterleave == -1)
5726 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005727 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005728 if (cur == NULL)
5729 return;
5730 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005731 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005732 else
5733 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005734 cur->content = grammar->start;
5735 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005736 if (choiceOrInterleave == 0) {
5737 if (ctxt->interleaves == NULL)
5738 ctxt->interleaves = xmlHashCreate(10);
5739 if (ctxt->interleaves == NULL) {
5740 if (ctxt->error != NULL)
5741 ctxt->error(ctxt->userData,
5742 "Failed to create interleaves hash table\n");
5743 ctxt->nbErrors++;
5744 } else {
5745 char tmpname[32];
5746
5747 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5748 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5749 if (ctxt->error != NULL)
5750 ctxt->error(ctxt->userData,
5751 "Failed to add %s to hash table\n", tmpname);
5752 ctxt->nbErrors++;
5753 }
5754 }
5755 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005756}
5757
5758/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005759 * xmlRelaxNGCheckCycles:
5760 * @ctxt: a Relax-NG parser context
5761 * @nodes: grammar children nodes
5762 * @depth: the counter
5763 *
5764 * Check for cycles.
5765 *
5766 * Returns 0 if check passed, and -1 in case of error
5767 */
5768static int
5769xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5770 xmlRelaxNGDefinePtr cur, int depth) {
5771 int ret = 0;
5772
5773 while ((ret == 0) && (cur != NULL)) {
5774 if ((cur->type == XML_RELAXNG_REF) ||
5775 (cur->type == XML_RELAXNG_PARENTREF)) {
5776 if (cur->depth == -1) {
5777 cur->depth = depth;
5778 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5779 cur->depth = -2;
5780 } else if (depth == cur->depth) {
5781 if (ctxt->error != NULL)
5782 ctxt->error(ctxt->userData,
5783 "Detected a cycle in %s references\n", cur->name);
5784 ctxt->nbErrors++;
5785 return(-1);
5786 }
5787 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5788 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5789 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005790 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
Daniel Veillardd4310742003-02-18 21:12:46 +00005791 }
5792 cur = cur->next;
5793 }
5794 return(ret);
5795}
5796
5797/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005798 * xmlRelaxNGTryUnlink:
5799 * @ctxt: a Relax-NG parser context
5800 * @cur: the definition to unlink
5801 * @parent: the parent definition
5802 * @prev: the previous sibling definition
5803 *
5804 * Try to unlink a definition. If not possble make it a NOOP
5805 *
5806 * Returns the new prev definition
5807 */
5808static xmlRelaxNGDefinePtr
5809xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5810 xmlRelaxNGDefinePtr cur,
5811 xmlRelaxNGDefinePtr parent,
5812 xmlRelaxNGDefinePtr prev) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005813 if (prev != NULL) {
5814 prev->next = cur->next;
5815 } else {
5816 if (parent != NULL) {
5817 if (parent->content == cur)
5818 parent->content = cur->next;
5819 else if (parent->attrs == cur)
5820 parent->attrs = cur->next;
5821 else if (parent->nameClass == cur)
5822 parent->nameClass = cur->next;
5823 } else {
5824 cur->type = XML_RELAXNG_NOOP;
5825 prev = cur;
5826 }
5827 }
5828 return(prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005829}
5830
5831/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005832 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005833 * @ctxt: a Relax-NG parser context
5834 * @nodes: grammar children nodes
5835 *
5836 * Check for simplification of empty and notAllowed
5837 */
5838static void
5839xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5840 xmlRelaxNGDefinePtr cur,
5841 xmlRelaxNGDefinePtr parent) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005842 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005843
Daniel Veillardfd573f12003-03-16 17:52:32 +00005844 while (cur != NULL) {
5845 if ((cur->type == XML_RELAXNG_REF) ||
5846 (cur->type == XML_RELAXNG_PARENTREF)) {
5847 if (cur->depth != -3) {
5848 cur->depth = -3;
5849 xmlRelaxNGSimplify(ctxt, cur->content, cur);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005850 }
5851 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005852 cur->parent = parent;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005853 if ((parent != NULL) &&
5854 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5855 (parent->type == XML_RELAXNG_LIST) ||
5856 (parent->type == XML_RELAXNG_GROUP) ||
5857 (parent->type == XML_RELAXNG_INTERLEAVE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00005858 (parent->type == XML_RELAXNG_ONEORMORE) ||
5859 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005860 parent->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005861 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005862 }
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005863 if ((parent != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00005864 (parent->type == XML_RELAXNG_CHOICE)) {
5865 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5866 } else
5867 prev = cur;
5868 } else if (cur->type == XML_RELAXNG_EMPTY){
5869 cur->parent = parent;
5870 if ((parent != NULL) &&
5871 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5872 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005873 parent->type = XML_RELAXNG_EMPTY;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005874 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005875 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005876 if ((parent != NULL) &&
5877 ((parent->type == XML_RELAXNG_GROUP) ||
5878 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5879 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5880 } else
5881 prev = cur;
5882 } else {
5883 cur->parent = parent;
5884 if (cur->content != NULL)
5885 xmlRelaxNGSimplify(ctxt, cur->content, cur);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005886 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
Daniel Veillardfd573f12003-03-16 17:52:32 +00005887 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5888 if (cur->nameClass != NULL)
5889 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5890 /*
Daniel Veillardce192eb2003-04-16 15:58:05 +00005891 * On Elements, try to move attribute only generating rules on
5892 * the attrs rules.
5893 */
5894 if (cur->type == XML_RELAXNG_ELEMENT) {
5895 int attronly;
5896 xmlRelaxNGDefinePtr tmp, pre;
5897
5898 while (cur->content != NULL) {
5899 attronly = xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5900 if (attronly == 1) {
5901 /*
5902 * migrate cur->content to attrs
5903 */
5904 tmp = cur->content;
5905 cur->content = tmp->next;
5906 tmp->next = cur->attrs;
5907 cur->attrs = tmp;
5908 } else {
5909 /*
5910 * cur->content can generate elements or text
5911 */
5912 break;
5913 }
5914 }
5915 pre = cur->content;
5916 while ((pre != NULL) && (pre->next != NULL)) {
5917 tmp = pre->next;
5918 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5919 if (attronly == 1) {
5920 /*
5921 * migrate tmp to attrs
5922 */
5923 pre->next = tmp->next;
5924 tmp->next = cur->attrs;
5925 cur->attrs = tmp;
5926 } else {
5927 pre = tmp;
5928 }
5929 }
5930 }
5931 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00005932 * This may result in a simplification
5933 */
5934 if ((cur->type == XML_RELAXNG_GROUP) ||
5935 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5936 if (cur->content == NULL)
5937 cur->type = XML_RELAXNG_EMPTY;
5938 else if (cur->content->next == NULL) {
5939 if ((parent == NULL) && (prev == NULL)) {
5940 cur->type = XML_RELAXNG_NOOP;
5941 } else if (prev == NULL) {
5942 parent->content = cur->content;
5943 cur->content->next = cur->next;
5944 cur = cur->content;
5945 } else {
5946 cur->content->next = cur->next;
5947 prev->next = cur->content;
5948 cur = cur->content;
5949 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005950 }
5951 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005952 /*
5953 * the current node may have been transformed back
5954 */
5955 if ((cur->type == XML_RELAXNG_EXCEPT) &&
5956 (cur->content != NULL) &&
5957 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
5958 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5959 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5960 if ((parent != NULL) &&
5961 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5962 (parent->type == XML_RELAXNG_LIST) ||
5963 (parent->type == XML_RELAXNG_GROUP) ||
5964 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5965 (parent->type == XML_RELAXNG_ONEORMORE) ||
5966 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5967 parent->type = XML_RELAXNG_NOT_ALLOWED;
5968 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005969 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005970 if ((parent != NULL) &&
5971 (parent->type == XML_RELAXNG_CHOICE)) {
5972 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5973 } else
5974 prev = cur;
5975 } else if (cur->type == XML_RELAXNG_EMPTY){
5976 if ((parent != NULL) &&
5977 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5978 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5979 parent->type = XML_RELAXNG_EMPTY;
5980 break;
5981 }
5982 if ((parent != NULL) &&
5983 ((parent->type == XML_RELAXNG_GROUP) ||
5984 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5985 (parent->type == XML_RELAXNG_CHOICE))) {
5986 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5987 } else
5988 prev = cur;
5989 } else {
5990 prev = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005991 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005992 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005993 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005994 }
5995}
5996
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005997/**
5998 * xmlRelaxNGGroupContentType:
5999 * @ct1: the first content type
6000 * @ct2: the second content type
6001 *
6002 * Try to group 2 content types
6003 *
6004 * Returns the content type
6005 */
6006static xmlRelaxNGContentType
6007xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
6008 xmlRelaxNGContentType ct2) {
6009 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6010 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6011 return(XML_RELAXNG_CONTENT_ERROR);
6012 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
6013 return(ct2);
6014 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
6015 return(ct1);
6016 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
6017 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6018 return(XML_RELAXNG_CONTENT_COMPLEX);
6019 return(XML_RELAXNG_CONTENT_ERROR);
6020}
6021
6022/**
6023 * xmlRelaxNGMaxContentType:
6024 * @ct1: the first content type
6025 * @ct2: the second content type
6026 *
6027 * Compute the max content-type
6028 *
6029 * Returns the content type
6030 */
6031static xmlRelaxNGContentType
6032xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
6033 xmlRelaxNGContentType ct2) {
6034 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6035 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6036 return(XML_RELAXNG_CONTENT_ERROR);
6037 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
6038 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6039 return(XML_RELAXNG_CONTENT_SIMPLE);
6040 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
6041 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6042 return(XML_RELAXNG_CONTENT_COMPLEX);
6043 return(XML_RELAXNG_CONTENT_EMPTY);
6044}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006045
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006046/**
6047 * xmlRelaxNGCheckRules:
6048 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006049 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006050 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006051 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006052 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006053 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006054 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006055 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006056 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006057static xmlRelaxNGContentType
Daniel Veillardfd573f12003-03-16 17:52:32 +00006058xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6059 xmlRelaxNGDefinePtr cur, int flags,
6060 xmlRelaxNGType ptype) {
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006061 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006062 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006063
Daniel Veillardfd573f12003-03-16 17:52:32 +00006064 while (cur != NULL) {
6065 ret = XML_RELAXNG_CONTENT_EMPTY;
6066 if ((cur->type == XML_RELAXNG_REF) ||
6067 (cur->type == XML_RELAXNG_PARENTREF)) {
6068 if (flags & XML_RELAXNG_IN_LIST) {
6069 if (ctxt->error != NULL)
6070 ctxt->error(ctxt->userData,
6071 "Found forbidden pattern list//ref\n");
6072 ctxt->nbErrors++;
6073 }
6074 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6075 if (ctxt->error != NULL)
6076 ctxt->error(ctxt->userData,
6077 "Found forbidden pattern data/except//ref\n");
6078 ctxt->nbErrors++;
6079 }
6080 if (cur->depth > -4) {
6081 cur->depth = -4;
6082 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6083 flags, cur->type);
6084 cur->depth = ret - 15 ;
6085 } else if (cur->depth == -4) {
6086 ret = XML_RELAXNG_CONTENT_COMPLEX;
6087 } else {
William M. Brack7b9154b2003-09-27 19:23:50 +00006088 ret = (xmlRelaxNGContentType) (cur->depth + 15);
Daniel Veillardfd573f12003-03-16 17:52:32 +00006089 }
6090 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6091 /*
6092 * The 7.3 Attribute derivation rule for groups is plugged there
6093 */
6094 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6095 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6096 if (ctxt->error != NULL)
6097 ctxt->error(ctxt->userData,
6098 "Found forbidden pattern data/except//element(ref)\n");
6099 ctxt->nbErrors++;
6100 }
6101 if (flags & XML_RELAXNG_IN_LIST) {
6102 if (ctxt->error != NULL)
6103 ctxt->error(ctxt->userData,
6104 "Found forbidden pattern list//element(ref)\n");
6105 ctxt->nbErrors++;
6106 }
6107 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6108 if (ctxt->error != NULL)
6109 ctxt->error(ctxt->userData,
6110 "Found forbidden pattern attribute//element(ref)\n");
6111 ctxt->nbErrors++;
6112 }
6113 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6114 if (ctxt->error != NULL)
6115 ctxt->error(ctxt->userData,
Daniel Veillard463a5472003-02-27 21:30:32 +00006116 "Found forbidden pattern attribute//element(ref)\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006117 ctxt->nbErrors++;
6118 }
6119 /*
6120 * reset since in the simple form elements are only child
6121 * of grammar/define
6122 */
6123 nflags = 0;
6124 ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6125 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6126 if (ctxt->error != NULL)
6127 ctxt->error(ctxt->userData,
6128 "Element %s attributes have a content type error\n",
6129 cur->name);
6130 ctxt->nbErrors++;
6131 }
6132 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6133 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6134 if (ctxt->error != NULL)
6135 ctxt->error(ctxt->userData,
6136 "Element %s has a content type error\n",
6137 cur->name);
6138 ctxt->nbErrors++;
6139 } else {
6140 ret = XML_RELAXNG_CONTENT_COMPLEX;
6141 }
6142 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6143 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6144 if (ctxt->error != NULL)
6145 ctxt->error(ctxt->userData,
6146 "Found forbidden pattern attribute//attribute\n");
6147 ctxt->nbErrors++;
6148 }
6149 if (flags & XML_RELAXNG_IN_LIST) {
6150 if (ctxt->error != NULL)
6151 ctxt->error(ctxt->userData,
6152 "Found forbidden pattern list//attribute\n");
6153 ctxt->nbErrors++;
6154 }
6155 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6156 if (ctxt->error != NULL)
6157 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00006158 "Found forbidden pattern oneOrMore//group//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006159 ctxt->nbErrors++;
6160 }
6161 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6162 if (ctxt->error != NULL)
6163 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00006164 "Found forbidden pattern oneOrMore//interleave//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006165 ctxt->nbErrors++;
6166 }
6167 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6168 if (ctxt->error != NULL)
6169 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00006170 "Found forbidden pattern data/except//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006171 ctxt->nbErrors++;
6172 }
6173 if (flags & XML_RELAXNG_IN_START) {
6174 if (ctxt->error != NULL)
6175 ctxt->error(ctxt->userData,
6176 "Found forbidden pattern start//attribute\n");
6177 ctxt->nbErrors++;
6178 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00006179 if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) {
6180 if (cur->ns == NULL) {
6181 if (ctxt->error != NULL)
6182 ctxt->error(ctxt->userData,
6183 "Found anyName attribute without oneOrMore ancestor\n");
6184 ctxt->nbErrors++;
6185 } else {
6186 if (ctxt->error != NULL)
6187 ctxt->error(ctxt->userData,
6188 "Found nsName attribute without oneOrMore ancestor\n");
6189 ctxt->nbErrors++;
6190 }
Daniel Veillard77648bb2003-02-20 15:03:22 +00006191 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006192 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6193 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6194 ret = XML_RELAXNG_CONTENT_EMPTY;
6195 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6196 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6197 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6198 if (ctxt->error != NULL)
6199 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00006200 "Found forbidden pattern data/except//oneOrMore\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006201 ctxt->nbErrors++;
6202 }
6203 if (flags & XML_RELAXNG_IN_START) {
6204 if (ctxt->error != NULL)
6205 ctxt->error(ctxt->userData,
6206 "Found forbidden pattern start//oneOrMore\n");
6207 ctxt->nbErrors++;
6208 }
6209 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6210 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6211 ret = xmlRelaxNGGroupContentType(ret, ret);
6212 } else if (cur->type == XML_RELAXNG_LIST) {
6213 if (flags & XML_RELAXNG_IN_LIST) {
6214 if (ctxt->error != NULL)
6215 ctxt->error(ctxt->userData,
6216 "Found forbidden pattern list//list\n");
6217 ctxt->nbErrors++;
6218 }
6219 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6220 if (ctxt->error != NULL)
6221 ctxt->error(ctxt->userData,
6222 "Found forbidden pattern data/except//list\n");
6223 ctxt->nbErrors++;
6224 }
6225 if (flags & XML_RELAXNG_IN_START) {
6226 if (ctxt->error != NULL)
6227 ctxt->error(ctxt->userData,
6228 "Found forbidden pattern start//list\n");
6229 ctxt->nbErrors++;
6230 }
6231 nflags = flags | XML_RELAXNG_IN_LIST;
6232 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6233 } else if (cur->type == XML_RELAXNG_GROUP) {
6234 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6235 if (ctxt->error != NULL)
6236 ctxt->error(ctxt->userData,
6237 "Found forbidden pattern data/except//group\n");
6238 ctxt->nbErrors++;
6239 }
6240 if (flags & XML_RELAXNG_IN_START) {
6241 if (ctxt->error != NULL)
6242 ctxt->error(ctxt->userData,
6243 "Found forbidden pattern start//group\n");
6244 ctxt->nbErrors++;
6245 }
6246 if (flags & XML_RELAXNG_IN_ONEORMORE)
6247 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6248 else
6249 nflags = flags;
6250 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6251 /*
6252 * The 7.3 Attribute derivation rule for groups is plugged there
6253 */
6254 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6255 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6256 if (flags & XML_RELAXNG_IN_LIST) {
6257 if (ctxt->error != NULL)
6258 ctxt->error(ctxt->userData,
6259 "Found forbidden pattern list//interleave\n");
6260 ctxt->nbErrors++;
6261 }
6262 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6263 if (ctxt->error != NULL)
6264 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00006265 "Found forbidden pattern data/except//interleave\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006266 ctxt->nbErrors++;
6267 }
6268 if (flags & XML_RELAXNG_IN_START) {
6269 if (ctxt->error != NULL)
6270 ctxt->error(ctxt->userData,
6271 "Found forbidden pattern start//interleave\n");
6272 ctxt->nbErrors++;
6273 }
6274 if (flags & XML_RELAXNG_IN_ONEORMORE)
6275 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6276 else
6277 nflags = flags;
6278 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6279 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6280 if ((cur->parent != NULL) &&
6281 (cur->parent->type == XML_RELAXNG_DATATYPE))
6282 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6283 else
6284 nflags = flags;
6285 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6286 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6287 if (flags & XML_RELAXNG_IN_START) {
6288 if (ctxt->error != NULL)
6289 ctxt->error(ctxt->userData,
6290 "Found forbidden pattern start//data\n");
6291 ctxt->nbErrors++;
6292 }
6293 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6294 ret = XML_RELAXNG_CONTENT_SIMPLE;
6295 } else if (cur->type == XML_RELAXNG_VALUE) {
6296 if (flags & XML_RELAXNG_IN_START) {
6297 if (ctxt->error != NULL)
6298 ctxt->error(ctxt->userData,
6299 "Found forbidden pattern start//value\n");
6300 ctxt->nbErrors++;
6301 }
6302 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6303 ret = XML_RELAXNG_CONTENT_SIMPLE;
6304 } else if (cur->type == XML_RELAXNG_TEXT) {
6305 if (flags & XML_RELAXNG_IN_LIST) {
6306 if (ctxt->error != NULL)
6307 ctxt->error(ctxt->userData,
6308 "Found forbidden pattern list//text\n");
6309 ctxt->nbErrors++;
6310 }
6311 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6312 if (ctxt->error != NULL)
6313 ctxt->error(ctxt->userData,
6314 "Found forbidden pattern data/except//text\n");
6315 ctxt->nbErrors++;
6316 }
6317 if (flags & XML_RELAXNG_IN_START) {
6318 if (ctxt->error != NULL)
6319 ctxt->error(ctxt->userData,
6320 "Found forbidden pattern start//text\n");
6321 ctxt->nbErrors++;
6322 }
6323 ret = XML_RELAXNG_CONTENT_COMPLEX;
6324 } else if (cur->type == XML_RELAXNG_EMPTY) {
6325 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6326 if (ctxt->error != NULL)
6327 ctxt->error(ctxt->userData,
6328 "Found forbidden pattern data/except//empty\n");
6329 ctxt->nbErrors++;
6330 }
6331 if (flags & XML_RELAXNG_IN_START) {
6332 if (ctxt->error != NULL)
6333 ctxt->error(ctxt->userData,
6334 "Found forbidden pattern start//empty\n");
6335 ctxt->nbErrors++;
6336 }
6337 ret = XML_RELAXNG_CONTENT_EMPTY;
6338 } else if (cur->type == XML_RELAXNG_CHOICE) {
6339 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6340 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6341 } else {
6342 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6343 }
6344 cur = cur->next;
6345 if (ptype == XML_RELAXNG_GROUP) {
6346 val = xmlRelaxNGGroupContentType(val, ret);
6347 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6348 tmp = xmlRelaxNGGroupContentType(val, ret);
6349 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6350 tmp = xmlRelaxNGMaxContentType(val, ret);
6351 } else if (ptype == XML_RELAXNG_CHOICE) {
6352 val = xmlRelaxNGMaxContentType(val, ret);
6353 } else if (ptype == XML_RELAXNG_LIST) {
6354 val = XML_RELAXNG_CONTENT_SIMPLE;
6355 } else if (ptype == XML_RELAXNG_EXCEPT) {
6356 if (ret == XML_RELAXNG_CONTENT_ERROR)
6357 val = XML_RELAXNG_CONTENT_ERROR;
6358 else
6359 val = XML_RELAXNG_CONTENT_SIMPLE;
6360 } else {
6361 val = xmlRelaxNGGroupContentType(val, ret);
6362 }
6363
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006364 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006365 return(val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006366}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006367
6368/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006369 * xmlRelaxNGParseGrammar:
6370 * @ctxt: a Relax-NG parser context
6371 * @nodes: grammar children nodes
6372 *
6373 * parse a Relax-NG <grammar> node
6374 *
6375 * Returns the internal xmlRelaxNGGrammarPtr built or
6376 * NULL in case of error
6377 */
6378static xmlRelaxNGGrammarPtr
6379xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
6380 xmlRelaxNGGrammarPtr ret, tmp, old;
6381
Daniel Veillardc482e262003-02-26 14:48:48 +00006382#ifdef DEBUG_GRAMMAR
6383 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6384#endif
6385
Daniel Veillard6eadf632003-01-23 18:29:16 +00006386 ret = xmlRelaxNGNewGrammar(ctxt);
6387 if (ret == NULL)
6388 return(NULL);
6389
6390 /*
6391 * Link the new grammar in the tree
6392 */
6393 ret->parent = ctxt->grammar;
6394 if (ctxt->grammar != NULL) {
6395 tmp = ctxt->grammar->children;
6396 if (tmp == NULL) {
6397 ctxt->grammar->children = ret;
6398 } else {
6399 while (tmp->next != NULL)
6400 tmp = tmp->next;
6401 tmp->next = ret;
6402 }
6403 }
6404
6405 old = ctxt->grammar;
6406 ctxt->grammar = ret;
6407 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6408 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006409 if (ctxt->grammar == NULL) {
6410 if (ctxt->error != NULL)
6411 ctxt->error(ctxt->userData,
6412 "Failed to parse <grammar> content\n");
6413 ctxt->nbErrors++;
6414 } else if (ctxt->grammar->start == NULL) {
6415 if (ctxt->error != NULL)
6416 ctxt->error(ctxt->userData,
6417 "Element <grammar> has no <start>\n");
6418 ctxt->nbErrors++;
6419 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006420
6421 /*
6422 * Apply 4.17 mergingd rules to defines and starts
6423 */
6424 xmlRelaxNGCombineStart(ctxt, ret);
6425 if (ret->defs != NULL) {
6426 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6427 ctxt);
6428 }
6429
6430 /*
6431 * link together defines and refs in this grammar
6432 */
6433 if (ret->refs != NULL) {
6434 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6435 ctxt);
6436 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006437
Daniel Veillard6eadf632003-01-23 18:29:16 +00006438 ctxt->grammar = old;
6439 return(ret);
6440}
6441
6442/**
6443 * xmlRelaxNGParseDocument:
6444 * @ctxt: a Relax-NG parser context
6445 * @node: the root node of the RelaxNG schema
6446 *
6447 * parse a Relax-NG definition resource and build an internal
6448 * xmlRelaxNG struture which can be used to validate instances.
6449 *
6450 * Returns the internal XML RelaxNG structure built or
6451 * NULL in case of error
6452 */
6453static xmlRelaxNGPtr
6454xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6455 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006456 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006457 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006458
6459 if ((ctxt == NULL) || (node == NULL))
6460 return (NULL);
6461
6462 schema = xmlRelaxNGNewRelaxNG(ctxt);
6463 if (schema == NULL)
6464 return(NULL);
6465
Daniel Veillard276be4a2003-01-24 01:03:34 +00006466 olddefine = ctxt->define;
6467 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006468 if (IS_RELAXNG(node, "grammar")) {
6469 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
6470 } else {
Daniel Veillardc482e262003-02-26 14:48:48 +00006471 xmlRelaxNGGrammarPtr tmp, ret;
6472
6473 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006474 if (schema->topgrammar == NULL) {
6475 return(schema);
6476 }
Daniel Veillardc482e262003-02-26 14:48:48 +00006477 /*
6478 * Link the new grammar in the tree
6479 */
6480 ret->parent = ctxt->grammar;
6481 if (ctxt->grammar != NULL) {
6482 tmp = ctxt->grammar->children;
6483 if (tmp == NULL) {
6484 ctxt->grammar->children = ret;
6485 } else {
6486 while (tmp->next != NULL)
6487 tmp = tmp->next;
6488 tmp->next = ret;
6489 }
6490 }
Daniel Veillarde431a272003-01-29 23:02:33 +00006491 old = ctxt->grammar;
Daniel Veillardc482e262003-02-26 14:48:48 +00006492 ctxt->grammar = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006493 xmlRelaxNGParseStart(ctxt, node);
Daniel Veillarde431a272003-01-29 23:02:33 +00006494 if (old != NULL)
6495 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006496 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006497 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006498 if (schema->topgrammar->start != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00006499 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006500 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00006501 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6502 while ((schema->topgrammar->start != NULL) &&
6503 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6504 (schema->topgrammar->start->next != NULL))
6505 schema->topgrammar->start = schema->topgrammar->start->content;
6506 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6507 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006508 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006509 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006510
6511#ifdef DEBUG
6512 if (schema == NULL)
6513 xmlGenericError(xmlGenericErrorContext,
6514 "xmlRelaxNGParseDocument() failed\n");
6515#endif
6516
6517 return (schema);
6518}
6519
6520/************************************************************************
6521 * *
6522 * Reading RelaxNGs *
6523 * *
6524 ************************************************************************/
6525
6526/**
6527 * xmlRelaxNGNewParserCtxt:
6528 * @URL: the location of the schema
6529 *
6530 * Create an XML RelaxNGs parse context for that file/resource expected
6531 * to contain an XML RelaxNGs file.
6532 *
6533 * Returns the parser context or NULL in case of error
6534 */
6535xmlRelaxNGParserCtxtPtr
6536xmlRelaxNGNewParserCtxt(const char *URL) {
6537 xmlRelaxNGParserCtxtPtr ret;
6538
6539 if (URL == NULL)
6540 return(NULL);
6541
6542 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6543 if (ret == NULL) {
6544 xmlGenericError(xmlGenericErrorContext,
6545 "Failed to allocate new schama parser context for %s\n", URL);
6546 return (NULL);
6547 }
6548 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6549 ret->URL = xmlStrdup((const xmlChar *)URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006550 ret->error = xmlGenericError;
6551 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006552 return (ret);
6553}
6554
6555/**
6556 * xmlRelaxNGNewMemParserCtxt:
6557 * @buffer: a pointer to a char array containing the schemas
6558 * @size: the size of the array
6559 *
6560 * Create an XML RelaxNGs parse context for that memory buffer expected
6561 * to contain an XML RelaxNGs file.
6562 *
6563 * Returns the parser context or NULL in case of error
6564 */
6565xmlRelaxNGParserCtxtPtr
6566xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) {
6567 xmlRelaxNGParserCtxtPtr ret;
6568
6569 if ((buffer == NULL) || (size <= 0))
6570 return(NULL);
6571
6572 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6573 if (ret == NULL) {
6574 xmlGenericError(xmlGenericErrorContext,
6575 "Failed to allocate new schama parser context\n");
6576 return (NULL);
6577 }
6578 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6579 ret->buffer = buffer;
6580 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006581 ret->error = xmlGenericError;
6582 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006583 return (ret);
6584}
6585
6586/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006587 * xmlRelaxNGNewDocParserCtxt:
6588 * @doc: a preparsed document tree
6589 *
6590 * Create an XML RelaxNGs parser context for that document.
6591 * Note: since the process of compiling a RelaxNG schemas modifies the
6592 * document, the @doc parameter is duplicated internally.
6593 *
6594 * Returns the parser context or NULL in case of error
6595 */
6596xmlRelaxNGParserCtxtPtr
6597xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc) {
6598 xmlRelaxNGParserCtxtPtr ret;
6599 xmlDocPtr copy;
6600
6601 if (doc == NULL)
6602 return(NULL);
6603 copy = xmlCopyDoc(doc, 1);
6604 if (copy == NULL)
6605 return(NULL);
6606
6607 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6608 if (ret == NULL) {
6609 xmlGenericError(xmlGenericErrorContext,
6610 "Failed to allocate new schama parser context\n");
6611 return (NULL);
6612 }
6613 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6614 ret->document = copy;
6615 ret->userData = xmlGenericErrorContext;
6616 return (ret);
6617}
6618
6619/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006620 * xmlRelaxNGFreeParserCtxt:
6621 * @ctxt: the schema parser context
6622 *
6623 * Free the resources associated to the schema parser context
6624 */
6625void
6626xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) {
6627 if (ctxt == NULL)
6628 return;
6629 if (ctxt->URL != NULL)
6630 xmlFree(ctxt->URL);
6631 if (ctxt->doc != NULL)
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00006632 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006633 if (ctxt->interleaves != NULL)
6634 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006635 if (ctxt->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006636 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006637 if (ctxt->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006638 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006639 if (ctxt->docTab != NULL)
6640 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006641 if (ctxt->incTab != NULL)
6642 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006643 if (ctxt->defTab != NULL) {
6644 int i;
6645
6646 for (i = 0;i < ctxt->defNr;i++)
6647 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6648 xmlFree(ctxt->defTab);
6649 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006650 xmlFree(ctxt);
6651}
6652
Daniel Veillard6eadf632003-01-23 18:29:16 +00006653/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006654 * xmlRelaxNGNormExtSpace:
6655 * @value: a value
6656 *
6657 * Removes the leading and ending spaces of the value
6658 * The string is modified "in situ"
6659 */
6660static void
6661xmlRelaxNGNormExtSpace(xmlChar *value) {
6662 xmlChar *start = value;
6663 xmlChar *cur = value;
6664 if (value == NULL)
6665 return;
6666
6667 while (IS_BLANK(*cur)) cur++;
6668 if (cur == start) {
6669 do {
6670 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
6671 if (*cur == 0)
6672 return;
6673 start = cur;
6674 while (IS_BLANK(*cur)) cur++;
6675 if (*cur == 0) {
6676 *start = 0;
6677 return;
6678 }
6679 } while (1);
6680 } else {
6681 do {
6682 while ((*cur != 0) && (!IS_BLANK(*cur)))
6683 *start++ = *cur++;
6684 if (*cur == 0) {
6685 *start = 0;
6686 return;
6687 }
6688 /* don't try to normalize the inner spaces */
6689 while (IS_BLANK(*cur)) cur++;
6690 *start++ = *cur++;
6691 if (*cur == 0) {
6692 *start = 0;
6693 return;
6694 }
6695 } while (1);
6696 }
6697}
6698
6699/**
6700 * xmlRelaxNGCheckAttributes:
6701 * @ctxt: a Relax-NG parser context
6702 * @node: a Relax-NG node
6703 *
6704 * Check all the attributes on the given node
6705 */
6706static void
6707xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6708 xmlAttrPtr cur, next;
6709
6710 cur = node->properties;
6711 while (cur != NULL) {
6712 next = cur->next;
6713 if ((cur->ns == NULL) ||
6714 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6715 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6716 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6717 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6718 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6719 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
Daniel Veillard2df2de22003-02-17 23:34:33 +00006720 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
Daniel Veillardd2298792003-02-14 16:54:11 +00006721 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6722 if (ctxt->error != NULL)
6723 ctxt->error(ctxt->userData,
6724 "Attribute %s is not allowed on %s\n",
6725 cur->name, node->name);
6726 ctxt->nbErrors++;
6727 }
6728 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6729 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6730 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6731 if (ctxt->error != NULL)
6732 ctxt->error(ctxt->userData,
6733 "Attribute %s is not allowed on %s\n",
6734 cur->name, node->name);
6735 ctxt->nbErrors++;
6736 }
6737 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6738 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6739 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6740 if (ctxt->error != NULL)
6741 ctxt->error(ctxt->userData,
6742 "Attribute %s is not allowed on %s\n",
6743 cur->name, node->name);
6744 ctxt->nbErrors++;
6745 }
6746 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6747 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6748 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6749 if (ctxt->error != NULL)
6750 ctxt->error(ctxt->userData,
6751 "Attribute %s is not allowed on %s\n",
6752 cur->name, node->name);
6753 ctxt->nbErrors++;
6754 }
6755 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6756 xmlChar *val;
6757 xmlURIPtr uri;
6758
6759 val = xmlNodeListGetString(node->doc, cur->children, 1);
6760 if (val != NULL) {
6761 if (val[0] != 0) {
6762 uri = xmlParseURI((const char *) val);
6763 if (uri == NULL) {
6764 if (ctxt->error != NULL)
6765 ctxt->error(ctxt->userData,
6766 "Attribute %s contains invalid URI %s\n",
6767 cur->name, val);
6768 ctxt->nbErrors++;
6769 } else {
6770 if (uri->scheme == NULL) {
6771 if (ctxt->error != NULL)
6772 ctxt->error(ctxt->userData,
6773 "Attribute %s URI %s is not absolute\n",
6774 cur->name, val);
6775 ctxt->nbErrors++;
6776 }
6777 if (uri->fragment != NULL) {
6778 if (ctxt->error != NULL)
6779 ctxt->error(ctxt->userData,
6780 "Attribute %s URI %s has a fragment ID\n",
6781 cur->name, val);
6782 ctxt->nbErrors++;
6783 }
6784 xmlFreeURI(uri);
6785 }
6786 }
6787 xmlFree(val);
6788 }
6789 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6790 if (ctxt->error != NULL)
6791 ctxt->error(ctxt->userData,
6792 "Unknown attribute %s on %s\n",
6793 cur->name, node->name);
6794 ctxt->nbErrors++;
6795 }
6796 }
6797 cur = next;
6798 }
6799}
6800
6801/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006802 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006803 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006804 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006805 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006806 * Cleanup the subtree from unwanted nodes for parsing, resolve
6807 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006808 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006809static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00006810xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) {
Daniel Veillardc5312d72003-02-21 17:14:10 +00006811 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006812
Daniel Veillard6eadf632003-01-23 18:29:16 +00006813 delete = NULL;
6814 cur = root;
6815 while (cur != NULL) {
6816 if (delete != NULL) {
6817 xmlUnlinkNode(delete);
6818 xmlFreeNode(delete);
6819 delete = NULL;
6820 }
6821 if (cur->type == XML_ELEMENT_NODE) {
6822 /*
6823 * Simplification 4.1. Annotations
6824 */
6825 if ((cur->ns == NULL) ||
6826 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
Daniel Veillardd2298792003-02-14 16:54:11 +00006827 if ((cur->parent != NULL) &&
6828 (cur->parent->type == XML_ELEMENT_NODE) &&
6829 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6830 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6831 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6832 if (ctxt->error != NULL)
6833 ctxt->error(ctxt->userData,
6834 "element %s doesn't allow foreign elements\n",
6835 cur->parent->name);
6836 ctxt->nbErrors++;
6837 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006838 delete = cur;
6839 goto skip_children;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006840 } else {
6841 xmlRelaxNGCleanupAttributes(ctxt, cur);
6842 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6843 xmlChar *href, *ns, *base, *URL;
6844 xmlRelaxNGDocumentPtr docu;
6845 xmlNodePtr tmp;
6846
6847 ns = xmlGetProp(cur, BAD_CAST "ns");
6848 if (ns == NULL) {
6849 tmp = cur->parent;
6850 while ((tmp != NULL) &&
6851 (tmp->type == XML_ELEMENT_NODE)) {
6852 ns = xmlGetProp(tmp, BAD_CAST "ns");
6853 if (ns != NULL)
6854 break;
6855 tmp = tmp->parent;
6856 }
6857 }
6858 href = xmlGetProp(cur, BAD_CAST "href");
6859 if (href == NULL) {
6860 if (ctxt->error != NULL)
6861 ctxt->error(ctxt->userData,
6862 "xmlRelaxNGParse: externalRef has no href attribute\n");
6863 ctxt->nbErrors++;
6864 delete = cur;
6865 goto skip_children;
6866 }
6867 base = xmlNodeGetBase(cur->doc, cur);
6868 URL = xmlBuildURI(href, base);
6869 if (URL == NULL) {
6870 if (ctxt->error != NULL)
6871 ctxt->error(ctxt->userData,
6872 "Failed to compute URL for externalRef %s\n", href);
6873 ctxt->nbErrors++;
6874 if (href != NULL)
6875 xmlFree(href);
6876 if (base != NULL)
6877 xmlFree(base);
6878 delete = cur;
6879 goto skip_children;
6880 }
6881 if (href != NULL)
6882 xmlFree(href);
6883 if (base != NULL)
6884 xmlFree(base);
6885 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6886 if (docu == NULL) {
6887 if (ctxt->error != NULL)
6888 ctxt->error(ctxt->userData,
6889 "Failed to load externalRef %s\n", URL);
6890 ctxt->nbErrors++;
6891 xmlFree(URL);
6892 delete = cur;
6893 goto skip_children;
6894 }
6895 if (ns != NULL)
6896 xmlFree(ns);
6897 xmlFree(URL);
6898 cur->_private = docu;
6899 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6900 xmlChar *href, *ns, *base, *URL;
6901 xmlRelaxNGIncludePtr incl;
6902 xmlNodePtr tmp;
6903
6904 href = xmlGetProp(cur, BAD_CAST "href");
6905 if (href == NULL) {
6906 if (ctxt->error != NULL)
6907 ctxt->error(ctxt->userData,
6908 "xmlRelaxNGParse: include has no href attribute\n");
6909 ctxt->nbErrors++;
6910 delete = cur;
6911 goto skip_children;
6912 }
6913 base = xmlNodeGetBase(cur->doc, cur);
6914 URL = xmlBuildURI(href, base);
6915 if (URL == NULL) {
6916 if (ctxt->error != NULL)
6917 ctxt->error(ctxt->userData,
6918 "Failed to compute URL for include %s\n", href);
6919 ctxt->nbErrors++;
6920 if (href != NULL)
6921 xmlFree(href);
6922 if (base != NULL)
6923 xmlFree(base);
6924 delete = cur;
6925 goto skip_children;
6926 }
6927 if (href != NULL)
6928 xmlFree(href);
6929 if (base != NULL)
6930 xmlFree(base);
6931 ns = xmlGetProp(cur, BAD_CAST "ns");
6932 if (ns == NULL) {
6933 tmp = cur->parent;
6934 while ((tmp != NULL) &&
6935 (tmp->type == XML_ELEMENT_NODE)) {
6936 ns = xmlGetProp(tmp, BAD_CAST "ns");
6937 if (ns != NULL)
6938 break;
6939 tmp = tmp->parent;
6940 }
6941 }
6942 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6943 if (ns != NULL)
6944 xmlFree(ns);
6945 if (incl == NULL) {
6946 if (ctxt->error != NULL)
6947 ctxt->error(ctxt->userData,
6948 "Failed to load include %s\n", URL);
6949 ctxt->nbErrors++;
6950 xmlFree(URL);
6951 delete = cur;
6952 goto skip_children;
6953 }
6954 xmlFree(URL);
6955 cur->_private = incl;
6956 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6957 (xmlStrEqual(cur->name, BAD_CAST "attribute"))) {
6958 xmlChar *name, *ns;
6959 xmlNodePtr text = NULL;
6960
6961 /*
6962 * Simplification 4.8. name attribute of element
6963 * and attribute elements
6964 */
6965 name = xmlGetProp(cur, BAD_CAST "name");
6966 if (name != NULL) {
6967 if (cur->children == NULL) {
6968 text = xmlNewChild(cur, cur->ns, BAD_CAST "name",
6969 name);
6970 } else {
6971 xmlNodePtr node;
6972 node = xmlNewNode(cur->ns, BAD_CAST "name");
6973 if (node != NULL) {
6974 xmlAddPrevSibling(cur->children, node);
6975 text = xmlNewText(name);
6976 xmlAddChild(node, text);
6977 text = node;
6978 }
6979 }
6980 if (text == NULL) {
6981 if (ctxt->error != NULL)
6982 ctxt->error(ctxt->userData,
6983 "Failed to create a name %s element\n", name);
6984 ctxt->nbErrors++;
6985 }
6986 xmlUnsetProp(cur, BAD_CAST "name");
6987 xmlFree(name);
6988 ns = xmlGetProp(cur, BAD_CAST "ns");
6989 if (ns != NULL) {
6990 if (text != NULL) {
6991 xmlSetProp(text, BAD_CAST "ns", ns);
6992 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
6993 }
6994 xmlFree(ns);
6995 } else if (xmlStrEqual(cur->name,
6996 BAD_CAST "attribute")) {
6997 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
6998 }
6999 }
7000 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7001 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7002 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7003 /*
7004 * Simplification 4.8. name attribute of element
7005 * and attribute elements
7006 */
7007 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7008 xmlNodePtr node;
7009 xmlChar *ns = NULL;
7010
7011 node = cur->parent;
7012 while ((node != NULL) &&
7013 (node->type == XML_ELEMENT_NODE)) {
7014 ns = xmlGetProp(node, BAD_CAST "ns");
7015 if (ns != NULL) {
7016 break;
7017 }
7018 node = node->parent;
7019 }
7020 if (ns == NULL) {
7021 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7022 } else {
7023 xmlSetProp(cur, BAD_CAST "ns", ns);
7024 xmlFree(ns);
7025 }
7026 }
7027 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7028 xmlChar *name, *local, *prefix;
7029
7030 /*
7031 * Simplification: 4.10. QNames
7032 */
7033 name = xmlNodeGetContent(cur);
7034 if (name != NULL) {
7035 local = xmlSplitQName2(name, &prefix);
7036 if (local != NULL) {
7037 xmlNsPtr ns;
7038
7039 ns = xmlSearchNs(cur->doc, cur, prefix);
7040 if (ns == NULL) {
7041 if (ctxt->error != NULL)
7042 ctxt->error(ctxt->userData,
7043 "xmlRelaxNGParse: no namespace for prefix %s\n", prefix);
7044 ctxt->nbErrors++;
7045 } else {
7046 xmlSetProp(cur, BAD_CAST "ns", ns->href);
7047 xmlNodeSetContent(cur, local);
7048 }
7049 xmlFree(local);
7050 xmlFree(prefix);
7051 }
7052 xmlFree(name);
7053 }
7054 }
7055 /*
7056 * 4.16
7057 */
7058 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7059 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7060 if (ctxt->error != NULL)
7061 ctxt->error(ctxt->userData,
7062 "Found nsName/except//nsName forbidden construct\n");
7063 ctxt->nbErrors++;
7064 }
7065 }
7066 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7067 (cur != root)) {
7068 int oldflags = ctxt->flags;
7069
7070 /*
7071 * 4.16
7072 */
7073 if ((cur->parent != NULL) &&
7074 (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) {
7075 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7076 xmlRelaxNGCleanupTree(ctxt, cur);
7077 ctxt->flags = oldflags;
7078 goto skip_children;
7079 } else if ((cur->parent != NULL) &&
7080 (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) {
7081 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7082 xmlRelaxNGCleanupTree(ctxt, cur);
7083 ctxt->flags = oldflags;
7084 goto skip_children;
7085 }
7086 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7087 /*
7088 * 4.16
7089 */
7090 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7091 if (ctxt->error != NULL)
7092 ctxt->error(ctxt->userData,
7093 "Found anyName/except//anyName forbidden construct\n");
7094 ctxt->nbErrors++;
7095 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7096 if (ctxt->error != NULL)
7097 ctxt->error(ctxt->userData,
7098 "Found nsName/except//anyName forbidden construct\n");
7099 ctxt->nbErrors++;
7100 }
7101 }
7102 /*
7103 * Thisd is not an else since "include" is transformed
7104 * into a div
7105 */
7106 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7107 xmlChar *ns;
7108 xmlNodePtr child, ins, tmp;
7109
7110 /*
7111 * implements rule 4.11
7112 */
7113
7114 ns = xmlGetProp(cur, BAD_CAST "ns");
7115
7116 child = cur->children;
7117 ins = cur;
7118 while (child != NULL) {
7119 if (ns != NULL) {
7120 if (!xmlHasProp(child, BAD_CAST "ns")) {
7121 xmlSetProp(child, BAD_CAST "ns", ns);
7122 }
7123 }
7124 tmp = child->next;
7125 xmlUnlinkNode(child);
7126 ins = xmlAddNextSibling(ins, child);
7127 child = tmp;
7128 }
7129 if (ns != NULL)
7130 xmlFree(ns);
7131 delete = cur;
7132 goto skip_children;
7133 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007134 }
7135 }
7136 /*
7137 * Simplification 4.2 whitespaces
7138 */
Daniel Veillard39eb88b2003-03-11 11:21:28 +00007139 else if ((cur->type == XML_TEXT_NODE) ||
7140 (cur->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007141 if (IS_BLANK_NODE(cur)) {
7142 if (cur->parent->type == XML_ELEMENT_NODE) {
7143 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) &&
7144 (!xmlStrEqual(cur->parent->name, BAD_CAST "param")))
7145 delete = cur;
7146 } else {
7147 delete = cur;
7148 goto skip_children;
7149 }
7150 }
Daniel Veillard39eb88b2003-03-11 11:21:28 +00007151 } else {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007152 delete = cur;
7153 goto skip_children;
7154 }
7155
7156 /*
7157 * Skip to next node
7158 */
7159 if (cur->children != NULL) {
7160 if ((cur->children->type != XML_ENTITY_DECL) &&
7161 (cur->children->type != XML_ENTITY_REF_NODE) &&
7162 (cur->children->type != XML_ENTITY_NODE)) {
7163 cur = cur->children;
7164 continue;
7165 }
7166 }
7167skip_children:
7168 if (cur->next != NULL) {
7169 cur = cur->next;
7170 continue;
7171 }
7172
7173 do {
7174 cur = cur->parent;
7175 if (cur == NULL)
7176 break;
7177 if (cur == root) {
7178 cur = NULL;
7179 break;
7180 }
7181 if (cur->next != NULL) {
7182 cur = cur->next;
7183 break;
7184 }
7185 } while (cur != NULL);
7186 }
7187 if (delete != NULL) {
7188 xmlUnlinkNode(delete);
7189 xmlFreeNode(delete);
7190 delete = NULL;
7191 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007192}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007193
Daniel Veillardc5312d72003-02-21 17:14:10 +00007194/**
7195 * xmlRelaxNGCleanupDoc:
7196 * @ctxt: a Relax-NG parser context
7197 * @doc: an xmldocPtr document pointer
7198 *
7199 * Cleanup the document from unwanted nodes for parsing, resolve
7200 * Include and externalRef lookups.
7201 *
7202 * Returns the cleaned up document or NULL in case of error
7203 */
7204static xmlDocPtr
7205xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) {
7206 xmlNodePtr root;
7207
7208 /*
7209 * Extract the root
7210 */
7211 root = xmlDocGetRootElement(doc);
7212 if (root == NULL) {
7213 if (ctxt->error != NULL)
7214 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
7215 ctxt->URL);
7216 ctxt->nbErrors++;
7217 return (NULL);
7218 }
7219 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007220 return(doc);
7221}
7222
7223/**
7224 * xmlRelaxNGParse:
7225 * @ctxt: a Relax-NG parser context
7226 *
7227 * parse a schema definition resource and build an internal
7228 * XML Shema struture which can be used to validate instances.
7229 * *WARNING* this interface is highly subject to change
7230 *
7231 * Returns the internal XML RelaxNG structure built from the resource or
7232 * NULL in case of error
7233 */
7234xmlRelaxNGPtr
7235xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7236{
7237 xmlRelaxNGPtr ret = NULL;
7238 xmlDocPtr doc;
7239 xmlNodePtr root;
7240
7241 xmlRelaxNGInitTypes();
7242
7243 if (ctxt == NULL)
7244 return (NULL);
7245
7246 /*
7247 * First step is to parse the input document into an DOM/Infoset
7248 */
7249 if (ctxt->URL != NULL) {
7250 doc = xmlParseFile((const char *) ctxt->URL);
7251 if (doc == NULL) {
7252 if (ctxt->error != NULL)
7253 ctxt->error(ctxt->userData,
7254 "xmlRelaxNGParse: could not load %s\n", ctxt->URL);
7255 ctxt->nbErrors++;
7256 return (NULL);
7257 }
7258 } else if (ctxt->buffer != NULL) {
7259 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
7260 if (doc == NULL) {
7261 if (ctxt->error != NULL)
7262 ctxt->error(ctxt->userData,
7263 "xmlRelaxNGParse: could not parse schemas\n");
7264 ctxt->nbErrors++;
7265 return (NULL);
7266 }
7267 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7268 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007269 } else if (ctxt->document != NULL) {
7270 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007271 } else {
7272 if (ctxt->error != NULL)
7273 ctxt->error(ctxt->userData,
7274 "xmlRelaxNGParse: nothing to parse\n");
7275 ctxt->nbErrors++;
7276 return (NULL);
7277 }
7278 ctxt->document = doc;
7279
7280 /*
7281 * Some preprocessing of the document content
7282 */
7283 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7284 if (doc == NULL) {
7285 xmlFreeDoc(ctxt->document);
7286 ctxt->document = NULL;
7287 return(NULL);
7288 }
7289
Daniel Veillard6eadf632003-01-23 18:29:16 +00007290 /*
7291 * Then do the parsing for good
7292 */
7293 root = xmlDocGetRootElement(doc);
7294 if (root == NULL) {
7295 if (ctxt->error != NULL)
7296 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
7297 ctxt->URL);
7298 ctxt->nbErrors++;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007299 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007300 return (NULL);
7301 }
7302 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007303 if (ret == NULL) {
7304 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007305 return(NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007306 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007307
7308 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007309 * Check the ref/defines links
7310 */
7311 /*
7312 * try to preprocess interleaves
7313 */
7314 if (ctxt->interleaves != NULL) {
7315 xmlHashScan(ctxt->interleaves,
7316 (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt);
7317 }
7318
7319 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007320 * if there was a parsing error return NULL
7321 */
7322 if (ctxt->nbErrors > 0) {
7323 xmlRelaxNGFree(ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007324 ctxt->document = NULL;
7325 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007326 return(NULL);
7327 }
7328
7329 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007330 * try to compile (parts of) the schemas
7331 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007332 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7333 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillardf4e55762003-04-15 23:32:22 +00007334 xmlRelaxNGDefinePtr def;
7335
7336 def = xmlRelaxNGNewDefine(ctxt, NULL);
7337 if (def != NULL) {
7338 def->type = XML_RELAXNG_START;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007339 def->content = ret->topgrammar->start;
7340 ret->topgrammar->start = def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007341 }
7342 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007343 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007344 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007345
7346 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007347 * Transfer the pointer for cleanup at the schema level.
7348 */
7349 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007350 ctxt->document = NULL;
7351 ret->documents = ctxt->documents;
7352 ctxt->documents = NULL;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007353
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007354 ret->includes = ctxt->includes;
7355 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007356 ret->defNr = ctxt->defNr;
7357 ret->defTab = ctxt->defTab;
7358 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007359 if (ctxt->idref == 1)
7360 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007361
7362 return (ret);
7363}
7364
7365/**
7366 * xmlRelaxNGSetParserErrors:
7367 * @ctxt: a Relax-NG validation context
7368 * @err: the error callback
7369 * @warn: the warning callback
7370 * @ctx: contextual data for the callbacks
7371 *
7372 * Set the callback functions used to handle errors for a validation context
7373 */
7374void
7375xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7376 xmlRelaxNGValidityErrorFunc err,
7377 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
7378 if (ctxt == NULL)
7379 return;
7380 ctxt->error = err;
7381 ctxt->warning = warn;
7382 ctxt->userData = ctx;
7383}
Daniel Veillard409a8142003-07-18 15:16:57 +00007384
7385/**
7386 * xmlRelaxNGGetParserErrors:
7387 * @ctxt: a Relax-NG validation context
7388 * @err: the error callback result
7389 * @warn: the warning callback result
7390 * @ctx: contextual data for the callbacks result
7391 *
7392 * Get the callback information used to handle errors for a validation context
7393 *
7394 * Returns -1 in case of failure, 0 otherwise.
7395 */
7396int
7397xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7398 xmlRelaxNGValidityErrorFunc *err,
7399 xmlRelaxNGValidityWarningFunc *warn, void **ctx) {
7400 if (ctxt == NULL)
7401 return(-1);
7402 if (err != NULL) *err = ctxt->error;
7403 if (warn != NULL) *warn = ctxt->warning;
7404 if (ctx != NULL) *ctx = ctxt->userData;
7405 return(0);
7406}
7407
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007408#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard6eadf632003-01-23 18:29:16 +00007409/************************************************************************
7410 * *
7411 * Dump back a compiled form *
7412 * *
7413 ************************************************************************/
7414static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define);
7415
7416/**
7417 * xmlRelaxNGDumpDefines:
7418 * @output: the file output
7419 * @defines: a list of define structures
7420 *
7421 * Dump a RelaxNG structure back
7422 */
7423static void
7424xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) {
7425 while (defines != NULL) {
7426 xmlRelaxNGDumpDefine(output, defines);
7427 defines = defines->next;
7428 }
7429}
7430
7431/**
7432 * xmlRelaxNGDumpDefine:
7433 * @output: the file output
7434 * @define: a define structure
7435 *
7436 * Dump a RelaxNG structure back
7437 */
7438static void
7439xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) {
7440 if (define == NULL)
7441 return;
7442 switch(define->type) {
7443 case XML_RELAXNG_EMPTY:
7444 fprintf(output, "<empty/>\n");
7445 break;
7446 case XML_RELAXNG_NOT_ALLOWED:
7447 fprintf(output, "<notAllowed/>\n");
7448 break;
7449 case XML_RELAXNG_TEXT:
7450 fprintf(output, "<text/>\n");
7451 break;
7452 case XML_RELAXNG_ELEMENT:
7453 fprintf(output, "<element>\n");
7454 if (define->name != NULL) {
7455 fprintf(output, "<name");
7456 if (define->ns != NULL)
7457 fprintf(output, " ns=\"%s\"", define->ns);
7458 fprintf(output, ">%s</name>\n", define->name);
7459 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007460 xmlRelaxNGDumpDefines(output, define->attrs);
7461 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007462 fprintf(output, "</element>\n");
7463 break;
7464 case XML_RELAXNG_LIST:
7465 fprintf(output, "<list>\n");
7466 xmlRelaxNGDumpDefines(output, define->content);
7467 fprintf(output, "</list>\n");
7468 break;
7469 case XML_RELAXNG_ONEORMORE:
7470 fprintf(output, "<oneOrMore>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007471 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007472 fprintf(output, "</oneOrMore>\n");
7473 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007474 case XML_RELAXNG_ZEROORMORE:
7475 fprintf(output, "<zeroOrMore>\n");
7476 xmlRelaxNGDumpDefines(output, define->content);
7477 fprintf(output, "</zeroOrMore>\n");
7478 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007479 case XML_RELAXNG_CHOICE:
7480 fprintf(output, "<choice>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007481 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007482 fprintf(output, "</choice>\n");
7483 break;
7484 case XML_RELAXNG_GROUP:
7485 fprintf(output, "<group>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007486 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007487 fprintf(output, "</group>\n");
7488 break;
7489 case XML_RELAXNG_INTERLEAVE:
7490 fprintf(output, "<interleave>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007491 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007492 fprintf(output, "</interleave>\n");
7493 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007494 case XML_RELAXNG_OPTIONAL:
7495 fprintf(output, "<optional>\n");
7496 xmlRelaxNGDumpDefines(output, define->content);
7497 fprintf(output, "</optional>\n");
7498 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007499 case XML_RELAXNG_ATTRIBUTE:
7500 fprintf(output, "<attribute>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007501 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007502 fprintf(output, "</attribute>\n");
7503 break;
7504 case XML_RELAXNG_DEF:
7505 fprintf(output, "<define");
7506 if (define->name != NULL)
7507 fprintf(output, " name=\"%s\"", define->name);
7508 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007509 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007510 fprintf(output, "</define>\n");
7511 break;
7512 case XML_RELAXNG_REF:
7513 fprintf(output, "<ref");
7514 if (define->name != NULL)
7515 fprintf(output, " name=\"%s\"", define->name);
7516 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007517 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007518 fprintf(output, "</ref>\n");
7519 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007520 case XML_RELAXNG_PARENTREF:
7521 fprintf(output, "<parentRef");
7522 if (define->name != NULL)
7523 fprintf(output, " name=\"%s\"", define->name);
7524 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007525 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard419a7682003-02-03 23:22:49 +00007526 fprintf(output, "</parentRef>\n");
7527 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007528 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard416589a2003-02-17 17:25:42 +00007529 fprintf(output, "<externalRef>");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007530 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillarde431a272003-01-29 23:02:33 +00007531 fprintf(output, "</externalRef>\n");
7532 break;
7533 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007534 case XML_RELAXNG_VALUE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007535 TODO
Daniel Veillard6eadf632003-01-23 18:29:16 +00007536 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007537 case XML_RELAXNG_START:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007538 case XML_RELAXNG_EXCEPT:
Daniel Veillard8fe98712003-02-19 00:19:14 +00007539 case XML_RELAXNG_PARAM:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007540 TODO
7541 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00007542 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007543 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard77648bb2003-02-20 15:03:22 +00007544 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007545 }
7546}
7547
7548/**
7549 * xmlRelaxNGDumpGrammar:
7550 * @output: the file output
7551 * @grammar: a grammar structure
7552 * @top: is this a top grammar
7553 *
7554 * Dump a RelaxNG structure back
7555 */
7556static void
7557xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7558{
7559 if (grammar == NULL)
7560 return;
7561
7562 fprintf(output, "<grammar");
7563 if (top)
7564 fprintf(output,
7565 " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7566 switch(grammar->combine) {
7567 case XML_RELAXNG_COMBINE_UNDEFINED:
7568 break;
7569 case XML_RELAXNG_COMBINE_CHOICE:
7570 fprintf(output, " combine=\"choice\"");
7571 break;
7572 case XML_RELAXNG_COMBINE_INTERLEAVE:
7573 fprintf(output, " combine=\"interleave\"");
7574 break;
7575 default:
7576 fprintf(output, " <!-- invalid combine value -->");
7577 }
7578 fprintf(output, ">\n");
7579 if (grammar->start == NULL) {
7580 fprintf(output, " <!-- grammar had no start -->");
7581 } else {
7582 fprintf(output, "<start>\n");
7583 xmlRelaxNGDumpDefine(output, grammar->start);
7584 fprintf(output, "</start>\n");
7585 }
7586 /* TODO ? Dump the defines ? */
7587 fprintf(output, "</grammar>\n");
7588}
7589
7590/**
7591 * xmlRelaxNGDump:
7592 * @output: the file output
7593 * @schema: a schema structure
7594 *
7595 * Dump a RelaxNG structure back
7596 */
7597void
7598xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7599{
7600 if (schema == NULL) {
7601 fprintf(output, "RelaxNG empty or failed to compile\n");
7602 return;
7603 }
7604 fprintf(output, "RelaxNG: ");
7605 if (schema->doc == NULL) {
7606 fprintf(output, "no document\n");
7607 } else if (schema->doc->URL != NULL) {
7608 fprintf(output, "%s\n", schema->doc->URL);
7609 } else {
7610 fprintf(output, "\n");
7611 }
7612 if (schema->topgrammar == NULL) {
7613 fprintf(output, "RelaxNG has no top grammar\n");
7614 return;
7615 }
7616 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7617}
7618
Daniel Veillardfebcca42003-02-16 15:44:18 +00007619/**
7620 * xmlRelaxNGDumpTree:
7621 * @output: the file output
7622 * @schema: a schema structure
7623 *
7624 * Dump the transformed RelaxNG tree.
7625 */
7626void
7627xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7628{
7629 if (schema == NULL) {
7630 fprintf(output, "RelaxNG empty or failed to compile\n");
7631 return;
7632 }
7633 if (schema->doc == NULL) {
7634 fprintf(output, "no document\n");
7635 } else {
7636 xmlDocDump(output, schema->doc);
7637 }
7638}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007639#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007640
Daniel Veillard6eadf632003-01-23 18:29:16 +00007641/************************************************************************
7642 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007643 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007644 * *
7645 ************************************************************************/
Daniel Veillardfd573f12003-03-16 17:52:32 +00007646static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7647 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007648
7649/**
7650 * xmlRelaxNGValidateCompiledCallback:
7651 * @exec: the regular expression instance
7652 * @token: the token which matched
7653 * @transdata: callback data, the define for the subelement if available
7654 @ @inputdata: callback data, the Relax NG validation context
7655 *
7656 * Handle the callback and if needed validate the element children.
7657 */
7658static void
7659xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7660 const xmlChar *token,
7661 void *transdata,
7662 void *inputdata) {
7663 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7664 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7665 int ret;
7666
7667#ifdef DEBUG_COMPILE
7668 xmlGenericError(xmlGenericErrorContext,
7669 "Compiled callback for: '%s'\n", token);
7670#endif
7671 if (ctxt == NULL) {
7672 fprintf(stderr, "callback on %s missing context\n", token);
7673 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7674 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7675 return;
7676 }
7677 if (define == NULL) {
7678 if (token[0] == '#')
7679 return;
7680 fprintf(stderr, "callback on %s missing define\n", token);
7681 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7682 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7683 return;
7684 }
7685 if ((ctxt == NULL) || (define == NULL)) {
7686 fprintf(stderr, "callback on %s missing info\n", token);
7687 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7688 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7689 return;
7690 } else if (define->type != XML_RELAXNG_ELEMENT) {
7691 fprintf(stderr, "callback on %s define is not element\n", token);
7692 if (ctxt->errNo == XML_RELAXNG_OK)
7693 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7694 return;
7695 }
7696 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007697 if (ret != 0)
7698 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007699}
7700
7701/**
7702 * xmlRelaxNGValidateCompiledContent:
7703 * @ctxt: the RelaxNG validation context
7704 * @regexp: the regular expression as compiled
7705 * @content: list of children to test against the regexp
7706 *
7707 * Validate the content model of an element or start using the regexp
7708 *
7709 * Returns 0 in case of success, -1 in case of error.
7710 */
7711static int
7712xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
7713 xmlRegexpPtr regexp, xmlNodePtr content) {
7714 xmlRegExecCtxtPtr exec;
7715 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007716 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007717 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007718
7719 if ((ctxt == NULL) || (regexp == NULL))
7720 return(-1);
7721 exec = xmlRegNewExecCtxt(regexp,
7722 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007723 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007724 cur = content;
7725 while (cur != NULL) {
7726 ctxt->state->seq = cur;
7727 switch (cur->type) {
7728 case XML_TEXT_NODE:
7729 case XML_CDATA_SECTION_NODE:
7730 if (xmlIsBlankNode(cur))
7731 break;
7732 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7733 if (ret < 0) {
7734 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name);
7735 }
7736 break;
7737 case XML_ELEMENT_NODE:
7738 if (cur->ns != NULL) {
7739 ret = xmlRegExecPushString2(exec, cur->name,
7740 cur->ns->href, ctxt);
7741 } else {
7742 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7743 }
7744 if (ret < 0) {
7745 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7746 }
7747 break;
7748 default:
7749 break;
7750 }
7751 if (ret < 0) break;
7752 /*
7753 * Switch to next element
7754 */
7755 cur = cur->next;
7756 }
7757 ret = xmlRegExecPushString(exec, NULL, NULL);
7758 if (ret == 1) {
7759 ret = 0;
7760 ctxt->state->seq = NULL;
7761 } else if (ret == 0) {
7762 /*
Daniel Veillardf4e55762003-04-15 23:32:22 +00007763 * TODO: get some of the names needed to exit the current state of exec
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007764 */
7765 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7766 ret = -1;
7767 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7768 xmlRelaxNGDumpValidError(ctxt);
7769 } else {
7770 ret = -1;
7771 }
7772 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007773 /*
7774 * There might be content model errors outside of the pure
7775 * regexp validation, e.g. for attribute values.
7776 */
7777 if ((ret == 0) && (ctxt->perr != 0)) {
7778 ret = ctxt->perr;
7779 }
7780 ctxt->perr = oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007781 return(ret);
7782}
7783
7784/************************************************************************
7785 * *
7786 * Progressive validation of when possible *
7787 * *
7788 ************************************************************************/
Daniel Veillardf4e55762003-04-15 23:32:22 +00007789static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7790 xmlRelaxNGDefinePtr defines);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007791static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log);
7792static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007793
7794/**
7795 * xmlRelaxNGElemPush:
7796 * @ctxt: the validation context
7797 * @exec: the regexp runtime for the new content model
7798 *
7799 * Push a new regexp for the current node content model on the stack
7800 *
7801 * Returns 0 in case of success and -1 in case of error.
7802 */
7803static int
7804xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) {
7805 if (ctxt->elemTab == NULL) {
7806 ctxt->elemMax = 10;
7807 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7808 sizeof(xmlRegExecCtxtPtr));
7809 if (ctxt->elemTab == NULL) {
7810 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7811 return(-1);
7812 }
7813 }
7814 if (ctxt->elemNr >= ctxt->elemMax) {
7815 ctxt->elemMax *= 2;
7816 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
7817 ctxt->elemMax * sizeof(xmlRegExecCtxtPtr));
7818 if (ctxt->elemTab == NULL) {
7819 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7820 return(-1);
7821 }
7822 }
7823 ctxt->elemTab[ctxt->elemNr++] = exec;
7824 ctxt->elem = exec;
7825 return(0);
7826}
7827
7828/**
7829 * xmlRelaxNGElemPop:
7830 * @ctxt: the validation context
7831 *
7832 * Pop the regexp of the current node content model from the stack
7833 *
7834 * Returns the exec or NULL if empty
7835 */
7836static xmlRegExecCtxtPtr
7837xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) {
7838 xmlRegExecCtxtPtr ret;
7839
7840 if (ctxt->elemNr <= 0) return(NULL);
7841 ctxt->elemNr--;
7842 ret = ctxt->elemTab[ctxt->elemNr];
7843 ctxt->elemTab[ctxt->elemNr] = NULL;
7844 if (ctxt->elemNr > 0)
7845 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7846 else
7847 ctxt->elem = NULL;
7848 return(ret);
7849}
7850
7851/**
7852 * xmlRelaxNGValidateProgressiveCallback:
7853 * @exec: the regular expression instance
7854 * @token: the token which matched
7855 * @transdata: callback data, the define for the subelement if available
7856 @ @inputdata: callback data, the Relax NG validation context
7857 *
7858 * Handle the callback and if needed validate the element children.
7859 * some of the in/out informations are passed via the context in @inputdata.
7860 */
7861static void
7862xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7863 const xmlChar *token,
7864 void *transdata,
7865 void *inputdata) {
7866 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7867 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007868 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007869 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007870 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007871
7872#ifdef DEBUG_PROGRESSIVE
7873 xmlGenericError(xmlGenericErrorContext,
7874 "Progressive callback for: '%s'\n", token);
7875#endif
7876 if (ctxt == NULL) {
7877 fprintf(stderr, "callback on %s missing context\n", token);
7878 return;
7879 }
7880 ctxt->pstate = 1;
7881 if (define == NULL) {
7882 if (token[0] == '#')
7883 return;
7884 fprintf(stderr, "callback on %s missing define\n", token);
7885 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7886 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7887 ctxt->pstate = -1;
7888 return;
7889 }
7890 if ((ctxt == NULL) || (define == NULL)) {
7891 fprintf(stderr, "callback on %s missing info\n", token);
7892 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7893 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7894 ctxt->pstate = -1;
7895 return;
7896 } else if (define->type != XML_RELAXNG_ELEMENT) {
7897 fprintf(stderr, "callback on %s define is not element\n", token);
7898 if (ctxt->errNo == XML_RELAXNG_OK)
7899 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7900 ctxt->pstate = -1;
7901 return;
7902 }
7903 if (node->type != XML_ELEMENT_NODE) {
7904 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7905 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7906 xmlRelaxNGDumpValidError(ctxt);
7907 ctxt->pstate = -1;
7908 return;
7909 }
7910 if (define->contModel == NULL) {
7911 /*
7912 * this node cannot be validated in a streamable fashion
7913 */
7914#ifdef DEBUG_PROGRESSIVE
7915 xmlGenericError(xmlGenericErrorContext,
7916 "Element '%s' validation is not streamable\n", token);
7917#endif
7918 ctxt->pstate = 0;
7919 ctxt->pdef = define;
7920 return;
7921 }
7922 exec = xmlRegNewExecCtxt(define->contModel,
7923 xmlRelaxNGValidateProgressiveCallback,
7924 ctxt);
7925 if (exec == NULL) {
7926 ctxt->pstate = -1;
7927 return;
7928 }
7929 xmlRelaxNGElemPush(ctxt, exec);
7930
7931 /*
7932 * Validate the attributes part of the content.
7933 */
7934 state = xmlRelaxNGNewValidState(ctxt, node);
7935 if (state == NULL) {
7936 ctxt->pstate = -1;
7937 return;
7938 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007939 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007940 ctxt->state = state;
7941 if (define->attrs != NULL) {
7942 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
7943 if (ret != 0) {
7944 ctxt->pstate = -1;
7945 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
7946 }
7947 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007948 if (ctxt->state != NULL) {
7949 ctxt->state->seq = NULL;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007950 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00007951 if (ret != 0) {
7952 ctxt->pstate = -1;
7953 }
7954 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
7955 } else if (ctxt->states != NULL) {
7956 int tmp = -1, i;
7957
7958 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007959
7960 for (i = 0; i < ctxt->states->nbState; i++) {
7961 state = ctxt->states->tabState[i];
7962 ctxt->state = state;
7963 ctxt->state->seq = NULL;
7964
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007965 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00007966 tmp = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00007967 break;
7968 }
7969 }
7970 if (tmp != 0) {
7971 /*
7972 * validation error, log the message for the "best" one
7973 */
7974 ctxt->flags |= FLAGS_IGNORABLE;
7975 xmlRelaxNGLogBestError(ctxt);
7976 }
7977 for (i = 0; i < ctxt->states->nbState; i++) {
7978 xmlRelaxNGFreeValidState(ctxt,
7979 ctxt->states->tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00007980 }
7981 xmlRelaxNGFreeStates(ctxt, ctxt->states);
7982 ctxt->states = NULL;
7983 if ((ret == 0) && (tmp == -1))
7984 ctxt->pstate = -1;
7985 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007986 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007987 if (ctxt->pstate == -1) {
7988 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
7989 xmlRelaxNGDumpValidError(ctxt);
7990 }
7991 }
7992 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007993}
7994
7995/**
7996 * xmlRelaxNGValidatePushElement:
7997 * @ctxt: the validation context
7998 * @doc: a document instance
7999 * @elem: an element instance
8000 *
8001 * Push a new element start on the RelaxNG validation stack.
8002 *
8003 * returns 1 if no validation problem was found or 0 if validating the
8004 * element requires a full node, and -1 in case of error.
8005 */
8006int
Daniel Veillard33300b42003-04-17 09:09:19 +00008007xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8008 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008009 xmlNodePtr elem)
8010{
8011 int ret = 1;
8012
8013 if ((ctxt == NULL) || (elem == NULL))
8014 return (-1);
8015
8016#ifdef DEBUG_PROGRESSIVE
8017 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8018#endif
8019 if (ctxt->elem == 0) {
8020 xmlRelaxNGPtr schema;
8021 xmlRelaxNGGrammarPtr grammar;
8022 xmlRegExecCtxtPtr exec;
8023 xmlRelaxNGDefinePtr define;
8024
8025 schema = ctxt->schema;
8026 if (schema == NULL) {
8027 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8028 return (-1);
8029 }
8030 grammar = schema->topgrammar;
8031 if ((grammar == NULL) || (grammar->start == NULL)) {
8032 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8033 return (-1);
8034 }
8035 define = grammar->start;
8036 if (define->contModel == NULL) {
8037 ctxt->pdef = define;
8038 return (0);
8039 }
8040 exec = xmlRegNewExecCtxt(define->contModel,
8041 xmlRelaxNGValidateProgressiveCallback,
8042 ctxt);
8043 if (exec == NULL) {
8044 return (-1);
8045 }
8046 xmlRelaxNGElemPush(ctxt, exec);
8047 }
8048 ctxt->pnode = elem;
8049 ctxt->pstate = 0;
8050 if (elem->ns != NULL) {
8051 ret =
8052 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8053 ctxt);
8054 } else {
8055 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8056 }
8057 if (ret < 0) {
8058 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8059 } else {
8060 if (ctxt->pstate == 0)
8061 ret = 0;
8062 else if (ctxt->pstate < 0)
8063 ret = -1;
8064 else
8065 ret = 1;
8066 }
8067#ifdef DEBUG_PROGRESSIVE
8068 if (ret < 0)
8069 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8070 elem->name);
8071#endif
8072 return (ret);
8073}
8074
8075/**
8076 * xmlRelaxNGValidatePushCData:
8077 * @ctxt: the RelaxNG validation context
8078 * @data: some character data read
8079 * @len: the lenght of the data
8080 *
8081 * check the CData parsed for validation in the current stack
8082 *
8083 * returns 1 if no validation problem was found or -1 otherwise
8084 */
8085int
8086xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard33300b42003-04-17 09:09:19 +00008087 const xmlChar * data,
8088 int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008089{
8090 int ret = 1;
8091
8092 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8093 return (-1);
8094
8095#ifdef DEBUG_PROGRESSIVE
8096 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8097#endif
8098
8099 while (*data != 0) {
8100 if (!IS_BLANK(*data))
8101 break;
8102 data++;
8103 }
8104 if (*data == 0)
8105 return(1);
8106
8107 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8108 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008109 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008110#ifdef DEBUG_PROGRESSIVE
8111 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
8112#endif
8113
8114 return(-1);
8115 }
8116 return(1);
8117}
8118
8119/**
8120 * xmlRelaxNGValidatePopElement:
8121 * @ctxt: the RelaxNG validation context
8122 * @doc: a document instance
8123 * @elem: an element instance
8124 *
8125 * Pop the element end from the RelaxNG validation stack.
8126 *
8127 * returns 1 if no validation problem was found or 0 otherwise
8128 */
8129int
8130xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8131 xmlDocPtr doc ATTRIBUTE_UNUSED,
8132 xmlNodePtr elem) {
8133 int ret;
8134 xmlRegExecCtxtPtr exec;
8135
8136 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1);
8137#ifdef DEBUG_PROGRESSIVE
8138 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8139#endif
8140 /*
8141 * verify that we reached a terminal state of the content model.
8142 */
8143 exec = xmlRelaxNGElemPop(ctxt);
8144 ret = xmlRegExecPushString(exec, NULL, NULL);
8145 if (ret == 0) {
8146 /*
8147 * TODO: get some of the names needed to exit the current state of exec
8148 */
8149 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8150 ret = -1;
8151 } else if (ret < 0) {
8152 ret = -1;
8153 } else {
8154 ret = 1;
8155 }
8156 xmlRegFreeExecCtxt(exec);
8157#ifdef DEBUG_PROGRESSIVE
8158 if (ret < 0)
8159 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8160 elem->name);
8161#endif
8162 return(ret);
8163}
8164
8165/**
8166 * xmlRelaxNGValidateFullElement:
8167 * @ctxt: the validation context
8168 * @doc: a document instance
8169 * @elem: an element instance
8170 *
8171 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8172 * 0 and the content of the node has been expanded.
8173 *
8174 * returns 1 if no validation problem was found or -1 in case of error.
8175 */
8176int
Daniel Veillard33300b42003-04-17 09:09:19 +00008177xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8178 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008179 xmlNodePtr elem) {
8180 int ret;
8181 xmlRelaxNGValidStatePtr state;
8182
8183 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1);
8184#ifdef DEBUG_PROGRESSIVE
8185 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8186#endif
8187 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8188 if (state == NULL) {
8189 return(-1);
8190 }
8191 state->seq = elem;
8192 ctxt->state = state;
8193 ctxt->errNo = XML_RELAXNG_OK;
8194 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
8195 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1;
8196 else ret = 1;
8197 xmlRelaxNGFreeValidState(ctxt, state);
8198 ctxt->state = NULL;
8199#ifdef DEBUG_PROGRESSIVE
8200 if (ret < 0)
8201 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8202 elem->name);
8203#endif
8204 return(ret);
8205}
8206
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008207/************************************************************************
8208 * *
8209 * Generic interpreted validation implementation *
8210 * *
8211 ************************************************************************/
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008212static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8213 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008214
8215/**
8216 * xmlRelaxNGSkipIgnored:
8217 * @ctxt: a schema validation context
8218 * @node: the top node.
8219 *
8220 * Skip ignorable nodes in that context
8221 *
8222 * Returns the new sibling or NULL in case of error.
8223 */
8224static xmlNodePtr
8225xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
8226 xmlNodePtr node) {
8227 /*
8228 * TODO complete and handle entities
8229 */
8230 while ((node != NULL) &&
8231 ((node->type == XML_COMMENT_NODE) ||
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008232 (node->type == XML_PI_NODE) ||
Daniel Veillard39eb88b2003-03-11 11:21:28 +00008233 (((node->type == XML_TEXT_NODE) ||
8234 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008235 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8236 (IS_BLANK_NODE(node)))))) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00008237 node = node->next;
8238 }
8239 return(node);
8240}
8241
8242/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008243 * xmlRelaxNGNormalize:
8244 * @ctxt: a schema validation context
8245 * @str: the string to normalize
8246 *
8247 * Implements the normalizeWhiteSpace( s ) function from
8248 * section 6.2.9 of the spec
8249 *
8250 * Returns the new string or NULL in case of error.
8251 */
8252static xmlChar *
8253xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) {
8254 xmlChar *ret, *p;
8255 const xmlChar *tmp;
8256 int len;
8257
8258 if (str == NULL)
8259 return(NULL);
8260 tmp = str;
8261 while (*tmp != 0) tmp++;
8262 len = tmp - str;
8263
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008264 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008265 if (ret == NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00008266 if (ctxt != NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008267 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
Daniel Veillardea3f3982003-01-26 19:45:18 +00008268 } else {
8269 xmlGenericError(xmlGenericErrorContext,
8270 "xmlRelaxNGNormalize: out of memory\n");
8271 }
Daniel Veillardedc91922003-01-26 00:52:04 +00008272 return(NULL);
8273 }
8274 p = ret;
8275 while (IS_BLANK(*str)) str++;
8276 while (*str != 0) {
8277 if (IS_BLANK(*str)) {
8278 while (IS_BLANK(*str)) str++;
8279 if (*str == 0)
8280 break;
8281 *p++ = ' ';
8282 } else
8283 *p++ = *str++;
8284 }
8285 *p = 0;
8286 return(ret);
8287}
8288
8289/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008290 * xmlRelaxNGValidateDatatype:
8291 * @ctxt: a Relax-NG validation context
8292 * @value: the string value
8293 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008294 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008295 *
8296 * Validate the given value against the dataype
8297 *
8298 * Returns 0 if the validation succeeded or an error code.
8299 */
8300static int
8301xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008302 xmlRelaxNGDefinePtr define, xmlNodePtr node) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008303 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008304 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008305 void *result = NULL;
8306 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008307
8308 if ((define == NULL) || (define->data == NULL)) {
8309 return(-1);
8310 }
8311 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008312 if (lib->check != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008313 if ((define->attrs != NULL) &&
8314 (define->attrs->type == XML_RELAXNG_PARAM)) {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008315 ret = lib->check(lib->data, define->name, value, &result, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008316 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008317 ret = lib->check(lib->data, define->name, value, NULL, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008318 }
8319 } else
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008320 ret = -1;
8321 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008322 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008323 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8324 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008325 return(-1);
8326 } else if (ret == 1) {
8327 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008328 } else if (ret == 2) {
8329 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008330 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008331 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008332 ret = -1;
8333 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008334 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008335 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
8336 if (lib->facet != NULL) {
8337 tmp = lib->facet(lib->data, define->name, cur->name,
8338 cur->value, value, result);
8339 if (tmp != 0)
8340 ret = -1;
8341 }
8342 cur = cur->next;
8343 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008344 if ((ret == 0) && (define->content != NULL)) {
8345 const xmlChar *oldvalue, *oldendvalue;
8346
8347 oldvalue = ctxt->state->value;
8348 oldendvalue = ctxt->state->endvalue;
8349 ctxt->state->value = (xmlChar *) value;
8350 ctxt->state->endvalue = NULL;
8351 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8352 ctxt->state->value = (xmlChar *) oldvalue;
8353 ctxt->state->endvalue = (xmlChar *) oldendvalue;
8354 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008355 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8356 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008357 return(ret);
8358}
8359
8360/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008361 * xmlRelaxNGNextValue:
8362 * @ctxt: a Relax-NG validation context
8363 *
8364 * Skip to the next value when validating within a list
8365 *
8366 * Returns 0 if the operation succeeded or an error code.
8367 */
8368static int
8369xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) {
8370 xmlChar *cur;
8371
8372 cur = ctxt->state->value;
8373 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
8374 ctxt->state->value = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00008375 ctxt->state->endvalue = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008376 return(0);
8377 }
8378 while (*cur != 0) cur++;
8379 while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++;
8380 if (cur == ctxt->state->endvalue)
8381 ctxt->state->value = NULL;
8382 else
8383 ctxt->state->value = cur;
8384 return(0);
8385}
8386
8387/**
8388 * xmlRelaxNGValidateValueList:
8389 * @ctxt: a Relax-NG validation context
8390 * @defines: the list of definitions to verify
8391 *
8392 * Validate the given set of definitions for the current value
8393 *
8394 * Returns 0 if the validation succeeded or an error code.
8395 */
8396static int
8397xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8398 xmlRelaxNGDefinePtr defines) {
8399 int ret = 0;
8400
8401 while (defines != NULL) {
8402 ret = xmlRelaxNGValidateValue(ctxt, defines);
8403 if (ret != 0)
8404 break;
8405 defines = defines->next;
8406 }
8407 return(ret);
8408}
8409
8410/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008411 * xmlRelaxNGValidateValue:
8412 * @ctxt: a Relax-NG validation context
8413 * @define: the definition to verify
8414 *
8415 * Validate the given definition for the current value
8416 *
8417 * Returns 0 if the validation succeeded or an error code.
8418 */
8419static int
8420xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8421 xmlRelaxNGDefinePtr define) {
Daniel Veillardedc91922003-01-26 00:52:04 +00008422 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008423 xmlChar *value;
8424
8425 value = ctxt->state->value;
8426 switch (define->type) {
Daniel Veillardd4310742003-02-18 21:12:46 +00008427 case XML_RELAXNG_EMPTY: {
8428 if ((value != NULL) && (value[0] != 0)) {
8429 int idx = 0;
8430
8431 while (IS_BLANK(value[idx]))
8432 idx++;
8433 if (value[idx] != 0)
8434 ret = -1;
8435 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008436 break;
Daniel Veillardd4310742003-02-18 21:12:46 +00008437 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008438 case XML_RELAXNG_TEXT:
8439 break;
Daniel Veillardedc91922003-01-26 00:52:04 +00008440 case XML_RELAXNG_VALUE: {
8441 if (!xmlStrEqual(value, define->value)) {
8442 if (define->name != NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00008443 xmlRelaxNGTypeLibraryPtr lib;
8444
8445 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00008446 if ((lib != NULL) && (lib->comp != NULL)) {
8447 ret = lib->comp(lib->data, define->name,
8448 define->value, define->node,
8449 (void *) define->attrs,
8450 value, ctxt->state->node);
8451 } else
Daniel Veillardea3f3982003-01-26 19:45:18 +00008452 ret = -1;
8453 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008454 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name);
Daniel Veillardea3f3982003-01-26 19:45:18 +00008455 return(-1);
8456 } else if (ret == 1) {
8457 ret = 0;
8458 } else {
8459 ret = -1;
8460 }
Daniel Veillardedc91922003-01-26 00:52:04 +00008461 } else {
8462 xmlChar *nval, *nvalue;
8463
8464 /*
8465 * TODO: trivial optimizations are possible by
8466 * computing at compile-time
8467 */
8468 nval = xmlRelaxNGNormalize(ctxt, define->value);
8469 nvalue = xmlRelaxNGNormalize(ctxt, value);
8470
Daniel Veillardea3f3982003-01-26 19:45:18 +00008471 if ((nval == NULL) || (nvalue == NULL) ||
8472 (!xmlStrEqual(nval, nvalue)))
Daniel Veillardedc91922003-01-26 00:52:04 +00008473 ret = -1;
8474 if (nval != NULL)
8475 xmlFree(nval);
8476 if (nvalue != NULL)
8477 xmlFree(nvalue);
8478 }
8479 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008480 if (ret == 0)
8481 xmlRelaxNGNextValue(ctxt);
Daniel Veillardedc91922003-01-26 00:52:04 +00008482 break;
8483 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008484 case XML_RELAXNG_DATATYPE: {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008485 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8486 ctxt->state->seq);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008487 if (ret == 0)
8488 xmlRelaxNGNextValue(ctxt);
8489
8490 break;
8491 }
8492 case XML_RELAXNG_CHOICE: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008493 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008494 xmlChar *oldvalue;
8495
8496 oldflags = ctxt->flags;
8497 ctxt->flags |= FLAGS_IGNORABLE;
8498
8499 oldvalue = ctxt->state->value;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008500 while (list != NULL) {
8501 ret = xmlRelaxNGValidateValue(ctxt, list);
8502 if (ret == 0) {
8503 break;
8504 }
8505 ctxt->state->value = oldvalue;
8506 list = list->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008507 }
8508 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00008509 if (ret != 0) {
8510 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8511 xmlRelaxNGDumpValidError(ctxt);
8512 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008513 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00008514 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008515 if (ret == 0)
8516 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008517 break;
8518 }
8519 case XML_RELAXNG_LIST: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008520 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008521 xmlChar *oldvalue, *oldend, *val, *cur;
Daniel Veillard416589a2003-02-17 17:25:42 +00008522#ifdef DEBUG_LIST
8523 int nb_values = 0;
8524#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008525
8526 oldvalue = ctxt->state->value;
8527 oldend = ctxt->state->endvalue;
8528
8529 val = xmlStrdup(oldvalue);
8530 if (val == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00008531 val = xmlStrdup(BAD_CAST "");
8532 }
8533 if (val == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008534 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008535 return(-1);
8536 }
8537 cur = val;
8538 while (*cur != 0) {
Daniel Veillard416589a2003-02-17 17:25:42 +00008539 if (IS_BLANK(*cur)) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008540 *cur = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008541 cur++;
8542#ifdef DEBUG_LIST
8543 nb_values++;
8544#endif
8545 while (IS_BLANK(*cur))
8546 *cur++ = 0;
8547 } else
8548 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008549 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008550#ifdef DEBUG_LIST
8551 xmlGenericError(xmlGenericErrorContext,
8552 "list value: '%s' found %d items\n", oldvalue, nb_values);
8553 nb_values = 0;
8554#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008555 ctxt->state->endvalue = cur;
8556 cur = val;
8557 while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008558
Daniel Veillardfd573f12003-03-16 17:52:32 +00008559 ctxt->state->value = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008560
Daniel Veillardfd573f12003-03-16 17:52:32 +00008561 while (list != NULL) {
8562 if (ctxt->state->value == ctxt->state->endvalue)
8563 ctxt->state->value = NULL;
8564 ret = xmlRelaxNGValidateValue(ctxt, list);
8565 if (ret != 0) {
8566#ifdef DEBUG_LIST
8567 xmlGenericError(xmlGenericErrorContext,
8568 "Failed to validate value: '%s' with %d rule\n",
8569 ctxt->state->value, nb_values);
8570#endif
8571 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008572 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008573#ifdef DEBUG_LIST
8574 nb_values++;
8575#endif
8576 list = list->next;
8577 }
8578
8579 if ((ret == 0) && (ctxt->state->value != NULL) &&
8580 (ctxt->state->value != ctxt->state->endvalue)) {
8581 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value);
8582 ret = -1;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008583 }
8584 xmlFree(val);
8585 ctxt->state->value = oldvalue;
8586 ctxt->state->endvalue = oldend;
8587 break;
8588 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008589 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008590 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8591 if (ret != 0) {
8592 break;
8593 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008594 /* no break on purpose */
8595 case XML_RELAXNG_ZEROORMORE: {
8596 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008597
8598 oldflags = ctxt->flags;
8599 ctxt->flags |= FLAGS_IGNORABLE;
8600 cur = ctxt->state->value;
8601 temp = NULL;
8602 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8603 (temp != cur)) {
8604 temp = cur;
8605 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8606 if (ret != 0) {
8607 ctxt->state->value = temp;
8608 ret = 0;
8609 break;
8610 }
8611 cur = ctxt->state->value;
8612 }
8613 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00008614 if (ret != 0) {
8615 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8616 xmlRelaxNGDumpValidError(ctxt);
8617 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008618 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00008619 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008620 break;
8621 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008622 case XML_RELAXNG_EXCEPT: {
8623 xmlRelaxNGDefinePtr list;
8624
8625 list = define->content;
8626 while (list != NULL) {
8627 ret = xmlRelaxNGValidateValue(ctxt, list);
8628 if (ret == 0) {
8629 ret = -1;
8630 break;
8631 } else
8632 ret = 0;
8633 list = list->next;
8634 }
8635 break;
8636 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008637 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008638 case XML_RELAXNG_GROUP: {
8639 xmlRelaxNGDefinePtr list;
8640
8641 list = define->content;
8642 while (list != NULL) {
8643 ret = xmlRelaxNGValidateValue(ctxt, list);
8644 if (ret != 0) {
8645 ret = -1;
8646 break;
8647 } else
8648 ret = 0;
8649 list = list->next;
8650 }
Daniel Veillardd4310742003-02-18 21:12:46 +00008651 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008652 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008653 case XML_RELAXNG_REF:
8654 case XML_RELAXNG_PARENTREF:
8655 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8656 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008657 default:
8658 TODO
8659 ret = -1;
8660 }
8661 return(ret);
8662}
8663
8664/**
8665 * xmlRelaxNGValidateValueContent:
8666 * @ctxt: a Relax-NG validation context
8667 * @defines: the list of definitions to verify
8668 *
8669 * Validate the given definitions for the current value
8670 *
8671 * Returns 0 if the validation succeeded or an error code.
8672 */
8673static int
8674xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8675 xmlRelaxNGDefinePtr defines) {
8676 int ret = 0;
8677
8678 while (defines != NULL) {
8679 ret = xmlRelaxNGValidateValue(ctxt, defines);
8680 if (ret != 0)
8681 break;
8682 defines = defines->next;
8683 }
8684 return(ret);
8685}
8686
8687/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008688 * xmlRelaxNGAttributeMatch:
8689 * @ctxt: a Relax-NG validation context
8690 * @define: the definition to check
8691 * @prop: the attribute
8692 *
8693 * Check if the attribute matches the definition nameClass
8694 *
8695 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8696 */
8697static int
8698xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8699 xmlRelaxNGDefinePtr define,
8700 xmlAttrPtr prop) {
8701 int ret;
8702
8703 if (define->name != NULL) {
8704 if (!xmlStrEqual(define->name, prop->name))
8705 return(0);
8706 }
8707 if (define->ns != NULL) {
8708 if (define->ns[0] == 0) {
8709 if (prop->ns != NULL)
8710 return(0);
8711 } else {
8712 if ((prop->ns == NULL) ||
8713 (!xmlStrEqual(define->ns, prop->ns->href)))
8714 return(0);
8715 }
8716 }
8717 if (define->nameClass == NULL)
8718 return(1);
8719 define = define->nameClass;
8720 if (define->type == XML_RELAXNG_EXCEPT) {
8721 xmlRelaxNGDefinePtr list;
8722
8723 list = define->content;
8724 while (list != NULL) {
8725 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8726 if (ret == 1)
8727 return(0);
8728 if (ret < 0)
8729 return(ret);
8730 list = list->next;
8731 }
8732 } else {
8733 TODO
8734 }
8735 return(1);
8736}
8737
8738/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008739 * xmlRelaxNGValidateAttribute:
8740 * @ctxt: a Relax-NG validation context
8741 * @define: the definition to verify
8742 *
8743 * Validate the given attribute definition for that node
8744 *
8745 * Returns 0 if the validation succeeded or an error code.
8746 */
8747static int
8748xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8749 xmlRelaxNGDefinePtr define) {
8750 int ret = 0, i;
8751 xmlChar *value, *oldvalue;
8752 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008753 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008754
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008755 if (ctxt->state->nbAttrLeft <= 0)
8756 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008757 if (define->name != NULL) {
8758 for (i = 0;i < ctxt->state->nbAttrs;i++) {
8759 tmp = ctxt->state->attrs[i];
8760 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8761 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8762 (tmp->ns == NULL)) ||
8763 ((tmp->ns != NULL) &&
8764 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8765 prop = tmp;
8766 break;
8767 }
8768 }
8769 }
8770 if (prop != NULL) {
8771 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8772 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008773 oldseq = ctxt->state->seq;
8774 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008775 ctxt->state->value = value;
Daniel Veillard231d7912003-02-09 14:22:17 +00008776 ctxt->state->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008777 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00008778 if (ctxt->state->value != NULL)
8779 value = ctxt->state->value;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008780 if (value != NULL)
8781 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00008782 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008783 ctxt->state->seq = oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008784 if (ret == 0) {
8785 /*
8786 * flag the attribute as processed
8787 */
8788 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008789 ctxt->state->nbAttrLeft--;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008790 }
8791 } else {
8792 ret = -1;
8793 }
8794#ifdef DEBUG
8795 xmlGenericError(xmlGenericErrorContext,
8796 "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret);
8797#endif
8798 } else {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008799 for (i = 0;i < ctxt->state->nbAttrs;i++) {
8800 tmp = ctxt->state->attrs[i];
Daniel Veillard144fae12003-02-03 13:17:57 +00008801 if ((tmp != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00008802 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008803 prop = tmp;
8804 break;
8805 }
8806 }
8807 if (prop != NULL) {
8808 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8809 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008810 oldseq = ctxt->state->seq;
8811 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008812 ctxt->state->value = value;
8813 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00008814 if (ctxt->state->value != NULL)
8815 value = ctxt->state->value;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008816 if (value != NULL)
8817 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00008818 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008819 ctxt->state->seq = oldseq;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008820 if (ret == 0) {
8821 /*
8822 * flag the attribute as processed
8823 */
8824 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008825 ctxt->state->nbAttrLeft--;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008826 }
8827 } else {
8828 ret = -1;
8829 }
8830#ifdef DEBUG
Daniel Veillard144fae12003-02-03 13:17:57 +00008831 if (define->ns != NULL) {
8832 xmlGenericError(xmlGenericErrorContext,
8833 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8834 define->ns, ret);
8835 } else {
8836 xmlGenericError(xmlGenericErrorContext,
8837 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8838 ret);
8839 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008840#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008841 }
8842
8843 return(ret);
8844}
8845
8846/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008847 * xmlRelaxNGValidateAttributeList:
8848 * @ctxt: a Relax-NG validation context
8849 * @define: the list of definition to verify
8850 *
8851 * Validate the given node against the list of attribute definitions
8852 *
8853 * Returns 0 if the validation succeeded or an error code.
8854 */
8855static int
8856xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8857 xmlRelaxNGDefinePtr defines) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00008858 int ret = 0, res;
8859 int needmore = 0;
8860 xmlRelaxNGDefinePtr cur;
8861
8862 cur = defines;
8863 while (cur != NULL) {
8864 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
8865 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8866 ret = -1;
8867 } else
8868 needmore = 1;
8869 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008870 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008871 if (!needmore)
8872 return(ret);
8873 cur = defines;
8874 while (cur != NULL) {
8875 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
8876 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8877 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8878 if (res < 0)
8879 ret = -1;
8880 } else {
8881 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8882 return(-1);
8883 }
8884 if (res == -1) /* continues on -2 */
8885 break;
8886 }
8887 cur = cur->next;
8888 }
8889
Daniel Veillardfd573f12003-03-16 17:52:32 +00008890 return(ret);
8891}
8892
8893/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008894 * xmlRelaxNGNodeMatchesList:
8895 * @node: the node
8896 * @list: a NULL terminated array of definitions
8897 *
8898 * Check if a node can be matched by one of the definitions
8899 *
8900 * Returns 1 if matches 0 otherwise
8901 */
8902static int
8903xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) {
8904 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008905 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008906
8907 if ((node == NULL) || (list == NULL))
8908 return(0);
8909
8910 cur = list[i++];
8911 while (cur != NULL) {
8912 if ((node->type == XML_ELEMENT_NODE) &&
8913 (cur->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008914 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8915 if (tmp == 1)
8916 return(1);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00008917 } else if (((node->type == XML_TEXT_NODE) ||
8918 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008919 (cur->type == XML_RELAXNG_TEXT)) {
8920 return(1);
8921 }
8922 cur = list[i++];
8923 }
8924 return(0);
8925}
8926
8927/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008928 * xmlRelaxNGValidateInterleave:
8929 * @ctxt: a Relax-NG validation context
8930 * @define: the definition to verify
8931 *
8932 * Validate an interleave definition for a node.
8933 *
8934 * Returns 0 if the validation succeeded or an error code.
8935 */
8936static int
8937xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
8938 xmlRelaxNGDefinePtr define) {
William M. Brack779af002003-08-01 15:55:39 +00008939 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008940 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008941 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008942
8943 xmlRelaxNGValidStatePtr oldstate;
8944 xmlRelaxNGPartitionPtr partitions;
8945 xmlRelaxNGInterleaveGroupPtr group = NULL;
8946 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
8947 xmlNodePtr *list = NULL, *lasts = NULL;
8948
8949 if (define->data != NULL) {
8950 partitions = (xmlRelaxNGPartitionPtr) define->data;
8951 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008952 } else {
8953 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
8954 return(-1);
8955 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008956 /*
8957 * Optimizations for MIXED
8958 */
8959 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00008960 if (define->dflags & IS_MIXED) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008961 ctxt->flags |= FLAGS_MIXED_CONTENT;
8962 if (nbgroups == 2) {
8963 /*
8964 * this is a pure <mixed> case
8965 */
8966 if (ctxt->state != NULL)
8967 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8968 ctxt->state->seq);
8969 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
8970 ret = xmlRelaxNGValidateDefinition(ctxt,
8971 partitions->groups[1]->rule);
8972 else
8973 ret = xmlRelaxNGValidateDefinition(ctxt,
8974 partitions->groups[0]->rule);
8975 if (ret == 0) {
8976 if (ctxt->state != NULL)
8977 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8978 ctxt->state->seq);
8979 }
8980 ctxt->flags = oldflags;
8981 return(ret);
8982 }
8983 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008984
8985 /*
8986 * Build arrays to store the first and last node of the chain
8987 * pertaining to each group
8988 */
8989 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8990 if (list == NULL) {
8991 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8992 return(-1);
8993 }
8994 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
8995 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8996 if (lasts == NULL) {
8997 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8998 return(-1);
8999 }
9000 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9001
9002 /*
9003 * Walk the sequence of children finding the right group and
9004 * sorting them in sequences.
9005 */
9006 cur = ctxt->state->seq;
9007 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9008 start = cur;
9009 while (cur != NULL) {
9010 ctxt->state->seq = cur;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009011 if ((partitions->triage != NULL) &&
9012 (partitions->flags & IS_DETERMINIST)) {
9013 void *tmp = NULL;
9014
9015 if ((cur->type == XML_TEXT_NODE) ||
9016 (cur->type == XML_CDATA_SECTION_NODE)) {
9017 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9018 NULL);
9019 } else if (cur->type == XML_ELEMENT_NODE) {
9020 if (cur->ns != NULL) {
9021 tmp = xmlHashLookup2(partitions->triage, cur->name,
9022 cur->ns->href);
9023 if (tmp == NULL)
9024 tmp = xmlHashLookup2(partitions->triage,
9025 BAD_CAST "#any", cur->ns->href);
9026 } else
9027 tmp = xmlHashLookup2(partitions->triage, cur->name, NULL);
9028 if (tmp == NULL)
9029 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9030 NULL);
9031 }
9032
9033 if (tmp == NULL) {
9034 i = nbgroups;
9035 } else {
9036 i = ((long) tmp) - 1;
9037 if (partitions->flags & IS_NEEDCHECK) {
9038 group = partitions->groups[i];
9039 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9040 i = nbgroups;
9041 }
9042 }
9043 } else {
9044 for (i = 0;i < nbgroups;i++) {
9045 group = partitions->groups[i];
9046 if (group == NULL)
9047 continue;
9048 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9049 break;
9050 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009051 }
9052 /*
9053 * We break as soon as an element not matched is found
9054 */
9055 if (i >= nbgroups) {
9056 break;
9057 }
9058 if (lasts[i] != NULL) {
9059 lasts[i]->next = cur;
9060 lasts[i] = cur;
9061 } else {
9062 list[i] = cur;
9063 lasts[i] = cur;
9064 }
9065 if (cur->next != NULL)
9066 lastchg = cur->next;
9067 else
9068 lastchg = cur;
9069 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
9070 }
9071 if (ret != 0) {
9072 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9073 ret = -1;
9074 goto done;
9075 }
9076 lastelem = cur;
9077 oldstate = ctxt->state;
9078 for (i = 0;i < nbgroups;i++) {
9079 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9080 group = partitions->groups[i];
9081 if (lasts[i] != NULL) {
9082 last = lasts[i]->next;
9083 lasts[i]->next = NULL;
9084 }
9085 ctxt->state->seq = list[i];
9086 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9087 if (ret != 0)
9088 break;
9089 if (ctxt->state != NULL) {
9090 cur = ctxt->state->seq;
9091 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
Daniel Veillard798024a2003-03-19 10:36:09 +00009092 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009093 oldstate = ctxt->state;
9094 ctxt->state = NULL;
9095 if (cur != NULL) {
9096 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9097 ret = -1;
9098 ctxt->state = oldstate;
9099 goto done;
9100 }
9101 } else if (ctxt->states != NULL) {
9102 int j;
9103 int found = 0;
9104
9105 for (j = 0;j < ctxt->states->nbState;j++) {
9106 cur = ctxt->states->tabState[j]->seq;
9107 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9108 if (cur == NULL) {
9109 found = 1;
9110 break;
9111 }
9112 }
9113 if (ctxt->states->nbState > 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00009114 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009115 oldstate = ctxt->states->tabState[ctxt->states->nbState - 1];
9116 }
9117 for (j = 0;j < ctxt->states->nbState - 1;j++) {
Daniel Veillard798024a2003-03-19 10:36:09 +00009118 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009119 }
9120 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9121 ctxt->states = NULL;
9122 if (found == 0) {
9123 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9124 ret = -1;
9125 ctxt->state = oldstate;
9126 goto done;
9127 }
9128 } else {
9129 ret = -1;
9130 break;
9131 }
9132 if (lasts[i] != NULL) {
9133 lasts[i]->next = last;
9134 }
9135 }
9136 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00009137 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009138 ctxt->state = oldstate;
9139 ctxt->state->seq = lastelem;
9140 if (ret != 0) {
9141 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9142 ret = -1;
9143 goto done;
9144 }
9145
9146done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009147 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009148 /*
9149 * builds the next links chain from the prev one
9150 */
9151 cur = lastchg;
9152 while (cur != NULL) {
9153 if ((cur == start) || (cur->prev == NULL))
9154 break;
9155 cur->prev->next = cur;
9156 cur = cur->prev;
9157 }
9158 if (ret == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00009159 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009160 }
9161
9162 xmlFree(list);
9163 xmlFree(lasts);
9164 return(ret);
9165}
9166
9167/**
9168 * xmlRelaxNGValidateDefinitionList:
9169 * @ctxt: a Relax-NG validation context
9170 * @define: the list of definition to verify
9171 *
9172 * Validate the given node content against the (list) of definitions
9173 *
9174 * Returns 0 if the validation succeeded or an error code.
9175 */
9176static int
9177xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9178 xmlRelaxNGDefinePtr defines) {
9179 int ret = 0, res;
9180
9181
Daniel Veillard952379b2003-03-17 15:37:12 +00009182 if (defines == NULL) {
9183 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list");
9184 return(-1);
9185 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009186 while (defines != NULL) {
9187 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9188 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9189 if (res < 0)
9190 ret = -1;
9191 } else {
9192 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9193 return(-1);
9194 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00009195 if (res == -1) /* continues on -2 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009196 break;
9197 defines = defines->next;
9198 }
9199
9200 return(ret);
9201}
9202
9203/**
9204 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009205 * @ctxt: a Relax-NG validation context
9206 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009207 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009208 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009209 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009210 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009211 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009212 */
9213static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00009214xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9215 xmlRelaxNGDefinePtr define,
9216 xmlNodePtr elem) {
Daniel Veillard580ced82003-03-21 21:22:48 +00009217 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009218
Daniel Veillardfd573f12003-03-16 17:52:32 +00009219 if (define->name != NULL) {
9220 if (!xmlStrEqual(elem->name, define->name)) {
9221 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9222 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009223 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009224 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009225 if ((define->ns != NULL) && (define->ns[0] != 0)) {
9226 if (elem->ns == NULL) {
9227 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS,
9228 elem->name);
9229 return(0);
9230 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9231 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9232 elem->name, define->ns);
9233 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009234 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009235 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
9236 (define->name == NULL)) {
9237 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
9238 elem->name);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009239 return(0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009240 } else if ((elem->ns != NULL) && (define->name != NULL)) {
9241 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
9242 define->name);
9243 return(0);
9244 }
9245
9246 if (define->nameClass == NULL)
9247 return(1);
9248
9249 define = define->nameClass;
9250 if (define->type == XML_RELAXNG_EXCEPT) {
9251 xmlRelaxNGDefinePtr list;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009252 if (ctxt != NULL) {
9253 oldflags = ctxt->flags;
9254 ctxt->flags |= FLAGS_IGNORABLE;
9255 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009256
9257 list = define->content;
9258 while (list != NULL) {
9259 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9260 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009261 if (ctxt != NULL)
9262 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009263 return(0);
9264 }
9265 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009266 if (ctxt != NULL)
9267 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009268 return(ret);
9269 }
9270 list = list->next;
9271 }
9272 ret = 1;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009273 if (ctxt != NULL) {
9274 ctxt->flags = oldflags;
9275 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009276 } else if (define->type == XML_RELAXNG_CHOICE) {
9277 xmlRelaxNGDefinePtr list;
9278
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009279 if (ctxt != NULL) {
9280 oldflags = ctxt->flags;
9281 ctxt->flags |= FLAGS_IGNORABLE;
9282 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009283
9284 list = define->nameClass;
9285 while (list != NULL) {
9286 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9287 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009288 if (ctxt != NULL)
9289 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009290 return(1);
9291 }
9292 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009293 if (ctxt != NULL)
9294 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009295 return(ret);
9296 }
9297 list = list->next;
9298 }
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009299 if (ctxt != NULL) {
9300 if (ret != 0) {
9301 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9302 xmlRelaxNGDumpValidError(ctxt);
9303 } else {
9304 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
9305 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009306 }
9307 ret = 0;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009308 if (ctxt != NULL) {
9309 ctxt->flags = oldflags;
9310 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009311 } else {
9312 TODO
9313 ret = -1;
9314 }
9315 return(ret);
9316}
9317
9318/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009319 * xmlRelaxNGBestState:
9320 * @ctxt: a Relax-NG validation context
9321 *
9322 * Find the "best" state in the ctxt->states list of states to report
9323 * errors about. I.e. a state with no element left in the child list
9324 * or the one with the less attributes left.
9325 * This is called only if a falidation error was detected
9326 *
9327 * Returns the index of the "best" state or -1 in case of error
9328 */
9329static int
9330xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt) {
9331 xmlRelaxNGValidStatePtr state;
9332 int i, tmp;
9333 int best = -1;
9334 int value = 1000000;
9335
9336 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9337 (ctxt->states->nbState <= 0))
9338 return(-1);
9339
9340 for (i = 0;i < ctxt->states->nbState;i++) {
9341 state = ctxt->states->tabState[i];
9342 if (state == NULL)
9343 continue;
9344 if (state->seq != NULL) {
9345 if ((best == -1) || (value > 100000)) {
9346 value = 100000;
9347 best = i;
9348 }
9349 } else {
9350 tmp = state->nbAttrLeft;
9351 if ((best == -1) || (value > tmp)) {
9352 value = tmp;
9353 best = i;
9354 }
9355 }
9356 }
9357 return(best);
9358}
9359
9360/**
9361 * xmlRelaxNGLogBestError:
9362 * @ctxt: a Relax-NG validation context
9363 *
9364 * Find the "best" state in the ctxt->states list of states to report
9365 * errors about and log it.
9366 */
9367static void
9368xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt) {
9369 int best;
9370
9371 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9372 (ctxt->states->nbState <= 0))
9373 return;
9374
9375 best = xmlRelaxNGBestState(ctxt);
9376 if ((best >= 0) && (best < ctxt->states->nbState)) {
9377 ctxt->state = ctxt->states->tabState[best];
9378
9379 xmlRelaxNGValidateElementEnd(ctxt, 1);
9380 }
9381}
9382
9383/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009384 * xmlRelaxNGValidateElementEnd:
9385 * @ctxt: a Relax-NG validation context
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009386 * @log: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009387 *
9388 * Validate the end of the element, implements check that
9389 * there is nothing left not consumed in the element content
9390 * or in the attribute list.
9391 *
9392 * Returns 0 if the validation succeeded or an error code.
9393 */
9394static int
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009395xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int log) {
9396 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009397 xmlRelaxNGValidStatePtr state;
9398
9399 state = ctxt->state;
9400 if (state->seq != NULL) {
9401 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9402 if (state->seq != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009403 if (log) {
9404 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9405 state->node->name, state->seq->name);
9406 }
9407 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009408 }
9409 }
9410 for (i = 0;i < state->nbAttrs;i++) {
9411 if (state->attrs[i] != NULL) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009412 if (log) {
9413 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9414 state->attrs[i]->name, state->node->name);
9415 }
9416 return(-1 -i);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009417 }
9418 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009419 return(0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009420}
9421
9422/**
9423 * xmlRelaxNGValidateState:
9424 * @ctxt: a Relax-NG validation context
9425 * @define: the definition to verify
9426 *
9427 * Validate the current state against the definition
9428 *
9429 * Returns 0 if the validation succeeded or an error code.
9430 */
9431static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009432xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9433 xmlRelaxNGDefinePtr define)
9434{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009435 xmlNodePtr node;
9436 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009437 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009438
9439 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009440 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9441 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009442 }
9443
9444 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009445 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009446 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009447 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009448 }
9449#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009450 for (i = 0; i < ctxt->depth; i++)
9451 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009452 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009453 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009454 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009455 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009456 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009457 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009458 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009459 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009460#endif
9461 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009462 switch (define->type) {
9463 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009464 node = xmlRelaxNGSkipIgnored(ctxt, node);
9465 ret = 0;
9466 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009467 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009468 ret = -1;
9469 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009470 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009471 while ((node != NULL) &&
9472 ((node->type == XML_TEXT_NODE) ||
9473 (node->type == XML_COMMENT_NODE) ||
9474 (node->type == XML_PI_NODE) ||
9475 (node->type == XML_CDATA_SECTION_NODE)))
9476 node = node->next;
9477 ctxt->state->seq = node;
9478 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009479 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009480 errNr = ctxt->errNr;
9481 node = xmlRelaxNGSkipIgnored(ctxt, node);
9482 if (node == NULL) {
9483 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9484 ret = -1;
9485 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9486 xmlRelaxNGDumpValidError(ctxt);
9487 break;
9488 }
9489 if (node->type != XML_ELEMENT_NODE) {
9490 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9491 ret = -1;
9492 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9493 xmlRelaxNGDumpValidError(ctxt);
9494 break;
9495 }
9496 /*
9497 * This node was already validated successfully against
9498 * this definition.
9499 */
9500 if (node->_private == define) {
9501 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9502 if (ctxt->errNr > errNr)
9503 xmlRelaxNGPopErrors(ctxt, errNr);
9504 if (ctxt->errNr != 0) {
9505 while ((ctxt->err != NULL) &&
9506 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9507 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9508 ||
9509 ((ctxt->err->err ==
9510 XML_RELAXNG_ERR_ELEMEXTRANS)
9511 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9512 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9513 || (ctxt->err->err ==
9514 XML_RELAXNG_ERR_NOTELEM)))
9515 xmlRelaxNGValidErrorPop(ctxt);
9516 }
9517 break;
9518 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009519
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009520 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9521 if (ret <= 0) {
9522 ret = -1;
9523 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9524 xmlRelaxNGDumpValidError(ctxt);
9525 break;
9526 }
9527 ret = 0;
9528 if (ctxt->errNr != 0) {
9529 if (ctxt->errNr > errNr)
9530 xmlRelaxNGPopErrors(ctxt, errNr);
9531 while ((ctxt->err != NULL) &&
9532 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9533 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9534 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9535 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9536 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9537 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9538 xmlRelaxNGValidErrorPop(ctxt);
9539 }
9540 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009541
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009542 oldflags = ctxt->flags;
9543 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9544 ctxt->flags -= FLAGS_MIXED_CONTENT;
9545 }
9546 state = xmlRelaxNGNewValidState(ctxt, node);
9547 if (state == NULL) {
9548 ret = -1;
9549 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9550 xmlRelaxNGDumpValidError(ctxt);
9551 break;
9552 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009553
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009554 oldstate = ctxt->state;
9555 ctxt->state = state;
9556 if (define->attrs != NULL) {
9557 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9558 if (tmp != 0) {
9559 ret = -1;
9560 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9561 }
9562 }
9563 if (define->contModel != NULL) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009564 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9565 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9566 xmlNodePtr nseq;
9567
9568 nstate = xmlRelaxNGNewValidState(ctxt, node);
9569 ctxt->state = nstate;
9570 ctxt->states = NULL;
9571
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009572 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9573 define->contModel,
9574 ctxt->state->seq);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009575 nseq = ctxt->state->seq;
9576 ctxt->state = tmpstate;
9577 ctxt->states = tmpstates;
9578 xmlRelaxNGFreeValidState(ctxt, nstate);
9579
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009580#ifdef DEBUG_COMPILE
9581 xmlGenericError(xmlGenericErrorContext,
9582 "Validating content of '%s' : %d\n", define->name, tmp);
9583#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009584 if (tmp != 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009585 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009586
9587 if (ctxt->states != NULL) {
9588 tmp = -1;
9589
Daniel Veillardce192eb2003-04-16 15:58:05 +00009590 for (i = 0; i < ctxt->states->nbState; i++) {
9591 state = ctxt->states->tabState[i];
9592 ctxt->state = state;
9593 ctxt->state->seq = nseq;
9594
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009595 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009596 tmp = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009597 break;
9598 }
9599 }
9600 if (tmp != 0) {
9601 /*
9602 * validation error, log the message for the "best" one
9603 */
9604 ctxt->flags |= FLAGS_IGNORABLE;
9605 xmlRelaxNGLogBestError(ctxt);
9606 }
9607 for (i = 0; i < ctxt->states->nbState; i++) {
9608 xmlRelaxNGFreeValidState(ctxt,
9609 ctxt->states->tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009610 }
9611 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9612 ctxt->flags = oldflags;
9613 ctxt->states = NULL;
9614 if ((ret == 0) && (tmp == -1))
9615 ret = -1;
9616 } else {
9617 state = ctxt->state;
9618 ctxt->state->seq = nseq;
9619 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009620 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009621 xmlRelaxNGFreeValidState(ctxt, state);
9622 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009623 } else {
9624 if (define->content != NULL) {
9625 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
9626 define->content);
9627 if (tmp != 0) {
9628 ret = -1;
9629 if (ctxt->state == NULL) {
9630 ctxt->state = oldstate;
9631 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9632 node->name);
9633 ctxt->state = NULL;
9634 } else {
9635 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9636 node->name);
9637 }
9638
9639 }
9640 }
9641 if (ctxt->states != NULL) {
9642 tmp = -1;
9643
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009644 for (i = 0; i < ctxt->states->nbState; i++) {
9645 state = ctxt->states->tabState[i];
9646 ctxt->state = state;
9647
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009648 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009649 tmp = 0;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009650 break;
9651 }
9652 }
9653 if (tmp != 0) {
9654 /*
9655 * validation error, log the message for the "best" one
9656 */
9657 ctxt->flags |= FLAGS_IGNORABLE;
9658 xmlRelaxNGLogBestError(ctxt);
9659 }
9660 for (i = 0; i < ctxt->states->nbState; i++) {
9661 xmlRelaxNGFreeValidState(ctxt,
9662 ctxt->states->tabState[i]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009663 }
9664 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9665 ctxt->flags = oldflags;
9666 ctxt->states = NULL;
9667 if ((ret == 0) && (tmp == -1))
9668 ret = -1;
9669 } else {
9670 state = ctxt->state;
9671 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009672 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009673 xmlRelaxNGFreeValidState(ctxt, state);
9674 }
9675 }
9676 if (ret == 0) {
9677 node->_private = define;
9678 }
9679 ctxt->flags = oldflags;
9680 ctxt->state = oldstate;
9681 if (oldstate != NULL)
9682 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9683 if (ret != 0) {
9684 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9685 xmlRelaxNGDumpValidError(ctxt);
9686 ret = 0;
9687 } else {
9688 ret = -2;
9689 }
9690 } else {
9691 if (ctxt->errNr > errNr)
9692 xmlRelaxNGPopErrors(ctxt, errNr);
9693 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009694
9695#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009696 xmlGenericError(xmlGenericErrorContext,
9697 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9698 node->name, ret);
9699 if (oldstate == NULL)
9700 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9701 else if (oldstate->seq == NULL)
9702 xmlGenericError(xmlGenericErrorContext, ": done\n");
9703 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9704 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9705 oldstate->seq->name);
9706 else
9707 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9708 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009709#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009710 break;
9711 case XML_RELAXNG_OPTIONAL:{
9712 errNr = ctxt->errNr;
9713 oldflags = ctxt->flags;
9714 ctxt->flags |= FLAGS_IGNORABLE;
9715 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9716 ret =
9717 xmlRelaxNGValidateDefinitionList(ctxt,
9718 define->content);
9719 if (ret != 0) {
9720 if (ctxt->state != NULL)
9721 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9722 ctxt->state = oldstate;
9723 ctxt->flags = oldflags;
9724 ret = 0;
9725 if (ctxt->errNr > errNr)
9726 xmlRelaxNGPopErrors(ctxt, errNr);
9727 break;
9728 }
9729 if (ctxt->states != NULL) {
9730 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9731 } else {
9732 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9733 if (ctxt->states == NULL) {
9734 xmlRelaxNGFreeValidState(ctxt, oldstate);
9735 ctxt->flags = oldflags;
9736 ret = -1;
9737 if (ctxt->errNr > errNr)
9738 xmlRelaxNGPopErrors(ctxt, errNr);
9739 break;
9740 }
9741 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9742 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9743 ctxt->state = NULL;
9744 }
9745 ctxt->flags = oldflags;
9746 ret = 0;
9747 if (ctxt->errNr > errNr)
9748 xmlRelaxNGPopErrors(ctxt, errNr);
9749 break;
9750 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009751 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009752 errNr = ctxt->errNr;
9753 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9754 if (ret != 0) {
9755 break;
9756 }
9757 if (ctxt->errNr > errNr)
9758 xmlRelaxNGPopErrors(ctxt, errNr);
9759 /* no break on purpose */
9760 case XML_RELAXNG_ZEROORMORE:{
9761 int progress;
9762 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9763 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009764
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009765 errNr = ctxt->errNr;
9766 res = xmlRelaxNGNewStates(ctxt, 1);
9767 if (res == NULL) {
9768 ret = -1;
9769 break;
9770 }
9771 /*
9772 * All the input states are also exit states
9773 */
9774 if (ctxt->state != NULL) {
9775 xmlRelaxNGAddStates(ctxt, res,
9776 xmlRelaxNGCopyValidState(ctxt,
9777 ctxt->
9778 state));
9779 } else {
9780 for (j = 0; j < ctxt->states->nbState; j++) {
9781 xmlRelaxNGAddStates(ctxt, res,
9782 xmlRelaxNGCopyValidState(ctxt,
9783 ctxt->
9784 states->
9785 tabState
9786 [j]));
9787 }
9788 }
9789 oldflags = ctxt->flags;
9790 ctxt->flags |= FLAGS_IGNORABLE;
9791 do {
9792 progress = 0;
9793 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009794
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009795 if (ctxt->states != NULL) {
9796 states = ctxt->states;
9797 for (i = 0; i < states->nbState; i++) {
9798 ctxt->state = states->tabState[i];
9799 ctxt->states = NULL;
9800 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9801 define->
9802 content);
9803 if (ret == 0) {
9804 if (ctxt->state != NULL) {
9805 tmp = xmlRelaxNGAddStates(ctxt, res,
9806 ctxt->state);
9807 ctxt->state = NULL;
9808 if (tmp == 1)
9809 progress = 1;
9810 } else if (ctxt->states != NULL) {
9811 for (j = 0; j < ctxt->states->nbState;
9812 j++) {
9813 tmp =
9814 xmlRelaxNGAddStates(ctxt, res,
9815 ctxt->
9816 states->
9817 tabState
9818 [j]);
9819 if (tmp == 1)
9820 progress = 1;
9821 }
9822 xmlRelaxNGFreeStates(ctxt,
9823 ctxt->states);
9824 ctxt->states = NULL;
9825 }
9826 } else {
9827 if (ctxt->state != NULL) {
9828 xmlRelaxNGFreeValidState(ctxt,
9829 ctxt->state);
9830 ctxt->state = NULL;
9831 }
9832 }
9833 }
9834 } else {
9835 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9836 define->
9837 content);
9838 if (ret != 0) {
9839 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9840 ctxt->state = NULL;
9841 } else {
9842 base = res->nbState;
9843 if (ctxt->state != NULL) {
9844 tmp = xmlRelaxNGAddStates(ctxt, res,
9845 ctxt->state);
9846 ctxt->state = NULL;
9847 if (tmp == 1)
9848 progress = 1;
9849 } else if (ctxt->states != NULL) {
9850 for (j = 0; j < ctxt->states->nbState; j++) {
9851 tmp = xmlRelaxNGAddStates(ctxt, res,
9852 ctxt->
9853 states->
9854 tabState[j]);
9855 if (tmp == 1)
9856 progress = 1;
9857 }
9858 if (states == NULL) {
9859 states = ctxt->states;
9860 } else {
9861 xmlRelaxNGFreeStates(ctxt,
9862 ctxt->states);
9863 }
9864 ctxt->states = NULL;
9865 }
9866 }
9867 }
9868 if (progress) {
9869 /*
9870 * Collect all the new nodes added at that step
9871 * and make them the new node set
9872 */
9873 if (res->nbState - base == 1) {
9874 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9875 res->
9876 tabState
9877 [base]);
9878 } else {
9879 if (states == NULL) {
9880 xmlRelaxNGNewStates(ctxt,
9881 res->nbState - base);
9882 }
9883 states->nbState = 0;
9884 for (i = base; i < res->nbState; i++)
9885 xmlRelaxNGAddStates(ctxt, states,
9886 xmlRelaxNGCopyValidState
9887 (ctxt,
9888 res->tabState[i]));
9889 ctxt->states = states;
9890 }
9891 }
9892 } while (progress == 1);
9893 if (states != NULL) {
9894 xmlRelaxNGFreeStates(ctxt, states);
9895 }
9896 ctxt->states = res;
9897 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009898#if 0
9899 /*
9900 * errors may have to be propagated back...
9901 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009902 if (ctxt->errNr > errNr)
9903 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009904#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009905 ret = 0;
9906 break;
9907 }
9908 case XML_RELAXNG_CHOICE:{
9909 xmlRelaxNGDefinePtr list = NULL;
9910 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009911
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009912 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009913
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009914 errNr = ctxt->errNr;
9915 if ((define->dflags & IS_TRIABLE)
9916 && (define->data != NULL)) {
9917 xmlHashTablePtr triage =
9918 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +00009919
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009920 /*
9921 * Something we can optimize cleanly there is only one
9922 * possble branch out !
9923 */
9924 if (node == NULL) {
9925 ret = -1;
9926 break;
9927 }
9928 if ((node->type == XML_TEXT_NODE) ||
9929 (node->type == XML_CDATA_SECTION_NODE)) {
9930 list =
9931 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
9932 } else if (node->type == XML_ELEMENT_NODE) {
9933 if (node->ns != NULL) {
9934 list = xmlHashLookup2(triage, node->name,
9935 node->ns->href);
9936 if (list == NULL)
9937 list =
9938 xmlHashLookup2(triage, BAD_CAST "#any",
9939 node->ns->href);
9940 } else
9941 list =
9942 xmlHashLookup2(triage, node->name, NULL);
9943 if (list == NULL)
9944 list =
9945 xmlHashLookup2(triage, BAD_CAST "#any",
9946 NULL);
9947 }
9948 if (list == NULL) {
9949 ret = -1;
9950 break;
9951 }
9952 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9953 if (ret == 0) {
9954 }
9955 break;
9956 }
Daniel Veillarde063f482003-03-21 16:53:17 +00009957
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009958 list = define->content;
9959 oldflags = ctxt->flags;
9960 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009961
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009962 while (list != NULL) {
9963 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9964 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9965 if (ret == 0) {
9966 if (states == NULL) {
9967 states = xmlRelaxNGNewStates(ctxt, 1);
9968 }
9969 if (ctxt->state != NULL) {
9970 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
9971 } else if (ctxt->states != NULL) {
9972 for (i = 0; i < ctxt->states->nbState; i++) {
9973 xmlRelaxNGAddStates(ctxt, states,
9974 ctxt->states->
9975 tabState[i]);
9976 }
9977 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9978 ctxt->states = NULL;
9979 }
9980 } else {
9981 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9982 }
9983 ctxt->state = oldstate;
9984 list = list->next;
9985 }
9986 if (states != NULL) {
9987 xmlRelaxNGFreeValidState(ctxt, oldstate);
9988 ctxt->states = states;
9989 ctxt->state = NULL;
9990 ret = 0;
9991 } else {
9992 ctxt->states = NULL;
9993 }
9994 ctxt->flags = oldflags;
9995 if (ret != 0) {
9996 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9997 xmlRelaxNGDumpValidError(ctxt);
9998 }
9999 } else {
10000 if (ctxt->errNr > errNr)
10001 xmlRelaxNGPopErrors(ctxt, errNr);
10002 }
10003 break;
10004 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010005 case XML_RELAXNG_DEF:
10006 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010007 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10008 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010009 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010010 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10011 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010012 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010013 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10014 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010015 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010016 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010017 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010018 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010019 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010020 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10021 break;
10022 case XML_RELAXNG_DATATYPE:{
10023 xmlNodePtr child;
10024 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010025
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010026 child = node;
10027 while (child != NULL) {
10028 if (child->type == XML_ELEMENT_NODE) {
10029 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10030 node->parent->name);
10031 ret = -1;
10032 break;
10033 } else if ((child->type == XML_TEXT_NODE) ||
10034 (child->type == XML_CDATA_SECTION_NODE)) {
10035 content = xmlStrcat(content, child->content);
10036 }
10037 /* TODO: handle entities ... */
10038 child = child->next;
10039 }
10040 if (ret == -1) {
10041 if (content != NULL)
10042 xmlFree(content);
10043 break;
10044 }
10045 if (content == NULL) {
10046 content = xmlStrdup(BAD_CAST "");
10047 if (content == NULL) {
10048 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
10049 ret = -1;
10050 break;
10051 }
10052 }
10053 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10054 ctxt->state->seq);
10055 if (ret == -1) {
10056 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10057 } else if (ret == 0) {
10058 ctxt->state->seq = NULL;
10059 }
10060 if (content != NULL)
10061 xmlFree(content);
10062 break;
10063 }
10064 case XML_RELAXNG_VALUE:{
10065 xmlChar *content = NULL;
10066 xmlChar *oldvalue;
10067 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010068
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010069 child = node;
10070 while (child != NULL) {
10071 if (child->type == XML_ELEMENT_NODE) {
10072 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10073 node->parent->name);
10074 ret = -1;
10075 break;
10076 } else if ((child->type == XML_TEXT_NODE) ||
10077 (child->type == XML_CDATA_SECTION_NODE)) {
10078 content = xmlStrcat(content, child->content);
10079 }
10080 /* TODO: handle entities ... */
10081 child = child->next;
10082 }
10083 if (ret == -1) {
10084 if (content != NULL)
10085 xmlFree(content);
10086 break;
10087 }
10088 if (content == NULL) {
10089 content = xmlStrdup(BAD_CAST "");
10090 if (content == NULL) {
10091 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
10092 ret = -1;
10093 break;
10094 }
10095 }
10096 oldvalue = ctxt->state->value;
10097 ctxt->state->value = content;
10098 ret = xmlRelaxNGValidateValue(ctxt, define);
10099 ctxt->state->value = oldvalue;
10100 if (ret == -1) {
10101 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10102 } else if (ret == 0) {
10103 ctxt->state->seq = NULL;
10104 }
10105 if (content != NULL)
10106 xmlFree(content);
10107 break;
10108 }
10109 case XML_RELAXNG_LIST:{
10110 xmlChar *content;
10111 xmlNodePtr child;
10112 xmlChar *oldvalue, *oldendvalue;
10113 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010114
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010115 /*
10116 * Make sure it's only text nodes
10117 */
10118
10119 content = NULL;
10120 child = node;
10121 while (child != NULL) {
10122 if (child->type == XML_ELEMENT_NODE) {
10123 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10124 node->parent->name);
10125 ret = -1;
10126 break;
10127 } else if ((child->type == XML_TEXT_NODE) ||
10128 (child->type == XML_CDATA_SECTION_NODE)) {
10129 content = xmlStrcat(content, child->content);
10130 }
10131 /* TODO: handle entities ... */
10132 child = child->next;
10133 }
10134 if (ret == -1) {
10135 if (content != NULL)
10136 xmlFree(content);
10137 break;
10138 }
10139 if (content == NULL) {
10140 content = xmlStrdup(BAD_CAST "");
10141 if (content == NULL) {
10142 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
10143 ret = -1;
10144 break;
10145 }
10146 }
10147 len = xmlStrlen(content);
10148 oldvalue = ctxt->state->value;
10149 oldendvalue = ctxt->state->endvalue;
10150 ctxt->state->value = content;
10151 ctxt->state->endvalue = content + len;
10152 ret = xmlRelaxNGValidateValue(ctxt, define);
10153 ctxt->state->value = oldvalue;
10154 ctxt->state->endvalue = oldendvalue;
10155 if (ret == -1) {
10156 VALID_ERR(XML_RELAXNG_ERR_LIST);
10157 } else if ((ret == 0) && (node != NULL)) {
10158 ctxt->state->seq = node->next;
10159 }
10160 if (content != NULL)
10161 xmlFree(content);
10162 break;
10163 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010164 case XML_RELAXNG_EXCEPT:
10165 case XML_RELAXNG_PARAM:
10166 TODO ret = -1;
10167 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010168 }
10169 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010170#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010171 for (i = 0; i < ctxt->depth; i++)
10172 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010173 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010174 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010175 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010176 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010177 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010178 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010179 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010180 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010181#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010182 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010183}
10184
10185/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010186 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010187 * @ctxt: a Relax-NG validation context
10188 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010189 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010190 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010191 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010192 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010193 */
10194static int
Daniel Veillardfd573f12003-03-16 17:52:32 +000010195xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10196 xmlRelaxNGDefinePtr define) {
10197 xmlRelaxNGStatesPtr states, res;
10198 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010199
Daniel Veillardfd573f12003-03-16 17:52:32 +000010200 /*
10201 * We should NOT have both ctxt->state and ctxt->states
10202 */
10203 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10204 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +000010205 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010206 ctxt->state = NULL;
10207 }
10208
10209 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
10210 if (ctxt->states != NULL) {
10211 ctxt->state = ctxt->states->tabState[0];
10212 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10213 ctxt->states = NULL;
10214 }
10215 ret = xmlRelaxNGValidateState(ctxt, define);
10216 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10217 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +000010218 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010219 ctxt->state = NULL;
10220 }
10221 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10222 ctxt->state = ctxt->states->tabState[0];
10223 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10224 ctxt->states = NULL;
10225 }
10226 return(ret);
10227 }
10228
10229 states = ctxt->states;
10230 ctxt->states = NULL;
10231 res = NULL;
10232 j = 0;
10233 oldflags = ctxt->flags;
10234 ctxt->flags |= FLAGS_IGNORABLE;
10235 for (i = 0;i < states->nbState;i++) {
10236 ctxt->state = states->tabState[i];
10237 ctxt->states = NULL;
10238 ret = xmlRelaxNGValidateState(ctxt, define);
10239 /*
10240 * We should NOT have both ctxt->state and ctxt->states
10241 */
10242 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10243 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +000010244 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010245 ctxt->state = NULL;
10246 }
10247 if (ret == 0) {
10248 if (ctxt->states == NULL) {
10249 if (res != NULL) {
10250 /* add the state to the container */
10251 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10252 ctxt->state = NULL;
10253 } else {
10254 /* add the state directly in states */
10255 states->tabState[j++] = ctxt->state;
10256 ctxt->state = NULL;
10257 }
10258 } else {
10259 if (res == NULL) {
10260 /* make it the new container and copy other results */
10261 res = ctxt->states;
10262 ctxt->states = NULL;
10263 for (k = 0;k < j;k++)
10264 xmlRelaxNGAddStates(ctxt, res, states->tabState[k]);
10265 } else {
10266 /* add all the new results to res and reff the container */
10267 for (k = 0;k < ctxt->states->nbState;k++)
10268 xmlRelaxNGAddStates(ctxt, res,
10269 ctxt->states->tabState[k]);
10270 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10271 ctxt->states = NULL;
10272 }
10273 }
10274 } else {
10275 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +000010276 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010277 ctxt->state = NULL;
10278 } else if (ctxt->states != NULL) {
10279 for (k = 0;k < ctxt->states->nbState;k++)
Daniel Veillard798024a2003-03-19 10:36:09 +000010280 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010281 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10282 ctxt->states = NULL;
10283 }
10284 }
10285 }
10286 ctxt->flags = oldflags;
10287 if (res != NULL) {
10288 xmlRelaxNGFreeStates(ctxt, states);
10289 ctxt->states = res;
10290 ret = 0;
10291 } else if (j > 1) {
10292 states->nbState = j;
10293 ctxt->states = states;
10294 ret =0;
10295 } else if (j == 1) {
10296 ctxt->state = states->tabState[0];
10297 xmlRelaxNGFreeStates(ctxt, states);
10298 ret = 0;
10299 } else {
10300 ret = -1;
10301 xmlRelaxNGFreeStates(ctxt, states);
10302 if (ctxt->states != NULL) {
10303 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10304 ctxt->states = NULL;
10305 }
10306 }
10307 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10308 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +000010309 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010310 ctxt->state = NULL;
10311 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010312 return(ret);
10313}
10314
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010315/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010316 * xmlRelaxNGValidateDocument:
10317 * @ctxt: a Relax-NG validation context
10318 * @doc: the document
10319 *
10320 * Validate the given document
10321 *
10322 * Returns 0 if the validation succeeded or an error code.
10323 */
10324static int
10325xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
10326 int ret;
10327 xmlRelaxNGPtr schema;
10328 xmlRelaxNGGrammarPtr grammar;
10329 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010330 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010331
10332 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
10333 return(-1);
10334
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010335 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010336 schema = ctxt->schema;
10337 grammar = schema->topgrammar;
10338 if (grammar == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +000010339 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010340 return(-1);
10341 }
10342 state = xmlRelaxNGNewValidState(ctxt, NULL);
10343 ctxt->state = state;
10344 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010345 if ((ctxt->state != NULL) && (state->seq != NULL)) {
10346 state = ctxt->state;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010347 node = state->seq;
10348 node = xmlRelaxNGSkipIgnored(ctxt, node);
10349 if (node != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +000010350 if (ret != -1) {
10351 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10352 ret = -1;
10353 }
10354 }
10355 } else if (ctxt->states != NULL) {
10356 int i;
10357 int tmp = -1;
10358
10359 for (i = 0;i < ctxt->states->nbState;i++) {
10360 state = ctxt->states->tabState[i];
10361 node = state->seq;
10362 node = xmlRelaxNGSkipIgnored(ctxt, node);
10363 if (node == NULL)
10364 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +000010365 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010366 }
10367 if (tmp == -1) {
10368 if (ret != -1) {
10369 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10370 ret = -1;
10371 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010372 }
10373 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010374 if (ctxt->state != NULL) {
10375 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10376 ctxt->state = NULL;
10377 }
Daniel Veillard580ced82003-03-21 21:22:48 +000010378 if (ret != 0)
Daniel Veillardfd573f12003-03-16 17:52:32 +000010379 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010380#ifdef DEBUG
10381 else if (ctxt->errNr != 0) {
10382 ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n",
10383 ctxt->errNr);
10384 xmlRelaxNGDumpValidError(ctxt);
10385 }
10386#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010387 if (ctxt->idref == 1) {
10388 xmlValidCtxt vctxt;
10389
10390 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10391 vctxt.valid = 1;
10392 vctxt.error = ctxt->error;
10393 vctxt.warning = ctxt->warning;
10394 vctxt.userData = ctxt->userData;
10395
10396 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10397 ret = -1;
10398 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010399 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
10400 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010401
10402 return(ret);
10403}
10404
Daniel Veillardfd573f12003-03-16 17:52:32 +000010405/************************************************************************
10406 * *
10407 * Validation interfaces *
10408 * *
10409 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +000010410/**
10411 * xmlRelaxNGNewValidCtxt:
10412 * @schema: a precompiled XML RelaxNGs
10413 *
10414 * Create an XML RelaxNGs validation context based on the given schema
10415 *
10416 * Returns the validation context or NULL in case of error
10417 */
10418xmlRelaxNGValidCtxtPtr
10419xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) {
10420 xmlRelaxNGValidCtxtPtr ret;
10421
10422 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10423 if (ret == NULL) {
10424 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardfd573f12003-03-16 17:52:32 +000010425 "Failed to allocate new schema validation context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010426 return (NULL);
10427 }
10428 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10429 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010430 ret->error = xmlGenericError;
10431 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010432 ret->errNr = 0;
10433 ret->errMax = 0;
10434 ret->err = NULL;
10435 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010436 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010437 ret->states = NULL;
10438 ret->freeState = NULL;
10439 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010440 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010441 return (ret);
10442}
10443
10444/**
10445 * xmlRelaxNGFreeValidCtxt:
10446 * @ctxt: the schema validation context
10447 *
10448 * Free the resources associated to the schema validation context
10449 */
10450void
10451xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard798024a2003-03-19 10:36:09 +000010452 int k;
10453
Daniel Veillard6eadf632003-01-23 18:29:16 +000010454 if (ctxt == NULL)
10455 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010456 if (ctxt->states != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +000010457 xmlRelaxNGFreeStates(NULL, ctxt->states);
10458 if (ctxt->freeState != NULL) {
10459 for (k = 0;k < ctxt->freeState->nbState;k++) {
10460 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10461 }
10462 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
10463 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010464 if (ctxt->freeStates != NULL) {
10465 for (k = 0;k < ctxt->freeStatesNr;k++) {
10466 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10467 }
10468 xmlFree(ctxt->freeStates);
10469 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010470 if (ctxt->errTab != NULL)
10471 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010472 if (ctxt->elemTab != NULL) {
10473 xmlRegExecCtxtPtr exec;
10474
10475 exec = xmlRelaxNGElemPop(ctxt);
10476 while (exec != NULL) {
10477 xmlRegFreeExecCtxt(exec);
10478 exec = xmlRelaxNGElemPop(ctxt);
10479 }
10480 xmlFree(ctxt->elemTab);
10481 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010482 xmlFree(ctxt);
10483}
10484
10485/**
10486 * xmlRelaxNGSetValidErrors:
10487 * @ctxt: a Relax-NG validation context
10488 * @err: the error function
10489 * @warn: the warning function
10490 * @ctx: the functions context
10491 *
10492 * Set the error and warning callback informations
10493 */
10494void
10495xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10496 xmlRelaxNGValidityErrorFunc err,
10497 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
10498 if (ctxt == NULL)
10499 return;
10500 ctxt->error = err;
10501 ctxt->warning = warn;
10502 ctxt->userData = ctx;
10503}
10504
10505/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010506 * xmlRelaxNGGetValidErrors:
10507 * @ctxt: a Relax-NG validation context
10508 * @err: the error function result
10509 * @warn: the warning function result
10510 * @ctx: the functions context result
10511 *
10512 * Get the error and warning callback informations
10513 *
10514 * Returns -1 in case of error and 0 otherwise
10515 */
10516int
10517xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10518 xmlRelaxNGValidityErrorFunc *err,
10519 xmlRelaxNGValidityWarningFunc *warn, void **ctx) {
10520 if (ctxt == NULL)
10521 return(-1);
10522 if (err != NULL) *err = ctxt->error;
10523 if (warn != NULL) *warn = ctxt->warning;
10524 if (ctx != NULL) *ctx = ctxt->userData;
10525 return(0);
10526}
10527
10528/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010529 * xmlRelaxNGValidateDoc:
10530 * @ctxt: a Relax-NG validation context
10531 * @doc: a parsed document tree
10532 *
10533 * Validate a document tree in memory.
10534 *
10535 * Returns 0 if the document is valid, a positive error code
10536 * number otherwise and -1 in case of internal or API error.
10537 */
10538int
10539xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
10540 int ret;
10541
10542 if ((ctxt == NULL) || (doc == NULL))
10543 return(-1);
10544
10545 ctxt->doc = doc;
10546
10547 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010548 /*
10549 * TODO: build error codes
10550 */
10551 if (ret == -1)
10552 return(1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010553 return(ret);
10554}
10555
10556#endif /* LIBXML_SCHEMAS_ENABLED */
10557