blob: a916fda58e652ed8e62d70a2a60678900f75405f [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 Veillard6eadf632003-01-23 18:29:16 +000058
59#define UNBOUNDED (1 << 30)
60#define TODO \
61 xmlGenericError(xmlGenericErrorContext, \
62 "Unimplemented block at %s:%d\n", \
63 __FILE__, __LINE__);
64
65typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
66typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
67
68typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
69typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
70
Daniel Veillardd41f4f42003-01-29 21:07:52 +000071typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
72typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
73
Daniel Veillarda9d912d2003-02-01 17:43:10 +000074typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
75typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
76
Daniel Veillard6eadf632003-01-23 18:29:16 +000077typedef enum {
78 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
79 XML_RELAXNG_COMBINE_CHOICE, /* choice */
80 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
81} xmlRelaxNGCombine;
82
Daniel Veillard4c5cf702003-02-21 15:40:34 +000083typedef enum {
84 XML_RELAXNG_CONTENT_ERROR = -1,
85 XML_RELAXNG_CONTENT_EMPTY = 0,
86 XML_RELAXNG_CONTENT_SIMPLE,
87 XML_RELAXNG_CONTENT_COMPLEX
88} xmlRelaxNGContentType;
89
Daniel Veillard6eadf632003-01-23 18:29:16 +000090typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
91typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
92
93struct _xmlRelaxNGGrammar {
94 xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */
95 xmlRelaxNGGrammarPtr children;/* the children grammar if any */
96 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
97 xmlRelaxNGDefinePtr start; /* <start> content */
98 xmlRelaxNGCombine combine; /* the default combine value */
Daniel Veillardd41f4f42003-01-29 21:07:52 +000099 xmlRelaxNGDefinePtr startList;/* list of <start> definitions */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000100 xmlHashTablePtr defs; /* define* */
101 xmlHashTablePtr refs; /* references */
102};
103
104
Daniel Veillard6eadf632003-01-23 18:29:16 +0000105typedef enum {
Daniel Veillard77648bb2003-02-20 15:03:22 +0000106 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000107 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
108 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard144fae12003-02-03 13:17:57 +0000109 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000110 XML_RELAXNG_TEXT, /* textual content */
111 XML_RELAXNG_ELEMENT, /* an element */
112 XML_RELAXNG_DATATYPE, /* extenal data type definition */
Daniel Veillard8fe98712003-02-19 00:19:14 +0000113 XML_RELAXNG_PARAM, /* extenal data type parameter */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000114 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
115 XML_RELAXNG_LIST, /* a list of patterns */
116 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
117 XML_RELAXNG_DEF, /* a definition */
118 XML_RELAXNG_REF, /* reference to a definition */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000119 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
Daniel Veillard419a7682003-02-03 23:22:49 +0000120 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000121 XML_RELAXNG_OPTIONAL, /* optional patterns */
122 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000123 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
124 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
125 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000126 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000127 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000128} xmlRelaxNGType;
129
Daniel Veillardfd573f12003-03-16 17:52:32 +0000130#define IS_NULLABLE 1
131#define IS_NOT_NULLABLE 2
132#define IS_INDETERMINIST 4
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000133#define IS_MIXED 8
Daniel Veillarde063f482003-03-21 16:53:17 +0000134#define IS_TRIABLE 16
135#define IS_PROCESSED 32
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000136
Daniel Veillard6eadf632003-01-23 18:29:16 +0000137struct _xmlRelaxNGDefine {
138 xmlRelaxNGType type; /* the type of definition */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000139 xmlNodePtr node; /* the node in the source */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000140 xmlChar *name; /* the element local name if present */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000141 xmlChar *ns; /* the namespace local name if present */
Daniel Veillardedc91922003-01-26 00:52:04 +0000142 xmlChar *value; /* value when available */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000143 void *data; /* data lib or specific pointer */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000144 xmlRelaxNGDefinePtr content;/* the expected content */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000145 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000146 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000147 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
Daniel Veillard3b2e4e12003-02-03 08:52:58 +0000148 xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000149 xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000150 short depth; /* used for the cycle detection */
Daniel Veillarde063f482003-03-21 16:53:17 +0000151 short dflags; /* define related flags */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000152};
153
154/**
155 * _xmlRelaxNG:
156 *
157 * A RelaxNGs definition
158 */
159struct _xmlRelaxNG {
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000160 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000161 xmlRelaxNGGrammarPtr topgrammar;
162 xmlDocPtr doc;
163
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000164 int idref; /* requires idref checking */
165
Daniel Veillard6eadf632003-01-23 18:29:16 +0000166 xmlHashTablePtr defs; /* define */
167 xmlHashTablePtr refs; /* references */
Daniel Veillardc482e262003-02-26 14:48:48 +0000168 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
169 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard419a7682003-02-03 23:22:49 +0000170 int defNr; /* number of defines used */
171 xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000172
Daniel Veillard6eadf632003-01-23 18:29:16 +0000173};
174
Daniel Veillard77648bb2003-02-20 15:03:22 +0000175#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
176#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
177#define XML_RELAXNG_IN_LIST (1 << 2)
178#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
179#define XML_RELAXNG_IN_START (1 << 4)
180#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
181#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
182#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000183#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
184#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000185
186struct _xmlRelaxNGParserCtxt {
187 void *userData; /* user specific data block */
188 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
189 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000190 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000191
192 xmlRelaxNGPtr schema; /* The schema in use */
193 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
Daniel Veillard419a7682003-02-03 23:22:49 +0000194 xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000195 int flags; /* parser flags */
196 int nbErrors; /* number of errors at parse time */
197 int nbWarnings; /* number of warnings at parse time */
Daniel Veillard276be4a2003-01-24 01:03:34 +0000198 const xmlChar *define; /* the current define scope */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000199 xmlRelaxNGDefinePtr def; /* the current define */
200
201 int nbInterleaves;
202 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000203
Daniel Veillardc482e262003-02-26 14:48:48 +0000204 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
205 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000206 xmlChar *URL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000207 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000208
Daniel Veillard419a7682003-02-03 23:22:49 +0000209 int defNr; /* number of defines used */
210 int defMax; /* number of defines aloocated */
211 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
212
Daniel Veillard6eadf632003-01-23 18:29:16 +0000213 const char *buffer;
214 int size;
215
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000216 /* the document stack */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000217 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000218 int docNr; /* Depth of the parsing stack */
219 int docMax; /* Max depth of the parsing stack */
220 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000221
222 /* the include stack */
Daniel Veillardd4310742003-02-18 21:12:46 +0000223 xmlRelaxNGIncludePtr inc; /* Current parsed include */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000224 int incNr; /* Depth of the include parsing stack */
225 int incMax; /* Max depth of the parsing stack */
226 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000227
228 int idref; /* requires idref checking */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000229};
230
231#define FLAGS_IGNORABLE 1
232#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000233#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000234
235/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000236 * xmlRelaxNGInterleaveGroup:
237 *
238 * A RelaxNGs partition set associated to lists of definitions
239 */
240typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
241typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
242struct _xmlRelaxNGInterleaveGroup {
243 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
244 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000245 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000246};
247
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000248#define IS_DETERMINIST 1
249#define IS_NEEDCHECK 2
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000250/**
251 * xmlRelaxNGPartitions:
252 *
253 * A RelaxNGs partition associated to an interleave group
254 */
255typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
256typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
257struct _xmlRelaxNGPartition {
258 int nbgroups; /* number of groups in the partitions */
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000259 xmlHashTablePtr triage; /* hash table used to direct nodes to the
260 right group when possible */
261 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000262 xmlRelaxNGInterleaveGroupPtr *groups;
263};
264
265/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000266 * xmlRelaxNGValidState:
267 *
268 * A RelaxNGs validation state
269 */
270#define MAX_ATTR 20
271typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
272typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
273struct _xmlRelaxNGValidState {
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000274 xmlNodePtr node; /* the current node */
275 xmlNodePtr seq; /* the sequence of children left to validate */
276 int nbAttrs; /* the number of attributes */
Daniel Veillard798024a2003-03-19 10:36:09 +0000277 int maxAttrs; /* the size of attrs */
Daniel Veillard1ed7f362003-02-03 10:57:45 +0000278 int nbAttrLeft; /* the number of attributes left to validate */
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000279 xmlChar *value; /* the value when operating on string */
280 xmlChar *endvalue; /* the end value when operating on string */
Daniel Veillard798024a2003-03-19 10:36:09 +0000281 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000282};
283
284/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000285 * xmlRelaxNGStates:
286 *
287 * A RelaxNGs container for validation state
288 */
289typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
290typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
291struct _xmlRelaxNGStates {
292 int nbState; /* the number of states */
293 int maxState; /* the size of the array */
294 xmlRelaxNGValidStatePtr *tabState;
295};
296
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000297#define ERROR_IS_DUP 1
Daniel Veillardfd573f12003-03-16 17:52:32 +0000298/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000299 * xmlRelaxNGValidError:
300 *
301 * A RelaxNGs validation error
302 */
303typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
304typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
305struct _xmlRelaxNGValidError {
306 xmlRelaxNGValidErr err; /* the error number */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000307 int flags; /* flags */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000308 xmlNodePtr node; /* the current node */
309 xmlNodePtr seq; /* the current child */
310 const xmlChar * arg1; /* first arg */
311 const xmlChar * arg2; /* second arg */
312};
313
314/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000315 * xmlRelaxNGValidCtxt:
316 *
317 * A RelaxNGs validation context
318 */
319
320struct _xmlRelaxNGValidCtxt {
321 void *userData; /* user specific data block */
322 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
323 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
324
325 xmlRelaxNGPtr schema; /* The schema in use */
326 xmlDocPtr doc; /* the document being validated */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000327 int flags; /* validation flags */
Daniel Veillard231d7912003-02-09 14:22:17 +0000328 int depth; /* validation depth */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000329 int idref; /* requires idref checking */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000330
331 /*
332 * Errors accumulated in branches may have to be stacked to be
333 * provided back when it's sure they affect validation.
334 */
335 xmlRelaxNGValidErrorPtr err; /* Last error */
336 int errNr; /* Depth of the error stack */
337 int errMax; /* Max depth of the error stack */
338 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000339
Daniel Veillardfd573f12003-03-16 17:52:32 +0000340 xmlRelaxNGValidStatePtr state; /* the current validation state */
341 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000342
343 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
344 int freeStatesNr;
345 int freeStatesMax;
346 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000347};
348
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000349/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000350 * xmlRelaxNGInclude:
351 *
352 * Structure associated to a RelaxNGs document element
353 */
354struct _xmlRelaxNGInclude {
Daniel Veillardc482e262003-02-26 14:48:48 +0000355 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000356 xmlChar *href; /* the normalized href value */
357 xmlDocPtr doc; /* the associated XML document */
358 xmlRelaxNGDefinePtr content;/* the definitions */
359 xmlRelaxNGPtr schema; /* the schema */
360};
361
362/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000363 * xmlRelaxNGDocument:
364 *
365 * Structure associated to a RelaxNGs document element
366 */
367struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000368 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000369 xmlChar *href; /* the normalized href value */
370 xmlDocPtr doc; /* the associated XML document */
371 xmlRelaxNGDefinePtr content;/* the definitions */
372 xmlRelaxNGPtr schema; /* the schema */
373};
374
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000375
Daniel Veillard6eadf632003-01-23 18:29:16 +0000376/************************************************************************
377 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000378 * Preliminary type checking interfaces *
379 * *
380 ************************************************************************/
381/**
382 * xmlRelaxNGTypeHave:
383 * @data: data needed for the library
384 * @type: the type name
385 * @value: the value to check
386 *
387 * Function provided by a type library to check if a type is exported
388 *
389 * Returns 1 if yes, 0 if no and -1 in case of error.
390 */
391typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type);
392
393/**
394 * xmlRelaxNGTypeCheck:
395 * @data: data needed for the library
396 * @type: the type name
397 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000398 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000399 *
400 * Function provided by a type library to check if a value match a type
401 *
402 * Returns 1 if yes, 0 if no and -1 in case of error.
403 */
404typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type,
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000405 const xmlChar *value, void **result,
406 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000407
408/**
409 * xmlRelaxNGFacetCheck:
410 * @data: data needed for the library
411 * @type: the type name
412 * @facet: the facet name
413 * @val: the facet value
414 * @strval: the string value
415 * @value: the value to check
416 *
417 * Function provided by a type library to check a value facet
418 *
419 * Returns 1 if yes, 0 if no and -1 in case of error.
420 */
421typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type,
422 const xmlChar *facet, const xmlChar *val,
423 const xmlChar *strval, void *value);
424
425/**
426 * xmlRelaxNGTypeFree:
427 * @data: data needed for the library
428 * @result: the value to free
429 *
430 * Function provided by a type library to free a returned result
431 */
432typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000433
434/**
435 * xmlRelaxNGTypeCompare:
436 * @data: data needed for the library
437 * @type: the type name
438 * @value1: the first value
439 * @value2: the second value
440 *
441 * Function provided by a type library to compare two values accordingly
442 * to a type.
443 *
444 * Returns 1 if yes, 0 if no and -1 in case of error.
445 */
446typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type,
447 const xmlChar *value1,
448 const xmlChar *value2);
449typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
450typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
451struct _xmlRelaxNGTypeLibrary {
452 const xmlChar *namespace; /* the datatypeLibrary value */
453 void *data; /* data needed for the library */
454 xmlRelaxNGTypeHave have; /* the export function */
455 xmlRelaxNGTypeCheck check; /* the checking function */
456 xmlRelaxNGTypeCompare comp; /* the compare function */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000457 xmlRelaxNGFacetCheck facet; /* the facet check function */
458 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000459};
460
461/************************************************************************
462 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000463 * Allocation functions *
464 * *
465 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000466static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
467static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillardd2298792003-02-14 16:54:11 +0000468static void xmlRelaxNGNormExtSpace(xmlChar *value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000469static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000470static int xmlRelaxNGEqualValidState(
471 xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
472 xmlRelaxNGValidStatePtr state1,
473 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000474static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
475 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000476
477/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000478 * xmlRelaxNGFreeDocument:
479 * @docu: a document structure
480 *
481 * Deallocate a RelaxNG document structure.
482 */
483static void
484xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
485{
486 if (docu == NULL)
487 return;
488
489 if (docu->href != NULL)
490 xmlFree(docu->href);
491 if (docu->doc != NULL)
492 xmlFreeDoc(docu->doc);
493 if (docu->schema != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000494 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000495 xmlFree(docu);
496}
497
498/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000499 * xmlRelaxNGFreeDocumentList:
500 * @docu: a list of document structure
501 *
502 * Deallocate a RelaxNG document structures.
503 */
504static void
505xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
506{
507 xmlRelaxNGDocumentPtr next;
508 while (docu != NULL) {
509 next = docu->next;
510 xmlRelaxNGFreeDocument(docu);
511 docu = next;
512 }
513}
514
515/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000516 * xmlRelaxNGFreeInclude:
517 * @incl: a include structure
518 *
519 * Deallocate a RelaxNG include structure.
520 */
521static void
522xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
523{
524 if (incl == NULL)
525 return;
526
527 if (incl->href != NULL)
528 xmlFree(incl->href);
529 if (incl->doc != NULL)
530 xmlFreeDoc(incl->doc);
531 if (incl->schema != NULL)
532 xmlRelaxNGFree(incl->schema);
533 xmlFree(incl);
534}
535
536/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000537 * xmlRelaxNGFreeIncludeList:
538 * @incl: a include structure list
539 *
540 * Deallocate a RelaxNG include structure.
541 */
542static void
543xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
544{
545 xmlRelaxNGIncludePtr next;
546 while (incl != NULL) {
547 next = incl->next;
548 xmlRelaxNGFreeInclude(incl);
549 incl = next;
550 }
551}
552
553/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000554 * xmlRelaxNGNewRelaxNG:
555 * @ctxt: a Relax-NG validation context (optional)
556 *
557 * Allocate a new RelaxNG structure.
558 *
559 * Returns the newly allocated structure or NULL in case or error
560 */
561static xmlRelaxNGPtr
562xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
563{
564 xmlRelaxNGPtr ret;
565
566 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
567 if (ret == NULL) {
568 if ((ctxt != NULL) && (ctxt->error != NULL))
569 ctxt->error(ctxt->userData, "Out of memory\n");
570 ctxt->nbErrors++;
571 return (NULL);
572 }
573 memset(ret, 0, sizeof(xmlRelaxNG));
574
575 return (ret);
576}
577
578/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000579 * xmlRelaxNGFreeInnerSchema:
580 * @schema: a schema structure
581 *
582 * Deallocate a RelaxNG schema structure.
583 */
584static void
585xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
586{
587 if (schema == NULL)
588 return;
589
590 if (schema->doc != NULL)
591 xmlFreeDoc(schema->doc);
592 if (schema->defTab != NULL) {
593 int i;
594
595 for (i = 0;i < schema->defNr;i++)
596 xmlRelaxNGFreeDefine(schema->defTab[i]);
597 xmlFree(schema->defTab);
598 }
599
600 xmlFree(schema);
601}
602
603/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000604 * xmlRelaxNGFree:
605 * @schema: a schema structure
606 *
607 * Deallocate a RelaxNG structure.
608 */
609void
610xmlRelaxNGFree(xmlRelaxNGPtr schema)
611{
612 if (schema == NULL)
613 return;
614
Daniel Veillard6eadf632003-01-23 18:29:16 +0000615 if (schema->topgrammar != NULL)
616 xmlRelaxNGFreeGrammar(schema->topgrammar);
617 if (schema->doc != NULL)
618 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000619 if (schema->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000620 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000621 if (schema->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000622 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000623 if (schema->defTab != NULL) {
624 int i;
625
626 for (i = 0;i < schema->defNr;i++)
627 xmlRelaxNGFreeDefine(schema->defTab[i]);
628 xmlFree(schema->defTab);
629 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000630
631 xmlFree(schema);
632}
633
634/**
635 * xmlRelaxNGNewGrammar:
636 * @ctxt: a Relax-NG validation context (optional)
637 *
638 * Allocate a new RelaxNG grammar.
639 *
640 * Returns the newly allocated structure or NULL in case or error
641 */
642static xmlRelaxNGGrammarPtr
643xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
644{
645 xmlRelaxNGGrammarPtr ret;
646
647 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
648 if (ret == NULL) {
649 if ((ctxt != NULL) && (ctxt->error != NULL))
650 ctxt->error(ctxt->userData, "Out of memory\n");
651 ctxt->nbErrors++;
652 return (NULL);
653 }
654 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
655
656 return (ret);
657}
658
659/**
660 * xmlRelaxNGFreeGrammar:
661 * @grammar: a grammar structure
662 *
663 * Deallocate a RelaxNG grammar structure.
664 */
665static void
666xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
667{
668 if (grammar == NULL)
669 return;
670
Daniel Veillardc482e262003-02-26 14:48:48 +0000671 if (grammar->children != NULL) {
672 xmlRelaxNGFreeGrammar(grammar->children);
673 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000674 if (grammar->next != NULL) {
675 xmlRelaxNGFreeGrammar(grammar->next);
676 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000677 if (grammar->refs != NULL) {
678 xmlHashFree(grammar->refs, NULL);
679 }
680 if (grammar->defs != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000681 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000682 }
683
684 xmlFree(grammar);
685}
686
687/**
688 * xmlRelaxNGNewDefine:
689 * @ctxt: a Relax-NG validation context
690 * @node: the node in the input document.
691 *
692 * Allocate a new RelaxNG define.
693 *
694 * Returns the newly allocated structure or NULL in case or error
695 */
696static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000697xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000698{
699 xmlRelaxNGDefinePtr ret;
700
Daniel Veillard419a7682003-02-03 23:22:49 +0000701 if (ctxt->defMax == 0) {
702 ctxt->defMax = 16;
703 ctxt->defNr = 0;
704 ctxt->defTab = (xmlRelaxNGDefinePtr *)
705 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
706 if (ctxt->defTab == NULL) {
707 if ((ctxt != NULL) && (ctxt->error != NULL))
708 ctxt->error(ctxt->userData, "Out of memory\n");
709 ctxt->nbErrors++;
710 return (NULL);
711 }
712 } else if (ctxt->defMax <= ctxt->defNr) {
713 xmlRelaxNGDefinePtr *tmp;
714 ctxt->defMax *= 2;
715 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
716 ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
717 if (tmp == NULL) {
718 if ((ctxt != NULL) && (ctxt->error != NULL))
719 ctxt->error(ctxt->userData, "Out of memory\n");
720 ctxt->nbErrors++;
721 return (NULL);
722 }
723 ctxt->defTab = tmp;
724 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000725 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
726 if (ret == NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000727 if ((ctxt != NULL) && (ctxt->error != NULL))
728 ctxt->error(ctxt->userData, "Out of memory\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +0000729 ctxt->nbErrors++;
Daniel Veillard419a7682003-02-03 23:22:49 +0000730 return(NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000731 }
732 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000733 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000734 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000735 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000736 return (ret);
737}
738
739/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000740 * xmlRelaxNGFreePartition:
741 * @partitions: a partition set structure
742 *
743 * Deallocate RelaxNG partition set structures.
744 */
745static void
746xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) {
747 xmlRelaxNGInterleaveGroupPtr group;
748 int j;
749
750 if (partitions != NULL) {
751 if (partitions->groups != NULL) {
752 for (j = 0;j < partitions->nbgroups;j++) {
753 group = partitions->groups[j];
754 if (group != NULL) {
755 if (group->defs != NULL)
756 xmlFree(group->defs);
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000757 if (group->attrs != NULL)
758 xmlFree(group->attrs);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000759 xmlFree(group);
760 }
761 }
762 xmlFree(partitions->groups);
763 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000764 if (partitions->triage != NULL) {
765 xmlHashFree(partitions->triage, NULL);
766 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000767 xmlFree(partitions);
768 }
769}
770/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000771 * xmlRelaxNGFreeDefine:
772 * @define: a define structure
773 *
774 * Deallocate a RelaxNG define structure.
775 */
776static void
777xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
778{
779 if (define == NULL)
780 return;
781
Daniel Veillard419a7682003-02-03 23:22:49 +0000782 if ((define->data != NULL) &&
783 (define->type == XML_RELAXNG_INTERLEAVE))
784 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
Daniel Veillarde063f482003-03-21 16:53:17 +0000785 if ((define->data != NULL) &&
786 (define->type == XML_RELAXNG_CHOICE))
787 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000788 if (define->name != NULL)
789 xmlFree(define->name);
790 if (define->ns != NULL)
791 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000792 if (define->value != NULL)
793 xmlFree(define->value);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000794 xmlFree(define);
795}
796
797/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000798 * xmlRelaxNGNewStates:
799 * @ctxt: a Relax-NG validation context
800 * @size: the default size for the container
801 *
802 * Allocate a new RelaxNG validation state container
803 * TODO: keep a pool in the ctxt
804 *
805 * Returns the newly allocated structure or NULL in case or error
806 */
807static xmlRelaxNGStatesPtr
808xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
809{
810 xmlRelaxNGStatesPtr ret;
811
Daniel Veillard798024a2003-03-19 10:36:09 +0000812 if ((ctxt != NULL) &&
813 (ctxt->freeState != NULL) &&
814 (ctxt->freeStatesNr > 0)) {
815 ctxt->freeStatesNr--;
816 ret = ctxt->freeStates[ctxt->freeStatesNr];
817 ret->nbState = 0;
818 return(ret);
819 }
Daniel Veillardfd573f12003-03-16 17:52:32 +0000820 if (size < 16) size = 16;
821
822 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
823 (size - 1) * sizeof(xmlRelaxNGValidStatePtr));
824 if (ret == NULL) {
825 if ((ctxt != NULL) && (ctxt->error != NULL))
826 ctxt->error(ctxt->userData, "Out of memory\n");
827 return (NULL);
828 }
829 ret->nbState = 0;
830 ret->maxState = size;
831 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc(
832 (size) * sizeof(xmlRelaxNGValidStatePtr));
833 if (ret->tabState == NULL) {
834 if ((ctxt != NULL) && (ctxt->error != NULL))
835 ctxt->error(ctxt->userData, "Out of memory\n");
836 xmlFree(ret->tabState);
837 return (NULL);
838 }
839 return(ret);
840}
841
842/**
Daniel Veillard798024a2003-03-19 10:36:09 +0000843 * xmlRelaxNGAddStateUniq:
844 * @ctxt: a Relax-NG validation context
845 * @states: the states container
846 * @state: the validation state
847 *
848 * Add a RelaxNG validation state to the container without checking
849 * for unicity.
850 *
851 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
852 */
853static int
854xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
855 xmlRelaxNGStatesPtr states,
856 xmlRelaxNGValidStatePtr state)
857{
858 if (state == NULL) {
859 return(-1);
860 }
861 if (states->nbState >= states->maxState) {
862 xmlRelaxNGValidStatePtr *tmp;
863 int size;
864
865 size = states->maxState * 2;
866 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
867 (size) * sizeof(xmlRelaxNGValidStatePtr));
868 if (tmp == NULL) {
869 if ((ctxt != NULL) && (ctxt->error != NULL))
870 ctxt->error(ctxt->userData, "Out of memory\n");
871 return(-1);
872 }
873 states->tabState = tmp;
874 states->maxState = size;
875 }
876 states->tabState[states->nbState++] = state;
877 return(1);
878}
879
880/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000881 * xmlRelaxNGAddState:
882 * @ctxt: a Relax-NG validation context
883 * @states: the states container
884 * @state: the validation state
885 *
886 * Add a RelaxNG validation state to the container
887 *
888 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
889 */
890static int
891xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states,
892 xmlRelaxNGValidStatePtr state)
893{
894 int i;
895
896 if (state == NULL) {
897 return(-1);
898 }
899 if (states->nbState >= states->maxState) {
900 xmlRelaxNGValidStatePtr *tmp;
901 int size;
902
903 size = states->maxState * 2;
904 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
905 (size) * sizeof(xmlRelaxNGValidStatePtr));
906 if (tmp == NULL) {
907 if ((ctxt != NULL) && (ctxt->error != NULL))
908 ctxt->error(ctxt->userData, "Out of memory\n");
909 return(-1);
910 }
911 states->tabState = tmp;
912 states->maxState = size;
913 }
914 for (i = 0;i < states->nbState;i++) {
915 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
Daniel Veillard798024a2003-03-19 10:36:09 +0000916 xmlRelaxNGFreeValidState(ctxt, state);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000917 return(0);
918 }
919 }
920 states->tabState[states->nbState++] = state;
921 return(1);
922}
923
924/**
925 * xmlRelaxNGFreeStates:
926 * @ctxt: a Relax-NG validation context
927 * @states: teh container
928 *
929 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000930 */
931static void
Daniel Veillard798024a2003-03-19 10:36:09 +0000932xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardfd573f12003-03-16 17:52:32 +0000933 xmlRelaxNGStatesPtr states)
934{
Daniel Veillard798024a2003-03-19 10:36:09 +0000935 if (states == NULL)
936 return;
Daniel Veillard798024a2003-03-19 10:36:09 +0000937 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
938 ctxt->freeStatesMax = 40;
939 ctxt->freeStatesNr = 0;
940 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
941 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
942 if (ctxt->freeStates == NULL) {
943 if ((ctxt != NULL) && (ctxt->error != NULL))
944 ctxt->error(ctxt->userData, "Out of memory\n");
945 }
946 } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
947 xmlRelaxNGStatesPtr *tmp;
948
949 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
950 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
951 if (tmp == NULL) {
952 if ((ctxt != NULL) && (ctxt->error != NULL))
953 ctxt->error(ctxt->userData, "Out of memory\n");
954 xmlFree(states->tabState);
955 xmlFree(states);
956 return;
957 }
958 ctxt->freeStates = tmp;
959 ctxt->freeStatesMax *= 2;
960 }
961 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +0000962 xmlFree(states->tabState);
963 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +0000964 } else {
965 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +0000966 }
967}
968
969/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000970 * xmlRelaxNGNewValidState:
971 * @ctxt: a Relax-NG validation context
972 * @node: the current node or NULL for the document
973 *
974 * Allocate a new RelaxNG validation state
975 *
976 * Returns the newly allocated structure or NULL in case or error
977 */
978static xmlRelaxNGValidStatePtr
979xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
980{
981 xmlRelaxNGValidStatePtr ret;
982 xmlAttrPtr attr;
983 xmlAttrPtr attrs[MAX_ATTR];
984 int nbAttrs = 0;
985 xmlNodePtr root = NULL;
986
987 if (node == NULL) {
988 root = xmlDocGetRootElement(ctxt->doc);
989 if (root == NULL)
990 return(NULL);
991 } else {
992 attr = node->properties;
993 while (attr != NULL) {
994 if (nbAttrs < MAX_ATTR)
995 attrs[nbAttrs++] = attr;
996 else
997 nbAttrs++;
998 attr = attr->next;
999 }
1000 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001001 if ((ctxt->freeState != NULL) &&
1002 (ctxt->freeState->nbState > 0)) {
1003 ctxt->freeState->nbState--;
1004 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1005 } else {
1006 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1007 if (ret == NULL) {
1008 if ((ctxt != NULL) && (ctxt->error != NULL))
1009 ctxt->error(ctxt->userData, "Out of memory\n");
1010 return (NULL);
1011 }
1012 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001013 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001014 ret->value = NULL;
1015 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001016 if (node == NULL) {
1017 ret->node = (xmlNodePtr) ctxt->doc;
1018 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001019 } else {
1020 ret->node = node;
1021 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001022 }
1023 ret->nbAttrs = 0;
1024 if (nbAttrs > 0) {
1025 if (ret->attrs == NULL) {
1026 if (nbAttrs < 4) ret->maxAttrs = 4;
1027 else ret->maxAttrs = nbAttrs;
1028 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1029 sizeof(xmlAttrPtr));
1030 if (ret->attrs == NULL) {
1031 if ((ctxt != NULL) && (ctxt->error != NULL))
1032 ctxt->error(ctxt->userData, "Out of memory\n");
1033 return (ret);
1034 }
1035 } else if (ret->maxAttrs < nbAttrs) {
1036 xmlAttrPtr *tmp;
1037
1038 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1039 sizeof(xmlAttrPtr));
1040 if (tmp == NULL) {
1041 if ((ctxt != NULL) && (ctxt->error != NULL))
1042 ctxt->error(ctxt->userData, "Out of memory\n");
1043 return (ret);
1044 }
1045 ret->attrs = tmp;
1046 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001047 ret->nbAttrs = nbAttrs;
Daniel Veillard798024a2003-03-19 10:36:09 +00001048 if (nbAttrs < MAX_ATTR) {
1049 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1050 } else {
1051 attr = node->properties;
1052 nbAttrs = 0;
1053 while (attr != NULL) {
1054 ret->attrs[nbAttrs++] = attr;
1055 attr = attr->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001056 }
1057 }
1058 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001059 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001060 return (ret);
1061}
1062
1063/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001064 * xmlRelaxNGCopyValidState:
1065 * @ctxt: a Relax-NG validation context
1066 * @state: a validation state
1067 *
1068 * Copy the validation state
1069 *
1070 * Returns the newly allocated structure or NULL in case or error
1071 */
1072static xmlRelaxNGValidStatePtr
1073xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1074 xmlRelaxNGValidStatePtr state)
1075{
1076 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001077 unsigned int maxAttrs;
1078 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001079
1080 if (state == NULL)
1081 return(NULL);
Daniel Veillard798024a2003-03-19 10:36:09 +00001082 if ((ctxt->freeState != NULL) &&
1083 (ctxt->freeState->nbState > 0)) {
1084 ctxt->freeState->nbState--;
1085 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1086 } else {
1087 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1088 if (ret == NULL) {
1089 if ((ctxt != NULL) && (ctxt->error != NULL))
1090 ctxt->error(ctxt->userData, "Out of memory\n");
1091 return (NULL);
1092 }
1093 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001094 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001095 attrs = ret->attrs;
1096 maxAttrs = ret->maxAttrs;
1097 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1098 ret->attrs = attrs;
1099 ret->maxAttrs = maxAttrs;
1100 if (state->nbAttrs > 0) {
1101 if (ret->attrs == NULL) {
1102 ret->maxAttrs = state->maxAttrs;
1103 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1104 sizeof(xmlAttrPtr));
1105 if (ret->attrs == NULL) {
1106 if ((ctxt != NULL) && (ctxt->error != NULL))
1107 ctxt->error(ctxt->userData, "Out of memory\n");
1108 ret->nbAttrs = 0;
1109 return (ret);
1110 }
1111 } else if (ret->maxAttrs < state->nbAttrs) {
1112 xmlAttrPtr *tmp;
1113
1114 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1115 sizeof(xmlAttrPtr));
1116 if (tmp == NULL) {
1117 if ((ctxt != NULL) && (ctxt->error != NULL))
1118 ctxt->error(ctxt->userData, "Out of memory\n");
1119 ret->nbAttrs = 0;
1120 return (ret);
1121 }
1122 ret->maxAttrs = state->maxAttrs;
1123 }
1124 memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr));
1125 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001126 return(ret);
1127}
1128
1129/**
1130 * xmlRelaxNGEqualValidState:
1131 * @ctxt: a Relax-NG validation context
1132 * @state1: a validation state
1133 * @state2: a validation state
1134 *
1135 * Compare the validation states for equality
1136 *
1137 * Returns 1 if equald, 0 otherwise
1138 */
1139static int
1140xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1141 xmlRelaxNGValidStatePtr state1,
1142 xmlRelaxNGValidStatePtr state2)
1143{
1144 int i;
1145
1146 if ((state1 == NULL) || (state2 == NULL))
1147 return(0);
1148 if (state1 == state2)
1149 return(1);
1150 if (state1->node != state2->node)
1151 return(0);
1152 if (state1->seq != state2->seq)
1153 return(0);
1154 if (state1->nbAttrLeft != state2->nbAttrLeft)
1155 return(0);
1156 if (state1->nbAttrs != state2->nbAttrs)
1157 return(0);
1158 if (state1->endvalue != state2->endvalue)
1159 return(0);
1160 if ((state1->value != state2->value) &&
1161 (!xmlStrEqual(state1->value, state2->value)))
1162 return(0);
1163 for (i = 0;i < state1->nbAttrs;i++) {
1164 if (state1->attrs[i] != state2->attrs[i])
1165 return(0);
1166 }
1167 return(1);
1168}
1169
1170/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001171 * xmlRelaxNGFreeValidState:
1172 * @state: a validation state structure
1173 *
1174 * Deallocate a RelaxNG validation state structure.
1175 */
1176static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001177xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1178 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001179{
1180 if (state == NULL)
1181 return;
1182
Daniel Veillard798024a2003-03-19 10:36:09 +00001183 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1184 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1185 }
1186 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1187 if (state->attrs != NULL)
1188 xmlFree(state->attrs);
1189 xmlFree(state);
1190 } else {
1191 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1192 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001193}
1194
1195/************************************************************************
1196 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001197 * Document functions *
1198 * *
1199 ************************************************************************/
1200static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1201 xmlDocPtr doc);
1202
1203/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001204 * xmlRelaxNGIncludePush:
1205 * @ctxt: the parser context
1206 * @value: the element doc
1207 *
1208 * Pushes a new include on top of the include stack
1209 *
1210 * Returns 0 in case of error, the index in the stack otherwise
1211 */
1212static int
1213xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1214 xmlRelaxNGIncludePtr value)
1215{
1216 if (ctxt->incTab == NULL) {
1217 ctxt->incMax = 4;
1218 ctxt->incNr = 0;
1219 ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc(
1220 ctxt->incMax * sizeof(ctxt->incTab[0]));
1221 if (ctxt->incTab == NULL) {
1222 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1223 return (0);
1224 }
1225 }
1226 if (ctxt->incNr >= ctxt->incMax) {
1227 ctxt->incMax *= 2;
1228 ctxt->incTab =
1229 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1230 ctxt->incMax *
1231 sizeof(ctxt->incTab[0]));
1232 if (ctxt->incTab == NULL) {
1233 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1234 return (0);
1235 }
1236 }
1237 ctxt->incTab[ctxt->incNr] = value;
1238 ctxt->inc = value;
1239 return (ctxt->incNr++);
1240}
1241
1242/**
1243 * xmlRelaxNGIncludePop:
1244 * @ctxt: the parser context
1245 *
1246 * Pops the top include from the include stack
1247 *
1248 * Returns the include just removed
1249 */
1250static xmlRelaxNGIncludePtr
1251xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1252{
1253 xmlRelaxNGIncludePtr ret;
1254
1255 if (ctxt->incNr <= 0)
1256 return (0);
1257 ctxt->incNr--;
1258 if (ctxt->incNr > 0)
1259 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1260 else
1261 ctxt->inc = NULL;
1262 ret = ctxt->incTab[ctxt->incNr];
1263 ctxt->incTab[ctxt->incNr] = 0;
1264 return (ret);
1265}
1266
1267/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001268 * xmlRelaxNGRemoveRedefine:
1269 * @ctxt: the parser context
1270 * @URL: the normalized URL
1271 * @target: the included target
1272 * @name: the define name to eliminate
1273 *
1274 * Applies the elimination algorithm of 4.7
1275 *
1276 * Returns 0 in case of error, 1 in case of success.
1277 */
1278static int
1279xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1280 const xmlChar *URL ATTRIBUTE_UNUSED,
1281 xmlNodePtr target, const xmlChar *name) {
1282 int found = 0;
1283 xmlNodePtr tmp, tmp2;
1284 xmlChar *name2;
1285
1286#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001287 if (name == NULL)
1288 xmlGenericError(xmlGenericErrorContext,
1289 "Elimination of <include> start from %s\n", URL);
1290 else
1291 xmlGenericError(xmlGenericErrorContext,
1292 "Elimination of <include> define %s from %s\n", name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001293#endif
1294 tmp = target;
1295 while (tmp != NULL) {
1296 tmp2 = tmp->next;
1297 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1298 found = 1;
1299 xmlUnlinkNode(tmp);
1300 xmlFreeNode(tmp);
1301 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1302 name2 = xmlGetProp(tmp, BAD_CAST "name");
1303 xmlRelaxNGNormExtSpace(name2);
1304 if (name2 != NULL) {
1305 if (xmlStrEqual(name, name2)) {
1306 found = 1;
1307 xmlUnlinkNode(tmp);
1308 xmlFreeNode(tmp);
1309 }
1310 xmlFree(name2);
1311 }
1312 } else if (IS_RELAXNG(tmp, "include")) {
1313 xmlChar *href = NULL;
1314 xmlRelaxNGDocumentPtr inc = tmp->_private;
1315
1316 if ((inc != NULL) && (inc->doc != NULL) &&
1317 (inc->doc->children != NULL)) {
1318
1319 if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) {
1320#ifdef DEBUG_INCLUDE
1321 href = xmlGetProp(tmp, BAD_CAST "href");
1322#endif
1323 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1324 inc->doc->children->children, name) == 1) {
1325 found = 1;
1326 }
1327 if (href != NULL)
1328 xmlFree(href);
1329 }
1330 }
1331 }
1332 tmp = tmp2;
1333 }
1334 return(found);
1335}
1336
1337/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001338 * xmlRelaxNGLoadInclude:
1339 * @ctxt: the parser context
1340 * @URL: the normalized URL
1341 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001342 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001343 *
1344 * First lookup if the document is already loaded into the parser context,
1345 * check against recursion. If not found the resource is loaded and
1346 * the content is preprocessed before being returned back to the caller.
1347 *
1348 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1349 */
1350static xmlRelaxNGIncludePtr
1351xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillard416589a2003-02-17 17:25:42 +00001352 xmlNodePtr node, const xmlChar *ns) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001353 xmlRelaxNGIncludePtr ret = NULL;
1354 xmlDocPtr doc;
1355 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001356 xmlNodePtr root, cur;
1357
1358#ifdef DEBUG_INCLUDE
1359 xmlGenericError(xmlGenericErrorContext,
1360 "xmlRelaxNGLoadInclude(%s)\n", URL);
1361#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001362
1363 /*
1364 * check against recursion in the stack
1365 */
1366 for (i = 0;i < ctxt->incNr;i++) {
1367 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1368 if (ctxt->error != NULL)
1369 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00001370 "Detected an Include recursion for %s\n",
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001371 URL);
1372 ctxt->nbErrors++;
1373 return(NULL);
1374 }
1375 }
1376
1377 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001378 * load the document
1379 */
1380 doc = xmlParseFile((const char *) URL);
1381 if (doc == NULL) {
1382 if (ctxt->error != NULL)
1383 ctxt->error(ctxt->userData,
1384 "xmlRelaxNG: could not load %s\n", URL);
1385 ctxt->nbErrors++;
1386 return (NULL);
1387 }
1388
Daniel Veillard5add8682003-03-10 13:13:58 +00001389#ifdef DEBUG_INCLUDE
1390 xmlGenericError(xmlGenericErrorContext,
1391 "Parsed %s Okay\n", URL);
1392#endif
1393
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001394 /*
1395 * Allocate the document structures and register it first.
1396 */
1397 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1398 if (ret == NULL) {
1399 if (ctxt->error != NULL)
1400 ctxt->error(ctxt->userData,
1401 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1402 ctxt->nbErrors++;
1403 xmlFreeDoc(doc);
1404 return (NULL);
1405 }
1406 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1407 ret->doc = doc;
1408 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001409 ret->next = ctxt->includes;
1410 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001411
1412 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001413 * transmit the ns if needed
1414 */
1415 if (ns != NULL) {
1416 root = xmlDocGetRootElement(doc);
1417 if (root != NULL) {
1418 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1419 xmlSetProp(root, BAD_CAST"ns", ns);
1420 }
1421 }
1422 }
1423
1424 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001425 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001426 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001427 xmlRelaxNGIncludePush(ctxt, ret);
1428
1429 /*
1430 * Some preprocessing of the document content, this include recursing
1431 * in the include stack.
1432 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001433#ifdef DEBUG_INCLUDE
1434 xmlGenericError(xmlGenericErrorContext,
1435 "cleanup of %s\n", URL);
1436#endif
1437
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001438 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1439 if (doc == NULL) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001440 ctxt->inc = NULL;
1441 return(NULL);
1442 }
1443
1444 /*
1445 * Pop up the include from the stack
1446 */
1447 xmlRelaxNGIncludePop(ctxt);
1448
Daniel Veillard5add8682003-03-10 13:13:58 +00001449#ifdef DEBUG_INCLUDE
1450 xmlGenericError(xmlGenericErrorContext,
1451 "Checking of %s\n", URL);
1452#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001453 /*
1454 * Check that the top element is a grammar
1455 */
1456 root = xmlDocGetRootElement(doc);
1457 if (root == NULL) {
1458 if (ctxt->error != NULL)
1459 ctxt->error(ctxt->userData,
1460 "xmlRelaxNG: included document is empty %s\n", URL);
1461 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001462 return (NULL);
1463 }
1464 if (!IS_RELAXNG(root, "grammar")) {
1465 if (ctxt->error != NULL)
1466 ctxt->error(ctxt->userData,
1467 "xmlRelaxNG: included document %s root is not a grammar\n",
1468 URL);
1469 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001470 return (NULL);
1471 }
1472
1473 /*
1474 * Elimination of redefined rules in the include.
1475 */
1476 cur = node->children;
1477 while (cur != NULL) {
1478 if (IS_RELAXNG(cur, "start")) {
1479 int found = 0;
1480
Daniel Veillard5add8682003-03-10 13:13:58 +00001481 found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001482 if (!found) {
1483 if (ctxt->error != NULL)
1484 ctxt->error(ctxt->userData,
1485 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1486 URL);
1487 ctxt->nbErrors++;
1488 }
1489 } else if (IS_RELAXNG(cur, "define")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001490 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001491
1492 name = xmlGetProp(cur, BAD_CAST "name");
1493 if (name == NULL) {
1494 if (ctxt->error != NULL)
1495 ctxt->error(ctxt->userData,
1496 "xmlRelaxNG: include %s has define without name\n",
1497 URL);
1498 ctxt->nbErrors++;
1499 } else {
Daniel Veillard5add8682003-03-10 13:13:58 +00001500 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001501
Daniel Veillardd2298792003-02-14 16:54:11 +00001502 xmlRelaxNGNormExtSpace(name);
Daniel Veillard5add8682003-03-10 13:13:58 +00001503 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1504 root->children, name);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001505 if (!found) {
1506 if (ctxt->error != NULL)
1507 ctxt->error(ctxt->userData,
1508 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1509 URL, name);
1510 ctxt->nbErrors++;
1511 }
1512 xmlFree(name);
1513 }
1514 }
1515 cur = cur->next;
1516 }
1517
1518
1519 return(ret);
1520}
1521
1522/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001523 * xmlRelaxNGValidErrorPush:
1524 * @ctxt: the validation context
1525 * @err: the error code
1526 * @arg1: the first string argument
1527 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001528 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001529 *
1530 * Pushes a new error on top of the error stack
1531 *
1532 * Returns 0 in case of error, the index in the stack otherwise
1533 */
1534static int
1535xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001536 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001537{
1538 xmlRelaxNGValidErrorPtr cur;
1539 if (ctxt->errTab == NULL) {
1540 ctxt->errMax = 8;
1541 ctxt->errNr = 0;
1542 ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc(
1543 ctxt->errMax * sizeof(xmlRelaxNGValidError));
1544 if (ctxt->errTab == NULL) {
1545 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1546 return (0);
1547 }
Daniel Veillard20863822003-03-22 17:51:47 +00001548 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001549 }
1550 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard20863822003-03-22 17:51:47 +00001551 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001552 ctxt->errTab =
1553 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard20863822003-03-22 17:51:47 +00001554 ctxt->errMax * sizeof(xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001555 if (ctxt->errTab == NULL) {
1556 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1557 return (0);
1558 }
Daniel Veillard20863822003-03-22 17:51:47 +00001559 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001560 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001561 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00001562 (ctxt->err->node == ctxt->state->node) &&
1563 (ctxt->err->err == err))
1564 return(ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001565 cur = &ctxt->errTab[ctxt->errNr];
1566 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001567 if (dup) {
1568 cur->arg1 = xmlStrdup(arg1);
1569 cur->arg2 = xmlStrdup(arg2);
1570 cur->flags = ERROR_IS_DUP;
1571 } else {
1572 cur->arg1 = arg1;
1573 cur->arg2 = arg2;
1574 cur->flags = 0;
1575 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001576 if (ctxt->state != NULL) {
1577 cur->node = ctxt->state->node;
1578 cur->seq = ctxt->state->seq;
1579 } else {
1580 cur->node = NULL;
1581 cur->seq = NULL;
1582 }
1583 ctxt->err = cur;
1584 return (ctxt->errNr++);
1585}
1586
1587/**
1588 * xmlRelaxNGValidErrorPop:
1589 * @ctxt: the validation context
1590 *
1591 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001592 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001593static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001594xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1595{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001596 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001597
Daniel Veillard580ced82003-03-21 21:22:48 +00001598 if (ctxt->errNr <= 0) {
1599 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001600 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001601 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001602 ctxt->errNr--;
1603 if (ctxt->errNr > 0)
1604 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1605 else
1606 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001607 cur = &ctxt->errTab[ctxt->errNr];
1608 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001609 if (cur->arg1 != NULL)
1610 xmlFree((xmlChar *)cur->arg1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001611 cur->arg1 = NULL;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001612 if (cur->arg2 != NULL)
1613 xmlFree((xmlChar *)cur->arg2);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001614 cur->arg2 = NULL;
1615 cur->flags = 0;
1616 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001617}
1618
Daniel Veillard42f12e92003-03-07 18:32:59 +00001619/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001620 * xmlRelaxNGDocumentPush:
1621 * @ctxt: the parser context
1622 * @value: the element doc
1623 *
1624 * Pushes a new doc on top of the doc stack
1625 *
1626 * Returns 0 in case of error, the index in the stack otherwise
1627 */
1628static int
1629xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1630 xmlRelaxNGDocumentPtr value)
1631{
1632 if (ctxt->docTab == NULL) {
1633 ctxt->docMax = 4;
1634 ctxt->docNr = 0;
1635 ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc(
1636 ctxt->docMax * sizeof(ctxt->docTab[0]));
1637 if (ctxt->docTab == NULL) {
1638 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1639 return (0);
1640 }
1641 }
1642 if (ctxt->docNr >= ctxt->docMax) {
1643 ctxt->docMax *= 2;
1644 ctxt->docTab =
1645 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1646 ctxt->docMax *
1647 sizeof(ctxt->docTab[0]));
1648 if (ctxt->docTab == NULL) {
1649 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1650 return (0);
1651 }
1652 }
1653 ctxt->docTab[ctxt->docNr] = value;
1654 ctxt->doc = value;
1655 return (ctxt->docNr++);
1656}
1657
1658/**
1659 * xmlRelaxNGDocumentPop:
1660 * @ctxt: the parser context
1661 *
1662 * Pops the top doc from the doc stack
1663 *
1664 * Returns the doc just removed
1665 */
1666static xmlRelaxNGDocumentPtr
1667xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1668{
1669 xmlRelaxNGDocumentPtr ret;
1670
1671 if (ctxt->docNr <= 0)
1672 return (0);
1673 ctxt->docNr--;
1674 if (ctxt->docNr > 0)
1675 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1676 else
1677 ctxt->doc = NULL;
1678 ret = ctxt->docTab[ctxt->docNr];
1679 ctxt->docTab[ctxt->docNr] = 0;
1680 return (ret);
1681}
1682
1683/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001684 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001685 * @ctxt: the parser context
1686 * @URL: the normalized URL
1687 * @ns: the inherited ns if any
1688 *
1689 * First lookup if the document is already loaded into the parser context,
1690 * check against recursion. If not found the resource is loaded and
1691 * the content is preprocessed before being returned back to the caller.
1692 *
1693 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1694 */
1695static xmlRelaxNGDocumentPtr
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001696xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001697 const xmlChar *ns) {
1698 xmlRelaxNGDocumentPtr ret = NULL;
1699 xmlDocPtr doc;
1700 xmlNodePtr root;
1701 int i;
1702
1703 /*
1704 * check against recursion in the stack
1705 */
1706 for (i = 0;i < ctxt->docNr;i++) {
1707 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1708 if (ctxt->error != NULL)
1709 ctxt->error(ctxt->userData,
1710 "Detected an externalRef recursion for %s\n",
1711 URL);
1712 ctxt->nbErrors++;
1713 return(NULL);
1714 }
1715 }
1716
1717 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001718 * load the document
1719 */
1720 doc = xmlParseFile((const char *) URL);
1721 if (doc == NULL) {
1722 if (ctxt->error != NULL)
1723 ctxt->error(ctxt->userData,
1724 "xmlRelaxNG: could not load %s\n", URL);
1725 ctxt->nbErrors++;
1726 return (NULL);
1727 }
1728
1729 /*
1730 * Allocate the document structures and register it first.
1731 */
1732 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1733 if (ret == NULL) {
1734 if (ctxt->error != NULL)
1735 ctxt->error(ctxt->userData,
1736 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1737 ctxt->nbErrors++;
1738 xmlFreeDoc(doc);
1739 return (NULL);
1740 }
1741 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1742 ret->doc = doc;
1743 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001744 ret->next = ctxt->documents;
1745 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001746
1747 /*
1748 * transmit the ns if needed
1749 */
1750 if (ns != NULL) {
1751 root = xmlDocGetRootElement(doc);
1752 if (root != NULL) {
1753 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1754 xmlSetProp(root, BAD_CAST"ns", ns);
1755 }
1756 }
1757 }
1758
1759 /*
1760 * push it on the stack and register it in the hash table
1761 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001762 xmlRelaxNGDocumentPush(ctxt, ret);
1763
1764 /*
1765 * Some preprocessing of the document content
1766 */
1767 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1768 if (doc == NULL) {
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001769 ctxt->doc = NULL;
1770 return(NULL);
1771 }
1772
1773 xmlRelaxNGDocumentPop(ctxt);
1774
1775 return(ret);
1776}
1777
1778/************************************************************************
1779 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001780 * Error functions *
1781 * *
1782 ************************************************************************/
1783
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001784#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1785#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
1786#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
1787#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
1788#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001789
Daniel Veillardfd573f12003-03-16 17:52:32 +00001790#ifdef DEBUG
Daniel Veillard231d7912003-02-09 14:22:17 +00001791static const char *
1792xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) {
1793 if (def == NULL)
1794 return("none");
1795 switch(def->type) {
1796 case XML_RELAXNG_EMPTY: return("empty");
1797 case XML_RELAXNG_NOT_ALLOWED: return("notAllowed");
1798 case XML_RELAXNG_EXCEPT: return("except");
1799 case XML_RELAXNG_TEXT: return("text");
1800 case XML_RELAXNG_ELEMENT: return("element");
1801 case XML_RELAXNG_DATATYPE: return("datatype");
1802 case XML_RELAXNG_VALUE: return("value");
1803 case XML_RELAXNG_LIST: return("list");
1804 case XML_RELAXNG_ATTRIBUTE: return("attribute");
1805 case XML_RELAXNG_DEF: return("def");
1806 case XML_RELAXNG_REF: return("ref");
1807 case XML_RELAXNG_EXTERNALREF: return("externalRef");
1808 case XML_RELAXNG_PARENTREF: return("parentRef");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001809 case XML_RELAXNG_OPTIONAL: return("optional");
1810 case XML_RELAXNG_ZEROORMORE: return("zeroOrMore");
Daniel Veillard231d7912003-02-09 14:22:17 +00001811 case XML_RELAXNG_ONEORMORE: return("oneOrMore");
1812 case XML_RELAXNG_CHOICE: return("choice");
1813 case XML_RELAXNG_GROUP: return("group");
1814 case XML_RELAXNG_INTERLEAVE: return("interleave");
1815 case XML_RELAXNG_START: return("start");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001816 case XML_RELAXNG_NOOP: return("noop");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001817 case XML_RELAXNG_PARAM: return("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00001818 }
1819 return("unknown");
1820}
Daniel Veillardfd573f12003-03-16 17:52:32 +00001821#endif
Daniel Veillardd2298792003-02-14 16:54:11 +00001822
Daniel Veillard6eadf632003-01-23 18:29:16 +00001823/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001824 * xmlRelaxNGGetErrorString:
1825 * @err: the error code
1826 * @arg1: the first string argument
1827 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00001828 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00001829 * computes a formatted error string for the given error code and args
1830 *
1831 * Returns the error string, it must be deallocated by the caller
1832 */
1833static xmlChar *
Daniel Veillardfd573f12003-03-16 17:52:32 +00001834xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1,
1835 const xmlChar *arg2) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00001836 char msg[1000];
1837
1838 if (arg1 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001839 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001840 if (arg2 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001841 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001842
1843 msg[0] = 0;
1844 switch (err) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001845 case XML_RELAXNG_OK:
1846 return(NULL);
1847 case XML_RELAXNG_ERR_MEMORY:
1848 return(xmlCharStrdup("out of memory"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001849 case XML_RELAXNG_ERR_TYPE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00001850 snprintf(msg, 1000, "failed to validate type %s", arg1);
1851 break;
1852 case XML_RELAXNG_ERR_TYPEVAL:
1853 snprintf(msg, 1000, "Type %s doesn't allow value %s", arg1, arg2);
1854 break;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001855 case XML_RELAXNG_ERR_DUPID:
1856 snprintf(msg, 1000, "ID %s redefined", arg1);
1857 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001858 case XML_RELAXNG_ERR_TYPECMP:
1859 snprintf(msg, 1000, "failed to compare type %s", arg1);
1860 break;
1861 case XML_RELAXNG_ERR_NOSTATE:
1862 return(xmlCharStrdup("Internal error: no state"));
1863 case XML_RELAXNG_ERR_NODEFINE:
1864 return(xmlCharStrdup("Internal error: no define"));
Daniel Veillard952379b2003-03-17 15:37:12 +00001865 case XML_RELAXNG_ERR_INTERNAL:
1866 snprintf(msg, 1000, "Internal error: %s", arg1);
1867 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001868 case XML_RELAXNG_ERR_LISTEXTRA:
1869 snprintf(msg, 1000, "Extra data in list: %s", arg1);
1870 break;
1871 case XML_RELAXNG_ERR_INTERNODATA:
1872 return(xmlCharStrdup("Internal: interleave block has no data"));
1873 case XML_RELAXNG_ERR_INTERSEQ:
1874 return(xmlCharStrdup("Invalid sequence in interleave"));
1875 case XML_RELAXNG_ERR_INTEREXTRA:
1876 snprintf(msg, 1000, "Extra element %s in interleave", arg1);
1877 break;
1878 case XML_RELAXNG_ERR_ELEMNAME:
1879 snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2);
1880 break;
1881 case XML_RELAXNG_ERR_ELEMNONS:
1882 snprintf(msg, 1000, "Expecting a namespace for element %s", arg1);
1883 break;
1884 case XML_RELAXNG_ERR_ELEMWRONGNS:
1885 snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s",
1886 arg1, arg2);
1887 break;
1888 case XML_RELAXNG_ERR_ELEMEXTRANS:
1889 snprintf(msg, 1000, "Expecting no namespace for element %s", arg1);
1890 break;
1891 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
1892 snprintf(msg, 1000, "Expecting element %s to be empty", arg1);
1893 break;
1894 case XML_RELAXNG_ERR_NOELEM:
1895 snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1);
1896 break;
1897 case XML_RELAXNG_ERR_NOTELEM:
1898 return(xmlCharStrdup("Expecting an element got text"));
1899 case XML_RELAXNG_ERR_ATTRVALID:
1900 snprintf(msg, 1000, "Element %s failed to validate attributes",
1901 arg1);
1902 break;
1903 case XML_RELAXNG_ERR_CONTENTVALID:
1904 snprintf(msg, 1000, "Element %s failed to validate content",
1905 arg1);
1906 break;
1907 case XML_RELAXNG_ERR_EXTRACONTENT:
1908 snprintf(msg, 1000, "Element %s has extra content: %s",
1909 arg1, arg2);
1910 break;
1911 case XML_RELAXNG_ERR_INVALIDATTR:
1912 snprintf(msg, 1000, "Invalid attribute %s for element %s",
1913 arg1, arg2);
1914 break;
1915 case XML_RELAXNG_ERR_DATAELEM:
1916 snprintf(msg, 1000, "Datatype element %s has child elements",
1917 arg1);
1918 break;
1919 case XML_RELAXNG_ERR_VALELEM:
1920 snprintf(msg, 1000, "Value element %s has child elements",
1921 arg1);
1922 break;
1923 case XML_RELAXNG_ERR_LISTELEM:
1924 snprintf(msg, 1000, "List element %s has child elements",
1925 arg1);
1926 break;
1927 case XML_RELAXNG_ERR_DATATYPE:
1928 snprintf(msg, 1000, "Error validating datatype %s",
1929 arg1);
1930 break;
1931 case XML_RELAXNG_ERR_VALUE:
1932 snprintf(msg, 1000, "Error validating value %s",
1933 arg1);
1934 break;
1935 case XML_RELAXNG_ERR_LIST:
1936 return(xmlCharStrdup("Error validating list"));
1937 case XML_RELAXNG_ERR_NOGRAMMAR:
1938 return(xmlCharStrdup("No top grammar defined"));
1939 case XML_RELAXNG_ERR_EXTRADATA:
1940 return(xmlCharStrdup("Extra data in the document"));
1941 default:
1942 TODO
Daniel Veillard42f12e92003-03-07 18:32:59 +00001943 }
1944 if (msg[0] == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001945 snprintf(msg, 1000, "Unknown error code %d", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001946 }
1947 msg[1000] = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001948 return(xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001949}
1950
1951/**
1952 * xmlRelaxNGValidErrorContext:
1953 * @ctxt: the validation context
1954 * @node: the node
1955 * @child: the node child generating the problem.
1956 *
1957 * Dump informations about the kocation of the error in the instance
Daniel Veillard6eadf632003-01-23 18:29:16 +00001958 */
1959static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001960xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node,
1961 xmlNodePtr child)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001962{
1963 int line = 0;
1964 const xmlChar *file = NULL;
1965 const xmlChar *name = NULL;
1966 const char *type = "error";
1967
1968 if ((ctxt == NULL) || (ctxt->error == NULL))
1969 return;
1970
1971 if (child != NULL)
1972 node = child;
1973
1974 if (node != NULL) {
1975 if ((node->type == XML_DOCUMENT_NODE) ||
1976 (node->type == XML_HTML_DOCUMENT_NODE)) {
1977 xmlDocPtr doc = (xmlDocPtr) node;
1978
1979 file = doc->URL;
1980 } else {
1981 /*
1982 * Try to find contextual informations to report
1983 */
1984 if (node->type == XML_ELEMENT_NODE) {
1985 line = (int) node->content;
1986 } else if ((node->prev != NULL) &&
1987 (node->prev->type == XML_ELEMENT_NODE)) {
1988 line = (int) node->prev->content;
1989 } else if ((node->parent != NULL) &&
1990 (node->parent->type == XML_ELEMENT_NODE)) {
1991 line = (int) node->parent->content;
1992 }
1993 if ((node->doc != NULL) && (node->doc->URL != NULL))
1994 file = node->doc->URL;
1995 if (node->name != NULL)
1996 name = node->name;
1997 }
1998 }
1999
Daniel Veillard42f12e92003-03-07 18:32:59 +00002000 type = "RNG validity error";
Daniel Veillard6eadf632003-01-23 18:29:16 +00002001
2002 if ((file != NULL) && (line != 0) && (name != NULL))
2003 ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n",
2004 type, file, line, name);
2005 else if ((file != NULL) && (name != NULL))
2006 ctxt->error(ctxt->userData, "%s: file %s element %s\n",
2007 type, file, name);
2008 else if ((file != NULL) && (line != 0))
2009 ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line);
2010 else if (file != NULL)
2011 ctxt->error(ctxt->userData, "%s: file %s\n", type, file);
2012 else if (name != NULL)
2013 ctxt->error(ctxt->userData, "%s: element %s\n", type, name);
2014 else
2015 ctxt->error(ctxt->userData, "%s\n", type);
2016}
Daniel Veillard42f12e92003-03-07 18:32:59 +00002017
2018/**
2019 * xmlRelaxNGShowValidError:
2020 * @ctxt: the validation context
2021 * @err: the error number
2022 * @node: the node
2023 * @child: the node child generating the problem.
2024 * @arg1: the first argument
2025 * @arg2: the second argument
2026 *
2027 * Show a validation error.
2028 */
2029static void
2030xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
2031 xmlNodePtr node, xmlNodePtr child,
2032 const xmlChar *arg1, const xmlChar *arg2)
2033{
2034 xmlChar *msg;
2035
2036 if (ctxt->error == NULL)
2037 return;
2038
2039 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2040 if (msg == NULL)
2041 return;
2042
2043 xmlRelaxNGValidErrorContext(ctxt, node, child);
2044 ctxt->error(ctxt->userData, "%s\n", msg);
2045 xmlFree(msg);
2046}
2047
2048/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002049 * xmlRelaxNGPopErrors:
2050 * @ctxt: the validation context
2051 * @level: the error level in the stack
2052 *
2053 * pop and discard all errors until the given level is reached
2054 */
2055static void
2056xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) {
2057 int i;
2058 xmlRelaxNGValidErrorPtr err;
2059
2060 for (i = level;i < ctxt->errNr;i++) {
2061 err = &ctxt->errTab[i];
2062 if (err->flags & ERROR_IS_DUP) {
2063 if (err->arg1 != NULL)
2064 xmlFree((xmlChar *)err->arg1);
2065 err->arg1 = NULL;
2066 if (err->arg2 != NULL)
2067 xmlFree((xmlChar *)err->arg2);
2068 err->arg2 = NULL;
2069 err->flags = 0;
2070 }
2071 }
2072 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002073 if (ctxt->errNr <= 0)
2074 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002075}
2076/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002077 * xmlRelaxNGDumpValidError:
2078 * @ctxt: the validation context
2079 *
2080 * Show all validation error over a given index.
2081 */
2082static void
2083xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard580ced82003-03-21 21:22:48 +00002084 int i, j;
2085 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002086
2087 for (i = 0;i < ctxt->errNr;i++) {
2088 err = &ctxt->errTab[i];
Daniel Veillard580ced82003-03-21 21:22:48 +00002089 for (j = 0;j < i;j++) {
2090 dup = &ctxt->errTab[j];
2091 if ((err->err == dup->err) && (err->node == dup->node) &&
2092 (xmlStrEqual(err->arg1, dup->arg1)) &&
2093 (xmlStrEqual(err->arg2, dup->arg2))) {
2094 goto skip;
2095 }
2096 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002097 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2098 err->arg1, err->arg2);
Daniel Veillard580ced82003-03-21 21:22:48 +00002099skip:
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002100 if (err->flags & ERROR_IS_DUP) {
2101 if (err->arg1 != NULL)
2102 xmlFree((xmlChar *)err->arg1);
2103 err->arg1 = NULL;
2104 if (err->arg2 != NULL)
2105 xmlFree((xmlChar *)err->arg2);
2106 err->arg2 = NULL;
2107 err->flags = 0;
2108 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002109 }
2110 ctxt->errNr = 0;
2111}
2112/**
2113 * xmlRelaxNGAddValidError:
2114 * @ctxt: the validation context
2115 * @err: the error number
2116 * @arg1: the first argument
2117 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002118 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002119 *
2120 * Register a validation error, either generating it if it's sure
2121 * or stacking it for later handling if unsure.
2122 */
2123static void
2124xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002125 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002126{
2127 if ((ctxt == NULL) || (ctxt->error == NULL))
2128 return;
2129
2130 /*
2131 * generate the error directly
2132 */
2133 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
2134 xmlNodePtr node, seq;
2135 /*
2136 * Flush first any stacked error which might be the
2137 * real cause of the problem.
2138 */
2139 if (ctxt->errNr != 0)
2140 xmlRelaxNGDumpValidError(ctxt);
2141 if (ctxt->state != NULL) {
2142 node = ctxt->state->node;
2143 seq = ctxt->state->seq;
2144 } else {
2145 node = seq = NULL;
2146 }
2147 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2148 }
2149 /*
2150 * Stack the error for later processing if needed
2151 */
2152 else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002153 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002154 }
2155}
2156
Daniel Veillard6eadf632003-01-23 18:29:16 +00002157
2158/************************************************************************
2159 * *
2160 * Type library hooks *
2161 * *
2162 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002163static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2164 const xmlChar *str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002165
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002166/**
2167 * xmlRelaxNGSchemaTypeHave:
2168 * @data: data needed for the library
2169 * @type: the type name
2170 *
2171 * Check if the given type is provided by
2172 * the W3C XMLSchema Datatype library.
2173 *
2174 * Returns 1 if yes, 0 if no and -1 in case of error.
2175 */
2176static int
2177xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002178 const xmlChar *type) {
2179 xmlSchemaTypePtr typ;
2180
2181 if (type == NULL)
2182 return(-1);
2183 typ = xmlSchemaGetPredefinedType(type,
2184 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2185 if (typ == NULL)
2186 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002187 return(1);
2188}
2189
2190/**
2191 * xmlRelaxNGSchemaTypeCheck:
2192 * @data: data needed for the library
2193 * @type: the type name
2194 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002195 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002196 *
2197 * Check if the given type and value are validated by
2198 * the W3C XMLSchema Datatype library.
2199 *
2200 * Returns 1 if yes, 0 if no and -1 in case of error.
2201 */
2202static int
2203xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002204 const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002205 const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002206 void **result,
2207 xmlNodePtr node) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002208 xmlSchemaTypePtr typ;
2209 int ret;
2210
2211 /*
2212 * TODO: the type should be cached ab provided back, interface subject
2213 * to changes.
2214 * TODO: handle facets, may require an additional interface and keep
2215 * the value returned from the validation.
2216 */
2217 if ((type == NULL) || (value == NULL))
2218 return(-1);
2219 typ = xmlSchemaGetPredefinedType(type,
2220 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2221 if (typ == NULL)
2222 return(-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002223 ret = xmlSchemaValPredefTypeNode(typ, value,
2224 (xmlSchemaValPtr *) result, node);
2225 if (ret == 2) /* special ID error code */
2226 return(2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002227 if (ret == 0)
2228 return(1);
2229 if (ret > 0)
2230 return(0);
2231 return(-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002232}
2233
2234/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002235 * xmlRelaxNGSchemaFacetCheck:
2236 * @data: data needed for the library
2237 * @type: the type name
2238 * @facet: the facet name
2239 * @val: the facet value
2240 * @strval: the string value
2241 * @value: the value to check
2242 *
2243 * Function provided by a type library to check a value facet
2244 *
2245 * Returns 1 if yes, 0 if no and -1 in case of error.
2246 */
2247static int
Daniel Veillard42f12e92003-03-07 18:32:59 +00002248xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002249 const xmlChar *facetname, const xmlChar *val,
2250 const xmlChar *strval, void *value) {
2251 xmlSchemaFacetPtr facet;
2252 xmlSchemaTypePtr typ;
2253 int ret;
2254
2255 if ((type == NULL) || (strval == NULL))
2256 return(-1);
2257 typ = xmlSchemaGetPredefinedType(type,
2258 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2259 if (typ == NULL)
2260 return(-1);
2261
2262 facet = xmlSchemaNewFacet();
2263 if (facet == NULL)
2264 return(-1);
2265
2266 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2267 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2268 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2269 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2270 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2271 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2272 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2273 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2274 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2275 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2276 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2277 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2278 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2279 facet->type = XML_SCHEMA_FACET_PATTERN;
2280 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2281 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2282 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2283 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2284 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2285 facet->type = XML_SCHEMA_FACET_LENGTH;
2286 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2287 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2288 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2289 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2290 } else {
2291 xmlSchemaFreeFacet(facet);
2292 return(-1);
2293 }
2294 facet->value = xmlStrdup(val);
2295 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2296 if (ret != 0) {
2297 xmlSchemaFreeFacet(facet);
2298 return(-1);
2299 }
2300 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2301 xmlSchemaFreeFacet(facet);
2302 if (ret != 0)
2303 return(-1);
2304 return(0);
2305}
2306
2307/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002308 * xmlRelaxNGSchemaFreeValue:
2309 * @data: data needed for the library
2310 * @value: the value to free
2311 *
2312 * Function provided by a type library to free a Schemas value
2313 *
2314 * Returns 1 if yes, 0 if no and -1 in case of error.
2315 */
2316static void
2317xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) {
2318 xmlSchemaFreeValue(value);
2319}
2320
2321/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002322 * xmlRelaxNGSchemaTypeCompare:
2323 * @data: data needed for the library
2324 * @type: the type name
2325 * @value1: the first value
2326 * @value2: the second value
2327 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002328 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002329 * Datatype library.
2330 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002331 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002332 */
2333static int
2334xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2335 const xmlChar *type ATTRIBUTE_UNUSED,
2336 const xmlChar *value1 ATTRIBUTE_UNUSED,
2337 const xmlChar *value2 ATTRIBUTE_UNUSED) {
Daniel Veillard80b19092003-03-28 13:29:53 +00002338 int ret;
2339 xmlSchemaTypePtr typ;
2340 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2341
2342 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2343 return(-1);
2344 typ = xmlSchemaGetPredefinedType(type,
2345 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2346 if (typ == NULL)
2347 return(-1);
2348 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, NULL);
2349 if (ret != 0)
2350 return(-1);
2351 if (res1 == NULL)
2352 return(-1);
2353 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, NULL);
2354 if (ret != 0) {
2355 xmlSchemaFreeValue(res1);
2356 return(-1);
2357 }
2358 if (res1 == NULL) {
2359 xmlSchemaFreeValue(res1);
2360 return(-1);
2361 }
2362 ret = xmlSchemaCompareValues(res1, res2);
2363 xmlSchemaFreeValue(res1);
2364 xmlSchemaFreeValue(res2);
2365 if (ret == -2)
2366 return(-1);
2367 if (ret == 0)
2368 return(1);
2369 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002370}
2371
2372/**
2373 * xmlRelaxNGDefaultTypeHave:
2374 * @data: data needed for the library
2375 * @type: the type name
2376 *
2377 * Check if the given type is provided by
2378 * the default datatype library.
2379 *
2380 * Returns 1 if yes, 0 if no and -1 in case of error.
2381 */
2382static int
2383xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) {
2384 if (type == NULL)
2385 return(-1);
2386 if (xmlStrEqual(type, BAD_CAST "string"))
2387 return(1);
2388 if (xmlStrEqual(type, BAD_CAST "token"))
2389 return(1);
2390 return(0);
2391}
2392
2393/**
2394 * xmlRelaxNGDefaultTypeCheck:
2395 * @data: data needed for the library
2396 * @type: the type name
2397 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002398 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002399 *
2400 * Check if the given type and value are validated by
2401 * the default datatype library.
2402 *
2403 * Returns 1 if yes, 0 if no and -1 in case of error.
2404 */
2405static int
2406xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2407 const xmlChar *type ATTRIBUTE_UNUSED,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002408 const xmlChar *value ATTRIBUTE_UNUSED,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002409 void **result ATTRIBUTE_UNUSED,
2410 xmlNodePtr node ATTRIBUTE_UNUSED) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002411 if (value == NULL)
2412 return(-1);
2413 if (xmlStrEqual(type, BAD_CAST "string"))
2414 return(1);
2415 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002416 return(1);
2417 }
2418
2419 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002420}
2421
2422/**
2423 * xmlRelaxNGDefaultTypeCompare:
2424 * @data: data needed for the library
2425 * @type: the type name
2426 * @value1: the first value
2427 * @value2: the second value
2428 *
2429 * Compare two values accordingly a type from the default
2430 * datatype library.
2431 *
2432 * Returns 1 if yes, 0 if no and -1 in case of error.
2433 */
2434static int
2435xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
2436 const xmlChar *type ATTRIBUTE_UNUSED,
2437 const xmlChar *value1 ATTRIBUTE_UNUSED,
2438 const xmlChar *value2 ATTRIBUTE_UNUSED) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00002439 int ret = -1;
2440
2441 if (xmlStrEqual(type, BAD_CAST "string")) {
2442 ret = xmlStrEqual(value1, value2);
2443 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2444 if (!xmlStrEqual(value1, value2)) {
2445 xmlChar *nval, *nvalue;
2446
2447 /*
2448 * TODO: trivial optimizations are possible by
2449 * computing at compile-time
2450 */
2451 nval = xmlRelaxNGNormalize(NULL, value1);
2452 nvalue = xmlRelaxNGNormalize(NULL, value2);
2453
Daniel Veillardd4310742003-02-18 21:12:46 +00002454 if ((nval == NULL) || (nvalue == NULL))
Daniel Veillardea3f3982003-01-26 19:45:18 +00002455 ret = -1;
Daniel Veillardd4310742003-02-18 21:12:46 +00002456 else if (xmlStrEqual(nval, nvalue))
2457 ret = 1;
2458 else
2459 ret = 0;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002460 if (nval != NULL)
2461 xmlFree(nval);
2462 if (nvalue != NULL)
2463 xmlFree(nvalue);
Daniel Veillardd4310742003-02-18 21:12:46 +00002464 } else
2465 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002466 }
2467 return(ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002468}
2469
2470static int xmlRelaxNGTypeInitialized = 0;
2471static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2472
2473/**
2474 * xmlRelaxNGFreeTypeLibrary:
2475 * @lib: the type library structure
2476 * @namespace: the URI bound to the library
2477 *
2478 * Free the structure associated to the type library
2479 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002480static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002481xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2482 const xmlChar *namespace ATTRIBUTE_UNUSED) {
2483 if (lib == NULL)
2484 return;
2485 if (lib->namespace != NULL)
2486 xmlFree((xmlChar *)lib->namespace);
2487 xmlFree(lib);
2488}
2489
2490/**
2491 * xmlRelaxNGRegisterTypeLibrary:
2492 * @namespace: the URI bound to the library
2493 * @data: data associated to the library
2494 * @have: the provide function
2495 * @check: the checking function
2496 * @comp: the comparison function
2497 *
2498 * Register a new type library
2499 *
2500 * Returns 0 in case of success and -1 in case of error.
2501 */
2502static int
2503xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data,
2504 xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002505 xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet,
2506 xmlRelaxNGTypeFree freef) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002507 xmlRelaxNGTypeLibraryPtr lib;
2508 int ret;
2509
2510 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2511 (check == NULL) || (comp == NULL))
2512 return(-1);
2513 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2514 xmlGenericError(xmlGenericErrorContext,
2515 "Relax-NG types library '%s' already registered\n",
2516 namespace);
2517 return(-1);
2518 }
2519 lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2520 if (lib == NULL) {
2521 xmlGenericError(xmlGenericErrorContext,
2522 "Relax-NG types library '%s' malloc() failed\n",
2523 namespace);
2524 return (-1);
2525 }
2526 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2527 lib->namespace = xmlStrdup(namespace);
2528 lib->data = data;
2529 lib->have = have;
2530 lib->comp = comp;
2531 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002532 lib->facet = facet;
2533 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002534 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2535 if (ret < 0) {
2536 xmlGenericError(xmlGenericErrorContext,
2537 "Relax-NG types library failed to register '%s'\n",
2538 namespace);
2539 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2540 return(-1);
2541 }
2542 return(0);
2543}
2544
2545/**
2546 * xmlRelaxNGInitTypes:
2547 *
2548 * Initilize the default type libraries.
2549 *
2550 * Returns 0 in case of success and -1 in case of error.
2551 */
2552static int
Daniel Veillard6eadf632003-01-23 18:29:16 +00002553xmlRelaxNGInitTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002554 if (xmlRelaxNGTypeInitialized != 0)
2555 return(0);
2556 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2557 if (xmlRelaxNGRegisteredTypes == NULL) {
2558 xmlGenericError(xmlGenericErrorContext,
2559 "Failed to allocate sh table for Relax-NG types\n");
2560 return(-1);
2561 }
2562 xmlRelaxNGRegisterTypeLibrary(
2563 BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes",
2564 NULL,
2565 xmlRelaxNGSchemaTypeHave,
2566 xmlRelaxNGSchemaTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002567 xmlRelaxNGSchemaTypeCompare,
2568 xmlRelaxNGSchemaFacetCheck,
Daniel Veillard80b19092003-03-28 13:29:53 +00002569 xmlRelaxNGSchemaFreeValue);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002570 xmlRelaxNGRegisterTypeLibrary(
2571 xmlRelaxNGNs,
2572 NULL,
2573 xmlRelaxNGDefaultTypeHave,
2574 xmlRelaxNGDefaultTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002575 xmlRelaxNGDefaultTypeCompare,
2576 NULL,
2577 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002578 xmlRelaxNGTypeInitialized = 1;
2579 return(0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002580}
2581
2582/**
2583 * xmlRelaxNGCleanupTypes:
2584 *
2585 * Cleanup the default Schemas type library associated to RelaxNG
2586 */
2587void
2588xmlRelaxNGCleanupTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002589 if (xmlRelaxNGTypeInitialized == 0)
2590 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002591 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002592 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2593 xmlRelaxNGFreeTypeLibrary);
2594 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002595}
2596
2597/************************************************************************
2598 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002599 * Compiling element content into regexp *
2600 * *
2601 * Sometime the element content can be compiled into a pure regexp, *
2602 * This allows a faster execution and streamability at that level *
2603 * *
2604 ************************************************************************/
2605
2606/**
2607 * xmlRelaxNGIsCompileable:
2608 * @define: the definition to check
2609 *
2610 * Check if a definition is nullable.
2611 *
2612 * Returns 1 if yes, 0 if no and -1 in case of error
2613 */
2614static int
2615xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) {
2616 if (def == NULL) {
2617 return(-1);
2618 }
2619 switch(def->type) {
2620 case XML_RELAXNG_REF:
2621 case XML_RELAXNG_EXTERNALREF:
2622 case XML_RELAXNG_PARENTREF:
2623 case XML_RELAXNG_NOOP:
2624 case XML_RELAXNG_START:
2625 return(xmlRelaxNGIsCompileable(def->content));
2626 case XML_RELAXNG_TEXT:
2627 case XML_RELAXNG_DATATYPE:
2628 case XML_RELAXNG_LIST:
2629 case XML_RELAXNG_PARAM:
2630 case XML_RELAXNG_VALUE:
2631
2632 case XML_RELAXNG_EMPTY:
2633 case XML_RELAXNG_ELEMENT:
2634 return(1);
2635 case XML_RELAXNG_OPTIONAL:
2636 case XML_RELAXNG_ZEROORMORE:
2637 case XML_RELAXNG_ONEORMORE:
2638 case XML_RELAXNG_CHOICE:
2639 case XML_RELAXNG_GROUP:
2640 case XML_RELAXNG_DEF: {
2641 xmlRelaxNGDefinePtr list;
2642 int ret;
2643
2644 list = def->content;
2645 while (list != NULL) {
2646 ret = xmlRelaxNGIsCompileable(list);
2647 if (ret != 1)
2648 return(ret);
2649 list = list->next;
2650 }
2651 return(1);
2652 }
2653 case XML_RELAXNG_EXCEPT:
2654 case XML_RELAXNG_ATTRIBUTE:
2655 case XML_RELAXNG_INTERLEAVE:
2656 return(0);
2657 case XML_RELAXNG_NOT_ALLOWED:
2658 return(-1);
2659 }
2660 return(-1);
2661}
2662
2663/************************************************************************
2664 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002665 * Parsing functions *
2666 * *
2667 ************************************************************************/
2668
2669static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(
2670 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
2671static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(
2672 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
2673static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(
Daniel Veillard154877e2003-01-30 12:17:05 +00002674 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002675static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(
2676 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002677static xmlRelaxNGPtr xmlRelaxNGParseDocument(
2678 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00002679static int xmlRelaxNGParseGrammarContent(
2680 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002681static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(
2682 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
2683 xmlRelaxNGDefinePtr def);
Daniel Veillard419a7682003-02-03 23:22:49 +00002684static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(
2685 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillardfd573f12003-03-16 17:52:32 +00002686static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
2687 xmlRelaxNGDefinePtr define, xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002688
2689
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002690#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00002691
2692/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00002693 * xmlRelaxNGIsNullable:
2694 * @define: the definition to verify
2695 *
2696 * Check if a definition is nullable.
2697 *
2698 * Returns 1 if yes, 0 if no and -1 in case of error
2699 */
2700static int
2701xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) {
2702 int ret;
2703 if (define == NULL)
2704 return(-1);
2705
Daniel Veillarde063f482003-03-21 16:53:17 +00002706 if (define->dflags & IS_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00002707 return(1);
Daniel Veillarde063f482003-03-21 16:53:17 +00002708 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00002709 return(0);
2710 switch (define->type) {
2711 case XML_RELAXNG_EMPTY:
2712 case XML_RELAXNG_TEXT:
2713 ret = 1; break;
2714 case XML_RELAXNG_NOOP:
2715 case XML_RELAXNG_DEF:
2716 case XML_RELAXNG_REF:
2717 case XML_RELAXNG_EXTERNALREF:
2718 case XML_RELAXNG_PARENTREF:
2719 case XML_RELAXNG_ONEORMORE:
2720 ret = xmlRelaxNGIsNullable(define->content);
2721 break;
2722 case XML_RELAXNG_EXCEPT:
2723 case XML_RELAXNG_NOT_ALLOWED:
2724 case XML_RELAXNG_ELEMENT:
2725 case XML_RELAXNG_DATATYPE:
2726 case XML_RELAXNG_PARAM:
2727 case XML_RELAXNG_VALUE:
2728 case XML_RELAXNG_LIST:
2729 case XML_RELAXNG_ATTRIBUTE:
2730 ret = 0; break;
2731 case XML_RELAXNG_CHOICE: {
2732 xmlRelaxNGDefinePtr list = define->content;
2733
2734 while (list != NULL) {
2735 ret = xmlRelaxNGIsNullable(list);
2736 if (ret != 0)
2737 goto done;
2738 list = list->next;
2739 }
2740 ret = 0; break;
2741 }
2742 case XML_RELAXNG_START:
2743 case XML_RELAXNG_INTERLEAVE:
2744 case XML_RELAXNG_GROUP: {
2745 xmlRelaxNGDefinePtr list = define->content;
2746
2747 while (list != NULL) {
2748 ret = xmlRelaxNGIsNullable(list);
2749 if (ret != 1)
2750 goto done;
2751 list = list->next;
2752 }
2753 return(1);
2754 }
2755 default:
2756 return(-1);
2757 }
2758done:
2759 if (ret == 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00002760 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00002761 if (ret == 1)
Daniel Veillarde063f482003-03-21 16:53:17 +00002762 define->dflags |= IS_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00002763 return(ret);
2764}
2765
2766/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00002767 * xmlRelaxNGIsBlank:
2768 * @str: a string
2769 *
2770 * Check if a string is ignorable c.f. 4.2. Whitespace
2771 *
2772 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
2773 */
2774static int
2775xmlRelaxNGIsBlank(xmlChar *str) {
2776 if (str == NULL)
2777 return(1);
2778 while (*str != 0) {
2779 if (!(IS_BLANK(*str))) return(0);
2780 str++;
2781 }
2782 return(1);
2783}
2784
Daniel Veillard6eadf632003-01-23 18:29:16 +00002785/**
2786 * xmlRelaxNGGetDataTypeLibrary:
2787 * @ctxt: a Relax-NG parser context
2788 * @node: the current data or value element
2789 *
2790 * Applies algorithm from 4.3. datatypeLibrary attribute
2791 *
2792 * Returns the datatypeLibary value or NULL if not found
2793 */
2794static xmlChar *
2795xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
2796 xmlNodePtr node) {
2797 xmlChar *ret, *escape;
2798
Daniel Veillard6eadf632003-01-23 18:29:16 +00002799 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
2800 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
2801 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00002802 if (ret[0] == 0) {
2803 xmlFree(ret);
2804 return(NULL);
2805 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002806 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
Daniel Veillard6eadf632003-01-23 18:29:16 +00002807 if (escape == NULL) {
2808 return(ret);
2809 }
2810 xmlFree(ret);
2811 return(escape);
2812 }
2813 }
2814 node = node->parent;
2815 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillarde5b110b2003-02-04 14:43:39 +00002816 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
2817 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00002818 if (ret[0] == 0) {
2819 xmlFree(ret);
2820 return(NULL);
2821 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00002822 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
2823 if (escape == NULL) {
2824 return(ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002825 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00002826 xmlFree(ret);
2827 return(escape);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002828 }
2829 node = node->parent;
2830 }
2831 return(NULL);
2832}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002833
2834/**
Daniel Veillardedc91922003-01-26 00:52:04 +00002835 * xmlRelaxNGParseValue:
2836 * @ctxt: a Relax-NG parser context
2837 * @node: the data node.
2838 *
2839 * parse the content of a RelaxNG value node.
2840 *
2841 * Returns the definition pointer or NULL in case of error
2842 */
2843static xmlRelaxNGDefinePtr
2844xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
2845 xmlRelaxNGDefinePtr def = NULL;
2846 xmlRelaxNGTypeLibraryPtr lib;
2847 xmlChar *type;
2848 xmlChar *library;
2849 int tmp;
2850
Daniel Veillardfd573f12003-03-16 17:52:32 +00002851 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00002852 if (def == NULL)
2853 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00002854 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00002855
2856 type = xmlGetProp(node, BAD_CAST "type");
2857 if (type != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00002858 xmlRelaxNGNormExtSpace(type);
2859 if (xmlValidateNCName(type, 0)) {
2860 if (ctxt->error != NULL)
2861 ctxt->error(ctxt->userData,
2862 "value type '%s' is not an NCName\n",
2863 type);
2864 ctxt->nbErrors++;
2865 }
Daniel Veillardedc91922003-01-26 00:52:04 +00002866 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
2867 if (library == NULL)
2868 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
2869
2870 def->name = type;
2871 def->ns = library;
2872
2873 lib = (xmlRelaxNGTypeLibraryPtr)
2874 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
2875 if (lib == NULL) {
2876 if (ctxt->error != NULL)
2877 ctxt->error(ctxt->userData,
2878 "Use of unregistered type library '%s'\n",
2879 library);
2880 ctxt->nbErrors++;
2881 def->data = NULL;
2882 } else {
2883 def->data = lib;
2884 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00002885 if (ctxt->error != NULL)
2886 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00002887 "Internal error with type library '%s': no 'have'\n",
2888 library);
2889 ctxt->nbErrors++;
2890 } else {
2891 tmp = lib->have(lib->data, def->name);
2892 if (tmp != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00002893 if (ctxt->error != NULL)
2894 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00002895 "Error type '%s' is not exported by type library '%s'\n",
2896 def->name, library);
2897 ctxt->nbErrors++;
2898 }
2899 }
2900 }
2901 }
2902 if (node->children == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002903 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00002904 } else if (((node->children->type != XML_TEXT_NODE) &&
2905 (node->children->type != XML_CDATA_SECTION_NODE)) ||
Daniel Veillardedc91922003-01-26 00:52:04 +00002906 (node->children->next != NULL)) {
2907 if (ctxt->error != NULL)
2908 ctxt->error(ctxt->userData,
2909 "Expecting a single text value for <value>content\n");
2910 ctxt->nbErrors++;
2911 } else {
2912 def->value = xmlNodeGetContent(node);
2913 if (def->value == NULL) {
2914 if (ctxt->error != NULL)
2915 ctxt->error(ctxt->userData,
2916 "Element <value> has no content\n");
2917 ctxt->nbErrors++;
2918 }
2919 }
2920 /* TODO check ahead of time that the value is okay per the type */
2921 return(def);
2922}
2923
2924/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002925 * xmlRelaxNGParseData:
2926 * @ctxt: a Relax-NG parser context
2927 * @node: the data node.
2928 *
2929 * parse the content of a RelaxNG data node.
2930 *
2931 * Returns the definition pointer or NULL in case of error
2932 */
2933static xmlRelaxNGDefinePtr
2934xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard416589a2003-02-17 17:25:42 +00002935 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00002936 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002937 xmlRelaxNGTypeLibraryPtr lib;
2938 xmlChar *type;
2939 xmlChar *library;
2940 xmlNodePtr content;
2941 int tmp;
2942
2943 type = xmlGetProp(node, BAD_CAST "type");
2944 if (type == NULL) {
2945 if (ctxt->error != NULL)
2946 ctxt->error(ctxt->userData,
2947 "data has no type\n");
2948 ctxt->nbErrors++;
2949 return(NULL);
2950 }
Daniel Veillardd2298792003-02-14 16:54:11 +00002951 xmlRelaxNGNormExtSpace(type);
2952 if (xmlValidateNCName(type, 0)) {
2953 if (ctxt->error != NULL)
2954 ctxt->error(ctxt->userData,
2955 "data type '%s' is not an NCName\n",
2956 type);
2957 ctxt->nbErrors++;
2958 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002959 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
2960 if (library == NULL)
2961 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
2962
Daniel Veillardfd573f12003-03-16 17:52:32 +00002963 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002964 if (def == NULL) {
2965 xmlFree(type);
2966 return(NULL);
2967 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00002968 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002969 def->name = type;
2970 def->ns = library;
2971
2972 lib = (xmlRelaxNGTypeLibraryPtr)
2973 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
2974 if (lib == NULL) {
2975 if (ctxt->error != NULL)
2976 ctxt->error(ctxt->userData,
2977 "Use of unregistered type library '%s'\n",
2978 library);
2979 ctxt->nbErrors++;
2980 def->data = NULL;
2981 } else {
2982 def->data = lib;
2983 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00002984 if (ctxt->error != NULL)
2985 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002986 "Internal error with type library '%s': no 'have'\n",
2987 library);
2988 ctxt->nbErrors++;
2989 } else {
2990 tmp = lib->have(lib->data, def->name);
2991 if (tmp != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00002992 if (ctxt->error != NULL)
2993 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002994 "Error type '%s' is not exported by type library '%s'\n",
2995 def->name, library);
2996 ctxt->nbErrors++;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002997 } else if ((xmlStrEqual(library, BAD_CAST
2998 "http://www.w3.org/2001/XMLSchema-datatypes")) &&
2999 ((xmlStrEqual(def->name, BAD_CAST "IDREF")) ||
3000 (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3001 ctxt->idref = 1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003002 }
3003 }
3004 }
3005 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003006
3007 /*
3008 * Handle optional params
3009 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003010 while (content != NULL) {
Daniel Veillard416589a2003-02-17 17:25:42 +00003011 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3012 break;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003013 if (xmlStrEqual(library,
3014 BAD_CAST"http://relaxng.org/ns/structure/1.0")) {
3015 if (ctxt->error != NULL)
3016 ctxt->error(ctxt->userData,
3017 "Type library '%s' does not allow type parameters\n",
3018 library);
3019 ctxt->nbErrors++;
3020 content = content->next;
3021 while ((content != NULL) &&
3022 (xmlStrEqual(content->name, BAD_CAST "param")))
3023 content = content->next;
3024 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003025 param = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003026 if (param != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003027 param->type = XML_RELAXNG_PARAM;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003028 param->name = xmlGetProp(content, BAD_CAST "name");
3029 if (param->name == NULL) {
3030 if (ctxt->error != NULL)
3031 ctxt->error(ctxt->userData,
3032 "param has no name\n");
3033 ctxt->nbErrors++;
3034 }
3035 param->value = xmlNodeGetContent(content);
3036 if (lastparam == NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003037 def->attrs = lastparam = param;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003038 } else {
3039 lastparam->next = param;
3040 lastparam = param;
3041 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003042 if (lib != NULL) {
3043 }
Daniel Veillard8fe98712003-02-19 00:19:14 +00003044 }
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003045 content = content->next;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003046 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003047 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003048 /*
3049 * Handle optional except
3050 */
3051 if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3052 xmlNodePtr child;
3053 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
3054
Daniel Veillardfd573f12003-03-16 17:52:32 +00003055 except = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard416589a2003-02-17 17:25:42 +00003056 if (except == NULL) {
3057 return(def);
3058 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003059 except->type = XML_RELAXNG_EXCEPT;
Daniel Veillard416589a2003-02-17 17:25:42 +00003060 child = content->children;
3061 if (last == NULL) {
3062 def->content = except;
3063 } else {
3064 last->next = except;
3065 }
3066 if (child == NULL) {
3067 if (ctxt->error != NULL)
3068 ctxt->error(ctxt->userData,
3069 "except has no content\n");
3070 ctxt->nbErrors++;
3071 }
3072 while (child != NULL) {
3073 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3074 if (tmp2 != NULL) {
3075 if (last2 == NULL) {
3076 except->content = last2 = tmp2;
3077 } else {
3078 last2->next = tmp2;
3079 last2 = tmp2;
3080 }
3081 }
3082 child = child->next;
3083 }
3084 content = content->next;
3085 }
3086 /*
3087 * Check there is no unhandled data
3088 */
3089 if (content != NULL) {
3090 if (ctxt->error != NULL)
3091 ctxt->error(ctxt->userData,
3092 "Element data has unexpected content %s\n", content->name);
3093 ctxt->nbErrors++;
3094 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003095
3096 return(def);
3097}
3098
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003099static const xmlChar *invalidName = BAD_CAST "\1";
3100
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003101/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003102 * xmlRelaxNGCompareNameClasses:
3103 * @defs1: the first element/attribute defs
3104 * @defs2: the second element/attribute defs
3105 * @name: the restriction on the name
3106 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003107 *
3108 * Compare the 2 lists of element definitions. The comparison is
3109 * that if both lists do not accept the same QNames, it returns 1
3110 * If the 2 lists can accept the same QName the comparison returns 0
3111 *
3112 * Returns 1 disttinct, 0 if equal
3113 */
3114static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003115xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3116 xmlRelaxNGDefinePtr def2) {
3117 int ret = 1;
3118 xmlNode node;
3119 xmlNs ns;
3120 xmlRelaxNGValidCtxt ctxt;
3121 ctxt.flags = FLAGS_IGNORABLE;
3122
Daniel Veillard42f12e92003-03-07 18:32:59 +00003123 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3124
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003125 if ((def1->type == XML_RELAXNG_ELEMENT) ||
3126 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3127 if (def2->type == XML_RELAXNG_TEXT)
3128 return(1);
3129 if (def1->name != NULL) {
3130 node.name = def1->name;
3131 } else {
3132 node.name = invalidName;
3133 }
3134 node.ns = &ns;
3135 if (def1->ns != NULL) {
3136 if (def1->ns[0] == 0) {
3137 node.ns = NULL;
3138 } else {
3139 ns.href = def1->ns;
3140 }
3141 } else {
3142 ns.href = invalidName;
3143 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003144 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003145 if (def1->nameClass != NULL) {
3146 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3147 } else {
3148 ret = 0;
3149 }
3150 } else {
3151 ret = 1;
3152 }
3153 } else if (def1->type == XML_RELAXNG_TEXT) {
3154 if (def2->type == XML_RELAXNG_TEXT)
3155 return(0);
3156 return(1);
3157 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003158 TODO
3159 ret = 0;
3160 } else {
3161 TODO
3162 ret = 0;
3163 }
3164 if (ret == 0)
3165 return(ret);
3166 if ((def2->type == XML_RELAXNG_ELEMENT) ||
3167 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3168 if (def2->name != NULL) {
3169 node.name = def2->name;
3170 } else {
3171 node.name = invalidName;
3172 }
3173 node.ns = &ns;
3174 if (def2->ns != NULL) {
3175 if (def2->ns[0] == 0) {
3176 node.ns = NULL;
3177 } else {
3178 ns.href = def2->ns;
3179 }
3180 } else {
3181 ns.href = invalidName;
3182 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003183 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003184 if (def2->nameClass != NULL) {
3185 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3186 } else {
3187 ret = 0;
3188 }
3189 } else {
3190 ret = 1;
3191 }
3192 } else {
3193 TODO
3194 ret = 0;
3195 }
3196
3197 return(ret);
3198}
3199
3200/**
3201 * xmlRelaxNGCompareElemDefLists:
3202 * @ctxt: a Relax-NG parser context
3203 * @defs1: the first list of element/attribute defs
3204 * @defs2: the second list of element/attribute defs
3205 *
3206 * Compare the 2 lists of element or attribute definitions. The comparison
3207 * is that if both lists do not accept the same QNames, it returns 1
3208 * If the 2 lists can accept the same QName the comparison returns 0
3209 *
3210 * Returns 1 disttinct, 0 if equal
3211 */
3212static int
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003213xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3214 xmlRelaxNGDefinePtr *def1,
3215 xmlRelaxNGDefinePtr *def2) {
3216 xmlRelaxNGDefinePtr *basedef2 = def2;
3217
Daniel Veillard154877e2003-01-30 12:17:05 +00003218 if ((def1 == NULL) || (def2 == NULL))
3219 return(1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003220 if ((*def1 == NULL) || (*def2 == NULL))
3221 return(1);
3222 while (*def1 != NULL) {
3223 while ((*def2) != NULL) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003224 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3225 return(0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003226 def2++;
3227 }
3228 def2 = basedef2;
3229 def1++;
3230 }
3231 return(1);
3232}
3233
3234/**
3235 * xmlRelaxNGGetElements:
3236 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003237 * @def: the definition definition
3238 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003239 *
3240 * Compute the list of top elements a definition can generate
3241 *
3242 * Returns a list of elements or NULL if none was found.
3243 */
3244static xmlRelaxNGDefinePtr *
3245xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003246 xmlRelaxNGDefinePtr def,
3247 int eora) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003248 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003249 int len = 0;
3250 int max = 0;
3251
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003252 /*
3253 * Don't run that check in case of error. Infinite recursion
3254 * becomes possible.
3255 */
3256 if (ctxt->nbErrors != 0)
3257 return(NULL);
3258
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003259 parent = NULL;
3260 cur = def;
3261 while (cur != NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003262 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3263 (cur->type == XML_RELAXNG_TEXT))) ||
3264 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003265 if (ret == NULL) {
3266 max = 10;
3267 ret = (xmlRelaxNGDefinePtr *)
3268 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3269 if (ret == NULL) {
3270 if (ctxt->error != NULL)
3271 ctxt->error(ctxt->userData,
3272 "Out of memory in element search\n");
3273 ctxt->nbErrors++;
3274 return(NULL);
3275 }
3276 } else if (max <= len) {
3277 max *= 2;
3278 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3279 if (ret == NULL) {
3280 if (ctxt->error != NULL)
3281 ctxt->error(ctxt->userData,
3282 "Out of memory in element search\n");
3283 ctxt->nbErrors++;
3284 return(NULL);
3285 }
3286 }
Daniel Veillardb08c9812003-01-28 23:09:49 +00003287 ret[len++] = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003288 ret[len] = NULL;
3289 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3290 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3291 (cur->type == XML_RELAXNG_GROUP) ||
3292 (cur->type == XML_RELAXNG_ONEORMORE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00003293 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3294 (cur->type == XML_RELAXNG_OPTIONAL) ||
Daniel Veillard952379b2003-03-17 15:37:12 +00003295 (cur->type == XML_RELAXNG_PARENTREF) ||
Daniel Veillardb08c9812003-01-28 23:09:49 +00003296 (cur->type == XML_RELAXNG_REF) ||
3297 (cur->type == XML_RELAXNG_DEF)) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003298 /*
3299 * Don't go within elements or attributes or string values.
3300 * Just gather the element top list
3301 */
3302 if (cur->content != NULL) {
3303 parent = cur;
3304 cur = cur->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003305 tmp = cur;
3306 while (tmp != NULL) {
3307 tmp->parent = parent;
3308 tmp = tmp->next;
3309 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003310 continue;
3311 }
3312 }
Daniel Veillard154877e2003-01-30 12:17:05 +00003313 if (cur == def)
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003314 break;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003315 if (cur->next != NULL) {
3316 cur = cur->next;
3317 continue;
3318 }
3319 do {
3320 cur = cur->parent;
3321 if (cur == NULL) break;
3322 if (cur == def) return(ret);
3323 if (cur->next != NULL) {
3324 cur = cur->next;
3325 break;
3326 }
3327 } while (cur != NULL);
3328 }
3329 return(ret);
3330}
3331
3332/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003333 * xmlRelaxNGCheckChoiceDeterminism:
3334 * @ctxt: a Relax-NG parser context
3335 * @def: the choice definition
3336 *
3337 * Also used to find indeterministic pattern in choice
3338 */
3339static void
3340xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
3341 xmlRelaxNGDefinePtr def) {
3342 xmlRelaxNGDefinePtr **list;
3343 xmlRelaxNGDefinePtr cur;
3344 int nbchild = 0, i, j, ret;
3345 int is_nullable = 0;
3346 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003347 xmlHashTablePtr triage = NULL;
3348 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003349
3350 if ((def == NULL) ||
3351 (def->type != XML_RELAXNG_CHOICE))
3352 return;
3353
Daniel Veillarde063f482003-03-21 16:53:17 +00003354 if (def->dflags & IS_PROCESSED)
3355 return;
3356
Daniel Veillardfd573f12003-03-16 17:52:32 +00003357 /*
3358 * Don't run that check in case of error. Infinite recursion
3359 * becomes possible.
3360 */
3361 if (ctxt->nbErrors != 0)
3362 return;
3363
3364 is_nullable = xmlRelaxNGIsNullable(def);
3365
3366 cur = def->content;
3367 while (cur != NULL) {
3368 nbchild++;
3369 cur = cur->next;
3370 }
3371
3372 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3373 sizeof(xmlRelaxNGDefinePtr *));
3374 if (list == NULL) {
3375 if (ctxt->error != NULL)
3376 ctxt->error(ctxt->userData,
3377 "Out of memory in choice computation\n");
3378 ctxt->nbErrors++;
3379 return;
3380 }
3381 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003382 /*
3383 * a bit strong but safe
3384 */
3385 if (is_nullable == 0) {
3386 triage = xmlHashCreate(10);
3387 } else {
3388 is_triable = 0;
3389 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003390 cur = def->content;
3391 while (cur != NULL) {
3392 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
Daniel Veillarde063f482003-03-21 16:53:17 +00003393 if ((list[i] == NULL) || (list[i][0] == NULL)) {
3394 is_triable = 0;
3395 } else if (is_triable == 1) {
3396 xmlRelaxNGDefinePtr *tmp;
3397 int res;
3398
3399 tmp = list[i];
3400 while ((*tmp != NULL) && (is_triable == 1)) {
3401 if ((*tmp)->type == XML_RELAXNG_TEXT) {
3402 res = xmlHashAddEntry2(triage,
3403 BAD_CAST "#text", NULL,
3404 (void *)cur);
3405 if (res != 0)
3406 is_triable = -1;
3407 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
3408 ((*tmp)->name != NULL)) {
3409 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3410 res = xmlHashAddEntry2(triage,
3411 (*tmp)->name, NULL,
3412 (void *)cur);
3413 else
3414 res = xmlHashAddEntry2(triage,
3415 (*tmp)->name, (*tmp)->ns,
3416 (void *)cur);
3417 if (res != 0)
3418 is_triable = -1;
3419 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
3420 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3421 res = xmlHashAddEntry2(triage,
3422 BAD_CAST "#any", NULL,
3423 (void *)cur);
3424 else
3425 res = xmlHashAddEntry2(triage,
3426 BAD_CAST "#any", (*tmp)->ns,
3427 (void *)cur);
3428 if (res != 0)
3429 is_triable = -1;
3430 } else {
3431 is_triable = -1;
3432 }
3433 tmp++;
3434 }
3435 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003436 i++;
3437 cur = cur->next;
3438 }
3439
3440 for (i = 0;i < nbchild;i++) {
3441 if (list[i] == NULL)
3442 continue;
3443 for (j = 0;j < i;j++) {
3444 if (list[j] == NULL)
3445 continue;
3446 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3447 if (ret == 0) {
3448 is_indeterminist = 1;
3449 }
3450 }
3451 }
3452 for (i = 0;i < nbchild;i++) {
3453 if (list[i] != NULL)
3454 xmlFree(list[i]);
3455 }
3456
3457 xmlFree(list);
3458 if (is_indeterminist) {
Daniel Veillarde063f482003-03-21 16:53:17 +00003459 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003460 }
Daniel Veillarde063f482003-03-21 16:53:17 +00003461 if (is_triable == 1) {
3462 def->dflags |= IS_TRIABLE;
3463 def->data = triage;
3464 } else if (triage != NULL) {
3465 xmlHashFree(triage, NULL);
3466 }
3467 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003468}
3469
3470/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003471 * xmlRelaxNGCheckGroupAttrs:
3472 * @ctxt: a Relax-NG parser context
3473 * @def: the group definition
3474 *
3475 * Detects violations of rule 7.3
3476 */
3477static void
3478xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
3479 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003480 xmlRelaxNGDefinePtr **list;
3481 xmlRelaxNGDefinePtr cur;
3482 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003483
3484 if ((def == NULL) ||
3485 ((def->type != XML_RELAXNG_GROUP) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00003486 (def->type != XML_RELAXNG_ELEMENT)))
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003487 return;
3488
Daniel Veillarde063f482003-03-21 16:53:17 +00003489 if (def->dflags & IS_PROCESSED)
3490 return;
3491
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003492 /*
3493 * Don't run that check in case of error. Infinite recursion
3494 * becomes possible.
3495 */
3496 if (ctxt->nbErrors != 0)
3497 return;
3498
Daniel Veillardfd573f12003-03-16 17:52:32 +00003499 cur = def->attrs;
3500 while (cur != NULL) {
3501 nbchild++;
3502 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003503 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003504 cur = def->content;
3505 while (cur != NULL) {
3506 nbchild++;
3507 cur = cur->next;
3508 }
3509
3510 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3511 sizeof(xmlRelaxNGDefinePtr *));
3512 if (list == NULL) {
3513 if (ctxt->error != NULL)
3514 ctxt->error(ctxt->userData,
3515 "Out of memory in group computation\n");
3516 ctxt->nbErrors++;
3517 return;
3518 }
3519 i = 0;
3520 cur = def->attrs;
3521 while (cur != NULL) {
3522 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
3523 i++;
3524 cur = cur->next;
3525 }
3526 cur = def->content;
3527 while (cur != NULL) {
3528 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
3529 i++;
3530 cur = cur->next;
3531 }
3532
3533 for (i = 0;i < nbchild;i++) {
3534 if (list[i] == NULL)
3535 continue;
3536 for (j = 0;j < i;j++) {
3537 if (list[j] == NULL)
3538 continue;
3539 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3540 if (ret == 0) {
3541 if (ctxt->error != NULL)
3542 ctxt->error(ctxt->userData,
3543 "Attributes conflicts in group\n");
3544 ctxt->nbErrors++;
3545 }
3546 }
3547 }
3548 for (i = 0;i < nbchild;i++) {
3549 if (list[i] != NULL)
3550 xmlFree(list[i]);
3551 }
3552
3553 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00003554 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003555}
3556
3557/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003558 * xmlRelaxNGComputeInterleaves:
3559 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003560 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00003561 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003562 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00003563 * A lot of work for preprocessing interleave definitions
3564 * is potentially needed to get a decent execution speed at runtime
3565 * - trying to get a total order on the element nodes generated
3566 * by the interleaves, order the list of interleave definitions
3567 * following that order.
3568 * - if <text/> is used to handle mixed content, it is better to
3569 * flag this in the define and simplify the runtime checking
3570 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003571 */
3572static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00003573xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
3574 xmlRelaxNGParserCtxtPtr ctxt,
3575 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003576 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003577
Daniel Veillardfd573f12003-03-16 17:52:32 +00003578 xmlRelaxNGPartitionPtr partitions = NULL;
3579 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
3580 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003581 int i,j,ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003582 int nbgroups = 0;
3583 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003584 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003585 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003586
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003587 /*
3588 * Don't run that check in case of error. Infinite recursion
3589 * becomes possible.
3590 */
3591 if (ctxt->nbErrors != 0)
3592 return;
3593
Daniel Veillardfd573f12003-03-16 17:52:32 +00003594#ifdef DEBUG_INTERLEAVE
3595 xmlGenericError(xmlGenericErrorContext,
3596 "xmlRelaxNGComputeInterleaves(%s)\n",
3597 name);
3598#endif
3599 cur = def->content;
3600 while (cur != NULL) {
3601 nbchild++;
3602 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003603 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003604
3605#ifdef DEBUG_INTERLEAVE
3606 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
3607#endif
3608 groups = (xmlRelaxNGInterleaveGroupPtr *)
3609 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
3610 if (groups == NULL)
3611 goto error;
3612 cur = def->content;
3613 while (cur != NULL) {
3614 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
3615 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
3616 if (groups[nbgroups] == NULL)
3617 goto error;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003618 if (cur->type == XML_RELAXNG_TEXT)
3619 is_mixed++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003620 groups[nbgroups]->rule = cur;
3621 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
3622 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
3623 nbgroups++;
3624 cur = cur->next;
3625 }
3626#ifdef DEBUG_INTERLEAVE
3627 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
3628#endif
3629
3630 /*
3631 * Let's check that all rules makes a partitions according to 7.4
3632 */
3633 partitions = (xmlRelaxNGPartitionPtr)
3634 xmlMalloc(sizeof(xmlRelaxNGPartition));
3635 if (partitions == NULL)
3636 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00003637 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00003638 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003639 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003640 for (i = 0;i < nbgroups;i++) {
3641 group = groups[i];
3642 for (j = i+1;j < nbgroups;j++) {
3643 if (groups[j] == NULL)
3644 continue;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003645
Daniel Veillardfd573f12003-03-16 17:52:32 +00003646 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
3647 groups[j]->defs);
3648 if (ret == 0) {
3649 if (ctxt->error != NULL)
3650 ctxt->error(ctxt->userData,
3651 "Element or text conflicts in interleave\n");
3652 ctxt->nbErrors++;
3653 }
3654 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
3655 groups[j]->attrs);
3656 if (ret == 0) {
3657 if (ctxt->error != NULL)
3658 ctxt->error(ctxt->userData,
3659 "Attributes conflicts in interleave\n");
3660 ctxt->nbErrors++;
3661 }
3662 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003663 tmp = group->defs;
3664 if ((tmp != NULL) && (*tmp != NULL)) {
3665 while (*tmp != NULL) {
3666 if ((*tmp)->type == XML_RELAXNG_TEXT) {
3667 res = xmlHashAddEntry2(partitions->triage,
3668 BAD_CAST "#text", NULL,
3669 (void *)(i + 1));
3670 if (res != 0)
3671 is_determinist = -1;
3672 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
3673 ((*tmp)->name != NULL)) {
3674 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3675 res = xmlHashAddEntry2(partitions->triage,
3676 (*tmp)->name, NULL,
3677 (void *)(i + 1));
3678 else
3679 res = xmlHashAddEntry2(partitions->triage,
3680 (*tmp)->name, (*tmp)->ns,
3681 (void *)(i + 1));
3682 if (res != 0)
3683 is_determinist = -1;
3684 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
3685 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3686 res = xmlHashAddEntry2(partitions->triage,
3687 BAD_CAST "#any", NULL,
3688 (void *)(i + 1));
3689 else
3690 res = xmlHashAddEntry2(partitions->triage,
3691 BAD_CAST "#any", (*tmp)->ns,
3692 (void *)(i + 1));
3693 if ((*tmp)->nameClass != NULL)
3694 is_determinist = 2;
3695 if (res != 0)
3696 is_determinist = -1;
3697 } else {
3698 is_determinist = -1;
3699 }
3700 tmp++;
3701 }
3702 } else {
3703 is_determinist = 0;
3704 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003705 }
3706 partitions->groups = groups;
3707
3708 /*
3709 * and save the partition list back in the def
3710 */
3711 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003712 if (is_mixed != 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00003713 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003714 if (is_determinist == 1)
3715 partitions->flags = IS_DETERMINIST;
3716 if (is_determinist == 2)
3717 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003718 return;
3719
3720error:
3721 if (ctxt->error != NULL)
3722 ctxt->error(ctxt->userData,
3723 "Out of memory in interleave computation\n");
3724 ctxt->nbErrors++;
3725 if (groups != NULL) {
3726 for (i = 0;i < nbgroups;i++)
3727 if (groups[i] != NULL) {
3728 if (groups[i]->defs != NULL)
3729 xmlFree(groups[i]->defs);
3730 xmlFree(groups[i]);
3731 }
3732 xmlFree(groups);
3733 }
3734 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003735}
3736
3737/**
3738 * xmlRelaxNGParseInterleave:
3739 * @ctxt: a Relax-NG parser context
3740 * @node: the data node.
3741 *
3742 * parse the content of a RelaxNG interleave node.
3743 *
3744 * Returns the definition pointer or NULL in case of error
3745 */
3746static xmlRelaxNGDefinePtr
3747xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3748 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003749 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003750 xmlNodePtr child;
3751
Daniel Veillardfd573f12003-03-16 17:52:32 +00003752 def = xmlRelaxNGNewDefine(ctxt, node);
3753 if (def == NULL) {
3754 return(NULL);
3755 }
3756 def->type = XML_RELAXNG_INTERLEAVE;
3757
3758 if (ctxt->interleaves == NULL)
3759 ctxt->interleaves = xmlHashCreate(10);
3760 if (ctxt->interleaves == NULL) {
3761 if (ctxt->error != NULL)
3762 ctxt->error(ctxt->userData,
3763 "Failed to create interleaves hash table\n");
3764 ctxt->nbErrors++;
3765 } else {
3766 char name[32];
3767
3768 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
3769 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
3770 if (ctxt->error != NULL)
3771 ctxt->error(ctxt->userData,
3772 "Failed to add %s to hash table\n", name);
3773 ctxt->nbErrors++;
3774 }
3775 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003776 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00003777 if (child == NULL) {
3778 if (ctxt->error != NULL)
3779 ctxt->error(ctxt->userData, "Element interleave is empty\n");
3780 ctxt->nbErrors++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00003781 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003782 while (child != NULL) {
3783 if (IS_RELAXNG(child, "element")) {
3784 cur = xmlRelaxNGParseElement(ctxt, child);
3785 } else {
3786 cur = xmlRelaxNGParsePattern(ctxt, child);
3787 }
3788 if (cur != NULL) {
3789 cur->parent = def;
3790 if (last == NULL) {
3791 def->content = last = cur;
3792 } else {
3793 last->next = cur;
3794 last = cur;
3795 }
3796 }
3797 child = child->next;
3798 }
3799
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003800 return(def);
3801}
Daniel Veillard6eadf632003-01-23 18:29:16 +00003802
3803/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00003804 * xmlRelaxNGParseInclude:
3805 * @ctxt: a Relax-NG parser context
3806 * @node: the include node
3807 *
3808 * Integrate the content of an include node in the current grammar
3809 *
3810 * Returns 0 in case of success or -1 in case of error
3811 */
3812static int
3813xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3814 xmlRelaxNGIncludePtr incl;
3815 xmlNodePtr root;
3816 int ret = 0, tmp;
3817
3818 incl = node->_private;
3819 if (incl == NULL) {
3820 if (ctxt->error != NULL)
3821 ctxt->error(ctxt->userData,
3822 "Include node has no data\n");
3823 ctxt->nbErrors++;
3824 return(-1);
3825 }
3826 root = xmlDocGetRootElement(incl->doc);
3827 if (root == NULL) {
3828 if (ctxt->error != NULL)
3829 ctxt->error(ctxt->userData,
3830 "Include document is empty\n");
3831 ctxt->nbErrors++;
3832 return(-1);
3833 }
3834 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
3835 if (ctxt->error != NULL)
3836 ctxt->error(ctxt->userData,
3837 "Include document root is not a grammar\n");
3838 ctxt->nbErrors++;
3839 return(-1);
3840 }
3841
3842 /*
3843 * Merge the definition from both the include and the internal list
3844 */
3845 if (root->children != NULL) {
3846 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
3847 if (tmp != 0)
3848 ret = -1;
3849 }
3850 if (node->children != NULL) {
3851 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
3852 if (tmp != 0)
3853 ret = -1;
3854 }
3855 return(ret);
3856}
3857
3858/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00003859 * xmlRelaxNGParseDefine:
3860 * @ctxt: a Relax-NG parser context
3861 * @node: the define node
3862 *
3863 * parse the content of a RelaxNG define element node.
3864 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00003865 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00003866 */
3867static int
3868xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3869 xmlChar *name;
3870 int ret = 0, tmp;
3871 xmlRelaxNGDefinePtr def;
3872 const xmlChar *olddefine;
3873
3874 name = xmlGetProp(node, BAD_CAST "name");
3875 if (name == NULL) {
3876 if (ctxt->error != NULL)
3877 ctxt->error(ctxt->userData,
3878 "define has no name\n");
3879 ctxt->nbErrors++;
3880 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00003881 xmlRelaxNGNormExtSpace(name);
3882 if (xmlValidateNCName(name, 0)) {
3883 if (ctxt->error != NULL)
3884 ctxt->error(ctxt->userData,
3885 "define name '%s' is not an NCName\n",
3886 name);
3887 ctxt->nbErrors++;
3888 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003889 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard276be4a2003-01-24 01:03:34 +00003890 if (def == NULL) {
3891 xmlFree(name);
3892 return(-1);
3893 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003894 def->type = XML_RELAXNG_DEF;
Daniel Veillard276be4a2003-01-24 01:03:34 +00003895 def->name = name;
3896 if (node->children == NULL) {
3897 if (ctxt->error != NULL)
3898 ctxt->error(ctxt->userData,
3899 "define has no children\n");
3900 ctxt->nbErrors++;
3901 } else {
3902 olddefine = ctxt->define;
3903 ctxt->define = name;
Daniel Veillard154877e2003-01-30 12:17:05 +00003904 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard276be4a2003-01-24 01:03:34 +00003905 ctxt->define = olddefine;
3906 }
3907 if (ctxt->grammar->defs == NULL)
3908 ctxt->grammar->defs = xmlHashCreate(10);
3909 if (ctxt->grammar->defs == NULL) {
3910 if (ctxt->error != NULL)
3911 ctxt->error(ctxt->userData,
3912 "Could not create definition hash\n");
3913 ctxt->nbErrors++;
3914 ret = -1;
Daniel Veillard276be4a2003-01-24 01:03:34 +00003915 } else {
3916 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
3917 if (tmp < 0) {
Daniel Veillard154877e2003-01-30 12:17:05 +00003918 xmlRelaxNGDefinePtr prev;
3919
3920 prev = xmlHashLookup(ctxt->grammar->defs, name);
3921 if (prev == NULL) {
3922 if (ctxt->error != NULL)
3923 ctxt->error(ctxt->userData,
3924 "Internal error on define aggregation of %s\n",
3925 name);
3926 ctxt->nbErrors++;
3927 ret = -1;
Daniel Veillard154877e2003-01-30 12:17:05 +00003928 } else {
3929 while (prev->nextHash != NULL)
3930 prev = prev->nextHash;
3931 prev->nextHash = def;
3932 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00003933 }
3934 }
3935 }
3936 return(ret);
3937}
3938
3939/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00003940 * xmlRelaxNGProcessExternalRef:
3941 * @ctxt: the parser context
3942 * @node: the externlRef node
3943 *
3944 * Process and compile an externlRef node
3945 *
3946 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
3947 */
3948static xmlRelaxNGDefinePtr
3949xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3950 xmlRelaxNGDocumentPtr docu;
3951 xmlNodePtr root, tmp;
3952 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00003953 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00003954 xmlRelaxNGDefinePtr def;
3955
3956 docu = node->_private;
3957 if (docu != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003958 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardfebcca42003-02-16 15:44:18 +00003959 if (def == NULL)
3960 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003961 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00003962
3963 if (docu->content == NULL) {
3964 /*
3965 * Then do the parsing for good
3966 */
3967 root = xmlDocGetRootElement(docu->doc);
3968 if (root == NULL) {
3969 if (ctxt->error != NULL)
3970 ctxt->error(ctxt->userData,
3971 "xmlRelaxNGParse: %s is empty\n",
3972 ctxt->URL);
3973 ctxt->nbErrors++;
3974 return (NULL);
3975 }
3976 /*
3977 * ns transmission rules
3978 */
3979 ns = xmlGetProp(root, BAD_CAST "ns");
3980 if (ns == NULL) {
3981 tmp = node;
3982 while ((tmp != NULL) &&
3983 (tmp->type == XML_ELEMENT_NODE)) {
3984 ns = xmlGetProp(tmp, BAD_CAST "ns");
3985 if (ns != NULL) {
3986 break;
3987 }
3988 tmp = tmp->parent;
3989 }
3990 if (ns != NULL) {
3991 xmlSetProp(root, BAD_CAST "ns", ns);
3992 newNs = 1;
3993 xmlFree(ns);
3994 }
3995 } else {
3996 xmlFree(ns);
3997 }
3998
3999 /*
4000 * Parsing to get a precompiled schemas.
4001 */
Daniel Veillard77648bb2003-02-20 15:03:22 +00004002 oldflags = ctxt->flags;
4003 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004004 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard77648bb2003-02-20 15:03:22 +00004005 ctxt->flags = oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004006 if ((docu->schema != NULL) &&
4007 (docu->schema->topgrammar != NULL)) {
4008 docu->content = docu->schema->topgrammar->start;
4009 }
4010
4011 /*
4012 * the externalRef may be reused in a different ns context
4013 */
4014 if (newNs == 1) {
4015 xmlUnsetProp(root, BAD_CAST "ns");
4016 }
4017 }
4018 def->content = docu->content;
4019 } else {
4020 def = NULL;
4021 }
4022 return(def);
4023}
4024
4025/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004026 * xmlRelaxNGParsePattern:
4027 * @ctxt: a Relax-NG parser context
4028 * @node: the pattern node.
4029 *
4030 * parse the content of a RelaxNG pattern node.
4031 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004032 * Returns the definition pointer or NULL in case of error or if no
4033 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004034 */
4035static xmlRelaxNGDefinePtr
4036xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4037 xmlRelaxNGDefinePtr def = NULL;
4038
Daniel Veillardd2298792003-02-14 16:54:11 +00004039 if (node == NULL) {
4040 return(NULL);
4041 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004042 if (IS_RELAXNG(node, "element")) {
4043 def = xmlRelaxNGParseElement(ctxt, node);
4044 } else if (IS_RELAXNG(node, "attribute")) {
4045 def = xmlRelaxNGParseAttribute(ctxt, node);
4046 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004047 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004048 if (def == NULL)
4049 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004050 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00004051 if (node->children != NULL) {
4052 if (ctxt->error != NULL)
4053 ctxt->error(ctxt->userData, "empty: had a child node\n");
4054 ctxt->nbErrors++;
4055 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004056 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004057 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004058 if (def == NULL)
4059 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004060 def->type = XML_RELAXNG_TEXT;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004061 if (node->children != NULL) {
4062 if (ctxt->error != NULL)
4063 ctxt->error(ctxt->userData, "text: had a child node\n");
4064 ctxt->nbErrors++;
4065 }
4066 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004067 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004068 if (def == NULL)
4069 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004070 def->type = XML_RELAXNG_ZEROORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004071 if (node->children == NULL) {
4072 if (ctxt->error != NULL)
4073 ctxt->error(ctxt->userData,
4074 "Element %s is empty\n", node->name);
4075 ctxt->nbErrors++;
4076 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004077 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
Daniel Veillardd2298792003-02-14 16:54:11 +00004078 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004079 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004080 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004081 if (def == NULL)
4082 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004083 def->type = XML_RELAXNG_ONEORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004084 if (node->children == NULL) {
4085 if (ctxt->error != NULL)
4086 ctxt->error(ctxt->userData,
4087 "Element %s is empty\n", node->name);
4088 ctxt->nbErrors++;
4089 } else {
4090 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4091 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004092 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004093 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004094 if (def == NULL)
4095 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004096 def->type = XML_RELAXNG_OPTIONAL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004097 if (node->children == NULL) {
4098 if (ctxt->error != NULL)
4099 ctxt->error(ctxt->userData,
4100 "Element %s is empty\n", node->name);
4101 ctxt->nbErrors++;
4102 } else {
4103 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4104 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004105 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004106 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004107 if (def == NULL)
4108 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004109 def->type = XML_RELAXNG_CHOICE;
4110 if (node->children == NULL) {
4111 if (ctxt->error != NULL)
4112 ctxt->error(ctxt->userData,
4113 "Element %s is empty\n", node->name);
4114 ctxt->nbErrors++;
4115 } else {
4116 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4117 }
4118 } else if (IS_RELAXNG(node, "group")) {
4119 def = xmlRelaxNGNewDefine(ctxt, node);
4120 if (def == NULL)
4121 return(NULL);
4122 def->type = XML_RELAXNG_GROUP;
4123 if (node->children == NULL) {
4124 if (ctxt->error != NULL)
4125 ctxt->error(ctxt->userData,
4126 "Element %s is empty\n", node->name);
4127 ctxt->nbErrors++;
4128 } else {
4129 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4130 }
4131 } else if (IS_RELAXNG(node, "ref")) {
4132 def = xmlRelaxNGNewDefine(ctxt, node);
4133 if (def == NULL)
4134 return(NULL);
4135 def->type = XML_RELAXNG_REF;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004136 def->name = xmlGetProp(node, BAD_CAST "name");
4137 if (def->name == NULL) {
4138 if (ctxt->error != NULL)
4139 ctxt->error(ctxt->userData,
4140 "ref has no name\n");
4141 ctxt->nbErrors++;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004142 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004143 xmlRelaxNGNormExtSpace(def->name);
4144 if (xmlValidateNCName(def->name, 0)) {
4145 if (ctxt->error != NULL)
4146 ctxt->error(ctxt->userData,
4147 "ref name '%s' is not an NCName\n",
4148 def->name);
4149 ctxt->nbErrors++;
4150 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004151 }
4152 if (node->children != NULL) {
4153 if (ctxt->error != NULL)
4154 ctxt->error(ctxt->userData,
4155 "ref is not empty\n");
4156 ctxt->nbErrors++;
4157 }
4158 if (ctxt->grammar->refs == NULL)
4159 ctxt->grammar->refs = xmlHashCreate(10);
4160 if (ctxt->grammar->refs == NULL) {
4161 if (ctxt->error != NULL)
4162 ctxt->error(ctxt->userData,
4163 "Could not create references hash\n");
4164 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004165 def = NULL;
4166 } else {
4167 int tmp;
4168
4169 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4170 if (tmp < 0) {
4171 xmlRelaxNGDefinePtr prev;
4172
4173 prev = (xmlRelaxNGDefinePtr)
4174 xmlHashLookup(ctxt->grammar->refs, def->name);
4175 if (prev == NULL) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004176 if (def->name != NULL) {
4177 if (ctxt->error != NULL)
4178 ctxt->error(ctxt->userData,
4179 "Error refs definitions '%s'\n",
4180 def->name);
4181 } else {
4182 if (ctxt->error != NULL)
4183 ctxt->error(ctxt->userData,
4184 "Error refs definitions\n");
4185 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004186 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004187 def = NULL;
4188 } else {
4189 def->nextHash = prev->nextHash;
4190 prev->nextHash = def;
4191 }
4192 }
4193 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004194 } else if (IS_RELAXNG(node, "data")) {
4195 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004196 } else if (IS_RELAXNG(node, "value")) {
4197 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004198 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004199 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004200 if (def == NULL)
4201 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004202 def->type = XML_RELAXNG_LIST;
Daniel Veillardd2298792003-02-14 16:54:11 +00004203 if (node->children == NULL) {
4204 if (ctxt->error != NULL)
4205 ctxt->error(ctxt->userData,
4206 "Element %s is empty\n", node->name);
4207 ctxt->nbErrors++;
4208 } else {
4209 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4210 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004211 } else if (IS_RELAXNG(node, "interleave")) {
4212 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004213 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004214 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004215 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004216 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004217 if (def == NULL)
4218 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004219 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004220 if (node->children != NULL) {
4221 if (ctxt->error != NULL)
4222 ctxt->error(ctxt->userData,
4223 "xmlRelaxNGParse: notAllowed element is not empty\n");
4224 ctxt->nbErrors++;
4225 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004226 } else if (IS_RELAXNG(node, "grammar")) {
4227 xmlRelaxNGGrammarPtr grammar, old;
4228 xmlRelaxNGGrammarPtr oldparent;
4229
Daniel Veillardc482e262003-02-26 14:48:48 +00004230#ifdef DEBUG_GRAMMAR
4231 xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n");
4232#endif
4233
Daniel Veillard419a7682003-02-03 23:22:49 +00004234 oldparent = ctxt->parentgrammar;
4235 old = ctxt->grammar;
4236 ctxt->parentgrammar = old;
4237 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4238 if (old != NULL) {
4239 ctxt->grammar = old;
4240 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004241#if 0
Daniel Veillard419a7682003-02-03 23:22:49 +00004242 if (grammar != NULL) {
4243 grammar->next = old->next;
4244 old->next = grammar;
4245 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004246#endif
Daniel Veillard419a7682003-02-03 23:22:49 +00004247 }
4248 if (grammar != NULL)
4249 def = grammar->start;
4250 else
4251 def = NULL;
4252 } else if (IS_RELAXNG(node, "parentRef")) {
4253 if (ctxt->parentgrammar == NULL) {
4254 if (ctxt->error != NULL)
4255 ctxt->error(ctxt->userData,
4256 "Use of parentRef without a parent grammar\n");
4257 ctxt->nbErrors++;
4258 return(NULL);
4259 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004260 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard419a7682003-02-03 23:22:49 +00004261 if (def == NULL)
4262 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004263 def->type = XML_RELAXNG_PARENTREF;
Daniel Veillard419a7682003-02-03 23:22:49 +00004264 def->name = xmlGetProp(node, BAD_CAST "name");
4265 if (def->name == NULL) {
4266 if (ctxt->error != NULL)
4267 ctxt->error(ctxt->userData,
4268 "parentRef has no name\n");
4269 ctxt->nbErrors++;
Daniel Veillardd2298792003-02-14 16:54:11 +00004270 } else {
4271 xmlRelaxNGNormExtSpace(def->name);
4272 if (xmlValidateNCName(def->name, 0)) {
4273 if (ctxt->error != NULL)
4274 ctxt->error(ctxt->userData,
4275 "parentRef name '%s' is not an NCName\n",
4276 def->name);
4277 ctxt->nbErrors++;
4278 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004279 }
4280 if (node->children != NULL) {
4281 if (ctxt->error != NULL)
4282 ctxt->error(ctxt->userData,
4283 "parentRef is not empty\n");
4284 ctxt->nbErrors++;
4285 }
4286 if (ctxt->parentgrammar->refs == NULL)
4287 ctxt->parentgrammar->refs = xmlHashCreate(10);
4288 if (ctxt->parentgrammar->refs == NULL) {
4289 if (ctxt->error != NULL)
4290 ctxt->error(ctxt->userData,
4291 "Could not create references hash\n");
4292 ctxt->nbErrors++;
4293 def = NULL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004294 } else if (def->name != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +00004295 int tmp;
4296
4297 tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4298 if (tmp < 0) {
4299 xmlRelaxNGDefinePtr prev;
4300
4301 prev = (xmlRelaxNGDefinePtr)
4302 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4303 if (prev == NULL) {
4304 if (ctxt->error != NULL)
4305 ctxt->error(ctxt->userData,
4306 "Internal error parentRef definitions '%s'\n",
4307 def->name);
4308 ctxt->nbErrors++;
4309 def = NULL;
4310 } else {
4311 def->nextHash = prev->nextHash;
4312 prev->nextHash = def;
4313 }
4314 }
4315 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004316 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004317 if (node->children == NULL) {
4318 if (ctxt->error != NULL)
4319 ctxt->error(ctxt->userData,
4320 "Mixed is empty\n");
4321 ctxt->nbErrors++;
4322 def = NULL;
4323 } else {
4324 def = xmlRelaxNGParseInterleave(ctxt, node);
4325 if (def != NULL) {
4326 xmlRelaxNGDefinePtr tmp;
4327
4328 if ((def->content != NULL) && (def->content->next != NULL)) {
4329 tmp = xmlRelaxNGNewDefine(ctxt, node);
4330 if (tmp != NULL) {
4331 tmp->type = XML_RELAXNG_GROUP;
4332 tmp->content = def->content;
4333 def->content = tmp;
4334 }
4335 }
4336
4337 tmp = xmlRelaxNGNewDefine(ctxt, node);
4338 if (tmp == NULL)
4339 return(def);
4340 tmp->type = XML_RELAXNG_TEXT;
4341 tmp->next = def->content;
4342 def->content = tmp;
4343 }
4344 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004345 } else {
4346 if (ctxt->error != NULL)
4347 ctxt->error(ctxt->userData,
4348 "Unexpected node %s is not a pattern\n",
4349 node->name);
4350 ctxt->nbErrors++;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004351 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004352 }
4353 return(def);
4354}
4355
4356/**
4357 * xmlRelaxNGParseAttribute:
4358 * @ctxt: a Relax-NG parser context
4359 * @node: the element node
4360 *
4361 * parse the content of a RelaxNG attribute node.
4362 *
4363 * Returns the definition pointer or NULL in case of error.
4364 */
4365static xmlRelaxNGDefinePtr
4366xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillardd2298792003-02-14 16:54:11 +00004367 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004368 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004369 int old_flags;
4370
Daniel Veillardfd573f12003-03-16 17:52:32 +00004371 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004372 if (ret == NULL)
4373 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004374 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004375 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004376 child = node->children;
4377 if (child == NULL) {
4378 if (ctxt->error != NULL)
4379 ctxt->error(ctxt->userData,
4380 "xmlRelaxNGParseattribute: attribute has no children\n");
4381 ctxt->nbErrors++;
4382 return(ret);
4383 }
4384 old_flags = ctxt->flags;
4385 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004386 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4387 if (cur != NULL)
4388 child = child->next;
4389
Daniel Veillardd2298792003-02-14 16:54:11 +00004390 if (child != NULL) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00004391 cur = xmlRelaxNGParsePattern(ctxt, child);
4392 if (cur != NULL) {
4393 switch (cur->type) {
4394 case XML_RELAXNG_EMPTY:
4395 case XML_RELAXNG_NOT_ALLOWED:
4396 case XML_RELAXNG_TEXT:
4397 case XML_RELAXNG_ELEMENT:
4398 case XML_RELAXNG_DATATYPE:
4399 case XML_RELAXNG_VALUE:
4400 case XML_RELAXNG_LIST:
4401 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00004402 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004403 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004404 case XML_RELAXNG_DEF:
4405 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004406 case XML_RELAXNG_ZEROORMORE:
4407 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004408 case XML_RELAXNG_CHOICE:
4409 case XML_RELAXNG_GROUP:
4410 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard77648bb2003-02-20 15:03:22 +00004411 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardd2298792003-02-14 16:54:11 +00004412 ret->content = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004413 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004414 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004415 case XML_RELAXNG_START:
Daniel Veillard8fe98712003-02-19 00:19:14 +00004416 case XML_RELAXNG_PARAM:
Daniel Veillard144fae12003-02-03 13:17:57 +00004417 case XML_RELAXNG_EXCEPT:
Daniel Veillardd2298792003-02-14 16:54:11 +00004418 if (ctxt->error != NULL)
4419 ctxt->error(ctxt->userData,
4420 "attribute has invalid content\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004421 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004422 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004423 case XML_RELAXNG_NOOP:
4424 TODO
4425 if (ctxt->error != NULL)
4426 ctxt->error(ctxt->userData,
4427 "Internal error, noop found\n");
4428 ctxt->nbErrors++;
4429 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004430 }
4431 }
4432 child = child->next;
4433 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004434 if (child != NULL) {
4435 if (ctxt->error != NULL)
4436 ctxt->error(ctxt->userData, "attribute has multiple children\n");
4437 ctxt->nbErrors++;
4438 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004439 ctxt->flags = old_flags;
4440 return(ret);
4441}
4442
4443/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004444 * xmlRelaxNGParseExceptNameClass:
4445 * @ctxt: a Relax-NG parser context
4446 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00004447 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004448 *
4449 * parse the content of a RelaxNG nameClass node.
4450 *
4451 * Returns the definition pointer or NULL in case of error.
4452 */
4453static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00004454xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
4455 xmlNodePtr node, int attr) {
4456 xmlRelaxNGDefinePtr ret, cur, last = NULL;
4457 xmlNodePtr child;
4458
Daniel Veillardd2298792003-02-14 16:54:11 +00004459 if (!IS_RELAXNG(node, "except")) {
4460 if (ctxt->error != NULL)
4461 ctxt->error(ctxt->userData,
4462 "Expecting an except node\n");
4463 ctxt->nbErrors++;
Daniel Veillard144fae12003-02-03 13:17:57 +00004464 return(NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004465 }
4466 if (node->next != NULL) {
4467 if (ctxt->error != NULL)
4468 ctxt->error(ctxt->userData,
4469 "exceptNameClass allows only a single except node\n");
4470 ctxt->nbErrors++;
4471 }
Daniel Veillard144fae12003-02-03 13:17:57 +00004472 if (node->children == NULL) {
4473 if (ctxt->error != NULL)
4474 ctxt->error(ctxt->userData,
4475 "except has no content\n");
4476 ctxt->nbErrors++;
4477 return(NULL);
4478 }
4479
Daniel Veillardfd573f12003-03-16 17:52:32 +00004480 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00004481 if (ret == NULL)
4482 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004483 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00004484 child = node->children;
4485 while (child != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004486 cur = xmlRelaxNGNewDefine(ctxt, child);
Daniel Veillard144fae12003-02-03 13:17:57 +00004487 if (cur == NULL)
4488 break;
4489 if (attr)
4490 cur->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004491 else
4492 cur->type = XML_RELAXNG_ELEMENT;
Daniel Veillard144fae12003-02-03 13:17:57 +00004493
Daniel Veillard419a7682003-02-03 23:22:49 +00004494 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard144fae12003-02-03 13:17:57 +00004495 if (last == NULL) {
4496 ret->content = cur;
4497 } else {
4498 last->next = cur;
4499 }
4500 last = cur;
4501 }
4502 child = child->next;
4503 }
4504
4505 return(ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004506}
4507
4508/**
4509 * xmlRelaxNGParseNameClass:
4510 * @ctxt: a Relax-NG parser context
4511 * @node: the nameClass node
4512 * @def: the current definition
4513 *
4514 * parse the content of a RelaxNG nameClass node.
4515 *
4516 * Returns the definition pointer or NULL in case of error.
4517 */
4518static xmlRelaxNGDefinePtr
4519xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
4520 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004521 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004522 xmlChar *val;
4523
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004524 ret = def;
4525 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
4526 (IS_RELAXNG(node, "nsName"))) {
4527 if ((def->type != XML_RELAXNG_ELEMENT) &&
4528 (def->type != XML_RELAXNG_ATTRIBUTE)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004529 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004530 if (ret == NULL)
4531 return(NULL);
4532 ret->parent = def;
4533 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
4534 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004535 else
4536 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004537 }
4538 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004539 if (IS_RELAXNG(node, "name")) {
4540 val = xmlNodeGetContent(node);
Daniel Veillardd2298792003-02-14 16:54:11 +00004541 xmlRelaxNGNormExtSpace(val);
4542 if (xmlValidateNCName(val, 0)) {
4543 if (ctxt->error != NULL) {
4544 if (node->parent != NULL)
4545 ctxt->error(ctxt->userData,
4546 "Element %s name '%s' is not an NCName\n",
4547 node->parent->name, val);
4548 else
4549 ctxt->error(ctxt->userData,
4550 "name '%s' is not an NCName\n",
4551 val);
4552 }
4553 ctxt->nbErrors++;
4554 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004555 ret->name = val;
4556 val = xmlGetProp(node, BAD_CAST "ns");
4557 ret->ns = val;
Daniel Veillard416589a2003-02-17 17:25:42 +00004558 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4559 (val != NULL) &&
4560 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
4561 ctxt->error(ctxt->userData,
4562 "Attribute with namespace '%s' is not allowed\n",
4563 val);
4564 ctxt->nbErrors++;
4565 }
4566 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4567 (val != NULL) &&
4568 (val[0] == 0) &&
4569 (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
4570 ctxt->error(ctxt->userData,
4571 "Attribute with QName 'xmlns' is not allowed\n",
4572 val);
4573 ctxt->nbErrors++;
4574 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004575 } else if (IS_RELAXNG(node, "anyName")) {
4576 ret->name = NULL;
4577 ret->ns = NULL;
4578 if (node->children != NULL) {
4579 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00004580 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
4581 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004582 }
4583 } else if (IS_RELAXNG(node, "nsName")) {
4584 ret->name = NULL;
4585 ret->ns = xmlGetProp(node, BAD_CAST "ns");
4586 if (ret->ns == NULL) {
4587 if (ctxt->error != NULL)
4588 ctxt->error(ctxt->userData,
4589 "nsName has no ns attribute\n");
4590 ctxt->nbErrors++;
4591 }
Daniel Veillard416589a2003-02-17 17:25:42 +00004592 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4593 (ret->ns != NULL) &&
4594 (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
4595 ctxt->error(ctxt->userData,
4596 "Attribute with namespace '%s' is not allowed\n",
4597 ret->ns);
4598 ctxt->nbErrors++;
4599 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004600 if (node->children != NULL) {
4601 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00004602 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
4603 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004604 }
4605 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004606 xmlNodePtr child;
4607 xmlRelaxNGDefinePtr last = NULL;
4608
4609 ret = xmlRelaxNGNewDefine(ctxt, node);
4610 if (ret == NULL)
4611 return(NULL);
4612 ret->parent = def;
4613 ret->type = XML_RELAXNG_CHOICE;
4614
Daniel Veillardd2298792003-02-14 16:54:11 +00004615 if (node->children == NULL) {
4616 if (ctxt->error != NULL)
4617 ctxt->error(ctxt->userData,
4618 "Element choice is empty\n");
4619 ctxt->nbErrors++;
4620 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004621
4622 child = node->children;
4623 while (child != NULL) {
4624 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
4625 if (tmp != NULL) {
4626 if (last == NULL) {
4627 last = ret->nameClass = tmp;
4628 } else {
4629 last->next = tmp;
4630 last = tmp;
4631 }
4632 }
4633 child = child->next;
4634 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004635 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004636 } else {
4637 if (ctxt->error != NULL)
4638 ctxt->error(ctxt->userData,
Daniel Veillardfd573f12003-03-16 17:52:32 +00004639 "expecting name, anyName, nsName or choice : got %s\n",
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004640 node->name);
4641 ctxt->nbErrors++;
4642 return(NULL);
4643 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004644 if (ret != def) {
4645 if (def->nameClass == NULL) {
4646 def->nameClass = ret;
4647 } else {
4648 tmp = def->nameClass;
4649 while (tmp->next != NULL) {
4650 tmp = tmp->next;
4651 }
4652 tmp->next = ret;
4653 }
4654 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004655 return(ret);
4656}
4657
4658/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004659 * xmlRelaxNGParseElement:
4660 * @ctxt: a Relax-NG parser context
4661 * @node: the element node
4662 *
4663 * parse the content of a RelaxNG element node.
4664 *
4665 * Returns the definition pointer or NULL in case of error.
4666 */
4667static xmlRelaxNGDefinePtr
4668xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4669 xmlRelaxNGDefinePtr ret, cur, last;
4670 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004671 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004672
Daniel Veillardfd573f12003-03-16 17:52:32 +00004673 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004674 if (ret == NULL)
4675 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004676 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004677 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004678 child = node->children;
4679 if (child == NULL) {
4680 if (ctxt->error != NULL)
4681 ctxt->error(ctxt->userData,
4682 "xmlRelaxNGParseElement: element has no children\n");
4683 ctxt->nbErrors++;
4684 return(ret);
4685 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004686 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4687 if (cur != NULL)
4688 child = child->next;
4689
Daniel Veillard6eadf632003-01-23 18:29:16 +00004690 if (child == NULL) {
4691 if (ctxt->error != NULL)
4692 ctxt->error(ctxt->userData,
4693 "xmlRelaxNGParseElement: element has no content\n");
4694 ctxt->nbErrors++;
4695 return(ret);
4696 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004697 olddefine = ctxt->define;
4698 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004699 last = NULL;
4700 while (child != NULL) {
4701 cur = xmlRelaxNGParsePattern(ctxt, child);
4702 if (cur != NULL) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004703 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004704 switch (cur->type) {
4705 case XML_RELAXNG_EMPTY:
4706 case XML_RELAXNG_NOT_ALLOWED:
4707 case XML_RELAXNG_TEXT:
4708 case XML_RELAXNG_ELEMENT:
4709 case XML_RELAXNG_DATATYPE:
4710 case XML_RELAXNG_VALUE:
4711 case XML_RELAXNG_LIST:
4712 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00004713 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004714 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004715 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004716 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004717 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004718 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004719 case XML_RELAXNG_CHOICE:
4720 case XML_RELAXNG_GROUP:
4721 case XML_RELAXNG_INTERLEAVE:
4722 if (last == NULL) {
4723 ret->content = last = cur;
4724 } else {
4725 if ((last->type == XML_RELAXNG_ELEMENT) &&
4726 (ret->content == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004727 ret->content = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004728 if (ret->content != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004729 ret->content->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004730 ret->content->content = last;
4731 } else {
4732 ret->content = last;
4733 }
4734 }
4735 last->next = cur;
4736 last = cur;
4737 }
4738 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004739 case XML_RELAXNG_ATTRIBUTE:
4740 cur->next = ret->attrs;
4741 ret->attrs = cur;
4742 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004743 case XML_RELAXNG_START:
Daniel Veillard8fe98712003-02-19 00:19:14 +00004744 case XML_RELAXNG_PARAM:
Daniel Veillard144fae12003-02-03 13:17:57 +00004745 case XML_RELAXNG_EXCEPT:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004746 TODO
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004747 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004748 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004749 case XML_RELAXNG_NOOP:
4750 TODO
4751 if (ctxt->error != NULL)
4752 ctxt->error(ctxt->userData,
4753 "Internal error, noop found\n");
4754 ctxt->nbErrors++;
4755 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004756 }
4757 }
4758 child = child->next;
4759 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004760 ctxt->define = olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004761 return(ret);
4762}
4763
4764/**
4765 * xmlRelaxNGParsePatterns:
4766 * @ctxt: a Relax-NG parser context
4767 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00004768 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00004769 *
4770 * parse the content of a RelaxNG start node.
4771 *
4772 * Returns the definition pointer or NULL in case of error.
4773 */
4774static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00004775xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
4776 int group) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004777 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004778
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004779 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004780 while (nodes != NULL) {
4781 if (IS_RELAXNG(nodes, "element")) {
4782 cur = xmlRelaxNGParseElement(ctxt, nodes);
4783 if (def == NULL) {
4784 def = last = cur;
4785 } else {
Daniel Veillard154877e2003-01-30 12:17:05 +00004786 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
4787 (def == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004788 def = xmlRelaxNGNewDefine(ctxt, nodes);
4789 def->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004790 def->content = last;
4791 }
4792 last->next = cur;
4793 last = cur;
4794 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004795 cur->parent = parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004796 } else {
4797 cur = xmlRelaxNGParsePattern(ctxt, nodes);
Daniel Veillard419a7682003-02-03 23:22:49 +00004798 if (cur != NULL) {
4799 if (def == NULL) {
4800 def = last = cur;
4801 } else {
4802 last->next = cur;
4803 last = cur;
4804 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004805 }
4806 }
4807 nodes = nodes->next;
4808 }
4809 return(def);
4810}
4811
4812/**
4813 * xmlRelaxNGParseStart:
4814 * @ctxt: a Relax-NG parser context
4815 * @nodes: start children nodes
4816 *
4817 * parse the content of a RelaxNG start node.
4818 *
4819 * Returns 0 in case of success, -1 in case of error
4820 */
4821static int
4822xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
4823 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00004824 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004825
Daniel Veillardd2298792003-02-14 16:54:11 +00004826 if (nodes == NULL) {
4827 if (ctxt->error != NULL)
4828 ctxt->error(ctxt->userData,
4829 "start has no children\n");
4830 ctxt->nbErrors++;
4831 return(-1);
4832 }
4833 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004834 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00004835 if (def == NULL)
4836 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004837 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00004838 if (nodes->children != NULL) {
4839 if (ctxt->error != NULL)
4840 ctxt->error(ctxt->userData, "element empty is not empty\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004841 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004842 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004843 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004844 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00004845 if (def == NULL)
4846 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004847 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardd2298792003-02-14 16:54:11 +00004848 if (nodes->children != NULL) {
4849 if (ctxt->error != NULL)
4850 ctxt->error(ctxt->userData,
4851 "element notAllowed is not empty\n");
4852 ctxt->nbErrors++;
4853 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004854 } else {
4855 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00004856 }
4857 if (ctxt->grammar->start != NULL) {
4858 last = ctxt->grammar->start;
4859 while (last->next != NULL)
4860 last = last->next;
4861 last->next = def;
4862 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004863 ctxt->grammar->start = def;
4864 }
4865 nodes = nodes->next;
4866 if (nodes != NULL) {
4867 if (ctxt->error != NULL)
4868 ctxt->error(ctxt->userData,
4869 "start more than one children\n");
4870 ctxt->nbErrors++;
4871 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004872 }
4873 return(ret);
4874}
4875
4876/**
4877 * xmlRelaxNGParseGrammarContent:
4878 * @ctxt: a Relax-NG parser context
4879 * @nodes: grammar children nodes
4880 *
4881 * parse the content of a RelaxNG grammar node.
4882 *
4883 * Returns 0 in case of success, -1 in case of error
4884 */
4885static int
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004886xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00004887{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004888 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004889
4890 if (nodes == NULL) {
4891 if (ctxt->error != NULL)
4892 ctxt->error(ctxt->userData,
4893 "grammar has no children\n");
4894 ctxt->nbErrors++;
4895 return(-1);
4896 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004897 while (nodes != NULL) {
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004898 if (IS_RELAXNG(nodes, "start")) {
4899 if (nodes->children == NULL) {
4900 if (ctxt->error != NULL)
4901 ctxt->error(ctxt->userData,
Daniel Veillardd2298792003-02-14 16:54:11 +00004902 "start has no children\n");
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004903 ctxt->nbErrors++;
4904 } else {
4905 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
4906 if (tmp != 0)
4907 ret = -1;
4908 }
4909 } else if (IS_RELAXNG(nodes, "define")) {
4910 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
4911 if (tmp != 0)
4912 ret = -1;
4913 } else if (IS_RELAXNG(nodes, "include")) {
4914 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
4915 if (tmp != 0)
4916 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004917 } else {
4918 if (ctxt->error != NULL)
4919 ctxt->error(ctxt->userData,
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004920 "grammar has unexpected child %s\n", nodes->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004921 ctxt->nbErrors++;
4922 ret = -1;
4923 }
4924 nodes = nodes->next;
4925 }
4926 return (ret);
4927}
4928
4929/**
4930 * xmlRelaxNGCheckReference:
4931 * @ref: the ref
4932 * @ctxt: a Relax-NG parser context
4933 * @name: the name associated to the defines
4934 *
4935 * Applies the 4.17. combine attribute rule for all the define
4936 * element of a given grammar using the same name.
4937 */
4938static void
4939xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
4940 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
4941 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004942 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004943
4944 grammar = ctxt->grammar;
4945 if (grammar == NULL) {
4946 if (ctxt->error != NULL)
4947 ctxt->error(ctxt->userData,
4948 "Internal error: no grammar in CheckReference %s\n",
4949 name);
4950 ctxt->nbErrors++;
4951 return;
4952 }
4953 if (ref->content != NULL) {
4954 if (ctxt->error != NULL)
4955 ctxt->error(ctxt->userData,
4956 "Internal error: reference has content in CheckReference %s\n",
4957 name);
4958 ctxt->nbErrors++;
4959 return;
4960 }
4961 if (grammar->defs != NULL) {
4962 def = xmlHashLookup(grammar->defs, name);
4963 if (def != NULL) {
Daniel Veillard276be4a2003-01-24 01:03:34 +00004964 cur = ref;
4965 while (cur != NULL) {
4966 cur->content = def;
4967 cur = cur->nextHash;
4968 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004969 } else {
Daniel Veillardd4310742003-02-18 21:12:46 +00004970 if (ctxt->error != NULL)
4971 ctxt->error(ctxt->userData,
4972 "Reference %s has no matching definition\n",
4973 name);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004974 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004975 }
Daniel Veillardd4310742003-02-18 21:12:46 +00004976 } else {
4977 if (ctxt->error != NULL)
4978 ctxt->error(ctxt->userData,
4979 "Reference %s has no matching definition\n",
4980 name);
4981 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004982 }
4983 /*
4984 * TODO: make a closure and verify there is no loop !
4985 */
4986}
4987
4988/**
4989 * xmlRelaxNGCheckCombine:
4990 * @define: the define(s) list
4991 * @ctxt: a Relax-NG parser context
4992 * @name: the name associated to the defines
4993 *
4994 * Applies the 4.17. combine attribute rule for all the define
4995 * element of a given grammar using the same name.
4996 */
4997static void
4998xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
4999 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5000 xmlChar *combine;
5001 int choiceOrInterleave = -1;
5002 int missing = 0;
5003 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5004
5005 if (define->nextHash == NULL)
5006 return;
5007 cur = define;
5008 while (cur != NULL) {
5009 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5010 if (combine != NULL) {
5011 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5012 if (choiceOrInterleave == -1)
5013 choiceOrInterleave = 1;
5014 else if (choiceOrInterleave == 0) {
5015 if (ctxt->error != NULL)
5016 ctxt->error(ctxt->userData,
5017 "Defines for %s use both 'choice' and 'interleave'\n",
5018 name);
5019 ctxt->nbErrors++;
5020 }
Daniel Veillard154877e2003-01-30 12:17:05 +00005021 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005022 if (choiceOrInterleave == -1)
5023 choiceOrInterleave = 0;
5024 else if (choiceOrInterleave == 1) {
5025 if (ctxt->error != NULL)
5026 ctxt->error(ctxt->userData,
5027 "Defines for %s use both 'choice' and 'interleave'\n",
5028 name);
5029 ctxt->nbErrors++;
5030 }
5031 } else {
5032 if (ctxt->error != NULL)
5033 ctxt->error(ctxt->userData,
5034 "Defines for %s use unknown combine value '%s''\n",
5035 name, combine);
5036 ctxt->nbErrors++;
5037 }
5038 xmlFree(combine);
5039 } else {
5040 if (missing == 0)
5041 missing = 1;
5042 else {
5043 if (ctxt->error != NULL)
5044 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005045 "Some defines for %s needs the combine attribute\n",
Daniel Veillard6eadf632003-01-23 18:29:16 +00005046 name);
5047 ctxt->nbErrors++;
5048 }
5049 }
5050
5051 cur = cur->nextHash;
5052 }
5053#ifdef DEBUG
5054 xmlGenericError(xmlGenericErrorContext,
5055 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5056 name, choiceOrInterleave);
5057#endif
5058 if (choiceOrInterleave == -1)
5059 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005060 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005061 if (cur == NULL)
5062 return;
5063 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005064 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005065 else
5066 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005067 tmp = define;
5068 last = NULL;
5069 while (tmp != NULL) {
5070 if (tmp->content != NULL) {
5071 if (tmp->content->next != NULL) {
5072 /*
5073 * we need first to create a wrapper.
5074 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00005075 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005076 if (tmp2 == NULL)
5077 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005078 tmp2->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005079 tmp2->content = tmp->content;
5080 } else {
5081 tmp2 = tmp->content;
5082 }
5083 if (last == NULL) {
5084 cur->content = tmp2;
5085 } else {
5086 last->next = tmp2;
5087 }
5088 last = tmp2;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005089 }
Daniel Veillard952379b2003-03-17 15:37:12 +00005090 tmp->content = cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005091 tmp = tmp->nextHash;
5092 }
5093 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005094 if (choiceOrInterleave == 0) {
5095 if (ctxt->interleaves == NULL)
5096 ctxt->interleaves = xmlHashCreate(10);
5097 if (ctxt->interleaves == NULL) {
5098 if (ctxt->error != NULL)
5099 ctxt->error(ctxt->userData,
5100 "Failed to create interleaves hash table\n");
5101 ctxt->nbErrors++;
5102 } else {
5103 char tmpname[32];
5104
5105 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5106 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5107 if (ctxt->error != NULL)
5108 ctxt->error(ctxt->userData,
5109 "Failed to add %s to hash table\n", tmpname);
5110 ctxt->nbErrors++;
5111 }
5112 }
5113 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005114}
5115
5116/**
5117 * xmlRelaxNGCombineStart:
5118 * @ctxt: a Relax-NG parser context
5119 * @grammar: the grammar
5120 *
5121 * Applies the 4.17. combine rule for all the start
5122 * element of a given grammar.
5123 */
5124static void
5125xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5126 xmlRelaxNGGrammarPtr grammar) {
5127 xmlRelaxNGDefinePtr starts;
5128 xmlChar *combine;
5129 int choiceOrInterleave = -1;
5130 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005131 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005132
Daniel Veillard2df2de22003-02-17 23:34:33 +00005133 starts = grammar->start;
5134 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard6eadf632003-01-23 18:29:16 +00005135 return;
5136 cur = starts;
5137 while (cur != NULL) {
Daniel Veillard2df2de22003-02-17 23:34:33 +00005138 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5139 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5140 combine = NULL;
5141 if (ctxt->error != NULL)
5142 ctxt->error(ctxt->userData,
5143 "Internal error: start element not found\n");
5144 ctxt->nbErrors++;
5145 } else {
5146 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5147 }
5148
Daniel Veillard6eadf632003-01-23 18:29:16 +00005149 if (combine != NULL) {
5150 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5151 if (choiceOrInterleave == -1)
5152 choiceOrInterleave = 1;
5153 else if (choiceOrInterleave == 0) {
5154 if (ctxt->error != NULL)
5155 ctxt->error(ctxt->userData,
5156 "<start> use both 'choice' and 'interleave'\n");
5157 ctxt->nbErrors++;
5158 }
Daniel Veillard2df2de22003-02-17 23:34:33 +00005159 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005160 if (choiceOrInterleave == -1)
5161 choiceOrInterleave = 0;
5162 else if (choiceOrInterleave == 1) {
5163 if (ctxt->error != NULL)
5164 ctxt->error(ctxt->userData,
5165 "<start> use both 'choice' and 'interleave'\n");
5166 ctxt->nbErrors++;
5167 }
5168 } else {
5169 if (ctxt->error != NULL)
5170 ctxt->error(ctxt->userData,
5171 "<start> uses unknown combine value '%s''\n", combine);
5172 ctxt->nbErrors++;
5173 }
5174 xmlFree(combine);
5175 } else {
5176 if (missing == 0)
5177 missing = 1;
5178 else {
5179 if (ctxt->error != NULL)
5180 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005181 "Some <start> element miss the combine attribute\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00005182 ctxt->nbErrors++;
5183 }
5184 }
5185
Daniel Veillard2df2de22003-02-17 23:34:33 +00005186 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005187 }
5188#ifdef DEBUG
5189 xmlGenericError(xmlGenericErrorContext,
5190 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5191 choiceOrInterleave);
5192#endif
5193 if (choiceOrInterleave == -1)
5194 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005195 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005196 if (cur == NULL)
5197 return;
5198 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005199 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005200 else
5201 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005202 cur->content = grammar->start;
5203 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005204 if (choiceOrInterleave == 0) {
5205 if (ctxt->interleaves == NULL)
5206 ctxt->interleaves = xmlHashCreate(10);
5207 if (ctxt->interleaves == NULL) {
5208 if (ctxt->error != NULL)
5209 ctxt->error(ctxt->userData,
5210 "Failed to create interleaves hash table\n");
5211 ctxt->nbErrors++;
5212 } else {
5213 char tmpname[32];
5214
5215 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5216 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5217 if (ctxt->error != NULL)
5218 ctxt->error(ctxt->userData,
5219 "Failed to add %s to hash table\n", tmpname);
5220 ctxt->nbErrors++;
5221 }
5222 }
5223 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005224}
5225
5226/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005227 * xmlRelaxNGCheckCycles:
5228 * @ctxt: a Relax-NG parser context
5229 * @nodes: grammar children nodes
5230 * @depth: the counter
5231 *
5232 * Check for cycles.
5233 *
5234 * Returns 0 if check passed, and -1 in case of error
5235 */
5236static int
5237xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5238 xmlRelaxNGDefinePtr cur, int depth) {
5239 int ret = 0;
5240
5241 while ((ret == 0) && (cur != NULL)) {
5242 if ((cur->type == XML_RELAXNG_REF) ||
5243 (cur->type == XML_RELAXNG_PARENTREF)) {
5244 if (cur->depth == -1) {
5245 cur->depth = depth;
5246 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5247 cur->depth = -2;
5248 } else if (depth == cur->depth) {
5249 if (ctxt->error != NULL)
5250 ctxt->error(ctxt->userData,
5251 "Detected a cycle in %s references\n", cur->name);
5252 ctxt->nbErrors++;
5253 return(-1);
5254 }
5255 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5256 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5257 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005258 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
Daniel Veillardd4310742003-02-18 21:12:46 +00005259 }
5260 cur = cur->next;
5261 }
5262 return(ret);
5263}
5264
5265/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005266 * xmlRelaxNGTryUnlink:
5267 * @ctxt: a Relax-NG parser context
5268 * @cur: the definition to unlink
5269 * @parent: the parent definition
5270 * @prev: the previous sibling definition
5271 *
5272 * Try to unlink a definition. If not possble make it a NOOP
5273 *
5274 * Returns the new prev definition
5275 */
5276static xmlRelaxNGDefinePtr
5277xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5278 xmlRelaxNGDefinePtr cur,
5279 xmlRelaxNGDefinePtr parent,
5280 xmlRelaxNGDefinePtr prev) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005281 if (prev != NULL) {
5282 prev->next = cur->next;
5283 } else {
5284 if (parent != NULL) {
5285 if (parent->content == cur)
5286 parent->content = cur->next;
5287 else if (parent->attrs == cur)
5288 parent->attrs = cur->next;
5289 else if (parent->nameClass == cur)
5290 parent->nameClass = cur->next;
5291 } else {
5292 cur->type = XML_RELAXNG_NOOP;
5293 prev = cur;
5294 }
5295 }
5296 return(prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005297}
5298
5299/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005300 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005301 * @ctxt: a Relax-NG parser context
5302 * @nodes: grammar children nodes
5303 *
5304 * Check for simplification of empty and notAllowed
5305 */
5306static void
5307xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5308 xmlRelaxNGDefinePtr cur,
5309 xmlRelaxNGDefinePtr parent) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005310 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005311
Daniel Veillardfd573f12003-03-16 17:52:32 +00005312 while (cur != NULL) {
5313 if ((cur->type == XML_RELAXNG_REF) ||
5314 (cur->type == XML_RELAXNG_PARENTREF)) {
5315 if (cur->depth != -3) {
5316 cur->depth = -3;
5317 xmlRelaxNGSimplify(ctxt, cur->content, cur);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005318 }
5319 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005320 cur->parent = parent;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005321 if ((parent != NULL) &&
5322 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5323 (parent->type == XML_RELAXNG_LIST) ||
5324 (parent->type == XML_RELAXNG_GROUP) ||
5325 (parent->type == XML_RELAXNG_INTERLEAVE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00005326 (parent->type == XML_RELAXNG_ONEORMORE) ||
5327 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005328 parent->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005329 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005330 }
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005331 if ((parent != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00005332 (parent->type == XML_RELAXNG_CHOICE)) {
5333 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5334 } else
5335 prev = cur;
5336 } else if (cur->type == XML_RELAXNG_EMPTY){
5337 cur->parent = parent;
5338 if ((parent != NULL) &&
5339 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5340 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005341 parent->type = XML_RELAXNG_EMPTY;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005342 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005343 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005344 if ((parent != NULL) &&
5345 ((parent->type == XML_RELAXNG_GROUP) ||
5346 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5347 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5348 } else
5349 prev = cur;
5350 } else {
5351 cur->parent = parent;
5352 if (cur->content != NULL)
5353 xmlRelaxNGSimplify(ctxt, cur->content, cur);
5354 if (cur->attrs != NULL)
5355 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5356 if (cur->nameClass != NULL)
5357 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5358 /*
5359 * This may result in a simplification
5360 */
5361 if ((cur->type == XML_RELAXNG_GROUP) ||
5362 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5363 if (cur->content == NULL)
5364 cur->type = XML_RELAXNG_EMPTY;
5365 else if (cur->content->next == NULL) {
5366 if ((parent == NULL) && (prev == NULL)) {
5367 cur->type = XML_RELAXNG_NOOP;
5368 } else if (prev == NULL) {
5369 parent->content = cur->content;
5370 cur->content->next = cur->next;
5371 cur = cur->content;
5372 } else {
5373 cur->content->next = cur->next;
5374 prev->next = cur->content;
5375 cur = cur->content;
5376 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005377 }
5378 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005379 /*
5380 * the current node may have been transformed back
5381 */
5382 if ((cur->type == XML_RELAXNG_EXCEPT) &&
5383 (cur->content != NULL) &&
5384 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
5385 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5386 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5387 if ((parent != NULL) &&
5388 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5389 (parent->type == XML_RELAXNG_LIST) ||
5390 (parent->type == XML_RELAXNG_GROUP) ||
5391 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5392 (parent->type == XML_RELAXNG_ONEORMORE) ||
5393 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5394 parent->type = XML_RELAXNG_NOT_ALLOWED;
5395 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005396 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005397 if ((parent != NULL) &&
5398 (parent->type == XML_RELAXNG_CHOICE)) {
5399 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5400 } else
5401 prev = cur;
5402 } else if (cur->type == XML_RELAXNG_EMPTY){
5403 if ((parent != NULL) &&
5404 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5405 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5406 parent->type = XML_RELAXNG_EMPTY;
5407 break;
5408 }
5409 if ((parent != NULL) &&
5410 ((parent->type == XML_RELAXNG_GROUP) ||
5411 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5412 (parent->type == XML_RELAXNG_CHOICE))) {
5413 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5414 } else
5415 prev = cur;
5416 } else {
5417 prev = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005418 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005419 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005420 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005421 }
5422}
5423
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005424/**
5425 * xmlRelaxNGGroupContentType:
5426 * @ct1: the first content type
5427 * @ct2: the second content type
5428 *
5429 * Try to group 2 content types
5430 *
5431 * Returns the content type
5432 */
5433static xmlRelaxNGContentType
5434xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
5435 xmlRelaxNGContentType ct2) {
5436 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
5437 (ct2 == XML_RELAXNG_CONTENT_ERROR))
5438 return(XML_RELAXNG_CONTENT_ERROR);
5439 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
5440 return(ct2);
5441 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
5442 return(ct1);
5443 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
5444 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
5445 return(XML_RELAXNG_CONTENT_COMPLEX);
5446 return(XML_RELAXNG_CONTENT_ERROR);
5447}
5448
5449/**
5450 * xmlRelaxNGMaxContentType:
5451 * @ct1: the first content type
5452 * @ct2: the second content type
5453 *
5454 * Compute the max content-type
5455 *
5456 * Returns the content type
5457 */
5458static xmlRelaxNGContentType
5459xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
5460 xmlRelaxNGContentType ct2) {
5461 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
5462 (ct2 == XML_RELAXNG_CONTENT_ERROR))
5463 return(XML_RELAXNG_CONTENT_ERROR);
5464 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
5465 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
5466 return(XML_RELAXNG_CONTENT_SIMPLE);
5467 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
5468 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
5469 return(XML_RELAXNG_CONTENT_COMPLEX);
5470 return(XML_RELAXNG_CONTENT_EMPTY);
5471}
Daniel Veillard77648bb2003-02-20 15:03:22 +00005472
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005473/**
5474 * xmlRelaxNGCheckRules:
5475 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005476 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00005477 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00005478 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005479 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005480 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005481 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005482 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005483 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005484static xmlRelaxNGContentType
Daniel Veillardfd573f12003-03-16 17:52:32 +00005485xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
5486 xmlRelaxNGDefinePtr cur, int flags,
5487 xmlRelaxNGType ptype) {
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005488 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005489 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005490
Daniel Veillardfd573f12003-03-16 17:52:32 +00005491 while (cur != NULL) {
5492 ret = XML_RELAXNG_CONTENT_EMPTY;
5493 if ((cur->type == XML_RELAXNG_REF) ||
5494 (cur->type == XML_RELAXNG_PARENTREF)) {
5495 if (flags & XML_RELAXNG_IN_LIST) {
5496 if (ctxt->error != NULL)
5497 ctxt->error(ctxt->userData,
5498 "Found forbidden pattern list//ref\n");
5499 ctxt->nbErrors++;
5500 }
5501 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5502 if (ctxt->error != NULL)
5503 ctxt->error(ctxt->userData,
5504 "Found forbidden pattern data/except//ref\n");
5505 ctxt->nbErrors++;
5506 }
5507 if (cur->depth > -4) {
5508 cur->depth = -4;
5509 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
5510 flags, cur->type);
5511 cur->depth = ret - 15 ;
5512 } else if (cur->depth == -4) {
5513 ret = XML_RELAXNG_CONTENT_COMPLEX;
5514 } else {
5515 ret = (xmlRelaxNGContentType) cur->depth + 15;
5516 }
5517 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5518 /*
5519 * The 7.3 Attribute derivation rule for groups is plugged there
5520 */
5521 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
5522 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5523 if (ctxt->error != NULL)
5524 ctxt->error(ctxt->userData,
5525 "Found forbidden pattern data/except//element(ref)\n");
5526 ctxt->nbErrors++;
5527 }
5528 if (flags & XML_RELAXNG_IN_LIST) {
5529 if (ctxt->error != NULL)
5530 ctxt->error(ctxt->userData,
5531 "Found forbidden pattern list//element(ref)\n");
5532 ctxt->nbErrors++;
5533 }
5534 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5535 if (ctxt->error != NULL)
5536 ctxt->error(ctxt->userData,
5537 "Found forbidden pattern attribute//element(ref)\n");
5538 ctxt->nbErrors++;
5539 }
5540 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5541 if (ctxt->error != NULL)
5542 ctxt->error(ctxt->userData,
Daniel Veillard463a5472003-02-27 21:30:32 +00005543 "Found forbidden pattern attribute//element(ref)\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005544 ctxt->nbErrors++;
5545 }
5546 /*
5547 * reset since in the simple form elements are only child
5548 * of grammar/define
5549 */
5550 nflags = 0;
5551 ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
5552 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
5553 if (ctxt->error != NULL)
5554 ctxt->error(ctxt->userData,
5555 "Element %s attributes have a content type error\n",
5556 cur->name);
5557 ctxt->nbErrors++;
5558 }
5559 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5560 if (ret == XML_RELAXNG_CONTENT_ERROR) {
5561 if (ctxt->error != NULL)
5562 ctxt->error(ctxt->userData,
5563 "Element %s has a content type error\n",
5564 cur->name);
5565 ctxt->nbErrors++;
5566 } else {
5567 ret = XML_RELAXNG_CONTENT_COMPLEX;
5568 }
5569 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
5570 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5571 if (ctxt->error != NULL)
5572 ctxt->error(ctxt->userData,
5573 "Found forbidden pattern attribute//attribute\n");
5574 ctxt->nbErrors++;
5575 }
5576 if (flags & XML_RELAXNG_IN_LIST) {
5577 if (ctxt->error != NULL)
5578 ctxt->error(ctxt->userData,
5579 "Found forbidden pattern list//attribute\n");
5580 ctxt->nbErrors++;
5581 }
5582 if (flags & XML_RELAXNG_IN_OOMGROUP) {
5583 if (ctxt->error != NULL)
5584 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005585 "Found forbidden pattern oneOrMore//group//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005586 ctxt->nbErrors++;
5587 }
5588 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
5589 if (ctxt->error != NULL)
5590 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005591 "Found forbidden pattern oneOrMore//interleave//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005592 ctxt->nbErrors++;
5593 }
5594 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5595 if (ctxt->error != NULL)
5596 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005597 "Found forbidden pattern data/except//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005598 ctxt->nbErrors++;
5599 }
5600 if (flags & XML_RELAXNG_IN_START) {
5601 if (ctxt->error != NULL)
5602 ctxt->error(ctxt->userData,
5603 "Found forbidden pattern start//attribute\n");
5604 ctxt->nbErrors++;
5605 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005606 if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) {
5607 if (cur->ns == NULL) {
5608 if (ctxt->error != NULL)
5609 ctxt->error(ctxt->userData,
5610 "Found anyName attribute without oneOrMore ancestor\n");
5611 ctxt->nbErrors++;
5612 } else {
5613 if (ctxt->error != NULL)
5614 ctxt->error(ctxt->userData,
5615 "Found nsName attribute without oneOrMore ancestor\n");
5616 ctxt->nbErrors++;
5617 }
Daniel Veillard77648bb2003-02-20 15:03:22 +00005618 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005619 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
5620 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5621 ret = XML_RELAXNG_CONTENT_EMPTY;
5622 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
5623 (cur->type == XML_RELAXNG_ZEROORMORE)) {
5624 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5625 if (ctxt->error != NULL)
5626 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005627 "Found forbidden pattern data/except//oneOrMore\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005628 ctxt->nbErrors++;
5629 }
5630 if (flags & XML_RELAXNG_IN_START) {
5631 if (ctxt->error != NULL)
5632 ctxt->error(ctxt->userData,
5633 "Found forbidden pattern start//oneOrMore\n");
5634 ctxt->nbErrors++;
5635 }
5636 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
5637 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5638 ret = xmlRelaxNGGroupContentType(ret, ret);
5639 } else if (cur->type == XML_RELAXNG_LIST) {
5640 if (flags & XML_RELAXNG_IN_LIST) {
5641 if (ctxt->error != NULL)
5642 ctxt->error(ctxt->userData,
5643 "Found forbidden pattern list//list\n");
5644 ctxt->nbErrors++;
5645 }
5646 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5647 if (ctxt->error != NULL)
5648 ctxt->error(ctxt->userData,
5649 "Found forbidden pattern data/except//list\n");
5650 ctxt->nbErrors++;
5651 }
5652 if (flags & XML_RELAXNG_IN_START) {
5653 if (ctxt->error != NULL)
5654 ctxt->error(ctxt->userData,
5655 "Found forbidden pattern start//list\n");
5656 ctxt->nbErrors++;
5657 }
5658 nflags = flags | XML_RELAXNG_IN_LIST;
5659 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5660 } else if (cur->type == XML_RELAXNG_GROUP) {
5661 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5662 if (ctxt->error != NULL)
5663 ctxt->error(ctxt->userData,
5664 "Found forbidden pattern data/except//group\n");
5665 ctxt->nbErrors++;
5666 }
5667 if (flags & XML_RELAXNG_IN_START) {
5668 if (ctxt->error != NULL)
5669 ctxt->error(ctxt->userData,
5670 "Found forbidden pattern start//group\n");
5671 ctxt->nbErrors++;
5672 }
5673 if (flags & XML_RELAXNG_IN_ONEORMORE)
5674 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
5675 else
5676 nflags = flags;
5677 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5678 /*
5679 * The 7.3 Attribute derivation rule for groups is plugged there
5680 */
5681 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
5682 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
5683 if (flags & XML_RELAXNG_IN_LIST) {
5684 if (ctxt->error != NULL)
5685 ctxt->error(ctxt->userData,
5686 "Found forbidden pattern list//interleave\n");
5687 ctxt->nbErrors++;
5688 }
5689 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5690 if (ctxt->error != NULL)
5691 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005692 "Found forbidden pattern data/except//interleave\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005693 ctxt->nbErrors++;
5694 }
5695 if (flags & XML_RELAXNG_IN_START) {
5696 if (ctxt->error != NULL)
5697 ctxt->error(ctxt->userData,
5698 "Found forbidden pattern start//interleave\n");
5699 ctxt->nbErrors++;
5700 }
5701 if (flags & XML_RELAXNG_IN_ONEORMORE)
5702 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
5703 else
5704 nflags = flags;
5705 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5706 } else if (cur->type == XML_RELAXNG_EXCEPT) {
5707 if ((cur->parent != NULL) &&
5708 (cur->parent->type == XML_RELAXNG_DATATYPE))
5709 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
5710 else
5711 nflags = flags;
5712 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5713 } else if (cur->type == XML_RELAXNG_DATATYPE) {
5714 if (flags & XML_RELAXNG_IN_START) {
5715 if (ctxt->error != NULL)
5716 ctxt->error(ctxt->userData,
5717 "Found forbidden pattern start//data\n");
5718 ctxt->nbErrors++;
5719 }
5720 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5721 ret = XML_RELAXNG_CONTENT_SIMPLE;
5722 } else if (cur->type == XML_RELAXNG_VALUE) {
5723 if (flags & XML_RELAXNG_IN_START) {
5724 if (ctxt->error != NULL)
5725 ctxt->error(ctxt->userData,
5726 "Found forbidden pattern start//value\n");
5727 ctxt->nbErrors++;
5728 }
5729 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5730 ret = XML_RELAXNG_CONTENT_SIMPLE;
5731 } else if (cur->type == XML_RELAXNG_TEXT) {
5732 if (flags & XML_RELAXNG_IN_LIST) {
5733 if (ctxt->error != NULL)
5734 ctxt->error(ctxt->userData,
5735 "Found forbidden pattern list//text\n");
5736 ctxt->nbErrors++;
5737 }
5738 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5739 if (ctxt->error != NULL)
5740 ctxt->error(ctxt->userData,
5741 "Found forbidden pattern data/except//text\n");
5742 ctxt->nbErrors++;
5743 }
5744 if (flags & XML_RELAXNG_IN_START) {
5745 if (ctxt->error != NULL)
5746 ctxt->error(ctxt->userData,
5747 "Found forbidden pattern start//text\n");
5748 ctxt->nbErrors++;
5749 }
5750 ret = XML_RELAXNG_CONTENT_COMPLEX;
5751 } else if (cur->type == XML_RELAXNG_EMPTY) {
5752 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5753 if (ctxt->error != NULL)
5754 ctxt->error(ctxt->userData,
5755 "Found forbidden pattern data/except//empty\n");
5756 ctxt->nbErrors++;
5757 }
5758 if (flags & XML_RELAXNG_IN_START) {
5759 if (ctxt->error != NULL)
5760 ctxt->error(ctxt->userData,
5761 "Found forbidden pattern start//empty\n");
5762 ctxt->nbErrors++;
5763 }
5764 ret = XML_RELAXNG_CONTENT_EMPTY;
5765 } else if (cur->type == XML_RELAXNG_CHOICE) {
5766 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
5767 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5768 } else {
5769 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5770 }
5771 cur = cur->next;
5772 if (ptype == XML_RELAXNG_GROUP) {
5773 val = xmlRelaxNGGroupContentType(val, ret);
5774 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
5775 tmp = xmlRelaxNGGroupContentType(val, ret);
5776 if (tmp != XML_RELAXNG_CONTENT_ERROR)
5777 tmp = xmlRelaxNGMaxContentType(val, ret);
5778 } else if (ptype == XML_RELAXNG_CHOICE) {
5779 val = xmlRelaxNGMaxContentType(val, ret);
5780 } else if (ptype == XML_RELAXNG_LIST) {
5781 val = XML_RELAXNG_CONTENT_SIMPLE;
5782 } else if (ptype == XML_RELAXNG_EXCEPT) {
5783 if (ret == XML_RELAXNG_CONTENT_ERROR)
5784 val = XML_RELAXNG_CONTENT_ERROR;
5785 else
5786 val = XML_RELAXNG_CONTENT_SIMPLE;
5787 } else {
5788 val = xmlRelaxNGGroupContentType(val, ret);
5789 }
5790
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005791 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005792 return(val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005793}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005794
5795/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005796 * xmlRelaxNGParseGrammar:
5797 * @ctxt: a Relax-NG parser context
5798 * @nodes: grammar children nodes
5799 *
5800 * parse a Relax-NG <grammar> node
5801 *
5802 * Returns the internal xmlRelaxNGGrammarPtr built or
5803 * NULL in case of error
5804 */
5805static xmlRelaxNGGrammarPtr
5806xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
5807 xmlRelaxNGGrammarPtr ret, tmp, old;
5808
Daniel Veillardc482e262003-02-26 14:48:48 +00005809#ifdef DEBUG_GRAMMAR
5810 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
5811#endif
5812
Daniel Veillard6eadf632003-01-23 18:29:16 +00005813 ret = xmlRelaxNGNewGrammar(ctxt);
5814 if (ret == NULL)
5815 return(NULL);
5816
5817 /*
5818 * Link the new grammar in the tree
5819 */
5820 ret->parent = ctxt->grammar;
5821 if (ctxt->grammar != NULL) {
5822 tmp = ctxt->grammar->children;
5823 if (tmp == NULL) {
5824 ctxt->grammar->children = ret;
5825 } else {
5826 while (tmp->next != NULL)
5827 tmp = tmp->next;
5828 tmp->next = ret;
5829 }
5830 }
5831
5832 old = ctxt->grammar;
5833 ctxt->grammar = ret;
5834 xmlRelaxNGParseGrammarContent(ctxt, nodes);
5835 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005836 if (ctxt->grammar == NULL) {
5837 if (ctxt->error != NULL)
5838 ctxt->error(ctxt->userData,
5839 "Failed to parse <grammar> content\n");
5840 ctxt->nbErrors++;
5841 } else if (ctxt->grammar->start == NULL) {
5842 if (ctxt->error != NULL)
5843 ctxt->error(ctxt->userData,
5844 "Element <grammar> has no <start>\n");
5845 ctxt->nbErrors++;
5846 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005847
5848 /*
5849 * Apply 4.17 mergingd rules to defines and starts
5850 */
5851 xmlRelaxNGCombineStart(ctxt, ret);
5852 if (ret->defs != NULL) {
5853 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
5854 ctxt);
5855 }
5856
5857 /*
5858 * link together defines and refs in this grammar
5859 */
5860 if (ret->refs != NULL) {
5861 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
5862 ctxt);
5863 }
Daniel Veillard952379b2003-03-17 15:37:12 +00005864
Daniel Veillard6eadf632003-01-23 18:29:16 +00005865 ctxt->grammar = old;
5866 return(ret);
5867}
5868
5869/**
5870 * xmlRelaxNGParseDocument:
5871 * @ctxt: a Relax-NG parser context
5872 * @node: the root node of the RelaxNG schema
5873 *
5874 * parse a Relax-NG definition resource and build an internal
5875 * xmlRelaxNG struture which can be used to validate instances.
5876 *
5877 * Returns the internal XML RelaxNG structure built or
5878 * NULL in case of error
5879 */
5880static xmlRelaxNGPtr
5881xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
5882 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005883 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00005884 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005885
5886 if ((ctxt == NULL) || (node == NULL))
5887 return (NULL);
5888
5889 schema = xmlRelaxNGNewRelaxNG(ctxt);
5890 if (schema == NULL)
5891 return(NULL);
5892
Daniel Veillard276be4a2003-01-24 01:03:34 +00005893 olddefine = ctxt->define;
5894 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005895 if (IS_RELAXNG(node, "grammar")) {
5896 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5897 } else {
Daniel Veillardc482e262003-02-26 14:48:48 +00005898 xmlRelaxNGGrammarPtr tmp, ret;
5899
5900 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005901 if (schema->topgrammar == NULL) {
5902 return(schema);
5903 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005904 /*
5905 * Link the new grammar in the tree
5906 */
5907 ret->parent = ctxt->grammar;
5908 if (ctxt->grammar != NULL) {
5909 tmp = ctxt->grammar->children;
5910 if (tmp == NULL) {
5911 ctxt->grammar->children = ret;
5912 } else {
5913 while (tmp->next != NULL)
5914 tmp = tmp->next;
5915 tmp->next = ret;
5916 }
5917 }
Daniel Veillarde431a272003-01-29 23:02:33 +00005918 old = ctxt->grammar;
Daniel Veillardc482e262003-02-26 14:48:48 +00005919 ctxt->grammar = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005920 xmlRelaxNGParseStart(ctxt, node);
Daniel Veillarde431a272003-01-29 23:02:33 +00005921 if (old != NULL)
5922 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005923 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005924 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00005925 if (schema->topgrammar->start != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005926 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005927 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005928 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
5929 while ((schema->topgrammar->start != NULL) &&
5930 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
5931 (schema->topgrammar->start->next != NULL))
5932 schema->topgrammar->start = schema->topgrammar->start->content;
5933 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
5934 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005935 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005936 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005937
5938#ifdef DEBUG
5939 if (schema == NULL)
5940 xmlGenericError(xmlGenericErrorContext,
5941 "xmlRelaxNGParseDocument() failed\n");
5942#endif
5943
5944 return (schema);
5945}
5946
5947/************************************************************************
5948 * *
5949 * Reading RelaxNGs *
5950 * *
5951 ************************************************************************/
5952
5953/**
5954 * xmlRelaxNGNewParserCtxt:
5955 * @URL: the location of the schema
5956 *
5957 * Create an XML RelaxNGs parse context for that file/resource expected
5958 * to contain an XML RelaxNGs file.
5959 *
5960 * Returns the parser context or NULL in case of error
5961 */
5962xmlRelaxNGParserCtxtPtr
5963xmlRelaxNGNewParserCtxt(const char *URL) {
5964 xmlRelaxNGParserCtxtPtr ret;
5965
5966 if (URL == NULL)
5967 return(NULL);
5968
5969 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
5970 if (ret == NULL) {
5971 xmlGenericError(xmlGenericErrorContext,
5972 "Failed to allocate new schama parser context for %s\n", URL);
5973 return (NULL);
5974 }
5975 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
5976 ret->URL = xmlStrdup((const xmlChar *)URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005977 ret->error = xmlGenericError;
5978 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005979 return (ret);
5980}
5981
5982/**
5983 * xmlRelaxNGNewMemParserCtxt:
5984 * @buffer: a pointer to a char array containing the schemas
5985 * @size: the size of the array
5986 *
5987 * Create an XML RelaxNGs parse context for that memory buffer expected
5988 * to contain an XML RelaxNGs file.
5989 *
5990 * Returns the parser context or NULL in case of error
5991 */
5992xmlRelaxNGParserCtxtPtr
5993xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) {
5994 xmlRelaxNGParserCtxtPtr ret;
5995
5996 if ((buffer == NULL) || (size <= 0))
5997 return(NULL);
5998
5999 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6000 if (ret == NULL) {
6001 xmlGenericError(xmlGenericErrorContext,
6002 "Failed to allocate new schama parser context\n");
6003 return (NULL);
6004 }
6005 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6006 ret->buffer = buffer;
6007 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006008 ret->error = xmlGenericError;
6009 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006010 return (ret);
6011}
6012
6013/**
6014 * xmlRelaxNGFreeParserCtxt:
6015 * @ctxt: the schema parser context
6016 *
6017 * Free the resources associated to the schema parser context
6018 */
6019void
6020xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) {
6021 if (ctxt == NULL)
6022 return;
6023 if (ctxt->URL != NULL)
6024 xmlFree(ctxt->URL);
6025 if (ctxt->doc != NULL)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006026 xmlFreeDoc(ctxt->document);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006027 if (ctxt->interleaves != NULL)
6028 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006029 if (ctxt->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006030 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006031 if (ctxt->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006032 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006033 if (ctxt->docTab != NULL)
6034 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006035 if (ctxt->incTab != NULL)
6036 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006037 if (ctxt->defTab != NULL) {
6038 int i;
6039
6040 for (i = 0;i < ctxt->defNr;i++)
6041 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6042 xmlFree(ctxt->defTab);
6043 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006044 xmlFree(ctxt);
6045}
6046
Daniel Veillard6eadf632003-01-23 18:29:16 +00006047/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006048 * xmlRelaxNGNormExtSpace:
6049 * @value: a value
6050 *
6051 * Removes the leading and ending spaces of the value
6052 * The string is modified "in situ"
6053 */
6054static void
6055xmlRelaxNGNormExtSpace(xmlChar *value) {
6056 xmlChar *start = value;
6057 xmlChar *cur = value;
6058 if (value == NULL)
6059 return;
6060
6061 while (IS_BLANK(*cur)) cur++;
6062 if (cur == start) {
6063 do {
6064 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
6065 if (*cur == 0)
6066 return;
6067 start = cur;
6068 while (IS_BLANK(*cur)) cur++;
6069 if (*cur == 0) {
6070 *start = 0;
6071 return;
6072 }
6073 } while (1);
6074 } else {
6075 do {
6076 while ((*cur != 0) && (!IS_BLANK(*cur)))
6077 *start++ = *cur++;
6078 if (*cur == 0) {
6079 *start = 0;
6080 return;
6081 }
6082 /* don't try to normalize the inner spaces */
6083 while (IS_BLANK(*cur)) cur++;
6084 *start++ = *cur++;
6085 if (*cur == 0) {
6086 *start = 0;
6087 return;
6088 }
6089 } while (1);
6090 }
6091}
6092
6093/**
6094 * xmlRelaxNGCheckAttributes:
6095 * @ctxt: a Relax-NG parser context
6096 * @node: a Relax-NG node
6097 *
6098 * Check all the attributes on the given node
6099 */
6100static void
6101xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6102 xmlAttrPtr cur, next;
6103
6104 cur = node->properties;
6105 while (cur != NULL) {
6106 next = cur->next;
6107 if ((cur->ns == NULL) ||
6108 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6109 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6110 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6111 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6112 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6113 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
Daniel Veillard2df2de22003-02-17 23:34:33 +00006114 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
Daniel Veillardd2298792003-02-14 16:54:11 +00006115 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6116 if (ctxt->error != NULL)
6117 ctxt->error(ctxt->userData,
6118 "Attribute %s is not allowed on %s\n",
6119 cur->name, node->name);
6120 ctxt->nbErrors++;
6121 }
6122 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6123 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6124 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6125 if (ctxt->error != NULL)
6126 ctxt->error(ctxt->userData,
6127 "Attribute %s is not allowed on %s\n",
6128 cur->name, node->name);
6129 ctxt->nbErrors++;
6130 }
6131 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6132 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6133 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6134 if (ctxt->error != NULL)
6135 ctxt->error(ctxt->userData,
6136 "Attribute %s is not allowed on %s\n",
6137 cur->name, node->name);
6138 ctxt->nbErrors++;
6139 }
6140 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6141 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6142 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6143 if (ctxt->error != NULL)
6144 ctxt->error(ctxt->userData,
6145 "Attribute %s is not allowed on %s\n",
6146 cur->name, node->name);
6147 ctxt->nbErrors++;
6148 }
6149 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6150 xmlChar *val;
6151 xmlURIPtr uri;
6152
6153 val = xmlNodeListGetString(node->doc, cur->children, 1);
6154 if (val != NULL) {
6155 if (val[0] != 0) {
6156 uri = xmlParseURI((const char *) val);
6157 if (uri == NULL) {
6158 if (ctxt->error != NULL)
6159 ctxt->error(ctxt->userData,
6160 "Attribute %s contains invalid URI %s\n",
6161 cur->name, val);
6162 ctxt->nbErrors++;
6163 } else {
6164 if (uri->scheme == NULL) {
6165 if (ctxt->error != NULL)
6166 ctxt->error(ctxt->userData,
6167 "Attribute %s URI %s is not absolute\n",
6168 cur->name, val);
6169 ctxt->nbErrors++;
6170 }
6171 if (uri->fragment != NULL) {
6172 if (ctxt->error != NULL)
6173 ctxt->error(ctxt->userData,
6174 "Attribute %s URI %s has a fragment ID\n",
6175 cur->name, val);
6176 ctxt->nbErrors++;
6177 }
6178 xmlFreeURI(uri);
6179 }
6180 }
6181 xmlFree(val);
6182 }
6183 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6184 if (ctxt->error != NULL)
6185 ctxt->error(ctxt->userData,
6186 "Unknown attribute %s on %s\n",
6187 cur->name, node->name);
6188 ctxt->nbErrors++;
6189 }
6190 }
6191 cur = next;
6192 }
6193}
6194
6195/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006196 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006197 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006198 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006199 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006200 * Cleanup the subtree from unwanted nodes for parsing, resolve
6201 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006202 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006203static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00006204xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) {
Daniel Veillardc5312d72003-02-21 17:14:10 +00006205 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006206
Daniel Veillard6eadf632003-01-23 18:29:16 +00006207 delete = NULL;
6208 cur = root;
6209 while (cur != NULL) {
6210 if (delete != NULL) {
6211 xmlUnlinkNode(delete);
6212 xmlFreeNode(delete);
6213 delete = NULL;
6214 }
6215 if (cur->type == XML_ELEMENT_NODE) {
6216 /*
6217 * Simplification 4.1. Annotations
6218 */
6219 if ((cur->ns == NULL) ||
6220 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
Daniel Veillardd2298792003-02-14 16:54:11 +00006221 if ((cur->parent != NULL) &&
6222 (cur->parent->type == XML_ELEMENT_NODE) &&
6223 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6224 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6225 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6226 if (ctxt->error != NULL)
6227 ctxt->error(ctxt->userData,
6228 "element %s doesn't allow foreign elements\n",
6229 cur->parent->name);
6230 ctxt->nbErrors++;
6231 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006232 delete = cur;
6233 goto skip_children;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006234 } else {
6235 xmlRelaxNGCleanupAttributes(ctxt, cur);
6236 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6237 xmlChar *href, *ns, *base, *URL;
6238 xmlRelaxNGDocumentPtr docu;
6239 xmlNodePtr tmp;
6240
6241 ns = xmlGetProp(cur, BAD_CAST "ns");
6242 if (ns == NULL) {
6243 tmp = cur->parent;
6244 while ((tmp != NULL) &&
6245 (tmp->type == XML_ELEMENT_NODE)) {
6246 ns = xmlGetProp(tmp, BAD_CAST "ns");
6247 if (ns != NULL)
6248 break;
6249 tmp = tmp->parent;
6250 }
6251 }
6252 href = xmlGetProp(cur, BAD_CAST "href");
6253 if (href == NULL) {
6254 if (ctxt->error != NULL)
6255 ctxt->error(ctxt->userData,
6256 "xmlRelaxNGParse: externalRef has no href attribute\n");
6257 ctxt->nbErrors++;
6258 delete = cur;
6259 goto skip_children;
6260 }
6261 base = xmlNodeGetBase(cur->doc, cur);
6262 URL = xmlBuildURI(href, base);
6263 if (URL == NULL) {
6264 if (ctxt->error != NULL)
6265 ctxt->error(ctxt->userData,
6266 "Failed to compute URL for externalRef %s\n", href);
6267 ctxt->nbErrors++;
6268 if (href != NULL)
6269 xmlFree(href);
6270 if (base != NULL)
6271 xmlFree(base);
6272 delete = cur;
6273 goto skip_children;
6274 }
6275 if (href != NULL)
6276 xmlFree(href);
6277 if (base != NULL)
6278 xmlFree(base);
6279 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6280 if (docu == NULL) {
6281 if (ctxt->error != NULL)
6282 ctxt->error(ctxt->userData,
6283 "Failed to load externalRef %s\n", URL);
6284 ctxt->nbErrors++;
6285 xmlFree(URL);
6286 delete = cur;
6287 goto skip_children;
6288 }
6289 if (ns != NULL)
6290 xmlFree(ns);
6291 xmlFree(URL);
6292 cur->_private = docu;
6293 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6294 xmlChar *href, *ns, *base, *URL;
6295 xmlRelaxNGIncludePtr incl;
6296 xmlNodePtr tmp;
6297
6298 href = xmlGetProp(cur, BAD_CAST "href");
6299 if (href == NULL) {
6300 if (ctxt->error != NULL)
6301 ctxt->error(ctxt->userData,
6302 "xmlRelaxNGParse: include has no href attribute\n");
6303 ctxt->nbErrors++;
6304 delete = cur;
6305 goto skip_children;
6306 }
6307 base = xmlNodeGetBase(cur->doc, cur);
6308 URL = xmlBuildURI(href, base);
6309 if (URL == NULL) {
6310 if (ctxt->error != NULL)
6311 ctxt->error(ctxt->userData,
6312 "Failed to compute URL for include %s\n", href);
6313 ctxt->nbErrors++;
6314 if (href != NULL)
6315 xmlFree(href);
6316 if (base != NULL)
6317 xmlFree(base);
6318 delete = cur;
6319 goto skip_children;
6320 }
6321 if (href != NULL)
6322 xmlFree(href);
6323 if (base != NULL)
6324 xmlFree(base);
6325 ns = xmlGetProp(cur, BAD_CAST "ns");
6326 if (ns == NULL) {
6327 tmp = cur->parent;
6328 while ((tmp != NULL) &&
6329 (tmp->type == XML_ELEMENT_NODE)) {
6330 ns = xmlGetProp(tmp, BAD_CAST "ns");
6331 if (ns != NULL)
6332 break;
6333 tmp = tmp->parent;
6334 }
6335 }
6336 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6337 if (ns != NULL)
6338 xmlFree(ns);
6339 if (incl == NULL) {
6340 if (ctxt->error != NULL)
6341 ctxt->error(ctxt->userData,
6342 "Failed to load include %s\n", URL);
6343 ctxt->nbErrors++;
6344 xmlFree(URL);
6345 delete = cur;
6346 goto skip_children;
6347 }
6348 xmlFree(URL);
6349 cur->_private = incl;
6350 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6351 (xmlStrEqual(cur->name, BAD_CAST "attribute"))) {
6352 xmlChar *name, *ns;
6353 xmlNodePtr text = NULL;
6354
6355 /*
6356 * Simplification 4.8. name attribute of element
6357 * and attribute elements
6358 */
6359 name = xmlGetProp(cur, BAD_CAST "name");
6360 if (name != NULL) {
6361 if (cur->children == NULL) {
6362 text = xmlNewChild(cur, cur->ns, BAD_CAST "name",
6363 name);
6364 } else {
6365 xmlNodePtr node;
6366 node = xmlNewNode(cur->ns, BAD_CAST "name");
6367 if (node != NULL) {
6368 xmlAddPrevSibling(cur->children, node);
6369 text = xmlNewText(name);
6370 xmlAddChild(node, text);
6371 text = node;
6372 }
6373 }
6374 if (text == NULL) {
6375 if (ctxt->error != NULL)
6376 ctxt->error(ctxt->userData,
6377 "Failed to create a name %s element\n", name);
6378 ctxt->nbErrors++;
6379 }
6380 xmlUnsetProp(cur, BAD_CAST "name");
6381 xmlFree(name);
6382 ns = xmlGetProp(cur, BAD_CAST "ns");
6383 if (ns != NULL) {
6384 if (text != NULL) {
6385 xmlSetProp(text, BAD_CAST "ns", ns);
6386 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
6387 }
6388 xmlFree(ns);
6389 } else if (xmlStrEqual(cur->name,
6390 BAD_CAST "attribute")) {
6391 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
6392 }
6393 }
6394 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
6395 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
6396 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
6397 /*
6398 * Simplification 4.8. name attribute of element
6399 * and attribute elements
6400 */
6401 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
6402 xmlNodePtr node;
6403 xmlChar *ns = NULL;
6404
6405 node = cur->parent;
6406 while ((node != NULL) &&
6407 (node->type == XML_ELEMENT_NODE)) {
6408 ns = xmlGetProp(node, BAD_CAST "ns");
6409 if (ns != NULL) {
6410 break;
6411 }
6412 node = node->parent;
6413 }
6414 if (ns == NULL) {
6415 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
6416 } else {
6417 xmlSetProp(cur, BAD_CAST "ns", ns);
6418 xmlFree(ns);
6419 }
6420 }
6421 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6422 xmlChar *name, *local, *prefix;
6423
6424 /*
6425 * Simplification: 4.10. QNames
6426 */
6427 name = xmlNodeGetContent(cur);
6428 if (name != NULL) {
6429 local = xmlSplitQName2(name, &prefix);
6430 if (local != NULL) {
6431 xmlNsPtr ns;
6432
6433 ns = xmlSearchNs(cur->doc, cur, prefix);
6434 if (ns == NULL) {
6435 if (ctxt->error != NULL)
6436 ctxt->error(ctxt->userData,
6437 "xmlRelaxNGParse: no namespace for prefix %s\n", prefix);
6438 ctxt->nbErrors++;
6439 } else {
6440 xmlSetProp(cur, BAD_CAST "ns", ns->href);
6441 xmlNodeSetContent(cur, local);
6442 }
6443 xmlFree(local);
6444 xmlFree(prefix);
6445 }
6446 xmlFree(name);
6447 }
6448 }
6449 /*
6450 * 4.16
6451 */
6452 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
6453 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
6454 if (ctxt->error != NULL)
6455 ctxt->error(ctxt->userData,
6456 "Found nsName/except//nsName forbidden construct\n");
6457 ctxt->nbErrors++;
6458 }
6459 }
6460 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
6461 (cur != root)) {
6462 int oldflags = ctxt->flags;
6463
6464 /*
6465 * 4.16
6466 */
6467 if ((cur->parent != NULL) &&
6468 (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) {
6469 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
6470 xmlRelaxNGCleanupTree(ctxt, cur);
6471 ctxt->flags = oldflags;
6472 goto skip_children;
6473 } else if ((cur->parent != NULL) &&
6474 (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) {
6475 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
6476 xmlRelaxNGCleanupTree(ctxt, cur);
6477 ctxt->flags = oldflags;
6478 goto skip_children;
6479 }
6480 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
6481 /*
6482 * 4.16
6483 */
6484 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
6485 if (ctxt->error != NULL)
6486 ctxt->error(ctxt->userData,
6487 "Found anyName/except//anyName forbidden construct\n");
6488 ctxt->nbErrors++;
6489 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
6490 if (ctxt->error != NULL)
6491 ctxt->error(ctxt->userData,
6492 "Found nsName/except//anyName forbidden construct\n");
6493 ctxt->nbErrors++;
6494 }
6495 }
6496 /*
6497 * Thisd is not an else since "include" is transformed
6498 * into a div
6499 */
6500 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
6501 xmlChar *ns;
6502 xmlNodePtr child, ins, tmp;
6503
6504 /*
6505 * implements rule 4.11
6506 */
6507
6508 ns = xmlGetProp(cur, BAD_CAST "ns");
6509
6510 child = cur->children;
6511 ins = cur;
6512 while (child != NULL) {
6513 if (ns != NULL) {
6514 if (!xmlHasProp(child, BAD_CAST "ns")) {
6515 xmlSetProp(child, BAD_CAST "ns", ns);
6516 }
6517 }
6518 tmp = child->next;
6519 xmlUnlinkNode(child);
6520 ins = xmlAddNextSibling(ins, child);
6521 child = tmp;
6522 }
6523 if (ns != NULL)
6524 xmlFree(ns);
6525 delete = cur;
6526 goto skip_children;
6527 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006528 }
6529 }
6530 /*
6531 * Simplification 4.2 whitespaces
6532 */
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006533 else if ((cur->type == XML_TEXT_NODE) ||
6534 (cur->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00006535 if (IS_BLANK_NODE(cur)) {
6536 if (cur->parent->type == XML_ELEMENT_NODE) {
6537 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) &&
6538 (!xmlStrEqual(cur->parent->name, BAD_CAST "param")))
6539 delete = cur;
6540 } else {
6541 delete = cur;
6542 goto skip_children;
6543 }
6544 }
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006545 } else {
Daniel Veillard6eadf632003-01-23 18:29:16 +00006546 delete = cur;
6547 goto skip_children;
6548 }
6549
6550 /*
6551 * Skip to next node
6552 */
6553 if (cur->children != NULL) {
6554 if ((cur->children->type != XML_ENTITY_DECL) &&
6555 (cur->children->type != XML_ENTITY_REF_NODE) &&
6556 (cur->children->type != XML_ENTITY_NODE)) {
6557 cur = cur->children;
6558 continue;
6559 }
6560 }
6561skip_children:
6562 if (cur->next != NULL) {
6563 cur = cur->next;
6564 continue;
6565 }
6566
6567 do {
6568 cur = cur->parent;
6569 if (cur == NULL)
6570 break;
6571 if (cur == root) {
6572 cur = NULL;
6573 break;
6574 }
6575 if (cur->next != NULL) {
6576 cur = cur->next;
6577 break;
6578 }
6579 } while (cur != NULL);
6580 }
6581 if (delete != NULL) {
6582 xmlUnlinkNode(delete);
6583 xmlFreeNode(delete);
6584 delete = NULL;
6585 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00006586}
Daniel Veillard6eadf632003-01-23 18:29:16 +00006587
Daniel Veillardc5312d72003-02-21 17:14:10 +00006588/**
6589 * xmlRelaxNGCleanupDoc:
6590 * @ctxt: a Relax-NG parser context
6591 * @doc: an xmldocPtr document pointer
6592 *
6593 * Cleanup the document from unwanted nodes for parsing, resolve
6594 * Include and externalRef lookups.
6595 *
6596 * Returns the cleaned up document or NULL in case of error
6597 */
6598static xmlDocPtr
6599xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) {
6600 xmlNodePtr root;
6601
6602 /*
6603 * Extract the root
6604 */
6605 root = xmlDocGetRootElement(doc);
6606 if (root == NULL) {
6607 if (ctxt->error != NULL)
6608 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
6609 ctxt->URL);
6610 ctxt->nbErrors++;
6611 return (NULL);
6612 }
6613 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006614 return(doc);
6615}
6616
6617/**
6618 * xmlRelaxNGParse:
6619 * @ctxt: a Relax-NG parser context
6620 *
6621 * parse a schema definition resource and build an internal
6622 * XML Shema struture which can be used to validate instances.
6623 * *WARNING* this interface is highly subject to change
6624 *
6625 * Returns the internal XML RelaxNG structure built from the resource or
6626 * NULL in case of error
6627 */
6628xmlRelaxNGPtr
6629xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
6630{
6631 xmlRelaxNGPtr ret = NULL;
6632 xmlDocPtr doc;
6633 xmlNodePtr root;
6634
6635 xmlRelaxNGInitTypes();
6636
6637 if (ctxt == NULL)
6638 return (NULL);
6639
6640 /*
6641 * First step is to parse the input document into an DOM/Infoset
6642 */
6643 if (ctxt->URL != NULL) {
6644 doc = xmlParseFile((const char *) ctxt->URL);
6645 if (doc == NULL) {
6646 if (ctxt->error != NULL)
6647 ctxt->error(ctxt->userData,
6648 "xmlRelaxNGParse: could not load %s\n", ctxt->URL);
6649 ctxt->nbErrors++;
6650 return (NULL);
6651 }
6652 } else if (ctxt->buffer != NULL) {
6653 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
6654 if (doc == NULL) {
6655 if (ctxt->error != NULL)
6656 ctxt->error(ctxt->userData,
6657 "xmlRelaxNGParse: could not parse schemas\n");
6658 ctxt->nbErrors++;
6659 return (NULL);
6660 }
6661 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
6662 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
6663 } else {
6664 if (ctxt->error != NULL)
6665 ctxt->error(ctxt->userData,
6666 "xmlRelaxNGParse: nothing to parse\n");
6667 ctxt->nbErrors++;
6668 return (NULL);
6669 }
6670 ctxt->document = doc;
6671
6672 /*
6673 * Some preprocessing of the document content
6674 */
6675 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
6676 if (doc == NULL) {
6677 xmlFreeDoc(ctxt->document);
6678 ctxt->document = NULL;
6679 return(NULL);
6680 }
6681
Daniel Veillard6eadf632003-01-23 18:29:16 +00006682 /*
6683 * Then do the parsing for good
6684 */
6685 root = xmlDocGetRootElement(doc);
6686 if (root == NULL) {
6687 if (ctxt->error != NULL)
6688 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
6689 ctxt->URL);
6690 ctxt->nbErrors++;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006691 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006692 return (NULL);
6693 }
6694 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006695 if (ret == NULL) {
6696 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006697 return(NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006698 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006699
6700 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00006701 * Check the ref/defines links
6702 */
6703 /*
6704 * try to preprocess interleaves
6705 */
6706 if (ctxt->interleaves != NULL) {
6707 xmlHashScan(ctxt->interleaves,
6708 (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt);
6709 }
6710
6711 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00006712 * if there was a parsing error return NULL
6713 */
6714 if (ctxt->nbErrors > 0) {
6715 xmlRelaxNGFree(ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006716 ctxt->document = NULL;
6717 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006718 return(NULL);
6719 }
6720
6721 /*
6722 * Transfer the pointer for cleanup at the schema level.
6723 */
6724 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006725 ctxt->document = NULL;
6726 ret->documents = ctxt->documents;
6727 ctxt->documents = NULL;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006728
Daniel Veillarde2a5a082003-02-02 14:35:17 +00006729 ret->includes = ctxt->includes;
6730 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00006731 ret->defNr = ctxt->defNr;
6732 ret->defTab = ctxt->defTab;
6733 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006734 if (ctxt->idref == 1)
6735 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006736
6737 return (ret);
6738}
6739
6740/**
6741 * xmlRelaxNGSetParserErrors:
6742 * @ctxt: a Relax-NG validation context
6743 * @err: the error callback
6744 * @warn: the warning callback
6745 * @ctx: contextual data for the callbacks
6746 *
6747 * Set the callback functions used to handle errors for a validation context
6748 */
6749void
6750xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
6751 xmlRelaxNGValidityErrorFunc err,
6752 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
6753 if (ctxt == NULL)
6754 return;
6755 ctxt->error = err;
6756 ctxt->warning = warn;
6757 ctxt->userData = ctx;
6758}
6759/************************************************************************
6760 * *
6761 * Dump back a compiled form *
6762 * *
6763 ************************************************************************/
6764static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define);
6765
6766/**
6767 * xmlRelaxNGDumpDefines:
6768 * @output: the file output
6769 * @defines: a list of define structures
6770 *
6771 * Dump a RelaxNG structure back
6772 */
6773static void
6774xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) {
6775 while (defines != NULL) {
6776 xmlRelaxNGDumpDefine(output, defines);
6777 defines = defines->next;
6778 }
6779}
6780
6781/**
6782 * xmlRelaxNGDumpDefine:
6783 * @output: the file output
6784 * @define: a define structure
6785 *
6786 * Dump a RelaxNG structure back
6787 */
6788static void
6789xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) {
6790 if (define == NULL)
6791 return;
6792 switch(define->type) {
6793 case XML_RELAXNG_EMPTY:
6794 fprintf(output, "<empty/>\n");
6795 break;
6796 case XML_RELAXNG_NOT_ALLOWED:
6797 fprintf(output, "<notAllowed/>\n");
6798 break;
6799 case XML_RELAXNG_TEXT:
6800 fprintf(output, "<text/>\n");
6801 break;
6802 case XML_RELAXNG_ELEMENT:
6803 fprintf(output, "<element>\n");
6804 if (define->name != NULL) {
6805 fprintf(output, "<name");
6806 if (define->ns != NULL)
6807 fprintf(output, " ns=\"%s\"", define->ns);
6808 fprintf(output, ">%s</name>\n", define->name);
6809 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006810 xmlRelaxNGDumpDefines(output, define->attrs);
6811 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006812 fprintf(output, "</element>\n");
6813 break;
6814 case XML_RELAXNG_LIST:
6815 fprintf(output, "<list>\n");
6816 xmlRelaxNGDumpDefines(output, define->content);
6817 fprintf(output, "</list>\n");
6818 break;
6819 case XML_RELAXNG_ONEORMORE:
6820 fprintf(output, "<oneOrMore>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006821 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006822 fprintf(output, "</oneOrMore>\n");
6823 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006824 case XML_RELAXNG_ZEROORMORE:
6825 fprintf(output, "<zeroOrMore>\n");
6826 xmlRelaxNGDumpDefines(output, define->content);
6827 fprintf(output, "</zeroOrMore>\n");
6828 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006829 case XML_RELAXNG_CHOICE:
6830 fprintf(output, "<choice>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006831 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006832 fprintf(output, "</choice>\n");
6833 break;
6834 case XML_RELAXNG_GROUP:
6835 fprintf(output, "<group>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006836 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006837 fprintf(output, "</group>\n");
6838 break;
6839 case XML_RELAXNG_INTERLEAVE:
6840 fprintf(output, "<interleave>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006841 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006842 fprintf(output, "</interleave>\n");
6843 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006844 case XML_RELAXNG_OPTIONAL:
6845 fprintf(output, "<optional>\n");
6846 xmlRelaxNGDumpDefines(output, define->content);
6847 fprintf(output, "</optional>\n");
6848 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006849 case XML_RELAXNG_ATTRIBUTE:
6850 fprintf(output, "<attribute>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006851 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006852 fprintf(output, "</attribute>\n");
6853 break;
6854 case XML_RELAXNG_DEF:
6855 fprintf(output, "<define");
6856 if (define->name != NULL)
6857 fprintf(output, " name=\"%s\"", define->name);
6858 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006859 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006860 fprintf(output, "</define>\n");
6861 break;
6862 case XML_RELAXNG_REF:
6863 fprintf(output, "<ref");
6864 if (define->name != NULL)
6865 fprintf(output, " name=\"%s\"", define->name);
6866 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006867 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006868 fprintf(output, "</ref>\n");
6869 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00006870 case XML_RELAXNG_PARENTREF:
6871 fprintf(output, "<parentRef");
6872 if (define->name != NULL)
6873 fprintf(output, " name=\"%s\"", define->name);
6874 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006875 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard419a7682003-02-03 23:22:49 +00006876 fprintf(output, "</parentRef>\n");
6877 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006878 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard416589a2003-02-17 17:25:42 +00006879 fprintf(output, "<externalRef>");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006880 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillarde431a272003-01-29 23:02:33 +00006881 fprintf(output, "</externalRef>\n");
6882 break;
6883 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00006884 case XML_RELAXNG_VALUE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00006885 TODO
Daniel Veillard6eadf632003-01-23 18:29:16 +00006886 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006887 case XML_RELAXNG_START:
Daniel Veillardfd573f12003-03-16 17:52:32 +00006888 case XML_RELAXNG_EXCEPT:
Daniel Veillard8fe98712003-02-19 00:19:14 +00006889 case XML_RELAXNG_PARAM:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006890 TODO
6891 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00006892 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +00006893 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006894 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006895 }
6896}
6897
6898/**
6899 * xmlRelaxNGDumpGrammar:
6900 * @output: the file output
6901 * @grammar: a grammar structure
6902 * @top: is this a top grammar
6903 *
6904 * Dump a RelaxNG structure back
6905 */
6906static void
6907xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
6908{
6909 if (grammar == NULL)
6910 return;
6911
6912 fprintf(output, "<grammar");
6913 if (top)
6914 fprintf(output,
6915 " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
6916 switch(grammar->combine) {
6917 case XML_RELAXNG_COMBINE_UNDEFINED:
6918 break;
6919 case XML_RELAXNG_COMBINE_CHOICE:
6920 fprintf(output, " combine=\"choice\"");
6921 break;
6922 case XML_RELAXNG_COMBINE_INTERLEAVE:
6923 fprintf(output, " combine=\"interleave\"");
6924 break;
6925 default:
6926 fprintf(output, " <!-- invalid combine value -->");
6927 }
6928 fprintf(output, ">\n");
6929 if (grammar->start == NULL) {
6930 fprintf(output, " <!-- grammar had no start -->");
6931 } else {
6932 fprintf(output, "<start>\n");
6933 xmlRelaxNGDumpDefine(output, grammar->start);
6934 fprintf(output, "</start>\n");
6935 }
6936 /* TODO ? Dump the defines ? */
6937 fprintf(output, "</grammar>\n");
6938}
6939
6940/**
6941 * xmlRelaxNGDump:
6942 * @output: the file output
6943 * @schema: a schema structure
6944 *
6945 * Dump a RelaxNG structure back
6946 */
6947void
6948xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
6949{
6950 if (schema == NULL) {
6951 fprintf(output, "RelaxNG empty or failed to compile\n");
6952 return;
6953 }
6954 fprintf(output, "RelaxNG: ");
6955 if (schema->doc == NULL) {
6956 fprintf(output, "no document\n");
6957 } else if (schema->doc->URL != NULL) {
6958 fprintf(output, "%s\n", schema->doc->URL);
6959 } else {
6960 fprintf(output, "\n");
6961 }
6962 if (schema->topgrammar == NULL) {
6963 fprintf(output, "RelaxNG has no top grammar\n");
6964 return;
6965 }
6966 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
6967}
6968
Daniel Veillardfebcca42003-02-16 15:44:18 +00006969/**
6970 * xmlRelaxNGDumpTree:
6971 * @output: the file output
6972 * @schema: a schema structure
6973 *
6974 * Dump the transformed RelaxNG tree.
6975 */
6976void
6977xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
6978{
6979 if (schema == NULL) {
6980 fprintf(output, "RelaxNG empty or failed to compile\n");
6981 return;
6982 }
6983 if (schema->doc == NULL) {
6984 fprintf(output, "no document\n");
6985 } else {
6986 xmlDocDump(output, schema->doc);
6987 }
6988}
6989
Daniel Veillard6eadf632003-01-23 18:29:16 +00006990/************************************************************************
6991 * *
6992 * Validation implementation *
6993 * *
6994 ************************************************************************/
Daniel Veillardfd573f12003-03-16 17:52:32 +00006995static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
6996 xmlRelaxNGDefinePtr define);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00006997static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
6998 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006999
7000/**
7001 * xmlRelaxNGSkipIgnored:
7002 * @ctxt: a schema validation context
7003 * @node: the top node.
7004 *
7005 * Skip ignorable nodes in that context
7006 *
7007 * Returns the new sibling or NULL in case of error.
7008 */
7009static xmlNodePtr
7010xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
7011 xmlNodePtr node) {
7012 /*
7013 * TODO complete and handle entities
7014 */
7015 while ((node != NULL) &&
7016 ((node->type == XML_COMMENT_NODE) ||
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007017 (node->type == XML_PI_NODE) ||
Daniel Veillard39eb88b2003-03-11 11:21:28 +00007018 (((node->type == XML_TEXT_NODE) ||
7019 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007020 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
7021 (IS_BLANK_NODE(node)))))) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007022 node = node->next;
7023 }
7024 return(node);
7025}
7026
7027/**
Daniel Veillardedc91922003-01-26 00:52:04 +00007028 * xmlRelaxNGNormalize:
7029 * @ctxt: a schema validation context
7030 * @str: the string to normalize
7031 *
7032 * Implements the normalizeWhiteSpace( s ) function from
7033 * section 6.2.9 of the spec
7034 *
7035 * Returns the new string or NULL in case of error.
7036 */
7037static xmlChar *
7038xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) {
7039 xmlChar *ret, *p;
7040 const xmlChar *tmp;
7041 int len;
7042
7043 if (str == NULL)
7044 return(NULL);
7045 tmp = str;
7046 while (*tmp != 0) tmp++;
7047 len = tmp - str;
7048
7049 ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar));
7050 if (ret == NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00007051 if (ctxt != NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007052 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
Daniel Veillardea3f3982003-01-26 19:45:18 +00007053 } else {
7054 xmlGenericError(xmlGenericErrorContext,
7055 "xmlRelaxNGNormalize: out of memory\n");
7056 }
Daniel Veillardedc91922003-01-26 00:52:04 +00007057 return(NULL);
7058 }
7059 p = ret;
7060 while (IS_BLANK(*str)) str++;
7061 while (*str != 0) {
7062 if (IS_BLANK(*str)) {
7063 while (IS_BLANK(*str)) str++;
7064 if (*str == 0)
7065 break;
7066 *p++ = ' ';
7067 } else
7068 *p++ = *str++;
7069 }
7070 *p = 0;
7071 return(ret);
7072}
7073
7074/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007075 * xmlRelaxNGValidateDatatype:
7076 * @ctxt: a Relax-NG validation context
7077 * @value: the string value
7078 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007079 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007080 *
7081 * Validate the given value against the dataype
7082 *
7083 * Returns 0 if the validation succeeded or an error code.
7084 */
7085static int
7086xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007087 xmlRelaxNGDefinePtr define, xmlNodePtr node) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007088 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007089 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007090 void *result = NULL;
7091 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007092
7093 if ((define == NULL) || (define->data == NULL)) {
7094 return(-1);
7095 }
7096 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007097 if (lib->check != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007098 if ((define->attrs != NULL) &&
7099 (define->attrs->type == XML_RELAXNG_PARAM)) {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007100 ret = lib->check(lib->data, define->name, value, &result, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007101 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007102 ret = lib->check(lib->data, define->name, value, NULL, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007103 }
7104 } else
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007105 ret = -1;
7106 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007107 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007108 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
7109 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007110 return(-1);
7111 } else if (ret == 1) {
7112 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007113 } else if (ret == 2) {
7114 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007115 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007116 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007117 ret = -1;
7118 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007119 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007120 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
7121 if (lib->facet != NULL) {
7122 tmp = lib->facet(lib->data, define->name, cur->name,
7123 cur->value, value, result);
7124 if (tmp != 0)
7125 ret = -1;
7126 }
7127 cur = cur->next;
7128 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007129 if ((ret == 0) && (define->content != NULL)) {
7130 const xmlChar *oldvalue, *oldendvalue;
7131
7132 oldvalue = ctxt->state->value;
7133 oldendvalue = ctxt->state->endvalue;
7134 ctxt->state->value = (xmlChar *) value;
7135 ctxt->state->endvalue = NULL;
7136 ret = xmlRelaxNGValidateValue(ctxt, define->content);
7137 ctxt->state->value = (xmlChar *) oldvalue;
7138 ctxt->state->endvalue = (xmlChar *) oldendvalue;
7139 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007140 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
7141 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007142 return(ret);
7143}
7144
7145/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007146 * xmlRelaxNGNextValue:
7147 * @ctxt: a Relax-NG validation context
7148 *
7149 * Skip to the next value when validating within a list
7150 *
7151 * Returns 0 if the operation succeeded or an error code.
7152 */
7153static int
7154xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) {
7155 xmlChar *cur;
7156
7157 cur = ctxt->state->value;
7158 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
7159 ctxt->state->value = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00007160 ctxt->state->endvalue = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007161 return(0);
7162 }
7163 while (*cur != 0) cur++;
7164 while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++;
7165 if (cur == ctxt->state->endvalue)
7166 ctxt->state->value = NULL;
7167 else
7168 ctxt->state->value = cur;
7169 return(0);
7170}
7171
7172/**
7173 * xmlRelaxNGValidateValueList:
7174 * @ctxt: a Relax-NG validation context
7175 * @defines: the list of definitions to verify
7176 *
7177 * Validate the given set of definitions for the current value
7178 *
7179 * Returns 0 if the validation succeeded or an error code.
7180 */
7181static int
7182xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
7183 xmlRelaxNGDefinePtr defines) {
7184 int ret = 0;
7185
7186 while (defines != NULL) {
7187 ret = xmlRelaxNGValidateValue(ctxt, defines);
7188 if (ret != 0)
7189 break;
7190 defines = defines->next;
7191 }
7192 return(ret);
7193}
7194
7195/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00007196 * xmlRelaxNGValidateValue:
7197 * @ctxt: a Relax-NG validation context
7198 * @define: the definition to verify
7199 *
7200 * Validate the given definition for the current value
7201 *
7202 * Returns 0 if the validation succeeded or an error code.
7203 */
7204static int
7205xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
7206 xmlRelaxNGDefinePtr define) {
Daniel Veillardedc91922003-01-26 00:52:04 +00007207 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007208 xmlChar *value;
7209
7210 value = ctxt->state->value;
7211 switch (define->type) {
Daniel Veillardd4310742003-02-18 21:12:46 +00007212 case XML_RELAXNG_EMPTY: {
7213 if ((value != NULL) && (value[0] != 0)) {
7214 int idx = 0;
7215
7216 while (IS_BLANK(value[idx]))
7217 idx++;
7218 if (value[idx] != 0)
7219 ret = -1;
7220 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007221 break;
Daniel Veillardd4310742003-02-18 21:12:46 +00007222 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007223 case XML_RELAXNG_TEXT:
7224 break;
Daniel Veillardedc91922003-01-26 00:52:04 +00007225 case XML_RELAXNG_VALUE: {
7226 if (!xmlStrEqual(value, define->value)) {
7227 if (define->name != NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00007228 xmlRelaxNGTypeLibraryPtr lib;
7229
7230 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
7231 if ((lib != NULL) && (lib->comp != NULL))
7232 ret = lib->comp(lib->data, define->name, value,
7233 define->value);
7234 else
7235 ret = -1;
7236 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007237 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name);
Daniel Veillardea3f3982003-01-26 19:45:18 +00007238 return(-1);
7239 } else if (ret == 1) {
7240 ret = 0;
7241 } else {
7242 ret = -1;
7243 }
Daniel Veillardedc91922003-01-26 00:52:04 +00007244 } else {
7245 xmlChar *nval, *nvalue;
7246
7247 /*
7248 * TODO: trivial optimizations are possible by
7249 * computing at compile-time
7250 */
7251 nval = xmlRelaxNGNormalize(ctxt, define->value);
7252 nvalue = xmlRelaxNGNormalize(ctxt, value);
7253
Daniel Veillardea3f3982003-01-26 19:45:18 +00007254 if ((nval == NULL) || (nvalue == NULL) ||
7255 (!xmlStrEqual(nval, nvalue)))
Daniel Veillardedc91922003-01-26 00:52:04 +00007256 ret = -1;
7257 if (nval != NULL)
7258 xmlFree(nval);
7259 if (nvalue != NULL)
7260 xmlFree(nvalue);
7261 }
7262 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007263 if (ret == 0)
7264 xmlRelaxNGNextValue(ctxt);
Daniel Veillardedc91922003-01-26 00:52:04 +00007265 break;
7266 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007267 case XML_RELAXNG_DATATYPE: {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007268 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
7269 ctxt->state->seq);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007270 if (ret == 0)
7271 xmlRelaxNGNextValue(ctxt);
7272
7273 break;
7274 }
7275 case XML_RELAXNG_CHOICE: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007276 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007277 xmlChar *oldvalue;
7278
7279 oldflags = ctxt->flags;
7280 ctxt->flags |= FLAGS_IGNORABLE;
7281
7282 oldvalue = ctxt->state->value;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007283 while (list != NULL) {
7284 ret = xmlRelaxNGValidateValue(ctxt, list);
7285 if (ret == 0) {
7286 break;
7287 }
7288 ctxt->state->value = oldvalue;
7289 list = list->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007290 }
7291 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00007292 if (ret != 0) {
7293 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7294 xmlRelaxNGDumpValidError(ctxt);
7295 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007296 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00007297 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007298 if (ret == 0)
7299 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007300 break;
7301 }
7302 case XML_RELAXNG_LIST: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007303 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007304 xmlChar *oldvalue, *oldend, *val, *cur;
Daniel Veillard416589a2003-02-17 17:25:42 +00007305#ifdef DEBUG_LIST
7306 int nb_values = 0;
7307#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007308
7309 oldvalue = ctxt->state->value;
7310 oldend = ctxt->state->endvalue;
7311
7312 val = xmlStrdup(oldvalue);
7313 if (val == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00007314 val = xmlStrdup(BAD_CAST "");
7315 }
7316 if (val == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007317 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007318 return(-1);
7319 }
7320 cur = val;
7321 while (*cur != 0) {
Daniel Veillard416589a2003-02-17 17:25:42 +00007322 if (IS_BLANK(*cur)) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007323 *cur = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00007324 cur++;
7325#ifdef DEBUG_LIST
7326 nb_values++;
7327#endif
7328 while (IS_BLANK(*cur))
7329 *cur++ = 0;
7330 } else
7331 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007332 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007333#ifdef DEBUG_LIST
7334 xmlGenericError(xmlGenericErrorContext,
7335 "list value: '%s' found %d items\n", oldvalue, nb_values);
7336 nb_values = 0;
7337#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007338 ctxt->state->endvalue = cur;
7339 cur = val;
7340 while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007341
Daniel Veillardfd573f12003-03-16 17:52:32 +00007342 ctxt->state->value = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007343
Daniel Veillardfd573f12003-03-16 17:52:32 +00007344 while (list != NULL) {
7345 if (ctxt->state->value == ctxt->state->endvalue)
7346 ctxt->state->value = NULL;
7347 ret = xmlRelaxNGValidateValue(ctxt, list);
7348 if (ret != 0) {
7349#ifdef DEBUG_LIST
7350 xmlGenericError(xmlGenericErrorContext,
7351 "Failed to validate value: '%s' with %d rule\n",
7352 ctxt->state->value, nb_values);
7353#endif
7354 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007355 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007356#ifdef DEBUG_LIST
7357 nb_values++;
7358#endif
7359 list = list->next;
7360 }
7361
7362 if ((ret == 0) && (ctxt->state->value != NULL) &&
7363 (ctxt->state->value != ctxt->state->endvalue)) {
7364 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value);
7365 ret = -1;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007366 }
7367 xmlFree(val);
7368 ctxt->state->value = oldvalue;
7369 ctxt->state->endvalue = oldend;
7370 break;
7371 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007372 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007373 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
7374 if (ret != 0) {
7375 break;
7376 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007377 /* no break on purpose */
7378 case XML_RELAXNG_ZEROORMORE: {
7379 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007380
7381 oldflags = ctxt->flags;
7382 ctxt->flags |= FLAGS_IGNORABLE;
7383 cur = ctxt->state->value;
7384 temp = NULL;
7385 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
7386 (temp != cur)) {
7387 temp = cur;
7388 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
7389 if (ret != 0) {
7390 ctxt->state->value = temp;
7391 ret = 0;
7392 break;
7393 }
7394 cur = ctxt->state->value;
7395 }
7396 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00007397 if (ret != 0) {
7398 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7399 xmlRelaxNGDumpValidError(ctxt);
7400 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007401 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00007402 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007403 break;
7404 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007405 case XML_RELAXNG_EXCEPT: {
7406 xmlRelaxNGDefinePtr list;
7407
7408 list = define->content;
7409 while (list != NULL) {
7410 ret = xmlRelaxNGValidateValue(ctxt, list);
7411 if (ret == 0) {
7412 ret = -1;
7413 break;
7414 } else
7415 ret = 0;
7416 list = list->next;
7417 }
7418 break;
7419 }
Daniel Veillard463a5472003-02-27 21:30:32 +00007420 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007421 case XML_RELAXNG_GROUP: {
7422 xmlRelaxNGDefinePtr list;
7423
7424 list = define->content;
7425 while (list != NULL) {
7426 ret = xmlRelaxNGValidateValue(ctxt, list);
7427 if (ret != 0) {
7428 ret = -1;
7429 break;
7430 } else
7431 ret = 0;
7432 list = list->next;
7433 }
Daniel Veillardd4310742003-02-18 21:12:46 +00007434 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007435 }
Daniel Veillard463a5472003-02-27 21:30:32 +00007436 case XML_RELAXNG_REF:
7437 case XML_RELAXNG_PARENTREF:
7438 ret = xmlRelaxNGValidateValue(ctxt, define->content);
7439 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007440 default:
7441 TODO
7442 ret = -1;
7443 }
7444 return(ret);
7445}
7446
7447/**
7448 * xmlRelaxNGValidateValueContent:
7449 * @ctxt: a Relax-NG validation context
7450 * @defines: the list of definitions to verify
7451 *
7452 * Validate the given definitions for the current value
7453 *
7454 * Returns 0 if the validation succeeded or an error code.
7455 */
7456static int
7457xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
7458 xmlRelaxNGDefinePtr defines) {
7459 int ret = 0;
7460
7461 while (defines != NULL) {
7462 ret = xmlRelaxNGValidateValue(ctxt, defines);
7463 if (ret != 0)
7464 break;
7465 defines = defines->next;
7466 }
7467 return(ret);
7468}
7469
7470/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007471 * xmlRelaxNGAttributeMatch:
7472 * @ctxt: a Relax-NG validation context
7473 * @define: the definition to check
7474 * @prop: the attribute
7475 *
7476 * Check if the attribute matches the definition nameClass
7477 *
7478 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
7479 */
7480static int
7481xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
7482 xmlRelaxNGDefinePtr define,
7483 xmlAttrPtr prop) {
7484 int ret;
7485
7486 if (define->name != NULL) {
7487 if (!xmlStrEqual(define->name, prop->name))
7488 return(0);
7489 }
7490 if (define->ns != NULL) {
7491 if (define->ns[0] == 0) {
7492 if (prop->ns != NULL)
7493 return(0);
7494 } else {
7495 if ((prop->ns == NULL) ||
7496 (!xmlStrEqual(define->ns, prop->ns->href)))
7497 return(0);
7498 }
7499 }
7500 if (define->nameClass == NULL)
7501 return(1);
7502 define = define->nameClass;
7503 if (define->type == XML_RELAXNG_EXCEPT) {
7504 xmlRelaxNGDefinePtr list;
7505
7506 list = define->content;
7507 while (list != NULL) {
7508 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
7509 if (ret == 1)
7510 return(0);
7511 if (ret < 0)
7512 return(ret);
7513 list = list->next;
7514 }
7515 } else {
7516 TODO
7517 }
7518 return(1);
7519}
7520
7521/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00007522 * xmlRelaxNGValidateAttribute:
7523 * @ctxt: a Relax-NG validation context
7524 * @define: the definition to verify
7525 *
7526 * Validate the given attribute definition for that node
7527 *
7528 * Returns 0 if the validation succeeded or an error code.
7529 */
7530static int
7531xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
7532 xmlRelaxNGDefinePtr define) {
7533 int ret = 0, i;
7534 xmlChar *value, *oldvalue;
7535 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007536 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007537
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007538 if (ctxt->state->nbAttrLeft <= 0)
7539 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007540 if (define->name != NULL) {
7541 for (i = 0;i < ctxt->state->nbAttrs;i++) {
7542 tmp = ctxt->state->attrs[i];
7543 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
7544 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
7545 (tmp->ns == NULL)) ||
7546 ((tmp->ns != NULL) &&
7547 (xmlStrEqual(define->ns, tmp->ns->href)))) {
7548 prop = tmp;
7549 break;
7550 }
7551 }
7552 }
7553 if (prop != NULL) {
7554 value = xmlNodeListGetString(prop->doc, prop->children, 1);
7555 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007556 oldseq = ctxt->state->seq;
7557 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007558 ctxt->state->value = value;
Daniel Veillard231d7912003-02-09 14:22:17 +00007559 ctxt->state->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007560 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00007561 if (ctxt->state->value != NULL)
7562 value = ctxt->state->value;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007563 if (value != NULL)
7564 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00007565 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007566 ctxt->state->seq = oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007567 if (ret == 0) {
7568 /*
7569 * flag the attribute as processed
7570 */
7571 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007572 ctxt->state->nbAttrLeft--;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007573 }
7574 } else {
7575 ret = -1;
7576 }
7577#ifdef DEBUG
7578 xmlGenericError(xmlGenericErrorContext,
7579 "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret);
7580#endif
7581 } else {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007582 for (i = 0;i < ctxt->state->nbAttrs;i++) {
7583 tmp = ctxt->state->attrs[i];
Daniel Veillard144fae12003-02-03 13:17:57 +00007584 if ((tmp != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00007585 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007586 prop = tmp;
7587 break;
7588 }
7589 }
7590 if (prop != NULL) {
7591 value = xmlNodeListGetString(prop->doc, prop->children, 1);
7592 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007593 oldseq = ctxt->state->seq;
7594 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007595 ctxt->state->value = value;
7596 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00007597 if (ctxt->state->value != NULL)
7598 value = ctxt->state->value;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007599 if (value != NULL)
7600 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00007601 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007602 ctxt->state->seq = oldseq;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007603 if (ret == 0) {
7604 /*
7605 * flag the attribute as processed
7606 */
7607 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007608 ctxt->state->nbAttrLeft--;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007609 }
7610 } else {
7611 ret = -1;
7612 }
7613#ifdef DEBUG
Daniel Veillard144fae12003-02-03 13:17:57 +00007614 if (define->ns != NULL) {
7615 xmlGenericError(xmlGenericErrorContext,
7616 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
7617 define->ns, ret);
7618 } else {
7619 xmlGenericError(xmlGenericErrorContext,
7620 "xmlRelaxNGValidateAttribute(anyName): %d\n",
7621 ret);
7622 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007623#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00007624 }
7625
7626 return(ret);
7627}
7628
7629/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007630 * xmlRelaxNGValidateAttributeList:
7631 * @ctxt: a Relax-NG validation context
7632 * @define: the list of definition to verify
7633 *
7634 * Validate the given node against the list of attribute definitions
7635 *
7636 * Returns 0 if the validation succeeded or an error code.
7637 */
7638static int
7639xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7640 xmlRelaxNGDefinePtr defines) {
7641 int ret = 0;
7642 while (defines != NULL) {
7643 if (xmlRelaxNGValidateAttribute(ctxt, defines) != 0)
7644 ret = -1;
7645 defines = defines->next;
7646 }
7647 return(ret);
7648}
7649
7650/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00007651 * xmlRelaxNGNodeMatchesList:
7652 * @node: the node
7653 * @list: a NULL terminated array of definitions
7654 *
7655 * Check if a node can be matched by one of the definitions
7656 *
7657 * Returns 1 if matches 0 otherwise
7658 */
7659static int
7660xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) {
7661 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00007662 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00007663
7664 if ((node == NULL) || (list == NULL))
7665 return(0);
7666
7667 cur = list[i++];
7668 while (cur != NULL) {
7669 if ((node->type == XML_ELEMENT_NODE) &&
7670 (cur->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00007671 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
7672 if (tmp == 1)
7673 return(1);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00007674 } else if (((node->type == XML_TEXT_NODE) ||
7675 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00007676 (cur->type == XML_RELAXNG_TEXT)) {
7677 return(1);
7678 }
7679 cur = list[i++];
7680 }
7681 return(0);
7682}
7683
7684/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007685 * xmlRelaxNGValidateInterleave:
7686 * @ctxt: a Relax-NG validation context
7687 * @define: the definition to verify
7688 *
7689 * Validate an interleave definition for a node.
7690 *
7691 * Returns 0 if the validation succeeded or an error code.
7692 */
7693static int
7694xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
7695 xmlRelaxNGDefinePtr define) {
7696 int ret = 0, i, nbgroups, left;
7697 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007698 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007699
7700 xmlRelaxNGValidStatePtr oldstate;
7701 xmlRelaxNGPartitionPtr partitions;
7702 xmlRelaxNGInterleaveGroupPtr group = NULL;
7703 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
7704 xmlNodePtr *list = NULL, *lasts = NULL;
7705
7706 if (define->data != NULL) {
7707 partitions = (xmlRelaxNGPartitionPtr) define->data;
7708 nbgroups = partitions->nbgroups;
7709 left = nbgroups;
7710 } else {
7711 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
7712 return(-1);
7713 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007714 /*
7715 * Optimizations for MIXED
7716 */
7717 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00007718 if (define->dflags & IS_MIXED) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007719 ctxt->flags |= FLAGS_MIXED_CONTENT;
7720 if (nbgroups == 2) {
7721 /*
7722 * this is a pure <mixed> case
7723 */
7724 if (ctxt->state != NULL)
7725 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
7726 ctxt->state->seq);
7727 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
7728 ret = xmlRelaxNGValidateDefinition(ctxt,
7729 partitions->groups[1]->rule);
7730 else
7731 ret = xmlRelaxNGValidateDefinition(ctxt,
7732 partitions->groups[0]->rule);
7733 if (ret == 0) {
7734 if (ctxt->state != NULL)
7735 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
7736 ctxt->state->seq);
7737 }
7738 ctxt->flags = oldflags;
7739 return(ret);
7740 }
7741 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007742
7743 /*
7744 * Build arrays to store the first and last node of the chain
7745 * pertaining to each group
7746 */
7747 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
7748 if (list == NULL) {
7749 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7750 return(-1);
7751 }
7752 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
7753 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
7754 if (lasts == NULL) {
7755 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7756 return(-1);
7757 }
7758 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
7759
7760 /*
7761 * Walk the sequence of children finding the right group and
7762 * sorting them in sequences.
7763 */
7764 cur = ctxt->state->seq;
7765 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
7766 start = cur;
7767 while (cur != NULL) {
7768 ctxt->state->seq = cur;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00007769 if ((partitions->triage != NULL) &&
7770 (partitions->flags & IS_DETERMINIST)) {
7771 void *tmp = NULL;
7772
7773 if ((cur->type == XML_TEXT_NODE) ||
7774 (cur->type == XML_CDATA_SECTION_NODE)) {
7775 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
7776 NULL);
7777 } else if (cur->type == XML_ELEMENT_NODE) {
7778 if (cur->ns != NULL) {
7779 tmp = xmlHashLookup2(partitions->triage, cur->name,
7780 cur->ns->href);
7781 if (tmp == NULL)
7782 tmp = xmlHashLookup2(partitions->triage,
7783 BAD_CAST "#any", cur->ns->href);
7784 } else
7785 tmp = xmlHashLookup2(partitions->triage, cur->name, NULL);
7786 if (tmp == NULL)
7787 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any",
7788 NULL);
7789 }
7790
7791 if (tmp == NULL) {
7792 i = nbgroups;
7793 } else {
7794 i = ((long) tmp) - 1;
7795 if (partitions->flags & IS_NEEDCHECK) {
7796 group = partitions->groups[i];
7797 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
7798 i = nbgroups;
7799 }
7800 }
7801 } else {
7802 for (i = 0;i < nbgroups;i++) {
7803 group = partitions->groups[i];
7804 if (group == NULL)
7805 continue;
7806 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
7807 break;
7808 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007809 }
7810 /*
7811 * We break as soon as an element not matched is found
7812 */
7813 if (i >= nbgroups) {
7814 break;
7815 }
7816 if (lasts[i] != NULL) {
7817 lasts[i]->next = cur;
7818 lasts[i] = cur;
7819 } else {
7820 list[i] = cur;
7821 lasts[i] = cur;
7822 }
7823 if (cur->next != NULL)
7824 lastchg = cur->next;
7825 else
7826 lastchg = cur;
7827 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
7828 }
7829 if (ret != 0) {
7830 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
7831 ret = -1;
7832 goto done;
7833 }
7834 lastelem = cur;
7835 oldstate = ctxt->state;
7836 for (i = 0;i < nbgroups;i++) {
7837 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
7838 group = partitions->groups[i];
7839 if (lasts[i] != NULL) {
7840 last = lasts[i]->next;
7841 lasts[i]->next = NULL;
7842 }
7843 ctxt->state->seq = list[i];
7844 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
7845 if (ret != 0)
7846 break;
7847 if (ctxt->state != NULL) {
7848 cur = ctxt->state->seq;
7849 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
Daniel Veillard798024a2003-03-19 10:36:09 +00007850 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007851 oldstate = ctxt->state;
7852 ctxt->state = NULL;
7853 if (cur != NULL) {
7854 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
7855 ret = -1;
7856 ctxt->state = oldstate;
7857 goto done;
7858 }
7859 } else if (ctxt->states != NULL) {
7860 int j;
7861 int found = 0;
7862
7863 for (j = 0;j < ctxt->states->nbState;j++) {
7864 cur = ctxt->states->tabState[j]->seq;
7865 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
7866 if (cur == NULL) {
7867 found = 1;
7868 break;
7869 }
7870 }
7871 if (ctxt->states->nbState > 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00007872 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007873 oldstate = ctxt->states->tabState[ctxt->states->nbState - 1];
7874 }
7875 for (j = 0;j < ctxt->states->nbState - 1;j++) {
Daniel Veillard798024a2003-03-19 10:36:09 +00007876 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007877 }
7878 xmlRelaxNGFreeStates(ctxt, ctxt->states);
7879 ctxt->states = NULL;
7880 if (found == 0) {
7881 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
7882 ret = -1;
7883 ctxt->state = oldstate;
7884 goto done;
7885 }
7886 } else {
7887 ret = -1;
7888 break;
7889 }
7890 if (lasts[i] != NULL) {
7891 lasts[i]->next = last;
7892 }
7893 }
7894 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00007895 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007896 ctxt->state = oldstate;
7897 ctxt->state->seq = lastelem;
7898 if (ret != 0) {
7899 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
7900 ret = -1;
7901 goto done;
7902 }
7903
7904done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007905 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007906 /*
7907 * builds the next links chain from the prev one
7908 */
7909 cur = lastchg;
7910 while (cur != NULL) {
7911 if ((cur == start) || (cur->prev == NULL))
7912 break;
7913 cur->prev->next = cur;
7914 cur = cur->prev;
7915 }
7916 if (ret == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007917 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007918 }
7919
7920 xmlFree(list);
7921 xmlFree(lasts);
7922 return(ret);
7923}
7924
7925/**
7926 * xmlRelaxNGValidateDefinitionList:
7927 * @ctxt: a Relax-NG validation context
7928 * @define: the list of definition to verify
7929 *
7930 * Validate the given node content against the (list) of definitions
7931 *
7932 * Returns 0 if the validation succeeded or an error code.
7933 */
7934static int
7935xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
7936 xmlRelaxNGDefinePtr defines) {
7937 int ret = 0, res;
7938
7939
Daniel Veillard952379b2003-03-17 15:37:12 +00007940 if (defines == NULL) {
7941 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list");
7942 return(-1);
7943 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007944 while (defines != NULL) {
7945 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
7946 res = xmlRelaxNGValidateDefinition(ctxt, defines);
7947 if (res < 0)
7948 ret = -1;
7949 } else {
7950 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
7951 return(-1);
7952 }
7953 if (ret < 0)
7954 break;
7955 defines = defines->next;
7956 }
7957
7958 return(ret);
7959}
7960
7961/**
7962 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00007963 * @ctxt: a Relax-NG validation context
7964 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00007965 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00007966 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00007967 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00007968 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00007969 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00007970 */
7971static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00007972xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
7973 xmlRelaxNGDefinePtr define,
7974 xmlNodePtr elem) {
Daniel Veillard580ced82003-03-21 21:22:48 +00007975 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00007976
Daniel Veillardfd573f12003-03-16 17:52:32 +00007977 if (define->name != NULL) {
7978 if (!xmlStrEqual(elem->name, define->name)) {
7979 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
7980 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007981 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007982 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007983 if ((define->ns != NULL) && (define->ns[0] != 0)) {
7984 if (elem->ns == NULL) {
7985 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS,
7986 elem->name);
7987 return(0);
7988 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
7989 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
7990 elem->name, define->ns);
7991 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007992 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007993 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
7994 (define->name == NULL)) {
7995 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
7996 elem->name);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007997 return(0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007998 } else if ((elem->ns != NULL) && (define->name != NULL)) {
7999 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
8000 define->name);
8001 return(0);
8002 }
8003
8004 if (define->nameClass == NULL)
8005 return(1);
8006
8007 define = define->nameClass;
8008 if (define->type == XML_RELAXNG_EXCEPT) {
8009 xmlRelaxNGDefinePtr list;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008010 if (ctxt != NULL) {
8011 oldflags = ctxt->flags;
8012 ctxt->flags |= FLAGS_IGNORABLE;
8013 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008014
8015 list = define->content;
8016 while (list != NULL) {
8017 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
8018 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008019 if (ctxt != NULL)
8020 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008021 return(0);
8022 }
8023 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008024 if (ctxt != NULL)
8025 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008026 return(ret);
8027 }
8028 list = list->next;
8029 }
8030 ret = 1;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008031 if (ctxt != NULL) {
8032 ctxt->flags = oldflags;
8033 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008034 } else if (define->type == XML_RELAXNG_CHOICE) {
8035 xmlRelaxNGDefinePtr list;
8036
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008037 if (ctxt != NULL) {
8038 oldflags = ctxt->flags;
8039 ctxt->flags |= FLAGS_IGNORABLE;
8040 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008041
8042 list = define->nameClass;
8043 while (list != NULL) {
8044 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
8045 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008046 if (ctxt != NULL)
8047 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008048 return(1);
8049 }
8050 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008051 if (ctxt != NULL)
8052 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008053 return(ret);
8054 }
8055 list = list->next;
8056 }
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008057 if (ctxt != NULL) {
8058 if (ret != 0) {
8059 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8060 xmlRelaxNGDumpValidError(ctxt);
8061 } else {
8062 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
8063 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008064 }
8065 ret = 0;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008066 if (ctxt != NULL) {
8067 ctxt->flags = oldflags;
8068 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008069 } else {
8070 TODO
8071 ret = -1;
8072 }
8073 return(ret);
8074}
8075
8076/**
8077 * xmlRelaxNGValidateElementEnd:
8078 * @ctxt: a Relax-NG validation context
8079 *
8080 * Validate the end of the element, implements check that
8081 * there is nothing left not consumed in the element content
8082 * or in the attribute list.
8083 *
8084 * Returns 0 if the validation succeeded or an error code.
8085 */
8086static int
8087xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) {
8088 int ret = 0, i;
8089 xmlRelaxNGValidStatePtr state;
8090
8091 state = ctxt->state;
8092 if (state->seq != NULL) {
8093 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
8094 if (state->seq != NULL) {
8095 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
8096 state->node->name, state->seq->name);
8097 ret = -1;
8098 }
8099 }
8100 for (i = 0;i < state->nbAttrs;i++) {
8101 if (state->attrs[i] != NULL) {
8102 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
8103 state->attrs[i]->name, state->node->name);
8104 ret = -1;
8105 }
8106 }
8107 return(ret);
8108}
8109
8110/**
8111 * xmlRelaxNGValidateState:
8112 * @ctxt: a Relax-NG validation context
8113 * @define: the definition to verify
8114 *
8115 * Validate the current state against the definition
8116 *
8117 * Returns 0 if the validation succeeded or an error code.
8118 */
8119static int
8120xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
8121 xmlRelaxNGDefinePtr define) {
8122 xmlNodePtr node;
8123 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00008124 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008125
8126 if (define == NULL) {
8127 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8128 return(-1);
8129 }
8130
8131 if (ctxt->state != NULL) {
8132 node = ctxt->state->seq;
8133 } else {
8134 node = NULL;
8135 }
8136#ifdef DEBUG
8137 for (i = 0;i < ctxt->depth;i++)
8138 xmlGenericError(xmlGenericErrorContext, " ");
8139 xmlGenericError(xmlGenericErrorContext,
8140 "Start validating %s ", xmlRelaxNGDefName(define));
8141 if (define->name != NULL)
8142 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
8143 if ((node != NULL) && (node->name != NULL))
8144 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
8145 else
8146 xmlGenericError(xmlGenericErrorContext, "\n");
8147#endif
8148 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008149 switch (define->type) {
8150 case XML_RELAXNG_EMPTY:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008151 node = xmlRelaxNGSkipIgnored(ctxt, node);
8152 ret = 0;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008153 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008154 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008155 ret = -1;
8156 break;
8157 case XML_RELAXNG_TEXT:
8158 while ((node != NULL) &&
8159 ((node->type == XML_TEXT_NODE) ||
8160 (node->type == XML_COMMENT_NODE) ||
8161 (node->type == XML_PI_NODE) ||
8162 (node->type == XML_CDATA_SECTION_NODE)))
8163 node = node->next;
8164 ctxt->state->seq = node;
8165 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008166 case XML_RELAXNG_ELEMENT:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008167 errNr = ctxt->errNr;
8168 node = xmlRelaxNGSkipIgnored(ctxt, node);
8169 if (node == NULL) {
8170 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
8171 ret = -1;
8172 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8173 xmlRelaxNGDumpValidError(ctxt);
8174 break;
8175 }
8176 if (node->type != XML_ELEMENT_NODE) {
8177 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8178 ret = -1;
8179 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8180 xmlRelaxNGDumpValidError(ctxt);
8181 break;
8182 }
8183 /*
8184 * This node was already validated successfully against
8185 * this definition.
8186 */
8187 if (node->_private == define) {
8188 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
Daniel Veillard580ced82003-03-21 21:22:48 +00008189 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008190 if (ctxt->errNr != 0) {
8191 while ((ctxt->err != NULL) &&
8192 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
8193 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
Daniel Veillard580ced82003-03-21 21:22:48 +00008194 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
8195 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008196 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
8197 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
8198 xmlRelaxNGValidErrorPop(ctxt);
8199 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008200 break;
8201 }
8202
8203 ret = xmlRelaxNGElementMatch(ctxt, define, node);
8204 if (ret <= 0) {
8205 ret = -1;
8206 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8207 xmlRelaxNGDumpValidError(ctxt);
8208 break;
8209 }
8210 ret = 0;
8211 if (ctxt->errNr != 0) {
Daniel Veillard580ced82003-03-21 21:22:48 +00008212 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008213 while ((ctxt->err != NULL) &&
8214 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
8215 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
Daniel Veillard580ced82003-03-21 21:22:48 +00008216 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
8217 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00008218 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
8219 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
8220 xmlRelaxNGValidErrorPop(ctxt);
8221 }
8222 errNr = ctxt->errNr;
8223
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008224 oldflags = ctxt->flags;
8225 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
8226 ctxt->flags -= FLAGS_MIXED_CONTENT;
8227 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008228 state = xmlRelaxNGNewValidState(ctxt, node);
8229 if (state == NULL) {
8230 ret = -1;
8231 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8232 xmlRelaxNGDumpValidError(ctxt);
8233 break;
8234 }
8235
8236 oldstate = ctxt->state;
8237 ctxt->state = state;
8238 if (define->attrs != NULL) {
8239 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8240 if (tmp != 0) {
8241 ret = -1;
8242 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8243 }
8244 }
8245 if (define->content != NULL) {
8246 tmp = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8247 if (tmp != 0) {
8248 ret = -1;
8249 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name);
8250 }
8251 }
8252 if (ctxt->states != NULL) {
8253 tmp = -1;
8254
Daniel Veillardfd573f12003-03-16 17:52:32 +00008255 ctxt->flags |= FLAGS_IGNORABLE;
8256
8257 for (i = 0;i < ctxt->states->nbState;i++) {
8258 state = ctxt->states->tabState[i];
8259 ctxt->state = state;
8260
8261 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
8262 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +00008263 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008264 }
8265 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8266 ctxt->flags = oldflags;
8267 ctxt->states = NULL;
8268 if ((ret == 0) && (tmp == -1))
8269 ret = -1;
8270 } else {
8271 state = ctxt->state;
8272 if (ret == 0)
8273 ret = xmlRelaxNGValidateElementEnd(ctxt);
Daniel Veillard798024a2003-03-19 10:36:09 +00008274 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008275 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008276 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008277 ctxt->state = oldstate;
8278 if (oldstate != NULL)
8279 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
8280 if (ret == 0) {
8281 node->_private = define;
8282 }
8283 if (ret != 0) {
8284 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8285 xmlRelaxNGDumpValidError(ctxt);
8286 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008287 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008288 }
8289
8290#ifdef DEBUG
8291 xmlGenericError(xmlGenericErrorContext,
8292 "xmlRelaxNGValidateDefinition(): validated %s : %d",
8293 node->name, ret);
8294 if (oldstate == NULL)
8295 xmlGenericError(xmlGenericErrorContext, ": no state\n");
8296 else if (oldstate->seq == NULL)
8297 xmlGenericError(xmlGenericErrorContext, ": done\n");
8298 else if (oldstate->seq->type == XML_ELEMENT_NODE)
8299 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
8300 oldstate->seq->name);
8301 else
8302 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
8303 oldstate->seq->name, oldstate->seq->type);
8304#endif
8305 break;
8306 case XML_RELAXNG_OPTIONAL: {
8307 oldflags = ctxt->flags;
8308 ctxt->flags |= FLAGS_IGNORABLE;
8309 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
8310 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8311 if (ret != 0) {
8312 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00008313 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008314 ctxt->state = oldstate;
8315 ctxt->flags = oldflags;
8316 ret = 0;
8317 break;
8318 }
8319 if (ctxt->states != NULL) {
8320 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
8321 } else {
8322 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
8323 if (ctxt->states == NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008324 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008325 ctxt->flags = oldflags;
8326 ret = -1;
8327 break;
8328 }
8329 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
8330 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
8331 ctxt->state = NULL;
8332 }
8333 ctxt->flags = oldflags;
8334 ret = 0;
8335 break;
8336 }
8337 case XML_RELAXNG_ONEORMORE:
8338 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8339 if (ret != 0) {
8340 break;
8341 }
8342 /* no break on purpose */
8343 case XML_RELAXNG_ZEROORMORE: {
8344 int progress;
8345 xmlRelaxNGStatesPtr states = NULL, res = NULL;
8346 int base, j;
8347
8348 res = xmlRelaxNGNewStates(ctxt, 1);
8349 if (res == NULL) {
8350 ret = -1;
8351 break;
8352 }
8353 /*
8354 * All the input states are also exit states
8355 */
8356 if (ctxt->state != NULL) {
8357 xmlRelaxNGAddStates(ctxt, res,
8358 xmlRelaxNGCopyValidState(ctxt, ctxt->state));
8359 } else {
8360 for (j = 0;j < ctxt->states->nbState;j++) {
8361 xmlRelaxNGAddStates(ctxt, res,
8362 xmlRelaxNGCopyValidState(ctxt,
8363 ctxt->states->tabState[j]));
8364 }
8365 }
8366 oldflags = ctxt->flags;
8367 ctxt->flags |= FLAGS_IGNORABLE;
8368 do {
8369 progress = 0;
8370 base = res->nbState;
8371
8372 if (ctxt->states != NULL) {
8373 states = ctxt->states;
8374 for (i = 0;i < states->nbState;i++) {
8375 ctxt->state = states->tabState[i];
8376 ctxt->states = NULL;
8377 ret = xmlRelaxNGValidateDefinitionList(ctxt,
8378 define->content);
8379 if (ret == 0) {
8380 if (ctxt->state != NULL) {
8381 tmp = xmlRelaxNGAddStates(ctxt, res,
8382 ctxt->state);
8383 ctxt->state = NULL;
8384 if (tmp == 1)
8385 progress = 1;
8386 } else if (ctxt->states != NULL) {
8387 for (j = 0;j < ctxt->states->nbState;j++) {
8388 tmp = xmlRelaxNGAddStates(ctxt, res,
8389 ctxt->states->tabState[j]);
8390 if (tmp == 1)
8391 progress = 1;
8392 }
8393 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8394 ctxt->states = NULL;
8395 }
8396 } else {
8397 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008398 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008399 ctxt->state = NULL;
8400 }
8401 }
8402 }
8403 } else {
8404 ret = xmlRelaxNGValidateDefinitionList(ctxt,
8405 define->content);
8406 if (ret != 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008407 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008408 ctxt->state = NULL;
8409 } else {
8410 base = res->nbState;
8411 if (ctxt->state != NULL) {
8412 tmp = xmlRelaxNGAddStates(ctxt, res,
8413 ctxt->state);
8414 ctxt->state = NULL;
8415 if (tmp == 1)
8416 progress = 1;
8417 } else if (ctxt->states != NULL) {
8418 for (j = 0;j < ctxt->states->nbState;j++) {
8419 tmp = xmlRelaxNGAddStates(ctxt, res,
8420 ctxt->states->tabState[j]);
8421 if (tmp == 1)
8422 progress = 1;
8423 }
8424 if (states == NULL) {
8425 states = ctxt->states;
8426 } else {
8427 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8428 }
8429 ctxt->states = NULL;
8430 }
8431 }
8432 }
8433 if (progress) {
8434 /*
8435 * Collect all the new nodes added at that step
8436 * and make them the new node set
8437 */
8438 if (res->nbState - base == 1) {
8439 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
8440 res->tabState[base]);
8441 } else {
8442 if (states == NULL) {
8443 xmlRelaxNGNewStates(ctxt, res->nbState - base);
8444 }
8445 states->nbState = 0;
8446 for (i = base;i < res->nbState;i++)
8447 xmlRelaxNGAddStates(ctxt, states,
8448 xmlRelaxNGCopyValidState(ctxt,
8449 res->tabState[i]));
8450 ctxt->states = states;
8451 }
8452 }
8453 } while (progress == 1);
8454 if (states != NULL) {
8455 xmlRelaxNGFreeStates(ctxt, states);
8456 }
8457 ctxt->states = res;
8458 ctxt->flags = oldflags;
8459 ret = 0;
8460 break;
8461 }
8462 case XML_RELAXNG_CHOICE: {
Daniel Veillard580ced82003-03-21 21:22:48 +00008463 xmlRelaxNGDefinePtr list = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008464 xmlRelaxNGStatesPtr states = NULL;
8465
Daniel Veillarde063f482003-03-21 16:53:17 +00008466 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008467
Daniel Veillarde063f482003-03-21 16:53:17 +00008468 if ((define->dflags & IS_TRIABLE) && (define->data != NULL)) {
8469 xmlHashTablePtr triage = (xmlHashTablePtr) define->data;
8470
8471 /*
8472 * Something we can optimize cleanly there is only one
8473 * possble branch out !
8474 */
8475 if (node == NULL) {
8476 ret = -1;
8477 break;
8478 }
8479 if ((node->type == XML_TEXT_NODE) ||
8480 (node->type == XML_CDATA_SECTION_NODE)) {
8481 list = xmlHashLookup2(triage, BAD_CAST "#text", NULL);
8482 } else if (node->type == XML_ELEMENT_NODE) {
8483 if (node->ns != NULL) {
8484 list = xmlHashLookup2(triage, node->name,
8485 node->ns->href);
8486 if (list == NULL)
8487 list = xmlHashLookup2(triage, BAD_CAST "#any",
8488 node->ns->href);
8489 } else
8490 list = xmlHashLookup2(triage, node->name, NULL);
8491 if (list == NULL)
8492 list = xmlHashLookup2(triage, BAD_CAST "#any", NULL);
8493 }
8494 if (list == NULL) {
8495 ret = -1;
8496 break;
8497 }
8498 ret = xmlRelaxNGValidateDefinition(ctxt, list);
8499 break;
8500 }
8501
8502 list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008503 oldflags = ctxt->flags;
8504 errNr = ctxt->errNr;
8505 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008506
8507 while (list != NULL) {
8508 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
8509 ret = xmlRelaxNGValidateDefinition(ctxt, list);
8510 if (ret == 0) {
8511 if (states == NULL) {
8512 states = xmlRelaxNGNewStates(ctxt, 1);
8513 }
8514 if (ctxt->state != NULL) {
8515 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
8516 } else if (ctxt->states != NULL) {
8517 for (i = 0;i < ctxt->states->nbState;i++) {
8518 xmlRelaxNGAddStates(ctxt, states,
8519 ctxt->states->tabState[i]);
8520 }
8521 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8522 ctxt->states = NULL;
8523 }
8524 } else {
Daniel Veillard798024a2003-03-19 10:36:09 +00008525 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008526 }
8527 ctxt->state = oldstate;
8528 list = list->next;
8529 }
8530 if (states != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008531 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008532 ctxt->states = states;
8533 ctxt->state = NULL;
8534 ret = 0;
8535 } else {
8536 ctxt->states = NULL;
8537 }
8538 ctxt->flags = oldflags;
8539 if (ret != 0) {
8540 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8541 xmlRelaxNGDumpValidError(ctxt);
8542 } else if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008543 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008544 }
8545 break;
8546 }
8547 case XML_RELAXNG_DEF:
8548 case XML_RELAXNG_GROUP:
8549 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008550 break;
8551 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008552 ret = xmlRelaxNGValidateInterleave(ctxt, define);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008553 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008554 case XML_RELAXNG_ATTRIBUTE:
8555 ret = xmlRelaxNGValidateAttribute(ctxt, define);
8556 break;
8557 case XML_RELAXNG_NOOP:
8558 case XML_RELAXNG_REF:
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008559 case XML_RELAXNG_EXTERNALREF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008560 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
8561 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00008562 case XML_RELAXNG_PARENTREF:
8563 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
8564 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008565 case XML_RELAXNG_DATATYPE: {
8566 xmlNodePtr child;
8567 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008568
Daniel Veillardfd573f12003-03-16 17:52:32 +00008569 child = node;
8570 while (child != NULL) {
8571 if (child->type == XML_ELEMENT_NODE) {
8572 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
8573 node->parent->name);
8574 ret = -1;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008575 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008576 } else if ((child->type == XML_TEXT_NODE) ||
8577 (child->type == XML_CDATA_SECTION_NODE)) {
8578 content = xmlStrcat(content, child->content);
8579 }
8580 /* TODO: handle entities ... */
8581 child = child->next;
8582 }
8583 if (ret == -1) {
8584 if (content != NULL)
8585 xmlFree(content);
8586 break;
8587 }
8588 if (content == NULL) {
8589 content = xmlStrdup(BAD_CAST "");
8590 if (content == NULL) {
8591 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8592 ret = -1;
8593 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008594 }
8595 }
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008596 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
8597 ctxt->state->seq);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008598 if (ret == -1) {
8599 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
8600 } else if (ret == 0) {
8601 ctxt->state->seq = NULL;
8602 }
8603 if (content != NULL)
8604 xmlFree(content);
8605 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008606 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008607 case XML_RELAXNG_VALUE: {
8608 xmlChar *content = NULL;
8609 xmlChar *oldvalue;
8610 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008611
Daniel Veillardfd573f12003-03-16 17:52:32 +00008612 child = node;
8613 while (child != NULL) {
8614 if (child->type == XML_ELEMENT_NODE) {
8615 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
8616 node->parent->name);
8617 ret = -1;
8618 break;
8619 } else if ((child->type == XML_TEXT_NODE) ||
8620 (child->type == XML_CDATA_SECTION_NODE)) {
8621 content = xmlStrcat(content, child->content);
8622 }
8623 /* TODO: handle entities ... */
8624 child = child->next;
8625 }
8626 if (ret == -1) {
8627 if (content != NULL)
8628 xmlFree(content);
8629 break;
8630 }
8631 if (content == NULL) {
8632 content = xmlStrdup(BAD_CAST "");
8633 if (content == NULL) {
8634 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8635 ret = -1;
8636 break;
8637 }
8638 }
8639 oldvalue = ctxt->state->value;
8640 ctxt->state->value = content;
8641 ret = xmlRelaxNGValidateValue(ctxt, define);
8642 ctxt->state->value = oldvalue;
8643 if (ret == -1) {
8644 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
8645 } else if (ret == 0) {
8646 ctxt->state->seq = NULL;
8647 }
8648 if (content != NULL)
8649 xmlFree(content);
8650 break;
8651 }
8652 case XML_RELAXNG_LIST: {
8653 xmlChar *content;
8654 xmlNodePtr child;
8655 xmlChar *oldvalue, *oldendvalue;
8656 int len;
8657
8658 /*
8659 * Make sure it's only text nodes
8660 */
8661
8662 content = NULL;
8663 child = node;
8664 while (child != NULL) {
8665 if (child->type == XML_ELEMENT_NODE) {
8666 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
8667 node->parent->name);
8668 ret = -1;
8669 break;
8670 } else if ((child->type == XML_TEXT_NODE) ||
8671 (child->type == XML_CDATA_SECTION_NODE)) {
8672 content = xmlStrcat(content, child->content);
8673 }
8674 /* TODO: handle entities ... */
8675 child = child->next;
8676 }
8677 if (ret == -1) {
8678 if (content != NULL)
8679 xmlFree(content);
8680 break;
8681 }
8682 if (content == NULL) {
8683 content = xmlStrdup(BAD_CAST "");
8684 if (content == NULL) {
8685 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8686 ret = -1;
8687 break;
8688 }
8689 }
8690 len = xmlStrlen(content);
8691 oldvalue = ctxt->state->value;
8692 oldendvalue = ctxt->state->endvalue;
8693 ctxt->state->value = content;
8694 ctxt->state->endvalue = content + len;
8695 ret = xmlRelaxNGValidateValue(ctxt, define);
8696 ctxt->state->value = oldvalue;
8697 ctxt->state->endvalue = oldendvalue;
8698 if (ret == -1) {
8699 VALID_ERR(XML_RELAXNG_ERR_LIST);
8700 } else if ((ret == 0) && (node != NULL)) {
8701 ctxt->state->seq = node->next;
8702 }
8703 if (content != NULL)
8704 xmlFree(content);
8705 break;
8706 }
8707 case XML_RELAXNG_START:
8708 case XML_RELAXNG_EXCEPT:
8709 case XML_RELAXNG_PARAM:
8710 TODO
8711 ret = -1;
8712 break;
8713 }
8714 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008715#ifdef DEBUG
Daniel Veillardfd573f12003-03-16 17:52:32 +00008716 for (i = 0;i < ctxt->depth;i++)
8717 xmlGenericError(xmlGenericErrorContext, " ");
8718 xmlGenericError(xmlGenericErrorContext,
8719 "Validating %s ", xmlRelaxNGDefName(define));
8720 if (define->name != NULL)
8721 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
8722 if (ret == 0)
8723 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
8724 else
8725 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008726#endif
Daniel Veillardfd573f12003-03-16 17:52:32 +00008727 return(ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008728}
8729
8730/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008731 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008732 * @ctxt: a Relax-NG validation context
8733 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008734 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008735 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008736 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008737 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008738 */
8739static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00008740xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
8741 xmlRelaxNGDefinePtr define) {
8742 xmlRelaxNGStatesPtr states, res;
8743 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008744
Daniel Veillardfd573f12003-03-16 17:52:32 +00008745 /*
8746 * We should NOT have both ctxt->state and ctxt->states
8747 */
8748 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8749 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008750 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008751 ctxt->state = NULL;
8752 }
8753
8754 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
8755 if (ctxt->states != NULL) {
8756 ctxt->state = ctxt->states->tabState[0];
8757 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8758 ctxt->states = NULL;
8759 }
8760 ret = xmlRelaxNGValidateState(ctxt, define);
8761 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8762 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008763 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008764 ctxt->state = NULL;
8765 }
8766 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
8767 ctxt->state = ctxt->states->tabState[0];
8768 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8769 ctxt->states = NULL;
8770 }
8771 return(ret);
8772 }
8773
8774 states = ctxt->states;
8775 ctxt->states = NULL;
8776 res = NULL;
8777 j = 0;
8778 oldflags = ctxt->flags;
8779 ctxt->flags |= FLAGS_IGNORABLE;
8780 for (i = 0;i < states->nbState;i++) {
8781 ctxt->state = states->tabState[i];
8782 ctxt->states = NULL;
8783 ret = xmlRelaxNGValidateState(ctxt, define);
8784 /*
8785 * We should NOT have both ctxt->state and ctxt->states
8786 */
8787 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8788 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008789 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008790 ctxt->state = NULL;
8791 }
8792 if (ret == 0) {
8793 if (ctxt->states == NULL) {
8794 if (res != NULL) {
8795 /* add the state to the container */
8796 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
8797 ctxt->state = NULL;
8798 } else {
8799 /* add the state directly in states */
8800 states->tabState[j++] = ctxt->state;
8801 ctxt->state = NULL;
8802 }
8803 } else {
8804 if (res == NULL) {
8805 /* make it the new container and copy other results */
8806 res = ctxt->states;
8807 ctxt->states = NULL;
8808 for (k = 0;k < j;k++)
8809 xmlRelaxNGAddStates(ctxt, res, states->tabState[k]);
8810 } else {
8811 /* add all the new results to res and reff the container */
8812 for (k = 0;k < ctxt->states->nbState;k++)
8813 xmlRelaxNGAddStates(ctxt, res,
8814 ctxt->states->tabState[k]);
8815 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8816 ctxt->states = NULL;
8817 }
8818 }
8819 } else {
8820 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008821 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008822 ctxt->state = NULL;
8823 } else if (ctxt->states != NULL) {
8824 for (k = 0;k < ctxt->states->nbState;k++)
Daniel Veillard798024a2003-03-19 10:36:09 +00008825 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008826 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8827 ctxt->states = NULL;
8828 }
8829 }
8830 }
8831 ctxt->flags = oldflags;
8832 if (res != NULL) {
8833 xmlRelaxNGFreeStates(ctxt, states);
8834 ctxt->states = res;
8835 ret = 0;
8836 } else if (j > 1) {
8837 states->nbState = j;
8838 ctxt->states = states;
8839 ret =0;
8840 } else if (j == 1) {
8841 ctxt->state = states->tabState[0];
8842 xmlRelaxNGFreeStates(ctxt, states);
8843 ret = 0;
8844 } else {
8845 ret = -1;
8846 xmlRelaxNGFreeStates(ctxt, states);
8847 if (ctxt->states != NULL) {
8848 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8849 ctxt->states = NULL;
8850 }
8851 }
8852 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8853 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008854 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008855 ctxt->state = NULL;
8856 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008857 return(ret);
8858}
8859
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008860/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008861 * xmlRelaxNGValidateDocument:
8862 * @ctxt: a Relax-NG validation context
8863 * @doc: the document
8864 *
8865 * Validate the given document
8866 *
8867 * Returns 0 if the validation succeeded or an error code.
8868 */
8869static int
8870xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
8871 int ret;
8872 xmlRelaxNGPtr schema;
8873 xmlRelaxNGGrammarPtr grammar;
8874 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008875 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008876
8877 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
8878 return(-1);
8879
8880 schema = ctxt->schema;
8881 grammar = schema->topgrammar;
8882 if (grammar == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008883 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008884 return(-1);
8885 }
8886 state = xmlRelaxNGNewValidState(ctxt, NULL);
8887 ctxt->state = state;
8888 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008889 if ((ctxt->state != NULL) && (state->seq != NULL)) {
8890 state = ctxt->state;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008891 node = state->seq;
8892 node = xmlRelaxNGSkipIgnored(ctxt, node);
8893 if (node != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008894 if (ret != -1) {
8895 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
8896 ret = -1;
8897 }
8898 }
8899 } else if (ctxt->states != NULL) {
8900 int i;
8901 int tmp = -1;
8902
8903 for (i = 0;i < ctxt->states->nbState;i++) {
8904 state = ctxt->states->tabState[i];
8905 node = state->seq;
8906 node = xmlRelaxNGSkipIgnored(ctxt, node);
8907 if (node == NULL)
8908 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +00008909 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008910 }
8911 if (tmp == -1) {
8912 if (ret != -1) {
8913 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
8914 ret = -1;
8915 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008916 }
8917 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00008918 if (ctxt->state != NULL) {
8919 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8920 ctxt->state = NULL;
8921 }
Daniel Veillard580ced82003-03-21 21:22:48 +00008922 if (ret != 0)
Daniel Veillardfd573f12003-03-16 17:52:32 +00008923 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +00008924#ifdef DEBUG
8925 else if (ctxt->errNr != 0) {
8926 ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n",
8927 ctxt->errNr);
8928 xmlRelaxNGDumpValidError(ctxt);
8929 }
8930#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008931 if (ctxt->idref == 1) {
8932 xmlValidCtxt vctxt;
8933
8934 memset(&vctxt, 0, sizeof(xmlValidCtxt));
8935 vctxt.valid = 1;
8936 vctxt.error = ctxt->error;
8937 vctxt.warning = ctxt->warning;
8938 vctxt.userData = ctxt->userData;
8939
8940 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
8941 ret = -1;
8942 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008943
8944 return(ret);
8945}
8946
Daniel Veillardfd573f12003-03-16 17:52:32 +00008947/************************************************************************
8948 * *
8949 * Validation interfaces *
8950 * *
8951 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +00008952/**
8953 * xmlRelaxNGNewValidCtxt:
8954 * @schema: a precompiled XML RelaxNGs
8955 *
8956 * Create an XML RelaxNGs validation context based on the given schema
8957 *
8958 * Returns the validation context or NULL in case of error
8959 */
8960xmlRelaxNGValidCtxtPtr
8961xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) {
8962 xmlRelaxNGValidCtxtPtr ret;
8963
8964 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
8965 if (ret == NULL) {
8966 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardfd573f12003-03-16 17:52:32 +00008967 "Failed to allocate new schema validation context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00008968 return (NULL);
8969 }
8970 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
8971 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00008972 ret->error = xmlGenericError;
8973 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +00008974 ret->errNr = 0;
8975 ret->errMax = 0;
8976 ret->err = NULL;
8977 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008978 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +00008979 ret->states = NULL;
8980 ret->freeState = NULL;
8981 ret->freeStates = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008982 return (ret);
8983}
8984
8985/**
8986 * xmlRelaxNGFreeValidCtxt:
8987 * @ctxt: the schema validation context
8988 *
8989 * Free the resources associated to the schema validation context
8990 */
8991void
8992xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008993 int k;
8994
Daniel Veillard6eadf632003-01-23 18:29:16 +00008995 if (ctxt == NULL)
8996 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008997 if (ctxt->states != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00008998 xmlRelaxNGFreeStates(NULL, ctxt->states);
8999 if (ctxt->freeState != NULL) {
9000 for (k = 0;k < ctxt->freeState->nbState;k++) {
9001 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
9002 }
9003 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
9004 }
Daniel Veillard798024a2003-03-19 10:36:09 +00009005 if (ctxt->freeStates != NULL) {
9006 for (k = 0;k < ctxt->freeStatesNr;k++) {
9007 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
9008 }
9009 xmlFree(ctxt->freeStates);
9010 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00009011 if (ctxt->errTab != NULL)
9012 xmlFree(ctxt->errTab);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009013 xmlFree(ctxt);
9014}
9015
9016/**
9017 * xmlRelaxNGSetValidErrors:
9018 * @ctxt: a Relax-NG validation context
9019 * @err: the error function
9020 * @warn: the warning function
9021 * @ctx: the functions context
9022 *
9023 * Set the error and warning callback informations
9024 */
9025void
9026xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
9027 xmlRelaxNGValidityErrorFunc err,
9028 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
9029 if (ctxt == NULL)
9030 return;
9031 ctxt->error = err;
9032 ctxt->warning = warn;
9033 ctxt->userData = ctx;
9034}
9035
9036/**
9037 * xmlRelaxNGValidateDoc:
9038 * @ctxt: a Relax-NG validation context
9039 * @doc: a parsed document tree
9040 *
9041 * Validate a document tree in memory.
9042 *
9043 * Returns 0 if the document is valid, a positive error code
9044 * number otherwise and -1 in case of internal or API error.
9045 */
9046int
9047xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
9048 int ret;
9049
9050 if ((ctxt == NULL) || (doc == NULL))
9051 return(-1);
9052
9053 ctxt->doc = doc;
9054
9055 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +00009056 /*
9057 * TODO: build error codes
9058 */
9059 if (ret == -1)
9060 return(1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009061 return(ret);
9062}
9063
9064#endif /* LIBXML_SCHEMAS_ENABLED */
9065