blob: 28d1a6067eeeea6062d0e18df0a67695c66d0ad6 [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 {
6088 ret = (xmlRelaxNGContentType) cur->depth + 15;
6089 }
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 Veillard6eadf632003-01-23 18:29:16 +00007408/************************************************************************
7409 * *
7410 * Dump back a compiled form *
7411 * *
7412 ************************************************************************/
7413static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define);
7414
7415/**
7416 * xmlRelaxNGDumpDefines:
7417 * @output: the file output
7418 * @defines: a list of define structures
7419 *
7420 * Dump a RelaxNG structure back
7421 */
7422static void
7423xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) {
7424 while (defines != NULL) {
7425 xmlRelaxNGDumpDefine(output, defines);
7426 defines = defines->next;
7427 }
7428}
7429
7430/**
7431 * xmlRelaxNGDumpDefine:
7432 * @output: the file output
7433 * @define: a define structure
7434 *
7435 * Dump a RelaxNG structure back
7436 */
7437static void
7438xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) {
7439 if (define == NULL)
7440 return;
7441 switch(define->type) {
7442 case XML_RELAXNG_EMPTY:
7443 fprintf(output, "<empty/>\n");
7444 break;
7445 case XML_RELAXNG_NOT_ALLOWED:
7446 fprintf(output, "<notAllowed/>\n");
7447 break;
7448 case XML_RELAXNG_TEXT:
7449 fprintf(output, "<text/>\n");
7450 break;
7451 case XML_RELAXNG_ELEMENT:
7452 fprintf(output, "<element>\n");
7453 if (define->name != NULL) {
7454 fprintf(output, "<name");
7455 if (define->ns != NULL)
7456 fprintf(output, " ns=\"%s\"", define->ns);
7457 fprintf(output, ">%s</name>\n", define->name);
7458 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007459 xmlRelaxNGDumpDefines(output, define->attrs);
7460 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007461 fprintf(output, "</element>\n");
7462 break;
7463 case XML_RELAXNG_LIST:
7464 fprintf(output, "<list>\n");
7465 xmlRelaxNGDumpDefines(output, define->content);
7466 fprintf(output, "</list>\n");
7467 break;
7468 case XML_RELAXNG_ONEORMORE:
7469 fprintf(output, "<oneOrMore>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007470 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007471 fprintf(output, "</oneOrMore>\n");
7472 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007473 case XML_RELAXNG_ZEROORMORE:
7474 fprintf(output, "<zeroOrMore>\n");
7475 xmlRelaxNGDumpDefines(output, define->content);
7476 fprintf(output, "</zeroOrMore>\n");
7477 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007478 case XML_RELAXNG_CHOICE:
7479 fprintf(output, "<choice>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007480 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007481 fprintf(output, "</choice>\n");
7482 break;
7483 case XML_RELAXNG_GROUP:
7484 fprintf(output, "<group>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007485 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007486 fprintf(output, "</group>\n");
7487 break;
7488 case XML_RELAXNG_INTERLEAVE:
7489 fprintf(output, "<interleave>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007490 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007491 fprintf(output, "</interleave>\n");
7492 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007493 case XML_RELAXNG_OPTIONAL:
7494 fprintf(output, "<optional>\n");
7495 xmlRelaxNGDumpDefines(output, define->content);
7496 fprintf(output, "</optional>\n");
7497 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007498 case XML_RELAXNG_ATTRIBUTE:
7499 fprintf(output, "<attribute>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007500 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007501 fprintf(output, "</attribute>\n");
7502 break;
7503 case XML_RELAXNG_DEF:
7504 fprintf(output, "<define");
7505 if (define->name != NULL)
7506 fprintf(output, " name=\"%s\"", define->name);
7507 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007508 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007509 fprintf(output, "</define>\n");
7510 break;
7511 case XML_RELAXNG_REF:
7512 fprintf(output, "<ref");
7513 if (define->name != NULL)
7514 fprintf(output, " name=\"%s\"", define->name);
7515 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007516 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007517 fprintf(output, "</ref>\n");
7518 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007519 case XML_RELAXNG_PARENTREF:
7520 fprintf(output, "<parentRef");
7521 if (define->name != NULL)
7522 fprintf(output, " name=\"%s\"", define->name);
7523 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007524 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard419a7682003-02-03 23:22:49 +00007525 fprintf(output, "</parentRef>\n");
7526 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007527 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard416589a2003-02-17 17:25:42 +00007528 fprintf(output, "<externalRef>");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007529 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillarde431a272003-01-29 23:02:33 +00007530 fprintf(output, "</externalRef>\n");
7531 break;
7532 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007533 case XML_RELAXNG_VALUE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007534 TODO
Daniel Veillard6eadf632003-01-23 18:29:16 +00007535 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007536 case XML_RELAXNG_START:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007537 case XML_RELAXNG_EXCEPT:
Daniel Veillard8fe98712003-02-19 00:19:14 +00007538 case XML_RELAXNG_PARAM:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007539 TODO
7540 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00007541 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007542 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard77648bb2003-02-20 15:03:22 +00007543 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007544 }
7545}
7546
7547/**
7548 * xmlRelaxNGDumpGrammar:
7549 * @output: the file output
7550 * @grammar: a grammar structure
7551 * @top: is this a top grammar
7552 *
7553 * Dump a RelaxNG structure back
7554 */
7555static void
7556xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7557{
7558 if (grammar == NULL)
7559 return;
7560
7561 fprintf(output, "<grammar");
7562 if (top)
7563 fprintf(output,
7564 " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7565 switch(grammar->combine) {
7566 case XML_RELAXNG_COMBINE_UNDEFINED:
7567 break;
7568 case XML_RELAXNG_COMBINE_CHOICE:
7569 fprintf(output, " combine=\"choice\"");
7570 break;
7571 case XML_RELAXNG_COMBINE_INTERLEAVE:
7572 fprintf(output, " combine=\"interleave\"");
7573 break;
7574 default:
7575 fprintf(output, " <!-- invalid combine value -->");
7576 }
7577 fprintf(output, ">\n");
7578 if (grammar->start == NULL) {
7579 fprintf(output, " <!-- grammar had no start -->");
7580 } else {
7581 fprintf(output, "<start>\n");
7582 xmlRelaxNGDumpDefine(output, grammar->start);
7583 fprintf(output, "</start>\n");
7584 }
7585 /* TODO ? Dump the defines ? */
7586 fprintf(output, "</grammar>\n");
7587}
7588
7589/**
7590 * xmlRelaxNGDump:
7591 * @output: the file output
7592 * @schema: a schema structure
7593 *
7594 * Dump a RelaxNG structure back
7595 */
7596void
7597xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7598{
7599 if (schema == NULL) {
7600 fprintf(output, "RelaxNG empty or failed to compile\n");
7601 return;
7602 }
7603 fprintf(output, "RelaxNG: ");
7604 if (schema->doc == NULL) {
7605 fprintf(output, "no document\n");
7606 } else if (schema->doc->URL != NULL) {
7607 fprintf(output, "%s\n", schema->doc->URL);
7608 } else {
7609 fprintf(output, "\n");
7610 }
7611 if (schema->topgrammar == NULL) {
7612 fprintf(output, "RelaxNG has no top grammar\n");
7613 return;
7614 }
7615 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7616}
7617
Daniel Veillardfebcca42003-02-16 15:44:18 +00007618/**
7619 * xmlRelaxNGDumpTree:
7620 * @output: the file output
7621 * @schema: a schema structure
7622 *
7623 * Dump the transformed RelaxNG tree.
7624 */
7625void
7626xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7627{
7628 if (schema == NULL) {
7629 fprintf(output, "RelaxNG empty or failed to compile\n");
7630 return;
7631 }
7632 if (schema->doc == NULL) {
7633 fprintf(output, "no document\n");
7634 } else {
7635 xmlDocDump(output, schema->doc);
7636 }
7637}
7638
Daniel Veillard6eadf632003-01-23 18:29:16 +00007639/************************************************************************
7640 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007641 * Validation of compiled content *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007642 * *
7643 ************************************************************************/
Daniel Veillardfd573f12003-03-16 17:52:32 +00007644static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7645 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007646
7647/**
7648 * xmlRelaxNGValidateCompiledCallback:
7649 * @exec: the regular expression instance
7650 * @token: the token which matched
7651 * @transdata: callback data, the define for the subelement if available
7652 @ @inputdata: callback data, the Relax NG validation context
7653 *
7654 * Handle the callback and if needed validate the element children.
7655 */
7656static void
7657xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7658 const xmlChar *token,
7659 void *transdata,
7660 void *inputdata) {
7661 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7662 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7663 int ret;
7664
7665#ifdef DEBUG_COMPILE
7666 xmlGenericError(xmlGenericErrorContext,
7667 "Compiled callback for: '%s'\n", token);
7668#endif
7669 if (ctxt == NULL) {
7670 fprintf(stderr, "callback on %s missing context\n", token);
7671 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7672 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7673 return;
7674 }
7675 if (define == NULL) {
7676 if (token[0] == '#')
7677 return;
7678 fprintf(stderr, "callback on %s missing define\n", token);
7679 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7680 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7681 return;
7682 }
7683 if ((ctxt == NULL) || (define == NULL)) {
7684 fprintf(stderr, "callback on %s missing info\n", token);
7685 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7686 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7687 return;
7688 } else if (define->type != XML_RELAXNG_ELEMENT) {
7689 fprintf(stderr, "callback on %s define is not element\n", token);
7690 if (ctxt->errNo == XML_RELAXNG_OK)
7691 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7692 return;
7693 }
7694 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007695 if (ret != 0)
7696 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007697}
7698
7699/**
7700 * xmlRelaxNGValidateCompiledContent:
7701 * @ctxt: the RelaxNG validation context
7702 * @regexp: the regular expression as compiled
7703 * @content: list of children to test against the regexp
7704 *
7705 * Validate the content model of an element or start using the regexp
7706 *
7707 * Returns 0 in case of success, -1 in case of error.
7708 */
7709static int
7710xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
7711 xmlRegexpPtr regexp, xmlNodePtr content) {
7712 xmlRegExecCtxtPtr exec;
7713 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007714 int ret = 0;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007715 int oldperr = ctxt->perr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007716
7717 if ((ctxt == NULL) || (regexp == NULL))
7718 return(-1);
7719 exec = xmlRegNewExecCtxt(regexp,
7720 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007721 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007722 cur = content;
7723 while (cur != NULL) {
7724 ctxt->state->seq = cur;
7725 switch (cur->type) {
7726 case XML_TEXT_NODE:
7727 case XML_CDATA_SECTION_NODE:
7728 if (xmlIsBlankNode(cur))
7729 break;
7730 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7731 if (ret < 0) {
7732 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, cur->parent->name);
7733 }
7734 break;
7735 case XML_ELEMENT_NODE:
7736 if (cur->ns != NULL) {
7737 ret = xmlRegExecPushString2(exec, cur->name,
7738 cur->ns->href, ctxt);
7739 } else {
7740 ret = xmlRegExecPushString(exec, cur->name, ctxt);
7741 }
7742 if (ret < 0) {
7743 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7744 }
7745 break;
7746 default:
7747 break;
7748 }
7749 if (ret < 0) break;
7750 /*
7751 * Switch to next element
7752 */
7753 cur = cur->next;
7754 }
7755 ret = xmlRegExecPushString(exec, NULL, NULL);
7756 if (ret == 1) {
7757 ret = 0;
7758 ctxt->state->seq = NULL;
7759 } else if (ret == 0) {
7760 /*
Daniel Veillardf4e55762003-04-15 23:32:22 +00007761 * TODO: get some of the names needed to exit the current state of exec
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007762 */
7763 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7764 ret = -1;
7765 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7766 xmlRelaxNGDumpValidError(ctxt);
7767 } else {
7768 ret = -1;
7769 }
7770 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007771 /*
7772 * There might be content model errors outside of the pure
7773 * regexp validation, e.g. for attribute values.
7774 */
7775 if ((ret == 0) && (ctxt->perr != 0)) {
7776 ret = ctxt->perr;
7777 }
7778 ctxt->perr = oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007779 return(ret);
7780}
7781
7782/************************************************************************
7783 * *
7784 * Progressive validation of when possible *
7785 * *
7786 ************************************************************************/
Daniel Veillardf4e55762003-04-15 23:32:22 +00007787static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7788 xmlRelaxNGDefinePtr defines);
7789static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt);
7790
7791/**
7792 * xmlRelaxNGElemPush:
7793 * @ctxt: the validation context
7794 * @exec: the regexp runtime for the new content model
7795 *
7796 * Push a new regexp for the current node content model on the stack
7797 *
7798 * Returns 0 in case of success and -1 in case of error.
7799 */
7800static int
7801xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec) {
7802 if (ctxt->elemTab == NULL) {
7803 ctxt->elemMax = 10;
7804 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7805 sizeof(xmlRegExecCtxtPtr));
7806 if (ctxt->elemTab == NULL) {
7807 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7808 return(-1);
7809 }
7810 }
7811 if (ctxt->elemNr >= ctxt->elemMax) {
7812 ctxt->elemMax *= 2;
7813 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
7814 ctxt->elemMax * sizeof(xmlRegExecCtxtPtr));
7815 if (ctxt->elemTab == NULL) {
7816 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7817 return(-1);
7818 }
7819 }
7820 ctxt->elemTab[ctxt->elemNr++] = exec;
7821 ctxt->elem = exec;
7822 return(0);
7823}
7824
7825/**
7826 * xmlRelaxNGElemPop:
7827 * @ctxt: the validation context
7828 *
7829 * Pop the regexp of the current node content model from the stack
7830 *
7831 * Returns the exec or NULL if empty
7832 */
7833static xmlRegExecCtxtPtr
7834xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt) {
7835 xmlRegExecCtxtPtr ret;
7836
7837 if (ctxt->elemNr <= 0) return(NULL);
7838 ctxt->elemNr--;
7839 ret = ctxt->elemTab[ctxt->elemNr];
7840 ctxt->elemTab[ctxt->elemNr] = NULL;
7841 if (ctxt->elemNr > 0)
7842 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7843 else
7844 ctxt->elem = NULL;
7845 return(ret);
7846}
7847
7848/**
7849 * xmlRelaxNGValidateProgressiveCallback:
7850 * @exec: the regular expression instance
7851 * @token: the token which matched
7852 * @transdata: callback data, the define for the subelement if available
7853 @ @inputdata: callback data, the Relax NG validation context
7854 *
7855 * Handle the callback and if needed validate the element children.
7856 * some of the in/out informations are passed via the context in @inputdata.
7857 */
7858static void
7859xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7860 const xmlChar *token,
7861 void *transdata,
7862 void *inputdata) {
7863 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7864 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00007865 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007866 xmlNodePtr node = ctxt->pnode;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00007867 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007868
7869#ifdef DEBUG_PROGRESSIVE
7870 xmlGenericError(xmlGenericErrorContext,
7871 "Progressive callback for: '%s'\n", token);
7872#endif
7873 if (ctxt == NULL) {
7874 fprintf(stderr, "callback on %s missing context\n", token);
7875 return;
7876 }
7877 ctxt->pstate = 1;
7878 if (define == NULL) {
7879 if (token[0] == '#')
7880 return;
7881 fprintf(stderr, "callback on %s missing define\n", token);
7882 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7883 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7884 ctxt->pstate = -1;
7885 return;
7886 }
7887 if ((ctxt == NULL) || (define == NULL)) {
7888 fprintf(stderr, "callback on %s missing info\n", token);
7889 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7890 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7891 ctxt->pstate = -1;
7892 return;
7893 } else if (define->type != XML_RELAXNG_ELEMENT) {
7894 fprintf(stderr, "callback on %s define is not element\n", token);
7895 if (ctxt->errNo == XML_RELAXNG_OK)
7896 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7897 ctxt->pstate = -1;
7898 return;
7899 }
7900 if (node->type != XML_ELEMENT_NODE) {
7901 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
7902 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7903 xmlRelaxNGDumpValidError(ctxt);
7904 ctxt->pstate = -1;
7905 return;
7906 }
7907 if (define->contModel == NULL) {
7908 /*
7909 * this node cannot be validated in a streamable fashion
7910 */
7911#ifdef DEBUG_PROGRESSIVE
7912 xmlGenericError(xmlGenericErrorContext,
7913 "Element '%s' validation is not streamable\n", token);
7914#endif
7915 ctxt->pstate = 0;
7916 ctxt->pdef = define;
7917 return;
7918 }
7919 exec = xmlRegNewExecCtxt(define->contModel,
7920 xmlRelaxNGValidateProgressiveCallback,
7921 ctxt);
7922 if (exec == NULL) {
7923 ctxt->pstate = -1;
7924 return;
7925 }
7926 xmlRelaxNGElemPush(ctxt, exec);
7927
7928 /*
7929 * Validate the attributes part of the content.
7930 */
7931 state = xmlRelaxNGNewValidState(ctxt, node);
7932 if (state == NULL) {
7933 ctxt->pstate = -1;
7934 return;
7935 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007936 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007937 ctxt->state = state;
7938 if (define->attrs != NULL) {
7939 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
7940 if (ret != 0) {
7941 ctxt->pstate = -1;
7942 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
7943 }
7944 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007945 if (ctxt->state != NULL) {
7946 ctxt->state->seq = NULL;
7947 ret = xmlRelaxNGValidateElementEnd(ctxt);
7948 if (ret != 0) {
7949 ctxt->pstate = -1;
7950 }
7951 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
7952 } else if (ctxt->states != NULL) {
7953 int tmp = -1, i;
7954
7955 oldflags = ctxt->flags;
7956 ctxt->flags |= FLAGS_IGNORABLE;
7957
7958 for (i = 0; i < ctxt->states->nbState; i++) {
7959 state = ctxt->states->tabState[i];
7960 ctxt->state = state;
7961 ctxt->state->seq = NULL;
7962
7963 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
7964 tmp = 0;
7965 xmlRelaxNGFreeValidState(ctxt, state);
7966 }
7967 xmlRelaxNGFreeStates(ctxt, ctxt->states);
7968 ctxt->states = NULL;
7969 if ((ret == 0) && (tmp == -1))
7970 ctxt->pstate = -1;
7971 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007972 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00007973 if (ctxt->pstate == -1) {
7974 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
7975 xmlRelaxNGDumpValidError(ctxt);
7976 }
7977 }
7978 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007979}
7980
7981/**
7982 * xmlRelaxNGValidatePushElement:
7983 * @ctxt: the validation context
7984 * @doc: a document instance
7985 * @elem: an element instance
7986 *
7987 * Push a new element start on the RelaxNG validation stack.
7988 *
7989 * returns 1 if no validation problem was found or 0 if validating the
7990 * element requires a full node, and -1 in case of error.
7991 */
7992int
Daniel Veillard33300b42003-04-17 09:09:19 +00007993xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
7994 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00007995 xmlNodePtr elem)
7996{
7997 int ret = 1;
7998
7999 if ((ctxt == NULL) || (elem == NULL))
8000 return (-1);
8001
8002#ifdef DEBUG_PROGRESSIVE
8003 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8004#endif
8005 if (ctxt->elem == 0) {
8006 xmlRelaxNGPtr schema;
8007 xmlRelaxNGGrammarPtr grammar;
8008 xmlRegExecCtxtPtr exec;
8009 xmlRelaxNGDefinePtr define;
8010
8011 schema = ctxt->schema;
8012 if (schema == NULL) {
8013 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8014 return (-1);
8015 }
8016 grammar = schema->topgrammar;
8017 if ((grammar == NULL) || (grammar->start == NULL)) {
8018 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8019 return (-1);
8020 }
8021 define = grammar->start;
8022 if (define->contModel == NULL) {
8023 ctxt->pdef = define;
8024 return (0);
8025 }
8026 exec = xmlRegNewExecCtxt(define->contModel,
8027 xmlRelaxNGValidateProgressiveCallback,
8028 ctxt);
8029 if (exec == NULL) {
8030 return (-1);
8031 }
8032 xmlRelaxNGElemPush(ctxt, exec);
8033 }
8034 ctxt->pnode = elem;
8035 ctxt->pstate = 0;
8036 if (elem->ns != NULL) {
8037 ret =
8038 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8039 ctxt);
8040 } else {
8041 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8042 }
8043 if (ret < 0) {
8044 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8045 } else {
8046 if (ctxt->pstate == 0)
8047 ret = 0;
8048 else if (ctxt->pstate < 0)
8049 ret = -1;
8050 else
8051 ret = 1;
8052 }
8053#ifdef DEBUG_PROGRESSIVE
8054 if (ret < 0)
8055 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8056 elem->name);
8057#endif
8058 return (ret);
8059}
8060
8061/**
8062 * xmlRelaxNGValidatePushCData:
8063 * @ctxt: the RelaxNG validation context
8064 * @data: some character data read
8065 * @len: the lenght of the data
8066 *
8067 * check the CData parsed for validation in the current stack
8068 *
8069 * returns 1 if no validation problem was found or -1 otherwise
8070 */
8071int
8072xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard33300b42003-04-17 09:09:19 +00008073 const xmlChar * data,
8074 int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008075{
8076 int ret = 1;
8077
8078 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8079 return (-1);
8080
8081#ifdef DEBUG_PROGRESSIVE
8082 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8083#endif
8084
8085 while (*data != 0) {
8086 if (!IS_BLANK(*data))
8087 break;
8088 data++;
8089 }
8090 if (*data == 0)
8091 return(1);
8092
8093 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8094 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008095 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008096#ifdef DEBUG_PROGRESSIVE
8097 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
8098#endif
8099
8100 return(-1);
8101 }
8102 return(1);
8103}
8104
8105/**
8106 * xmlRelaxNGValidatePopElement:
8107 * @ctxt: the RelaxNG validation context
8108 * @doc: a document instance
8109 * @elem: an element instance
8110 *
8111 * Pop the element end from the RelaxNG validation stack.
8112 *
8113 * returns 1 if no validation problem was found or 0 otherwise
8114 */
8115int
8116xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8117 xmlDocPtr doc ATTRIBUTE_UNUSED,
8118 xmlNodePtr elem) {
8119 int ret;
8120 xmlRegExecCtxtPtr exec;
8121
8122 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL)) return(-1);
8123#ifdef DEBUG_PROGRESSIVE
8124 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8125#endif
8126 /*
8127 * verify that we reached a terminal state of the content model.
8128 */
8129 exec = xmlRelaxNGElemPop(ctxt);
8130 ret = xmlRegExecPushString(exec, NULL, NULL);
8131 if (ret == 0) {
8132 /*
8133 * TODO: get some of the names needed to exit the current state of exec
8134 */
8135 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8136 ret = -1;
8137 } else if (ret < 0) {
8138 ret = -1;
8139 } else {
8140 ret = 1;
8141 }
8142 xmlRegFreeExecCtxt(exec);
8143#ifdef DEBUG_PROGRESSIVE
8144 if (ret < 0)
8145 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8146 elem->name);
8147#endif
8148 return(ret);
8149}
8150
8151/**
8152 * xmlRelaxNGValidateFullElement:
8153 * @ctxt: the validation context
8154 * @doc: a document instance
8155 * @elem: an element instance
8156 *
8157 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8158 * 0 and the content of the node has been expanded.
8159 *
8160 * returns 1 if no validation problem was found or -1 in case of error.
8161 */
8162int
Daniel Veillard33300b42003-04-17 09:09:19 +00008163xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8164 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008165 xmlNodePtr elem) {
8166 int ret;
8167 xmlRelaxNGValidStatePtr state;
8168
8169 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL)) return(-1);
8170#ifdef DEBUG_PROGRESSIVE
8171 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8172#endif
8173 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8174 if (state == NULL) {
8175 return(-1);
8176 }
8177 state->seq = elem;
8178 ctxt->state = state;
8179 ctxt->errNo = XML_RELAXNG_OK;
8180 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
8181 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK)) ret = -1;
8182 else ret = 1;
8183 xmlRelaxNGFreeValidState(ctxt, state);
8184 ctxt->state = NULL;
8185#ifdef DEBUG_PROGRESSIVE
8186 if (ret < 0)
8187 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8188 elem->name);
8189#endif
8190 return(ret);
8191}
8192
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008193/************************************************************************
8194 * *
8195 * Generic interpreted validation implementation *
8196 * *
8197 ************************************************************************/
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008198static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8199 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008200
8201/**
8202 * xmlRelaxNGSkipIgnored:
8203 * @ctxt: a schema validation context
8204 * @node: the top node.
8205 *
8206 * Skip ignorable nodes in that context
8207 *
8208 * Returns the new sibling or NULL in case of error.
8209 */
8210static xmlNodePtr
8211xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
8212 xmlNodePtr node) {
8213 /*
8214 * TODO complete and handle entities
8215 */
8216 while ((node != NULL) &&
8217 ((node->type == XML_COMMENT_NODE) ||
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008218 (node->type == XML_PI_NODE) ||
Daniel Veillard39eb88b2003-03-11 11:21:28 +00008219 (((node->type == XML_TEXT_NODE) ||
8220 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008221 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8222 (IS_BLANK_NODE(node)))))) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00008223 node = node->next;
8224 }
8225 return(node);
8226}
8227
8228/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008229 * xmlRelaxNGNormalize:
8230 * @ctxt: a schema validation context
8231 * @str: the string to normalize
8232 *
8233 * Implements the normalizeWhiteSpace( s ) function from
8234 * section 6.2.9 of the spec
8235 *
8236 * Returns the new string or NULL in case of error.
8237 */
8238static xmlChar *
8239xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) {
8240 xmlChar *ret, *p;
8241 const xmlChar *tmp;
8242 int len;
8243
8244 if (str == NULL)
8245 return(NULL);
8246 tmp = str;
8247 while (*tmp != 0) tmp++;
8248 len = tmp - str;
8249
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008250 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008251 if (ret == NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00008252 if (ctxt != NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008253 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
Daniel Veillardea3f3982003-01-26 19:45:18 +00008254 } else {
8255 xmlGenericError(xmlGenericErrorContext,
8256 "xmlRelaxNGNormalize: out of memory\n");
8257 }
Daniel Veillardedc91922003-01-26 00:52:04 +00008258 return(NULL);
8259 }
8260 p = ret;
8261 while (IS_BLANK(*str)) str++;
8262 while (*str != 0) {
8263 if (IS_BLANK(*str)) {
8264 while (IS_BLANK(*str)) str++;
8265 if (*str == 0)
8266 break;
8267 *p++ = ' ';
8268 } else
8269 *p++ = *str++;
8270 }
8271 *p = 0;
8272 return(ret);
8273}
8274
8275/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008276 * xmlRelaxNGValidateDatatype:
8277 * @ctxt: a Relax-NG validation context
8278 * @value: the string value
8279 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008280 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008281 *
8282 * Validate the given value against the dataype
8283 *
8284 * Returns 0 if the validation succeeded or an error code.
8285 */
8286static int
8287xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008288 xmlRelaxNGDefinePtr define, xmlNodePtr node) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008289 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008290 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008291 void *result = NULL;
8292 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008293
8294 if ((define == NULL) || (define->data == NULL)) {
8295 return(-1);
8296 }
8297 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008298 if (lib->check != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008299 if ((define->attrs != NULL) &&
8300 (define->attrs->type == XML_RELAXNG_PARAM)) {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008301 ret = lib->check(lib->data, define->name, value, &result, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008302 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008303 ret = lib->check(lib->data, define->name, value, NULL, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008304 }
8305 } else
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008306 ret = -1;
8307 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008308 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008309 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8310 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008311 return(-1);
8312 } else if (ret == 1) {
8313 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008314 } else if (ret == 2) {
8315 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008316 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008317 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008318 ret = -1;
8319 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008320 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008321 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
8322 if (lib->facet != NULL) {
8323 tmp = lib->facet(lib->data, define->name, cur->name,
8324 cur->value, value, result);
8325 if (tmp != 0)
8326 ret = -1;
8327 }
8328 cur = cur->next;
8329 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008330 if ((ret == 0) && (define->content != NULL)) {
8331 const xmlChar *oldvalue, *oldendvalue;
8332
8333 oldvalue = ctxt->state->value;
8334 oldendvalue = ctxt->state->endvalue;
8335 ctxt->state->value = (xmlChar *) value;
8336 ctxt->state->endvalue = NULL;
8337 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8338 ctxt->state->value = (xmlChar *) oldvalue;
8339 ctxt->state->endvalue = (xmlChar *) oldendvalue;
8340 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008341 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8342 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008343 return(ret);
8344}
8345
8346/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008347 * xmlRelaxNGNextValue:
8348 * @ctxt: a Relax-NG validation context
8349 *
8350 * Skip to the next value when validating within a list
8351 *
8352 * Returns 0 if the operation succeeded or an error code.
8353 */
8354static int
8355xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) {
8356 xmlChar *cur;
8357
8358 cur = ctxt->state->value;
8359 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
8360 ctxt->state->value = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00008361 ctxt->state->endvalue = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008362 return(0);
8363 }
8364 while (*cur != 0) cur++;
8365 while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++;
8366 if (cur == ctxt->state->endvalue)
8367 ctxt->state->value = NULL;
8368 else
8369 ctxt->state->value = cur;
8370 return(0);
8371}
8372
8373/**
8374 * xmlRelaxNGValidateValueList:
8375 * @ctxt: a Relax-NG validation context
8376 * @defines: the list of definitions to verify
8377 *
8378 * Validate the given set of definitions for the current value
8379 *
8380 * Returns 0 if the validation succeeded or an error code.
8381 */
8382static int
8383xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8384 xmlRelaxNGDefinePtr defines) {
8385 int ret = 0;
8386
8387 while (defines != NULL) {
8388 ret = xmlRelaxNGValidateValue(ctxt, defines);
8389 if (ret != 0)
8390 break;
8391 defines = defines->next;
8392 }
8393 return(ret);
8394}
8395
8396/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008397 * xmlRelaxNGValidateValue:
8398 * @ctxt: a Relax-NG validation context
8399 * @define: the definition to verify
8400 *
8401 * Validate the given definition for the current value
8402 *
8403 * Returns 0 if the validation succeeded or an error code.
8404 */
8405static int
8406xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8407 xmlRelaxNGDefinePtr define) {
Daniel Veillardedc91922003-01-26 00:52:04 +00008408 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008409 xmlChar *value;
8410
8411 value = ctxt->state->value;
8412 switch (define->type) {
Daniel Veillardd4310742003-02-18 21:12:46 +00008413 case XML_RELAXNG_EMPTY: {
8414 if ((value != NULL) && (value[0] != 0)) {
8415 int idx = 0;
8416
8417 while (IS_BLANK(value[idx]))
8418 idx++;
8419 if (value[idx] != 0)
8420 ret = -1;
8421 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008422 break;
Daniel Veillardd4310742003-02-18 21:12:46 +00008423 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008424 case XML_RELAXNG_TEXT:
8425 break;
Daniel Veillardedc91922003-01-26 00:52:04 +00008426 case XML_RELAXNG_VALUE: {
8427 if (!xmlStrEqual(value, define->value)) {
8428 if (define->name != NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00008429 xmlRelaxNGTypeLibraryPtr lib;
8430
8431 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00008432 if ((lib != NULL) && (lib->comp != NULL)) {
8433 ret = lib->comp(lib->data, define->name,
8434 define->value, define->node,
8435 (void *) define->attrs,
8436 value, ctxt->state->node);
8437 } else
Daniel Veillardea3f3982003-01-26 19:45:18 +00008438 ret = -1;
8439 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008440 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name);
Daniel Veillardea3f3982003-01-26 19:45:18 +00008441 return(-1);
8442 } else if (ret == 1) {
8443 ret = 0;
8444 } else {
8445 ret = -1;
8446 }
Daniel Veillardedc91922003-01-26 00:52:04 +00008447 } else {
8448 xmlChar *nval, *nvalue;
8449
8450 /*
8451 * TODO: trivial optimizations are possible by
8452 * computing at compile-time
8453 */
8454 nval = xmlRelaxNGNormalize(ctxt, define->value);
8455 nvalue = xmlRelaxNGNormalize(ctxt, value);
8456
Daniel Veillardea3f3982003-01-26 19:45:18 +00008457 if ((nval == NULL) || (nvalue == NULL) ||
8458 (!xmlStrEqual(nval, nvalue)))
Daniel Veillardedc91922003-01-26 00:52:04 +00008459 ret = -1;
8460 if (nval != NULL)
8461 xmlFree(nval);
8462 if (nvalue != NULL)
8463 xmlFree(nvalue);
8464 }
8465 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008466 if (ret == 0)
8467 xmlRelaxNGNextValue(ctxt);
Daniel Veillardedc91922003-01-26 00:52:04 +00008468 break;
8469 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008470 case XML_RELAXNG_DATATYPE: {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008471 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8472 ctxt->state->seq);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008473 if (ret == 0)
8474 xmlRelaxNGNextValue(ctxt);
8475
8476 break;
8477 }
8478 case XML_RELAXNG_CHOICE: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008479 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008480 xmlChar *oldvalue;
8481
8482 oldflags = ctxt->flags;
8483 ctxt->flags |= FLAGS_IGNORABLE;
8484
8485 oldvalue = ctxt->state->value;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008486 while (list != NULL) {
8487 ret = xmlRelaxNGValidateValue(ctxt, list);
8488 if (ret == 0) {
8489 break;
8490 }
8491 ctxt->state->value = oldvalue;
8492 list = list->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008493 }
8494 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00008495 if (ret != 0) {
8496 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8497 xmlRelaxNGDumpValidError(ctxt);
8498 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008499 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00008500 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008501 if (ret == 0)
8502 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008503 break;
8504 }
8505 case XML_RELAXNG_LIST: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008506 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008507 xmlChar *oldvalue, *oldend, *val, *cur;
Daniel Veillard416589a2003-02-17 17:25:42 +00008508#ifdef DEBUG_LIST
8509 int nb_values = 0;
8510#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008511
8512 oldvalue = ctxt->state->value;
8513 oldend = ctxt->state->endvalue;
8514
8515 val = xmlStrdup(oldvalue);
8516 if (val == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00008517 val = xmlStrdup(BAD_CAST "");
8518 }
8519 if (val == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008520 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008521 return(-1);
8522 }
8523 cur = val;
8524 while (*cur != 0) {
Daniel Veillard416589a2003-02-17 17:25:42 +00008525 if (IS_BLANK(*cur)) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008526 *cur = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008527 cur++;
8528#ifdef DEBUG_LIST
8529 nb_values++;
8530#endif
8531 while (IS_BLANK(*cur))
8532 *cur++ = 0;
8533 } else
8534 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008535 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008536#ifdef DEBUG_LIST
8537 xmlGenericError(xmlGenericErrorContext,
8538 "list value: '%s' found %d items\n", oldvalue, nb_values);
8539 nb_values = 0;
8540#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008541 ctxt->state->endvalue = cur;
8542 cur = val;
8543 while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008544
Daniel Veillardfd573f12003-03-16 17:52:32 +00008545 ctxt->state->value = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008546
Daniel Veillardfd573f12003-03-16 17:52:32 +00008547 while (list != NULL) {
8548 if (ctxt->state->value == ctxt->state->endvalue)
8549 ctxt->state->value = NULL;
8550 ret = xmlRelaxNGValidateValue(ctxt, list);
8551 if (ret != 0) {
8552#ifdef DEBUG_LIST
8553 xmlGenericError(xmlGenericErrorContext,
8554 "Failed to validate value: '%s' with %d rule\n",
8555 ctxt->state->value, nb_values);
8556#endif
8557 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008558 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008559#ifdef DEBUG_LIST
8560 nb_values++;
8561#endif
8562 list = list->next;
8563 }
8564
8565 if ((ret == 0) && (ctxt->state->value != NULL) &&
8566 (ctxt->state->value != ctxt->state->endvalue)) {
8567 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value);
8568 ret = -1;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008569 }
8570 xmlFree(val);
8571 ctxt->state->value = oldvalue;
8572 ctxt->state->endvalue = oldend;
8573 break;
8574 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008575 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008576 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8577 if (ret != 0) {
8578 break;
8579 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008580 /* no break on purpose */
8581 case XML_RELAXNG_ZEROORMORE: {
8582 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008583
8584 oldflags = ctxt->flags;
8585 ctxt->flags |= FLAGS_IGNORABLE;
8586 cur = ctxt->state->value;
8587 temp = NULL;
8588 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8589 (temp != cur)) {
8590 temp = cur;
8591 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8592 if (ret != 0) {
8593 ctxt->state->value = temp;
8594 ret = 0;
8595 break;
8596 }
8597 cur = ctxt->state->value;
8598 }
8599 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00008600 if (ret != 0) {
8601 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8602 xmlRelaxNGDumpValidError(ctxt);
8603 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008604 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00008605 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008606 break;
8607 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008608 case XML_RELAXNG_EXCEPT: {
8609 xmlRelaxNGDefinePtr list;
8610
8611 list = define->content;
8612 while (list != NULL) {
8613 ret = xmlRelaxNGValidateValue(ctxt, list);
8614 if (ret == 0) {
8615 ret = -1;
8616 break;
8617 } else
8618 ret = 0;
8619 list = list->next;
8620 }
8621 break;
8622 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008623 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008624 case XML_RELAXNG_GROUP: {
8625 xmlRelaxNGDefinePtr list;
8626
8627 list = define->content;
8628 while (list != NULL) {
8629 ret = xmlRelaxNGValidateValue(ctxt, list);
8630 if (ret != 0) {
8631 ret = -1;
8632 break;
8633 } else
8634 ret = 0;
8635 list = list->next;
8636 }
Daniel Veillardd4310742003-02-18 21:12:46 +00008637 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008638 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008639 case XML_RELAXNG_REF:
8640 case XML_RELAXNG_PARENTREF:
8641 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8642 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008643 default:
8644 TODO
8645 ret = -1;
8646 }
8647 return(ret);
8648}
8649
8650/**
8651 * xmlRelaxNGValidateValueContent:
8652 * @ctxt: a Relax-NG validation context
8653 * @defines: the list of definitions to verify
8654 *
8655 * Validate the given definitions for the current value
8656 *
8657 * Returns 0 if the validation succeeded or an error code.
8658 */
8659static int
8660xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8661 xmlRelaxNGDefinePtr defines) {
8662 int ret = 0;
8663
8664 while (defines != NULL) {
8665 ret = xmlRelaxNGValidateValue(ctxt, defines);
8666 if (ret != 0)
8667 break;
8668 defines = defines->next;
8669 }
8670 return(ret);
8671}
8672
8673/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008674 * xmlRelaxNGAttributeMatch:
8675 * @ctxt: a Relax-NG validation context
8676 * @define: the definition to check
8677 * @prop: the attribute
8678 *
8679 * Check if the attribute matches the definition nameClass
8680 *
8681 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8682 */
8683static int
8684xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8685 xmlRelaxNGDefinePtr define,
8686 xmlAttrPtr prop) {
8687 int ret;
8688
8689 if (define->name != NULL) {
8690 if (!xmlStrEqual(define->name, prop->name))
8691 return(0);
8692 }
8693 if (define->ns != NULL) {
8694 if (define->ns[0] == 0) {
8695 if (prop->ns != NULL)
8696 return(0);
8697 } else {
8698 if ((prop->ns == NULL) ||
8699 (!xmlStrEqual(define->ns, prop->ns->href)))
8700 return(0);
8701 }
8702 }
8703 if (define->nameClass == NULL)
8704 return(1);
8705 define = define->nameClass;
8706 if (define->type == XML_RELAXNG_EXCEPT) {
8707 xmlRelaxNGDefinePtr list;
8708
8709 list = define->content;
8710 while (list != NULL) {
8711 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8712 if (ret == 1)
8713 return(0);
8714 if (ret < 0)
8715 return(ret);
8716 list = list->next;
8717 }
8718 } else {
8719 TODO
8720 }
8721 return(1);
8722}
8723
8724/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008725 * xmlRelaxNGValidateAttribute:
8726 * @ctxt: a Relax-NG validation context
8727 * @define: the definition to verify
8728 *
8729 * Validate the given attribute definition for that node
8730 *
8731 * Returns 0 if the validation succeeded or an error code.
8732 */
8733static int
8734xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8735 xmlRelaxNGDefinePtr define) {
8736 int ret = 0, i;
8737 xmlChar *value, *oldvalue;
8738 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008739 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008740
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008741 if (ctxt->state->nbAttrLeft <= 0)
8742 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008743 if (define->name != NULL) {
8744 for (i = 0;i < ctxt->state->nbAttrs;i++) {
8745 tmp = ctxt->state->attrs[i];
8746 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8747 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8748 (tmp->ns == NULL)) ||
8749 ((tmp->ns != NULL) &&
8750 (xmlStrEqual(define->ns, tmp->ns->href)))) {
8751 prop = tmp;
8752 break;
8753 }
8754 }
8755 }
8756 if (prop != NULL) {
8757 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8758 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008759 oldseq = ctxt->state->seq;
8760 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008761 ctxt->state->value = value;
Daniel Veillard231d7912003-02-09 14:22:17 +00008762 ctxt->state->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008763 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00008764 if (ctxt->state->value != NULL)
8765 value = ctxt->state->value;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008766 if (value != NULL)
8767 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00008768 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008769 ctxt->state->seq = oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008770 if (ret == 0) {
8771 /*
8772 * flag the attribute as processed
8773 */
8774 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008775 ctxt->state->nbAttrLeft--;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008776 }
8777 } else {
8778 ret = -1;
8779 }
8780#ifdef DEBUG
8781 xmlGenericError(xmlGenericErrorContext,
8782 "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret);
8783#endif
8784 } else {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008785 for (i = 0;i < ctxt->state->nbAttrs;i++) {
8786 tmp = ctxt->state->attrs[i];
Daniel Veillard144fae12003-02-03 13:17:57 +00008787 if ((tmp != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00008788 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008789 prop = tmp;
8790 break;
8791 }
8792 }
8793 if (prop != NULL) {
8794 value = xmlNodeListGetString(prop->doc, prop->children, 1);
8795 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008796 oldseq = ctxt->state->seq;
8797 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008798 ctxt->state->value = value;
8799 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00008800 if (ctxt->state->value != NULL)
8801 value = ctxt->state->value;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008802 if (value != NULL)
8803 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00008804 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008805 ctxt->state->seq = oldseq;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008806 if (ret == 0) {
8807 /*
8808 * flag the attribute as processed
8809 */
8810 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00008811 ctxt->state->nbAttrLeft--;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008812 }
8813 } else {
8814 ret = -1;
8815 }
8816#ifdef DEBUG
Daniel Veillard144fae12003-02-03 13:17:57 +00008817 if (define->ns != NULL) {
8818 xmlGenericError(xmlGenericErrorContext,
8819 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8820 define->ns, ret);
8821 } else {
8822 xmlGenericError(xmlGenericErrorContext,
8823 "xmlRelaxNGValidateAttribute(anyName): %d\n",
8824 ret);
8825 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00008826#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00008827 }
8828
8829 return(ret);
8830}
8831
8832/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008833 * xmlRelaxNGValidateAttributeList:
8834 * @ctxt: a Relax-NG validation context
8835 * @define: the list of definition to verify
8836 *
8837 * Validate the given node against the list of attribute definitions
8838 *
8839 * Returns 0 if the validation succeeded or an error code.
8840 */
8841static int
8842xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8843 xmlRelaxNGDefinePtr defines) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00008844 int ret = 0, res;
8845 int needmore = 0;
8846 xmlRelaxNGDefinePtr cur;
8847
8848 cur = defines;
8849 while (cur != NULL) {
8850 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
8851 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
8852 ret = -1;
8853 } else
8854 needmore = 1;
8855 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008856 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008857 if (!needmore)
8858 return(ret);
8859 cur = defines;
8860 while (cur != NULL) {
8861 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
8862 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8863 res = xmlRelaxNGValidateDefinition(ctxt, cur);
8864 if (res < 0)
8865 ret = -1;
8866 } else {
8867 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8868 return(-1);
8869 }
8870 if (res == -1) /* continues on -2 */
8871 break;
8872 }
8873 cur = cur->next;
8874 }
8875
Daniel Veillardfd573f12003-03-16 17:52:32 +00008876 return(ret);
8877}
8878
8879/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008880 * xmlRelaxNGNodeMatchesList:
8881 * @node: the node
8882 * @list: a NULL terminated array of definitions
8883 *
8884 * Check if a node can be matched by one of the definitions
8885 *
8886 * Returns 1 if matches 0 otherwise
8887 */
8888static int
8889xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) {
8890 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008891 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008892
8893 if ((node == NULL) || (list == NULL))
8894 return(0);
8895
8896 cur = list[i++];
8897 while (cur != NULL) {
8898 if ((node->type == XML_ELEMENT_NODE) &&
8899 (cur->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008900 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8901 if (tmp == 1)
8902 return(1);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00008903 } else if (((node->type == XML_TEXT_NODE) ||
8904 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008905 (cur->type == XML_RELAXNG_TEXT)) {
8906 return(1);
8907 }
8908 cur = list[i++];
8909 }
8910 return(0);
8911}
8912
8913/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008914 * xmlRelaxNGValidateInterleave:
8915 * @ctxt: a Relax-NG validation context
8916 * @define: the definition to verify
8917 *
8918 * Validate an interleave definition for a node.
8919 *
8920 * Returns 0 if the validation succeeded or an error code.
8921 */
8922static int
8923xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
8924 xmlRelaxNGDefinePtr define) {
William M. Brack779af002003-08-01 15:55:39 +00008925 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008926 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008927 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008928
8929 xmlRelaxNGValidStatePtr oldstate;
8930 xmlRelaxNGPartitionPtr partitions;
8931 xmlRelaxNGInterleaveGroupPtr group = NULL;
8932 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
8933 xmlNodePtr *list = NULL, *lasts = NULL;
8934
8935 if (define->data != NULL) {
8936 partitions = (xmlRelaxNGPartitionPtr) define->data;
8937 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008938 } else {
8939 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
8940 return(-1);
8941 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008942 /*
8943 * Optimizations for MIXED
8944 */
8945 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00008946 if (define->dflags & IS_MIXED) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008947 ctxt->flags |= FLAGS_MIXED_CONTENT;
8948 if (nbgroups == 2) {
8949 /*
8950 * this is a pure <mixed> case
8951 */
8952 if (ctxt->state != NULL)
8953 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8954 ctxt->state->seq);
8955 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
8956 ret = xmlRelaxNGValidateDefinition(ctxt,
8957 partitions->groups[1]->rule);
8958 else
8959 ret = xmlRelaxNGValidateDefinition(ctxt,
8960 partitions->groups[0]->rule);
8961 if (ret == 0) {
8962 if (ctxt->state != NULL)
8963 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8964 ctxt->state->seq);
8965 }
8966 ctxt->flags = oldflags;
8967 return(ret);
8968 }
8969 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008970
8971 /*
8972 * Build arrays to store the first and last node of the chain
8973 * pertaining to each group
8974 */
8975 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8976 if (list == NULL) {
8977 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8978 return(-1);
8979 }
8980 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
8981 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8982 if (lasts == NULL) {
8983 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8984 return(-1);
8985 }
8986 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
8987
8988 /*
8989 * Walk the sequence of children finding the right group and
8990 * sorting them in sequences.
8991 */
8992 cur = ctxt->state->seq;
8993 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
8994 start = cur;
8995 while (cur != NULL) {
8996 ctxt->state->seq = cur;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00008997 if ((partitions->triage != NULL) &&
8998 (partitions->flags & IS_DETERMINIST)) {
8999 void *tmp = NULL;
9000
9001 if ((cur->type == XML_TEXT_NODE) ||
9002 (cur->type == XML_CDATA_SECTION_NODE)) {
9003 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9004 NULL);
9005 } else if (cur->type == XML_ELEMENT_NODE) {
9006 if (cur->ns != NULL) {
9007 tmp = xmlHashLookup2(partitions->triage, cur->name,
9008 cur->ns->href);
9009 if (tmp == NULL)
9010 tmp = xmlHashLookup2(partitions->triage,
9011 BAD_CAST "#any", cur->ns->href);
9012 } else
9013 tmp = xmlHashLookup2(partitions->triage, cur->name, NULL);
9014 if (tmp == NULL)
9015 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9016 NULL);
9017 }
9018
9019 if (tmp == NULL) {
9020 i = nbgroups;
9021 } else {
9022 i = ((long) tmp) - 1;
9023 if (partitions->flags & IS_NEEDCHECK) {
9024 group = partitions->groups[i];
9025 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9026 i = nbgroups;
9027 }
9028 }
9029 } else {
9030 for (i = 0;i < nbgroups;i++) {
9031 group = partitions->groups[i];
9032 if (group == NULL)
9033 continue;
9034 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9035 break;
9036 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009037 }
9038 /*
9039 * We break as soon as an element not matched is found
9040 */
9041 if (i >= nbgroups) {
9042 break;
9043 }
9044 if (lasts[i] != NULL) {
9045 lasts[i]->next = cur;
9046 lasts[i] = cur;
9047 } else {
9048 list[i] = cur;
9049 lasts[i] = cur;
9050 }
9051 if (cur->next != NULL)
9052 lastchg = cur->next;
9053 else
9054 lastchg = cur;
9055 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
9056 }
9057 if (ret != 0) {
9058 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9059 ret = -1;
9060 goto done;
9061 }
9062 lastelem = cur;
9063 oldstate = ctxt->state;
9064 for (i = 0;i < nbgroups;i++) {
9065 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9066 group = partitions->groups[i];
9067 if (lasts[i] != NULL) {
9068 last = lasts[i]->next;
9069 lasts[i]->next = NULL;
9070 }
9071 ctxt->state->seq = list[i];
9072 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9073 if (ret != 0)
9074 break;
9075 if (ctxt->state != NULL) {
9076 cur = ctxt->state->seq;
9077 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
Daniel Veillard798024a2003-03-19 10:36:09 +00009078 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009079 oldstate = ctxt->state;
9080 ctxt->state = NULL;
9081 if (cur != NULL) {
9082 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9083 ret = -1;
9084 ctxt->state = oldstate;
9085 goto done;
9086 }
9087 } else if (ctxt->states != NULL) {
9088 int j;
9089 int found = 0;
9090
9091 for (j = 0;j < ctxt->states->nbState;j++) {
9092 cur = ctxt->states->tabState[j]->seq;
9093 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9094 if (cur == NULL) {
9095 found = 1;
9096 break;
9097 }
9098 }
9099 if (ctxt->states->nbState > 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00009100 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009101 oldstate = ctxt->states->tabState[ctxt->states->nbState - 1];
9102 }
9103 for (j = 0;j < ctxt->states->nbState - 1;j++) {
Daniel Veillard798024a2003-03-19 10:36:09 +00009104 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009105 }
9106 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9107 ctxt->states = NULL;
9108 if (found == 0) {
9109 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9110 ret = -1;
9111 ctxt->state = oldstate;
9112 goto done;
9113 }
9114 } else {
9115 ret = -1;
9116 break;
9117 }
9118 if (lasts[i] != NULL) {
9119 lasts[i]->next = last;
9120 }
9121 }
9122 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00009123 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009124 ctxt->state = oldstate;
9125 ctxt->state->seq = lastelem;
9126 if (ret != 0) {
9127 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9128 ret = -1;
9129 goto done;
9130 }
9131
9132done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009133 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009134 /*
9135 * builds the next links chain from the prev one
9136 */
9137 cur = lastchg;
9138 while (cur != NULL) {
9139 if ((cur == start) || (cur->prev == NULL))
9140 break;
9141 cur->prev->next = cur;
9142 cur = cur->prev;
9143 }
9144 if (ret == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00009145 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009146 }
9147
9148 xmlFree(list);
9149 xmlFree(lasts);
9150 return(ret);
9151}
9152
9153/**
9154 * xmlRelaxNGValidateDefinitionList:
9155 * @ctxt: a Relax-NG validation context
9156 * @define: the list of definition to verify
9157 *
9158 * Validate the given node content against the (list) of definitions
9159 *
9160 * Returns 0 if the validation succeeded or an error code.
9161 */
9162static int
9163xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9164 xmlRelaxNGDefinePtr defines) {
9165 int ret = 0, res;
9166
9167
Daniel Veillard952379b2003-03-17 15:37:12 +00009168 if (defines == NULL) {
9169 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list");
9170 return(-1);
9171 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009172 while (defines != NULL) {
9173 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9174 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9175 if (res < 0)
9176 ret = -1;
9177 } else {
9178 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9179 return(-1);
9180 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00009181 if (res == -1) /* continues on -2 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009182 break;
9183 defines = defines->next;
9184 }
9185
9186 return(ret);
9187}
9188
9189/**
9190 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009191 * @ctxt: a Relax-NG validation context
9192 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009193 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009194 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009195 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009196 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009197 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009198 */
9199static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00009200xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9201 xmlRelaxNGDefinePtr define,
9202 xmlNodePtr elem) {
Daniel Veillard580ced82003-03-21 21:22:48 +00009203 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009204
Daniel Veillardfd573f12003-03-16 17:52:32 +00009205 if (define->name != NULL) {
9206 if (!xmlStrEqual(elem->name, define->name)) {
9207 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9208 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009209 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009210 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009211 if ((define->ns != NULL) && (define->ns[0] != 0)) {
9212 if (elem->ns == NULL) {
9213 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS,
9214 elem->name);
9215 return(0);
9216 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9217 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9218 elem->name, define->ns);
9219 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009220 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009221 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
9222 (define->name == NULL)) {
9223 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
9224 elem->name);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009225 return(0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009226 } else if ((elem->ns != NULL) && (define->name != NULL)) {
9227 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
9228 define->name);
9229 return(0);
9230 }
9231
9232 if (define->nameClass == NULL)
9233 return(1);
9234
9235 define = define->nameClass;
9236 if (define->type == XML_RELAXNG_EXCEPT) {
9237 xmlRelaxNGDefinePtr list;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009238 if (ctxt != NULL) {
9239 oldflags = ctxt->flags;
9240 ctxt->flags |= FLAGS_IGNORABLE;
9241 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009242
9243 list = define->content;
9244 while (list != NULL) {
9245 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9246 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009247 if (ctxt != NULL)
9248 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009249 return(0);
9250 }
9251 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009252 if (ctxt != NULL)
9253 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009254 return(ret);
9255 }
9256 list = list->next;
9257 }
9258 ret = 1;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009259 if (ctxt != NULL) {
9260 ctxt->flags = oldflags;
9261 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009262 } else if (define->type == XML_RELAXNG_CHOICE) {
9263 xmlRelaxNGDefinePtr list;
9264
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009265 if (ctxt != NULL) {
9266 oldflags = ctxt->flags;
9267 ctxt->flags |= FLAGS_IGNORABLE;
9268 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009269
9270 list = define->nameClass;
9271 while (list != NULL) {
9272 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9273 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009274 if (ctxt != NULL)
9275 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009276 return(1);
9277 }
9278 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009279 if (ctxt != NULL)
9280 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009281 return(ret);
9282 }
9283 list = list->next;
9284 }
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009285 if (ctxt != NULL) {
9286 if (ret != 0) {
9287 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9288 xmlRelaxNGDumpValidError(ctxt);
9289 } else {
9290 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
9291 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009292 }
9293 ret = 0;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009294 if (ctxt != NULL) {
9295 ctxt->flags = oldflags;
9296 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009297 } else {
9298 TODO
9299 ret = -1;
9300 }
9301 return(ret);
9302}
9303
9304/**
9305 * xmlRelaxNGValidateElementEnd:
9306 * @ctxt: a Relax-NG validation context
9307 *
9308 * Validate the end of the element, implements check that
9309 * there is nothing left not consumed in the element content
9310 * or in the attribute list.
9311 *
9312 * Returns 0 if the validation succeeded or an error code.
9313 */
9314static int
9315xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) {
9316 int ret = 0, i;
9317 xmlRelaxNGValidStatePtr state;
9318
9319 state = ctxt->state;
9320 if (state->seq != NULL) {
9321 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9322 if (state->seq != NULL) {
9323 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9324 state->node->name, state->seq->name);
9325 ret = -1;
9326 }
9327 }
9328 for (i = 0;i < state->nbAttrs;i++) {
9329 if (state->attrs[i] != NULL) {
9330 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9331 state->attrs[i]->name, state->node->name);
9332 ret = -1;
9333 }
9334 }
9335 return(ret);
9336}
9337
9338/**
9339 * xmlRelaxNGValidateState:
9340 * @ctxt: a Relax-NG validation context
9341 * @define: the definition to verify
9342 *
9343 * Validate the current state against the definition
9344 *
9345 * Returns 0 if the validation succeeded or an error code.
9346 */
9347static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009348xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9349 xmlRelaxNGDefinePtr define)
9350{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009351 xmlNodePtr node;
9352 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009353 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009354
9355 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009356 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9357 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009358 }
9359
9360 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009361 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009362 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009363 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009364 }
9365#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009366 for (i = 0; i < ctxt->depth; i++)
9367 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009368 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009369 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009370 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009371 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009372 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009373 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009374 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009375 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009376#endif
9377 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009378 switch (define->type) {
9379 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009380 node = xmlRelaxNGSkipIgnored(ctxt, node);
9381 ret = 0;
9382 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009383 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009384 ret = -1;
9385 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009386 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009387 while ((node != NULL) &&
9388 ((node->type == XML_TEXT_NODE) ||
9389 (node->type == XML_COMMENT_NODE) ||
9390 (node->type == XML_PI_NODE) ||
9391 (node->type == XML_CDATA_SECTION_NODE)))
9392 node = node->next;
9393 ctxt->state->seq = node;
9394 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009395 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009396 errNr = ctxt->errNr;
9397 node = xmlRelaxNGSkipIgnored(ctxt, node);
9398 if (node == NULL) {
9399 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9400 ret = -1;
9401 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9402 xmlRelaxNGDumpValidError(ctxt);
9403 break;
9404 }
9405 if (node->type != XML_ELEMENT_NODE) {
9406 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9407 ret = -1;
9408 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9409 xmlRelaxNGDumpValidError(ctxt);
9410 break;
9411 }
9412 /*
9413 * This node was already validated successfully against
9414 * this definition.
9415 */
9416 if (node->_private == define) {
9417 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9418 if (ctxt->errNr > errNr)
9419 xmlRelaxNGPopErrors(ctxt, errNr);
9420 if (ctxt->errNr != 0) {
9421 while ((ctxt->err != NULL) &&
9422 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9423 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9424 ||
9425 ((ctxt->err->err ==
9426 XML_RELAXNG_ERR_ELEMEXTRANS)
9427 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9428 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9429 || (ctxt->err->err ==
9430 XML_RELAXNG_ERR_NOTELEM)))
9431 xmlRelaxNGValidErrorPop(ctxt);
9432 }
9433 break;
9434 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009435
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009436 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9437 if (ret <= 0) {
9438 ret = -1;
9439 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9440 xmlRelaxNGDumpValidError(ctxt);
9441 break;
9442 }
9443 ret = 0;
9444 if (ctxt->errNr != 0) {
9445 if (ctxt->errNr > errNr)
9446 xmlRelaxNGPopErrors(ctxt, errNr);
9447 while ((ctxt->err != NULL) &&
9448 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9449 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9450 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9451 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9452 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9453 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9454 xmlRelaxNGValidErrorPop(ctxt);
9455 }
9456 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009457
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009458 oldflags = ctxt->flags;
9459 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9460 ctxt->flags -= FLAGS_MIXED_CONTENT;
9461 }
9462 state = xmlRelaxNGNewValidState(ctxt, node);
9463 if (state == NULL) {
9464 ret = -1;
9465 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9466 xmlRelaxNGDumpValidError(ctxt);
9467 break;
9468 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009469
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009470 oldstate = ctxt->state;
9471 ctxt->state = state;
9472 if (define->attrs != NULL) {
9473 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9474 if (tmp != 0) {
9475 ret = -1;
9476 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9477 }
9478 }
9479 if (define->contModel != NULL) {
Daniel Veillardce192eb2003-04-16 15:58:05 +00009480 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9481 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9482 xmlNodePtr nseq;
9483
9484 nstate = xmlRelaxNGNewValidState(ctxt, node);
9485 ctxt->state = nstate;
9486 ctxt->states = NULL;
9487
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009488 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9489 define->contModel,
9490 ctxt->state->seq);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009491 nseq = ctxt->state->seq;
9492 ctxt->state = tmpstate;
9493 ctxt->states = tmpstates;
9494 xmlRelaxNGFreeValidState(ctxt, nstate);
9495
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009496#ifdef DEBUG_COMPILE
9497 xmlGenericError(xmlGenericErrorContext,
9498 "Validating content of '%s' : %d\n", define->name, tmp);
9499#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009500 if (tmp != 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009501 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009502
9503 if (ctxt->states != NULL) {
9504 tmp = -1;
9505
9506 ctxt->flags |= FLAGS_IGNORABLE;
9507
9508 for (i = 0; i < ctxt->states->nbState; i++) {
9509 state = ctxt->states->tabState[i];
9510 ctxt->state = state;
9511 ctxt->state->seq = nseq;
9512
9513 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
9514 tmp = 0;
9515 xmlRelaxNGFreeValidState(ctxt, state);
9516 }
9517 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9518 ctxt->flags = oldflags;
9519 ctxt->states = NULL;
9520 if ((ret == 0) && (tmp == -1))
9521 ret = -1;
9522 } else {
9523 state = ctxt->state;
9524 ctxt->state->seq = nseq;
9525 if (ret == 0)
9526 ret = xmlRelaxNGValidateElementEnd(ctxt);
9527 xmlRelaxNGFreeValidState(ctxt, state);
9528 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009529 } else {
9530 if (define->content != NULL) {
9531 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
9532 define->content);
9533 if (tmp != 0) {
9534 ret = -1;
9535 if (ctxt->state == NULL) {
9536 ctxt->state = oldstate;
9537 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9538 node->name);
9539 ctxt->state = NULL;
9540 } else {
9541 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9542 node->name);
9543 }
9544
9545 }
9546 }
9547 if (ctxt->states != NULL) {
9548 tmp = -1;
9549
9550 ctxt->flags |= FLAGS_IGNORABLE;
9551
9552 for (i = 0; i < ctxt->states->nbState; i++) {
9553 state = ctxt->states->tabState[i];
9554 ctxt->state = state;
9555
9556 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
9557 tmp = 0;
9558 xmlRelaxNGFreeValidState(ctxt, state);
9559 }
9560 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9561 ctxt->flags = oldflags;
9562 ctxt->states = NULL;
9563 if ((ret == 0) && (tmp == -1))
9564 ret = -1;
9565 } else {
9566 state = ctxt->state;
9567 if (ret == 0)
9568 ret = xmlRelaxNGValidateElementEnd(ctxt);
9569 xmlRelaxNGFreeValidState(ctxt, state);
9570 }
9571 }
9572 if (ret == 0) {
9573 node->_private = define;
9574 }
9575 ctxt->flags = oldflags;
9576 ctxt->state = oldstate;
9577 if (oldstate != NULL)
9578 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9579 if (ret != 0) {
9580 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9581 xmlRelaxNGDumpValidError(ctxt);
9582 ret = 0;
9583 } else {
9584 ret = -2;
9585 }
9586 } else {
9587 if (ctxt->errNr > errNr)
9588 xmlRelaxNGPopErrors(ctxt, errNr);
9589 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009590
9591#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009592 xmlGenericError(xmlGenericErrorContext,
9593 "xmlRelaxNGValidateDefinition(): validated %s : %d",
9594 node->name, ret);
9595 if (oldstate == NULL)
9596 xmlGenericError(xmlGenericErrorContext, ": no state\n");
9597 else if (oldstate->seq == NULL)
9598 xmlGenericError(xmlGenericErrorContext, ": done\n");
9599 else if (oldstate->seq->type == XML_ELEMENT_NODE)
9600 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9601 oldstate->seq->name);
9602 else
9603 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9604 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009605#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009606 break;
9607 case XML_RELAXNG_OPTIONAL:{
9608 errNr = ctxt->errNr;
9609 oldflags = ctxt->flags;
9610 ctxt->flags |= FLAGS_IGNORABLE;
9611 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9612 ret =
9613 xmlRelaxNGValidateDefinitionList(ctxt,
9614 define->content);
9615 if (ret != 0) {
9616 if (ctxt->state != NULL)
9617 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9618 ctxt->state = oldstate;
9619 ctxt->flags = oldflags;
9620 ret = 0;
9621 if (ctxt->errNr > errNr)
9622 xmlRelaxNGPopErrors(ctxt, errNr);
9623 break;
9624 }
9625 if (ctxt->states != NULL) {
9626 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9627 } else {
9628 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9629 if (ctxt->states == NULL) {
9630 xmlRelaxNGFreeValidState(ctxt, oldstate);
9631 ctxt->flags = oldflags;
9632 ret = -1;
9633 if (ctxt->errNr > errNr)
9634 xmlRelaxNGPopErrors(ctxt, errNr);
9635 break;
9636 }
9637 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9638 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9639 ctxt->state = NULL;
9640 }
9641 ctxt->flags = oldflags;
9642 ret = 0;
9643 if (ctxt->errNr > errNr)
9644 xmlRelaxNGPopErrors(ctxt, errNr);
9645 break;
9646 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009647 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009648 errNr = ctxt->errNr;
9649 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9650 if (ret != 0) {
9651 break;
9652 }
9653 if (ctxt->errNr > errNr)
9654 xmlRelaxNGPopErrors(ctxt, errNr);
9655 /* no break on purpose */
9656 case XML_RELAXNG_ZEROORMORE:{
9657 int progress;
9658 xmlRelaxNGStatesPtr states = NULL, res = NULL;
9659 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009660
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009661 errNr = ctxt->errNr;
9662 res = xmlRelaxNGNewStates(ctxt, 1);
9663 if (res == NULL) {
9664 ret = -1;
9665 break;
9666 }
9667 /*
9668 * All the input states are also exit states
9669 */
9670 if (ctxt->state != NULL) {
9671 xmlRelaxNGAddStates(ctxt, res,
9672 xmlRelaxNGCopyValidState(ctxt,
9673 ctxt->
9674 state));
9675 } else {
9676 for (j = 0; j < ctxt->states->nbState; j++) {
9677 xmlRelaxNGAddStates(ctxt, res,
9678 xmlRelaxNGCopyValidState(ctxt,
9679 ctxt->
9680 states->
9681 tabState
9682 [j]));
9683 }
9684 }
9685 oldflags = ctxt->flags;
9686 ctxt->flags |= FLAGS_IGNORABLE;
9687 do {
9688 progress = 0;
9689 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009690
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009691 if (ctxt->states != NULL) {
9692 states = ctxt->states;
9693 for (i = 0; i < states->nbState; i++) {
9694 ctxt->state = states->tabState[i];
9695 ctxt->states = NULL;
9696 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9697 define->
9698 content);
9699 if (ret == 0) {
9700 if (ctxt->state != NULL) {
9701 tmp = xmlRelaxNGAddStates(ctxt, res,
9702 ctxt->state);
9703 ctxt->state = NULL;
9704 if (tmp == 1)
9705 progress = 1;
9706 } else if (ctxt->states != NULL) {
9707 for (j = 0; j < ctxt->states->nbState;
9708 j++) {
9709 tmp =
9710 xmlRelaxNGAddStates(ctxt, res,
9711 ctxt->
9712 states->
9713 tabState
9714 [j]);
9715 if (tmp == 1)
9716 progress = 1;
9717 }
9718 xmlRelaxNGFreeStates(ctxt,
9719 ctxt->states);
9720 ctxt->states = NULL;
9721 }
9722 } else {
9723 if (ctxt->state != NULL) {
9724 xmlRelaxNGFreeValidState(ctxt,
9725 ctxt->state);
9726 ctxt->state = NULL;
9727 }
9728 }
9729 }
9730 } else {
9731 ret = xmlRelaxNGValidateDefinitionList(ctxt,
9732 define->
9733 content);
9734 if (ret != 0) {
9735 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9736 ctxt->state = NULL;
9737 } else {
9738 base = res->nbState;
9739 if (ctxt->state != NULL) {
9740 tmp = xmlRelaxNGAddStates(ctxt, res,
9741 ctxt->state);
9742 ctxt->state = NULL;
9743 if (tmp == 1)
9744 progress = 1;
9745 } else if (ctxt->states != NULL) {
9746 for (j = 0; j < ctxt->states->nbState; j++) {
9747 tmp = xmlRelaxNGAddStates(ctxt, res,
9748 ctxt->
9749 states->
9750 tabState[j]);
9751 if (tmp == 1)
9752 progress = 1;
9753 }
9754 if (states == NULL) {
9755 states = ctxt->states;
9756 } else {
9757 xmlRelaxNGFreeStates(ctxt,
9758 ctxt->states);
9759 }
9760 ctxt->states = NULL;
9761 }
9762 }
9763 }
9764 if (progress) {
9765 /*
9766 * Collect all the new nodes added at that step
9767 * and make them the new node set
9768 */
9769 if (res->nbState - base == 1) {
9770 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
9771 res->
9772 tabState
9773 [base]);
9774 } else {
9775 if (states == NULL) {
9776 xmlRelaxNGNewStates(ctxt,
9777 res->nbState - base);
9778 }
9779 states->nbState = 0;
9780 for (i = base; i < res->nbState; i++)
9781 xmlRelaxNGAddStates(ctxt, states,
9782 xmlRelaxNGCopyValidState
9783 (ctxt,
9784 res->tabState[i]));
9785 ctxt->states = states;
9786 }
9787 }
9788 } while (progress == 1);
9789 if (states != NULL) {
9790 xmlRelaxNGFreeStates(ctxt, states);
9791 }
9792 ctxt->states = res;
9793 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009794#if 0
9795 /*
9796 * errors may have to be propagated back...
9797 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009798 if (ctxt->errNr > errNr)
9799 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00009800#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009801 ret = 0;
9802 break;
9803 }
9804 case XML_RELAXNG_CHOICE:{
9805 xmlRelaxNGDefinePtr list = NULL;
9806 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009807
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009808 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009809
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009810 errNr = ctxt->errNr;
9811 if ((define->dflags & IS_TRIABLE)
9812 && (define->data != NULL)) {
9813 xmlHashTablePtr triage =
9814 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +00009815
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009816 /*
9817 * Something we can optimize cleanly there is only one
9818 * possble branch out !
9819 */
9820 if (node == NULL) {
9821 ret = -1;
9822 break;
9823 }
9824 if ((node->type == XML_TEXT_NODE) ||
9825 (node->type == XML_CDATA_SECTION_NODE)) {
9826 list =
9827 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
9828 } else if (node->type == XML_ELEMENT_NODE) {
9829 if (node->ns != NULL) {
9830 list = xmlHashLookup2(triage, node->name,
9831 node->ns->href);
9832 if (list == NULL)
9833 list =
9834 xmlHashLookup2(triage, BAD_CAST "#any",
9835 node->ns->href);
9836 } else
9837 list =
9838 xmlHashLookup2(triage, node->name, NULL);
9839 if (list == NULL)
9840 list =
9841 xmlHashLookup2(triage, BAD_CAST "#any",
9842 NULL);
9843 }
9844 if (list == NULL) {
9845 ret = -1;
9846 break;
9847 }
9848 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9849 if (ret == 0) {
9850 }
9851 break;
9852 }
Daniel Veillarde063f482003-03-21 16:53:17 +00009853
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009854 list = define->content;
9855 oldflags = ctxt->flags;
9856 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009857
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009858 while (list != NULL) {
9859 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9860 ret = xmlRelaxNGValidateDefinition(ctxt, list);
9861 if (ret == 0) {
9862 if (states == NULL) {
9863 states = xmlRelaxNGNewStates(ctxt, 1);
9864 }
9865 if (ctxt->state != NULL) {
9866 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
9867 } else if (ctxt->states != NULL) {
9868 for (i = 0; i < ctxt->states->nbState; i++) {
9869 xmlRelaxNGAddStates(ctxt, states,
9870 ctxt->states->
9871 tabState[i]);
9872 }
9873 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9874 ctxt->states = NULL;
9875 }
9876 } else {
9877 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9878 }
9879 ctxt->state = oldstate;
9880 list = list->next;
9881 }
9882 if (states != NULL) {
9883 xmlRelaxNGFreeValidState(ctxt, oldstate);
9884 ctxt->states = states;
9885 ctxt->state = NULL;
9886 ret = 0;
9887 } else {
9888 ctxt->states = NULL;
9889 }
9890 ctxt->flags = oldflags;
9891 if (ret != 0) {
9892 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9893 xmlRelaxNGDumpValidError(ctxt);
9894 }
9895 } else {
9896 if (ctxt->errNr > errNr)
9897 xmlRelaxNGPopErrors(ctxt, errNr);
9898 }
9899 break;
9900 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009901 case XML_RELAXNG_DEF:
9902 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009903 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9904 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009905 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009906 ret = xmlRelaxNGValidateInterleave(ctxt, define);
9907 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009908 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009909 ret = xmlRelaxNGValidateAttribute(ctxt, define);
9910 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +00009911 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009912 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +00009913 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009914 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +00009915 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009916 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
9917 break;
9918 case XML_RELAXNG_DATATYPE:{
9919 xmlNodePtr child;
9920 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009921
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009922 child = node;
9923 while (child != NULL) {
9924 if (child->type == XML_ELEMENT_NODE) {
9925 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
9926 node->parent->name);
9927 ret = -1;
9928 break;
9929 } else if ((child->type == XML_TEXT_NODE) ||
9930 (child->type == XML_CDATA_SECTION_NODE)) {
9931 content = xmlStrcat(content, child->content);
9932 }
9933 /* TODO: handle entities ... */
9934 child = child->next;
9935 }
9936 if (ret == -1) {
9937 if (content != NULL)
9938 xmlFree(content);
9939 break;
9940 }
9941 if (content == NULL) {
9942 content = xmlStrdup(BAD_CAST "");
9943 if (content == NULL) {
9944 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
9945 ret = -1;
9946 break;
9947 }
9948 }
9949 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
9950 ctxt->state->seq);
9951 if (ret == -1) {
9952 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
9953 } else if (ret == 0) {
9954 ctxt->state->seq = NULL;
9955 }
9956 if (content != NULL)
9957 xmlFree(content);
9958 break;
9959 }
9960 case XML_RELAXNG_VALUE:{
9961 xmlChar *content = NULL;
9962 xmlChar *oldvalue;
9963 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009964
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009965 child = node;
9966 while (child != NULL) {
9967 if (child->type == XML_ELEMENT_NODE) {
9968 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
9969 node->parent->name);
9970 ret = -1;
9971 break;
9972 } else if ((child->type == XML_TEXT_NODE) ||
9973 (child->type == XML_CDATA_SECTION_NODE)) {
9974 content = xmlStrcat(content, child->content);
9975 }
9976 /* TODO: handle entities ... */
9977 child = child->next;
9978 }
9979 if (ret == -1) {
9980 if (content != NULL)
9981 xmlFree(content);
9982 break;
9983 }
9984 if (content == NULL) {
9985 content = xmlStrdup(BAD_CAST "");
9986 if (content == NULL) {
9987 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
9988 ret = -1;
9989 break;
9990 }
9991 }
9992 oldvalue = ctxt->state->value;
9993 ctxt->state->value = content;
9994 ret = xmlRelaxNGValidateValue(ctxt, define);
9995 ctxt->state->value = oldvalue;
9996 if (ret == -1) {
9997 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
9998 } else if (ret == 0) {
9999 ctxt->state->seq = NULL;
10000 }
10001 if (content != NULL)
10002 xmlFree(content);
10003 break;
10004 }
10005 case XML_RELAXNG_LIST:{
10006 xmlChar *content;
10007 xmlNodePtr child;
10008 xmlChar *oldvalue, *oldendvalue;
10009 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010010
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010011 /*
10012 * Make sure it's only text nodes
10013 */
10014
10015 content = NULL;
10016 child = node;
10017 while (child != NULL) {
10018 if (child->type == XML_ELEMENT_NODE) {
10019 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10020 node->parent->name);
10021 ret = -1;
10022 break;
10023 } else if ((child->type == XML_TEXT_NODE) ||
10024 (child->type == XML_CDATA_SECTION_NODE)) {
10025 content = xmlStrcat(content, child->content);
10026 }
10027 /* TODO: handle entities ... */
10028 child = child->next;
10029 }
10030 if (ret == -1) {
10031 if (content != NULL)
10032 xmlFree(content);
10033 break;
10034 }
10035 if (content == NULL) {
10036 content = xmlStrdup(BAD_CAST "");
10037 if (content == NULL) {
10038 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
10039 ret = -1;
10040 break;
10041 }
10042 }
10043 len = xmlStrlen(content);
10044 oldvalue = ctxt->state->value;
10045 oldendvalue = ctxt->state->endvalue;
10046 ctxt->state->value = content;
10047 ctxt->state->endvalue = content + len;
10048 ret = xmlRelaxNGValidateValue(ctxt, define);
10049 ctxt->state->value = oldvalue;
10050 ctxt->state->endvalue = oldendvalue;
10051 if (ret == -1) {
10052 VALID_ERR(XML_RELAXNG_ERR_LIST);
10053 } else if ((ret == 0) && (node != NULL)) {
10054 ctxt->state->seq = node->next;
10055 }
10056 if (content != NULL)
10057 xmlFree(content);
10058 break;
10059 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010060 case XML_RELAXNG_EXCEPT:
10061 case XML_RELAXNG_PARAM:
10062 TODO ret = -1;
10063 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010064 }
10065 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010066#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010067 for (i = 0; i < ctxt->depth; i++)
10068 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010069 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010070 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010071 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010072 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010073 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010074 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010075 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010076 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010077#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010078 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010079}
10080
10081/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010082 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010083 * @ctxt: a Relax-NG validation context
10084 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010085 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010086 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010087 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010088 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010089 */
10090static int
Daniel Veillardfd573f12003-03-16 17:52:32 +000010091xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10092 xmlRelaxNGDefinePtr define) {
10093 xmlRelaxNGStatesPtr states, res;
10094 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010095
Daniel Veillardfd573f12003-03-16 17:52:32 +000010096 /*
10097 * We should NOT have both ctxt->state and ctxt->states
10098 */
10099 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10100 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +000010101 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010102 ctxt->state = NULL;
10103 }
10104
10105 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
10106 if (ctxt->states != NULL) {
10107 ctxt->state = ctxt->states->tabState[0];
10108 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10109 ctxt->states = NULL;
10110 }
10111 ret = xmlRelaxNGValidateState(ctxt, define);
10112 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10113 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +000010114 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010115 ctxt->state = NULL;
10116 }
10117 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10118 ctxt->state = ctxt->states->tabState[0];
10119 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10120 ctxt->states = NULL;
10121 }
10122 return(ret);
10123 }
10124
10125 states = ctxt->states;
10126 ctxt->states = NULL;
10127 res = NULL;
10128 j = 0;
10129 oldflags = ctxt->flags;
10130 ctxt->flags |= FLAGS_IGNORABLE;
10131 for (i = 0;i < states->nbState;i++) {
10132 ctxt->state = states->tabState[i];
10133 ctxt->states = NULL;
10134 ret = xmlRelaxNGValidateState(ctxt, define);
10135 /*
10136 * We should NOT have both ctxt->state and ctxt->states
10137 */
10138 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10139 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +000010140 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010141 ctxt->state = NULL;
10142 }
10143 if (ret == 0) {
10144 if (ctxt->states == NULL) {
10145 if (res != NULL) {
10146 /* add the state to the container */
10147 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10148 ctxt->state = NULL;
10149 } else {
10150 /* add the state directly in states */
10151 states->tabState[j++] = ctxt->state;
10152 ctxt->state = NULL;
10153 }
10154 } else {
10155 if (res == NULL) {
10156 /* make it the new container and copy other results */
10157 res = ctxt->states;
10158 ctxt->states = NULL;
10159 for (k = 0;k < j;k++)
10160 xmlRelaxNGAddStates(ctxt, res, states->tabState[k]);
10161 } else {
10162 /* add all the new results to res and reff the container */
10163 for (k = 0;k < ctxt->states->nbState;k++)
10164 xmlRelaxNGAddStates(ctxt, res,
10165 ctxt->states->tabState[k]);
10166 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10167 ctxt->states = NULL;
10168 }
10169 }
10170 } else {
10171 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +000010172 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010173 ctxt->state = NULL;
10174 } else if (ctxt->states != NULL) {
10175 for (k = 0;k < ctxt->states->nbState;k++)
Daniel Veillard798024a2003-03-19 10:36:09 +000010176 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010177 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10178 ctxt->states = NULL;
10179 }
10180 }
10181 }
10182 ctxt->flags = oldflags;
10183 if (res != NULL) {
10184 xmlRelaxNGFreeStates(ctxt, states);
10185 ctxt->states = res;
10186 ret = 0;
10187 } else if (j > 1) {
10188 states->nbState = j;
10189 ctxt->states = states;
10190 ret =0;
10191 } else if (j == 1) {
10192 ctxt->state = states->tabState[0];
10193 xmlRelaxNGFreeStates(ctxt, states);
10194 ret = 0;
10195 } else {
10196 ret = -1;
10197 xmlRelaxNGFreeStates(ctxt, states);
10198 if (ctxt->states != NULL) {
10199 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10200 ctxt->states = NULL;
10201 }
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 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010208 return(ret);
10209}
10210
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010211/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010212 * xmlRelaxNGValidateDocument:
10213 * @ctxt: a Relax-NG validation context
10214 * @doc: the document
10215 *
10216 * Validate the given document
10217 *
10218 * Returns 0 if the validation succeeded or an error code.
10219 */
10220static int
10221xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
10222 int ret;
10223 xmlRelaxNGPtr schema;
10224 xmlRelaxNGGrammarPtr grammar;
10225 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010226 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010227
10228 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
10229 return(-1);
10230
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010231 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010232 schema = ctxt->schema;
10233 grammar = schema->topgrammar;
10234 if (grammar == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +000010235 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010236 return(-1);
10237 }
10238 state = xmlRelaxNGNewValidState(ctxt, NULL);
10239 ctxt->state = state;
10240 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010241 if ((ctxt->state != NULL) && (state->seq != NULL)) {
10242 state = ctxt->state;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010243 node = state->seq;
10244 node = xmlRelaxNGSkipIgnored(ctxt, node);
10245 if (node != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +000010246 if (ret != -1) {
10247 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10248 ret = -1;
10249 }
10250 }
10251 } else if (ctxt->states != NULL) {
10252 int i;
10253 int tmp = -1;
10254
10255 for (i = 0;i < ctxt->states->nbState;i++) {
10256 state = ctxt->states->tabState[i];
10257 node = state->seq;
10258 node = xmlRelaxNGSkipIgnored(ctxt, node);
10259 if (node == NULL)
10260 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +000010261 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010262 }
10263 if (tmp == -1) {
10264 if (ret != -1) {
10265 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10266 ret = -1;
10267 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010268 }
10269 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010270 if (ctxt->state != NULL) {
10271 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10272 ctxt->state = NULL;
10273 }
Daniel Veillard580ced82003-03-21 21:22:48 +000010274 if (ret != 0)
Daniel Veillardfd573f12003-03-16 17:52:32 +000010275 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010276#ifdef DEBUG
10277 else if (ctxt->errNr != 0) {
10278 ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n",
10279 ctxt->errNr);
10280 xmlRelaxNGDumpValidError(ctxt);
10281 }
10282#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010283 if (ctxt->idref == 1) {
10284 xmlValidCtxt vctxt;
10285
10286 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10287 vctxt.valid = 1;
10288 vctxt.error = ctxt->error;
10289 vctxt.warning = ctxt->warning;
10290 vctxt.userData = ctxt->userData;
10291
10292 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10293 ret = -1;
10294 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010295 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
10296 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010297
10298 return(ret);
10299}
10300
Daniel Veillardfd573f12003-03-16 17:52:32 +000010301/************************************************************************
10302 * *
10303 * Validation interfaces *
10304 * *
10305 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +000010306/**
10307 * xmlRelaxNGNewValidCtxt:
10308 * @schema: a precompiled XML RelaxNGs
10309 *
10310 * Create an XML RelaxNGs validation context based on the given schema
10311 *
10312 * Returns the validation context or NULL in case of error
10313 */
10314xmlRelaxNGValidCtxtPtr
10315xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) {
10316 xmlRelaxNGValidCtxtPtr ret;
10317
10318 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10319 if (ret == NULL) {
10320 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardfd573f12003-03-16 17:52:32 +000010321 "Failed to allocate new schema validation context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010322 return (NULL);
10323 }
10324 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10325 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010326 ret->error = xmlGenericError;
10327 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010328 ret->errNr = 0;
10329 ret->errMax = 0;
10330 ret->err = NULL;
10331 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010332 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010333 ret->states = NULL;
10334 ret->freeState = NULL;
10335 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010336 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010337 return (ret);
10338}
10339
10340/**
10341 * xmlRelaxNGFreeValidCtxt:
10342 * @ctxt: the schema validation context
10343 *
10344 * Free the resources associated to the schema validation context
10345 */
10346void
10347xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard798024a2003-03-19 10:36:09 +000010348 int k;
10349
Daniel Veillard6eadf632003-01-23 18:29:16 +000010350 if (ctxt == NULL)
10351 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010352 if (ctxt->states != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +000010353 xmlRelaxNGFreeStates(NULL, ctxt->states);
10354 if (ctxt->freeState != NULL) {
10355 for (k = 0;k < ctxt->freeState->nbState;k++) {
10356 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10357 }
10358 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
10359 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010360 if (ctxt->freeStates != NULL) {
10361 for (k = 0;k < ctxt->freeStatesNr;k++) {
10362 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10363 }
10364 xmlFree(ctxt->freeStates);
10365 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010366 if (ctxt->errTab != NULL)
10367 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010368 if (ctxt->elemTab != NULL) {
10369 xmlRegExecCtxtPtr exec;
10370
10371 exec = xmlRelaxNGElemPop(ctxt);
10372 while (exec != NULL) {
10373 xmlRegFreeExecCtxt(exec);
10374 exec = xmlRelaxNGElemPop(ctxt);
10375 }
10376 xmlFree(ctxt->elemTab);
10377 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010378 xmlFree(ctxt);
10379}
10380
10381/**
10382 * xmlRelaxNGSetValidErrors:
10383 * @ctxt: a Relax-NG validation context
10384 * @err: the error function
10385 * @warn: the warning function
10386 * @ctx: the functions context
10387 *
10388 * Set the error and warning callback informations
10389 */
10390void
10391xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10392 xmlRelaxNGValidityErrorFunc err,
10393 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
10394 if (ctxt == NULL)
10395 return;
10396 ctxt->error = err;
10397 ctxt->warning = warn;
10398 ctxt->userData = ctx;
10399}
10400
10401/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010402 * xmlRelaxNGGetValidErrors:
10403 * @ctxt: a Relax-NG validation context
10404 * @err: the error function result
10405 * @warn: the warning function result
10406 * @ctx: the functions context result
10407 *
10408 * Get the error and warning callback informations
10409 *
10410 * Returns -1 in case of error and 0 otherwise
10411 */
10412int
10413xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10414 xmlRelaxNGValidityErrorFunc *err,
10415 xmlRelaxNGValidityWarningFunc *warn, void **ctx) {
10416 if (ctxt == NULL)
10417 return(-1);
10418 if (err != NULL) *err = ctxt->error;
10419 if (warn != NULL) *warn = ctxt->warning;
10420 if (ctx != NULL) *ctx = ctxt->userData;
10421 return(0);
10422}
10423
10424/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010425 * xmlRelaxNGValidateDoc:
10426 * @ctxt: a Relax-NG validation context
10427 * @doc: a parsed document tree
10428 *
10429 * Validate a document tree in memory.
10430 *
10431 * Returns 0 if the document is valid, a positive error code
10432 * number otherwise and -1 in case of internal or API error.
10433 */
10434int
10435xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
10436 int ret;
10437
10438 if ((ctxt == NULL) || (doc == NULL))
10439 return(-1);
10440
10441 ctxt->doc = doc;
10442
10443 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000010444 /*
10445 * TODO: build error codes
10446 */
10447 if (ret == -1)
10448 return(1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010449 return(ret);
10450}
10451
10452#endif /* LIBXML_SCHEMAS_ENABLED */
10453