blob: 4deb647a0de4d76446bc3e1bd674b17a78d6c7fe [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 Veillardd41f4f42003-01-29 21:07:52 +000011 * - error reporting
Daniel Veillard1ed7f362003-02-03 10:57:45 +000012 * - handle namespace declarations as attributes.
Daniel Veillardf4b4f982003-02-13 11:02:08 +000013 * - add support for DTD compatibility spec
14 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
Daniel Veillardfd573f12003-03-16 17:52:32 +000015 * - report better mem allocations at runtime and abort immediately.
Daniel Veillardd41f4f42003-01-29 21:07:52 +000016 */
17
Daniel Veillard6eadf632003-01-23 18:29:16 +000018#define IN_LIBXML
19#include "libxml.h"
20
21#ifdef LIBXML_SCHEMAS_ENABLED
22
23#include <string.h>
24#include <stdio.h>
25#include <libxml/xmlmemory.h>
26#include <libxml/parser.h>
27#include <libxml/parserInternals.h>
28#include <libxml/hash.h>
29#include <libxml/uri.h>
30
31#include <libxml/relaxng.h>
32
33#include <libxml/xmlschemastypes.h>
34#include <libxml/xmlautomata.h>
35#include <libxml/xmlregexp.h>
Daniel Veillardc6e997c2003-01-27 12:35:42 +000036#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000037
38/*
39 * The Relax-NG namespace
40 */
41static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
42 "http://relaxng.org/ns/structure/1.0";
43
44#define IS_RELAXNG(node, type) \
45 ((node != NULL) && (node->ns != NULL) && \
46 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
47 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
48
49
Daniel Veillard952379b2003-03-17 15:37:12 +000050/* #define DEBUG 1 */
Daniel Veillardc482e262003-02-26 14:48:48 +000051/* #define DEBUG_GRAMMAR 1 */
Daniel Veillard71531f32003-02-05 13:19:53 +000052/* #define DEBUG_CONTENT 1 */
53/* #define DEBUG_TYPE 1 */
54/* #define DEBUG_VALID 1 */
Daniel Veillarde5b110b2003-02-04 14:43:39 +000055/* #define DEBUG_INTERLEAVE 1 */
Daniel Veillard416589a2003-02-17 17:25:42 +000056/* #define DEBUG_LIST 1 */
Daniel Veillard5add8682003-03-10 13:13:58 +000057/* #define DEBUG_INCLUDE */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000058/* #define DEBUG_ERROR 1 */
Daniel Veillard6eadf632003-01-23 18:29:16 +000059
60#define UNBOUNDED (1 << 30)
Daniel Veillard5f1946a2003-03-31 16:38:16 +000061#define MAX_ERROR 5
62
Daniel Veillard6eadf632003-01-23 18:29:16 +000063#define TODO \
64 xmlGenericError(xmlGenericErrorContext, \
65 "Unimplemented block at %s:%d\n", \
66 __FILE__, __LINE__);
67
68typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
69typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
70
71typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
72typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
73
Daniel Veillardd41f4f42003-01-29 21:07:52 +000074typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
75typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
76
Daniel Veillarda9d912d2003-02-01 17:43:10 +000077typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
78typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
79
Daniel Veillard6eadf632003-01-23 18:29:16 +000080typedef enum {
81 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
82 XML_RELAXNG_COMBINE_CHOICE, /* choice */
83 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
84} xmlRelaxNGCombine;
85
Daniel Veillard4c5cf702003-02-21 15:40:34 +000086typedef enum {
87 XML_RELAXNG_CONTENT_ERROR = -1,
88 XML_RELAXNG_CONTENT_EMPTY = 0,
89 XML_RELAXNG_CONTENT_SIMPLE,
90 XML_RELAXNG_CONTENT_COMPLEX
91} xmlRelaxNGContentType;
92
Daniel Veillard6eadf632003-01-23 18:29:16 +000093typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
94typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
95
96struct _xmlRelaxNGGrammar {
97 xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */
98 xmlRelaxNGGrammarPtr children;/* the children grammar if any */
99 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
100 xmlRelaxNGDefinePtr start; /* <start> content */
101 xmlRelaxNGCombine combine; /* the default combine value */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000102 xmlRelaxNGDefinePtr startList;/* list of <start> definitions */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000103 xmlHashTablePtr defs; /* define* */
104 xmlHashTablePtr refs; /* references */
105};
106
107
Daniel Veillard6eadf632003-01-23 18:29:16 +0000108typedef enum {
Daniel Veillard77648bb2003-02-20 15:03:22 +0000109 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000110 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
111 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard144fae12003-02-03 13:17:57 +0000112 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000113 XML_RELAXNG_TEXT, /* textual content */
114 XML_RELAXNG_ELEMENT, /* an element */
115 XML_RELAXNG_DATATYPE, /* extenal data type definition */
Daniel Veillard8fe98712003-02-19 00:19:14 +0000116 XML_RELAXNG_PARAM, /* extenal data type parameter */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000117 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
118 XML_RELAXNG_LIST, /* a list of patterns */
119 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
120 XML_RELAXNG_DEF, /* a definition */
121 XML_RELAXNG_REF, /* reference to a definition */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000122 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
Daniel Veillard419a7682003-02-03 23:22:49 +0000123 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000124 XML_RELAXNG_OPTIONAL, /* optional patterns */
125 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000126 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
127 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
128 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000129 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000130 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000131} xmlRelaxNGType;
132
Daniel Veillard52b48c72003-04-13 19:53:42 +0000133#define IS_NULLABLE (1 << 0)
134#define IS_NOT_NULLABLE (1 << 1)
135#define IS_INDETERMINIST (1 << 2)
136#define IS_MIXED (1 << 3)
137#define IS_TRIABLE (1 << 4)
138#define IS_PROCESSED (1 << 5)
139#define IS_COMPILABLE (1 << 6)
140#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000141
Daniel Veillard6eadf632003-01-23 18:29:16 +0000142struct _xmlRelaxNGDefine {
143 xmlRelaxNGType type; /* the type of definition */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000144 xmlNodePtr node; /* the node in the source */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000145 xmlChar *name; /* the element local name if present */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000146 xmlChar *ns; /* the namespace local name if present */
Daniel Veillardedc91922003-01-26 00:52:04 +0000147 xmlChar *value; /* value when available */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000148 void *data; /* data lib or specific pointer */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000149 xmlRelaxNGDefinePtr content;/* the expected content */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000150 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000151 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000152 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
Daniel Veillard3b2e4e12003-02-03 08:52:58 +0000153 xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000154 xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000155 short depth; /* used for the cycle detection */
Daniel Veillarde063f482003-03-21 16:53:17 +0000156 short dflags; /* define related flags */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000157 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000158};
159
160/**
161 * _xmlRelaxNG:
162 *
163 * A RelaxNGs definition
164 */
165struct _xmlRelaxNG {
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000166 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000167 xmlRelaxNGGrammarPtr topgrammar;
168 xmlDocPtr doc;
169
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000170 int idref; /* requires idref checking */
171
Daniel Veillard6eadf632003-01-23 18:29:16 +0000172 xmlHashTablePtr defs; /* define */
173 xmlHashTablePtr refs; /* references */
Daniel Veillardc482e262003-02-26 14:48:48 +0000174 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
175 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard419a7682003-02-03 23:22:49 +0000176 int defNr; /* number of defines used */
177 xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000178
Daniel Veillard6eadf632003-01-23 18:29:16 +0000179};
180
Daniel Veillard77648bb2003-02-20 15:03:22 +0000181#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
182#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
183#define XML_RELAXNG_IN_LIST (1 << 2)
184#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
185#define XML_RELAXNG_IN_START (1 << 4)
186#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
187#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
188#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000189#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
190#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000191
192struct _xmlRelaxNGParserCtxt {
193 void *userData; /* user specific data block */
194 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
195 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000196 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000197
198 xmlRelaxNGPtr schema; /* The schema in use */
199 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
Daniel Veillard419a7682003-02-03 23:22:49 +0000200 xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000201 int flags; /* parser flags */
202 int nbErrors; /* number of errors at parse time */
203 int nbWarnings; /* number of warnings at parse time */
Daniel Veillard276be4a2003-01-24 01:03:34 +0000204 const xmlChar *define; /* the current define scope */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000205 xmlRelaxNGDefinePtr def; /* the current define */
206
207 int nbInterleaves;
208 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000209
Daniel Veillardc482e262003-02-26 14:48:48 +0000210 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
211 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000212 xmlChar *URL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000213 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000214
Daniel Veillard419a7682003-02-03 23:22:49 +0000215 int defNr; /* number of defines used */
216 int defMax; /* number of defines aloocated */
217 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
218
Daniel Veillard6eadf632003-01-23 18:29:16 +0000219 const char *buffer;
220 int size;
221
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000222 /* the document stack */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000223 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000224 int docNr; /* Depth of the parsing stack */
225 int docMax; /* Max depth of the parsing stack */
226 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000227
228 /* the include stack */
Daniel Veillardd4310742003-02-18 21:12:46 +0000229 xmlRelaxNGIncludePtr inc; /* Current parsed include */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000230 int incNr; /* Depth of the include parsing stack */
231 int incMax; /* Max depth of the parsing stack */
232 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000233
234 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000235
236 /* used to compile content models */
237 xmlAutomataPtr am; /* the automata */
238 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000239};
240
241#define FLAGS_IGNORABLE 1
242#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000243#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000244
245/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000246 * xmlRelaxNGInterleaveGroup:
247 *
248 * A RelaxNGs partition set associated to lists of definitions
249 */
250typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
251typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
252struct _xmlRelaxNGInterleaveGroup {
253 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
254 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000255 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000256};
257
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000258#define IS_DETERMINIST 1
259#define IS_NEEDCHECK 2
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000260/**
261 * xmlRelaxNGPartitions:
262 *
263 * A RelaxNGs partition associated to an interleave group
264 */
265typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
266typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
267struct _xmlRelaxNGPartition {
268 int nbgroups; /* number of groups in the partitions */
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000269 xmlHashTablePtr triage; /* hash table used to direct nodes to the
270 right group when possible */
271 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000272 xmlRelaxNGInterleaveGroupPtr *groups;
273};
274
275/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000276 * xmlRelaxNGValidState:
277 *
278 * A RelaxNGs validation state
279 */
280#define MAX_ATTR 20
281typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
282typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
283struct _xmlRelaxNGValidState {
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000284 xmlNodePtr node; /* the current node */
285 xmlNodePtr seq; /* the sequence of children left to validate */
286 int nbAttrs; /* the number of attributes */
Daniel Veillard798024a2003-03-19 10:36:09 +0000287 int maxAttrs; /* the size of attrs */
Daniel Veillard1ed7f362003-02-03 10:57:45 +0000288 int nbAttrLeft; /* the number of attributes left to validate */
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000289 xmlChar *value; /* the value when operating on string */
290 xmlChar *endvalue; /* the end value when operating on string */
Daniel Veillard798024a2003-03-19 10:36:09 +0000291 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000292};
293
294/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000295 * xmlRelaxNGStates:
296 *
297 * A RelaxNGs container for validation state
298 */
299typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
300typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
301struct _xmlRelaxNGStates {
302 int nbState; /* the number of states */
303 int maxState; /* the size of the array */
304 xmlRelaxNGValidStatePtr *tabState;
305};
306
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000307#define ERROR_IS_DUP 1
Daniel Veillardfd573f12003-03-16 17:52:32 +0000308/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000309 * xmlRelaxNGValidError:
310 *
311 * A RelaxNGs validation error
312 */
313typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
314typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
315struct _xmlRelaxNGValidError {
316 xmlRelaxNGValidErr err; /* the error number */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000317 int flags; /* flags */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000318 xmlNodePtr node; /* the current node */
319 xmlNodePtr seq; /* the current child */
320 const xmlChar * arg1; /* first arg */
321 const xmlChar * arg2; /* second arg */
322};
323
324/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000325 * xmlRelaxNGValidCtxt:
326 *
327 * A RelaxNGs validation context
328 */
329
330struct _xmlRelaxNGValidCtxt {
331 void *userData; /* user specific data block */
332 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
333 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
334
335 xmlRelaxNGPtr schema; /* The schema in use */
336 xmlDocPtr doc; /* the document being validated */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000337 int flags; /* validation flags */
Daniel Veillard231d7912003-02-09 14:22:17 +0000338 int depth; /* validation depth */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000339 int idref; /* requires idref checking */
Daniel Veillarda507fbf2003-03-31 16:09:37 +0000340 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000341
342 /*
343 * Errors accumulated in branches may have to be stacked to be
344 * provided back when it's sure they affect validation.
345 */
346 xmlRelaxNGValidErrorPtr err; /* Last error */
347 int errNr; /* Depth of the error stack */
348 int errMax; /* Max depth of the error stack */
349 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000350
Daniel Veillardfd573f12003-03-16 17:52:32 +0000351 xmlRelaxNGValidStatePtr state; /* the current validation state */
352 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000353
354 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
355 int freeStatesNr;
356 int freeStatesMax;
357 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000358};
359
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000360/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000361 * xmlRelaxNGInclude:
362 *
363 * Structure associated to a RelaxNGs document element
364 */
365struct _xmlRelaxNGInclude {
Daniel Veillardc482e262003-02-26 14:48:48 +0000366 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000367 xmlChar *href; /* the normalized href value */
368 xmlDocPtr doc; /* the associated XML document */
369 xmlRelaxNGDefinePtr content;/* the definitions */
370 xmlRelaxNGPtr schema; /* the schema */
371};
372
373/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000374 * xmlRelaxNGDocument:
375 *
376 * Structure associated to a RelaxNGs document element
377 */
378struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000379 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000380 xmlChar *href; /* the normalized href value */
381 xmlDocPtr doc; /* the associated XML document */
382 xmlRelaxNGDefinePtr content;/* the definitions */
383 xmlRelaxNGPtr schema; /* the schema */
384};
385
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000386
Daniel Veillard6eadf632003-01-23 18:29:16 +0000387/************************************************************************
388 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000389 * Preliminary type checking interfaces *
390 * *
391 ************************************************************************/
392/**
393 * xmlRelaxNGTypeHave:
394 * @data: data needed for the library
395 * @type: the type name
396 * @value: the value to check
397 *
398 * Function provided by a type library to check if a type is exported
399 *
400 * Returns 1 if yes, 0 if no and -1 in case of error.
401 */
402typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type);
403
404/**
405 * xmlRelaxNGTypeCheck:
406 * @data: data needed for the library
407 * @type: the type name
408 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000409 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000410 *
411 * Function provided by a type library to check if a value match a type
412 *
413 * Returns 1 if yes, 0 if no and -1 in case of error.
414 */
415typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type,
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000416 const xmlChar *value, void **result,
417 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000418
419/**
420 * xmlRelaxNGFacetCheck:
421 * @data: data needed for the library
422 * @type: the type name
423 * @facet: the facet name
424 * @val: the facet value
425 * @strval: the string value
426 * @value: the value to check
427 *
428 * Function provided by a type library to check a value facet
429 *
430 * Returns 1 if yes, 0 if no and -1 in case of error.
431 */
432typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type,
433 const xmlChar *facet, const xmlChar *val,
434 const xmlChar *strval, void *value);
435
436/**
437 * xmlRelaxNGTypeFree:
438 * @data: data needed for the library
439 * @result: the value to free
440 *
441 * Function provided by a type library to free a returned result
442 */
443typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000444
445/**
446 * xmlRelaxNGTypeCompare:
447 * @data: data needed for the library
448 * @type: the type name
449 * @value1: the first value
450 * @value2: the second value
451 *
452 * Function provided by a type library to compare two values accordingly
453 * to a type.
454 *
455 * Returns 1 if yes, 0 if no and -1 in case of error.
456 */
457typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type,
458 const xmlChar *value1,
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000459 xmlNodePtr ctxt1,
460 void *comp1,
461 const xmlChar *value2,
462 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000463typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
464typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
465struct _xmlRelaxNGTypeLibrary {
466 const xmlChar *namespace; /* the datatypeLibrary value */
467 void *data; /* data needed for the library */
468 xmlRelaxNGTypeHave have; /* the export function */
469 xmlRelaxNGTypeCheck check; /* the checking function */
470 xmlRelaxNGTypeCompare comp; /* the compare function */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000471 xmlRelaxNGFacetCheck facet; /* the facet check function */
472 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000473};
474
475/************************************************************************
476 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000477 * Allocation functions *
478 * *
479 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000480static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
481static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillardd2298792003-02-14 16:54:11 +0000482static void xmlRelaxNGNormExtSpace(xmlChar *value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000483static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000484static int xmlRelaxNGEqualValidState(
485 xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
486 xmlRelaxNGValidStatePtr state1,
487 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000488static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
489 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000490
491/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000492 * xmlRelaxNGFreeDocument:
493 * @docu: a document structure
494 *
495 * Deallocate a RelaxNG document structure.
496 */
497static void
498xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
499{
500 if (docu == NULL)
501 return;
502
503 if (docu->href != NULL)
504 xmlFree(docu->href);
505 if (docu->doc != NULL)
506 xmlFreeDoc(docu->doc);
507 if (docu->schema != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000508 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000509 xmlFree(docu);
510}
511
512/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000513 * xmlRelaxNGFreeDocumentList:
514 * @docu: a list of document structure
515 *
516 * Deallocate a RelaxNG document structures.
517 */
518static void
519xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
520{
521 xmlRelaxNGDocumentPtr next;
522 while (docu != NULL) {
523 next = docu->next;
524 xmlRelaxNGFreeDocument(docu);
525 docu = next;
526 }
527}
528
529/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000530 * xmlRelaxNGFreeInclude:
531 * @incl: a include structure
532 *
533 * Deallocate a RelaxNG include structure.
534 */
535static void
536xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
537{
538 if (incl == NULL)
539 return;
540
541 if (incl->href != NULL)
542 xmlFree(incl->href);
543 if (incl->doc != NULL)
544 xmlFreeDoc(incl->doc);
545 if (incl->schema != NULL)
546 xmlRelaxNGFree(incl->schema);
547 xmlFree(incl);
548}
549
550/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000551 * xmlRelaxNGFreeIncludeList:
552 * @incl: a include structure list
553 *
554 * Deallocate a RelaxNG include structure.
555 */
556static void
557xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
558{
559 xmlRelaxNGIncludePtr next;
560 while (incl != NULL) {
561 next = incl->next;
562 xmlRelaxNGFreeInclude(incl);
563 incl = next;
564 }
565}
566
567/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000568 * xmlRelaxNGNewRelaxNG:
569 * @ctxt: a Relax-NG validation context (optional)
570 *
571 * Allocate a new RelaxNG structure.
572 *
573 * Returns the newly allocated structure or NULL in case or error
574 */
575static xmlRelaxNGPtr
576xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
577{
578 xmlRelaxNGPtr ret;
579
580 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
581 if (ret == NULL) {
582 if ((ctxt != NULL) && (ctxt->error != NULL))
583 ctxt->error(ctxt->userData, "Out of memory\n");
584 ctxt->nbErrors++;
585 return (NULL);
586 }
587 memset(ret, 0, sizeof(xmlRelaxNG));
588
589 return (ret);
590}
591
592/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000593 * xmlRelaxNGFreeInnerSchema:
594 * @schema: a schema structure
595 *
596 * Deallocate a RelaxNG schema structure.
597 */
598static void
599xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
600{
601 if (schema == NULL)
602 return;
603
604 if (schema->doc != NULL)
605 xmlFreeDoc(schema->doc);
606 if (schema->defTab != NULL) {
607 int i;
608
609 for (i = 0;i < schema->defNr;i++)
610 xmlRelaxNGFreeDefine(schema->defTab[i]);
611 xmlFree(schema->defTab);
612 }
613
614 xmlFree(schema);
615}
616
617/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000618 * xmlRelaxNGFree:
619 * @schema: a schema structure
620 *
621 * Deallocate a RelaxNG structure.
622 */
623void
624xmlRelaxNGFree(xmlRelaxNGPtr schema)
625{
626 if (schema == NULL)
627 return;
628
Daniel Veillard6eadf632003-01-23 18:29:16 +0000629 if (schema->topgrammar != NULL)
630 xmlRelaxNGFreeGrammar(schema->topgrammar);
631 if (schema->doc != NULL)
632 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000633 if (schema->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000634 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000635 if (schema->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000636 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000637 if (schema->defTab != NULL) {
638 int i;
639
640 for (i = 0;i < schema->defNr;i++)
641 xmlRelaxNGFreeDefine(schema->defTab[i]);
642 xmlFree(schema->defTab);
643 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000644
645 xmlFree(schema);
646}
647
648/**
649 * xmlRelaxNGNewGrammar:
650 * @ctxt: a Relax-NG validation context (optional)
651 *
652 * Allocate a new RelaxNG grammar.
653 *
654 * Returns the newly allocated structure or NULL in case or error
655 */
656static xmlRelaxNGGrammarPtr
657xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
658{
659 xmlRelaxNGGrammarPtr ret;
660
661 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
662 if (ret == NULL) {
663 if ((ctxt != NULL) && (ctxt->error != NULL))
664 ctxt->error(ctxt->userData, "Out of memory\n");
665 ctxt->nbErrors++;
666 return (NULL);
667 }
668 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
669
670 return (ret);
671}
672
673/**
674 * xmlRelaxNGFreeGrammar:
675 * @grammar: a grammar structure
676 *
677 * Deallocate a RelaxNG grammar structure.
678 */
679static void
680xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
681{
682 if (grammar == NULL)
683 return;
684
Daniel Veillardc482e262003-02-26 14:48:48 +0000685 if (grammar->children != NULL) {
686 xmlRelaxNGFreeGrammar(grammar->children);
687 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000688 if (grammar->next != NULL) {
689 xmlRelaxNGFreeGrammar(grammar->next);
690 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000691 if (grammar->refs != NULL) {
692 xmlHashFree(grammar->refs, NULL);
693 }
694 if (grammar->defs != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000695 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000696 }
697
698 xmlFree(grammar);
699}
700
701/**
702 * xmlRelaxNGNewDefine:
703 * @ctxt: a Relax-NG validation context
704 * @node: the node in the input document.
705 *
706 * Allocate a new RelaxNG define.
707 *
708 * Returns the newly allocated structure or NULL in case or error
709 */
710static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000711xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000712{
713 xmlRelaxNGDefinePtr ret;
714
Daniel Veillard419a7682003-02-03 23:22:49 +0000715 if (ctxt->defMax == 0) {
716 ctxt->defMax = 16;
717 ctxt->defNr = 0;
718 ctxt->defTab = (xmlRelaxNGDefinePtr *)
719 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
720 if (ctxt->defTab == NULL) {
721 if ((ctxt != NULL) && (ctxt->error != NULL))
722 ctxt->error(ctxt->userData, "Out of memory\n");
723 ctxt->nbErrors++;
724 return (NULL);
725 }
726 } else if (ctxt->defMax <= ctxt->defNr) {
727 xmlRelaxNGDefinePtr *tmp;
728 ctxt->defMax *= 2;
729 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
730 ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
731 if (tmp == NULL) {
732 if ((ctxt != NULL) && (ctxt->error != NULL))
733 ctxt->error(ctxt->userData, "Out of memory\n");
734 ctxt->nbErrors++;
735 return (NULL);
736 }
737 ctxt->defTab = tmp;
738 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000739 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
740 if (ret == NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000741 if ((ctxt != NULL) && (ctxt->error != NULL))
742 ctxt->error(ctxt->userData, "Out of memory\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +0000743 ctxt->nbErrors++;
Daniel Veillard419a7682003-02-03 23:22:49 +0000744 return(NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000745 }
746 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000747 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000748 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000749 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000750 return (ret);
751}
752
753/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000754 * xmlRelaxNGFreePartition:
755 * @partitions: a partition set structure
756 *
757 * Deallocate RelaxNG partition set structures.
758 */
759static void
760xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) {
761 xmlRelaxNGInterleaveGroupPtr group;
762 int j;
763
764 if (partitions != NULL) {
765 if (partitions->groups != NULL) {
766 for (j = 0;j < partitions->nbgroups;j++) {
767 group = partitions->groups[j];
768 if (group != NULL) {
769 if (group->defs != NULL)
770 xmlFree(group->defs);
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000771 if (group->attrs != NULL)
772 xmlFree(group->attrs);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000773 xmlFree(group);
774 }
775 }
776 xmlFree(partitions->groups);
777 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000778 if (partitions->triage != NULL) {
779 xmlHashFree(partitions->triage, NULL);
780 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000781 xmlFree(partitions);
782 }
783}
784/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000785 * xmlRelaxNGFreeDefine:
786 * @define: a define structure
787 *
788 * Deallocate a RelaxNG define structure.
789 */
790static void
791xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
792{
793 if (define == NULL)
794 return;
795
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000796 if ((define->type == XML_RELAXNG_VALUE) &&
797 (define->attrs != NULL)) {
798 xmlRelaxNGTypeLibraryPtr lib;
799
800 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
801 if ((lib != NULL) && (lib->freef != NULL))
802 lib->freef(lib->data, (void *) define->attrs);
803 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000804 if ((define->data != NULL) &&
805 (define->type == XML_RELAXNG_INTERLEAVE))
806 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
Daniel Veillarde063f482003-03-21 16:53:17 +0000807 if ((define->data != NULL) &&
808 (define->type == XML_RELAXNG_CHOICE))
809 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000810 if (define->name != NULL)
811 xmlFree(define->name);
812 if (define->ns != NULL)
813 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000814 if (define->value != NULL)
815 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000816 if (define->contModel != NULL)
817 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000818 xmlFree(define);
819}
820
821/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000822 * xmlRelaxNGNewStates:
823 * @ctxt: a Relax-NG validation context
824 * @size: the default size for the container
825 *
826 * Allocate a new RelaxNG validation state container
827 * TODO: keep a pool in the ctxt
828 *
829 * Returns the newly allocated structure or NULL in case or error
830 */
831static xmlRelaxNGStatesPtr
832xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
833{
834 xmlRelaxNGStatesPtr ret;
835
Daniel Veillard798024a2003-03-19 10:36:09 +0000836 if ((ctxt != NULL) &&
837 (ctxt->freeState != NULL) &&
838 (ctxt->freeStatesNr > 0)) {
839 ctxt->freeStatesNr--;
840 ret = ctxt->freeStates[ctxt->freeStatesNr];
841 ret->nbState = 0;
842 return(ret);
843 }
Daniel Veillardfd573f12003-03-16 17:52:32 +0000844 if (size < 16) size = 16;
845
846 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
847 (size - 1) * sizeof(xmlRelaxNGValidStatePtr));
848 if (ret == NULL) {
849 if ((ctxt != NULL) && (ctxt->error != NULL))
850 ctxt->error(ctxt->userData, "Out of memory\n");
851 return (NULL);
852 }
853 ret->nbState = 0;
854 ret->maxState = size;
855 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc(
856 (size) * sizeof(xmlRelaxNGValidStatePtr));
857 if (ret->tabState == NULL) {
858 if ((ctxt != NULL) && (ctxt->error != NULL))
859 ctxt->error(ctxt->userData, "Out of memory\n");
860 xmlFree(ret->tabState);
861 return (NULL);
862 }
863 return(ret);
864}
865
866/**
Daniel Veillard798024a2003-03-19 10:36:09 +0000867 * xmlRelaxNGAddStateUniq:
868 * @ctxt: a Relax-NG validation context
869 * @states: the states container
870 * @state: the validation state
871 *
872 * Add a RelaxNG validation state to the container without checking
873 * for unicity.
874 *
875 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
876 */
877static int
878xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
879 xmlRelaxNGStatesPtr states,
880 xmlRelaxNGValidStatePtr state)
881{
882 if (state == NULL) {
883 return(-1);
884 }
885 if (states->nbState >= states->maxState) {
886 xmlRelaxNGValidStatePtr *tmp;
887 int size;
888
889 size = states->maxState * 2;
890 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
891 (size) * sizeof(xmlRelaxNGValidStatePtr));
892 if (tmp == NULL) {
893 if ((ctxt != NULL) && (ctxt->error != NULL))
894 ctxt->error(ctxt->userData, "Out of memory\n");
895 return(-1);
896 }
897 states->tabState = tmp;
898 states->maxState = size;
899 }
900 states->tabState[states->nbState++] = state;
901 return(1);
902}
903
904/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000905 * xmlRelaxNGAddState:
906 * @ctxt: a Relax-NG validation context
907 * @states: the states container
908 * @state: the validation state
909 *
910 * Add a RelaxNG validation state to the container
911 *
912 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
913 */
914static int
915xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states,
916 xmlRelaxNGValidStatePtr state)
917{
918 int i;
919
920 if (state == NULL) {
921 return(-1);
922 }
923 if (states->nbState >= states->maxState) {
924 xmlRelaxNGValidStatePtr *tmp;
925 int size;
926
927 size = states->maxState * 2;
928 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
929 (size) * sizeof(xmlRelaxNGValidStatePtr));
930 if (tmp == NULL) {
931 if ((ctxt != NULL) && (ctxt->error != NULL))
932 ctxt->error(ctxt->userData, "Out of memory\n");
933 return(-1);
934 }
935 states->tabState = tmp;
936 states->maxState = size;
937 }
938 for (i = 0;i < states->nbState;i++) {
939 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
Daniel Veillard798024a2003-03-19 10:36:09 +0000940 xmlRelaxNGFreeValidState(ctxt, state);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000941 return(0);
942 }
943 }
944 states->tabState[states->nbState++] = state;
945 return(1);
946}
947
948/**
949 * xmlRelaxNGFreeStates:
950 * @ctxt: a Relax-NG validation context
951 * @states: teh container
952 *
953 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000954 */
955static void
Daniel Veillard798024a2003-03-19 10:36:09 +0000956xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardfd573f12003-03-16 17:52:32 +0000957 xmlRelaxNGStatesPtr states)
958{
Daniel Veillard798024a2003-03-19 10:36:09 +0000959 if (states == NULL)
960 return;
Daniel Veillard798024a2003-03-19 10:36:09 +0000961 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
962 ctxt->freeStatesMax = 40;
963 ctxt->freeStatesNr = 0;
964 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
965 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
966 if (ctxt->freeStates == NULL) {
967 if ((ctxt != NULL) && (ctxt->error != NULL))
968 ctxt->error(ctxt->userData, "Out of memory\n");
969 }
970 } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
971 xmlRelaxNGStatesPtr *tmp;
972
973 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
974 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
975 if (tmp == NULL) {
976 if ((ctxt != NULL) && (ctxt->error != NULL))
977 ctxt->error(ctxt->userData, "Out of memory\n");
978 xmlFree(states->tabState);
979 xmlFree(states);
980 return;
981 }
982 ctxt->freeStates = tmp;
983 ctxt->freeStatesMax *= 2;
984 }
985 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +0000986 xmlFree(states->tabState);
987 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +0000988 } else {
989 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +0000990 }
991}
992
993/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000994 * xmlRelaxNGNewValidState:
995 * @ctxt: a Relax-NG validation context
996 * @node: the current node or NULL for the document
997 *
998 * Allocate a new RelaxNG validation state
999 *
1000 * Returns the newly allocated structure or NULL in case or error
1001 */
1002static xmlRelaxNGValidStatePtr
1003xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1004{
1005 xmlRelaxNGValidStatePtr ret;
1006 xmlAttrPtr attr;
1007 xmlAttrPtr attrs[MAX_ATTR];
1008 int nbAttrs = 0;
1009 xmlNodePtr root = NULL;
1010
1011 if (node == NULL) {
1012 root = xmlDocGetRootElement(ctxt->doc);
1013 if (root == NULL)
1014 return(NULL);
1015 } else {
1016 attr = node->properties;
1017 while (attr != NULL) {
1018 if (nbAttrs < MAX_ATTR)
1019 attrs[nbAttrs++] = attr;
1020 else
1021 nbAttrs++;
1022 attr = attr->next;
1023 }
1024 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001025 if ((ctxt->freeState != NULL) &&
1026 (ctxt->freeState->nbState > 0)) {
1027 ctxt->freeState->nbState--;
1028 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1029 } else {
1030 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1031 if (ret == NULL) {
1032 if ((ctxt != NULL) && (ctxt->error != NULL))
1033 ctxt->error(ctxt->userData, "Out of memory\n");
1034 return (NULL);
1035 }
1036 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001037 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001038 ret->value = NULL;
1039 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001040 if (node == NULL) {
1041 ret->node = (xmlNodePtr) ctxt->doc;
1042 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001043 } else {
1044 ret->node = node;
1045 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001046 }
1047 ret->nbAttrs = 0;
1048 if (nbAttrs > 0) {
1049 if (ret->attrs == NULL) {
1050 if (nbAttrs < 4) ret->maxAttrs = 4;
1051 else ret->maxAttrs = nbAttrs;
1052 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1053 sizeof(xmlAttrPtr));
1054 if (ret->attrs == NULL) {
1055 if ((ctxt != NULL) && (ctxt->error != NULL))
1056 ctxt->error(ctxt->userData, "Out of memory\n");
1057 return (ret);
1058 }
1059 } else if (ret->maxAttrs < nbAttrs) {
1060 xmlAttrPtr *tmp;
1061
1062 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1063 sizeof(xmlAttrPtr));
1064 if (tmp == NULL) {
1065 if ((ctxt != NULL) && (ctxt->error != NULL))
1066 ctxt->error(ctxt->userData, "Out of memory\n");
1067 return (ret);
1068 }
1069 ret->attrs = tmp;
1070 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001071 ret->nbAttrs = nbAttrs;
Daniel Veillard798024a2003-03-19 10:36:09 +00001072 if (nbAttrs < MAX_ATTR) {
1073 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1074 } else {
1075 attr = node->properties;
1076 nbAttrs = 0;
1077 while (attr != NULL) {
1078 ret->attrs[nbAttrs++] = attr;
1079 attr = attr->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001080 }
1081 }
1082 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001083 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001084 return (ret);
1085}
1086
1087/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001088 * xmlRelaxNGCopyValidState:
1089 * @ctxt: a Relax-NG validation context
1090 * @state: a validation state
1091 *
1092 * Copy the validation state
1093 *
1094 * Returns the newly allocated structure or NULL in case or error
1095 */
1096static xmlRelaxNGValidStatePtr
1097xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1098 xmlRelaxNGValidStatePtr state)
1099{
1100 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001101 unsigned int maxAttrs;
1102 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001103
1104 if (state == NULL)
1105 return(NULL);
Daniel Veillard798024a2003-03-19 10:36:09 +00001106 if ((ctxt->freeState != NULL) &&
1107 (ctxt->freeState->nbState > 0)) {
1108 ctxt->freeState->nbState--;
1109 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1110 } else {
1111 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1112 if (ret == NULL) {
1113 if ((ctxt != NULL) && (ctxt->error != NULL))
1114 ctxt->error(ctxt->userData, "Out of memory\n");
1115 return (NULL);
1116 }
1117 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001118 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001119 attrs = ret->attrs;
1120 maxAttrs = ret->maxAttrs;
1121 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1122 ret->attrs = attrs;
1123 ret->maxAttrs = maxAttrs;
1124 if (state->nbAttrs > 0) {
1125 if (ret->attrs == NULL) {
1126 ret->maxAttrs = state->maxAttrs;
1127 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1128 sizeof(xmlAttrPtr));
1129 if (ret->attrs == NULL) {
1130 if ((ctxt != NULL) && (ctxt->error != NULL))
1131 ctxt->error(ctxt->userData, "Out of memory\n");
1132 ret->nbAttrs = 0;
1133 return (ret);
1134 }
1135 } else if (ret->maxAttrs < state->nbAttrs) {
1136 xmlAttrPtr *tmp;
1137
1138 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1139 sizeof(xmlAttrPtr));
1140 if (tmp == NULL) {
1141 if ((ctxt != NULL) && (ctxt->error != NULL))
1142 ctxt->error(ctxt->userData, "Out of memory\n");
1143 ret->nbAttrs = 0;
1144 return (ret);
1145 }
1146 ret->maxAttrs = state->maxAttrs;
1147 }
1148 memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr));
1149 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001150 return(ret);
1151}
1152
1153/**
1154 * xmlRelaxNGEqualValidState:
1155 * @ctxt: a Relax-NG validation context
1156 * @state1: a validation state
1157 * @state2: a validation state
1158 *
1159 * Compare the validation states for equality
1160 *
1161 * Returns 1 if equald, 0 otherwise
1162 */
1163static int
1164xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1165 xmlRelaxNGValidStatePtr state1,
1166 xmlRelaxNGValidStatePtr state2)
1167{
1168 int i;
1169
1170 if ((state1 == NULL) || (state2 == NULL))
1171 return(0);
1172 if (state1 == state2)
1173 return(1);
1174 if (state1->node != state2->node)
1175 return(0);
1176 if (state1->seq != state2->seq)
1177 return(0);
1178 if (state1->nbAttrLeft != state2->nbAttrLeft)
1179 return(0);
1180 if (state1->nbAttrs != state2->nbAttrs)
1181 return(0);
1182 if (state1->endvalue != state2->endvalue)
1183 return(0);
1184 if ((state1->value != state2->value) &&
1185 (!xmlStrEqual(state1->value, state2->value)))
1186 return(0);
1187 for (i = 0;i < state1->nbAttrs;i++) {
1188 if (state1->attrs[i] != state2->attrs[i])
1189 return(0);
1190 }
1191 return(1);
1192}
1193
1194/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001195 * xmlRelaxNGFreeValidState:
1196 * @state: a validation state structure
1197 *
1198 * Deallocate a RelaxNG validation state structure.
1199 */
1200static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001201xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1202 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001203{
1204 if (state == NULL)
1205 return;
1206
Daniel Veillard798024a2003-03-19 10:36:09 +00001207 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1208 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1209 }
1210 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1211 if (state->attrs != NULL)
1212 xmlFree(state->attrs);
1213 xmlFree(state);
1214 } else {
1215 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1216 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001217}
1218
1219/************************************************************************
1220 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001221 * Document functions *
1222 * *
1223 ************************************************************************/
1224static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1225 xmlDocPtr doc);
1226
1227/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001228 * xmlRelaxNGIncludePush:
1229 * @ctxt: the parser context
1230 * @value: the element doc
1231 *
1232 * Pushes a new include on top of the include stack
1233 *
1234 * Returns 0 in case of error, the index in the stack otherwise
1235 */
1236static int
1237xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1238 xmlRelaxNGIncludePtr value)
1239{
1240 if (ctxt->incTab == NULL) {
1241 ctxt->incMax = 4;
1242 ctxt->incNr = 0;
1243 ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc(
1244 ctxt->incMax * sizeof(ctxt->incTab[0]));
1245 if (ctxt->incTab == NULL) {
1246 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1247 return (0);
1248 }
1249 }
1250 if (ctxt->incNr >= ctxt->incMax) {
1251 ctxt->incMax *= 2;
1252 ctxt->incTab =
1253 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1254 ctxt->incMax *
1255 sizeof(ctxt->incTab[0]));
1256 if (ctxt->incTab == NULL) {
1257 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1258 return (0);
1259 }
1260 }
1261 ctxt->incTab[ctxt->incNr] = value;
1262 ctxt->inc = value;
1263 return (ctxt->incNr++);
1264}
1265
1266/**
1267 * xmlRelaxNGIncludePop:
1268 * @ctxt: the parser context
1269 *
1270 * Pops the top include from the include stack
1271 *
1272 * Returns the include just removed
1273 */
1274static xmlRelaxNGIncludePtr
1275xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1276{
1277 xmlRelaxNGIncludePtr ret;
1278
1279 if (ctxt->incNr <= 0)
1280 return (0);
1281 ctxt->incNr--;
1282 if (ctxt->incNr > 0)
1283 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1284 else
1285 ctxt->inc = NULL;
1286 ret = ctxt->incTab[ctxt->incNr];
1287 ctxt->incTab[ctxt->incNr] = 0;
1288 return (ret);
1289}
1290
1291/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001292 * xmlRelaxNGRemoveRedefine:
1293 * @ctxt: the parser context
1294 * @URL: the normalized URL
1295 * @target: the included target
1296 * @name: the define name to eliminate
1297 *
1298 * Applies the elimination algorithm of 4.7
1299 *
1300 * Returns 0 in case of error, 1 in case of success.
1301 */
1302static int
1303xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1304 const xmlChar *URL ATTRIBUTE_UNUSED,
1305 xmlNodePtr target, const xmlChar *name) {
1306 int found = 0;
1307 xmlNodePtr tmp, tmp2;
1308 xmlChar *name2;
1309
1310#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001311 if (name == NULL)
1312 xmlGenericError(xmlGenericErrorContext,
1313 "Elimination of <include> start from %s\n", URL);
1314 else
1315 xmlGenericError(xmlGenericErrorContext,
1316 "Elimination of <include> define %s from %s\n", name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001317#endif
1318 tmp = target;
1319 while (tmp != NULL) {
1320 tmp2 = tmp->next;
1321 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1322 found = 1;
1323 xmlUnlinkNode(tmp);
1324 xmlFreeNode(tmp);
1325 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1326 name2 = xmlGetProp(tmp, BAD_CAST "name");
1327 xmlRelaxNGNormExtSpace(name2);
1328 if (name2 != NULL) {
1329 if (xmlStrEqual(name, name2)) {
1330 found = 1;
1331 xmlUnlinkNode(tmp);
1332 xmlFreeNode(tmp);
1333 }
1334 xmlFree(name2);
1335 }
1336 } else if (IS_RELAXNG(tmp, "include")) {
1337 xmlChar *href = NULL;
1338 xmlRelaxNGDocumentPtr inc = tmp->_private;
1339
1340 if ((inc != NULL) && (inc->doc != NULL) &&
1341 (inc->doc->children != NULL)) {
1342
1343 if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) {
1344#ifdef DEBUG_INCLUDE
1345 href = xmlGetProp(tmp, BAD_CAST "href");
1346#endif
1347 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1348 inc->doc->children->children, name) == 1) {
1349 found = 1;
1350 }
1351 if (href != NULL)
1352 xmlFree(href);
1353 }
1354 }
1355 }
1356 tmp = tmp2;
1357 }
1358 return(found);
1359}
1360
1361/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001362 * xmlRelaxNGLoadInclude:
1363 * @ctxt: the parser context
1364 * @URL: the normalized URL
1365 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001366 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001367 *
1368 * First lookup if the document is already loaded into the parser context,
1369 * check against recursion. If not found the resource is loaded and
1370 * the content is preprocessed before being returned back to the caller.
1371 *
1372 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1373 */
1374static xmlRelaxNGIncludePtr
1375xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillard416589a2003-02-17 17:25:42 +00001376 xmlNodePtr node, const xmlChar *ns) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001377 xmlRelaxNGIncludePtr ret = NULL;
1378 xmlDocPtr doc;
1379 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001380 xmlNodePtr root, cur;
1381
1382#ifdef DEBUG_INCLUDE
1383 xmlGenericError(xmlGenericErrorContext,
1384 "xmlRelaxNGLoadInclude(%s)\n", URL);
1385#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001386
1387 /*
1388 * check against recursion in the stack
1389 */
1390 for (i = 0;i < ctxt->incNr;i++) {
1391 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1392 if (ctxt->error != NULL)
1393 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00001394 "Detected an Include recursion for %s\n",
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001395 URL);
1396 ctxt->nbErrors++;
1397 return(NULL);
1398 }
1399 }
1400
1401 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001402 * load the document
1403 */
1404 doc = xmlParseFile((const char *) URL);
1405 if (doc == NULL) {
1406 if (ctxt->error != NULL)
1407 ctxt->error(ctxt->userData,
1408 "xmlRelaxNG: could not load %s\n", URL);
1409 ctxt->nbErrors++;
1410 return (NULL);
1411 }
1412
Daniel Veillard5add8682003-03-10 13:13:58 +00001413#ifdef DEBUG_INCLUDE
1414 xmlGenericError(xmlGenericErrorContext,
1415 "Parsed %s Okay\n", URL);
1416#endif
1417
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001418 /*
1419 * Allocate the document structures and register it first.
1420 */
1421 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1422 if (ret == NULL) {
1423 if (ctxt->error != NULL)
1424 ctxt->error(ctxt->userData,
1425 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1426 ctxt->nbErrors++;
1427 xmlFreeDoc(doc);
1428 return (NULL);
1429 }
1430 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1431 ret->doc = doc;
1432 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001433 ret->next = ctxt->includes;
1434 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001435
1436 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001437 * transmit the ns if needed
1438 */
1439 if (ns != NULL) {
1440 root = xmlDocGetRootElement(doc);
1441 if (root != NULL) {
1442 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1443 xmlSetProp(root, BAD_CAST"ns", ns);
1444 }
1445 }
1446 }
1447
1448 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001449 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001450 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001451 xmlRelaxNGIncludePush(ctxt, ret);
1452
1453 /*
1454 * Some preprocessing of the document content, this include recursing
1455 * in the include stack.
1456 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001457#ifdef DEBUG_INCLUDE
1458 xmlGenericError(xmlGenericErrorContext,
1459 "cleanup of %s\n", URL);
1460#endif
1461
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001462 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1463 if (doc == NULL) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001464 ctxt->inc = NULL;
1465 return(NULL);
1466 }
1467
1468 /*
1469 * Pop up the include from the stack
1470 */
1471 xmlRelaxNGIncludePop(ctxt);
1472
Daniel Veillard5add8682003-03-10 13:13:58 +00001473#ifdef DEBUG_INCLUDE
1474 xmlGenericError(xmlGenericErrorContext,
1475 "Checking of %s\n", URL);
1476#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001477 /*
1478 * Check that the top element is a grammar
1479 */
1480 root = xmlDocGetRootElement(doc);
1481 if (root == NULL) {
1482 if (ctxt->error != NULL)
1483 ctxt->error(ctxt->userData,
1484 "xmlRelaxNG: included document is empty %s\n", URL);
1485 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001486 return (NULL);
1487 }
1488 if (!IS_RELAXNG(root, "grammar")) {
1489 if (ctxt->error != NULL)
1490 ctxt->error(ctxt->userData,
1491 "xmlRelaxNG: included document %s root is not a grammar\n",
1492 URL);
1493 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001494 return (NULL);
1495 }
1496
1497 /*
1498 * Elimination of redefined rules in the include.
1499 */
1500 cur = node->children;
1501 while (cur != NULL) {
1502 if (IS_RELAXNG(cur, "start")) {
1503 int found = 0;
1504
Daniel Veillard5add8682003-03-10 13:13:58 +00001505 found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001506 if (!found) {
1507 if (ctxt->error != NULL)
1508 ctxt->error(ctxt->userData,
1509 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1510 URL);
1511 ctxt->nbErrors++;
1512 }
1513 } else if (IS_RELAXNG(cur, "define")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001514 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001515
1516 name = xmlGetProp(cur, BAD_CAST "name");
1517 if (name == NULL) {
1518 if (ctxt->error != NULL)
1519 ctxt->error(ctxt->userData,
1520 "xmlRelaxNG: include %s has define without name\n",
1521 URL);
1522 ctxt->nbErrors++;
1523 } else {
Daniel Veillard5add8682003-03-10 13:13:58 +00001524 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001525
Daniel Veillardd2298792003-02-14 16:54:11 +00001526 xmlRelaxNGNormExtSpace(name);
Daniel Veillard5add8682003-03-10 13:13:58 +00001527 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1528 root->children, name);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001529 if (!found) {
1530 if (ctxt->error != NULL)
1531 ctxt->error(ctxt->userData,
1532 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1533 URL, name);
1534 ctxt->nbErrors++;
1535 }
1536 xmlFree(name);
1537 }
1538 }
1539 cur = cur->next;
1540 }
1541
1542
1543 return(ret);
1544}
1545
1546/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001547 * xmlRelaxNGValidErrorPush:
1548 * @ctxt: the validation context
1549 * @err: the error code
1550 * @arg1: the first string argument
1551 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001552 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001553 *
1554 * Pushes a new error on top of the error stack
1555 *
1556 * Returns 0 in case of error, the index in the stack otherwise
1557 */
1558static int
1559xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001560 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001561{
1562 xmlRelaxNGValidErrorPtr cur;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001563#ifdef DEBUG_ERROR
1564 xmlGenericError(xmlGenericErrorContext,
1565 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
1566#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001567 if (ctxt->errTab == NULL) {
1568 ctxt->errMax = 8;
1569 ctxt->errNr = 0;
1570 ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc(
1571 ctxt->errMax * sizeof(xmlRelaxNGValidError));
1572 if (ctxt->errTab == NULL) {
1573 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1574 return (0);
1575 }
Daniel Veillard20863822003-03-22 17:51:47 +00001576 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001577 }
1578 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard20863822003-03-22 17:51:47 +00001579 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001580 ctxt->errTab =
1581 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard20863822003-03-22 17:51:47 +00001582 ctxt->errMax * sizeof(xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001583 if (ctxt->errTab == NULL) {
1584 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1585 return (0);
1586 }
Daniel Veillard20863822003-03-22 17:51:47 +00001587 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001588 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001589 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00001590 (ctxt->err->node == ctxt->state->node) &&
1591 (ctxt->err->err == err))
1592 return(ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001593 cur = &ctxt->errTab[ctxt->errNr];
1594 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001595 if (dup) {
1596 cur->arg1 = xmlStrdup(arg1);
1597 cur->arg2 = xmlStrdup(arg2);
1598 cur->flags = ERROR_IS_DUP;
1599 } else {
1600 cur->arg1 = arg1;
1601 cur->arg2 = arg2;
1602 cur->flags = 0;
1603 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001604 if (ctxt->state != NULL) {
1605 cur->node = ctxt->state->node;
1606 cur->seq = ctxt->state->seq;
1607 } else {
1608 cur->node = NULL;
1609 cur->seq = NULL;
1610 }
1611 ctxt->err = cur;
1612 return (ctxt->errNr++);
1613}
1614
1615/**
1616 * xmlRelaxNGValidErrorPop:
1617 * @ctxt: the validation context
1618 *
1619 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001620 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001621static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001622xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1623{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001624 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001625
Daniel Veillard580ced82003-03-21 21:22:48 +00001626 if (ctxt->errNr <= 0) {
1627 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001628 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001629 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001630 ctxt->errNr--;
1631 if (ctxt->errNr > 0)
1632 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1633 else
1634 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001635 cur = &ctxt->errTab[ctxt->errNr];
1636 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001637 if (cur->arg1 != NULL)
1638 xmlFree((xmlChar *)cur->arg1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001639 cur->arg1 = NULL;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001640 if (cur->arg2 != NULL)
1641 xmlFree((xmlChar *)cur->arg2);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001642 cur->arg2 = NULL;
1643 cur->flags = 0;
1644 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001645}
1646
Daniel Veillard42f12e92003-03-07 18:32:59 +00001647/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001648 * xmlRelaxNGDocumentPush:
1649 * @ctxt: the parser context
1650 * @value: the element doc
1651 *
1652 * Pushes a new doc on top of the doc stack
1653 *
1654 * Returns 0 in case of error, the index in the stack otherwise
1655 */
1656static int
1657xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1658 xmlRelaxNGDocumentPtr value)
1659{
1660 if (ctxt->docTab == NULL) {
1661 ctxt->docMax = 4;
1662 ctxt->docNr = 0;
1663 ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc(
1664 ctxt->docMax * sizeof(ctxt->docTab[0]));
1665 if (ctxt->docTab == NULL) {
1666 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1667 return (0);
1668 }
1669 }
1670 if (ctxt->docNr >= ctxt->docMax) {
1671 ctxt->docMax *= 2;
1672 ctxt->docTab =
1673 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1674 ctxt->docMax *
1675 sizeof(ctxt->docTab[0]));
1676 if (ctxt->docTab == NULL) {
1677 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1678 return (0);
1679 }
1680 }
1681 ctxt->docTab[ctxt->docNr] = value;
1682 ctxt->doc = value;
1683 return (ctxt->docNr++);
1684}
1685
1686/**
1687 * xmlRelaxNGDocumentPop:
1688 * @ctxt: the parser context
1689 *
1690 * Pops the top doc from the doc stack
1691 *
1692 * Returns the doc just removed
1693 */
1694static xmlRelaxNGDocumentPtr
1695xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1696{
1697 xmlRelaxNGDocumentPtr ret;
1698
1699 if (ctxt->docNr <= 0)
1700 return (0);
1701 ctxt->docNr--;
1702 if (ctxt->docNr > 0)
1703 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1704 else
1705 ctxt->doc = NULL;
1706 ret = ctxt->docTab[ctxt->docNr];
1707 ctxt->docTab[ctxt->docNr] = 0;
1708 return (ret);
1709}
1710
1711/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001712 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001713 * @ctxt: the parser context
1714 * @URL: the normalized URL
1715 * @ns: the inherited ns if any
1716 *
1717 * First lookup if the document is already loaded into the parser context,
1718 * check against recursion. If not found the resource is loaded and
1719 * the content is preprocessed before being returned back to the caller.
1720 *
1721 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1722 */
1723static xmlRelaxNGDocumentPtr
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001724xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001725 const xmlChar *ns) {
1726 xmlRelaxNGDocumentPtr ret = NULL;
1727 xmlDocPtr doc;
1728 xmlNodePtr root;
1729 int i;
1730
1731 /*
1732 * check against recursion in the stack
1733 */
1734 for (i = 0;i < ctxt->docNr;i++) {
1735 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1736 if (ctxt->error != NULL)
1737 ctxt->error(ctxt->userData,
1738 "Detected an externalRef recursion for %s\n",
1739 URL);
1740 ctxt->nbErrors++;
1741 return(NULL);
1742 }
1743 }
1744
1745 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001746 * load the document
1747 */
1748 doc = xmlParseFile((const char *) URL);
1749 if (doc == NULL) {
1750 if (ctxt->error != NULL)
1751 ctxt->error(ctxt->userData,
1752 "xmlRelaxNG: could not load %s\n", URL);
1753 ctxt->nbErrors++;
1754 return (NULL);
1755 }
1756
1757 /*
1758 * Allocate the document structures and register it first.
1759 */
1760 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1761 if (ret == NULL) {
1762 if (ctxt->error != NULL)
1763 ctxt->error(ctxt->userData,
1764 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1765 ctxt->nbErrors++;
1766 xmlFreeDoc(doc);
1767 return (NULL);
1768 }
1769 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1770 ret->doc = doc;
1771 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001772 ret->next = ctxt->documents;
1773 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001774
1775 /*
1776 * transmit the ns if needed
1777 */
1778 if (ns != NULL) {
1779 root = xmlDocGetRootElement(doc);
1780 if (root != NULL) {
1781 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1782 xmlSetProp(root, BAD_CAST"ns", ns);
1783 }
1784 }
1785 }
1786
1787 /*
1788 * push it on the stack and register it in the hash table
1789 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001790 xmlRelaxNGDocumentPush(ctxt, ret);
1791
1792 /*
1793 * Some preprocessing of the document content
1794 */
1795 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1796 if (doc == NULL) {
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001797 ctxt->doc = NULL;
1798 return(NULL);
1799 }
1800
1801 xmlRelaxNGDocumentPop(ctxt);
1802
1803 return(ret);
1804}
1805
1806/************************************************************************
1807 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001808 * Error functions *
1809 * *
1810 ************************************************************************/
1811
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001812#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1813#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
1814#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
1815#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
1816#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001817
Daniel Veillard231d7912003-02-09 14:22:17 +00001818static const char *
1819xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) {
1820 if (def == NULL)
1821 return("none");
1822 switch(def->type) {
1823 case XML_RELAXNG_EMPTY: return("empty");
1824 case XML_RELAXNG_NOT_ALLOWED: return("notAllowed");
1825 case XML_RELAXNG_EXCEPT: return("except");
1826 case XML_RELAXNG_TEXT: return("text");
1827 case XML_RELAXNG_ELEMENT: return("element");
1828 case XML_RELAXNG_DATATYPE: return("datatype");
1829 case XML_RELAXNG_VALUE: return("value");
1830 case XML_RELAXNG_LIST: return("list");
1831 case XML_RELAXNG_ATTRIBUTE: return("attribute");
1832 case XML_RELAXNG_DEF: return("def");
1833 case XML_RELAXNG_REF: return("ref");
1834 case XML_RELAXNG_EXTERNALREF: return("externalRef");
1835 case XML_RELAXNG_PARENTREF: return("parentRef");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001836 case XML_RELAXNG_OPTIONAL: return("optional");
1837 case XML_RELAXNG_ZEROORMORE: return("zeroOrMore");
Daniel Veillard231d7912003-02-09 14:22:17 +00001838 case XML_RELAXNG_ONEORMORE: return("oneOrMore");
1839 case XML_RELAXNG_CHOICE: return("choice");
1840 case XML_RELAXNG_GROUP: return("group");
1841 case XML_RELAXNG_INTERLEAVE: return("interleave");
1842 case XML_RELAXNG_START: return("start");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001843 case XML_RELAXNG_NOOP: return("noop");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001844 case XML_RELAXNG_PARAM: return("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00001845 }
1846 return("unknown");
1847}
Daniel Veillardd2298792003-02-14 16:54:11 +00001848
Daniel Veillard6eadf632003-01-23 18:29:16 +00001849/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001850 * xmlRelaxNGGetErrorString:
1851 * @err: the error code
1852 * @arg1: the first string argument
1853 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00001854 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00001855 * computes a formatted error string for the given error code and args
1856 *
1857 * Returns the error string, it must be deallocated by the caller
1858 */
1859static xmlChar *
Daniel Veillardfd573f12003-03-16 17:52:32 +00001860xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1,
1861 const xmlChar *arg2) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00001862 char msg[1000];
1863
1864 if (arg1 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001865 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001866 if (arg2 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001867 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001868
1869 msg[0] = 0;
1870 switch (err) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001871 case XML_RELAXNG_OK:
1872 return(NULL);
1873 case XML_RELAXNG_ERR_MEMORY:
1874 return(xmlCharStrdup("out of memory"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001875 case XML_RELAXNG_ERR_TYPE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00001876 snprintf(msg, 1000, "failed to validate type %s", arg1);
1877 break;
1878 case XML_RELAXNG_ERR_TYPEVAL:
Daniel Veillardc4c21552003-03-29 10:53:38 +00001879 snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001880 break;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001881 case XML_RELAXNG_ERR_DUPID:
1882 snprintf(msg, 1000, "ID %s redefined", arg1);
1883 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001884 case XML_RELAXNG_ERR_TYPECMP:
1885 snprintf(msg, 1000, "failed to compare type %s", arg1);
1886 break;
1887 case XML_RELAXNG_ERR_NOSTATE:
1888 return(xmlCharStrdup("Internal error: no state"));
1889 case XML_RELAXNG_ERR_NODEFINE:
1890 return(xmlCharStrdup("Internal error: no define"));
Daniel Veillard952379b2003-03-17 15:37:12 +00001891 case XML_RELAXNG_ERR_INTERNAL:
1892 snprintf(msg, 1000, "Internal error: %s", arg1);
1893 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001894 case XML_RELAXNG_ERR_LISTEXTRA:
1895 snprintf(msg, 1000, "Extra data in list: %s", arg1);
1896 break;
1897 case XML_RELAXNG_ERR_INTERNODATA:
1898 return(xmlCharStrdup("Internal: interleave block has no data"));
1899 case XML_RELAXNG_ERR_INTERSEQ:
1900 return(xmlCharStrdup("Invalid sequence in interleave"));
1901 case XML_RELAXNG_ERR_INTEREXTRA:
1902 snprintf(msg, 1000, "Extra element %s in interleave", arg1);
1903 break;
1904 case XML_RELAXNG_ERR_ELEMNAME:
1905 snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2);
1906 break;
1907 case XML_RELAXNG_ERR_ELEMNONS:
1908 snprintf(msg, 1000, "Expecting a namespace for element %s", arg1);
1909 break;
1910 case XML_RELAXNG_ERR_ELEMWRONGNS:
1911 snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s",
1912 arg1, arg2);
1913 break;
1914 case XML_RELAXNG_ERR_ELEMEXTRANS:
1915 snprintf(msg, 1000, "Expecting no namespace for element %s", arg1);
1916 break;
1917 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
1918 snprintf(msg, 1000, "Expecting element %s to be empty", arg1);
1919 break;
1920 case XML_RELAXNG_ERR_NOELEM:
1921 snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1);
1922 break;
1923 case XML_RELAXNG_ERR_NOTELEM:
1924 return(xmlCharStrdup("Expecting an element got text"));
1925 case XML_RELAXNG_ERR_ATTRVALID:
1926 snprintf(msg, 1000, "Element %s failed to validate attributes",
1927 arg1);
1928 break;
1929 case XML_RELAXNG_ERR_CONTENTVALID:
1930 snprintf(msg, 1000, "Element %s failed to validate content",
1931 arg1);
1932 break;
1933 case XML_RELAXNG_ERR_EXTRACONTENT:
1934 snprintf(msg, 1000, "Element %s has extra content: %s",
1935 arg1, arg2);
1936 break;
1937 case XML_RELAXNG_ERR_INVALIDATTR:
1938 snprintf(msg, 1000, "Invalid attribute %s for element %s",
1939 arg1, arg2);
1940 break;
Daniel Veillardc4c21552003-03-29 10:53:38 +00001941 case XML_RELAXNG_ERR_LACKDATA:
1942 snprintf(msg, 1000, "Datatype element %s contains no data",
1943 arg1);
1944 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001945 case XML_RELAXNG_ERR_DATAELEM:
1946 snprintf(msg, 1000, "Datatype element %s has child elements",
1947 arg1);
1948 break;
1949 case XML_RELAXNG_ERR_VALELEM:
1950 snprintf(msg, 1000, "Value element %s has child elements",
1951 arg1);
1952 break;
1953 case XML_RELAXNG_ERR_LISTELEM:
1954 snprintf(msg, 1000, "List element %s has child elements",
1955 arg1);
1956 break;
1957 case XML_RELAXNG_ERR_DATATYPE:
1958 snprintf(msg, 1000, "Error validating datatype %s",
1959 arg1);
1960 break;
1961 case XML_RELAXNG_ERR_VALUE:
1962 snprintf(msg, 1000, "Error validating value %s",
1963 arg1);
1964 break;
1965 case XML_RELAXNG_ERR_LIST:
1966 return(xmlCharStrdup("Error validating list"));
1967 case XML_RELAXNG_ERR_NOGRAMMAR:
1968 return(xmlCharStrdup("No top grammar defined"));
1969 case XML_RELAXNG_ERR_EXTRADATA:
1970 return(xmlCharStrdup("Extra data in the document"));
1971 default:
1972 TODO
Daniel Veillard42f12e92003-03-07 18:32:59 +00001973 }
1974 if (msg[0] == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001975 snprintf(msg, 1000, "Unknown error code %d", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001976 }
1977 msg[1000] = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001978 return(xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001979}
1980
1981/**
1982 * xmlRelaxNGValidErrorContext:
1983 * @ctxt: the validation context
1984 * @node: the node
1985 * @child: the node child generating the problem.
1986 *
1987 * Dump informations about the kocation of the error in the instance
Daniel Veillard6eadf632003-01-23 18:29:16 +00001988 */
1989static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001990xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node,
1991 xmlNodePtr child)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001992{
1993 int line = 0;
1994 const xmlChar *file = NULL;
1995 const xmlChar *name = NULL;
1996 const char *type = "error";
1997
1998 if ((ctxt == NULL) || (ctxt->error == NULL))
1999 return;
2000
2001 if (child != NULL)
2002 node = child;
2003
2004 if (node != NULL) {
2005 if ((node->type == XML_DOCUMENT_NODE) ||
2006 (node->type == XML_HTML_DOCUMENT_NODE)) {
2007 xmlDocPtr doc = (xmlDocPtr) node;
2008
2009 file = doc->URL;
2010 } else {
2011 /*
2012 * Try to find contextual informations to report
2013 */
2014 if (node->type == XML_ELEMENT_NODE) {
2015 line = (int) node->content;
2016 } else if ((node->prev != NULL) &&
2017 (node->prev->type == XML_ELEMENT_NODE)) {
2018 line = (int) node->prev->content;
2019 } else if ((node->parent != NULL) &&
2020 (node->parent->type == XML_ELEMENT_NODE)) {
2021 line = (int) node->parent->content;
2022 }
2023 if ((node->doc != NULL) && (node->doc->URL != NULL))
2024 file = node->doc->URL;
2025 if (node->name != NULL)
2026 name = node->name;
2027 }
2028 }
2029
Daniel Veillard42f12e92003-03-07 18:32:59 +00002030 type = "RNG validity error";
Daniel Veillard6eadf632003-01-23 18:29:16 +00002031
2032 if ((file != NULL) && (line != 0) && (name != NULL))
2033 ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n",
2034 type, file, line, name);
2035 else if ((file != NULL) && (name != NULL))
2036 ctxt->error(ctxt->userData, "%s: file %s element %s\n",
2037 type, file, name);
2038 else if ((file != NULL) && (line != 0))
2039 ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line);
2040 else if (file != NULL)
2041 ctxt->error(ctxt->userData, "%s: file %s\n", type, file);
2042 else if (name != NULL)
2043 ctxt->error(ctxt->userData, "%s: element %s\n", type, name);
2044 else
2045 ctxt->error(ctxt->userData, "%s\n", type);
2046}
Daniel Veillard42f12e92003-03-07 18:32:59 +00002047
2048/**
2049 * xmlRelaxNGShowValidError:
2050 * @ctxt: the validation context
2051 * @err: the error number
2052 * @node: the node
2053 * @child: the node child generating the problem.
2054 * @arg1: the first argument
2055 * @arg2: the second argument
2056 *
2057 * Show a validation error.
2058 */
2059static void
2060xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
2061 xmlNodePtr node, xmlNodePtr child,
2062 const xmlChar *arg1, const xmlChar *arg2)
2063{
2064 xmlChar *msg;
2065
2066 if (ctxt->error == NULL)
2067 return;
2068
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002069#ifdef DEBUG_ERROR
2070 xmlGenericError(xmlGenericErrorContext,
2071 "Show error %d\n", err);
2072#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002073 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2074 if (msg == NULL)
2075 return;
2076
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002077 if (ctxt->errNo == XML_RELAXNG_OK)
2078 ctxt->errNo = err;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002079 xmlRelaxNGValidErrorContext(ctxt, node, child);
2080 ctxt->error(ctxt->userData, "%s\n", msg);
2081 xmlFree(msg);
2082}
2083
2084/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002085 * xmlRelaxNGPopErrors:
2086 * @ctxt: the validation context
2087 * @level: the error level in the stack
2088 *
2089 * pop and discard all errors until the given level is reached
2090 */
2091static void
2092xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) {
2093 int i;
2094 xmlRelaxNGValidErrorPtr err;
2095
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002096#ifdef DEBUG_ERROR
2097 xmlGenericError(xmlGenericErrorContext,
2098 "Pop errors till level %d\n", level);
2099#endif
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002100 for (i = level;i < ctxt->errNr;i++) {
2101 err = &ctxt->errTab[i];
2102 if (err->flags & ERROR_IS_DUP) {
2103 if (err->arg1 != NULL)
2104 xmlFree((xmlChar *)err->arg1);
2105 err->arg1 = NULL;
2106 if (err->arg2 != NULL)
2107 xmlFree((xmlChar *)err->arg2);
2108 err->arg2 = NULL;
2109 err->flags = 0;
2110 }
2111 }
2112 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002113 if (ctxt->errNr <= 0)
2114 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002115}
2116/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002117 * xmlRelaxNGDumpValidError:
2118 * @ctxt: the validation context
2119 *
2120 * Show all validation error over a given index.
2121 */
2122static void
2123xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002124 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002125 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002126
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002127#ifdef DEBUG_ERROR
2128 xmlGenericError(xmlGenericErrorContext,
2129 "Dumping error stack %d errors\n", ctxt->errNr);
2130#endif
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002131 for (i = 0, k = 0;i < ctxt->errNr;i++) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00002132 err = &ctxt->errTab[i];
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002133 if (k < MAX_ERROR) {
2134 for (j = 0;j < i;j++) {
2135 dup = &ctxt->errTab[j];
2136 if ((err->err == dup->err) && (err->node == dup->node) &&
2137 (xmlStrEqual(err->arg1, dup->arg1)) &&
2138 (xmlStrEqual(err->arg2, dup->arg2))) {
2139 goto skip;
2140 }
Daniel Veillard580ced82003-03-21 21:22:48 +00002141 }
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002142 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2143 err->arg1, err->arg2);
2144 k++;
Daniel Veillard580ced82003-03-21 21:22:48 +00002145 }
Daniel Veillard580ced82003-03-21 21:22:48 +00002146skip:
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002147 if (err->flags & ERROR_IS_DUP) {
2148 if (err->arg1 != NULL)
2149 xmlFree((xmlChar *)err->arg1);
2150 err->arg1 = NULL;
2151 if (err->arg2 != NULL)
2152 xmlFree((xmlChar *)err->arg2);
2153 err->arg2 = NULL;
2154 err->flags = 0;
2155 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002156 }
2157 ctxt->errNr = 0;
2158}
2159/**
2160 * xmlRelaxNGAddValidError:
2161 * @ctxt: the validation context
2162 * @err: the error number
2163 * @arg1: the first argument
2164 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002165 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002166 *
2167 * Register a validation error, either generating it if it's sure
2168 * or stacking it for later handling if unsure.
2169 */
2170static void
2171xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002172 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002173{
2174 if ((ctxt == NULL) || (ctxt->error == NULL))
2175 return;
2176
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002177#ifdef DEBUG_ERROR
2178 xmlGenericError(xmlGenericErrorContext,
2179 "Adding error %d\n", err);
2180#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002181 /*
2182 * generate the error directly
2183 */
2184 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
2185 xmlNodePtr node, seq;
2186 /*
2187 * Flush first any stacked error which might be the
2188 * real cause of the problem.
2189 */
2190 if (ctxt->errNr != 0)
2191 xmlRelaxNGDumpValidError(ctxt);
2192 if (ctxt->state != NULL) {
2193 node = ctxt->state->node;
2194 seq = ctxt->state->seq;
2195 } else {
2196 node = seq = NULL;
2197 }
2198 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2199 }
2200 /*
2201 * Stack the error for later processing if needed
2202 */
2203 else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002204 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002205 }
2206}
2207
Daniel Veillard6eadf632003-01-23 18:29:16 +00002208
2209/************************************************************************
2210 * *
2211 * Type library hooks *
2212 * *
2213 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002214static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2215 const xmlChar *str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002216
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002217/**
2218 * xmlRelaxNGSchemaTypeHave:
2219 * @data: data needed for the library
2220 * @type: the type name
2221 *
2222 * Check if the given type is provided by
2223 * the W3C XMLSchema Datatype library.
2224 *
2225 * Returns 1 if yes, 0 if no and -1 in case of error.
2226 */
2227static int
2228xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002229 const xmlChar *type) {
2230 xmlSchemaTypePtr typ;
2231
2232 if (type == NULL)
2233 return(-1);
2234 typ = xmlSchemaGetPredefinedType(type,
2235 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2236 if (typ == NULL)
2237 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002238 return(1);
2239}
2240
2241/**
2242 * xmlRelaxNGSchemaTypeCheck:
2243 * @data: data needed for the library
2244 * @type: the type name
2245 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002246 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002247 *
2248 * Check if the given type and value are validated by
2249 * the W3C XMLSchema Datatype library.
2250 *
2251 * Returns 1 if yes, 0 if no and -1 in case of error.
2252 */
2253static int
2254xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002255 const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002256 const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002257 void **result,
2258 xmlNodePtr node) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002259 xmlSchemaTypePtr typ;
2260 int ret;
2261
2262 /*
2263 * TODO: the type should be cached ab provided back, interface subject
2264 * to changes.
2265 * TODO: handle facets, may require an additional interface and keep
2266 * the value returned from the validation.
2267 */
2268 if ((type == NULL) || (value == NULL))
2269 return(-1);
2270 typ = xmlSchemaGetPredefinedType(type,
2271 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2272 if (typ == NULL)
2273 return(-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002274 ret = xmlSchemaValPredefTypeNode(typ, value,
2275 (xmlSchemaValPtr *) result, node);
2276 if (ret == 2) /* special ID error code */
2277 return(2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002278 if (ret == 0)
2279 return(1);
2280 if (ret > 0)
2281 return(0);
2282 return(-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002283}
2284
2285/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002286 * xmlRelaxNGSchemaFacetCheck:
2287 * @data: data needed for the library
2288 * @type: the type name
2289 * @facet: the facet name
2290 * @val: the facet value
2291 * @strval: the string value
2292 * @value: the value to check
2293 *
2294 * Function provided by a type library to check a value facet
2295 *
2296 * Returns 1 if yes, 0 if no and -1 in case of error.
2297 */
2298static int
Daniel Veillard42f12e92003-03-07 18:32:59 +00002299xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002300 const xmlChar *facetname, const xmlChar *val,
2301 const xmlChar *strval, void *value) {
2302 xmlSchemaFacetPtr facet;
2303 xmlSchemaTypePtr typ;
2304 int ret;
2305
2306 if ((type == NULL) || (strval == NULL))
2307 return(-1);
2308 typ = xmlSchemaGetPredefinedType(type,
2309 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2310 if (typ == NULL)
2311 return(-1);
2312
2313 facet = xmlSchemaNewFacet();
2314 if (facet == NULL)
2315 return(-1);
2316
2317 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2318 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2319 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2320 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2321 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2322 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2323 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2324 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2325 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2326 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2327 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2328 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2329 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2330 facet->type = XML_SCHEMA_FACET_PATTERN;
2331 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2332 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2333 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2334 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2335 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2336 facet->type = XML_SCHEMA_FACET_LENGTH;
2337 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2338 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2339 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2340 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2341 } else {
2342 xmlSchemaFreeFacet(facet);
2343 return(-1);
2344 }
2345 facet->value = xmlStrdup(val);
2346 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2347 if (ret != 0) {
2348 xmlSchemaFreeFacet(facet);
2349 return(-1);
2350 }
2351 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2352 xmlSchemaFreeFacet(facet);
2353 if (ret != 0)
2354 return(-1);
2355 return(0);
2356}
2357
2358/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002359 * xmlRelaxNGSchemaFreeValue:
2360 * @data: data needed for the library
2361 * @value: the value to free
2362 *
2363 * Function provided by a type library to free a Schemas value
2364 *
2365 * Returns 1 if yes, 0 if no and -1 in case of error.
2366 */
2367static void
2368xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) {
2369 xmlSchemaFreeValue(value);
2370}
2371
2372/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002373 * xmlRelaxNGSchemaTypeCompare:
2374 * @data: data needed for the library
2375 * @type: the type name
2376 * @value1: the first value
2377 * @value2: the second value
2378 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002379 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002380 * Datatype library.
2381 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002382 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002383 */
2384static int
2385xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002386 const xmlChar *type,
2387 const xmlChar *value1,
2388 xmlNodePtr ctxt1,
2389 void *comp1,
2390 const xmlChar *value2,
2391 xmlNodePtr ctxt2) {
Daniel Veillard80b19092003-03-28 13:29:53 +00002392 int ret;
2393 xmlSchemaTypePtr typ;
2394 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2395
2396 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2397 return(-1);
2398 typ = xmlSchemaGetPredefinedType(type,
2399 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2400 if (typ == NULL)
2401 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002402 if (comp1 == NULL) {
2403 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2404 if (ret != 0)
2405 return(-1);
2406 if (res1 == NULL)
2407 return(-1);
2408 } else {
2409 res1 = (xmlSchemaValPtr) comp1;
2410 }
2411 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002412 if (ret != 0) {
2413 xmlSchemaFreeValue(res1);
2414 return(-1);
2415 }
2416 if (res1 == NULL) {
2417 xmlSchemaFreeValue(res1);
2418 return(-1);
2419 }
2420 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002421 if (res1 != (xmlSchemaValPtr) comp1)
2422 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002423 xmlSchemaFreeValue(res2);
2424 if (ret == -2)
2425 return(-1);
2426 if (ret == 0)
2427 return(1);
2428 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002429}
2430
2431/**
2432 * xmlRelaxNGDefaultTypeHave:
2433 * @data: data needed for the library
2434 * @type: the type name
2435 *
2436 * Check if the given type is provided by
2437 * the default datatype library.
2438 *
2439 * Returns 1 if yes, 0 if no and -1 in case of error.
2440 */
2441static int
2442xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) {
2443 if (type == NULL)
2444 return(-1);
2445 if (xmlStrEqual(type, BAD_CAST "string"))
2446 return(1);
2447 if (xmlStrEqual(type, BAD_CAST "token"))
2448 return(1);
2449 return(0);
2450}
2451
2452/**
2453 * xmlRelaxNGDefaultTypeCheck:
2454 * @data: data needed for the library
2455 * @type: the type name
2456 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002457 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002458 *
2459 * Check if the given type and value are validated by
2460 * the default datatype library.
2461 *
2462 * Returns 1 if yes, 0 if no and -1 in case of error.
2463 */
2464static int
2465xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2466 const xmlChar *type ATTRIBUTE_UNUSED,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002467 const xmlChar *value ATTRIBUTE_UNUSED,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002468 void **result ATTRIBUTE_UNUSED,
2469 xmlNodePtr node ATTRIBUTE_UNUSED) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002470 if (value == NULL)
2471 return(-1);
2472 if (xmlStrEqual(type, BAD_CAST "string"))
2473 return(1);
2474 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002475 return(1);
2476 }
2477
2478 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002479}
2480
2481/**
2482 * xmlRelaxNGDefaultTypeCompare:
2483 * @data: data needed for the library
2484 * @type: the type name
2485 * @value1: the first value
2486 * @value2: the second value
2487 *
2488 * Compare two values accordingly a type from the default
2489 * datatype library.
2490 *
2491 * Returns 1 if yes, 0 if no and -1 in case of error.
2492 */
2493static int
2494xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002495 const xmlChar *type,
2496 const xmlChar *value1,
2497 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2498 void *comp1 ATTRIBUTE_UNUSED,
2499 const xmlChar *value2,
2500 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00002501 int ret = -1;
2502
2503 if (xmlStrEqual(type, BAD_CAST "string")) {
2504 ret = xmlStrEqual(value1, value2);
2505 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2506 if (!xmlStrEqual(value1, value2)) {
2507 xmlChar *nval, *nvalue;
2508
2509 /*
2510 * TODO: trivial optimizations are possible by
2511 * computing at compile-time
2512 */
2513 nval = xmlRelaxNGNormalize(NULL, value1);
2514 nvalue = xmlRelaxNGNormalize(NULL, value2);
2515
Daniel Veillardd4310742003-02-18 21:12:46 +00002516 if ((nval == NULL) || (nvalue == NULL))
Daniel Veillardea3f3982003-01-26 19:45:18 +00002517 ret = -1;
Daniel Veillardd4310742003-02-18 21:12:46 +00002518 else if (xmlStrEqual(nval, nvalue))
2519 ret = 1;
2520 else
2521 ret = 0;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002522 if (nval != NULL)
2523 xmlFree(nval);
2524 if (nvalue != NULL)
2525 xmlFree(nvalue);
Daniel Veillardd4310742003-02-18 21:12:46 +00002526 } else
2527 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002528 }
2529 return(ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002530}
2531
2532static int xmlRelaxNGTypeInitialized = 0;
2533static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2534
2535/**
2536 * xmlRelaxNGFreeTypeLibrary:
2537 * @lib: the type library structure
2538 * @namespace: the URI bound to the library
2539 *
2540 * Free the structure associated to the type library
2541 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002542static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002543xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2544 const xmlChar *namespace ATTRIBUTE_UNUSED) {
2545 if (lib == NULL)
2546 return;
2547 if (lib->namespace != NULL)
2548 xmlFree((xmlChar *)lib->namespace);
2549 xmlFree(lib);
2550}
2551
2552/**
2553 * xmlRelaxNGRegisterTypeLibrary:
2554 * @namespace: the URI bound to the library
2555 * @data: data associated to the library
2556 * @have: the provide function
2557 * @check: the checking function
2558 * @comp: the comparison function
2559 *
2560 * Register a new type library
2561 *
2562 * Returns 0 in case of success and -1 in case of error.
2563 */
2564static int
2565xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data,
2566 xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002567 xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet,
2568 xmlRelaxNGTypeFree freef) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002569 xmlRelaxNGTypeLibraryPtr lib;
2570 int ret;
2571
2572 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2573 (check == NULL) || (comp == NULL))
2574 return(-1);
2575 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2576 xmlGenericError(xmlGenericErrorContext,
2577 "Relax-NG types library '%s' already registered\n",
2578 namespace);
2579 return(-1);
2580 }
2581 lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2582 if (lib == NULL) {
2583 xmlGenericError(xmlGenericErrorContext,
2584 "Relax-NG types library '%s' malloc() failed\n",
2585 namespace);
2586 return (-1);
2587 }
2588 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2589 lib->namespace = xmlStrdup(namespace);
2590 lib->data = data;
2591 lib->have = have;
2592 lib->comp = comp;
2593 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002594 lib->facet = facet;
2595 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002596 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2597 if (ret < 0) {
2598 xmlGenericError(xmlGenericErrorContext,
2599 "Relax-NG types library failed to register '%s'\n",
2600 namespace);
2601 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2602 return(-1);
2603 }
2604 return(0);
2605}
2606
2607/**
2608 * xmlRelaxNGInitTypes:
2609 *
2610 * Initilize the default type libraries.
2611 *
2612 * Returns 0 in case of success and -1 in case of error.
2613 */
2614static int
Daniel Veillard6eadf632003-01-23 18:29:16 +00002615xmlRelaxNGInitTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002616 if (xmlRelaxNGTypeInitialized != 0)
2617 return(0);
2618 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2619 if (xmlRelaxNGRegisteredTypes == NULL) {
2620 xmlGenericError(xmlGenericErrorContext,
2621 "Failed to allocate sh table for Relax-NG types\n");
2622 return(-1);
2623 }
2624 xmlRelaxNGRegisterTypeLibrary(
2625 BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes",
2626 NULL,
2627 xmlRelaxNGSchemaTypeHave,
2628 xmlRelaxNGSchemaTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002629 xmlRelaxNGSchemaTypeCompare,
2630 xmlRelaxNGSchemaFacetCheck,
Daniel Veillard80b19092003-03-28 13:29:53 +00002631 xmlRelaxNGSchemaFreeValue);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002632 xmlRelaxNGRegisterTypeLibrary(
2633 xmlRelaxNGNs,
2634 NULL,
2635 xmlRelaxNGDefaultTypeHave,
2636 xmlRelaxNGDefaultTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002637 xmlRelaxNGDefaultTypeCompare,
2638 NULL,
2639 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002640 xmlRelaxNGTypeInitialized = 1;
2641 return(0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002642}
2643
2644/**
2645 * xmlRelaxNGCleanupTypes:
2646 *
2647 * Cleanup the default Schemas type library associated to RelaxNG
2648 */
2649void
2650xmlRelaxNGCleanupTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002651 if (xmlRelaxNGTypeInitialized == 0)
2652 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002653 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002654 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2655 xmlRelaxNGFreeTypeLibrary);
2656 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002657}
2658
2659/************************************************************************
2660 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002661 * Compiling element content into regexp *
2662 * *
2663 * Sometime the element content can be compiled into a pure regexp, *
2664 * This allows a faster execution and streamability at that level *
2665 * *
2666 ************************************************************************/
2667
Daniel Veillard52b48c72003-04-13 19:53:42 +00002668static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2669 xmlRelaxNGDefinePtr def);
2670
Daniel Veillard952379b2003-03-17 15:37:12 +00002671/**
2672 * xmlRelaxNGIsCompileable:
2673 * @define: the definition to check
2674 *
2675 * Check if a definition is nullable.
2676 *
2677 * Returns 1 if yes, 0 if no and -1 in case of error
2678 */
2679static int
2680xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00002681 int ret = -1;
2682
Daniel Veillard952379b2003-03-17 15:37:12 +00002683 if (def == NULL) {
2684 return(-1);
2685 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002686 if ((def->type != XML_RELAXNG_ELEMENT) &&
2687 (def->dflags & IS_COMPILABLE))
2688 return(1);
2689 if ((def->type != XML_RELAXNG_ELEMENT) &&
2690 (def->dflags & IS_NOT_COMPILABLE))
2691 return(0);
Daniel Veillard952379b2003-03-17 15:37:12 +00002692 switch(def->type) {
2693 case XML_RELAXNG_REF:
2694 case XML_RELAXNG_EXTERNALREF:
2695 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002696 if (def->depth == -20) {
2697 return(1);
2698 } else {
2699 def->depth = -20;
2700 ret = xmlRelaxNGIsCompileable(def->content);
2701 }
2702 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002703 case XML_RELAXNG_NOOP:
2704 case XML_RELAXNG_START:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002705 ret = xmlRelaxNGIsCompileable(def->content);
2706 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002707 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002708 case XML_RELAXNG_EMPTY:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002709 ret = 1;
2710 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002711 case XML_RELAXNG_ELEMENT:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002712 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2713 ((def->dflags & IS_COMPILABLE) == 0)) {
2714 ret = xmlRelaxNGIsCompileable(def->content);
2715 if (ret == 0) def->dflags |= IS_NOT_COMPILABLE;
2716 if (ret == 1) def->dflags |= IS_COMPILABLE;
2717 }
2718 if ((def->nameClass != NULL) || (def->name == NULL))
2719 return(0);
2720 else
2721 return(1);
2722 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002723 case XML_RELAXNG_OPTIONAL:
2724 case XML_RELAXNG_ZEROORMORE:
2725 case XML_RELAXNG_ONEORMORE:
2726 case XML_RELAXNG_CHOICE:
2727 case XML_RELAXNG_GROUP:
2728 case XML_RELAXNG_DEF: {
2729 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002730
2731 list = def->content;
2732 while (list != NULL) {
2733 ret = xmlRelaxNGIsCompileable(list);
2734 if (ret != 1)
Daniel Veillard52b48c72003-04-13 19:53:42 +00002735 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002736 list = list->next;
2737 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002738 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002739 }
2740 case XML_RELAXNG_EXCEPT:
2741 case XML_RELAXNG_ATTRIBUTE:
2742 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002743 case XML_RELAXNG_DATATYPE:
2744 case XML_RELAXNG_LIST:
2745 case XML_RELAXNG_PARAM:
2746 case XML_RELAXNG_VALUE:
2747 ret = 0;
2748 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002749 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002750 ret = -1;
2751 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002752 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002753 if (ret == 0) def->dflags |= IS_NOT_COMPILABLE;
2754 if (ret == 1) def->dflags |= IS_COMPILABLE;
2755 return(ret);
2756}
2757
2758/**
2759 * xmlRelaxNGCompile:
2760 * ctxt: the RelaxNG parser context
2761 * @define: the definition tree to compile
2762 *
2763 * Compile the set of definitions, it works recursively, till the
2764 * element boundaries, where it tries to compile the content if possible
2765 *
2766 * Returns 0 if success and -1 in case of error
2767 */
2768static int
2769xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) {
2770 int ret = 0;
2771 xmlRelaxNGDefinePtr list;
2772
2773 if ((ctxt == NULL) || (def == NULL)) return(-1);
2774
2775 switch(def->type) {
2776 case XML_RELAXNG_START:
2777 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
2778 xmlAutomataPtr oldam = ctxt->am;
2779 xmlAutomataStatePtr oldstate = ctxt->state;
2780
2781 def->depth = -25;
2782
2783 list = def->content;
2784 ctxt->am = xmlNewAutomata();
2785 if (ctxt->am == NULL)
2786 return(-1);
2787 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2788 while (list != NULL) {
2789 xmlRelaxNGCompile(ctxt, list);
2790 list = list->next;
2791 }
2792 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2793 def->contModel = xmlAutomataCompile(ctxt->am);
2794 xmlRegexpIsDeterminist(def->contModel);
2795
2796 xmlFreeAutomata(ctxt->am);
2797 ctxt->state = oldstate;
2798 ctxt->am = oldam;
2799 }
2800 break;
2801 case XML_RELAXNG_ELEMENT:
2802 if ((ctxt->am != NULL) && (def->name != NULL)) {
2803 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
2804 ctxt->state, NULL, def->name, def->ns, NULL);
2805 }
2806 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
2807 xmlAutomataPtr oldam = ctxt->am;
2808 xmlAutomataStatePtr oldstate = ctxt->state;
2809
2810 def->depth = -25;
2811
2812 list = def->content;
2813 ctxt->am = xmlNewAutomata();
2814 if (ctxt->am == NULL)
2815 return(-1);
2816 ctxt->state = xmlAutomataGetInitState(ctxt->am);
2817 while (list != NULL) {
2818 xmlRelaxNGCompile(ctxt, list);
2819 list = list->next;
2820 }
2821 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
2822 def->contModel = xmlAutomataCompile(ctxt->am);
2823 xmlRegexpIsDeterminist(def->contModel);
2824
2825 xmlFreeAutomata(ctxt->am);
2826 ctxt->state = oldstate;
2827 ctxt->am = oldam;
2828 } else {
2829 xmlAutomataPtr oldam = ctxt->am;
2830
2831 /*
2832 * we can't build the content model for this element content
2833 * but it still might be possible to build it for some of its
2834 * children, recurse.
2835 */
2836 ret = xmlRelaxNGTryCompile(ctxt, def);
2837 ctxt->am = oldam;
2838 }
2839 break;
2840 case XML_RELAXNG_REF:
2841 case XML_RELAXNG_EXTERNALREF:
2842 case XML_RELAXNG_PARENTREF:
2843 case XML_RELAXNG_NOOP:
2844 ret = xmlRelaxNGCompile(ctxt, def->content);
2845 break;
2846 case XML_RELAXNG_OPTIONAL: {
2847 xmlAutomataStatePtr oldstate = ctxt->state;
2848
2849 xmlRelaxNGCompile(ctxt, def->content);
2850 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
2851 break;
2852 }
2853 case XML_RELAXNG_ZEROORMORE: {
2854 xmlAutomataStatePtr oldstate;
2855
2856 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
2857 oldstate = ctxt->state;
2858 xmlRelaxNGCompile(ctxt, def->content);
2859 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
2860 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2861 break;
2862 }
2863 case XML_RELAXNG_ONEORMORE: {
2864 xmlAutomataStatePtr oldstate;
2865
2866 xmlRelaxNGCompile(ctxt, def->content);
2867 oldstate = ctxt->state;
2868 xmlRelaxNGCompile(ctxt, def->content);
2869 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
2870 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2871 break;
2872 }
2873 case XML_RELAXNG_CHOICE: {
2874 xmlAutomataStatePtr target = NULL;
2875 xmlAutomataStatePtr oldstate = ctxt->state;
2876
2877 list = def->content;
2878 while (list != NULL) {
2879 ctxt->state = oldstate;
2880 xmlRelaxNGCompile(ctxt, list);
2881 if (target == NULL)
2882 target = ctxt->state;
2883 else {
2884 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, target);
2885 }
2886 list = list->next;
2887 }
2888
2889 break;
2890 }
2891 case XML_RELAXNG_GROUP:
2892 case XML_RELAXNG_DEF:
2893 list = def->content;
2894 while (list != NULL) {
2895 xmlRelaxNGCompile(ctxt, list);
2896 list = list->next;
2897 }
2898 break;
2899 case XML_RELAXNG_TEXT: {
2900 xmlAutomataStatePtr oldstate;
2901
2902 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
2903 oldstate = ctxt->state;
2904 xmlRelaxNGCompile(ctxt, def->content);
2905 xmlAutomataNewTransition(ctxt->am, ctxt->state, ctxt->state,
2906 BAD_CAST "#text", NULL);
2907 ctxt->state = xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
2908 break;
2909 }
2910 case XML_RELAXNG_EMPTY:
2911 break;
2912 case XML_RELAXNG_EXCEPT:
2913 case XML_RELAXNG_ATTRIBUTE:
2914 case XML_RELAXNG_INTERLEAVE:
2915 case XML_RELAXNG_NOT_ALLOWED:
2916 case XML_RELAXNG_DATATYPE:
2917 case XML_RELAXNG_LIST:
2918 case XML_RELAXNG_PARAM:
2919 case XML_RELAXNG_VALUE:
2920 TODO /* This should not happen and generate an internal error */
2921 printf("trying to compile %s\n", xmlRelaxNGDefName(def));
2922
2923 break;
2924 }
2925 return(ret);
2926}
2927
2928/**
2929 * xmlRelaxNGTryCompile:
2930 * ctxt: the RelaxNG parser context
2931 * @define: the definition tree to compile
2932 *
2933 * Try to compile the set of definitions, it works recursively,
2934 * possibly ignoring parts which cannot be compiled.
2935 *
2936 * Returns 0 if success and -1 in case of error
2937 */
2938static int
2939xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def) {
2940 int ret = 0;
2941 xmlRelaxNGDefinePtr list;
2942
2943 if ((ctxt == NULL) || (def == NULL)) return(-1);
2944
2945 if ((def->type == XML_RELAXNG_START) ||
2946 (def->type == XML_RELAXNG_ELEMENT)) {
2947 ret = xmlRelaxNGIsCompileable(def);
2948 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
2949 ctxt->am = NULL;
2950 ret = xmlRelaxNGCompile(ctxt, def);
2951 return(ret);
2952 }
2953 }
2954 switch(def->type) {
2955 case XML_RELAXNG_REF:
2956 case XML_RELAXNG_EXTERNALREF:
2957 case XML_RELAXNG_PARENTREF:
2958 case XML_RELAXNG_NOOP:
2959 case XML_RELAXNG_START:
2960 ret = xmlRelaxNGTryCompile(ctxt, def->content);
2961 break;
2962 case XML_RELAXNG_TEXT:
2963 case XML_RELAXNG_DATATYPE:
2964 case XML_RELAXNG_LIST:
2965 case XML_RELAXNG_PARAM:
2966 case XML_RELAXNG_VALUE:
2967 case XML_RELAXNG_EMPTY:
2968 case XML_RELAXNG_ELEMENT:
2969 ret = 0;
2970 break;
2971 case XML_RELAXNG_OPTIONAL:
2972 case XML_RELAXNG_ZEROORMORE:
2973 case XML_RELAXNG_ONEORMORE:
2974 case XML_RELAXNG_CHOICE:
2975 case XML_RELAXNG_GROUP:
2976 case XML_RELAXNG_DEF:
2977 list = def->content;
2978 while (list != NULL) {
2979 ret = xmlRelaxNGTryCompile(ctxt, list);
2980 if (ret != 0)
2981 break;
2982 list = list->next;
2983 }
2984 break;
2985 case XML_RELAXNG_EXCEPT:
2986 case XML_RELAXNG_ATTRIBUTE:
2987 case XML_RELAXNG_INTERLEAVE:
2988 case XML_RELAXNG_NOT_ALLOWED:
2989 ret = 0;
2990 break;
2991 }
2992 return(ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00002993}
2994
2995/************************************************************************
2996 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002997 * Parsing functions *
2998 * *
2999 ************************************************************************/
3000
3001static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(
3002 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3003static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(
3004 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
3005static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(
Daniel Veillard154877e2003-01-30 12:17:05 +00003006 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003007static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(
3008 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00003009static xmlRelaxNGPtr xmlRelaxNGParseDocument(
3010 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00003011static int xmlRelaxNGParseGrammarContent(
3012 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00003013static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(
3014 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
3015 xmlRelaxNGDefinePtr def);
Daniel Veillard419a7682003-02-03 23:22:49 +00003016static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(
3017 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003018static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3019 xmlRelaxNGDefinePtr define, xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003020
3021
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003022#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003023
3024/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003025 * xmlRelaxNGIsNullable:
3026 * @define: the definition to verify
3027 *
3028 * Check if a definition is nullable.
3029 *
3030 * Returns 1 if yes, 0 if no and -1 in case of error
3031 */
3032static int
3033xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) {
3034 int ret;
3035 if (define == NULL)
3036 return(-1);
3037
Daniel Veillarde063f482003-03-21 16:53:17 +00003038 if (define->dflags & IS_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00003039 return(1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003040 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00003041 return(0);
3042 switch (define->type) {
3043 case XML_RELAXNG_EMPTY:
3044 case XML_RELAXNG_TEXT:
3045 ret = 1; break;
3046 case XML_RELAXNG_NOOP:
3047 case XML_RELAXNG_DEF:
3048 case XML_RELAXNG_REF:
3049 case XML_RELAXNG_EXTERNALREF:
3050 case XML_RELAXNG_PARENTREF:
3051 case XML_RELAXNG_ONEORMORE:
3052 ret = xmlRelaxNGIsNullable(define->content);
3053 break;
3054 case XML_RELAXNG_EXCEPT:
3055 case XML_RELAXNG_NOT_ALLOWED:
3056 case XML_RELAXNG_ELEMENT:
3057 case XML_RELAXNG_DATATYPE:
3058 case XML_RELAXNG_PARAM:
3059 case XML_RELAXNG_VALUE:
3060 case XML_RELAXNG_LIST:
3061 case XML_RELAXNG_ATTRIBUTE:
3062 ret = 0; break;
3063 case XML_RELAXNG_CHOICE: {
3064 xmlRelaxNGDefinePtr list = define->content;
3065
3066 while (list != NULL) {
3067 ret = xmlRelaxNGIsNullable(list);
3068 if (ret != 0)
3069 goto done;
3070 list = list->next;
3071 }
3072 ret = 0; break;
3073 }
3074 case XML_RELAXNG_START:
3075 case XML_RELAXNG_INTERLEAVE:
3076 case XML_RELAXNG_GROUP: {
3077 xmlRelaxNGDefinePtr list = define->content;
3078
3079 while (list != NULL) {
3080 ret = xmlRelaxNGIsNullable(list);
3081 if (ret != 1)
3082 goto done;
3083 list = list->next;
3084 }
3085 return(1);
3086 }
3087 default:
3088 return(-1);
3089 }
3090done:
3091 if (ret == 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00003092 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003093 if (ret == 1)
Daniel Veillarde063f482003-03-21 16:53:17 +00003094 define->dflags |= IS_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003095 return(ret);
3096}
3097
3098/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003099 * xmlRelaxNGIsBlank:
3100 * @str: a string
3101 *
3102 * Check if a string is ignorable c.f. 4.2. Whitespace
3103 *
3104 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3105 */
3106static int
3107xmlRelaxNGIsBlank(xmlChar *str) {
3108 if (str == NULL)
3109 return(1);
3110 while (*str != 0) {
3111 if (!(IS_BLANK(*str))) return(0);
3112 str++;
3113 }
3114 return(1);
3115}
3116
Daniel Veillard6eadf632003-01-23 18:29:16 +00003117/**
3118 * xmlRelaxNGGetDataTypeLibrary:
3119 * @ctxt: a Relax-NG parser context
3120 * @node: the current data or value element
3121 *
3122 * Applies algorithm from 4.3. datatypeLibrary attribute
3123 *
3124 * Returns the datatypeLibary value or NULL if not found
3125 */
3126static xmlChar *
3127xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3128 xmlNodePtr node) {
3129 xmlChar *ret, *escape;
3130
Daniel Veillard6eadf632003-01-23 18:29:16 +00003131 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3132 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3133 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00003134 if (ret[0] == 0) {
3135 xmlFree(ret);
3136 return(NULL);
3137 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003138 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
Daniel Veillard6eadf632003-01-23 18:29:16 +00003139 if (escape == NULL) {
3140 return(ret);
3141 }
3142 xmlFree(ret);
3143 return(escape);
3144 }
3145 }
3146 node = node->parent;
3147 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillarde5b110b2003-02-04 14:43:39 +00003148 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3149 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00003150 if (ret[0] == 0) {
3151 xmlFree(ret);
3152 return(NULL);
3153 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00003154 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3155 if (escape == NULL) {
3156 return(ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003157 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00003158 xmlFree(ret);
3159 return(escape);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003160 }
3161 node = node->parent;
3162 }
3163 return(NULL);
3164}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003165
3166/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003167 * xmlRelaxNGParseValue:
3168 * @ctxt: a Relax-NG parser context
3169 * @node: the data node.
3170 *
3171 * parse the content of a RelaxNG value node.
3172 *
3173 * Returns the definition pointer or NULL in case of error
3174 */
3175static xmlRelaxNGDefinePtr
3176xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3177 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003178 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003179 xmlChar *type;
3180 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003181 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003182
Daniel Veillardfd573f12003-03-16 17:52:32 +00003183 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003184 if (def == NULL)
3185 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003186 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003187
3188 type = xmlGetProp(node, BAD_CAST "type");
3189 if (type != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00003190 xmlRelaxNGNormExtSpace(type);
3191 if (xmlValidateNCName(type, 0)) {
3192 if (ctxt->error != NULL)
3193 ctxt->error(ctxt->userData,
3194 "value type '%s' is not an NCName\n",
3195 type);
3196 ctxt->nbErrors++;
3197 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003198 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3199 if (library == NULL)
3200 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3201
3202 def->name = type;
3203 def->ns = library;
3204
3205 lib = (xmlRelaxNGTypeLibraryPtr)
3206 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3207 if (lib == NULL) {
3208 if (ctxt->error != NULL)
3209 ctxt->error(ctxt->userData,
3210 "Use of unregistered type library '%s'\n",
3211 library);
3212 ctxt->nbErrors++;
3213 def->data = NULL;
3214 } else {
3215 def->data = lib;
3216 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003217 if (ctxt->error != NULL)
3218 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00003219 "Internal error with type library '%s': no 'have'\n",
3220 library);
3221 ctxt->nbErrors++;
3222 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003223 success = lib->have(lib->data, def->name);
3224 if (success != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003225 if (ctxt->error != NULL)
3226 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00003227 "Error type '%s' is not exported by type library '%s'\n",
3228 def->name, library);
3229 ctxt->nbErrors++;
3230 }
3231 }
3232 }
3233 }
3234 if (node->children == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00003235 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003236 } else if (((node->children->type != XML_TEXT_NODE) &&
3237 (node->children->type != XML_CDATA_SECTION_NODE)) ||
Daniel Veillardedc91922003-01-26 00:52:04 +00003238 (node->children->next != NULL)) {
3239 if (ctxt->error != NULL)
3240 ctxt->error(ctxt->userData,
3241 "Expecting a single text value for <value>content\n");
3242 ctxt->nbErrors++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003243 } else if (def != NULL) {
Daniel Veillardedc91922003-01-26 00:52:04 +00003244 def->value = xmlNodeGetContent(node);
3245 if (def->value == NULL) {
3246 if (ctxt->error != NULL)
3247 ctxt->error(ctxt->userData,
3248 "Element <value> has no content\n");
3249 ctxt->nbErrors++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003250 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3251 void *val = NULL;
3252
3253 success = lib->check(lib->data, def->name, def->value, &val, node);
3254 if (success != 1) {
3255 if (ctxt->error != NULL)
3256 ctxt->error(ctxt->userData,
3257 "Value '%s' is not acceptable for type '%s'\n",
3258 def->value, def->name);
3259 ctxt->nbErrors++;
3260 } else {
3261 if (val != NULL)
3262 def->attrs = val;
3263 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003264 }
3265 }
3266 /* TODO check ahead of time that the value is okay per the type */
3267 return(def);
3268}
3269
3270/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003271 * xmlRelaxNGParseData:
3272 * @ctxt: a Relax-NG parser context
3273 * @node: the data node.
3274 *
3275 * parse the content of a RelaxNG data node.
3276 *
3277 * Returns the definition pointer or NULL in case of error
3278 */
3279static xmlRelaxNGDefinePtr
3280xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard416589a2003-02-17 17:25:42 +00003281 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003282 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003283 xmlRelaxNGTypeLibraryPtr lib;
3284 xmlChar *type;
3285 xmlChar *library;
3286 xmlNodePtr content;
3287 int tmp;
3288
3289 type = xmlGetProp(node, BAD_CAST "type");
3290 if (type == NULL) {
3291 if (ctxt->error != NULL)
3292 ctxt->error(ctxt->userData,
3293 "data has no type\n");
3294 ctxt->nbErrors++;
3295 return(NULL);
3296 }
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 "data type '%s' is not an NCName\n",
3302 type);
3303 ctxt->nbErrors++;
3304 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003305 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3306 if (library == NULL)
3307 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3308
Daniel Veillardfd573f12003-03-16 17:52:32 +00003309 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003310 if (def == NULL) {
3311 xmlFree(type);
3312 return(NULL);
3313 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003314 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003315 def->name = type;
3316 def->ns = library;
3317
3318 lib = (xmlRelaxNGTypeLibraryPtr)
3319 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3320 if (lib == NULL) {
3321 if (ctxt->error != NULL)
3322 ctxt->error(ctxt->userData,
3323 "Use of unregistered type library '%s'\n",
3324 library);
3325 ctxt->nbErrors++;
3326 def->data = NULL;
3327 } else {
3328 def->data = lib;
3329 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003330 if (ctxt->error != NULL)
3331 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003332 "Internal error with type library '%s': no 'have'\n",
3333 library);
3334 ctxt->nbErrors++;
3335 } else {
3336 tmp = lib->have(lib->data, def->name);
3337 if (tmp != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003338 if (ctxt->error != NULL)
3339 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003340 "Error type '%s' is not exported by type library '%s'\n",
3341 def->name, library);
3342 ctxt->nbErrors++;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003343 } else if ((xmlStrEqual(library, BAD_CAST
3344 "http://www.w3.org/2001/XMLSchema-datatypes")) &&
3345 ((xmlStrEqual(def->name, BAD_CAST "IDREF")) ||
3346 (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3347 ctxt->idref = 1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003348 }
3349 }
3350 }
3351 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003352
3353 /*
3354 * Handle optional params
3355 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003356 while (content != NULL) {
Daniel Veillard416589a2003-02-17 17:25:42 +00003357 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3358 break;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003359 if (xmlStrEqual(library,
3360 BAD_CAST"http://relaxng.org/ns/structure/1.0")) {
3361 if (ctxt->error != NULL)
3362 ctxt->error(ctxt->userData,
3363 "Type library '%s' does not allow type parameters\n",
3364 library);
3365 ctxt->nbErrors++;
3366 content = content->next;
3367 while ((content != NULL) &&
3368 (xmlStrEqual(content->name, BAD_CAST "param")))
3369 content = content->next;
3370 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003371 param = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003372 if (param != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003373 param->type = XML_RELAXNG_PARAM;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003374 param->name = xmlGetProp(content, BAD_CAST "name");
3375 if (param->name == NULL) {
3376 if (ctxt->error != NULL)
3377 ctxt->error(ctxt->userData,
3378 "param has no name\n");
3379 ctxt->nbErrors++;
3380 }
3381 param->value = xmlNodeGetContent(content);
3382 if (lastparam == NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003383 def->attrs = lastparam = param;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003384 } else {
3385 lastparam->next = param;
3386 lastparam = param;
3387 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003388 if (lib != NULL) {
3389 }
Daniel Veillard8fe98712003-02-19 00:19:14 +00003390 }
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003391 content = content->next;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003392 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003393 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003394 /*
3395 * Handle optional except
3396 */
3397 if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3398 xmlNodePtr child;
3399 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
3400
Daniel Veillardfd573f12003-03-16 17:52:32 +00003401 except = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard416589a2003-02-17 17:25:42 +00003402 if (except == NULL) {
3403 return(def);
3404 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003405 except->type = XML_RELAXNG_EXCEPT;
Daniel Veillard416589a2003-02-17 17:25:42 +00003406 child = content->children;
3407 if (last == NULL) {
3408 def->content = except;
3409 } else {
3410 last->next = except;
3411 }
3412 if (child == NULL) {
3413 if (ctxt->error != NULL)
3414 ctxt->error(ctxt->userData,
3415 "except has no content\n");
3416 ctxt->nbErrors++;
3417 }
3418 while (child != NULL) {
3419 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3420 if (tmp2 != NULL) {
3421 if (last2 == NULL) {
3422 except->content = last2 = tmp2;
3423 } else {
3424 last2->next = tmp2;
3425 last2 = tmp2;
3426 }
3427 }
3428 child = child->next;
3429 }
3430 content = content->next;
3431 }
3432 /*
3433 * Check there is no unhandled data
3434 */
3435 if (content != NULL) {
3436 if (ctxt->error != NULL)
3437 ctxt->error(ctxt->userData,
3438 "Element data has unexpected content %s\n", content->name);
3439 ctxt->nbErrors++;
3440 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003441
3442 return(def);
3443}
3444
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003445static const xmlChar *invalidName = BAD_CAST "\1";
3446
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003447/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003448 * xmlRelaxNGCompareNameClasses:
3449 * @defs1: the first element/attribute defs
3450 * @defs2: the second element/attribute defs
3451 * @name: the restriction on the name
3452 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003453 *
3454 * Compare the 2 lists of element definitions. The comparison is
3455 * that if both lists do not accept the same QNames, it returns 1
3456 * If the 2 lists can accept the same QName the comparison returns 0
3457 *
3458 * Returns 1 disttinct, 0 if equal
3459 */
3460static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003461xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3462 xmlRelaxNGDefinePtr def2) {
3463 int ret = 1;
3464 xmlNode node;
3465 xmlNs ns;
3466 xmlRelaxNGValidCtxt ctxt;
3467 ctxt.flags = FLAGS_IGNORABLE;
3468
Daniel Veillard42f12e92003-03-07 18:32:59 +00003469 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3470
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003471 if ((def1->type == XML_RELAXNG_ELEMENT) ||
3472 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3473 if (def2->type == XML_RELAXNG_TEXT)
3474 return(1);
3475 if (def1->name != NULL) {
3476 node.name = def1->name;
3477 } else {
3478 node.name = invalidName;
3479 }
3480 node.ns = &ns;
3481 if (def1->ns != NULL) {
3482 if (def1->ns[0] == 0) {
3483 node.ns = NULL;
3484 } else {
3485 ns.href = def1->ns;
3486 }
3487 } else {
3488 ns.href = invalidName;
3489 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003490 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003491 if (def1->nameClass != NULL) {
3492 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3493 } else {
3494 ret = 0;
3495 }
3496 } else {
3497 ret = 1;
3498 }
3499 } else if (def1->type == XML_RELAXNG_TEXT) {
3500 if (def2->type == XML_RELAXNG_TEXT)
3501 return(0);
3502 return(1);
3503 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003504 TODO
3505 ret = 0;
3506 } else {
3507 TODO
3508 ret = 0;
3509 }
3510 if (ret == 0)
3511 return(ret);
3512 if ((def2->type == XML_RELAXNG_ELEMENT) ||
3513 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3514 if (def2->name != NULL) {
3515 node.name = def2->name;
3516 } else {
3517 node.name = invalidName;
3518 }
3519 node.ns = &ns;
3520 if (def2->ns != NULL) {
3521 if (def2->ns[0] == 0) {
3522 node.ns = NULL;
3523 } else {
3524 ns.href = def2->ns;
3525 }
3526 } else {
3527 ns.href = invalidName;
3528 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003529 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003530 if (def2->nameClass != NULL) {
3531 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3532 } else {
3533 ret = 0;
3534 }
3535 } else {
3536 ret = 1;
3537 }
3538 } else {
3539 TODO
3540 ret = 0;
3541 }
3542
3543 return(ret);
3544}
3545
3546/**
3547 * xmlRelaxNGCompareElemDefLists:
3548 * @ctxt: a Relax-NG parser context
3549 * @defs1: the first list of element/attribute defs
3550 * @defs2: the second list of element/attribute defs
3551 *
3552 * Compare the 2 lists of element or attribute definitions. The comparison
3553 * is that if both lists do not accept the same QNames, it returns 1
3554 * If the 2 lists can accept the same QName the comparison returns 0
3555 *
3556 * Returns 1 disttinct, 0 if equal
3557 */
3558static int
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003559xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3560 xmlRelaxNGDefinePtr *def1,
3561 xmlRelaxNGDefinePtr *def2) {
3562 xmlRelaxNGDefinePtr *basedef2 = def2;
3563
Daniel Veillard154877e2003-01-30 12:17:05 +00003564 if ((def1 == NULL) || (def2 == NULL))
3565 return(1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003566 if ((*def1 == NULL) || (*def2 == NULL))
3567 return(1);
3568 while (*def1 != NULL) {
3569 while ((*def2) != NULL) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003570 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3571 return(0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003572 def2++;
3573 }
3574 def2 = basedef2;
3575 def1++;
3576 }
3577 return(1);
3578}
3579
3580/**
3581 * xmlRelaxNGGetElements:
3582 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003583 * @def: the definition definition
3584 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003585 *
3586 * Compute the list of top elements a definition can generate
3587 *
3588 * Returns a list of elements or NULL if none was found.
3589 */
3590static xmlRelaxNGDefinePtr *
3591xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003592 xmlRelaxNGDefinePtr def,
3593 int eora) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003594 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003595 int len = 0;
3596 int max = 0;
3597
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003598 /*
3599 * Don't run that check in case of error. Infinite recursion
3600 * becomes possible.
3601 */
3602 if (ctxt->nbErrors != 0)
3603 return(NULL);
3604
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003605 parent = NULL;
3606 cur = def;
3607 while (cur != NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003608 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3609 (cur->type == XML_RELAXNG_TEXT))) ||
3610 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003611 if (ret == NULL) {
3612 max = 10;
3613 ret = (xmlRelaxNGDefinePtr *)
3614 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3615 if (ret == NULL) {
3616 if (ctxt->error != NULL)
3617 ctxt->error(ctxt->userData,
3618 "Out of memory in element search\n");
3619 ctxt->nbErrors++;
3620 return(NULL);
3621 }
3622 } else if (max <= len) {
3623 max *= 2;
3624 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3625 if (ret == NULL) {
3626 if (ctxt->error != NULL)
3627 ctxt->error(ctxt->userData,
3628 "Out of memory in element search\n");
3629 ctxt->nbErrors++;
3630 return(NULL);
3631 }
3632 }
Daniel Veillardb08c9812003-01-28 23:09:49 +00003633 ret[len++] = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003634 ret[len] = NULL;
3635 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3636 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3637 (cur->type == XML_RELAXNG_GROUP) ||
3638 (cur->type == XML_RELAXNG_ONEORMORE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00003639 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3640 (cur->type == XML_RELAXNG_OPTIONAL) ||
Daniel Veillard952379b2003-03-17 15:37:12 +00003641 (cur->type == XML_RELAXNG_PARENTREF) ||
Daniel Veillardb08c9812003-01-28 23:09:49 +00003642 (cur->type == XML_RELAXNG_REF) ||
3643 (cur->type == XML_RELAXNG_DEF)) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003644 /*
3645 * Don't go within elements or attributes or string values.
3646 * Just gather the element top list
3647 */
3648 if (cur->content != NULL) {
3649 parent = cur;
3650 cur = cur->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003651 tmp = cur;
3652 while (tmp != NULL) {
3653 tmp->parent = parent;
3654 tmp = tmp->next;
3655 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003656 continue;
3657 }
3658 }
Daniel Veillard154877e2003-01-30 12:17:05 +00003659 if (cur == def)
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003660 break;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003661 if (cur->next != NULL) {
3662 cur = cur->next;
3663 continue;
3664 }
3665 do {
3666 cur = cur->parent;
3667 if (cur == NULL) break;
3668 if (cur == def) return(ret);
3669 if (cur->next != NULL) {
3670 cur = cur->next;
3671 break;
3672 }
3673 } while (cur != NULL);
3674 }
3675 return(ret);
3676}
3677
3678/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003679 * xmlRelaxNGCheckChoiceDeterminism:
3680 * @ctxt: a Relax-NG parser context
3681 * @def: the choice definition
3682 *
3683 * Also used to find indeterministic pattern in choice
3684 */
3685static void
3686xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
3687 xmlRelaxNGDefinePtr def) {
3688 xmlRelaxNGDefinePtr **list;
3689 xmlRelaxNGDefinePtr cur;
3690 int nbchild = 0, i, j, ret;
3691 int is_nullable = 0;
3692 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003693 xmlHashTablePtr triage = NULL;
3694 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003695
3696 if ((def == NULL) ||
3697 (def->type != XML_RELAXNG_CHOICE))
3698 return;
3699
Daniel Veillarde063f482003-03-21 16:53:17 +00003700 if (def->dflags & IS_PROCESSED)
3701 return;
3702
Daniel Veillardfd573f12003-03-16 17:52:32 +00003703 /*
3704 * Don't run that check in case of error. Infinite recursion
3705 * becomes possible.
3706 */
3707 if (ctxt->nbErrors != 0)
3708 return;
3709
3710 is_nullable = xmlRelaxNGIsNullable(def);
3711
3712 cur = def->content;
3713 while (cur != NULL) {
3714 nbchild++;
3715 cur = cur->next;
3716 }
3717
3718 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3719 sizeof(xmlRelaxNGDefinePtr *));
3720 if (list == NULL) {
3721 if (ctxt->error != NULL)
3722 ctxt->error(ctxt->userData,
3723 "Out of memory in choice computation\n");
3724 ctxt->nbErrors++;
3725 return;
3726 }
3727 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003728 /*
3729 * a bit strong but safe
3730 */
3731 if (is_nullable == 0) {
3732 triage = xmlHashCreate(10);
3733 } else {
3734 is_triable = 0;
3735 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003736 cur = def->content;
3737 while (cur != NULL) {
3738 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
Daniel Veillarde063f482003-03-21 16:53:17 +00003739 if ((list[i] == NULL) || (list[i][0] == NULL)) {
3740 is_triable = 0;
3741 } else if (is_triable == 1) {
3742 xmlRelaxNGDefinePtr *tmp;
3743 int res;
3744
3745 tmp = list[i];
3746 while ((*tmp != NULL) && (is_triable == 1)) {
3747 if ((*tmp)->type == XML_RELAXNG_TEXT) {
3748 res = xmlHashAddEntry2(triage,
3749 BAD_CAST "#text", NULL,
3750 (void *)cur);
3751 if (res != 0)
3752 is_triable = -1;
3753 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
3754 ((*tmp)->name != NULL)) {
3755 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3756 res = xmlHashAddEntry2(triage,
3757 (*tmp)->name, NULL,
3758 (void *)cur);
3759 else
3760 res = xmlHashAddEntry2(triage,
3761 (*tmp)->name, (*tmp)->ns,
3762 (void *)cur);
3763 if (res != 0)
3764 is_triable = -1;
3765 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
3766 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3767 res = xmlHashAddEntry2(triage,
3768 BAD_CAST "#any", NULL,
3769 (void *)cur);
3770 else
3771 res = xmlHashAddEntry2(triage,
3772 BAD_CAST "#any", (*tmp)->ns,
3773 (void *)cur);
3774 if (res != 0)
3775 is_triable = -1;
3776 } else {
3777 is_triable = -1;
3778 }
3779 tmp++;
3780 }
3781 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003782 i++;
3783 cur = cur->next;
3784 }
3785
3786 for (i = 0;i < nbchild;i++) {
3787 if (list[i] == NULL)
3788 continue;
3789 for (j = 0;j < i;j++) {
3790 if (list[j] == NULL)
3791 continue;
3792 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3793 if (ret == 0) {
3794 is_indeterminist = 1;
3795 }
3796 }
3797 }
3798 for (i = 0;i < nbchild;i++) {
3799 if (list[i] != NULL)
3800 xmlFree(list[i]);
3801 }
3802
3803 xmlFree(list);
3804 if (is_indeterminist) {
Daniel Veillarde063f482003-03-21 16:53:17 +00003805 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003806 }
Daniel Veillarde063f482003-03-21 16:53:17 +00003807 if (is_triable == 1) {
3808 def->dflags |= IS_TRIABLE;
3809 def->data = triage;
3810 } else if (triage != NULL) {
3811 xmlHashFree(triage, NULL);
3812 }
3813 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003814}
3815
3816/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003817 * xmlRelaxNGCheckGroupAttrs:
3818 * @ctxt: a Relax-NG parser context
3819 * @def: the group definition
3820 *
3821 * Detects violations of rule 7.3
3822 */
3823static void
3824xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
3825 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003826 xmlRelaxNGDefinePtr **list;
3827 xmlRelaxNGDefinePtr cur;
3828 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003829
3830 if ((def == NULL) ||
3831 ((def->type != XML_RELAXNG_GROUP) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00003832 (def->type != XML_RELAXNG_ELEMENT)))
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003833 return;
3834
Daniel Veillarde063f482003-03-21 16:53:17 +00003835 if (def->dflags & IS_PROCESSED)
3836 return;
3837
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003838 /*
3839 * Don't run that check in case of error. Infinite recursion
3840 * becomes possible.
3841 */
3842 if (ctxt->nbErrors != 0)
3843 return;
3844
Daniel Veillardfd573f12003-03-16 17:52:32 +00003845 cur = def->attrs;
3846 while (cur != NULL) {
3847 nbchild++;
3848 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003849 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003850 cur = def->content;
3851 while (cur != NULL) {
3852 nbchild++;
3853 cur = cur->next;
3854 }
3855
3856 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3857 sizeof(xmlRelaxNGDefinePtr *));
3858 if (list == NULL) {
3859 if (ctxt->error != NULL)
3860 ctxt->error(ctxt->userData,
3861 "Out of memory in group computation\n");
3862 ctxt->nbErrors++;
3863 return;
3864 }
3865 i = 0;
3866 cur = def->attrs;
3867 while (cur != NULL) {
3868 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
3869 i++;
3870 cur = cur->next;
3871 }
3872 cur = def->content;
3873 while (cur != NULL) {
3874 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
3875 i++;
3876 cur = cur->next;
3877 }
3878
3879 for (i = 0;i < nbchild;i++) {
3880 if (list[i] == NULL)
3881 continue;
3882 for (j = 0;j < i;j++) {
3883 if (list[j] == NULL)
3884 continue;
3885 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3886 if (ret == 0) {
3887 if (ctxt->error != NULL)
3888 ctxt->error(ctxt->userData,
3889 "Attributes conflicts in group\n");
3890 ctxt->nbErrors++;
3891 }
3892 }
3893 }
3894 for (i = 0;i < nbchild;i++) {
3895 if (list[i] != NULL)
3896 xmlFree(list[i]);
3897 }
3898
3899 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00003900 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003901}
3902
3903/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003904 * xmlRelaxNGComputeInterleaves:
3905 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003906 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00003907 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003908 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00003909 * A lot of work for preprocessing interleave definitions
3910 * is potentially needed to get a decent execution speed at runtime
3911 * - trying to get a total order on the element nodes generated
3912 * by the interleaves, order the list of interleave definitions
3913 * following that order.
3914 * - if <text/> is used to handle mixed content, it is better to
3915 * flag this in the define and simplify the runtime checking
3916 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003917 */
3918static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00003919xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
3920 xmlRelaxNGParserCtxtPtr ctxt,
3921 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003922 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003923
Daniel Veillardfd573f12003-03-16 17:52:32 +00003924 xmlRelaxNGPartitionPtr partitions = NULL;
3925 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
3926 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003927 int i,j,ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003928 int nbgroups = 0;
3929 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003930 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003931 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003932
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003933 /*
3934 * Don't run that check in case of error. Infinite recursion
3935 * becomes possible.
3936 */
3937 if (ctxt->nbErrors != 0)
3938 return;
3939
Daniel Veillardfd573f12003-03-16 17:52:32 +00003940#ifdef DEBUG_INTERLEAVE
3941 xmlGenericError(xmlGenericErrorContext,
3942 "xmlRelaxNGComputeInterleaves(%s)\n",
3943 name);
3944#endif
3945 cur = def->content;
3946 while (cur != NULL) {
3947 nbchild++;
3948 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003949 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003950
3951#ifdef DEBUG_INTERLEAVE
3952 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
3953#endif
3954 groups = (xmlRelaxNGInterleaveGroupPtr *)
3955 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
3956 if (groups == NULL)
3957 goto error;
3958 cur = def->content;
3959 while (cur != NULL) {
3960 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
3961 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
3962 if (groups[nbgroups] == NULL)
3963 goto error;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003964 if (cur->type == XML_RELAXNG_TEXT)
3965 is_mixed++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003966 groups[nbgroups]->rule = cur;
3967 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
3968 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
3969 nbgroups++;
3970 cur = cur->next;
3971 }
3972#ifdef DEBUG_INTERLEAVE
3973 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
3974#endif
3975
3976 /*
3977 * Let's check that all rules makes a partitions according to 7.4
3978 */
3979 partitions = (xmlRelaxNGPartitionPtr)
3980 xmlMalloc(sizeof(xmlRelaxNGPartition));
3981 if (partitions == NULL)
3982 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00003983 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00003984 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003985 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003986 for (i = 0;i < nbgroups;i++) {
3987 group = groups[i];
3988 for (j = i+1;j < nbgroups;j++) {
3989 if (groups[j] == NULL)
3990 continue;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003991
Daniel Veillardfd573f12003-03-16 17:52:32 +00003992 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
3993 groups[j]->defs);
3994 if (ret == 0) {
3995 if (ctxt->error != NULL)
3996 ctxt->error(ctxt->userData,
3997 "Element or text conflicts in interleave\n");
3998 ctxt->nbErrors++;
3999 }
4000 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4001 groups[j]->attrs);
4002 if (ret == 0) {
4003 if (ctxt->error != NULL)
4004 ctxt->error(ctxt->userData,
4005 "Attributes conflicts in interleave\n");
4006 ctxt->nbErrors++;
4007 }
4008 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004009 tmp = group->defs;
4010 if ((tmp != NULL) && (*tmp != NULL)) {
4011 while (*tmp != NULL) {
4012 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4013 res = xmlHashAddEntry2(partitions->triage,
4014 BAD_CAST "#text", NULL,
4015 (void *)(i + 1));
4016 if (res != 0)
4017 is_determinist = -1;
4018 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4019 ((*tmp)->name != NULL)) {
4020 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4021 res = xmlHashAddEntry2(partitions->triage,
4022 (*tmp)->name, NULL,
4023 (void *)(i + 1));
4024 else
4025 res = xmlHashAddEntry2(partitions->triage,
4026 (*tmp)->name, (*tmp)->ns,
4027 (void *)(i + 1));
4028 if (res != 0)
4029 is_determinist = -1;
4030 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4031 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4032 res = xmlHashAddEntry2(partitions->triage,
4033 BAD_CAST "#any", NULL,
4034 (void *)(i + 1));
4035 else
4036 res = xmlHashAddEntry2(partitions->triage,
4037 BAD_CAST "#any", (*tmp)->ns,
4038 (void *)(i + 1));
4039 if ((*tmp)->nameClass != NULL)
4040 is_determinist = 2;
4041 if (res != 0)
4042 is_determinist = -1;
4043 } else {
4044 is_determinist = -1;
4045 }
4046 tmp++;
4047 }
4048 } else {
4049 is_determinist = 0;
4050 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004051 }
4052 partitions->groups = groups;
4053
4054 /*
4055 * and save the partition list back in the def
4056 */
4057 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004058 if (is_mixed != 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00004059 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004060 if (is_determinist == 1)
4061 partitions->flags = IS_DETERMINIST;
4062 if (is_determinist == 2)
4063 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004064 return;
4065
4066error:
4067 if (ctxt->error != NULL)
4068 ctxt->error(ctxt->userData,
4069 "Out of memory in interleave computation\n");
4070 ctxt->nbErrors++;
4071 if (groups != NULL) {
4072 for (i = 0;i < nbgroups;i++)
4073 if (groups[i] != NULL) {
4074 if (groups[i]->defs != NULL)
4075 xmlFree(groups[i]->defs);
4076 xmlFree(groups[i]);
4077 }
4078 xmlFree(groups);
4079 }
4080 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004081}
4082
4083/**
4084 * xmlRelaxNGParseInterleave:
4085 * @ctxt: a Relax-NG parser context
4086 * @node: the data node.
4087 *
4088 * parse the content of a RelaxNG interleave node.
4089 *
4090 * Returns the definition pointer or NULL in case of error
4091 */
4092static xmlRelaxNGDefinePtr
4093xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4094 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004095 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004096 xmlNodePtr child;
4097
Daniel Veillardfd573f12003-03-16 17:52:32 +00004098 def = xmlRelaxNGNewDefine(ctxt, node);
4099 if (def == NULL) {
4100 return(NULL);
4101 }
4102 def->type = XML_RELAXNG_INTERLEAVE;
4103
4104 if (ctxt->interleaves == NULL)
4105 ctxt->interleaves = xmlHashCreate(10);
4106 if (ctxt->interleaves == NULL) {
4107 if (ctxt->error != NULL)
4108 ctxt->error(ctxt->userData,
4109 "Failed to create interleaves hash table\n");
4110 ctxt->nbErrors++;
4111 } else {
4112 char name[32];
4113
4114 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4115 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4116 if (ctxt->error != NULL)
4117 ctxt->error(ctxt->userData,
4118 "Failed to add %s to hash table\n", name);
4119 ctxt->nbErrors++;
4120 }
4121 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004122 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004123 if (child == NULL) {
4124 if (ctxt->error != NULL)
4125 ctxt->error(ctxt->userData, "Element interleave is empty\n");
4126 ctxt->nbErrors++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004127 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004128 while (child != NULL) {
4129 if (IS_RELAXNG(child, "element")) {
4130 cur = xmlRelaxNGParseElement(ctxt, child);
4131 } else {
4132 cur = xmlRelaxNGParsePattern(ctxt, child);
4133 }
4134 if (cur != NULL) {
4135 cur->parent = def;
4136 if (last == NULL) {
4137 def->content = last = cur;
4138 } else {
4139 last->next = cur;
4140 last = cur;
4141 }
4142 }
4143 child = child->next;
4144 }
4145
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004146 return(def);
4147}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004148
4149/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004150 * xmlRelaxNGParseInclude:
4151 * @ctxt: a Relax-NG parser context
4152 * @node: the include node
4153 *
4154 * Integrate the content of an include node in the current grammar
4155 *
4156 * Returns 0 in case of success or -1 in case of error
4157 */
4158static int
4159xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4160 xmlRelaxNGIncludePtr incl;
4161 xmlNodePtr root;
4162 int ret = 0, tmp;
4163
4164 incl = node->_private;
4165 if (incl == NULL) {
4166 if (ctxt->error != NULL)
4167 ctxt->error(ctxt->userData,
4168 "Include node has no data\n");
4169 ctxt->nbErrors++;
4170 return(-1);
4171 }
4172 root = xmlDocGetRootElement(incl->doc);
4173 if (root == NULL) {
4174 if (ctxt->error != NULL)
4175 ctxt->error(ctxt->userData,
4176 "Include document is empty\n");
4177 ctxt->nbErrors++;
4178 return(-1);
4179 }
4180 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
4181 if (ctxt->error != NULL)
4182 ctxt->error(ctxt->userData,
4183 "Include document root is not a grammar\n");
4184 ctxt->nbErrors++;
4185 return(-1);
4186 }
4187
4188 /*
4189 * Merge the definition from both the include and the internal list
4190 */
4191 if (root->children != NULL) {
4192 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4193 if (tmp != 0)
4194 ret = -1;
4195 }
4196 if (node->children != NULL) {
4197 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4198 if (tmp != 0)
4199 ret = -1;
4200 }
4201 return(ret);
4202}
4203
4204/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004205 * xmlRelaxNGParseDefine:
4206 * @ctxt: a Relax-NG parser context
4207 * @node: the define node
4208 *
4209 * parse the content of a RelaxNG define element node.
4210 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004211 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004212 */
4213static int
4214xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4215 xmlChar *name;
4216 int ret = 0, tmp;
4217 xmlRelaxNGDefinePtr def;
4218 const xmlChar *olddefine;
4219
4220 name = xmlGetProp(node, BAD_CAST "name");
4221 if (name == NULL) {
4222 if (ctxt->error != NULL)
4223 ctxt->error(ctxt->userData,
4224 "define has no name\n");
4225 ctxt->nbErrors++;
4226 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004227 xmlRelaxNGNormExtSpace(name);
4228 if (xmlValidateNCName(name, 0)) {
4229 if (ctxt->error != NULL)
4230 ctxt->error(ctxt->userData,
4231 "define name '%s' is not an NCName\n",
4232 name);
4233 ctxt->nbErrors++;
4234 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004235 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004236 if (def == NULL) {
4237 xmlFree(name);
4238 return(-1);
4239 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004240 def->type = XML_RELAXNG_DEF;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004241 def->name = name;
4242 if (node->children == NULL) {
4243 if (ctxt->error != NULL)
4244 ctxt->error(ctxt->userData,
4245 "define has no children\n");
4246 ctxt->nbErrors++;
4247 } else {
4248 olddefine = ctxt->define;
4249 ctxt->define = name;
Daniel Veillard154877e2003-01-30 12:17:05 +00004250 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004251 ctxt->define = olddefine;
4252 }
4253 if (ctxt->grammar->defs == NULL)
4254 ctxt->grammar->defs = xmlHashCreate(10);
4255 if (ctxt->grammar->defs == NULL) {
4256 if (ctxt->error != NULL)
4257 ctxt->error(ctxt->userData,
4258 "Could not create definition hash\n");
4259 ctxt->nbErrors++;
4260 ret = -1;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004261 } else {
4262 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4263 if (tmp < 0) {
Daniel Veillard154877e2003-01-30 12:17:05 +00004264 xmlRelaxNGDefinePtr prev;
4265
4266 prev = xmlHashLookup(ctxt->grammar->defs, name);
4267 if (prev == NULL) {
4268 if (ctxt->error != NULL)
4269 ctxt->error(ctxt->userData,
4270 "Internal error on define aggregation of %s\n",
4271 name);
4272 ctxt->nbErrors++;
4273 ret = -1;
Daniel Veillard154877e2003-01-30 12:17:05 +00004274 } else {
4275 while (prev->nextHash != NULL)
4276 prev = prev->nextHash;
4277 prev->nextHash = def;
4278 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004279 }
4280 }
4281 }
4282 return(ret);
4283}
4284
4285/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004286 * xmlRelaxNGProcessExternalRef:
4287 * @ctxt: the parser context
4288 * @node: the externlRef node
4289 *
4290 * Process and compile an externlRef node
4291 *
4292 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4293 */
4294static xmlRelaxNGDefinePtr
4295xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4296 xmlRelaxNGDocumentPtr docu;
4297 xmlNodePtr root, tmp;
4298 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004299 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004300 xmlRelaxNGDefinePtr def;
4301
4302 docu = node->_private;
4303 if (docu != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004304 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004305 if (def == NULL)
4306 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004307 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004308
4309 if (docu->content == NULL) {
4310 /*
4311 * Then do the parsing for good
4312 */
4313 root = xmlDocGetRootElement(docu->doc);
4314 if (root == NULL) {
4315 if (ctxt->error != NULL)
4316 ctxt->error(ctxt->userData,
4317 "xmlRelaxNGParse: %s is empty\n",
4318 ctxt->URL);
4319 ctxt->nbErrors++;
4320 return (NULL);
4321 }
4322 /*
4323 * ns transmission rules
4324 */
4325 ns = xmlGetProp(root, BAD_CAST "ns");
4326 if (ns == NULL) {
4327 tmp = node;
4328 while ((tmp != NULL) &&
4329 (tmp->type == XML_ELEMENT_NODE)) {
4330 ns = xmlGetProp(tmp, BAD_CAST "ns");
4331 if (ns != NULL) {
4332 break;
4333 }
4334 tmp = tmp->parent;
4335 }
4336 if (ns != NULL) {
4337 xmlSetProp(root, BAD_CAST "ns", ns);
4338 newNs = 1;
4339 xmlFree(ns);
4340 }
4341 } else {
4342 xmlFree(ns);
4343 }
4344
4345 /*
4346 * Parsing to get a precompiled schemas.
4347 */
Daniel Veillard77648bb2003-02-20 15:03:22 +00004348 oldflags = ctxt->flags;
4349 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004350 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard77648bb2003-02-20 15:03:22 +00004351 ctxt->flags = oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004352 if ((docu->schema != NULL) &&
4353 (docu->schema->topgrammar != NULL)) {
4354 docu->content = docu->schema->topgrammar->start;
4355 }
4356
4357 /*
4358 * the externalRef may be reused in a different ns context
4359 */
4360 if (newNs == 1) {
4361 xmlUnsetProp(root, BAD_CAST "ns");
4362 }
4363 }
4364 def->content = docu->content;
4365 } else {
4366 def = NULL;
4367 }
4368 return(def);
4369}
4370
4371/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004372 * xmlRelaxNGParsePattern:
4373 * @ctxt: a Relax-NG parser context
4374 * @node: the pattern node.
4375 *
4376 * parse the content of a RelaxNG pattern node.
4377 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004378 * Returns the definition pointer or NULL in case of error or if no
4379 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004380 */
4381static xmlRelaxNGDefinePtr
4382xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4383 xmlRelaxNGDefinePtr def = NULL;
4384
Daniel Veillardd2298792003-02-14 16:54:11 +00004385 if (node == NULL) {
4386 return(NULL);
4387 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004388 if (IS_RELAXNG(node, "element")) {
4389 def = xmlRelaxNGParseElement(ctxt, node);
4390 } else if (IS_RELAXNG(node, "attribute")) {
4391 def = xmlRelaxNGParseAttribute(ctxt, node);
4392 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004393 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004394 if (def == NULL)
4395 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004396 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00004397 if (node->children != NULL) {
4398 if (ctxt->error != NULL)
4399 ctxt->error(ctxt->userData, "empty: had a child node\n");
4400 ctxt->nbErrors++;
4401 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004402 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004403 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004404 if (def == NULL)
4405 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004406 def->type = XML_RELAXNG_TEXT;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004407 if (node->children != NULL) {
4408 if (ctxt->error != NULL)
4409 ctxt->error(ctxt->userData, "text: had a child node\n");
4410 ctxt->nbErrors++;
4411 }
4412 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004413 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004414 if (def == NULL)
4415 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004416 def->type = XML_RELAXNG_ZEROORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004417 if (node->children == NULL) {
4418 if (ctxt->error != NULL)
4419 ctxt->error(ctxt->userData,
4420 "Element %s is empty\n", node->name);
4421 ctxt->nbErrors++;
4422 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004423 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
Daniel Veillardd2298792003-02-14 16:54:11 +00004424 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004425 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004426 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004427 if (def == NULL)
4428 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004429 def->type = XML_RELAXNG_ONEORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004430 if (node->children == NULL) {
4431 if (ctxt->error != NULL)
4432 ctxt->error(ctxt->userData,
4433 "Element %s is empty\n", node->name);
4434 ctxt->nbErrors++;
4435 } else {
4436 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4437 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004438 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004439 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004440 if (def == NULL)
4441 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004442 def->type = XML_RELAXNG_OPTIONAL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004443 if (node->children == NULL) {
4444 if (ctxt->error != NULL)
4445 ctxt->error(ctxt->userData,
4446 "Element %s is empty\n", node->name);
4447 ctxt->nbErrors++;
4448 } else {
4449 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4450 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004451 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004452 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004453 if (def == NULL)
4454 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004455 def->type = XML_RELAXNG_CHOICE;
4456 if (node->children == NULL) {
4457 if (ctxt->error != NULL)
4458 ctxt->error(ctxt->userData,
4459 "Element %s is empty\n", node->name);
4460 ctxt->nbErrors++;
4461 } else {
4462 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4463 }
4464 } else if (IS_RELAXNG(node, "group")) {
4465 def = xmlRelaxNGNewDefine(ctxt, node);
4466 if (def == NULL)
4467 return(NULL);
4468 def->type = XML_RELAXNG_GROUP;
4469 if (node->children == NULL) {
4470 if (ctxt->error != NULL)
4471 ctxt->error(ctxt->userData,
4472 "Element %s is empty\n", node->name);
4473 ctxt->nbErrors++;
4474 } else {
4475 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4476 }
4477 } else if (IS_RELAXNG(node, "ref")) {
4478 def = xmlRelaxNGNewDefine(ctxt, node);
4479 if (def == NULL)
4480 return(NULL);
4481 def->type = XML_RELAXNG_REF;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004482 def->name = xmlGetProp(node, BAD_CAST "name");
4483 if (def->name == NULL) {
4484 if (ctxt->error != NULL)
4485 ctxt->error(ctxt->userData,
4486 "ref has no name\n");
4487 ctxt->nbErrors++;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004488 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004489 xmlRelaxNGNormExtSpace(def->name);
4490 if (xmlValidateNCName(def->name, 0)) {
4491 if (ctxt->error != NULL)
4492 ctxt->error(ctxt->userData,
4493 "ref name '%s' is not an NCName\n",
4494 def->name);
4495 ctxt->nbErrors++;
4496 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004497 }
4498 if (node->children != NULL) {
4499 if (ctxt->error != NULL)
4500 ctxt->error(ctxt->userData,
4501 "ref is not empty\n");
4502 ctxt->nbErrors++;
4503 }
4504 if (ctxt->grammar->refs == NULL)
4505 ctxt->grammar->refs = xmlHashCreate(10);
4506 if (ctxt->grammar->refs == NULL) {
4507 if (ctxt->error != NULL)
4508 ctxt->error(ctxt->userData,
4509 "Could not create references hash\n");
4510 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004511 def = NULL;
4512 } else {
4513 int tmp;
4514
4515 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4516 if (tmp < 0) {
4517 xmlRelaxNGDefinePtr prev;
4518
4519 prev = (xmlRelaxNGDefinePtr)
4520 xmlHashLookup(ctxt->grammar->refs, def->name);
4521 if (prev == NULL) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004522 if (def->name != NULL) {
4523 if (ctxt->error != NULL)
4524 ctxt->error(ctxt->userData,
4525 "Error refs definitions '%s'\n",
4526 def->name);
4527 } else {
4528 if (ctxt->error != NULL)
4529 ctxt->error(ctxt->userData,
4530 "Error refs definitions\n");
4531 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004532 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004533 def = NULL;
4534 } else {
4535 def->nextHash = prev->nextHash;
4536 prev->nextHash = def;
4537 }
4538 }
4539 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004540 } else if (IS_RELAXNG(node, "data")) {
4541 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004542 } else if (IS_RELAXNG(node, "value")) {
4543 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004544 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004545 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004546 if (def == NULL)
4547 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004548 def->type = XML_RELAXNG_LIST;
Daniel Veillardd2298792003-02-14 16:54:11 +00004549 if (node->children == NULL) {
4550 if (ctxt->error != NULL)
4551 ctxt->error(ctxt->userData,
4552 "Element %s is empty\n", node->name);
4553 ctxt->nbErrors++;
4554 } else {
4555 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4556 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004557 } else if (IS_RELAXNG(node, "interleave")) {
4558 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004559 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004560 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004561 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004562 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004563 if (def == NULL)
4564 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004565 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004566 if (node->children != NULL) {
4567 if (ctxt->error != NULL)
4568 ctxt->error(ctxt->userData,
4569 "xmlRelaxNGParse: notAllowed element is not empty\n");
4570 ctxt->nbErrors++;
4571 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004572 } else if (IS_RELAXNG(node, "grammar")) {
4573 xmlRelaxNGGrammarPtr grammar, old;
4574 xmlRelaxNGGrammarPtr oldparent;
4575
Daniel Veillardc482e262003-02-26 14:48:48 +00004576#ifdef DEBUG_GRAMMAR
4577 xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n");
4578#endif
4579
Daniel Veillard419a7682003-02-03 23:22:49 +00004580 oldparent = ctxt->parentgrammar;
4581 old = ctxt->grammar;
4582 ctxt->parentgrammar = old;
4583 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4584 if (old != NULL) {
4585 ctxt->grammar = old;
4586 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004587#if 0
Daniel Veillard419a7682003-02-03 23:22:49 +00004588 if (grammar != NULL) {
4589 grammar->next = old->next;
4590 old->next = grammar;
4591 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004592#endif
Daniel Veillard419a7682003-02-03 23:22:49 +00004593 }
4594 if (grammar != NULL)
4595 def = grammar->start;
4596 else
4597 def = NULL;
4598 } else if (IS_RELAXNG(node, "parentRef")) {
4599 if (ctxt->parentgrammar == NULL) {
4600 if (ctxt->error != NULL)
4601 ctxt->error(ctxt->userData,
4602 "Use of parentRef without a parent grammar\n");
4603 ctxt->nbErrors++;
4604 return(NULL);
4605 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004606 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard419a7682003-02-03 23:22:49 +00004607 if (def == NULL)
4608 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004609 def->type = XML_RELAXNG_PARENTREF;
Daniel Veillard419a7682003-02-03 23:22:49 +00004610 def->name = xmlGetProp(node, BAD_CAST "name");
4611 if (def->name == NULL) {
4612 if (ctxt->error != NULL)
4613 ctxt->error(ctxt->userData,
4614 "parentRef has no name\n");
4615 ctxt->nbErrors++;
Daniel Veillardd2298792003-02-14 16:54:11 +00004616 } else {
4617 xmlRelaxNGNormExtSpace(def->name);
4618 if (xmlValidateNCName(def->name, 0)) {
4619 if (ctxt->error != NULL)
4620 ctxt->error(ctxt->userData,
4621 "parentRef name '%s' is not an NCName\n",
4622 def->name);
4623 ctxt->nbErrors++;
4624 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004625 }
4626 if (node->children != NULL) {
4627 if (ctxt->error != NULL)
4628 ctxt->error(ctxt->userData,
4629 "parentRef is not empty\n");
4630 ctxt->nbErrors++;
4631 }
4632 if (ctxt->parentgrammar->refs == NULL)
4633 ctxt->parentgrammar->refs = xmlHashCreate(10);
4634 if (ctxt->parentgrammar->refs == NULL) {
4635 if (ctxt->error != NULL)
4636 ctxt->error(ctxt->userData,
4637 "Could not create references hash\n");
4638 ctxt->nbErrors++;
4639 def = NULL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004640 } else if (def->name != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +00004641 int tmp;
4642
4643 tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4644 if (tmp < 0) {
4645 xmlRelaxNGDefinePtr prev;
4646
4647 prev = (xmlRelaxNGDefinePtr)
4648 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4649 if (prev == NULL) {
4650 if (ctxt->error != NULL)
4651 ctxt->error(ctxt->userData,
4652 "Internal error parentRef definitions '%s'\n",
4653 def->name);
4654 ctxt->nbErrors++;
4655 def = NULL;
4656 } else {
4657 def->nextHash = prev->nextHash;
4658 prev->nextHash = def;
4659 }
4660 }
4661 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004662 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004663 if (node->children == NULL) {
4664 if (ctxt->error != NULL)
4665 ctxt->error(ctxt->userData,
4666 "Mixed is empty\n");
4667 ctxt->nbErrors++;
4668 def = NULL;
4669 } else {
4670 def = xmlRelaxNGParseInterleave(ctxt, node);
4671 if (def != NULL) {
4672 xmlRelaxNGDefinePtr tmp;
4673
4674 if ((def->content != NULL) && (def->content->next != NULL)) {
4675 tmp = xmlRelaxNGNewDefine(ctxt, node);
4676 if (tmp != NULL) {
4677 tmp->type = XML_RELAXNG_GROUP;
4678 tmp->content = def->content;
4679 def->content = tmp;
4680 }
4681 }
4682
4683 tmp = xmlRelaxNGNewDefine(ctxt, node);
4684 if (tmp == NULL)
4685 return(def);
4686 tmp->type = XML_RELAXNG_TEXT;
4687 tmp->next = def->content;
4688 def->content = tmp;
4689 }
4690 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004691 } else {
4692 if (ctxt->error != NULL)
4693 ctxt->error(ctxt->userData,
4694 "Unexpected node %s is not a pattern\n",
4695 node->name);
4696 ctxt->nbErrors++;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004697 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004698 }
4699 return(def);
4700}
4701
4702/**
4703 * xmlRelaxNGParseAttribute:
4704 * @ctxt: a Relax-NG parser context
4705 * @node: the element node
4706 *
4707 * parse the content of a RelaxNG attribute node.
4708 *
4709 * Returns the definition pointer or NULL in case of error.
4710 */
4711static xmlRelaxNGDefinePtr
4712xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillardd2298792003-02-14 16:54:11 +00004713 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004714 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004715 int old_flags;
4716
Daniel Veillardfd573f12003-03-16 17:52:32 +00004717 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004718 if (ret == NULL)
4719 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004720 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004721 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004722 child = node->children;
4723 if (child == NULL) {
4724 if (ctxt->error != NULL)
4725 ctxt->error(ctxt->userData,
4726 "xmlRelaxNGParseattribute: attribute has no children\n");
4727 ctxt->nbErrors++;
4728 return(ret);
4729 }
4730 old_flags = ctxt->flags;
4731 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004732 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4733 if (cur != NULL)
4734 child = child->next;
4735
Daniel Veillardd2298792003-02-14 16:54:11 +00004736 if (child != NULL) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00004737 cur = xmlRelaxNGParsePattern(ctxt, child);
4738 if (cur != NULL) {
4739 switch (cur->type) {
4740 case XML_RELAXNG_EMPTY:
4741 case XML_RELAXNG_NOT_ALLOWED:
4742 case XML_RELAXNG_TEXT:
4743 case XML_RELAXNG_ELEMENT:
4744 case XML_RELAXNG_DATATYPE:
4745 case XML_RELAXNG_VALUE:
4746 case XML_RELAXNG_LIST:
4747 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00004748 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004749 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004750 case XML_RELAXNG_DEF:
4751 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004752 case XML_RELAXNG_ZEROORMORE:
4753 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004754 case XML_RELAXNG_CHOICE:
4755 case XML_RELAXNG_GROUP:
4756 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard77648bb2003-02-20 15:03:22 +00004757 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardd2298792003-02-14 16:54:11 +00004758 ret->content = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004759 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004760 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004761 case XML_RELAXNG_START:
Daniel Veillard8fe98712003-02-19 00:19:14 +00004762 case XML_RELAXNG_PARAM:
Daniel Veillard144fae12003-02-03 13:17:57 +00004763 case XML_RELAXNG_EXCEPT:
Daniel Veillardd2298792003-02-14 16:54:11 +00004764 if (ctxt->error != NULL)
4765 ctxt->error(ctxt->userData,
4766 "attribute has invalid content\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004767 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004768 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004769 case XML_RELAXNG_NOOP:
4770 TODO
4771 if (ctxt->error != NULL)
4772 ctxt->error(ctxt->userData,
4773 "Internal error, noop found\n");
4774 ctxt->nbErrors++;
4775 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004776 }
4777 }
4778 child = child->next;
4779 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004780 if (child != NULL) {
4781 if (ctxt->error != NULL)
4782 ctxt->error(ctxt->userData, "attribute has multiple children\n");
4783 ctxt->nbErrors++;
4784 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004785 ctxt->flags = old_flags;
4786 return(ret);
4787}
4788
4789/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004790 * xmlRelaxNGParseExceptNameClass:
4791 * @ctxt: a Relax-NG parser context
4792 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00004793 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004794 *
4795 * parse the content of a RelaxNG nameClass node.
4796 *
4797 * Returns the definition pointer or NULL in case of error.
4798 */
4799static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00004800xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
4801 xmlNodePtr node, int attr) {
4802 xmlRelaxNGDefinePtr ret, cur, last = NULL;
4803 xmlNodePtr child;
4804
Daniel Veillardd2298792003-02-14 16:54:11 +00004805 if (!IS_RELAXNG(node, "except")) {
4806 if (ctxt->error != NULL)
4807 ctxt->error(ctxt->userData,
4808 "Expecting an except node\n");
4809 ctxt->nbErrors++;
Daniel Veillard144fae12003-02-03 13:17:57 +00004810 return(NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004811 }
4812 if (node->next != NULL) {
4813 if (ctxt->error != NULL)
4814 ctxt->error(ctxt->userData,
4815 "exceptNameClass allows only a single except node\n");
4816 ctxt->nbErrors++;
4817 }
Daniel Veillard144fae12003-02-03 13:17:57 +00004818 if (node->children == NULL) {
4819 if (ctxt->error != NULL)
4820 ctxt->error(ctxt->userData,
4821 "except has no content\n");
4822 ctxt->nbErrors++;
4823 return(NULL);
4824 }
4825
Daniel Veillardfd573f12003-03-16 17:52:32 +00004826 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00004827 if (ret == NULL)
4828 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004829 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00004830 child = node->children;
4831 while (child != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004832 cur = xmlRelaxNGNewDefine(ctxt, child);
Daniel Veillard144fae12003-02-03 13:17:57 +00004833 if (cur == NULL)
4834 break;
4835 if (attr)
4836 cur->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004837 else
4838 cur->type = XML_RELAXNG_ELEMENT;
Daniel Veillard144fae12003-02-03 13:17:57 +00004839
Daniel Veillard419a7682003-02-03 23:22:49 +00004840 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard144fae12003-02-03 13:17:57 +00004841 if (last == NULL) {
4842 ret->content = cur;
4843 } else {
4844 last->next = cur;
4845 }
4846 last = cur;
4847 }
4848 child = child->next;
4849 }
4850
4851 return(ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004852}
4853
4854/**
4855 * xmlRelaxNGParseNameClass:
4856 * @ctxt: a Relax-NG parser context
4857 * @node: the nameClass node
4858 * @def: the current definition
4859 *
4860 * parse the content of a RelaxNG nameClass node.
4861 *
4862 * Returns the definition pointer or NULL in case of error.
4863 */
4864static xmlRelaxNGDefinePtr
4865xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
4866 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004867 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004868 xmlChar *val;
4869
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004870 ret = def;
4871 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
4872 (IS_RELAXNG(node, "nsName"))) {
4873 if ((def->type != XML_RELAXNG_ELEMENT) &&
4874 (def->type != XML_RELAXNG_ATTRIBUTE)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004875 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004876 if (ret == NULL)
4877 return(NULL);
4878 ret->parent = def;
4879 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
4880 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004881 else
4882 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004883 }
4884 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004885 if (IS_RELAXNG(node, "name")) {
4886 val = xmlNodeGetContent(node);
Daniel Veillardd2298792003-02-14 16:54:11 +00004887 xmlRelaxNGNormExtSpace(val);
4888 if (xmlValidateNCName(val, 0)) {
4889 if (ctxt->error != NULL) {
4890 if (node->parent != NULL)
4891 ctxt->error(ctxt->userData,
4892 "Element %s name '%s' is not an NCName\n",
4893 node->parent->name, val);
4894 else
4895 ctxt->error(ctxt->userData,
4896 "name '%s' is not an NCName\n",
4897 val);
4898 }
4899 ctxt->nbErrors++;
4900 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004901 ret->name = val;
4902 val = xmlGetProp(node, BAD_CAST "ns");
4903 ret->ns = val;
Daniel Veillard416589a2003-02-17 17:25:42 +00004904 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4905 (val != NULL) &&
4906 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
4907 ctxt->error(ctxt->userData,
4908 "Attribute with namespace '%s' is not allowed\n",
4909 val);
4910 ctxt->nbErrors++;
4911 }
4912 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4913 (val != NULL) &&
4914 (val[0] == 0) &&
4915 (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
4916 ctxt->error(ctxt->userData,
4917 "Attribute with QName 'xmlns' is not allowed\n",
4918 val);
4919 ctxt->nbErrors++;
4920 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004921 } else if (IS_RELAXNG(node, "anyName")) {
4922 ret->name = NULL;
4923 ret->ns = NULL;
4924 if (node->children != NULL) {
4925 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00004926 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
4927 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004928 }
4929 } else if (IS_RELAXNG(node, "nsName")) {
4930 ret->name = NULL;
4931 ret->ns = xmlGetProp(node, BAD_CAST "ns");
4932 if (ret->ns == NULL) {
4933 if (ctxt->error != NULL)
4934 ctxt->error(ctxt->userData,
4935 "nsName has no ns attribute\n");
4936 ctxt->nbErrors++;
4937 }
Daniel Veillard416589a2003-02-17 17:25:42 +00004938 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4939 (ret->ns != NULL) &&
4940 (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
4941 ctxt->error(ctxt->userData,
4942 "Attribute with namespace '%s' is not allowed\n",
4943 ret->ns);
4944 ctxt->nbErrors++;
4945 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004946 if (node->children != NULL) {
4947 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00004948 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
4949 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004950 }
4951 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004952 xmlNodePtr child;
4953 xmlRelaxNGDefinePtr last = NULL;
4954
4955 ret = xmlRelaxNGNewDefine(ctxt, node);
4956 if (ret == NULL)
4957 return(NULL);
4958 ret->parent = def;
4959 ret->type = XML_RELAXNG_CHOICE;
4960
Daniel Veillardd2298792003-02-14 16:54:11 +00004961 if (node->children == NULL) {
4962 if (ctxt->error != NULL)
4963 ctxt->error(ctxt->userData,
4964 "Element choice is empty\n");
4965 ctxt->nbErrors++;
4966 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004967
4968 child = node->children;
4969 while (child != NULL) {
4970 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
4971 if (tmp != NULL) {
4972 if (last == NULL) {
4973 last = ret->nameClass = tmp;
4974 } else {
4975 last->next = tmp;
4976 last = tmp;
4977 }
4978 }
4979 child = child->next;
4980 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004981 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004982 } else {
4983 if (ctxt->error != NULL)
4984 ctxt->error(ctxt->userData,
Daniel Veillardfd573f12003-03-16 17:52:32 +00004985 "expecting name, anyName, nsName or choice : got %s\n",
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004986 node->name);
4987 ctxt->nbErrors++;
4988 return(NULL);
4989 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004990 if (ret != def) {
4991 if (def->nameClass == NULL) {
4992 def->nameClass = ret;
4993 } else {
4994 tmp = def->nameClass;
4995 while (tmp->next != NULL) {
4996 tmp = tmp->next;
4997 }
4998 tmp->next = ret;
4999 }
5000 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005001 return(ret);
5002}
5003
5004/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005005 * xmlRelaxNGParseElement:
5006 * @ctxt: a Relax-NG parser context
5007 * @node: the element node
5008 *
5009 * parse the content of a RelaxNG element node.
5010 *
5011 * Returns the definition pointer or NULL in case of error.
5012 */
5013static xmlRelaxNGDefinePtr
5014xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
5015 xmlRelaxNGDefinePtr ret, cur, last;
5016 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005017 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005018
Daniel Veillardfd573f12003-03-16 17:52:32 +00005019 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005020 if (ret == NULL)
5021 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005022 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005023 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005024 child = node->children;
5025 if (child == NULL) {
5026 if (ctxt->error != NULL)
5027 ctxt->error(ctxt->userData,
5028 "xmlRelaxNGParseElement: element has no children\n");
5029 ctxt->nbErrors++;
5030 return(ret);
5031 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005032 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5033 if (cur != NULL)
5034 child = child->next;
5035
Daniel Veillard6eadf632003-01-23 18:29:16 +00005036 if (child == NULL) {
5037 if (ctxt->error != NULL)
5038 ctxt->error(ctxt->userData,
5039 "xmlRelaxNGParseElement: element has no content\n");
5040 ctxt->nbErrors++;
5041 return(ret);
5042 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005043 olddefine = ctxt->define;
5044 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005045 last = NULL;
5046 while (child != NULL) {
5047 cur = xmlRelaxNGParsePattern(ctxt, child);
5048 if (cur != NULL) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005049 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005050 switch (cur->type) {
5051 case XML_RELAXNG_EMPTY:
5052 case XML_RELAXNG_NOT_ALLOWED:
5053 case XML_RELAXNG_TEXT:
5054 case XML_RELAXNG_ELEMENT:
5055 case XML_RELAXNG_DATATYPE:
5056 case XML_RELAXNG_VALUE:
5057 case XML_RELAXNG_LIST:
5058 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00005059 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005060 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00005061 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00005062 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00005063 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00005064 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00005065 case XML_RELAXNG_CHOICE:
5066 case XML_RELAXNG_GROUP:
5067 case XML_RELAXNG_INTERLEAVE:
5068 if (last == NULL) {
5069 ret->content = last = cur;
5070 } else {
5071 if ((last->type == XML_RELAXNG_ELEMENT) &&
5072 (ret->content == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005073 ret->content = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005074 if (ret->content != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005075 ret->content->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005076 ret->content->content = last;
5077 } else {
5078 ret->content = last;
5079 }
5080 }
5081 last->next = cur;
5082 last = cur;
5083 }
5084 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005085 case XML_RELAXNG_ATTRIBUTE:
5086 cur->next = ret->attrs;
5087 ret->attrs = cur;
5088 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005089 case XML_RELAXNG_START:
Daniel Veillard8fe98712003-02-19 00:19:14 +00005090 case XML_RELAXNG_PARAM:
Daniel Veillard144fae12003-02-03 13:17:57 +00005091 case XML_RELAXNG_EXCEPT:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005092 TODO
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005093 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005094 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00005095 case XML_RELAXNG_NOOP:
5096 TODO
5097 if (ctxt->error != NULL)
5098 ctxt->error(ctxt->userData,
5099 "Internal error, noop found\n");
5100 ctxt->nbErrors++;
5101 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005102 }
5103 }
5104 child = child->next;
5105 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005106 ctxt->define = olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005107 return(ret);
5108}
5109
5110/**
5111 * xmlRelaxNGParsePatterns:
5112 * @ctxt: a Relax-NG parser context
5113 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005114 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005115 *
5116 * parse the content of a RelaxNG start node.
5117 *
5118 * Returns the definition pointer or NULL in case of error.
5119 */
5120static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005121xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
5122 int group) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005123 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005124
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005125 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005126 while (nodes != NULL) {
5127 if (IS_RELAXNG(nodes, "element")) {
5128 cur = xmlRelaxNGParseElement(ctxt, nodes);
5129 if (def == NULL) {
5130 def = last = cur;
5131 } else {
Daniel Veillard154877e2003-01-30 12:17:05 +00005132 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5133 (def == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005134 def = xmlRelaxNGNewDefine(ctxt, nodes);
5135 def->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005136 def->content = last;
5137 }
5138 last->next = cur;
5139 last = cur;
5140 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005141 cur->parent = parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005142 } else {
5143 cur = xmlRelaxNGParsePattern(ctxt, nodes);
Daniel Veillard419a7682003-02-03 23:22:49 +00005144 if (cur != NULL) {
5145 if (def == NULL) {
5146 def = last = cur;
5147 } else {
5148 last->next = cur;
5149 last = cur;
5150 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005151 }
5152 }
5153 nodes = nodes->next;
5154 }
5155 return(def);
5156}
5157
5158/**
5159 * xmlRelaxNGParseStart:
5160 * @ctxt: a Relax-NG parser context
5161 * @nodes: start children nodes
5162 *
5163 * parse the content of a RelaxNG start node.
5164 *
5165 * Returns 0 in case of success, -1 in case of error
5166 */
5167static int
5168xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
5169 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005170 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005171
Daniel Veillardd2298792003-02-14 16:54:11 +00005172 if (nodes == NULL) {
5173 if (ctxt->error != NULL)
5174 ctxt->error(ctxt->userData,
5175 "start has no children\n");
5176 ctxt->nbErrors++;
5177 return(-1);
5178 }
5179 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005180 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00005181 if (def == NULL)
5182 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005183 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00005184 if (nodes->children != NULL) {
5185 if (ctxt->error != NULL)
5186 ctxt->error(ctxt->userData, "element empty is not empty\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005187 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005188 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005189 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005190 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00005191 if (def == NULL)
5192 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005193 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardd2298792003-02-14 16:54:11 +00005194 if (nodes->children != NULL) {
5195 if (ctxt->error != NULL)
5196 ctxt->error(ctxt->userData,
5197 "element notAllowed is not empty\n");
5198 ctxt->nbErrors++;
5199 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005200 } else {
5201 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005202 }
5203 if (ctxt->grammar->start != NULL) {
5204 last = ctxt->grammar->start;
5205 while (last->next != NULL)
5206 last = last->next;
5207 last->next = def;
5208 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00005209 ctxt->grammar->start = def;
5210 }
5211 nodes = nodes->next;
5212 if (nodes != NULL) {
5213 if (ctxt->error != NULL)
5214 ctxt->error(ctxt->userData,
5215 "start more than one children\n");
5216 ctxt->nbErrors++;
5217 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005218 }
5219 return(ret);
5220}
5221
5222/**
5223 * xmlRelaxNGParseGrammarContent:
5224 * @ctxt: a Relax-NG parser context
5225 * @nodes: grammar children nodes
5226 *
5227 * parse the content of a RelaxNG grammar node.
5228 *
5229 * Returns 0 in case of success, -1 in case of error
5230 */
5231static int
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005232xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005233{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005234 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005235
5236 if (nodes == NULL) {
5237 if (ctxt->error != NULL)
5238 ctxt->error(ctxt->userData,
5239 "grammar has no children\n");
5240 ctxt->nbErrors++;
5241 return(-1);
5242 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005243 while (nodes != NULL) {
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005244 if (IS_RELAXNG(nodes, "start")) {
5245 if (nodes->children == NULL) {
5246 if (ctxt->error != NULL)
5247 ctxt->error(ctxt->userData,
Daniel Veillardd2298792003-02-14 16:54:11 +00005248 "start has no children\n");
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005249 ctxt->nbErrors++;
5250 } else {
5251 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5252 if (tmp != 0)
5253 ret = -1;
5254 }
5255 } else if (IS_RELAXNG(nodes, "define")) {
5256 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5257 if (tmp != 0)
5258 ret = -1;
5259 } else if (IS_RELAXNG(nodes, "include")) {
5260 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5261 if (tmp != 0)
5262 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005263 } else {
5264 if (ctxt->error != NULL)
5265 ctxt->error(ctxt->userData,
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005266 "grammar has unexpected child %s\n", nodes->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005267 ctxt->nbErrors++;
5268 ret = -1;
5269 }
5270 nodes = nodes->next;
5271 }
5272 return (ret);
5273}
5274
5275/**
5276 * xmlRelaxNGCheckReference:
5277 * @ref: the ref
5278 * @ctxt: a Relax-NG parser context
5279 * @name: the name associated to the defines
5280 *
5281 * Applies the 4.17. combine attribute rule for all the define
5282 * element of a given grammar using the same name.
5283 */
5284static void
5285xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
5286 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5287 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005288 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005289
5290 grammar = ctxt->grammar;
5291 if (grammar == NULL) {
5292 if (ctxt->error != NULL)
5293 ctxt->error(ctxt->userData,
5294 "Internal error: no grammar in CheckReference %s\n",
5295 name);
5296 ctxt->nbErrors++;
5297 return;
5298 }
5299 if (ref->content != NULL) {
5300 if (ctxt->error != NULL)
5301 ctxt->error(ctxt->userData,
5302 "Internal error: reference has content in CheckReference %s\n",
5303 name);
5304 ctxt->nbErrors++;
5305 return;
5306 }
5307 if (grammar->defs != NULL) {
5308 def = xmlHashLookup(grammar->defs, name);
5309 if (def != NULL) {
Daniel Veillard276be4a2003-01-24 01:03:34 +00005310 cur = ref;
5311 while (cur != NULL) {
5312 cur->content = def;
5313 cur = cur->nextHash;
5314 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005315 } else {
Daniel Veillardd4310742003-02-18 21:12:46 +00005316 if (ctxt->error != NULL)
5317 ctxt->error(ctxt->userData,
5318 "Reference %s has no matching definition\n",
5319 name);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005320 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005321 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005322 } else {
5323 if (ctxt->error != NULL)
5324 ctxt->error(ctxt->userData,
5325 "Reference %s has no matching definition\n",
5326 name);
5327 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005328 }
5329 /*
5330 * TODO: make a closure and verify there is no loop !
5331 */
5332}
5333
5334/**
5335 * xmlRelaxNGCheckCombine:
5336 * @define: the define(s) list
5337 * @ctxt: a Relax-NG parser context
5338 * @name: the name associated to the defines
5339 *
5340 * Applies the 4.17. combine attribute rule for all the define
5341 * element of a given grammar using the same name.
5342 */
5343static void
5344xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
5345 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5346 xmlChar *combine;
5347 int choiceOrInterleave = -1;
5348 int missing = 0;
5349 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5350
5351 if (define->nextHash == NULL)
5352 return;
5353 cur = define;
5354 while (cur != NULL) {
5355 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5356 if (combine != NULL) {
5357 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5358 if (choiceOrInterleave == -1)
5359 choiceOrInterleave = 1;
5360 else if (choiceOrInterleave == 0) {
5361 if (ctxt->error != NULL)
5362 ctxt->error(ctxt->userData,
5363 "Defines for %s use both 'choice' and 'interleave'\n",
5364 name);
5365 ctxt->nbErrors++;
5366 }
Daniel Veillard154877e2003-01-30 12:17:05 +00005367 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005368 if (choiceOrInterleave == -1)
5369 choiceOrInterleave = 0;
5370 else if (choiceOrInterleave == 1) {
5371 if (ctxt->error != NULL)
5372 ctxt->error(ctxt->userData,
5373 "Defines for %s use both 'choice' and 'interleave'\n",
5374 name);
5375 ctxt->nbErrors++;
5376 }
5377 } else {
5378 if (ctxt->error != NULL)
5379 ctxt->error(ctxt->userData,
5380 "Defines for %s use unknown combine value '%s''\n",
5381 name, combine);
5382 ctxt->nbErrors++;
5383 }
5384 xmlFree(combine);
5385 } else {
5386 if (missing == 0)
5387 missing = 1;
5388 else {
5389 if (ctxt->error != NULL)
5390 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005391 "Some defines for %s needs the combine attribute\n",
Daniel Veillard6eadf632003-01-23 18:29:16 +00005392 name);
5393 ctxt->nbErrors++;
5394 }
5395 }
5396
5397 cur = cur->nextHash;
5398 }
5399#ifdef DEBUG
5400 xmlGenericError(xmlGenericErrorContext,
5401 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5402 name, choiceOrInterleave);
5403#endif
5404 if (choiceOrInterleave == -1)
5405 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005406 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005407 if (cur == NULL)
5408 return;
5409 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005410 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005411 else
5412 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005413 tmp = define;
5414 last = NULL;
5415 while (tmp != NULL) {
5416 if (tmp->content != NULL) {
5417 if (tmp->content->next != NULL) {
5418 /*
5419 * we need first to create a wrapper.
5420 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00005421 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005422 if (tmp2 == NULL)
5423 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005424 tmp2->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005425 tmp2->content = tmp->content;
5426 } else {
5427 tmp2 = tmp->content;
5428 }
5429 if (last == NULL) {
5430 cur->content = tmp2;
5431 } else {
5432 last->next = tmp2;
5433 }
5434 last = tmp2;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005435 }
Daniel Veillard952379b2003-03-17 15:37:12 +00005436 tmp->content = cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005437 tmp = tmp->nextHash;
5438 }
5439 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005440 if (choiceOrInterleave == 0) {
5441 if (ctxt->interleaves == NULL)
5442 ctxt->interleaves = xmlHashCreate(10);
5443 if (ctxt->interleaves == NULL) {
5444 if (ctxt->error != NULL)
5445 ctxt->error(ctxt->userData,
5446 "Failed to create interleaves hash table\n");
5447 ctxt->nbErrors++;
5448 } else {
5449 char tmpname[32];
5450
5451 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5452 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5453 if (ctxt->error != NULL)
5454 ctxt->error(ctxt->userData,
5455 "Failed to add %s to hash table\n", tmpname);
5456 ctxt->nbErrors++;
5457 }
5458 }
5459 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005460}
5461
5462/**
5463 * xmlRelaxNGCombineStart:
5464 * @ctxt: a Relax-NG parser context
5465 * @grammar: the grammar
5466 *
5467 * Applies the 4.17. combine rule for all the start
5468 * element of a given grammar.
5469 */
5470static void
5471xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5472 xmlRelaxNGGrammarPtr grammar) {
5473 xmlRelaxNGDefinePtr starts;
5474 xmlChar *combine;
5475 int choiceOrInterleave = -1;
5476 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005477 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005478
Daniel Veillard2df2de22003-02-17 23:34:33 +00005479 starts = grammar->start;
5480 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard6eadf632003-01-23 18:29:16 +00005481 return;
5482 cur = starts;
5483 while (cur != NULL) {
Daniel Veillard2df2de22003-02-17 23:34:33 +00005484 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5485 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5486 combine = NULL;
5487 if (ctxt->error != NULL)
5488 ctxt->error(ctxt->userData,
5489 "Internal error: start element not found\n");
5490 ctxt->nbErrors++;
5491 } else {
5492 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5493 }
5494
Daniel Veillard6eadf632003-01-23 18:29:16 +00005495 if (combine != NULL) {
5496 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5497 if (choiceOrInterleave == -1)
5498 choiceOrInterleave = 1;
5499 else if (choiceOrInterleave == 0) {
5500 if (ctxt->error != NULL)
5501 ctxt->error(ctxt->userData,
5502 "<start> use both 'choice' and 'interleave'\n");
5503 ctxt->nbErrors++;
5504 }
Daniel Veillard2df2de22003-02-17 23:34:33 +00005505 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005506 if (choiceOrInterleave == -1)
5507 choiceOrInterleave = 0;
5508 else if (choiceOrInterleave == 1) {
5509 if (ctxt->error != NULL)
5510 ctxt->error(ctxt->userData,
5511 "<start> use both 'choice' and 'interleave'\n");
5512 ctxt->nbErrors++;
5513 }
5514 } else {
5515 if (ctxt->error != NULL)
5516 ctxt->error(ctxt->userData,
5517 "<start> uses unknown combine value '%s''\n", combine);
5518 ctxt->nbErrors++;
5519 }
5520 xmlFree(combine);
5521 } else {
5522 if (missing == 0)
5523 missing = 1;
5524 else {
5525 if (ctxt->error != NULL)
5526 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005527 "Some <start> element miss the combine attribute\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00005528 ctxt->nbErrors++;
5529 }
5530 }
5531
Daniel Veillard2df2de22003-02-17 23:34:33 +00005532 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005533 }
5534#ifdef DEBUG
5535 xmlGenericError(xmlGenericErrorContext,
5536 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5537 choiceOrInterleave);
5538#endif
5539 if (choiceOrInterleave == -1)
5540 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005541 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005542 if (cur == NULL)
5543 return;
5544 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005545 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005546 else
5547 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005548 cur->content = grammar->start;
5549 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005550 if (choiceOrInterleave == 0) {
5551 if (ctxt->interleaves == NULL)
5552 ctxt->interleaves = xmlHashCreate(10);
5553 if (ctxt->interleaves == NULL) {
5554 if (ctxt->error != NULL)
5555 ctxt->error(ctxt->userData,
5556 "Failed to create interleaves hash table\n");
5557 ctxt->nbErrors++;
5558 } else {
5559 char tmpname[32];
5560
5561 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5562 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5563 if (ctxt->error != NULL)
5564 ctxt->error(ctxt->userData,
5565 "Failed to add %s to hash table\n", tmpname);
5566 ctxt->nbErrors++;
5567 }
5568 }
5569 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005570}
5571
5572/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005573 * xmlRelaxNGCheckCycles:
5574 * @ctxt: a Relax-NG parser context
5575 * @nodes: grammar children nodes
5576 * @depth: the counter
5577 *
5578 * Check for cycles.
5579 *
5580 * Returns 0 if check passed, and -1 in case of error
5581 */
5582static int
5583xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5584 xmlRelaxNGDefinePtr cur, int depth) {
5585 int ret = 0;
5586
5587 while ((ret == 0) && (cur != NULL)) {
5588 if ((cur->type == XML_RELAXNG_REF) ||
5589 (cur->type == XML_RELAXNG_PARENTREF)) {
5590 if (cur->depth == -1) {
5591 cur->depth = depth;
5592 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5593 cur->depth = -2;
5594 } else if (depth == cur->depth) {
5595 if (ctxt->error != NULL)
5596 ctxt->error(ctxt->userData,
5597 "Detected a cycle in %s references\n", cur->name);
5598 ctxt->nbErrors++;
5599 return(-1);
5600 }
5601 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5602 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5603 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005604 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
Daniel Veillardd4310742003-02-18 21:12:46 +00005605 }
5606 cur = cur->next;
5607 }
5608 return(ret);
5609}
5610
5611/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005612 * xmlRelaxNGTryUnlink:
5613 * @ctxt: a Relax-NG parser context
5614 * @cur: the definition to unlink
5615 * @parent: the parent definition
5616 * @prev: the previous sibling definition
5617 *
5618 * Try to unlink a definition. If not possble make it a NOOP
5619 *
5620 * Returns the new prev definition
5621 */
5622static xmlRelaxNGDefinePtr
5623xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5624 xmlRelaxNGDefinePtr cur,
5625 xmlRelaxNGDefinePtr parent,
5626 xmlRelaxNGDefinePtr prev) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005627 if (prev != NULL) {
5628 prev->next = cur->next;
5629 } else {
5630 if (parent != NULL) {
5631 if (parent->content == cur)
5632 parent->content = cur->next;
5633 else if (parent->attrs == cur)
5634 parent->attrs = cur->next;
5635 else if (parent->nameClass == cur)
5636 parent->nameClass = cur->next;
5637 } else {
5638 cur->type = XML_RELAXNG_NOOP;
5639 prev = cur;
5640 }
5641 }
5642 return(prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005643}
5644
5645/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005646 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005647 * @ctxt: a Relax-NG parser context
5648 * @nodes: grammar children nodes
5649 *
5650 * Check for simplification of empty and notAllowed
5651 */
5652static void
5653xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5654 xmlRelaxNGDefinePtr cur,
5655 xmlRelaxNGDefinePtr parent) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005656 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005657
Daniel Veillardfd573f12003-03-16 17:52:32 +00005658 while (cur != NULL) {
5659 if ((cur->type == XML_RELAXNG_REF) ||
5660 (cur->type == XML_RELAXNG_PARENTREF)) {
5661 if (cur->depth != -3) {
5662 cur->depth = -3;
5663 xmlRelaxNGSimplify(ctxt, cur->content, cur);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005664 }
5665 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005666 cur->parent = parent;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005667 if ((parent != NULL) &&
5668 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5669 (parent->type == XML_RELAXNG_LIST) ||
5670 (parent->type == XML_RELAXNG_GROUP) ||
5671 (parent->type == XML_RELAXNG_INTERLEAVE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00005672 (parent->type == XML_RELAXNG_ONEORMORE) ||
5673 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005674 parent->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005675 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005676 }
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005677 if ((parent != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00005678 (parent->type == XML_RELAXNG_CHOICE)) {
5679 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5680 } else
5681 prev = cur;
5682 } else if (cur->type == XML_RELAXNG_EMPTY){
5683 cur->parent = parent;
5684 if ((parent != NULL) &&
5685 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5686 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005687 parent->type = XML_RELAXNG_EMPTY;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005688 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005689 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005690 if ((parent != NULL) &&
5691 ((parent->type == XML_RELAXNG_GROUP) ||
5692 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5693 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5694 } else
5695 prev = cur;
5696 } else {
5697 cur->parent = parent;
5698 if (cur->content != NULL)
5699 xmlRelaxNGSimplify(ctxt, cur->content, cur);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005700 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
Daniel Veillardfd573f12003-03-16 17:52:32 +00005701 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5702 if (cur->nameClass != NULL)
5703 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5704 /*
5705 * This may result in a simplification
5706 */
5707 if ((cur->type == XML_RELAXNG_GROUP) ||
5708 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5709 if (cur->content == NULL)
5710 cur->type = XML_RELAXNG_EMPTY;
5711 else if (cur->content->next == NULL) {
5712 if ((parent == NULL) && (prev == NULL)) {
5713 cur->type = XML_RELAXNG_NOOP;
5714 } else if (prev == NULL) {
5715 parent->content = cur->content;
5716 cur->content->next = cur->next;
5717 cur = cur->content;
5718 } else {
5719 cur->content->next = cur->next;
5720 prev->next = cur->content;
5721 cur = cur->content;
5722 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005723 }
5724 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005725 /*
5726 * the current node may have been transformed back
5727 */
5728 if ((cur->type == XML_RELAXNG_EXCEPT) &&
5729 (cur->content != NULL) &&
5730 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
5731 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5732 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5733 if ((parent != NULL) &&
5734 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5735 (parent->type == XML_RELAXNG_LIST) ||
5736 (parent->type == XML_RELAXNG_GROUP) ||
5737 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5738 (parent->type == XML_RELAXNG_ONEORMORE) ||
5739 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5740 parent->type = XML_RELAXNG_NOT_ALLOWED;
5741 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005742 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005743 if ((parent != NULL) &&
5744 (parent->type == XML_RELAXNG_CHOICE)) {
5745 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5746 } else
5747 prev = cur;
5748 } else if (cur->type == XML_RELAXNG_EMPTY){
5749 if ((parent != NULL) &&
5750 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5751 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5752 parent->type = XML_RELAXNG_EMPTY;
5753 break;
5754 }
5755 if ((parent != NULL) &&
5756 ((parent->type == XML_RELAXNG_GROUP) ||
5757 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5758 (parent->type == XML_RELAXNG_CHOICE))) {
5759 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5760 } else
5761 prev = cur;
5762 } else {
5763 prev = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005764 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005765 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005766 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005767 }
5768}
5769
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005770/**
5771 * xmlRelaxNGGroupContentType:
5772 * @ct1: the first content type
5773 * @ct2: the second content type
5774 *
5775 * Try to group 2 content types
5776 *
5777 * Returns the content type
5778 */
5779static xmlRelaxNGContentType
5780xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
5781 xmlRelaxNGContentType ct2) {
5782 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
5783 (ct2 == XML_RELAXNG_CONTENT_ERROR))
5784 return(XML_RELAXNG_CONTENT_ERROR);
5785 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
5786 return(ct2);
5787 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
5788 return(ct1);
5789 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
5790 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
5791 return(XML_RELAXNG_CONTENT_COMPLEX);
5792 return(XML_RELAXNG_CONTENT_ERROR);
5793}
5794
5795/**
5796 * xmlRelaxNGMaxContentType:
5797 * @ct1: the first content type
5798 * @ct2: the second content type
5799 *
5800 * Compute the max content-type
5801 *
5802 * Returns the content type
5803 */
5804static xmlRelaxNGContentType
5805xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
5806 xmlRelaxNGContentType ct2) {
5807 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
5808 (ct2 == XML_RELAXNG_CONTENT_ERROR))
5809 return(XML_RELAXNG_CONTENT_ERROR);
5810 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
5811 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
5812 return(XML_RELAXNG_CONTENT_SIMPLE);
5813 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
5814 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
5815 return(XML_RELAXNG_CONTENT_COMPLEX);
5816 return(XML_RELAXNG_CONTENT_EMPTY);
5817}
Daniel Veillard77648bb2003-02-20 15:03:22 +00005818
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005819/**
5820 * xmlRelaxNGCheckRules:
5821 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005822 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00005823 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00005824 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005825 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005826 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005827 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005828 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005829 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005830static xmlRelaxNGContentType
Daniel Veillardfd573f12003-03-16 17:52:32 +00005831xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
5832 xmlRelaxNGDefinePtr cur, int flags,
5833 xmlRelaxNGType ptype) {
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005834 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005835 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005836
Daniel Veillardfd573f12003-03-16 17:52:32 +00005837 while (cur != NULL) {
5838 ret = XML_RELAXNG_CONTENT_EMPTY;
5839 if ((cur->type == XML_RELAXNG_REF) ||
5840 (cur->type == XML_RELAXNG_PARENTREF)) {
5841 if (flags & XML_RELAXNG_IN_LIST) {
5842 if (ctxt->error != NULL)
5843 ctxt->error(ctxt->userData,
5844 "Found forbidden pattern list//ref\n");
5845 ctxt->nbErrors++;
5846 }
5847 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5848 if (ctxt->error != NULL)
5849 ctxt->error(ctxt->userData,
5850 "Found forbidden pattern data/except//ref\n");
5851 ctxt->nbErrors++;
5852 }
5853 if (cur->depth > -4) {
5854 cur->depth = -4;
5855 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
5856 flags, cur->type);
5857 cur->depth = ret - 15 ;
5858 } else if (cur->depth == -4) {
5859 ret = XML_RELAXNG_CONTENT_COMPLEX;
5860 } else {
5861 ret = (xmlRelaxNGContentType) cur->depth + 15;
5862 }
5863 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5864 /*
5865 * The 7.3 Attribute derivation rule for groups is plugged there
5866 */
5867 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
5868 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5869 if (ctxt->error != NULL)
5870 ctxt->error(ctxt->userData,
5871 "Found forbidden pattern data/except//element(ref)\n");
5872 ctxt->nbErrors++;
5873 }
5874 if (flags & XML_RELAXNG_IN_LIST) {
5875 if (ctxt->error != NULL)
5876 ctxt->error(ctxt->userData,
5877 "Found forbidden pattern list//element(ref)\n");
5878 ctxt->nbErrors++;
5879 }
5880 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5881 if (ctxt->error != NULL)
5882 ctxt->error(ctxt->userData,
5883 "Found forbidden pattern attribute//element(ref)\n");
5884 ctxt->nbErrors++;
5885 }
5886 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5887 if (ctxt->error != NULL)
5888 ctxt->error(ctxt->userData,
Daniel Veillard463a5472003-02-27 21:30:32 +00005889 "Found forbidden pattern attribute//element(ref)\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005890 ctxt->nbErrors++;
5891 }
5892 /*
5893 * reset since in the simple form elements are only child
5894 * of grammar/define
5895 */
5896 nflags = 0;
5897 ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
5898 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
5899 if (ctxt->error != NULL)
5900 ctxt->error(ctxt->userData,
5901 "Element %s attributes have a content type error\n",
5902 cur->name);
5903 ctxt->nbErrors++;
5904 }
5905 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5906 if (ret == XML_RELAXNG_CONTENT_ERROR) {
5907 if (ctxt->error != NULL)
5908 ctxt->error(ctxt->userData,
5909 "Element %s has a content type error\n",
5910 cur->name);
5911 ctxt->nbErrors++;
5912 } else {
5913 ret = XML_RELAXNG_CONTENT_COMPLEX;
5914 }
5915 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
5916 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5917 if (ctxt->error != NULL)
5918 ctxt->error(ctxt->userData,
5919 "Found forbidden pattern attribute//attribute\n");
5920 ctxt->nbErrors++;
5921 }
5922 if (flags & XML_RELAXNG_IN_LIST) {
5923 if (ctxt->error != NULL)
5924 ctxt->error(ctxt->userData,
5925 "Found forbidden pattern list//attribute\n");
5926 ctxt->nbErrors++;
5927 }
5928 if (flags & XML_RELAXNG_IN_OOMGROUP) {
5929 if (ctxt->error != NULL)
5930 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005931 "Found forbidden pattern oneOrMore//group//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005932 ctxt->nbErrors++;
5933 }
5934 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
5935 if (ctxt->error != NULL)
5936 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005937 "Found forbidden pattern oneOrMore//interleave//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005938 ctxt->nbErrors++;
5939 }
5940 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5941 if (ctxt->error != NULL)
5942 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005943 "Found forbidden pattern data/except//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005944 ctxt->nbErrors++;
5945 }
5946 if (flags & XML_RELAXNG_IN_START) {
5947 if (ctxt->error != NULL)
5948 ctxt->error(ctxt->userData,
5949 "Found forbidden pattern start//attribute\n");
5950 ctxt->nbErrors++;
5951 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005952 if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) {
5953 if (cur->ns == NULL) {
5954 if (ctxt->error != NULL)
5955 ctxt->error(ctxt->userData,
5956 "Found anyName attribute without oneOrMore ancestor\n");
5957 ctxt->nbErrors++;
5958 } else {
5959 if (ctxt->error != NULL)
5960 ctxt->error(ctxt->userData,
5961 "Found nsName attribute without oneOrMore ancestor\n");
5962 ctxt->nbErrors++;
5963 }
Daniel Veillard77648bb2003-02-20 15:03:22 +00005964 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005965 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
5966 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5967 ret = XML_RELAXNG_CONTENT_EMPTY;
5968 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
5969 (cur->type == XML_RELAXNG_ZEROORMORE)) {
5970 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5971 if (ctxt->error != NULL)
5972 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005973 "Found forbidden pattern data/except//oneOrMore\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005974 ctxt->nbErrors++;
5975 }
5976 if (flags & XML_RELAXNG_IN_START) {
5977 if (ctxt->error != NULL)
5978 ctxt->error(ctxt->userData,
5979 "Found forbidden pattern start//oneOrMore\n");
5980 ctxt->nbErrors++;
5981 }
5982 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
5983 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5984 ret = xmlRelaxNGGroupContentType(ret, ret);
5985 } else if (cur->type == XML_RELAXNG_LIST) {
5986 if (flags & XML_RELAXNG_IN_LIST) {
5987 if (ctxt->error != NULL)
5988 ctxt->error(ctxt->userData,
5989 "Found forbidden pattern list//list\n");
5990 ctxt->nbErrors++;
5991 }
5992 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5993 if (ctxt->error != NULL)
5994 ctxt->error(ctxt->userData,
5995 "Found forbidden pattern data/except//list\n");
5996 ctxt->nbErrors++;
5997 }
5998 if (flags & XML_RELAXNG_IN_START) {
5999 if (ctxt->error != NULL)
6000 ctxt->error(ctxt->userData,
6001 "Found forbidden pattern start//list\n");
6002 ctxt->nbErrors++;
6003 }
6004 nflags = flags | XML_RELAXNG_IN_LIST;
6005 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6006 } else if (cur->type == XML_RELAXNG_GROUP) {
6007 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6008 if (ctxt->error != NULL)
6009 ctxt->error(ctxt->userData,
6010 "Found forbidden pattern data/except//group\n");
6011 ctxt->nbErrors++;
6012 }
6013 if (flags & XML_RELAXNG_IN_START) {
6014 if (ctxt->error != NULL)
6015 ctxt->error(ctxt->userData,
6016 "Found forbidden pattern start//group\n");
6017 ctxt->nbErrors++;
6018 }
6019 if (flags & XML_RELAXNG_IN_ONEORMORE)
6020 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6021 else
6022 nflags = flags;
6023 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6024 /*
6025 * The 7.3 Attribute derivation rule for groups is plugged there
6026 */
6027 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6028 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6029 if (flags & XML_RELAXNG_IN_LIST) {
6030 if (ctxt->error != NULL)
6031 ctxt->error(ctxt->userData,
6032 "Found forbidden pattern list//interleave\n");
6033 ctxt->nbErrors++;
6034 }
6035 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6036 if (ctxt->error != NULL)
6037 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00006038 "Found forbidden pattern data/except//interleave\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006039 ctxt->nbErrors++;
6040 }
6041 if (flags & XML_RELAXNG_IN_START) {
6042 if (ctxt->error != NULL)
6043 ctxt->error(ctxt->userData,
6044 "Found forbidden pattern start//interleave\n");
6045 ctxt->nbErrors++;
6046 }
6047 if (flags & XML_RELAXNG_IN_ONEORMORE)
6048 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6049 else
6050 nflags = flags;
6051 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6052 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6053 if ((cur->parent != NULL) &&
6054 (cur->parent->type == XML_RELAXNG_DATATYPE))
6055 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6056 else
6057 nflags = flags;
6058 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6059 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6060 if (flags & XML_RELAXNG_IN_START) {
6061 if (ctxt->error != NULL)
6062 ctxt->error(ctxt->userData,
6063 "Found forbidden pattern start//data\n");
6064 ctxt->nbErrors++;
6065 }
6066 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6067 ret = XML_RELAXNG_CONTENT_SIMPLE;
6068 } else if (cur->type == XML_RELAXNG_VALUE) {
6069 if (flags & XML_RELAXNG_IN_START) {
6070 if (ctxt->error != NULL)
6071 ctxt->error(ctxt->userData,
6072 "Found forbidden pattern start//value\n");
6073 ctxt->nbErrors++;
6074 }
6075 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6076 ret = XML_RELAXNG_CONTENT_SIMPLE;
6077 } else if (cur->type == XML_RELAXNG_TEXT) {
6078 if (flags & XML_RELAXNG_IN_LIST) {
6079 if (ctxt->error != NULL)
6080 ctxt->error(ctxt->userData,
6081 "Found forbidden pattern list//text\n");
6082 ctxt->nbErrors++;
6083 }
6084 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6085 if (ctxt->error != NULL)
6086 ctxt->error(ctxt->userData,
6087 "Found forbidden pattern data/except//text\n");
6088 ctxt->nbErrors++;
6089 }
6090 if (flags & XML_RELAXNG_IN_START) {
6091 if (ctxt->error != NULL)
6092 ctxt->error(ctxt->userData,
6093 "Found forbidden pattern start//text\n");
6094 ctxt->nbErrors++;
6095 }
6096 ret = XML_RELAXNG_CONTENT_COMPLEX;
6097 } else if (cur->type == XML_RELAXNG_EMPTY) {
6098 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6099 if (ctxt->error != NULL)
6100 ctxt->error(ctxt->userData,
6101 "Found forbidden pattern data/except//empty\n");
6102 ctxt->nbErrors++;
6103 }
6104 if (flags & XML_RELAXNG_IN_START) {
6105 if (ctxt->error != NULL)
6106 ctxt->error(ctxt->userData,
6107 "Found forbidden pattern start//empty\n");
6108 ctxt->nbErrors++;
6109 }
6110 ret = XML_RELAXNG_CONTENT_EMPTY;
6111 } else if (cur->type == XML_RELAXNG_CHOICE) {
6112 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6113 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6114 } else {
6115 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6116 }
6117 cur = cur->next;
6118 if (ptype == XML_RELAXNG_GROUP) {
6119 val = xmlRelaxNGGroupContentType(val, ret);
6120 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
6121 tmp = xmlRelaxNGGroupContentType(val, ret);
6122 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6123 tmp = xmlRelaxNGMaxContentType(val, ret);
6124 } else if (ptype == XML_RELAXNG_CHOICE) {
6125 val = xmlRelaxNGMaxContentType(val, ret);
6126 } else if (ptype == XML_RELAXNG_LIST) {
6127 val = XML_RELAXNG_CONTENT_SIMPLE;
6128 } else if (ptype == XML_RELAXNG_EXCEPT) {
6129 if (ret == XML_RELAXNG_CONTENT_ERROR)
6130 val = XML_RELAXNG_CONTENT_ERROR;
6131 else
6132 val = XML_RELAXNG_CONTENT_SIMPLE;
6133 } else {
6134 val = xmlRelaxNGGroupContentType(val, ret);
6135 }
6136
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006137 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006138 return(val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006139}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006140
6141/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006142 * xmlRelaxNGParseGrammar:
6143 * @ctxt: a Relax-NG parser context
6144 * @nodes: grammar children nodes
6145 *
6146 * parse a Relax-NG <grammar> node
6147 *
6148 * Returns the internal xmlRelaxNGGrammarPtr built or
6149 * NULL in case of error
6150 */
6151static xmlRelaxNGGrammarPtr
6152xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
6153 xmlRelaxNGGrammarPtr ret, tmp, old;
6154
Daniel Veillardc482e262003-02-26 14:48:48 +00006155#ifdef DEBUG_GRAMMAR
6156 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6157#endif
6158
Daniel Veillard6eadf632003-01-23 18:29:16 +00006159 ret = xmlRelaxNGNewGrammar(ctxt);
6160 if (ret == NULL)
6161 return(NULL);
6162
6163 /*
6164 * Link the new grammar in the tree
6165 */
6166 ret->parent = ctxt->grammar;
6167 if (ctxt->grammar != NULL) {
6168 tmp = ctxt->grammar->children;
6169 if (tmp == NULL) {
6170 ctxt->grammar->children = ret;
6171 } else {
6172 while (tmp->next != NULL)
6173 tmp = tmp->next;
6174 tmp->next = ret;
6175 }
6176 }
6177
6178 old = ctxt->grammar;
6179 ctxt->grammar = ret;
6180 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6181 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006182 if (ctxt->grammar == NULL) {
6183 if (ctxt->error != NULL)
6184 ctxt->error(ctxt->userData,
6185 "Failed to parse <grammar> content\n");
6186 ctxt->nbErrors++;
6187 } else if (ctxt->grammar->start == NULL) {
6188 if (ctxt->error != NULL)
6189 ctxt->error(ctxt->userData,
6190 "Element <grammar> has no <start>\n");
6191 ctxt->nbErrors++;
6192 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006193
6194 /*
6195 * Apply 4.17 mergingd rules to defines and starts
6196 */
6197 xmlRelaxNGCombineStart(ctxt, ret);
6198 if (ret->defs != NULL) {
6199 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6200 ctxt);
6201 }
6202
6203 /*
6204 * link together defines and refs in this grammar
6205 */
6206 if (ret->refs != NULL) {
6207 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6208 ctxt);
6209 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006210
Daniel Veillard6eadf632003-01-23 18:29:16 +00006211 ctxt->grammar = old;
6212 return(ret);
6213}
6214
6215/**
6216 * xmlRelaxNGParseDocument:
6217 * @ctxt: a Relax-NG parser context
6218 * @node: the root node of the RelaxNG schema
6219 *
6220 * parse a Relax-NG definition resource and build an internal
6221 * xmlRelaxNG struture which can be used to validate instances.
6222 *
6223 * Returns the internal XML RelaxNG structure built or
6224 * NULL in case of error
6225 */
6226static xmlRelaxNGPtr
6227xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6228 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006229 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006230 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006231
6232 if ((ctxt == NULL) || (node == NULL))
6233 return (NULL);
6234
6235 schema = xmlRelaxNGNewRelaxNG(ctxt);
6236 if (schema == NULL)
6237 return(NULL);
6238
Daniel Veillard276be4a2003-01-24 01:03:34 +00006239 olddefine = ctxt->define;
6240 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006241 if (IS_RELAXNG(node, "grammar")) {
6242 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
6243 } else {
Daniel Veillardc482e262003-02-26 14:48:48 +00006244 xmlRelaxNGGrammarPtr tmp, ret;
6245
6246 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006247 if (schema->topgrammar == NULL) {
6248 return(schema);
6249 }
Daniel Veillardc482e262003-02-26 14:48:48 +00006250 /*
6251 * Link the new grammar in the tree
6252 */
6253 ret->parent = ctxt->grammar;
6254 if (ctxt->grammar != NULL) {
6255 tmp = ctxt->grammar->children;
6256 if (tmp == NULL) {
6257 ctxt->grammar->children = ret;
6258 } else {
6259 while (tmp->next != NULL)
6260 tmp = tmp->next;
6261 tmp->next = ret;
6262 }
6263 }
Daniel Veillarde431a272003-01-29 23:02:33 +00006264 old = ctxt->grammar;
Daniel Veillardc482e262003-02-26 14:48:48 +00006265 ctxt->grammar = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006266 xmlRelaxNGParseStart(ctxt, node);
Daniel Veillarde431a272003-01-29 23:02:33 +00006267 if (old != NULL)
6268 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006269 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006270 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006271 if (schema->topgrammar->start != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00006272 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006273 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00006274 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6275 while ((schema->topgrammar->start != NULL) &&
6276 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6277 (schema->topgrammar->start->next != NULL))
6278 schema->topgrammar->start = schema->topgrammar->start->content;
6279 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6280 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006281 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006282 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006283
6284#ifdef DEBUG
6285 if (schema == NULL)
6286 xmlGenericError(xmlGenericErrorContext,
6287 "xmlRelaxNGParseDocument() failed\n");
6288#endif
6289
6290 return (schema);
6291}
6292
6293/************************************************************************
6294 * *
6295 * Reading RelaxNGs *
6296 * *
6297 ************************************************************************/
6298
6299/**
6300 * xmlRelaxNGNewParserCtxt:
6301 * @URL: the location of the schema
6302 *
6303 * Create an XML RelaxNGs parse context for that file/resource expected
6304 * to contain an XML RelaxNGs file.
6305 *
6306 * Returns the parser context or NULL in case of error
6307 */
6308xmlRelaxNGParserCtxtPtr
6309xmlRelaxNGNewParserCtxt(const char *URL) {
6310 xmlRelaxNGParserCtxtPtr ret;
6311
6312 if (URL == NULL)
6313 return(NULL);
6314
6315 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6316 if (ret == NULL) {
6317 xmlGenericError(xmlGenericErrorContext,
6318 "Failed to allocate new schama parser context for %s\n", URL);
6319 return (NULL);
6320 }
6321 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6322 ret->URL = xmlStrdup((const xmlChar *)URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006323 ret->error = xmlGenericError;
6324 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006325 return (ret);
6326}
6327
6328/**
6329 * xmlRelaxNGNewMemParserCtxt:
6330 * @buffer: a pointer to a char array containing the schemas
6331 * @size: the size of the array
6332 *
6333 * Create an XML RelaxNGs parse context for that memory buffer expected
6334 * to contain an XML RelaxNGs file.
6335 *
6336 * Returns the parser context or NULL in case of error
6337 */
6338xmlRelaxNGParserCtxtPtr
6339xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) {
6340 xmlRelaxNGParserCtxtPtr ret;
6341
6342 if ((buffer == NULL) || (size <= 0))
6343 return(NULL);
6344
6345 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6346 if (ret == NULL) {
6347 xmlGenericError(xmlGenericErrorContext,
6348 "Failed to allocate new schama parser context\n");
6349 return (NULL);
6350 }
6351 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6352 ret->buffer = buffer;
6353 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006354 ret->error = xmlGenericError;
6355 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006356 return (ret);
6357}
6358
6359/**
6360 * xmlRelaxNGFreeParserCtxt:
6361 * @ctxt: the schema parser context
6362 *
6363 * Free the resources associated to the schema parser context
6364 */
6365void
6366xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) {
6367 if (ctxt == NULL)
6368 return;
6369 if (ctxt->URL != NULL)
6370 xmlFree(ctxt->URL);
6371 if (ctxt->doc != NULL)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006372 xmlFreeDoc(ctxt->document);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006373 if (ctxt->interleaves != NULL)
6374 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006375 if (ctxt->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006376 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006377 if (ctxt->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006378 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006379 if (ctxt->docTab != NULL)
6380 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006381 if (ctxt->incTab != NULL)
6382 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006383 if (ctxt->defTab != NULL) {
6384 int i;
6385
6386 for (i = 0;i < ctxt->defNr;i++)
6387 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6388 xmlFree(ctxt->defTab);
6389 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006390 xmlFree(ctxt);
6391}
6392
Daniel Veillard6eadf632003-01-23 18:29:16 +00006393/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006394 * xmlRelaxNGNormExtSpace:
6395 * @value: a value
6396 *
6397 * Removes the leading and ending spaces of the value
6398 * The string is modified "in situ"
6399 */
6400static void
6401xmlRelaxNGNormExtSpace(xmlChar *value) {
6402 xmlChar *start = value;
6403 xmlChar *cur = value;
6404 if (value == NULL)
6405 return;
6406
6407 while (IS_BLANK(*cur)) cur++;
6408 if (cur == start) {
6409 do {
6410 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
6411 if (*cur == 0)
6412 return;
6413 start = cur;
6414 while (IS_BLANK(*cur)) cur++;
6415 if (*cur == 0) {
6416 *start = 0;
6417 return;
6418 }
6419 } while (1);
6420 } else {
6421 do {
6422 while ((*cur != 0) && (!IS_BLANK(*cur)))
6423 *start++ = *cur++;
6424 if (*cur == 0) {
6425 *start = 0;
6426 return;
6427 }
6428 /* don't try to normalize the inner spaces */
6429 while (IS_BLANK(*cur)) cur++;
6430 *start++ = *cur++;
6431 if (*cur == 0) {
6432 *start = 0;
6433 return;
6434 }
6435 } while (1);
6436 }
6437}
6438
6439/**
6440 * xmlRelaxNGCheckAttributes:
6441 * @ctxt: a Relax-NG parser context
6442 * @node: a Relax-NG node
6443 *
6444 * Check all the attributes on the given node
6445 */
6446static void
6447xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6448 xmlAttrPtr cur, next;
6449
6450 cur = node->properties;
6451 while (cur != NULL) {
6452 next = cur->next;
6453 if ((cur->ns == NULL) ||
6454 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6455 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6456 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6457 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6458 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6459 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
Daniel Veillard2df2de22003-02-17 23:34:33 +00006460 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
Daniel Veillardd2298792003-02-14 16:54:11 +00006461 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6462 if (ctxt->error != NULL)
6463 ctxt->error(ctxt->userData,
6464 "Attribute %s is not allowed on %s\n",
6465 cur->name, node->name);
6466 ctxt->nbErrors++;
6467 }
6468 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6469 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6470 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6471 if (ctxt->error != NULL)
6472 ctxt->error(ctxt->userData,
6473 "Attribute %s is not allowed on %s\n",
6474 cur->name, node->name);
6475 ctxt->nbErrors++;
6476 }
6477 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6478 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6479 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6480 if (ctxt->error != NULL)
6481 ctxt->error(ctxt->userData,
6482 "Attribute %s is not allowed on %s\n",
6483 cur->name, node->name);
6484 ctxt->nbErrors++;
6485 }
6486 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6487 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6488 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6489 if (ctxt->error != NULL)
6490 ctxt->error(ctxt->userData,
6491 "Attribute %s is not allowed on %s\n",
6492 cur->name, node->name);
6493 ctxt->nbErrors++;
6494 }
6495 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6496 xmlChar *val;
6497 xmlURIPtr uri;
6498
6499 val = xmlNodeListGetString(node->doc, cur->children, 1);
6500 if (val != NULL) {
6501 if (val[0] != 0) {
6502 uri = xmlParseURI((const char *) val);
6503 if (uri == NULL) {
6504 if (ctxt->error != NULL)
6505 ctxt->error(ctxt->userData,
6506 "Attribute %s contains invalid URI %s\n",
6507 cur->name, val);
6508 ctxt->nbErrors++;
6509 } else {
6510 if (uri->scheme == NULL) {
6511 if (ctxt->error != NULL)
6512 ctxt->error(ctxt->userData,
6513 "Attribute %s URI %s is not absolute\n",
6514 cur->name, val);
6515 ctxt->nbErrors++;
6516 }
6517 if (uri->fragment != NULL) {
6518 if (ctxt->error != NULL)
6519 ctxt->error(ctxt->userData,
6520 "Attribute %s URI %s has a fragment ID\n",
6521 cur->name, val);
6522 ctxt->nbErrors++;
6523 }
6524 xmlFreeURI(uri);
6525 }
6526 }
6527 xmlFree(val);
6528 }
6529 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6530 if (ctxt->error != NULL)
6531 ctxt->error(ctxt->userData,
6532 "Unknown attribute %s on %s\n",
6533 cur->name, node->name);
6534 ctxt->nbErrors++;
6535 }
6536 }
6537 cur = next;
6538 }
6539}
6540
6541/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006542 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006543 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006544 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006545 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006546 * Cleanup the subtree from unwanted nodes for parsing, resolve
6547 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006548 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006549static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00006550xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) {
Daniel Veillardc5312d72003-02-21 17:14:10 +00006551 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006552
Daniel Veillard6eadf632003-01-23 18:29:16 +00006553 delete = NULL;
6554 cur = root;
6555 while (cur != NULL) {
6556 if (delete != NULL) {
6557 xmlUnlinkNode(delete);
6558 xmlFreeNode(delete);
6559 delete = NULL;
6560 }
6561 if (cur->type == XML_ELEMENT_NODE) {
6562 /*
6563 * Simplification 4.1. Annotations
6564 */
6565 if ((cur->ns == NULL) ||
6566 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
Daniel Veillardd2298792003-02-14 16:54:11 +00006567 if ((cur->parent != NULL) &&
6568 (cur->parent->type == XML_ELEMENT_NODE) &&
6569 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6570 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6571 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6572 if (ctxt->error != NULL)
6573 ctxt->error(ctxt->userData,
6574 "element %s doesn't allow foreign elements\n",
6575 cur->parent->name);
6576 ctxt->nbErrors++;
6577 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006578 delete = cur;
6579 goto skip_children;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006580 } else {
6581 xmlRelaxNGCleanupAttributes(ctxt, cur);
6582 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6583 xmlChar *href, *ns, *base, *URL;
6584 xmlRelaxNGDocumentPtr docu;
6585 xmlNodePtr tmp;
6586
6587 ns = xmlGetProp(cur, BAD_CAST "ns");
6588 if (ns == NULL) {
6589 tmp = cur->parent;
6590 while ((tmp != NULL) &&
6591 (tmp->type == XML_ELEMENT_NODE)) {
6592 ns = xmlGetProp(tmp, BAD_CAST "ns");
6593 if (ns != NULL)
6594 break;
6595 tmp = tmp->parent;
6596 }
6597 }
6598 href = xmlGetProp(cur, BAD_CAST "href");
6599 if (href == NULL) {
6600 if (ctxt->error != NULL)
6601 ctxt->error(ctxt->userData,
6602 "xmlRelaxNGParse: externalRef has no href attribute\n");
6603 ctxt->nbErrors++;
6604 delete = cur;
6605 goto skip_children;
6606 }
6607 base = xmlNodeGetBase(cur->doc, cur);
6608 URL = xmlBuildURI(href, base);
6609 if (URL == NULL) {
6610 if (ctxt->error != NULL)
6611 ctxt->error(ctxt->userData,
6612 "Failed to compute URL for externalRef %s\n", href);
6613 ctxt->nbErrors++;
6614 if (href != NULL)
6615 xmlFree(href);
6616 if (base != NULL)
6617 xmlFree(base);
6618 delete = cur;
6619 goto skip_children;
6620 }
6621 if (href != NULL)
6622 xmlFree(href);
6623 if (base != NULL)
6624 xmlFree(base);
6625 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6626 if (docu == NULL) {
6627 if (ctxt->error != NULL)
6628 ctxt->error(ctxt->userData,
6629 "Failed to load externalRef %s\n", URL);
6630 ctxt->nbErrors++;
6631 xmlFree(URL);
6632 delete = cur;
6633 goto skip_children;
6634 }
6635 if (ns != NULL)
6636 xmlFree(ns);
6637 xmlFree(URL);
6638 cur->_private = docu;
6639 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6640 xmlChar *href, *ns, *base, *URL;
6641 xmlRelaxNGIncludePtr incl;
6642 xmlNodePtr tmp;
6643
6644 href = xmlGetProp(cur, BAD_CAST "href");
6645 if (href == NULL) {
6646 if (ctxt->error != NULL)
6647 ctxt->error(ctxt->userData,
6648 "xmlRelaxNGParse: include has no href attribute\n");
6649 ctxt->nbErrors++;
6650 delete = cur;
6651 goto skip_children;
6652 }
6653 base = xmlNodeGetBase(cur->doc, cur);
6654 URL = xmlBuildURI(href, base);
6655 if (URL == NULL) {
6656 if (ctxt->error != NULL)
6657 ctxt->error(ctxt->userData,
6658 "Failed to compute URL for include %s\n", href);
6659 ctxt->nbErrors++;
6660 if (href != NULL)
6661 xmlFree(href);
6662 if (base != NULL)
6663 xmlFree(base);
6664 delete = cur;
6665 goto skip_children;
6666 }
6667 if (href != NULL)
6668 xmlFree(href);
6669 if (base != NULL)
6670 xmlFree(base);
6671 ns = xmlGetProp(cur, BAD_CAST "ns");
6672 if (ns == NULL) {
6673 tmp = cur->parent;
6674 while ((tmp != NULL) &&
6675 (tmp->type == XML_ELEMENT_NODE)) {
6676 ns = xmlGetProp(tmp, BAD_CAST "ns");
6677 if (ns != NULL)
6678 break;
6679 tmp = tmp->parent;
6680 }
6681 }
6682 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6683 if (ns != NULL)
6684 xmlFree(ns);
6685 if (incl == NULL) {
6686 if (ctxt->error != NULL)
6687 ctxt->error(ctxt->userData,
6688 "Failed to load include %s\n", URL);
6689 ctxt->nbErrors++;
6690 xmlFree(URL);
6691 delete = cur;
6692 goto skip_children;
6693 }
6694 xmlFree(URL);
6695 cur->_private = incl;
6696 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6697 (xmlStrEqual(cur->name, BAD_CAST "attribute"))) {
6698 xmlChar *name, *ns;
6699 xmlNodePtr text = NULL;
6700
6701 /*
6702 * Simplification 4.8. name attribute of element
6703 * and attribute elements
6704 */
6705 name = xmlGetProp(cur, BAD_CAST "name");
6706 if (name != NULL) {
6707 if (cur->children == NULL) {
6708 text = xmlNewChild(cur, cur->ns, BAD_CAST "name",
6709 name);
6710 } else {
6711 xmlNodePtr node;
6712 node = xmlNewNode(cur->ns, BAD_CAST "name");
6713 if (node != NULL) {
6714 xmlAddPrevSibling(cur->children, node);
6715 text = xmlNewText(name);
6716 xmlAddChild(node, text);
6717 text = node;
6718 }
6719 }
6720 if (text == NULL) {
6721 if (ctxt->error != NULL)
6722 ctxt->error(ctxt->userData,
6723 "Failed to create a name %s element\n", name);
6724 ctxt->nbErrors++;
6725 }
6726 xmlUnsetProp(cur, BAD_CAST "name");
6727 xmlFree(name);
6728 ns = xmlGetProp(cur, BAD_CAST "ns");
6729 if (ns != NULL) {
6730 if (text != NULL) {
6731 xmlSetProp(text, BAD_CAST "ns", ns);
6732 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
6733 }
6734 xmlFree(ns);
6735 } else if (xmlStrEqual(cur->name,
6736 BAD_CAST "attribute")) {
6737 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
6738 }
6739 }
6740 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
6741 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
6742 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
6743 /*
6744 * Simplification 4.8. name attribute of element
6745 * and attribute elements
6746 */
6747 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
6748 xmlNodePtr node;
6749 xmlChar *ns = NULL;
6750
6751 node = cur->parent;
6752 while ((node != NULL) &&
6753 (node->type == XML_ELEMENT_NODE)) {
6754 ns = xmlGetProp(node, BAD_CAST "ns");
6755 if (ns != NULL) {
6756 break;
6757 }
6758 node = node->parent;
6759 }
6760 if (ns == NULL) {
6761 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
6762 } else {
6763 xmlSetProp(cur, BAD_CAST "ns", ns);
6764 xmlFree(ns);
6765 }
6766 }
6767 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6768 xmlChar *name, *local, *prefix;
6769
6770 /*
6771 * Simplification: 4.10. QNames
6772 */
6773 name = xmlNodeGetContent(cur);
6774 if (name != NULL) {
6775 local = xmlSplitQName2(name, &prefix);
6776 if (local != NULL) {
6777 xmlNsPtr ns;
6778
6779 ns = xmlSearchNs(cur->doc, cur, prefix);
6780 if (ns == NULL) {
6781 if (ctxt->error != NULL)
6782 ctxt->error(ctxt->userData,
6783 "xmlRelaxNGParse: no namespace for prefix %s\n", prefix);
6784 ctxt->nbErrors++;
6785 } else {
6786 xmlSetProp(cur, BAD_CAST "ns", ns->href);
6787 xmlNodeSetContent(cur, local);
6788 }
6789 xmlFree(local);
6790 xmlFree(prefix);
6791 }
6792 xmlFree(name);
6793 }
6794 }
6795 /*
6796 * 4.16
6797 */
6798 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
6799 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
6800 if (ctxt->error != NULL)
6801 ctxt->error(ctxt->userData,
6802 "Found nsName/except//nsName forbidden construct\n");
6803 ctxt->nbErrors++;
6804 }
6805 }
6806 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
6807 (cur != root)) {
6808 int oldflags = ctxt->flags;
6809
6810 /*
6811 * 4.16
6812 */
6813 if ((cur->parent != NULL) &&
6814 (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) {
6815 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
6816 xmlRelaxNGCleanupTree(ctxt, cur);
6817 ctxt->flags = oldflags;
6818 goto skip_children;
6819 } else if ((cur->parent != NULL) &&
6820 (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) {
6821 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
6822 xmlRelaxNGCleanupTree(ctxt, cur);
6823 ctxt->flags = oldflags;
6824 goto skip_children;
6825 }
6826 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
6827 /*
6828 * 4.16
6829 */
6830 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
6831 if (ctxt->error != NULL)
6832 ctxt->error(ctxt->userData,
6833 "Found anyName/except//anyName forbidden construct\n");
6834 ctxt->nbErrors++;
6835 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
6836 if (ctxt->error != NULL)
6837 ctxt->error(ctxt->userData,
6838 "Found nsName/except//anyName forbidden construct\n");
6839 ctxt->nbErrors++;
6840 }
6841 }
6842 /*
6843 * Thisd is not an else since "include" is transformed
6844 * into a div
6845 */
6846 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
6847 xmlChar *ns;
6848 xmlNodePtr child, ins, tmp;
6849
6850 /*
6851 * implements rule 4.11
6852 */
6853
6854 ns = xmlGetProp(cur, BAD_CAST "ns");
6855
6856 child = cur->children;
6857 ins = cur;
6858 while (child != NULL) {
6859 if (ns != NULL) {
6860 if (!xmlHasProp(child, BAD_CAST "ns")) {
6861 xmlSetProp(child, BAD_CAST "ns", ns);
6862 }
6863 }
6864 tmp = child->next;
6865 xmlUnlinkNode(child);
6866 ins = xmlAddNextSibling(ins, child);
6867 child = tmp;
6868 }
6869 if (ns != NULL)
6870 xmlFree(ns);
6871 delete = cur;
6872 goto skip_children;
6873 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006874 }
6875 }
6876 /*
6877 * Simplification 4.2 whitespaces
6878 */
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006879 else if ((cur->type == XML_TEXT_NODE) ||
6880 (cur->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00006881 if (IS_BLANK_NODE(cur)) {
6882 if (cur->parent->type == XML_ELEMENT_NODE) {
6883 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) &&
6884 (!xmlStrEqual(cur->parent->name, BAD_CAST "param")))
6885 delete = cur;
6886 } else {
6887 delete = cur;
6888 goto skip_children;
6889 }
6890 }
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006891 } else {
Daniel Veillard6eadf632003-01-23 18:29:16 +00006892 delete = cur;
6893 goto skip_children;
6894 }
6895
6896 /*
6897 * Skip to next node
6898 */
6899 if (cur->children != NULL) {
6900 if ((cur->children->type != XML_ENTITY_DECL) &&
6901 (cur->children->type != XML_ENTITY_REF_NODE) &&
6902 (cur->children->type != XML_ENTITY_NODE)) {
6903 cur = cur->children;
6904 continue;
6905 }
6906 }
6907skip_children:
6908 if (cur->next != NULL) {
6909 cur = cur->next;
6910 continue;
6911 }
6912
6913 do {
6914 cur = cur->parent;
6915 if (cur == NULL)
6916 break;
6917 if (cur == root) {
6918 cur = NULL;
6919 break;
6920 }
6921 if (cur->next != NULL) {
6922 cur = cur->next;
6923 break;
6924 }
6925 } while (cur != NULL);
6926 }
6927 if (delete != NULL) {
6928 xmlUnlinkNode(delete);
6929 xmlFreeNode(delete);
6930 delete = NULL;
6931 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00006932}
Daniel Veillard6eadf632003-01-23 18:29:16 +00006933
Daniel Veillardc5312d72003-02-21 17:14:10 +00006934/**
6935 * xmlRelaxNGCleanupDoc:
6936 * @ctxt: a Relax-NG parser context
6937 * @doc: an xmldocPtr document pointer
6938 *
6939 * Cleanup the document from unwanted nodes for parsing, resolve
6940 * Include and externalRef lookups.
6941 *
6942 * Returns the cleaned up document or NULL in case of error
6943 */
6944static xmlDocPtr
6945xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) {
6946 xmlNodePtr root;
6947
6948 /*
6949 * Extract the root
6950 */
6951 root = xmlDocGetRootElement(doc);
6952 if (root == NULL) {
6953 if (ctxt->error != NULL)
6954 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
6955 ctxt->URL);
6956 ctxt->nbErrors++;
6957 return (NULL);
6958 }
6959 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006960 return(doc);
6961}
6962
6963/**
6964 * xmlRelaxNGParse:
6965 * @ctxt: a Relax-NG parser context
6966 *
6967 * parse a schema definition resource and build an internal
6968 * XML Shema struture which can be used to validate instances.
6969 * *WARNING* this interface is highly subject to change
6970 *
6971 * Returns the internal XML RelaxNG structure built from the resource or
6972 * NULL in case of error
6973 */
6974xmlRelaxNGPtr
6975xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
6976{
6977 xmlRelaxNGPtr ret = NULL;
6978 xmlDocPtr doc;
6979 xmlNodePtr root;
6980
6981 xmlRelaxNGInitTypes();
6982
6983 if (ctxt == NULL)
6984 return (NULL);
6985
6986 /*
6987 * First step is to parse the input document into an DOM/Infoset
6988 */
6989 if (ctxt->URL != NULL) {
6990 doc = xmlParseFile((const char *) ctxt->URL);
6991 if (doc == NULL) {
6992 if (ctxt->error != NULL)
6993 ctxt->error(ctxt->userData,
6994 "xmlRelaxNGParse: could not load %s\n", ctxt->URL);
6995 ctxt->nbErrors++;
6996 return (NULL);
6997 }
6998 } else if (ctxt->buffer != NULL) {
6999 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
7000 if (doc == NULL) {
7001 if (ctxt->error != NULL)
7002 ctxt->error(ctxt->userData,
7003 "xmlRelaxNGParse: could not parse schemas\n");
7004 ctxt->nbErrors++;
7005 return (NULL);
7006 }
7007 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7008 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7009 } else {
7010 if (ctxt->error != NULL)
7011 ctxt->error(ctxt->userData,
7012 "xmlRelaxNGParse: nothing to parse\n");
7013 ctxt->nbErrors++;
7014 return (NULL);
7015 }
7016 ctxt->document = doc;
7017
7018 /*
7019 * Some preprocessing of the document content
7020 */
7021 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7022 if (doc == NULL) {
7023 xmlFreeDoc(ctxt->document);
7024 ctxt->document = NULL;
7025 return(NULL);
7026 }
7027
Daniel Veillard6eadf632003-01-23 18:29:16 +00007028 /*
7029 * Then do the parsing for good
7030 */
7031 root = xmlDocGetRootElement(doc);
7032 if (root == NULL) {
7033 if (ctxt->error != NULL)
7034 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
7035 ctxt->URL);
7036 ctxt->nbErrors++;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007037 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007038 return (NULL);
7039 }
7040 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007041 if (ret == NULL) {
7042 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007043 return(NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007044 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007045
7046 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007047 * Check the ref/defines links
7048 */
7049 /*
7050 * try to preprocess interleaves
7051 */
7052 if (ctxt->interleaves != NULL) {
7053 xmlHashScan(ctxt->interleaves,
7054 (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt);
7055 }
7056
7057 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007058 * if there was a parsing error return NULL
7059 */
7060 if (ctxt->nbErrors > 0) {
7061 xmlRelaxNGFree(ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007062 ctxt->document = NULL;
7063 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007064 return(NULL);
7065 }
7066
7067 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007068 * try to compile (parts of) the schemas
7069 */
7070 if (ctxt->grammar != NULL)
7071 xmlRelaxNGTryCompile(ctxt, ctxt->grammar->start);
7072
7073 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007074 * Transfer the pointer for cleanup at the schema level.
7075 */
7076 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007077 ctxt->document = NULL;
7078 ret->documents = ctxt->documents;
7079 ctxt->documents = NULL;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007080
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007081 ret->includes = ctxt->includes;
7082 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007083 ret->defNr = ctxt->defNr;
7084 ret->defTab = ctxt->defTab;
7085 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007086 if (ctxt->idref == 1)
7087 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007088
7089 return (ret);
7090}
7091
7092/**
7093 * xmlRelaxNGSetParserErrors:
7094 * @ctxt: a Relax-NG validation context
7095 * @err: the error callback
7096 * @warn: the warning callback
7097 * @ctx: contextual data for the callbacks
7098 *
7099 * Set the callback functions used to handle errors for a validation context
7100 */
7101void
7102xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7103 xmlRelaxNGValidityErrorFunc err,
7104 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
7105 if (ctxt == NULL)
7106 return;
7107 ctxt->error = err;
7108 ctxt->warning = warn;
7109 ctxt->userData = ctx;
7110}
7111/************************************************************************
7112 * *
7113 * Dump back a compiled form *
7114 * *
7115 ************************************************************************/
7116static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define);
7117
7118/**
7119 * xmlRelaxNGDumpDefines:
7120 * @output: the file output
7121 * @defines: a list of define structures
7122 *
7123 * Dump a RelaxNG structure back
7124 */
7125static void
7126xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) {
7127 while (defines != NULL) {
7128 xmlRelaxNGDumpDefine(output, defines);
7129 defines = defines->next;
7130 }
7131}
7132
7133/**
7134 * xmlRelaxNGDumpDefine:
7135 * @output: the file output
7136 * @define: a define structure
7137 *
7138 * Dump a RelaxNG structure back
7139 */
7140static void
7141xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) {
7142 if (define == NULL)
7143 return;
7144 switch(define->type) {
7145 case XML_RELAXNG_EMPTY:
7146 fprintf(output, "<empty/>\n");
7147 break;
7148 case XML_RELAXNG_NOT_ALLOWED:
7149 fprintf(output, "<notAllowed/>\n");
7150 break;
7151 case XML_RELAXNG_TEXT:
7152 fprintf(output, "<text/>\n");
7153 break;
7154 case XML_RELAXNG_ELEMENT:
7155 fprintf(output, "<element>\n");
7156 if (define->name != NULL) {
7157 fprintf(output, "<name");
7158 if (define->ns != NULL)
7159 fprintf(output, " ns=\"%s\"", define->ns);
7160 fprintf(output, ">%s</name>\n", define->name);
7161 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007162 xmlRelaxNGDumpDefines(output, define->attrs);
7163 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007164 fprintf(output, "</element>\n");
7165 break;
7166 case XML_RELAXNG_LIST:
7167 fprintf(output, "<list>\n");
7168 xmlRelaxNGDumpDefines(output, define->content);
7169 fprintf(output, "</list>\n");
7170 break;
7171 case XML_RELAXNG_ONEORMORE:
7172 fprintf(output, "<oneOrMore>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007173 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007174 fprintf(output, "</oneOrMore>\n");
7175 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007176 case XML_RELAXNG_ZEROORMORE:
7177 fprintf(output, "<zeroOrMore>\n");
7178 xmlRelaxNGDumpDefines(output, define->content);
7179 fprintf(output, "</zeroOrMore>\n");
7180 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007181 case XML_RELAXNG_CHOICE:
7182 fprintf(output, "<choice>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007183 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007184 fprintf(output, "</choice>\n");
7185 break;
7186 case XML_RELAXNG_GROUP:
7187 fprintf(output, "<group>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007188 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007189 fprintf(output, "</group>\n");
7190 break;
7191 case XML_RELAXNG_INTERLEAVE:
7192 fprintf(output, "<interleave>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007193 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007194 fprintf(output, "</interleave>\n");
7195 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007196 case XML_RELAXNG_OPTIONAL:
7197 fprintf(output, "<optional>\n");
7198 xmlRelaxNGDumpDefines(output, define->content);
7199 fprintf(output, "</optional>\n");
7200 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007201 case XML_RELAXNG_ATTRIBUTE:
7202 fprintf(output, "<attribute>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007203 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007204 fprintf(output, "</attribute>\n");
7205 break;
7206 case XML_RELAXNG_DEF:
7207 fprintf(output, "<define");
7208 if (define->name != NULL)
7209 fprintf(output, " name=\"%s\"", define->name);
7210 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007211 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007212 fprintf(output, "</define>\n");
7213 break;
7214 case XML_RELAXNG_REF:
7215 fprintf(output, "<ref");
7216 if (define->name != NULL)
7217 fprintf(output, " name=\"%s\"", define->name);
7218 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007219 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007220 fprintf(output, "</ref>\n");
7221 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007222 case XML_RELAXNG_PARENTREF:
7223 fprintf(output, "<parentRef");
7224 if (define->name != NULL)
7225 fprintf(output, " name=\"%s\"", define->name);
7226 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007227 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard419a7682003-02-03 23:22:49 +00007228 fprintf(output, "</parentRef>\n");
7229 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007230 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard416589a2003-02-17 17:25:42 +00007231 fprintf(output, "<externalRef>");
Daniel Veillardfd573f12003-03-16 17:52:32 +00007232 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillarde431a272003-01-29 23:02:33 +00007233 fprintf(output, "</externalRef>\n");
7234 break;
7235 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007236 case XML_RELAXNG_VALUE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007237 TODO
Daniel Veillard6eadf632003-01-23 18:29:16 +00007238 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007239 case XML_RELAXNG_START:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007240 case XML_RELAXNG_EXCEPT:
Daniel Veillard8fe98712003-02-19 00:19:14 +00007241 case XML_RELAXNG_PARAM:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007242 TODO
7243 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00007244 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007245 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard77648bb2003-02-20 15:03:22 +00007246 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007247 }
7248}
7249
7250/**
7251 * xmlRelaxNGDumpGrammar:
7252 * @output: the file output
7253 * @grammar: a grammar structure
7254 * @top: is this a top grammar
7255 *
7256 * Dump a RelaxNG structure back
7257 */
7258static void
7259xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7260{
7261 if (grammar == NULL)
7262 return;
7263
7264 fprintf(output, "<grammar");
7265 if (top)
7266 fprintf(output,
7267 " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7268 switch(grammar->combine) {
7269 case XML_RELAXNG_COMBINE_UNDEFINED:
7270 break;
7271 case XML_RELAXNG_COMBINE_CHOICE:
7272 fprintf(output, " combine=\"choice\"");
7273 break;
7274 case XML_RELAXNG_COMBINE_INTERLEAVE:
7275 fprintf(output, " combine=\"interleave\"");
7276 break;
7277 default:
7278 fprintf(output, " <!-- invalid combine value -->");
7279 }
7280 fprintf(output, ">\n");
7281 if (grammar->start == NULL) {
7282 fprintf(output, " <!-- grammar had no start -->");
7283 } else {
7284 fprintf(output, "<start>\n");
7285 xmlRelaxNGDumpDefine(output, grammar->start);
7286 fprintf(output, "</start>\n");
7287 }
7288 /* TODO ? Dump the defines ? */
7289 fprintf(output, "</grammar>\n");
7290}
7291
7292/**
7293 * xmlRelaxNGDump:
7294 * @output: the file output
7295 * @schema: a schema structure
7296 *
7297 * Dump a RelaxNG structure back
7298 */
7299void
7300xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7301{
7302 if (schema == NULL) {
7303 fprintf(output, "RelaxNG empty or failed to compile\n");
7304 return;
7305 }
7306 fprintf(output, "RelaxNG: ");
7307 if (schema->doc == NULL) {
7308 fprintf(output, "no document\n");
7309 } else if (schema->doc->URL != NULL) {
7310 fprintf(output, "%s\n", schema->doc->URL);
7311 } else {
7312 fprintf(output, "\n");
7313 }
7314 if (schema->topgrammar == NULL) {
7315 fprintf(output, "RelaxNG has no top grammar\n");
7316 return;
7317 }
7318 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7319}
7320
Daniel Veillardfebcca42003-02-16 15:44:18 +00007321/**
7322 * xmlRelaxNGDumpTree:
7323 * @output: the file output
7324 * @schema: a schema structure
7325 *
7326 * Dump the transformed RelaxNG tree.
7327 */
7328void
7329xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7330{
7331 if (schema == NULL) {
7332 fprintf(output, "RelaxNG empty or failed to compile\n");
7333 return;
7334 }
7335 if (schema->doc == NULL) {
7336 fprintf(output, "no document\n");
7337 } else {
7338 xmlDocDump(output, schema->doc);
7339 }
7340}
7341
Daniel Veillard6eadf632003-01-23 18:29:16 +00007342/************************************************************************
7343 * *
7344 * Validation implementation *
7345 * *
7346 ************************************************************************/
Daniel Veillardfd573f12003-03-16 17:52:32 +00007347static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7348 xmlRelaxNGDefinePtr define);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007349static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
7350 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007351
7352/**
7353 * xmlRelaxNGSkipIgnored:
7354 * @ctxt: a schema validation context
7355 * @node: the top node.
7356 *
7357 * Skip ignorable nodes in that context
7358 *
7359 * Returns the new sibling or NULL in case of error.
7360 */
7361static xmlNodePtr
7362xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
7363 xmlNodePtr node) {
7364 /*
7365 * TODO complete and handle entities
7366 */
7367 while ((node != NULL) &&
7368 ((node->type == XML_COMMENT_NODE) ||
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007369 (node->type == XML_PI_NODE) ||
Daniel Veillard39eb88b2003-03-11 11:21:28 +00007370 (((node->type == XML_TEXT_NODE) ||
7371 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007372 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
7373 (IS_BLANK_NODE(node)))))) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007374 node = node->next;
7375 }
7376 return(node);
7377}
7378
7379/**
Daniel Veillardedc91922003-01-26 00:52:04 +00007380 * xmlRelaxNGNormalize:
7381 * @ctxt: a schema validation context
7382 * @str: the string to normalize
7383 *
7384 * Implements the normalizeWhiteSpace( s ) function from
7385 * section 6.2.9 of the spec
7386 *
7387 * Returns the new string or NULL in case of error.
7388 */
7389static xmlChar *
7390xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) {
7391 xmlChar *ret, *p;
7392 const xmlChar *tmp;
7393 int len;
7394
7395 if (str == NULL)
7396 return(NULL);
7397 tmp = str;
7398 while (*tmp != 0) tmp++;
7399 len = tmp - str;
7400
7401 ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar));
7402 if (ret == NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00007403 if (ctxt != NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007404 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
Daniel Veillardea3f3982003-01-26 19:45:18 +00007405 } else {
7406 xmlGenericError(xmlGenericErrorContext,
7407 "xmlRelaxNGNormalize: out of memory\n");
7408 }
Daniel Veillardedc91922003-01-26 00:52:04 +00007409 return(NULL);
7410 }
7411 p = ret;
7412 while (IS_BLANK(*str)) str++;
7413 while (*str != 0) {
7414 if (IS_BLANK(*str)) {
7415 while (IS_BLANK(*str)) str++;
7416 if (*str == 0)
7417 break;
7418 *p++ = ' ';
7419 } else
7420 *p++ = *str++;
7421 }
7422 *p = 0;
7423 return(ret);
7424}
7425
7426/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007427 * xmlRelaxNGValidateDatatype:
7428 * @ctxt: a Relax-NG validation context
7429 * @value: the string value
7430 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007431 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007432 *
7433 * Validate the given value against the dataype
7434 *
7435 * Returns 0 if the validation succeeded or an error code.
7436 */
7437static int
7438xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007439 xmlRelaxNGDefinePtr define, xmlNodePtr node) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007440 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007441 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007442 void *result = NULL;
7443 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007444
7445 if ((define == NULL) || (define->data == NULL)) {
7446 return(-1);
7447 }
7448 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007449 if (lib->check != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007450 if ((define->attrs != NULL) &&
7451 (define->attrs->type == XML_RELAXNG_PARAM)) {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007452 ret = lib->check(lib->data, define->name, value, &result, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007453 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007454 ret = lib->check(lib->data, define->name, value, NULL, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007455 }
7456 } else
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007457 ret = -1;
7458 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007459 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007460 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
7461 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007462 return(-1);
7463 } else if (ret == 1) {
7464 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007465 } else if (ret == 2) {
7466 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007467 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007468 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007469 ret = -1;
7470 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007471 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007472 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
7473 if (lib->facet != NULL) {
7474 tmp = lib->facet(lib->data, define->name, cur->name,
7475 cur->value, value, result);
7476 if (tmp != 0)
7477 ret = -1;
7478 }
7479 cur = cur->next;
7480 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007481 if ((ret == 0) && (define->content != NULL)) {
7482 const xmlChar *oldvalue, *oldendvalue;
7483
7484 oldvalue = ctxt->state->value;
7485 oldendvalue = ctxt->state->endvalue;
7486 ctxt->state->value = (xmlChar *) value;
7487 ctxt->state->endvalue = NULL;
7488 ret = xmlRelaxNGValidateValue(ctxt, define->content);
7489 ctxt->state->value = (xmlChar *) oldvalue;
7490 ctxt->state->endvalue = (xmlChar *) oldendvalue;
7491 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007492 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
7493 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007494 return(ret);
7495}
7496
7497/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007498 * xmlRelaxNGNextValue:
7499 * @ctxt: a Relax-NG validation context
7500 *
7501 * Skip to the next value when validating within a list
7502 *
7503 * Returns 0 if the operation succeeded or an error code.
7504 */
7505static int
7506xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) {
7507 xmlChar *cur;
7508
7509 cur = ctxt->state->value;
7510 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
7511 ctxt->state->value = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00007512 ctxt->state->endvalue = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007513 return(0);
7514 }
7515 while (*cur != 0) cur++;
7516 while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++;
7517 if (cur == ctxt->state->endvalue)
7518 ctxt->state->value = NULL;
7519 else
7520 ctxt->state->value = cur;
7521 return(0);
7522}
7523
7524/**
7525 * xmlRelaxNGValidateValueList:
7526 * @ctxt: a Relax-NG validation context
7527 * @defines: the list of definitions to verify
7528 *
7529 * Validate the given set of definitions for the current value
7530 *
7531 * Returns 0 if the validation succeeded or an error code.
7532 */
7533static int
7534xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
7535 xmlRelaxNGDefinePtr defines) {
7536 int ret = 0;
7537
7538 while (defines != NULL) {
7539 ret = xmlRelaxNGValidateValue(ctxt, defines);
7540 if (ret != 0)
7541 break;
7542 defines = defines->next;
7543 }
7544 return(ret);
7545}
7546
7547/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00007548 * xmlRelaxNGValidateValue:
7549 * @ctxt: a Relax-NG validation context
7550 * @define: the definition to verify
7551 *
7552 * Validate the given definition for the current value
7553 *
7554 * Returns 0 if the validation succeeded or an error code.
7555 */
7556static int
7557xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
7558 xmlRelaxNGDefinePtr define) {
Daniel Veillardedc91922003-01-26 00:52:04 +00007559 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007560 xmlChar *value;
7561
7562 value = ctxt->state->value;
7563 switch (define->type) {
Daniel Veillardd4310742003-02-18 21:12:46 +00007564 case XML_RELAXNG_EMPTY: {
7565 if ((value != NULL) && (value[0] != 0)) {
7566 int idx = 0;
7567
7568 while (IS_BLANK(value[idx]))
7569 idx++;
7570 if (value[idx] != 0)
7571 ret = -1;
7572 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007573 break;
Daniel Veillardd4310742003-02-18 21:12:46 +00007574 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007575 case XML_RELAXNG_TEXT:
7576 break;
Daniel Veillardedc91922003-01-26 00:52:04 +00007577 case XML_RELAXNG_VALUE: {
7578 if (!xmlStrEqual(value, define->value)) {
7579 if (define->name != NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00007580 xmlRelaxNGTypeLibraryPtr lib;
7581
7582 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00007583 if ((lib != NULL) && (lib->comp != NULL)) {
7584 ret = lib->comp(lib->data, define->name,
7585 define->value, define->node,
7586 (void *) define->attrs,
7587 value, ctxt->state->node);
7588 } else
Daniel Veillardea3f3982003-01-26 19:45:18 +00007589 ret = -1;
7590 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007591 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name);
Daniel Veillardea3f3982003-01-26 19:45:18 +00007592 return(-1);
7593 } else if (ret == 1) {
7594 ret = 0;
7595 } else {
7596 ret = -1;
7597 }
Daniel Veillardedc91922003-01-26 00:52:04 +00007598 } else {
7599 xmlChar *nval, *nvalue;
7600
7601 /*
7602 * TODO: trivial optimizations are possible by
7603 * computing at compile-time
7604 */
7605 nval = xmlRelaxNGNormalize(ctxt, define->value);
7606 nvalue = xmlRelaxNGNormalize(ctxt, value);
7607
Daniel Veillardea3f3982003-01-26 19:45:18 +00007608 if ((nval == NULL) || (nvalue == NULL) ||
7609 (!xmlStrEqual(nval, nvalue)))
Daniel Veillardedc91922003-01-26 00:52:04 +00007610 ret = -1;
7611 if (nval != NULL)
7612 xmlFree(nval);
7613 if (nvalue != NULL)
7614 xmlFree(nvalue);
7615 }
7616 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007617 if (ret == 0)
7618 xmlRelaxNGNextValue(ctxt);
Daniel Veillardedc91922003-01-26 00:52:04 +00007619 break;
7620 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007621 case XML_RELAXNG_DATATYPE: {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007622 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
7623 ctxt->state->seq);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007624 if (ret == 0)
7625 xmlRelaxNGNextValue(ctxt);
7626
7627 break;
7628 }
7629 case XML_RELAXNG_CHOICE: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007630 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007631 xmlChar *oldvalue;
7632
7633 oldflags = ctxt->flags;
7634 ctxt->flags |= FLAGS_IGNORABLE;
7635
7636 oldvalue = ctxt->state->value;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007637 while (list != NULL) {
7638 ret = xmlRelaxNGValidateValue(ctxt, list);
7639 if (ret == 0) {
7640 break;
7641 }
7642 ctxt->state->value = oldvalue;
7643 list = list->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007644 }
7645 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00007646 if (ret != 0) {
7647 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7648 xmlRelaxNGDumpValidError(ctxt);
7649 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007650 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00007651 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007652 if (ret == 0)
7653 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007654 break;
7655 }
7656 case XML_RELAXNG_LIST: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007657 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007658 xmlChar *oldvalue, *oldend, *val, *cur;
Daniel Veillard416589a2003-02-17 17:25:42 +00007659#ifdef DEBUG_LIST
7660 int nb_values = 0;
7661#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007662
7663 oldvalue = ctxt->state->value;
7664 oldend = ctxt->state->endvalue;
7665
7666 val = xmlStrdup(oldvalue);
7667 if (val == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00007668 val = xmlStrdup(BAD_CAST "");
7669 }
7670 if (val == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007671 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007672 return(-1);
7673 }
7674 cur = val;
7675 while (*cur != 0) {
Daniel Veillard416589a2003-02-17 17:25:42 +00007676 if (IS_BLANK(*cur)) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007677 *cur = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00007678 cur++;
7679#ifdef DEBUG_LIST
7680 nb_values++;
7681#endif
7682 while (IS_BLANK(*cur))
7683 *cur++ = 0;
7684 } else
7685 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007686 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007687#ifdef DEBUG_LIST
7688 xmlGenericError(xmlGenericErrorContext,
7689 "list value: '%s' found %d items\n", oldvalue, nb_values);
7690 nb_values = 0;
7691#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007692 ctxt->state->endvalue = cur;
7693 cur = val;
7694 while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007695
Daniel Veillardfd573f12003-03-16 17:52:32 +00007696 ctxt->state->value = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007697
Daniel Veillardfd573f12003-03-16 17:52:32 +00007698 while (list != NULL) {
7699 if (ctxt->state->value == ctxt->state->endvalue)
7700 ctxt->state->value = NULL;
7701 ret = xmlRelaxNGValidateValue(ctxt, list);
7702 if (ret != 0) {
7703#ifdef DEBUG_LIST
7704 xmlGenericError(xmlGenericErrorContext,
7705 "Failed to validate value: '%s' with %d rule\n",
7706 ctxt->state->value, nb_values);
7707#endif
7708 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007709 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007710#ifdef DEBUG_LIST
7711 nb_values++;
7712#endif
7713 list = list->next;
7714 }
7715
7716 if ((ret == 0) && (ctxt->state->value != NULL) &&
7717 (ctxt->state->value != ctxt->state->endvalue)) {
7718 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value);
7719 ret = -1;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007720 }
7721 xmlFree(val);
7722 ctxt->state->value = oldvalue;
7723 ctxt->state->endvalue = oldend;
7724 break;
7725 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007726 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007727 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
7728 if (ret != 0) {
7729 break;
7730 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007731 /* no break on purpose */
7732 case XML_RELAXNG_ZEROORMORE: {
7733 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007734
7735 oldflags = ctxt->flags;
7736 ctxt->flags |= FLAGS_IGNORABLE;
7737 cur = ctxt->state->value;
7738 temp = NULL;
7739 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
7740 (temp != cur)) {
7741 temp = cur;
7742 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
7743 if (ret != 0) {
7744 ctxt->state->value = temp;
7745 ret = 0;
7746 break;
7747 }
7748 cur = ctxt->state->value;
7749 }
7750 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00007751 if (ret != 0) {
7752 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7753 xmlRelaxNGDumpValidError(ctxt);
7754 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007755 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00007756 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007757 break;
7758 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007759 case XML_RELAXNG_EXCEPT: {
7760 xmlRelaxNGDefinePtr list;
7761
7762 list = define->content;
7763 while (list != NULL) {
7764 ret = xmlRelaxNGValidateValue(ctxt, list);
7765 if (ret == 0) {
7766 ret = -1;
7767 break;
7768 } else
7769 ret = 0;
7770 list = list->next;
7771 }
7772 break;
7773 }
Daniel Veillard463a5472003-02-27 21:30:32 +00007774 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007775 case XML_RELAXNG_GROUP: {
7776 xmlRelaxNGDefinePtr list;
7777
7778 list = define->content;
7779 while (list != NULL) {
7780 ret = xmlRelaxNGValidateValue(ctxt, list);
7781 if (ret != 0) {
7782 ret = -1;
7783 break;
7784 } else
7785 ret = 0;
7786 list = list->next;
7787 }
Daniel Veillardd4310742003-02-18 21:12:46 +00007788 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007789 }
Daniel Veillard463a5472003-02-27 21:30:32 +00007790 case XML_RELAXNG_REF:
7791 case XML_RELAXNG_PARENTREF:
7792 ret = xmlRelaxNGValidateValue(ctxt, define->content);
7793 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007794 default:
7795 TODO
7796 ret = -1;
7797 }
7798 return(ret);
7799}
7800
7801/**
7802 * xmlRelaxNGValidateValueContent:
7803 * @ctxt: a Relax-NG validation context
7804 * @defines: the list of definitions to verify
7805 *
7806 * Validate the given definitions for the current value
7807 *
7808 * Returns 0 if the validation succeeded or an error code.
7809 */
7810static int
7811xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
7812 xmlRelaxNGDefinePtr defines) {
7813 int ret = 0;
7814
7815 while (defines != NULL) {
7816 ret = xmlRelaxNGValidateValue(ctxt, defines);
7817 if (ret != 0)
7818 break;
7819 defines = defines->next;
7820 }
7821 return(ret);
7822}
7823
7824/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007825 * xmlRelaxNGAttributeMatch:
7826 * @ctxt: a Relax-NG validation context
7827 * @define: the definition to check
7828 * @prop: the attribute
7829 *
7830 * Check if the attribute matches the definition nameClass
7831 *
7832 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
7833 */
7834static int
7835xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
7836 xmlRelaxNGDefinePtr define,
7837 xmlAttrPtr prop) {
7838 int ret;
7839
7840 if (define->name != NULL) {
7841 if (!xmlStrEqual(define->name, prop->name))
7842 return(0);
7843 }
7844 if (define->ns != NULL) {
7845 if (define->ns[0] == 0) {
7846 if (prop->ns != NULL)
7847 return(0);
7848 } else {
7849 if ((prop->ns == NULL) ||
7850 (!xmlStrEqual(define->ns, prop->ns->href)))
7851 return(0);
7852 }
7853 }
7854 if (define->nameClass == NULL)
7855 return(1);
7856 define = define->nameClass;
7857 if (define->type == XML_RELAXNG_EXCEPT) {
7858 xmlRelaxNGDefinePtr list;
7859
7860 list = define->content;
7861 while (list != NULL) {
7862 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
7863 if (ret == 1)
7864 return(0);
7865 if (ret < 0)
7866 return(ret);
7867 list = list->next;
7868 }
7869 } else {
7870 TODO
7871 }
7872 return(1);
7873}
7874
7875/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00007876 * xmlRelaxNGValidateAttribute:
7877 * @ctxt: a Relax-NG validation context
7878 * @define: the definition to verify
7879 *
7880 * Validate the given attribute definition for that node
7881 *
7882 * Returns 0 if the validation succeeded or an error code.
7883 */
7884static int
7885xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
7886 xmlRelaxNGDefinePtr define) {
7887 int ret = 0, i;
7888 xmlChar *value, *oldvalue;
7889 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007890 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007891
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007892 if (ctxt->state->nbAttrLeft <= 0)
7893 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007894 if (define->name != NULL) {
7895 for (i = 0;i < ctxt->state->nbAttrs;i++) {
7896 tmp = ctxt->state->attrs[i];
7897 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
7898 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
7899 (tmp->ns == NULL)) ||
7900 ((tmp->ns != NULL) &&
7901 (xmlStrEqual(define->ns, tmp->ns->href)))) {
7902 prop = tmp;
7903 break;
7904 }
7905 }
7906 }
7907 if (prop != NULL) {
7908 value = xmlNodeListGetString(prop->doc, prop->children, 1);
7909 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007910 oldseq = ctxt->state->seq;
7911 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007912 ctxt->state->value = value;
Daniel Veillard231d7912003-02-09 14:22:17 +00007913 ctxt->state->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007914 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00007915 if (ctxt->state->value != NULL)
7916 value = ctxt->state->value;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007917 if (value != NULL)
7918 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00007919 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007920 ctxt->state->seq = oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007921 if (ret == 0) {
7922 /*
7923 * flag the attribute as processed
7924 */
7925 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007926 ctxt->state->nbAttrLeft--;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007927 }
7928 } else {
7929 ret = -1;
7930 }
7931#ifdef DEBUG
7932 xmlGenericError(xmlGenericErrorContext,
7933 "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret);
7934#endif
7935 } else {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007936 for (i = 0;i < ctxt->state->nbAttrs;i++) {
7937 tmp = ctxt->state->attrs[i];
Daniel Veillard144fae12003-02-03 13:17:57 +00007938 if ((tmp != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00007939 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007940 prop = tmp;
7941 break;
7942 }
7943 }
7944 if (prop != NULL) {
7945 value = xmlNodeListGetString(prop->doc, prop->children, 1);
7946 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007947 oldseq = ctxt->state->seq;
7948 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007949 ctxt->state->value = value;
7950 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00007951 if (ctxt->state->value != NULL)
7952 value = ctxt->state->value;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007953 if (value != NULL)
7954 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00007955 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007956 ctxt->state->seq = oldseq;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007957 if (ret == 0) {
7958 /*
7959 * flag the attribute as processed
7960 */
7961 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007962 ctxt->state->nbAttrLeft--;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007963 }
7964 } else {
7965 ret = -1;
7966 }
7967#ifdef DEBUG
Daniel Veillard144fae12003-02-03 13:17:57 +00007968 if (define->ns != NULL) {
7969 xmlGenericError(xmlGenericErrorContext,
7970 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
7971 define->ns, ret);
7972 } else {
7973 xmlGenericError(xmlGenericErrorContext,
7974 "xmlRelaxNGValidateAttribute(anyName): %d\n",
7975 ret);
7976 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007977#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00007978 }
7979
7980 return(ret);
7981}
7982
7983/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007984 * xmlRelaxNGValidateAttributeList:
7985 * @ctxt: a Relax-NG validation context
7986 * @define: the list of definition to verify
7987 *
7988 * Validate the given node against the list of attribute definitions
7989 *
7990 * Returns 0 if the validation succeeded or an error code.
7991 */
7992static int
7993xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7994 xmlRelaxNGDefinePtr defines) {
7995 int ret = 0;
7996 while (defines != NULL) {
7997 if (xmlRelaxNGValidateAttribute(ctxt, defines) != 0)
7998 ret = -1;
7999 defines = defines->next;
8000 }
8001 return(ret);
8002}
8003
8004/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008005 * xmlRelaxNGNodeMatchesList:
8006 * @node: the node
8007 * @list: a NULL terminated array of definitions
8008 *
8009 * Check if a node can be matched by one of the definitions
8010 *
8011 * Returns 1 if matches 0 otherwise
8012 */
8013static int
8014xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) {
8015 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008016 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008017
8018 if ((node == NULL) || (list == NULL))
8019 return(0);
8020
8021 cur = list[i++];
8022 while (cur != NULL) {
8023 if ((node->type == XML_ELEMENT_NODE) &&
8024 (cur->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008025 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
8026 if (tmp == 1)
8027 return(1);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00008028 } else if (((node->type == XML_TEXT_NODE) ||
8029 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00008030 (cur->type == XML_RELAXNG_TEXT)) {
8031 return(1);
8032 }
8033 cur = list[i++];
8034 }
8035 return(0);
8036}
8037
8038/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008039 * xmlRelaxNGValidateInterleave:
8040 * @ctxt: a Relax-NG validation context
8041 * @define: the definition to verify
8042 *
8043 * Validate an interleave definition for a node.
8044 *
8045 * Returns 0 if the validation succeeded or an error code.
8046 */
8047static int
8048xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
8049 xmlRelaxNGDefinePtr define) {
8050 int ret = 0, i, nbgroups, left;
8051 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008052 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008053
8054 xmlRelaxNGValidStatePtr oldstate;
8055 xmlRelaxNGPartitionPtr partitions;
8056 xmlRelaxNGInterleaveGroupPtr group = NULL;
8057 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
8058 xmlNodePtr *list = NULL, *lasts = NULL;
8059
8060 if (define->data != NULL) {
8061 partitions = (xmlRelaxNGPartitionPtr) define->data;
8062 nbgroups = partitions->nbgroups;
8063 left = nbgroups;
8064 } else {
8065 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
8066 return(-1);
8067 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008068 /*
8069 * Optimizations for MIXED
8070 */
8071 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00008072 if (define->dflags & IS_MIXED) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008073 ctxt->flags |= FLAGS_MIXED_CONTENT;
8074 if (nbgroups == 2) {
8075 /*
8076 * this is a pure <mixed> case
8077 */
8078 if (ctxt->state != NULL)
8079 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8080 ctxt->state->seq);
8081 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
8082 ret = xmlRelaxNGValidateDefinition(ctxt,
8083 partitions->groups[1]->rule);
8084 else
8085 ret = xmlRelaxNGValidateDefinition(ctxt,
8086 partitions->groups[0]->rule);
8087 if (ret == 0) {
8088 if (ctxt->state != NULL)
8089 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
8090 ctxt->state->seq);
8091 }
8092 ctxt->flags = oldflags;
8093 return(ret);
8094 }
8095 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008096
8097 /*
8098 * Build arrays to store the first and last node of the chain
8099 * pertaining to each group
8100 */
8101 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8102 if (list == NULL) {
8103 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8104 return(-1);
8105 }
8106 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
8107 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
8108 if (lasts == NULL) {
8109 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8110 return(-1);
8111 }
8112 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
8113
8114 /*
8115 * Walk the sequence of children finding the right group and
8116 * sorting them in sequences.
8117 */
8118 cur = ctxt->state->seq;
8119 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
8120 start = cur;
8121 while (cur != NULL) {
8122 ctxt->state->seq = cur;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00008123 if ((partitions->triage != NULL) &&
8124 (partitions->flags & IS_DETERMINIST)) {
8125 void *tmp = NULL;
8126
8127 if ((cur->type == XML_TEXT_NODE) ||
8128 (cur->type == XML_CDATA_SECTION_NODE)) {
8129 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
8130 NULL);
8131 } else if (cur->type == XML_ELEMENT_NODE) {
8132 if (cur->ns != NULL) {
8133 tmp = xmlHashLookup2(partitions->triage, cur->name,
8134 cur->ns->href);
8135 if (tmp == NULL)
8136 tmp = xmlHashLookup2(partitions->triage,
8137 BAD_CAST "#any", cur->ns->href);
8138 } else
8139 tmp = xmlHashLookup2(partitions->triage, cur->name, NULL);
8140 if (tmp == NULL)
8141 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any",
8142 NULL);
8143 }
8144
8145 if (tmp == NULL) {
8146 i = nbgroups;
8147 } else {
8148 i = ((long) tmp) - 1;
8149 if (partitions->flags & IS_NEEDCHECK) {
8150 group = partitions->groups[i];
8151 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
8152 i = nbgroups;
8153 }
8154 }
8155 } else {
8156 for (i = 0;i < nbgroups;i++) {
8157 group = partitions->groups[i];
8158 if (group == NULL)
8159 continue;
8160 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
8161 break;
8162 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008163 }
8164 /*
8165 * We break as soon as an element not matched is found
8166 */
8167 if (i >= nbgroups) {
8168 break;
8169 }
8170 if (lasts[i] != NULL) {
8171 lasts[i]->next = cur;
8172 lasts[i] = cur;
8173 } else {
8174 list[i] = cur;
8175 lasts[i] = cur;
8176 }
8177 if (cur->next != NULL)
8178 lastchg = cur->next;
8179 else
8180 lastchg = cur;
8181 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
8182 }
8183 if (ret != 0) {
8184 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
8185 ret = -1;
8186 goto done;
8187 }
8188 lastelem = cur;
8189 oldstate = ctxt->state;
8190 for (i = 0;i < nbgroups;i++) {
8191 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
8192 group = partitions->groups[i];
8193 if (lasts[i] != NULL) {
8194 last = lasts[i]->next;
8195 lasts[i]->next = NULL;
8196 }
8197 ctxt->state->seq = list[i];
8198 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
8199 if (ret != 0)
8200 break;
8201 if (ctxt->state != NULL) {
8202 cur = ctxt->state->seq;
8203 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
Daniel Veillard798024a2003-03-19 10:36:09 +00008204 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008205 oldstate = ctxt->state;
8206 ctxt->state = NULL;
8207 if (cur != NULL) {
8208 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
8209 ret = -1;
8210 ctxt->state = oldstate;
8211 goto done;
8212 }
8213 } else if (ctxt->states != NULL) {
8214 int j;
8215 int found = 0;
8216
8217 for (j = 0;j < ctxt->states->nbState;j++) {
8218 cur = ctxt->states->tabState[j]->seq;
8219 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
8220 if (cur == NULL) {
8221 found = 1;
8222 break;
8223 }
8224 }
8225 if (ctxt->states->nbState > 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008226 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008227 oldstate = ctxt->states->tabState[ctxt->states->nbState - 1];
8228 }
8229 for (j = 0;j < ctxt->states->nbState - 1;j++) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008230 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008231 }
8232 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8233 ctxt->states = NULL;
8234 if (found == 0) {
8235 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
8236 ret = -1;
8237 ctxt->state = oldstate;
8238 goto done;
8239 }
8240 } else {
8241 ret = -1;
8242 break;
8243 }
8244 if (lasts[i] != NULL) {
8245 lasts[i]->next = last;
8246 }
8247 }
8248 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00008249 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008250 ctxt->state = oldstate;
8251 ctxt->state->seq = lastelem;
8252 if (ret != 0) {
8253 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
8254 ret = -1;
8255 goto done;
8256 }
8257
8258done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008259 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008260 /*
8261 * builds the next links chain from the prev one
8262 */
8263 cur = lastchg;
8264 while (cur != NULL) {
8265 if ((cur == start) || (cur->prev == NULL))
8266 break;
8267 cur->prev->next = cur;
8268 cur = cur->prev;
8269 }
8270 if (ret == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008271 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008272 }
8273
8274 xmlFree(list);
8275 xmlFree(lasts);
8276 return(ret);
8277}
8278
8279/**
8280 * xmlRelaxNGValidateDefinitionList:
8281 * @ctxt: a Relax-NG validation context
8282 * @define: the list of definition to verify
8283 *
8284 * Validate the given node content against the (list) of definitions
8285 *
8286 * Returns 0 if the validation succeeded or an error code.
8287 */
8288static int
8289xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
8290 xmlRelaxNGDefinePtr defines) {
8291 int ret = 0, res;
8292
8293
Daniel Veillard952379b2003-03-17 15:37:12 +00008294 if (defines == NULL) {
8295 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list");
8296 return(-1);
8297 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008298 while (defines != NULL) {
8299 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8300 res = xmlRelaxNGValidateDefinition(ctxt, defines);
8301 if (res < 0)
8302 ret = -1;
8303 } else {
8304 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8305 return(-1);
8306 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008307 if (res == -1) /* continues on -2 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00008308 break;
8309 defines = defines->next;
8310 }
8311
8312 return(ret);
8313}
8314
8315/**
8316 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00008317 * @ctxt: a Relax-NG validation context
8318 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00008319 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00008320 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008321 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00008322 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008323 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00008324 */
8325static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00008326xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
8327 xmlRelaxNGDefinePtr define,
8328 xmlNodePtr elem) {
Daniel Veillard580ced82003-03-21 21:22:48 +00008329 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008330
Daniel Veillardfd573f12003-03-16 17:52:32 +00008331 if (define->name != NULL) {
8332 if (!xmlStrEqual(elem->name, define->name)) {
8333 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
8334 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008335 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008336 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008337 if ((define->ns != NULL) && (define->ns[0] != 0)) {
8338 if (elem->ns == NULL) {
8339 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS,
8340 elem->name);
8341 return(0);
8342 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
8343 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
8344 elem->name, define->ns);
8345 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008346 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008347 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
8348 (define->name == NULL)) {
8349 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
8350 elem->name);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008351 return(0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008352 } else if ((elem->ns != NULL) && (define->name != NULL)) {
8353 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
8354 define->name);
8355 return(0);
8356 }
8357
8358 if (define->nameClass == NULL)
8359 return(1);
8360
8361 define = define->nameClass;
8362 if (define->type == XML_RELAXNG_EXCEPT) {
8363 xmlRelaxNGDefinePtr list;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008364 if (ctxt != NULL) {
8365 oldflags = ctxt->flags;
8366 ctxt->flags |= FLAGS_IGNORABLE;
8367 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008368
8369 list = define->content;
8370 while (list != NULL) {
8371 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
8372 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008373 if (ctxt != NULL)
8374 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008375 return(0);
8376 }
8377 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008378 if (ctxt != NULL)
8379 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008380 return(ret);
8381 }
8382 list = list->next;
8383 }
8384 ret = 1;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008385 if (ctxt != NULL) {
8386 ctxt->flags = oldflags;
8387 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008388 } else if (define->type == XML_RELAXNG_CHOICE) {
8389 xmlRelaxNGDefinePtr list;
8390
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008391 if (ctxt != NULL) {
8392 oldflags = ctxt->flags;
8393 ctxt->flags |= FLAGS_IGNORABLE;
8394 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008395
8396 list = define->nameClass;
8397 while (list != NULL) {
8398 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
8399 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008400 if (ctxt != NULL)
8401 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008402 return(1);
8403 }
8404 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008405 if (ctxt != NULL)
8406 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008407 return(ret);
8408 }
8409 list = list->next;
8410 }
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008411 if (ctxt != NULL) {
8412 if (ret != 0) {
8413 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8414 xmlRelaxNGDumpValidError(ctxt);
8415 } else {
8416 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
8417 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008418 }
8419 ret = 0;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008420 if (ctxt != NULL) {
8421 ctxt->flags = oldflags;
8422 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008423 } else {
8424 TODO
8425 ret = -1;
8426 }
8427 return(ret);
8428}
8429
8430/**
8431 * xmlRelaxNGValidateElementEnd:
8432 * @ctxt: a Relax-NG validation context
8433 *
8434 * Validate the end of the element, implements check that
8435 * there is nothing left not consumed in the element content
8436 * or in the attribute list.
8437 *
8438 * Returns 0 if the validation succeeded or an error code.
8439 */
8440static int
8441xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) {
8442 int ret = 0, i;
8443 xmlRelaxNGValidStatePtr state;
8444
8445 state = ctxt->state;
8446 if (state->seq != NULL) {
8447 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
8448 if (state->seq != NULL) {
8449 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
8450 state->node->name, state->seq->name);
8451 ret = -1;
8452 }
8453 }
8454 for (i = 0;i < state->nbAttrs;i++) {
8455 if (state->attrs[i] != NULL) {
8456 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
8457 state->attrs[i]->name, state->node->name);
8458 ret = -1;
8459 }
8460 }
8461 return(ret);
8462}
8463
8464/**
8465 * xmlRelaxNGValidateState:
8466 * @ctxt: a Relax-NG validation context
8467 * @define: the definition to verify
8468 *
8469 * Validate the current state against the definition
8470 *
8471 * Returns 0 if the validation succeeded or an error code.
8472 */
8473static int
8474xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
8475 xmlRelaxNGDefinePtr define) {
8476 xmlNodePtr node;
8477 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00008478 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008479
8480 if (define == NULL) {
8481 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8482 return(-1);
8483 }
8484
8485 if (ctxt->state != NULL) {
8486 node = ctxt->state->seq;
8487 } else {
8488 node = NULL;
8489 }
8490#ifdef DEBUG
8491 for (i = 0;i < ctxt->depth;i++)
8492 xmlGenericError(xmlGenericErrorContext, " ");
8493 xmlGenericError(xmlGenericErrorContext,
8494 "Start validating %s ", xmlRelaxNGDefName(define));
8495 if (define->name != NULL)
8496 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
8497 if ((node != NULL) && (node->name != NULL))
8498 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
8499 else
8500 xmlGenericError(xmlGenericErrorContext, "\n");
8501#endif
8502 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008503 switch (define->type) {
8504 case XML_RELAXNG_EMPTY:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008505 node = xmlRelaxNGSkipIgnored(ctxt, node);
8506 ret = 0;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008507 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008508 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008509 ret = -1;
8510 break;
8511 case XML_RELAXNG_TEXT:
8512 while ((node != NULL) &&
8513 ((node->type == XML_TEXT_NODE) ||
8514 (node->type == XML_COMMENT_NODE) ||
8515 (node->type == XML_PI_NODE) ||
8516 (node->type == XML_CDATA_SECTION_NODE)))
8517 node = node->next;
8518 ctxt->state->seq = node;
8519 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008520 case XML_RELAXNG_ELEMENT:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008521 errNr = ctxt->errNr;
8522 node = xmlRelaxNGSkipIgnored(ctxt, node);
8523 if (node == NULL) {
8524 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
8525 ret = -1;
8526 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8527 xmlRelaxNGDumpValidError(ctxt);
8528 break;
8529 }
8530 if (node->type != XML_ELEMENT_NODE) {
8531 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8532 ret = -1;
8533 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8534 xmlRelaxNGDumpValidError(ctxt);
8535 break;
8536 }
8537 /*
8538 * This node was already validated successfully against
8539 * this definition.
8540 */
8541 if (node->_private == define) {
8542 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
Daniel Veillard580ced82003-03-21 21:22:48 +00008543 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008544 if (ctxt->errNr != 0) {
8545 while ((ctxt->err != NULL) &&
8546 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
8547 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
Daniel Veillard580ced82003-03-21 21:22:48 +00008548 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
8549 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008550 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
8551 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
8552 xmlRelaxNGValidErrorPop(ctxt);
8553 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008554 break;
8555 }
8556
8557 ret = xmlRelaxNGElementMatch(ctxt, define, node);
8558 if (ret <= 0) {
8559 ret = -1;
8560 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8561 xmlRelaxNGDumpValidError(ctxt);
8562 break;
8563 }
8564 ret = 0;
8565 if (ctxt->errNr != 0) {
Daniel Veillard580ced82003-03-21 21:22:48 +00008566 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008567 while ((ctxt->err != NULL) &&
8568 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
8569 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
Daniel Veillard580ced82003-03-21 21:22:48 +00008570 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
8571 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00008572 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
8573 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
8574 xmlRelaxNGValidErrorPop(ctxt);
8575 }
8576 errNr = ctxt->errNr;
8577
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008578 oldflags = ctxt->flags;
8579 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
8580 ctxt->flags -= FLAGS_MIXED_CONTENT;
8581 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008582 state = xmlRelaxNGNewValidState(ctxt, node);
8583 if (state == NULL) {
8584 ret = -1;
8585 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8586 xmlRelaxNGDumpValidError(ctxt);
8587 break;
8588 }
8589
8590 oldstate = ctxt->state;
8591 ctxt->state = state;
8592 if (define->attrs != NULL) {
8593 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8594 if (tmp != 0) {
8595 ret = -1;
8596 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8597 }
8598 }
8599 if (define->content != NULL) {
8600 tmp = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8601 if (tmp != 0) {
8602 ret = -1;
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00008603 if (ctxt->state == NULL) {
8604 ctxt->state = oldstate;
8605 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name);
8606 ctxt->state = NULL;
8607 } else {
8608 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name);
8609 }
8610
Daniel Veillardfd573f12003-03-16 17:52:32 +00008611 }
8612 }
8613 if (ctxt->states != NULL) {
8614 tmp = -1;
8615
Daniel Veillardfd573f12003-03-16 17:52:32 +00008616 ctxt->flags |= FLAGS_IGNORABLE;
8617
8618 for (i = 0;i < ctxt->states->nbState;i++) {
8619 state = ctxt->states->tabState[i];
8620 ctxt->state = state;
8621
8622 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
8623 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +00008624 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008625 }
8626 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8627 ctxt->flags = oldflags;
8628 ctxt->states = NULL;
8629 if ((ret == 0) && (tmp == -1))
8630 ret = -1;
8631 } else {
8632 state = ctxt->state;
8633 if (ret == 0)
8634 ret = xmlRelaxNGValidateElementEnd(ctxt);
Daniel Veillard798024a2003-03-19 10:36:09 +00008635 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008636 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008637 if (ret == 0) {
8638 node->_private = define;
8639 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008640 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008641 ctxt->state = oldstate;
8642 if (oldstate != NULL)
8643 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008644 if (ret != 0) {
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008645 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008646 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008647 ret = 0;
8648 } else {
8649 ret = -2;
8650 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008651 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008652 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008653 }
8654
8655#ifdef DEBUG
8656 xmlGenericError(xmlGenericErrorContext,
8657 "xmlRelaxNGValidateDefinition(): validated %s : %d",
8658 node->name, ret);
8659 if (oldstate == NULL)
8660 xmlGenericError(xmlGenericErrorContext, ": no state\n");
8661 else if (oldstate->seq == NULL)
8662 xmlGenericError(xmlGenericErrorContext, ": done\n");
8663 else if (oldstate->seq->type == XML_ELEMENT_NODE)
8664 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
8665 oldstate->seq->name);
8666 else
8667 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
8668 oldstate->seq->name, oldstate->seq->type);
8669#endif
8670 break;
8671 case XML_RELAXNG_OPTIONAL: {
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008672 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008673 oldflags = ctxt->flags;
8674 ctxt->flags |= FLAGS_IGNORABLE;
8675 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
8676 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8677 if (ret != 0) {
8678 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00008679 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008680 ctxt->state = oldstate;
8681 ctxt->flags = oldflags;
8682 ret = 0;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008683 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008684 break;
8685 }
8686 if (ctxt->states != NULL) {
8687 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
8688 } else {
8689 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
8690 if (ctxt->states == NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008691 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008692 ctxt->flags = oldflags;
8693 ret = -1;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008694 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008695 break;
8696 }
8697 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
8698 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
8699 ctxt->state = NULL;
8700 }
8701 ctxt->flags = oldflags;
8702 ret = 0;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008703 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008704 break;
8705 }
8706 case XML_RELAXNG_ONEORMORE:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008707 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008708 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8709 if (ret != 0) {
8710 break;
8711 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008712 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008713 /* no break on purpose */
8714 case XML_RELAXNG_ZEROORMORE: {
8715 int progress;
8716 xmlRelaxNGStatesPtr states = NULL, res = NULL;
8717 int base, j;
8718
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008719 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008720 res = xmlRelaxNGNewStates(ctxt, 1);
8721 if (res == NULL) {
8722 ret = -1;
8723 break;
8724 }
8725 /*
8726 * All the input states are also exit states
8727 */
8728 if (ctxt->state != NULL) {
8729 xmlRelaxNGAddStates(ctxt, res,
8730 xmlRelaxNGCopyValidState(ctxt, ctxt->state));
8731 } else {
8732 for (j = 0;j < ctxt->states->nbState;j++) {
8733 xmlRelaxNGAddStates(ctxt, res,
8734 xmlRelaxNGCopyValidState(ctxt,
8735 ctxt->states->tabState[j]));
8736 }
8737 }
8738 oldflags = ctxt->flags;
8739 ctxt->flags |= FLAGS_IGNORABLE;
8740 do {
8741 progress = 0;
8742 base = res->nbState;
8743
8744 if (ctxt->states != NULL) {
8745 states = ctxt->states;
8746 for (i = 0;i < states->nbState;i++) {
8747 ctxt->state = states->tabState[i];
8748 ctxt->states = NULL;
8749 ret = xmlRelaxNGValidateDefinitionList(ctxt,
8750 define->content);
8751 if (ret == 0) {
8752 if (ctxt->state != NULL) {
8753 tmp = xmlRelaxNGAddStates(ctxt, res,
8754 ctxt->state);
8755 ctxt->state = NULL;
8756 if (tmp == 1)
8757 progress = 1;
8758 } else if (ctxt->states != NULL) {
8759 for (j = 0;j < ctxt->states->nbState;j++) {
8760 tmp = xmlRelaxNGAddStates(ctxt, res,
8761 ctxt->states->tabState[j]);
8762 if (tmp == 1)
8763 progress = 1;
8764 }
8765 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8766 ctxt->states = NULL;
8767 }
8768 } else {
8769 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008770 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008771 ctxt->state = NULL;
8772 }
8773 }
8774 }
8775 } else {
8776 ret = xmlRelaxNGValidateDefinitionList(ctxt,
8777 define->content);
8778 if (ret != 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008779 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008780 ctxt->state = NULL;
8781 } else {
8782 base = res->nbState;
8783 if (ctxt->state != NULL) {
8784 tmp = xmlRelaxNGAddStates(ctxt, res,
8785 ctxt->state);
8786 ctxt->state = NULL;
8787 if (tmp == 1)
8788 progress = 1;
8789 } else if (ctxt->states != NULL) {
8790 for (j = 0;j < ctxt->states->nbState;j++) {
8791 tmp = xmlRelaxNGAddStates(ctxt, res,
8792 ctxt->states->tabState[j]);
8793 if (tmp == 1)
8794 progress = 1;
8795 }
8796 if (states == NULL) {
8797 states = ctxt->states;
8798 } else {
8799 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8800 }
8801 ctxt->states = NULL;
8802 }
8803 }
8804 }
8805 if (progress) {
8806 /*
8807 * Collect all the new nodes added at that step
8808 * and make them the new node set
8809 */
8810 if (res->nbState - base == 1) {
8811 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
8812 res->tabState[base]);
8813 } else {
8814 if (states == NULL) {
8815 xmlRelaxNGNewStates(ctxt, res->nbState - base);
8816 }
8817 states->nbState = 0;
8818 for (i = base;i < res->nbState;i++)
8819 xmlRelaxNGAddStates(ctxt, states,
8820 xmlRelaxNGCopyValidState(ctxt,
8821 res->tabState[i]));
8822 ctxt->states = states;
8823 }
8824 }
8825 } while (progress == 1);
8826 if (states != NULL) {
8827 xmlRelaxNGFreeStates(ctxt, states);
8828 }
8829 ctxt->states = res;
8830 ctxt->flags = oldflags;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008831 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008832 ret = 0;
8833 break;
8834 }
8835 case XML_RELAXNG_CHOICE: {
Daniel Veillard580ced82003-03-21 21:22:48 +00008836 xmlRelaxNGDefinePtr list = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008837 xmlRelaxNGStatesPtr states = NULL;
8838
Daniel Veillarde063f482003-03-21 16:53:17 +00008839 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008840
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008841 errNr = ctxt->errNr;
Daniel Veillarde063f482003-03-21 16:53:17 +00008842 if ((define->dflags & IS_TRIABLE) && (define->data != NULL)) {
8843 xmlHashTablePtr triage = (xmlHashTablePtr) define->data;
8844
8845 /*
8846 * Something we can optimize cleanly there is only one
8847 * possble branch out !
8848 */
8849 if (node == NULL) {
8850 ret = -1;
8851 break;
8852 }
8853 if ((node->type == XML_TEXT_NODE) ||
8854 (node->type == XML_CDATA_SECTION_NODE)) {
8855 list = xmlHashLookup2(triage, BAD_CAST "#text", NULL);
8856 } else if (node->type == XML_ELEMENT_NODE) {
8857 if (node->ns != NULL) {
8858 list = xmlHashLookup2(triage, node->name,
8859 node->ns->href);
8860 if (list == NULL)
8861 list = xmlHashLookup2(triage, BAD_CAST "#any",
8862 node->ns->href);
8863 } else
8864 list = xmlHashLookup2(triage, node->name, NULL);
8865 if (list == NULL)
8866 list = xmlHashLookup2(triage, BAD_CAST "#any", NULL);
8867 }
8868 if (list == NULL) {
8869 ret = -1;
8870 break;
8871 }
8872 ret = xmlRelaxNGValidateDefinition(ctxt, list);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008873 if (ret == 0) {
8874 }
Daniel Veillarde063f482003-03-21 16:53:17 +00008875 break;
8876 }
8877
8878 list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008879 oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008880 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008881
8882 while (list != NULL) {
8883 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
8884 ret = xmlRelaxNGValidateDefinition(ctxt, list);
8885 if (ret == 0) {
8886 if (states == NULL) {
8887 states = xmlRelaxNGNewStates(ctxt, 1);
8888 }
8889 if (ctxt->state != NULL) {
8890 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
8891 } else if (ctxt->states != NULL) {
8892 for (i = 0;i < ctxt->states->nbState;i++) {
8893 xmlRelaxNGAddStates(ctxt, states,
8894 ctxt->states->tabState[i]);
8895 }
8896 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8897 ctxt->states = NULL;
8898 }
8899 } else {
Daniel Veillard798024a2003-03-19 10:36:09 +00008900 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008901 }
8902 ctxt->state = oldstate;
8903 list = list->next;
8904 }
8905 if (states != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008906 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008907 ctxt->states = states;
8908 ctxt->state = NULL;
8909 ret = 0;
8910 } else {
8911 ctxt->states = NULL;
8912 }
8913 ctxt->flags = oldflags;
8914 if (ret != 0) {
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008915 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008916 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008917 }
8918 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008919 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008920 }
8921 break;
8922 }
8923 case XML_RELAXNG_DEF:
8924 case XML_RELAXNG_GROUP:
8925 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008926 break;
8927 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008928 ret = xmlRelaxNGValidateInterleave(ctxt, define);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008929 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008930 case XML_RELAXNG_ATTRIBUTE:
8931 ret = xmlRelaxNGValidateAttribute(ctxt, define);
8932 break;
8933 case XML_RELAXNG_NOOP:
8934 case XML_RELAXNG_REF:
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008935 case XML_RELAXNG_EXTERNALREF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008936 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
8937 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00008938 case XML_RELAXNG_PARENTREF:
8939 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
8940 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008941 case XML_RELAXNG_DATATYPE: {
8942 xmlNodePtr child;
8943 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008944
Daniel Veillardfd573f12003-03-16 17:52:32 +00008945 child = node;
8946 while (child != NULL) {
8947 if (child->type == XML_ELEMENT_NODE) {
8948 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
8949 node->parent->name);
8950 ret = -1;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008951 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008952 } else if ((child->type == XML_TEXT_NODE) ||
8953 (child->type == XML_CDATA_SECTION_NODE)) {
8954 content = xmlStrcat(content, child->content);
8955 }
8956 /* TODO: handle entities ... */
8957 child = child->next;
8958 }
8959 if (ret == -1) {
8960 if (content != NULL)
8961 xmlFree(content);
8962 break;
8963 }
8964 if (content == NULL) {
8965 content = xmlStrdup(BAD_CAST "");
8966 if (content == NULL) {
8967 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8968 ret = -1;
8969 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008970 }
8971 }
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008972 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
8973 ctxt->state->seq);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008974 if (ret == -1) {
8975 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
8976 } else if (ret == 0) {
8977 ctxt->state->seq = NULL;
8978 }
8979 if (content != NULL)
8980 xmlFree(content);
8981 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008982 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008983 case XML_RELAXNG_VALUE: {
8984 xmlChar *content = NULL;
8985 xmlChar *oldvalue;
8986 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008987
Daniel Veillardfd573f12003-03-16 17:52:32 +00008988 child = node;
8989 while (child != NULL) {
8990 if (child->type == XML_ELEMENT_NODE) {
8991 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
8992 node->parent->name);
8993 ret = -1;
8994 break;
8995 } else if ((child->type == XML_TEXT_NODE) ||
8996 (child->type == XML_CDATA_SECTION_NODE)) {
8997 content = xmlStrcat(content, child->content);
8998 }
8999 /* TODO: handle entities ... */
9000 child = child->next;
9001 }
9002 if (ret == -1) {
9003 if (content != NULL)
9004 xmlFree(content);
9005 break;
9006 }
9007 if (content == NULL) {
9008 content = xmlStrdup(BAD_CAST "");
9009 if (content == NULL) {
9010 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
9011 ret = -1;
9012 break;
9013 }
9014 }
9015 oldvalue = ctxt->state->value;
9016 ctxt->state->value = content;
9017 ret = xmlRelaxNGValidateValue(ctxt, define);
9018 ctxt->state->value = oldvalue;
9019 if (ret == -1) {
9020 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
9021 } else if (ret == 0) {
9022 ctxt->state->seq = NULL;
9023 }
9024 if (content != NULL)
9025 xmlFree(content);
9026 break;
9027 }
9028 case XML_RELAXNG_LIST: {
9029 xmlChar *content;
9030 xmlNodePtr child;
9031 xmlChar *oldvalue, *oldendvalue;
9032 int len;
9033
9034 /*
9035 * Make sure it's only text nodes
9036 */
9037
9038 content = NULL;
9039 child = node;
9040 while (child != NULL) {
9041 if (child->type == XML_ELEMENT_NODE) {
9042 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
9043 node->parent->name);
9044 ret = -1;
9045 break;
9046 } else if ((child->type == XML_TEXT_NODE) ||
9047 (child->type == XML_CDATA_SECTION_NODE)) {
9048 content = xmlStrcat(content, child->content);
9049 }
9050 /* TODO: handle entities ... */
9051 child = child->next;
9052 }
9053 if (ret == -1) {
9054 if (content != NULL)
9055 xmlFree(content);
9056 break;
9057 }
9058 if (content == NULL) {
9059 content = xmlStrdup(BAD_CAST "");
9060 if (content == NULL) {
9061 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
9062 ret = -1;
9063 break;
9064 }
9065 }
9066 len = xmlStrlen(content);
9067 oldvalue = ctxt->state->value;
9068 oldendvalue = ctxt->state->endvalue;
9069 ctxt->state->value = content;
9070 ctxt->state->endvalue = content + len;
9071 ret = xmlRelaxNGValidateValue(ctxt, define);
9072 ctxt->state->value = oldvalue;
9073 ctxt->state->endvalue = oldendvalue;
9074 if (ret == -1) {
9075 VALID_ERR(XML_RELAXNG_ERR_LIST);
9076 } else if ((ret == 0) && (node != NULL)) {
9077 ctxt->state->seq = node->next;
9078 }
9079 if (content != NULL)
9080 xmlFree(content);
9081 break;
9082 }
9083 case XML_RELAXNG_START:
9084 case XML_RELAXNG_EXCEPT:
9085 case XML_RELAXNG_PARAM:
9086 TODO
9087 ret = -1;
9088 break;
9089 }
9090 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009091#ifdef DEBUG
Daniel Veillardfd573f12003-03-16 17:52:32 +00009092 for (i = 0;i < ctxt->depth;i++)
9093 xmlGenericError(xmlGenericErrorContext, " ");
9094 xmlGenericError(xmlGenericErrorContext,
9095 "Validating %s ", xmlRelaxNGDefName(define));
9096 if (define->name != NULL)
9097 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
9098 if (ret == 0)
9099 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
9100 else
9101 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009102#endif
Daniel Veillardfd573f12003-03-16 17:52:32 +00009103 return(ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009104}
9105
9106/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009107 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009108 * @ctxt: a Relax-NG validation context
9109 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009110 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009111 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009112 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009113 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009114 */
9115static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00009116xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
9117 xmlRelaxNGDefinePtr define) {
9118 xmlRelaxNGStatesPtr states, res;
9119 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009120
Daniel Veillardfd573f12003-03-16 17:52:32 +00009121 /*
9122 * We should NOT have both ctxt->state and ctxt->states
9123 */
9124 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
9125 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00009126 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009127 ctxt->state = NULL;
9128 }
9129
9130 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
9131 if (ctxt->states != NULL) {
9132 ctxt->state = ctxt->states->tabState[0];
9133 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9134 ctxt->states = NULL;
9135 }
9136 ret = xmlRelaxNGValidateState(ctxt, define);
9137 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
9138 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00009139 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009140 ctxt->state = NULL;
9141 }
9142 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
9143 ctxt->state = ctxt->states->tabState[0];
9144 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9145 ctxt->states = NULL;
9146 }
9147 return(ret);
9148 }
9149
9150 states = ctxt->states;
9151 ctxt->states = NULL;
9152 res = NULL;
9153 j = 0;
9154 oldflags = ctxt->flags;
9155 ctxt->flags |= FLAGS_IGNORABLE;
9156 for (i = 0;i < states->nbState;i++) {
9157 ctxt->state = states->tabState[i];
9158 ctxt->states = NULL;
9159 ret = xmlRelaxNGValidateState(ctxt, define);
9160 /*
9161 * We should NOT have both ctxt->state and ctxt->states
9162 */
9163 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
9164 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00009165 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009166 ctxt->state = NULL;
9167 }
9168 if (ret == 0) {
9169 if (ctxt->states == NULL) {
9170 if (res != NULL) {
9171 /* add the state to the container */
9172 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
9173 ctxt->state = NULL;
9174 } else {
9175 /* add the state directly in states */
9176 states->tabState[j++] = ctxt->state;
9177 ctxt->state = NULL;
9178 }
9179 } else {
9180 if (res == NULL) {
9181 /* make it the new container and copy other results */
9182 res = ctxt->states;
9183 ctxt->states = NULL;
9184 for (k = 0;k < j;k++)
9185 xmlRelaxNGAddStates(ctxt, res, states->tabState[k]);
9186 } else {
9187 /* add all the new results to res and reff the container */
9188 for (k = 0;k < ctxt->states->nbState;k++)
9189 xmlRelaxNGAddStates(ctxt, res,
9190 ctxt->states->tabState[k]);
9191 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9192 ctxt->states = NULL;
9193 }
9194 }
9195 } else {
9196 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00009197 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009198 ctxt->state = NULL;
9199 } else if (ctxt->states != NULL) {
9200 for (k = 0;k < ctxt->states->nbState;k++)
Daniel Veillard798024a2003-03-19 10:36:09 +00009201 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009202 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9203 ctxt->states = NULL;
9204 }
9205 }
9206 }
9207 ctxt->flags = oldflags;
9208 if (res != NULL) {
9209 xmlRelaxNGFreeStates(ctxt, states);
9210 ctxt->states = res;
9211 ret = 0;
9212 } else if (j > 1) {
9213 states->nbState = j;
9214 ctxt->states = states;
9215 ret =0;
9216 } else if (j == 1) {
9217 ctxt->state = states->tabState[0];
9218 xmlRelaxNGFreeStates(ctxt, states);
9219 ret = 0;
9220 } else {
9221 ret = -1;
9222 xmlRelaxNGFreeStates(ctxt, states);
9223 if (ctxt->states != NULL) {
9224 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9225 ctxt->states = NULL;
9226 }
9227 }
9228 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
9229 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00009230 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009231 ctxt->state = NULL;
9232 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009233 return(ret);
9234}
9235
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009236/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009237 * xmlRelaxNGValidateDocument:
9238 * @ctxt: a Relax-NG validation context
9239 * @doc: the document
9240 *
9241 * Validate the given document
9242 *
9243 * Returns 0 if the validation succeeded or an error code.
9244 */
9245static int
9246xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
9247 int ret;
9248 xmlRelaxNGPtr schema;
9249 xmlRelaxNGGrammarPtr grammar;
9250 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009251 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009252
9253 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
9254 return(-1);
9255
Daniel Veillarda507fbf2003-03-31 16:09:37 +00009256 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009257 schema = ctxt->schema;
9258 grammar = schema->topgrammar;
9259 if (grammar == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00009260 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009261 return(-1);
9262 }
9263 state = xmlRelaxNGNewValidState(ctxt, NULL);
9264 ctxt->state = state;
9265 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009266 if ((ctxt->state != NULL) && (state->seq != NULL)) {
9267 state = ctxt->state;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009268 node = state->seq;
9269 node = xmlRelaxNGSkipIgnored(ctxt, node);
9270 if (node != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00009271 if (ret != -1) {
9272 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
9273 ret = -1;
9274 }
9275 }
9276 } else if (ctxt->states != NULL) {
9277 int i;
9278 int tmp = -1;
9279
9280 for (i = 0;i < ctxt->states->nbState;i++) {
9281 state = ctxt->states->tabState[i];
9282 node = state->seq;
9283 node = xmlRelaxNGSkipIgnored(ctxt, node);
9284 if (node == NULL)
9285 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +00009286 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009287 }
9288 if (tmp == -1) {
9289 if (ret != -1) {
9290 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
9291 ret = -1;
9292 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009293 }
9294 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009295 if (ctxt->state != NULL) {
9296 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9297 ctxt->state = NULL;
9298 }
Daniel Veillard580ced82003-03-21 21:22:48 +00009299 if (ret != 0)
Daniel Veillardfd573f12003-03-16 17:52:32 +00009300 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +00009301#ifdef DEBUG
9302 else if (ctxt->errNr != 0) {
9303 ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n",
9304 ctxt->errNr);
9305 xmlRelaxNGDumpValidError(ctxt);
9306 }
9307#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009308 if (ctxt->idref == 1) {
9309 xmlValidCtxt vctxt;
9310
9311 memset(&vctxt, 0, sizeof(xmlValidCtxt));
9312 vctxt.valid = 1;
9313 vctxt.error = ctxt->error;
9314 vctxt.warning = ctxt->warning;
9315 vctxt.userData = ctxt->userData;
9316
9317 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
9318 ret = -1;
9319 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00009320 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
9321 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009322
9323 return(ret);
9324}
9325
Daniel Veillardfd573f12003-03-16 17:52:32 +00009326/************************************************************************
9327 * *
9328 * Validation interfaces *
9329 * *
9330 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +00009331/**
9332 * xmlRelaxNGNewValidCtxt:
9333 * @schema: a precompiled XML RelaxNGs
9334 *
9335 * Create an XML RelaxNGs validation context based on the given schema
9336 *
9337 * Returns the validation context or NULL in case of error
9338 */
9339xmlRelaxNGValidCtxtPtr
9340xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) {
9341 xmlRelaxNGValidCtxtPtr ret;
9342
9343 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
9344 if (ret == NULL) {
9345 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardfd573f12003-03-16 17:52:32 +00009346 "Failed to allocate new schema validation context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00009347 return (NULL);
9348 }
9349 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
9350 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00009351 ret->error = xmlGenericError;
9352 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +00009353 ret->errNr = 0;
9354 ret->errMax = 0;
9355 ret->err = NULL;
9356 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009357 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +00009358 ret->states = NULL;
9359 ret->freeState = NULL;
9360 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00009361 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009362 return (ret);
9363}
9364
9365/**
9366 * xmlRelaxNGFreeValidCtxt:
9367 * @ctxt: the schema validation context
9368 *
9369 * Free the resources associated to the schema validation context
9370 */
9371void
9372xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard798024a2003-03-19 10:36:09 +00009373 int k;
9374
Daniel Veillard6eadf632003-01-23 18:29:16 +00009375 if (ctxt == NULL)
9376 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009377 if (ctxt->states != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00009378 xmlRelaxNGFreeStates(NULL, ctxt->states);
9379 if (ctxt->freeState != NULL) {
9380 for (k = 0;k < ctxt->freeState->nbState;k++) {
9381 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
9382 }
9383 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
9384 }
Daniel Veillard798024a2003-03-19 10:36:09 +00009385 if (ctxt->freeStates != NULL) {
9386 for (k = 0;k < ctxt->freeStatesNr;k++) {
9387 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
9388 }
9389 xmlFree(ctxt->freeStates);
9390 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00009391 if (ctxt->errTab != NULL)
9392 xmlFree(ctxt->errTab);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009393 xmlFree(ctxt);
9394}
9395
9396/**
9397 * xmlRelaxNGSetValidErrors:
9398 * @ctxt: a Relax-NG validation context
9399 * @err: the error function
9400 * @warn: the warning function
9401 * @ctx: the functions context
9402 *
9403 * Set the error and warning callback informations
9404 */
9405void
9406xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
9407 xmlRelaxNGValidityErrorFunc err,
9408 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
9409 if (ctxt == NULL)
9410 return;
9411 ctxt->error = err;
9412 ctxt->warning = warn;
9413 ctxt->userData = ctx;
9414}
9415
9416/**
9417 * xmlRelaxNGValidateDoc:
9418 * @ctxt: a Relax-NG validation context
9419 * @doc: a parsed document tree
9420 *
9421 * Validate a document tree in memory.
9422 *
9423 * Returns 0 if the document is valid, a positive error code
9424 * number otherwise and -1 in case of internal or API error.
9425 */
9426int
9427xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
9428 int ret;
9429
9430 if ((ctxt == NULL) || (doc == NULL))
9431 return(-1);
9432
9433 ctxt->doc = doc;
9434
9435 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +00009436 /*
9437 * TODO: build error codes
9438 */
9439 if (ret == -1)
9440 return(1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009441 return(ret);
9442}
9443
9444#endif /* LIBXML_SCHEMAS_ENABLED */
9445