blob: 723e5fd38fdbe99bb5a2e78017fd9775fcf54d20 [file] [log] [blame]
Daniel Veillard6eadf632003-01-23 18:29:16 +00001/*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
Daniel Veillardd41f4f42003-01-29 21:07:52 +00009/**
10 * TODO:
Daniel Veillardd41f4f42003-01-29 21:07:52 +000011 * - error reporting
Daniel Veillard1ed7f362003-02-03 10:57:45 +000012 * - handle namespace declarations as attributes.
Daniel Veillardf4b4f982003-02-13 11:02:08 +000013 * - add support for DTD compatibility spec
14 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
Daniel Veillardfd573f12003-03-16 17:52:32 +000015 * - report better mem allocations at runtime and abort immediately.
Daniel Veillardd41f4f42003-01-29 21:07:52 +000016 */
17
Daniel Veillard6eadf632003-01-23 18:29:16 +000018#define IN_LIBXML
19#include "libxml.h"
20
21#ifdef LIBXML_SCHEMAS_ENABLED
22
23#include <string.h>
24#include <stdio.h>
25#include <libxml/xmlmemory.h>
26#include <libxml/parser.h>
27#include <libxml/parserInternals.h>
28#include <libxml/hash.h>
29#include <libxml/uri.h>
30
31#include <libxml/relaxng.h>
32
33#include <libxml/xmlschemastypes.h>
34#include <libxml/xmlautomata.h>
35#include <libxml/xmlregexp.h>
Daniel Veillardc6e997c2003-01-27 12:35:42 +000036#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000037
38/*
39 * The Relax-NG namespace
40 */
41static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
42 "http://relaxng.org/ns/structure/1.0";
43
44#define IS_RELAXNG(node, type) \
45 ((node != NULL) && (node->ns != NULL) && \
46 (xmlStrEqual(node->name, (const xmlChar *) type)) && \
47 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
48
49
Daniel Veillard952379b2003-03-17 15:37:12 +000050/* #define DEBUG 1 */
Daniel Veillardc482e262003-02-26 14:48:48 +000051/* #define DEBUG_GRAMMAR 1 */
Daniel Veillard71531f32003-02-05 13:19:53 +000052/* #define DEBUG_CONTENT 1 */
53/* #define DEBUG_TYPE 1 */
54/* #define DEBUG_VALID 1 */
Daniel Veillarde5b110b2003-02-04 14:43:39 +000055/* #define DEBUG_INTERLEAVE 1 */
Daniel Veillard416589a2003-02-17 17:25:42 +000056/* #define DEBUG_LIST 1 */
Daniel Veillard5add8682003-03-10 13:13:58 +000057/* #define DEBUG_INCLUDE */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000058/* #define DEBUG_ERROR 1 */
Daniel Veillard6eadf632003-01-23 18:29:16 +000059
60#define UNBOUNDED (1 << 30)
61#define TODO \
62 xmlGenericError(xmlGenericErrorContext, \
63 "Unimplemented block at %s:%d\n", \
64 __FILE__, __LINE__);
65
66typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
67typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
68
69typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
70typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
71
Daniel Veillardd41f4f42003-01-29 21:07:52 +000072typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
73typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
74
Daniel Veillarda9d912d2003-02-01 17:43:10 +000075typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
76typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
77
Daniel Veillard6eadf632003-01-23 18:29:16 +000078typedef enum {
79 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
80 XML_RELAXNG_COMBINE_CHOICE, /* choice */
81 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
82} xmlRelaxNGCombine;
83
Daniel Veillard4c5cf702003-02-21 15:40:34 +000084typedef enum {
85 XML_RELAXNG_CONTENT_ERROR = -1,
86 XML_RELAXNG_CONTENT_EMPTY = 0,
87 XML_RELAXNG_CONTENT_SIMPLE,
88 XML_RELAXNG_CONTENT_COMPLEX
89} xmlRelaxNGContentType;
90
Daniel Veillard6eadf632003-01-23 18:29:16 +000091typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
92typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
93
94struct _xmlRelaxNGGrammar {
95 xmlRelaxNGGrammarPtr parent;/* the parent grammar if any */
96 xmlRelaxNGGrammarPtr children;/* the children grammar if any */
97 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
98 xmlRelaxNGDefinePtr start; /* <start> content */
99 xmlRelaxNGCombine combine; /* the default combine value */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000100 xmlRelaxNGDefinePtr startList;/* list of <start> definitions */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000101 xmlHashTablePtr defs; /* define* */
102 xmlHashTablePtr refs; /* references */
103};
104
105
Daniel Veillard6eadf632003-01-23 18:29:16 +0000106typedef enum {
Daniel Veillard77648bb2003-02-20 15:03:22 +0000107 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000108 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
109 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard144fae12003-02-03 13:17:57 +0000110 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000111 XML_RELAXNG_TEXT, /* textual content */
112 XML_RELAXNG_ELEMENT, /* an element */
113 XML_RELAXNG_DATATYPE, /* extenal data type definition */
Daniel Veillard8fe98712003-02-19 00:19:14 +0000114 XML_RELAXNG_PARAM, /* extenal data type parameter */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000115 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
116 XML_RELAXNG_LIST, /* a list of patterns */
117 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
118 XML_RELAXNG_DEF, /* a definition */
119 XML_RELAXNG_REF, /* reference to a definition */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000120 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
Daniel Veillard419a7682003-02-03 23:22:49 +0000121 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000122 XML_RELAXNG_OPTIONAL, /* optional patterns */
123 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000124 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
125 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
126 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000127 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000128 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000129} xmlRelaxNGType;
130
Daniel Veillardfd573f12003-03-16 17:52:32 +0000131#define IS_NULLABLE 1
132#define IS_NOT_NULLABLE 2
133#define IS_INDETERMINIST 4
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000134#define IS_MIXED 8
Daniel Veillarde063f482003-03-21 16:53:17 +0000135#define IS_TRIABLE 16
136#define IS_PROCESSED 32
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000137
Daniel Veillard6eadf632003-01-23 18:29:16 +0000138struct _xmlRelaxNGDefine {
139 xmlRelaxNGType type; /* the type of definition */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000140 xmlNodePtr node; /* the node in the source */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000141 xmlChar *name; /* the element local name if present */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000142 xmlChar *ns; /* the namespace local name if present */
Daniel Veillardedc91922003-01-26 00:52:04 +0000143 xmlChar *value; /* value when available */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000144 void *data; /* data lib or specific pointer */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000145 xmlRelaxNGDefinePtr content;/* the expected content */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000146 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000147 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000148 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
Daniel Veillard3b2e4e12003-02-03 08:52:58 +0000149 xmlRelaxNGDefinePtr nameClass;/* the nameClass definition if any */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000150 xmlRelaxNGDefinePtr nextHash;/* next define in defs/refs hash tables */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000151 short depth; /* used for the cycle detection */
Daniel Veillarde063f482003-03-21 16:53:17 +0000152 short dflags; /* define related flags */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000153};
154
155/**
156 * _xmlRelaxNG:
157 *
158 * A RelaxNGs definition
159 */
160struct _xmlRelaxNG {
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000161 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000162 xmlRelaxNGGrammarPtr topgrammar;
163 xmlDocPtr doc;
164
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000165 int idref; /* requires idref checking */
166
Daniel Veillard6eadf632003-01-23 18:29:16 +0000167 xmlHashTablePtr defs; /* define */
168 xmlHashTablePtr refs; /* references */
Daniel Veillardc482e262003-02-26 14:48:48 +0000169 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
170 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard419a7682003-02-03 23:22:49 +0000171 int defNr; /* number of defines used */
172 xmlRelaxNGDefinePtr *defTab;/* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000173
Daniel Veillard6eadf632003-01-23 18:29:16 +0000174};
175
Daniel Veillard77648bb2003-02-20 15:03:22 +0000176#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
177#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
178#define XML_RELAXNG_IN_LIST (1 << 2)
179#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
180#define XML_RELAXNG_IN_START (1 << 4)
181#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
182#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
183#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000184#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
185#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000186
187struct _xmlRelaxNGParserCtxt {
188 void *userData; /* user specific data block */
189 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
190 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000191 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000192
193 xmlRelaxNGPtr schema; /* The schema in use */
194 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
Daniel Veillard419a7682003-02-03 23:22:49 +0000195 xmlRelaxNGGrammarPtr parentgrammar;/* the parent grammar */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000196 int flags; /* parser flags */
197 int nbErrors; /* number of errors at parse time */
198 int nbWarnings; /* number of warnings at parse time */
Daniel Veillard276be4a2003-01-24 01:03:34 +0000199 const xmlChar *define; /* the current define scope */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000200 xmlRelaxNGDefinePtr def; /* the current define */
201
202 int nbInterleaves;
203 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000204
Daniel Veillardc482e262003-02-26 14:48:48 +0000205 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
206 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000207 xmlChar *URL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000208 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000209
Daniel Veillard419a7682003-02-03 23:22:49 +0000210 int defNr; /* number of defines used */
211 int defMax; /* number of defines aloocated */
212 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
213
Daniel Veillard6eadf632003-01-23 18:29:16 +0000214 const char *buffer;
215 int size;
216
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000217 /* the document stack */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000218 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000219 int docNr; /* Depth of the parsing stack */
220 int docMax; /* Max depth of the parsing stack */
221 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000222
223 /* the include stack */
Daniel Veillardd4310742003-02-18 21:12:46 +0000224 xmlRelaxNGIncludePtr inc; /* Current parsed include */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000225 int incNr; /* Depth of the include parsing stack */
226 int incMax; /* Max depth of the parsing stack */
227 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000228
229 int idref; /* requires idref checking */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000230};
231
232#define FLAGS_IGNORABLE 1
233#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000234#define FLAGS_MIXED_CONTENT 4
Daniel Veillard6eadf632003-01-23 18:29:16 +0000235
236/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000237 * xmlRelaxNGInterleaveGroup:
238 *
239 * A RelaxNGs partition set associated to lists of definitions
240 */
241typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
242typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
243struct _xmlRelaxNGInterleaveGroup {
244 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
245 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000246 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000247};
248
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000249#define IS_DETERMINIST 1
250#define IS_NEEDCHECK 2
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000251/**
252 * xmlRelaxNGPartitions:
253 *
254 * A RelaxNGs partition associated to an interleave group
255 */
256typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
257typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
258struct _xmlRelaxNGPartition {
259 int nbgroups; /* number of groups in the partitions */
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000260 xmlHashTablePtr triage; /* hash table used to direct nodes to the
261 right group when possible */
262 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000263 xmlRelaxNGInterleaveGroupPtr *groups;
264};
265
266/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000267 * xmlRelaxNGValidState:
268 *
269 * A RelaxNGs validation state
270 */
271#define MAX_ATTR 20
272typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
273typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
274struct _xmlRelaxNGValidState {
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000275 xmlNodePtr node; /* the current node */
276 xmlNodePtr seq; /* the sequence of children left to validate */
277 int nbAttrs; /* the number of attributes */
Daniel Veillard798024a2003-03-19 10:36:09 +0000278 int maxAttrs; /* the size of attrs */
Daniel Veillard1ed7f362003-02-03 10:57:45 +0000279 int nbAttrLeft; /* the number of attributes left to validate */
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000280 xmlChar *value; /* the value when operating on string */
281 xmlChar *endvalue; /* the end value when operating on string */
Daniel Veillard798024a2003-03-19 10:36:09 +0000282 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000283};
284
285/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000286 * xmlRelaxNGStates:
287 *
288 * A RelaxNGs container for validation state
289 */
290typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
291typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
292struct _xmlRelaxNGStates {
293 int nbState; /* the number of states */
294 int maxState; /* the size of the array */
295 xmlRelaxNGValidStatePtr *tabState;
296};
297
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000298#define ERROR_IS_DUP 1
Daniel Veillardfd573f12003-03-16 17:52:32 +0000299/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000300 * xmlRelaxNGValidError:
301 *
302 * A RelaxNGs validation error
303 */
304typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
305typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
306struct _xmlRelaxNGValidError {
307 xmlRelaxNGValidErr err; /* the error number */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000308 int flags; /* flags */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000309 xmlNodePtr node; /* the current node */
310 xmlNodePtr seq; /* the current child */
311 const xmlChar * arg1; /* first arg */
312 const xmlChar * arg2; /* second arg */
313};
314
315/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000316 * xmlRelaxNGValidCtxt:
317 *
318 * A RelaxNGs validation context
319 */
320
321struct _xmlRelaxNGValidCtxt {
322 void *userData; /* user specific data block */
323 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
324 xmlRelaxNGValidityWarningFunc warning;/* the callback in case of warning */
325
326 xmlRelaxNGPtr schema; /* The schema in use */
327 xmlDocPtr doc; /* the document being validated */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000328 int flags; /* validation flags */
Daniel Veillard231d7912003-02-09 14:22:17 +0000329 int depth; /* validation depth */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000330 int idref; /* requires idref checking */
Daniel Veillarda507fbf2003-03-31 16:09:37 +0000331 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000332
333 /*
334 * Errors accumulated in branches may have to be stacked to be
335 * provided back when it's sure they affect validation.
336 */
337 xmlRelaxNGValidErrorPtr err; /* Last error */
338 int errNr; /* Depth of the error stack */
339 int errMax; /* Max depth of the error stack */
340 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000341
Daniel Veillardfd573f12003-03-16 17:52:32 +0000342 xmlRelaxNGValidStatePtr state; /* the current validation state */
343 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000344
345 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
346 int freeStatesNr;
347 int freeStatesMax;
348 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000349};
350
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000351/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000352 * xmlRelaxNGInclude:
353 *
354 * Structure associated to a RelaxNGs document element
355 */
356struct _xmlRelaxNGInclude {
Daniel Veillardc482e262003-02-26 14:48:48 +0000357 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000358 xmlChar *href; /* the normalized href value */
359 xmlDocPtr doc; /* the associated XML document */
360 xmlRelaxNGDefinePtr content;/* the definitions */
361 xmlRelaxNGPtr schema; /* the schema */
362};
363
364/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000365 * xmlRelaxNGDocument:
366 *
367 * Structure associated to a RelaxNGs document element
368 */
369struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000370 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000371 xmlChar *href; /* the normalized href value */
372 xmlDocPtr doc; /* the associated XML document */
373 xmlRelaxNGDefinePtr content;/* the definitions */
374 xmlRelaxNGPtr schema; /* the schema */
375};
376
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000377
Daniel Veillard6eadf632003-01-23 18:29:16 +0000378/************************************************************************
379 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000380 * Preliminary type checking interfaces *
381 * *
382 ************************************************************************/
383/**
384 * xmlRelaxNGTypeHave:
385 * @data: data needed for the library
386 * @type: the type name
387 * @value: the value to check
388 *
389 * Function provided by a type library to check if a type is exported
390 *
391 * Returns 1 if yes, 0 if no and -1 in case of error.
392 */
393typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar *type);
394
395/**
396 * xmlRelaxNGTypeCheck:
397 * @data: data needed for the library
398 * @type: the type name
399 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000400 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000401 *
402 * Function provided by a type library to check if a value match a type
403 *
404 * Returns 1 if yes, 0 if no and -1 in case of error.
405 */
406typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar *type,
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000407 const xmlChar *value, void **result,
408 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000409
410/**
411 * xmlRelaxNGFacetCheck:
412 * @data: data needed for the library
413 * @type: the type name
414 * @facet: the facet name
415 * @val: the facet value
416 * @strval: the string value
417 * @value: the value to check
418 *
419 * Function provided by a type library to check a value facet
420 *
421 * Returns 1 if yes, 0 if no and -1 in case of error.
422 */
423typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar *type,
424 const xmlChar *facet, const xmlChar *val,
425 const xmlChar *strval, void *value);
426
427/**
428 * xmlRelaxNGTypeFree:
429 * @data: data needed for the library
430 * @result: the value to free
431 *
432 * Function provided by a type library to free a returned result
433 */
434typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000435
436/**
437 * xmlRelaxNGTypeCompare:
438 * @data: data needed for the library
439 * @type: the type name
440 * @value1: the first value
441 * @value2: the second value
442 *
443 * Function provided by a type library to compare two values accordingly
444 * to a type.
445 *
446 * Returns 1 if yes, 0 if no and -1 in case of error.
447 */
448typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar *type,
449 const xmlChar *value1,
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000450 xmlNodePtr ctxt1,
451 void *comp1,
452 const xmlChar *value2,
453 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000454typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
455typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
456struct _xmlRelaxNGTypeLibrary {
457 const xmlChar *namespace; /* the datatypeLibrary value */
458 void *data; /* data needed for the library */
459 xmlRelaxNGTypeHave have; /* the export function */
460 xmlRelaxNGTypeCheck check; /* the checking function */
461 xmlRelaxNGTypeCompare comp; /* the compare function */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000462 xmlRelaxNGFacetCheck facet; /* the facet check function */
463 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000464};
465
466/************************************************************************
467 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000468 * Allocation functions *
469 * *
470 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000471static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
472static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillardd2298792003-02-14 16:54:11 +0000473static void xmlRelaxNGNormExtSpace(xmlChar *value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000474static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000475static int xmlRelaxNGEqualValidState(
476 xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
477 xmlRelaxNGValidStatePtr state1,
478 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000479static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
480 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000481
482/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000483 * xmlRelaxNGFreeDocument:
484 * @docu: a document structure
485 *
486 * Deallocate a RelaxNG document structure.
487 */
488static void
489xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
490{
491 if (docu == NULL)
492 return;
493
494 if (docu->href != NULL)
495 xmlFree(docu->href);
496 if (docu->doc != NULL)
497 xmlFreeDoc(docu->doc);
498 if (docu->schema != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000499 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000500 xmlFree(docu);
501}
502
503/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000504 * xmlRelaxNGFreeDocumentList:
505 * @docu: a list of document structure
506 *
507 * Deallocate a RelaxNG document structures.
508 */
509static void
510xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
511{
512 xmlRelaxNGDocumentPtr next;
513 while (docu != NULL) {
514 next = docu->next;
515 xmlRelaxNGFreeDocument(docu);
516 docu = next;
517 }
518}
519
520/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000521 * xmlRelaxNGFreeInclude:
522 * @incl: a include structure
523 *
524 * Deallocate a RelaxNG include structure.
525 */
526static void
527xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
528{
529 if (incl == NULL)
530 return;
531
532 if (incl->href != NULL)
533 xmlFree(incl->href);
534 if (incl->doc != NULL)
535 xmlFreeDoc(incl->doc);
536 if (incl->schema != NULL)
537 xmlRelaxNGFree(incl->schema);
538 xmlFree(incl);
539}
540
541/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000542 * xmlRelaxNGFreeIncludeList:
543 * @incl: a include structure list
544 *
545 * Deallocate a RelaxNG include structure.
546 */
547static void
548xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
549{
550 xmlRelaxNGIncludePtr next;
551 while (incl != NULL) {
552 next = incl->next;
553 xmlRelaxNGFreeInclude(incl);
554 incl = next;
555 }
556}
557
558/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000559 * xmlRelaxNGNewRelaxNG:
560 * @ctxt: a Relax-NG validation context (optional)
561 *
562 * Allocate a new RelaxNG structure.
563 *
564 * Returns the newly allocated structure or NULL in case or error
565 */
566static xmlRelaxNGPtr
567xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
568{
569 xmlRelaxNGPtr ret;
570
571 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
572 if (ret == NULL) {
573 if ((ctxt != NULL) && (ctxt->error != NULL))
574 ctxt->error(ctxt->userData, "Out of memory\n");
575 ctxt->nbErrors++;
576 return (NULL);
577 }
578 memset(ret, 0, sizeof(xmlRelaxNG));
579
580 return (ret);
581}
582
583/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000584 * xmlRelaxNGFreeInnerSchema:
585 * @schema: a schema structure
586 *
587 * Deallocate a RelaxNG schema structure.
588 */
589static void
590xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
591{
592 if (schema == NULL)
593 return;
594
595 if (schema->doc != NULL)
596 xmlFreeDoc(schema->doc);
597 if (schema->defTab != NULL) {
598 int i;
599
600 for (i = 0;i < schema->defNr;i++)
601 xmlRelaxNGFreeDefine(schema->defTab[i]);
602 xmlFree(schema->defTab);
603 }
604
605 xmlFree(schema);
606}
607
608/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000609 * xmlRelaxNGFree:
610 * @schema: a schema structure
611 *
612 * Deallocate a RelaxNG structure.
613 */
614void
615xmlRelaxNGFree(xmlRelaxNGPtr schema)
616{
617 if (schema == NULL)
618 return;
619
Daniel Veillard6eadf632003-01-23 18:29:16 +0000620 if (schema->topgrammar != NULL)
621 xmlRelaxNGFreeGrammar(schema->topgrammar);
622 if (schema->doc != NULL)
623 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000624 if (schema->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000625 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000626 if (schema->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +0000627 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000628 if (schema->defTab != NULL) {
629 int i;
630
631 for (i = 0;i < schema->defNr;i++)
632 xmlRelaxNGFreeDefine(schema->defTab[i]);
633 xmlFree(schema->defTab);
634 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000635
636 xmlFree(schema);
637}
638
639/**
640 * xmlRelaxNGNewGrammar:
641 * @ctxt: a Relax-NG validation context (optional)
642 *
643 * Allocate a new RelaxNG grammar.
644 *
645 * Returns the newly allocated structure or NULL in case or error
646 */
647static xmlRelaxNGGrammarPtr
648xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
649{
650 xmlRelaxNGGrammarPtr ret;
651
652 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
653 if (ret == NULL) {
654 if ((ctxt != NULL) && (ctxt->error != NULL))
655 ctxt->error(ctxt->userData, "Out of memory\n");
656 ctxt->nbErrors++;
657 return (NULL);
658 }
659 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
660
661 return (ret);
662}
663
664/**
665 * xmlRelaxNGFreeGrammar:
666 * @grammar: a grammar structure
667 *
668 * Deallocate a RelaxNG grammar structure.
669 */
670static void
671xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
672{
673 if (grammar == NULL)
674 return;
675
Daniel Veillardc482e262003-02-26 14:48:48 +0000676 if (grammar->children != NULL) {
677 xmlRelaxNGFreeGrammar(grammar->children);
678 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000679 if (grammar->next != NULL) {
680 xmlRelaxNGFreeGrammar(grammar->next);
681 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000682 if (grammar->refs != NULL) {
683 xmlHashFree(grammar->refs, NULL);
684 }
685 if (grammar->defs != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000686 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000687 }
688
689 xmlFree(grammar);
690}
691
692/**
693 * xmlRelaxNGNewDefine:
694 * @ctxt: a Relax-NG validation context
695 * @node: the node in the input document.
696 *
697 * Allocate a new RelaxNG define.
698 *
699 * Returns the newly allocated structure or NULL in case or error
700 */
701static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000702xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000703{
704 xmlRelaxNGDefinePtr ret;
705
Daniel Veillard419a7682003-02-03 23:22:49 +0000706 if (ctxt->defMax == 0) {
707 ctxt->defMax = 16;
708 ctxt->defNr = 0;
709 ctxt->defTab = (xmlRelaxNGDefinePtr *)
710 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
711 if (ctxt->defTab == NULL) {
712 if ((ctxt != NULL) && (ctxt->error != NULL))
713 ctxt->error(ctxt->userData, "Out of memory\n");
714 ctxt->nbErrors++;
715 return (NULL);
716 }
717 } else if (ctxt->defMax <= ctxt->defNr) {
718 xmlRelaxNGDefinePtr *tmp;
719 ctxt->defMax *= 2;
720 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
721 ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
722 if (tmp == NULL) {
723 if ((ctxt != NULL) && (ctxt->error != NULL))
724 ctxt->error(ctxt->userData, "Out of memory\n");
725 ctxt->nbErrors++;
726 return (NULL);
727 }
728 ctxt->defTab = tmp;
729 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000730 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
731 if (ret == NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +0000732 if ((ctxt != NULL) && (ctxt->error != NULL))
733 ctxt->error(ctxt->userData, "Out of memory\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +0000734 ctxt->nbErrors++;
Daniel Veillard419a7682003-02-03 23:22:49 +0000735 return(NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000736 }
737 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000738 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000739 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000740 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000741 return (ret);
742}
743
744/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000745 * xmlRelaxNGFreePartition:
746 * @partitions: a partition set structure
747 *
748 * Deallocate RelaxNG partition set structures.
749 */
750static void
751xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions) {
752 xmlRelaxNGInterleaveGroupPtr group;
753 int j;
754
755 if (partitions != NULL) {
756 if (partitions->groups != NULL) {
757 for (j = 0;j < partitions->nbgroups;j++) {
758 group = partitions->groups[j];
759 if (group != NULL) {
760 if (group->defs != NULL)
761 xmlFree(group->defs);
Daniel Veillard44e1dd02003-02-21 23:23:28 +0000762 if (group->attrs != NULL)
763 xmlFree(group->attrs);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000764 xmlFree(group);
765 }
766 }
767 xmlFree(partitions->groups);
768 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000769 if (partitions->triage != NULL) {
770 xmlHashFree(partitions->triage, NULL);
771 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000772 xmlFree(partitions);
773 }
774}
775/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000776 * xmlRelaxNGFreeDefine:
777 * @define: a define structure
778 *
779 * Deallocate a RelaxNG define structure.
780 */
781static void
782xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
783{
784 if (define == NULL)
785 return;
786
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000787 if ((define->type == XML_RELAXNG_VALUE) &&
788 (define->attrs != NULL)) {
789 xmlRelaxNGTypeLibraryPtr lib;
790
791 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
792 if ((lib != NULL) && (lib->freef != NULL))
793 lib->freef(lib->data, (void *) define->attrs);
794 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000795 if ((define->data != NULL) &&
796 (define->type == XML_RELAXNG_INTERLEAVE))
797 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
Daniel Veillarde063f482003-03-21 16:53:17 +0000798 if ((define->data != NULL) &&
799 (define->type == XML_RELAXNG_CHOICE))
800 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000801 if (define->name != NULL)
802 xmlFree(define->name);
803 if (define->ns != NULL)
804 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000805 if (define->value != NULL)
806 xmlFree(define->value);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000807 xmlFree(define);
808}
809
810/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000811 * xmlRelaxNGNewStates:
812 * @ctxt: a Relax-NG validation context
813 * @size: the default size for the container
814 *
815 * Allocate a new RelaxNG validation state container
816 * TODO: keep a pool in the ctxt
817 *
818 * Returns the newly allocated structure or NULL in case or error
819 */
820static xmlRelaxNGStatesPtr
821xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
822{
823 xmlRelaxNGStatesPtr ret;
824
Daniel Veillard798024a2003-03-19 10:36:09 +0000825 if ((ctxt != NULL) &&
826 (ctxt->freeState != NULL) &&
827 (ctxt->freeStatesNr > 0)) {
828 ctxt->freeStatesNr--;
829 ret = ctxt->freeStates[ctxt->freeStatesNr];
830 ret->nbState = 0;
831 return(ret);
832 }
Daniel Veillardfd573f12003-03-16 17:52:32 +0000833 if (size < 16) size = 16;
834
835 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
836 (size - 1) * sizeof(xmlRelaxNGValidStatePtr));
837 if (ret == NULL) {
838 if ((ctxt != NULL) && (ctxt->error != NULL))
839 ctxt->error(ctxt->userData, "Out of memory\n");
840 return (NULL);
841 }
842 ret->nbState = 0;
843 ret->maxState = size;
844 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc(
845 (size) * sizeof(xmlRelaxNGValidStatePtr));
846 if (ret->tabState == NULL) {
847 if ((ctxt != NULL) && (ctxt->error != NULL))
848 ctxt->error(ctxt->userData, "Out of memory\n");
849 xmlFree(ret->tabState);
850 return (NULL);
851 }
852 return(ret);
853}
854
855/**
Daniel Veillard798024a2003-03-19 10:36:09 +0000856 * xmlRelaxNGAddStateUniq:
857 * @ctxt: a Relax-NG validation context
858 * @states: the states container
859 * @state: the validation state
860 *
861 * Add a RelaxNG validation state to the container without checking
862 * for unicity.
863 *
864 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
865 */
866static int
867xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
868 xmlRelaxNGStatesPtr states,
869 xmlRelaxNGValidStatePtr state)
870{
871 if (state == NULL) {
872 return(-1);
873 }
874 if (states->nbState >= states->maxState) {
875 xmlRelaxNGValidStatePtr *tmp;
876 int size;
877
878 size = states->maxState * 2;
879 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
880 (size) * sizeof(xmlRelaxNGValidStatePtr));
881 if (tmp == NULL) {
882 if ((ctxt != NULL) && (ctxt->error != NULL))
883 ctxt->error(ctxt->userData, "Out of memory\n");
884 return(-1);
885 }
886 states->tabState = tmp;
887 states->maxState = size;
888 }
889 states->tabState[states->nbState++] = state;
890 return(1);
891}
892
893/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000894 * xmlRelaxNGAddState:
895 * @ctxt: a Relax-NG validation context
896 * @states: the states container
897 * @state: the validation state
898 *
899 * Add a RelaxNG validation state to the container
900 *
901 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
902 */
903static int
904xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGStatesPtr states,
905 xmlRelaxNGValidStatePtr state)
906{
907 int i;
908
909 if (state == NULL) {
910 return(-1);
911 }
912 if (states->nbState >= states->maxState) {
913 xmlRelaxNGValidStatePtr *tmp;
914 int size;
915
916 size = states->maxState * 2;
917 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
918 (size) * sizeof(xmlRelaxNGValidStatePtr));
919 if (tmp == NULL) {
920 if ((ctxt != NULL) && (ctxt->error != NULL))
921 ctxt->error(ctxt->userData, "Out of memory\n");
922 return(-1);
923 }
924 states->tabState = tmp;
925 states->maxState = size;
926 }
927 for (i = 0;i < states->nbState;i++) {
928 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
Daniel Veillard798024a2003-03-19 10:36:09 +0000929 xmlRelaxNGFreeValidState(ctxt, state);
Daniel Veillardfd573f12003-03-16 17:52:32 +0000930 return(0);
931 }
932 }
933 states->tabState[states->nbState++] = state;
934 return(1);
935}
936
937/**
938 * xmlRelaxNGFreeStates:
939 * @ctxt: a Relax-NG validation context
940 * @states: teh container
941 *
942 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +0000943 */
944static void
Daniel Veillard798024a2003-03-19 10:36:09 +0000945xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardfd573f12003-03-16 17:52:32 +0000946 xmlRelaxNGStatesPtr states)
947{
Daniel Veillard798024a2003-03-19 10:36:09 +0000948 if (states == NULL)
949 return;
Daniel Veillard798024a2003-03-19 10:36:09 +0000950 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
951 ctxt->freeStatesMax = 40;
952 ctxt->freeStatesNr = 0;
953 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
954 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
955 if (ctxt->freeStates == NULL) {
956 if ((ctxt != NULL) && (ctxt->error != NULL))
957 ctxt->error(ctxt->userData, "Out of memory\n");
958 }
959 } else if ((ctxt != NULL) && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
960 xmlRelaxNGStatesPtr *tmp;
961
962 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
963 2 * ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
964 if (tmp == NULL) {
965 if ((ctxt != NULL) && (ctxt->error != NULL))
966 ctxt->error(ctxt->userData, "Out of memory\n");
967 xmlFree(states->tabState);
968 xmlFree(states);
969 return;
970 }
971 ctxt->freeStates = tmp;
972 ctxt->freeStatesMax *= 2;
973 }
974 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +0000975 xmlFree(states->tabState);
976 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +0000977 } else {
978 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +0000979 }
980}
981
982/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000983 * xmlRelaxNGNewValidState:
984 * @ctxt: a Relax-NG validation context
985 * @node: the current node or NULL for the document
986 *
987 * Allocate a new RelaxNG validation state
988 *
989 * Returns the newly allocated structure or NULL in case or error
990 */
991static xmlRelaxNGValidStatePtr
992xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
993{
994 xmlRelaxNGValidStatePtr ret;
995 xmlAttrPtr attr;
996 xmlAttrPtr attrs[MAX_ATTR];
997 int nbAttrs = 0;
998 xmlNodePtr root = NULL;
999
1000 if (node == NULL) {
1001 root = xmlDocGetRootElement(ctxt->doc);
1002 if (root == NULL)
1003 return(NULL);
1004 } else {
1005 attr = node->properties;
1006 while (attr != NULL) {
1007 if (nbAttrs < MAX_ATTR)
1008 attrs[nbAttrs++] = attr;
1009 else
1010 nbAttrs++;
1011 attr = attr->next;
1012 }
1013 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001014 if ((ctxt->freeState != NULL) &&
1015 (ctxt->freeState->nbState > 0)) {
1016 ctxt->freeState->nbState--;
1017 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1018 } else {
1019 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1020 if (ret == NULL) {
1021 if ((ctxt != NULL) && (ctxt->error != NULL))
1022 ctxt->error(ctxt->userData, "Out of memory\n");
1023 return (NULL);
1024 }
1025 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001026 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001027 ret->value = NULL;
1028 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001029 if (node == NULL) {
1030 ret->node = (xmlNodePtr) ctxt->doc;
1031 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001032 } else {
1033 ret->node = node;
1034 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001035 }
1036 ret->nbAttrs = 0;
1037 if (nbAttrs > 0) {
1038 if (ret->attrs == NULL) {
1039 if (nbAttrs < 4) ret->maxAttrs = 4;
1040 else ret->maxAttrs = nbAttrs;
1041 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1042 sizeof(xmlAttrPtr));
1043 if (ret->attrs == NULL) {
1044 if ((ctxt != NULL) && (ctxt->error != NULL))
1045 ctxt->error(ctxt->userData, "Out of memory\n");
1046 return (ret);
1047 }
1048 } else if (ret->maxAttrs < nbAttrs) {
1049 xmlAttrPtr *tmp;
1050
1051 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1052 sizeof(xmlAttrPtr));
1053 if (tmp == NULL) {
1054 if ((ctxt != NULL) && (ctxt->error != NULL))
1055 ctxt->error(ctxt->userData, "Out of memory\n");
1056 return (ret);
1057 }
1058 ret->attrs = tmp;
1059 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001060 ret->nbAttrs = nbAttrs;
Daniel Veillard798024a2003-03-19 10:36:09 +00001061 if (nbAttrs < MAX_ATTR) {
1062 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1063 } else {
1064 attr = node->properties;
1065 nbAttrs = 0;
1066 while (attr != NULL) {
1067 ret->attrs[nbAttrs++] = attr;
1068 attr = attr->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001069 }
1070 }
1071 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001072 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001073 return (ret);
1074}
1075
1076/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001077 * xmlRelaxNGCopyValidState:
1078 * @ctxt: a Relax-NG validation context
1079 * @state: a validation state
1080 *
1081 * Copy the validation state
1082 *
1083 * Returns the newly allocated structure or NULL in case or error
1084 */
1085static xmlRelaxNGValidStatePtr
1086xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1087 xmlRelaxNGValidStatePtr state)
1088{
1089 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001090 unsigned int maxAttrs;
1091 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001092
1093 if (state == NULL)
1094 return(NULL);
Daniel Veillard798024a2003-03-19 10:36:09 +00001095 if ((ctxt->freeState != NULL) &&
1096 (ctxt->freeState->nbState > 0)) {
1097 ctxt->freeState->nbState--;
1098 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1099 } else {
1100 ret = (xmlRelaxNGValidStatePtr) xmlMalloc(sizeof(xmlRelaxNGValidState));
1101 if (ret == NULL) {
1102 if ((ctxt != NULL) && (ctxt->error != NULL))
1103 ctxt->error(ctxt->userData, "Out of memory\n");
1104 return (NULL);
1105 }
1106 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001107 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001108 attrs = ret->attrs;
1109 maxAttrs = ret->maxAttrs;
1110 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1111 ret->attrs = attrs;
1112 ret->maxAttrs = maxAttrs;
1113 if (state->nbAttrs > 0) {
1114 if (ret->attrs == NULL) {
1115 ret->maxAttrs = state->maxAttrs;
1116 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1117 sizeof(xmlAttrPtr));
1118 if (ret->attrs == NULL) {
1119 if ((ctxt != NULL) && (ctxt->error != NULL))
1120 ctxt->error(ctxt->userData, "Out of memory\n");
1121 ret->nbAttrs = 0;
1122 return (ret);
1123 }
1124 } else if (ret->maxAttrs < state->nbAttrs) {
1125 xmlAttrPtr *tmp;
1126
1127 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1128 sizeof(xmlAttrPtr));
1129 if (tmp == NULL) {
1130 if ((ctxt != NULL) && (ctxt->error != NULL))
1131 ctxt->error(ctxt->userData, "Out of memory\n");
1132 ret->nbAttrs = 0;
1133 return (ret);
1134 }
1135 ret->maxAttrs = state->maxAttrs;
1136 }
1137 memcpy(ret->attrs, state->attrs, state->nbAttrs * sizeof(xmlAttrPtr));
1138 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001139 return(ret);
1140}
1141
1142/**
1143 * xmlRelaxNGEqualValidState:
1144 * @ctxt: a Relax-NG validation context
1145 * @state1: a validation state
1146 * @state2: a validation state
1147 *
1148 * Compare the validation states for equality
1149 *
1150 * Returns 1 if equald, 0 otherwise
1151 */
1152static int
1153xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1154 xmlRelaxNGValidStatePtr state1,
1155 xmlRelaxNGValidStatePtr state2)
1156{
1157 int i;
1158
1159 if ((state1 == NULL) || (state2 == NULL))
1160 return(0);
1161 if (state1 == state2)
1162 return(1);
1163 if (state1->node != state2->node)
1164 return(0);
1165 if (state1->seq != state2->seq)
1166 return(0);
1167 if (state1->nbAttrLeft != state2->nbAttrLeft)
1168 return(0);
1169 if (state1->nbAttrs != state2->nbAttrs)
1170 return(0);
1171 if (state1->endvalue != state2->endvalue)
1172 return(0);
1173 if ((state1->value != state2->value) &&
1174 (!xmlStrEqual(state1->value, state2->value)))
1175 return(0);
1176 for (i = 0;i < state1->nbAttrs;i++) {
1177 if (state1->attrs[i] != state2->attrs[i])
1178 return(0);
1179 }
1180 return(1);
1181}
1182
1183/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001184 * xmlRelaxNGFreeValidState:
1185 * @state: a validation state structure
1186 *
1187 * Deallocate a RelaxNG validation state structure.
1188 */
1189static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001190xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1191 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001192{
1193 if (state == NULL)
1194 return;
1195
Daniel Veillard798024a2003-03-19 10:36:09 +00001196 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1197 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1198 }
1199 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1200 if (state->attrs != NULL)
1201 xmlFree(state->attrs);
1202 xmlFree(state);
1203 } else {
1204 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1205 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001206}
1207
1208/************************************************************************
1209 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001210 * Document functions *
1211 * *
1212 ************************************************************************/
1213static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1214 xmlDocPtr doc);
1215
1216/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001217 * xmlRelaxNGIncludePush:
1218 * @ctxt: the parser context
1219 * @value: the element doc
1220 *
1221 * Pushes a new include on top of the include stack
1222 *
1223 * Returns 0 in case of error, the index in the stack otherwise
1224 */
1225static int
1226xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1227 xmlRelaxNGIncludePtr value)
1228{
1229 if (ctxt->incTab == NULL) {
1230 ctxt->incMax = 4;
1231 ctxt->incNr = 0;
1232 ctxt->incTab = (xmlRelaxNGIncludePtr *) xmlMalloc(
1233 ctxt->incMax * sizeof(ctxt->incTab[0]));
1234 if (ctxt->incTab == NULL) {
1235 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1236 return (0);
1237 }
1238 }
1239 if (ctxt->incNr >= ctxt->incMax) {
1240 ctxt->incMax *= 2;
1241 ctxt->incTab =
1242 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1243 ctxt->incMax *
1244 sizeof(ctxt->incTab[0]));
1245 if (ctxt->incTab == NULL) {
1246 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1247 return (0);
1248 }
1249 }
1250 ctxt->incTab[ctxt->incNr] = value;
1251 ctxt->inc = value;
1252 return (ctxt->incNr++);
1253}
1254
1255/**
1256 * xmlRelaxNGIncludePop:
1257 * @ctxt: the parser context
1258 *
1259 * Pops the top include from the include stack
1260 *
1261 * Returns the include just removed
1262 */
1263static xmlRelaxNGIncludePtr
1264xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1265{
1266 xmlRelaxNGIncludePtr ret;
1267
1268 if (ctxt->incNr <= 0)
1269 return (0);
1270 ctxt->incNr--;
1271 if (ctxt->incNr > 0)
1272 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1273 else
1274 ctxt->inc = NULL;
1275 ret = ctxt->incTab[ctxt->incNr];
1276 ctxt->incTab[ctxt->incNr] = 0;
1277 return (ret);
1278}
1279
1280/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001281 * xmlRelaxNGRemoveRedefine:
1282 * @ctxt: the parser context
1283 * @URL: the normalized URL
1284 * @target: the included target
1285 * @name: the define name to eliminate
1286 *
1287 * Applies the elimination algorithm of 4.7
1288 *
1289 * Returns 0 in case of error, 1 in case of success.
1290 */
1291static int
1292xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1293 const xmlChar *URL ATTRIBUTE_UNUSED,
1294 xmlNodePtr target, const xmlChar *name) {
1295 int found = 0;
1296 xmlNodePtr tmp, tmp2;
1297 xmlChar *name2;
1298
1299#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001300 if (name == NULL)
1301 xmlGenericError(xmlGenericErrorContext,
1302 "Elimination of <include> start from %s\n", URL);
1303 else
1304 xmlGenericError(xmlGenericErrorContext,
1305 "Elimination of <include> define %s from %s\n", name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001306#endif
1307 tmp = target;
1308 while (tmp != NULL) {
1309 tmp2 = tmp->next;
1310 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1311 found = 1;
1312 xmlUnlinkNode(tmp);
1313 xmlFreeNode(tmp);
1314 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1315 name2 = xmlGetProp(tmp, BAD_CAST "name");
1316 xmlRelaxNGNormExtSpace(name2);
1317 if (name2 != NULL) {
1318 if (xmlStrEqual(name, name2)) {
1319 found = 1;
1320 xmlUnlinkNode(tmp);
1321 xmlFreeNode(tmp);
1322 }
1323 xmlFree(name2);
1324 }
1325 } else if (IS_RELAXNG(tmp, "include")) {
1326 xmlChar *href = NULL;
1327 xmlRelaxNGDocumentPtr inc = tmp->_private;
1328
1329 if ((inc != NULL) && (inc->doc != NULL) &&
1330 (inc->doc->children != NULL)) {
1331
1332 if (xmlStrEqual(inc->doc->children->name, BAD_CAST "grammar")) {
1333#ifdef DEBUG_INCLUDE
1334 href = xmlGetProp(tmp, BAD_CAST "href");
1335#endif
1336 if (xmlRelaxNGRemoveRedefine(ctxt, href,
1337 inc->doc->children->children, name) == 1) {
1338 found = 1;
1339 }
1340 if (href != NULL)
1341 xmlFree(href);
1342 }
1343 }
1344 }
1345 tmp = tmp2;
1346 }
1347 return(found);
1348}
1349
1350/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001351 * xmlRelaxNGLoadInclude:
1352 * @ctxt: the parser context
1353 * @URL: the normalized URL
1354 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001355 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001356 *
1357 * First lookup if the document is already loaded into the parser context,
1358 * check against recursion. If not found the resource is loaded and
1359 * the content is preprocessed before being returned back to the caller.
1360 *
1361 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1362 */
1363static xmlRelaxNGIncludePtr
1364xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillard416589a2003-02-17 17:25:42 +00001365 xmlNodePtr node, const xmlChar *ns) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001366 xmlRelaxNGIncludePtr ret = NULL;
1367 xmlDocPtr doc;
1368 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001369 xmlNodePtr root, cur;
1370
1371#ifdef DEBUG_INCLUDE
1372 xmlGenericError(xmlGenericErrorContext,
1373 "xmlRelaxNGLoadInclude(%s)\n", URL);
1374#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001375
1376 /*
1377 * check against recursion in the stack
1378 */
1379 for (i = 0;i < ctxt->incNr;i++) {
1380 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1381 if (ctxt->error != NULL)
1382 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00001383 "Detected an Include recursion for %s\n",
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001384 URL);
1385 ctxt->nbErrors++;
1386 return(NULL);
1387 }
1388 }
1389
1390 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001391 * load the document
1392 */
1393 doc = xmlParseFile((const char *) URL);
1394 if (doc == NULL) {
1395 if (ctxt->error != NULL)
1396 ctxt->error(ctxt->userData,
1397 "xmlRelaxNG: could not load %s\n", URL);
1398 ctxt->nbErrors++;
1399 return (NULL);
1400 }
1401
Daniel Veillard5add8682003-03-10 13:13:58 +00001402#ifdef DEBUG_INCLUDE
1403 xmlGenericError(xmlGenericErrorContext,
1404 "Parsed %s Okay\n", URL);
1405#endif
1406
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001407 /*
1408 * Allocate the document structures and register it first.
1409 */
1410 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1411 if (ret == NULL) {
1412 if (ctxt->error != NULL)
1413 ctxt->error(ctxt->userData,
1414 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1415 ctxt->nbErrors++;
1416 xmlFreeDoc(doc);
1417 return (NULL);
1418 }
1419 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1420 ret->doc = doc;
1421 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001422 ret->next = ctxt->includes;
1423 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001424
1425 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001426 * transmit the ns if needed
1427 */
1428 if (ns != NULL) {
1429 root = xmlDocGetRootElement(doc);
1430 if (root != NULL) {
1431 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1432 xmlSetProp(root, BAD_CAST"ns", ns);
1433 }
1434 }
1435 }
1436
1437 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001438 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001439 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001440 xmlRelaxNGIncludePush(ctxt, ret);
1441
1442 /*
1443 * Some preprocessing of the document content, this include recursing
1444 * in the include stack.
1445 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001446#ifdef DEBUG_INCLUDE
1447 xmlGenericError(xmlGenericErrorContext,
1448 "cleanup of %s\n", URL);
1449#endif
1450
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001451 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1452 if (doc == NULL) {
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001453 ctxt->inc = NULL;
1454 return(NULL);
1455 }
1456
1457 /*
1458 * Pop up the include from the stack
1459 */
1460 xmlRelaxNGIncludePop(ctxt);
1461
Daniel Veillard5add8682003-03-10 13:13:58 +00001462#ifdef DEBUG_INCLUDE
1463 xmlGenericError(xmlGenericErrorContext,
1464 "Checking of %s\n", URL);
1465#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001466 /*
1467 * Check that the top element is a grammar
1468 */
1469 root = xmlDocGetRootElement(doc);
1470 if (root == NULL) {
1471 if (ctxt->error != NULL)
1472 ctxt->error(ctxt->userData,
1473 "xmlRelaxNG: included document is empty %s\n", URL);
1474 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001475 return (NULL);
1476 }
1477 if (!IS_RELAXNG(root, "grammar")) {
1478 if (ctxt->error != NULL)
1479 ctxt->error(ctxt->userData,
1480 "xmlRelaxNG: included document %s root is not a grammar\n",
1481 URL);
1482 ctxt->nbErrors++;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001483 return (NULL);
1484 }
1485
1486 /*
1487 * Elimination of redefined rules in the include.
1488 */
1489 cur = node->children;
1490 while (cur != NULL) {
1491 if (IS_RELAXNG(cur, "start")) {
1492 int found = 0;
1493
Daniel Veillard5add8682003-03-10 13:13:58 +00001494 found = xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001495 if (!found) {
1496 if (ctxt->error != NULL)
1497 ctxt->error(ctxt->userData,
1498 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1499 URL);
1500 ctxt->nbErrors++;
1501 }
1502 } else if (IS_RELAXNG(cur, "define")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001503 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001504
1505 name = xmlGetProp(cur, BAD_CAST "name");
1506 if (name == NULL) {
1507 if (ctxt->error != NULL)
1508 ctxt->error(ctxt->userData,
1509 "xmlRelaxNG: include %s has define without name\n",
1510 URL);
1511 ctxt->nbErrors++;
1512 } else {
Daniel Veillard5add8682003-03-10 13:13:58 +00001513 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001514
Daniel Veillardd2298792003-02-14 16:54:11 +00001515 xmlRelaxNGNormExtSpace(name);
Daniel Veillard5add8682003-03-10 13:13:58 +00001516 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1517 root->children, name);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001518 if (!found) {
1519 if (ctxt->error != NULL)
1520 ctxt->error(ctxt->userData,
1521 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1522 URL, name);
1523 ctxt->nbErrors++;
1524 }
1525 xmlFree(name);
1526 }
1527 }
1528 cur = cur->next;
1529 }
1530
1531
1532 return(ret);
1533}
1534
1535/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001536 * xmlRelaxNGValidErrorPush:
1537 * @ctxt: the validation context
1538 * @err: the error code
1539 * @arg1: the first string argument
1540 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001541 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001542 *
1543 * Pushes a new error on top of the error stack
1544 *
1545 * Returns 0 in case of error, the index in the stack otherwise
1546 */
1547static int
1548xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001549 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001550{
1551 xmlRelaxNGValidErrorPtr cur;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001552#ifdef DEBUG_ERROR
1553 xmlGenericError(xmlGenericErrorContext,
1554 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
1555#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001556 if (ctxt->errTab == NULL) {
1557 ctxt->errMax = 8;
1558 ctxt->errNr = 0;
1559 ctxt->errTab = (xmlRelaxNGValidErrorPtr) xmlMalloc(
1560 ctxt->errMax * sizeof(xmlRelaxNGValidError));
1561 if (ctxt->errTab == NULL) {
1562 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1563 return (0);
1564 }
Daniel Veillard20863822003-03-22 17:51:47 +00001565 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001566 }
1567 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard20863822003-03-22 17:51:47 +00001568 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001569 ctxt->errTab =
1570 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard20863822003-03-22 17:51:47 +00001571 ctxt->errMax * sizeof(xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001572 if (ctxt->errTab == NULL) {
1573 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1574 return (0);
1575 }
Daniel Veillard20863822003-03-22 17:51:47 +00001576 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001577 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001578 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00001579 (ctxt->err->node == ctxt->state->node) &&
1580 (ctxt->err->err == err))
1581 return(ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001582 cur = &ctxt->errTab[ctxt->errNr];
1583 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001584 if (dup) {
1585 cur->arg1 = xmlStrdup(arg1);
1586 cur->arg2 = xmlStrdup(arg2);
1587 cur->flags = ERROR_IS_DUP;
1588 } else {
1589 cur->arg1 = arg1;
1590 cur->arg2 = arg2;
1591 cur->flags = 0;
1592 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001593 if (ctxt->state != NULL) {
1594 cur->node = ctxt->state->node;
1595 cur->seq = ctxt->state->seq;
1596 } else {
1597 cur->node = NULL;
1598 cur->seq = NULL;
1599 }
1600 ctxt->err = cur;
1601 return (ctxt->errNr++);
1602}
1603
1604/**
1605 * xmlRelaxNGValidErrorPop:
1606 * @ctxt: the validation context
1607 *
1608 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001609 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001610static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001611xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1612{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001613 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001614
Daniel Veillard580ced82003-03-21 21:22:48 +00001615 if (ctxt->errNr <= 0) {
1616 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001617 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001618 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001619 ctxt->errNr--;
1620 if (ctxt->errNr > 0)
1621 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1622 else
1623 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001624 cur = &ctxt->errTab[ctxt->errNr];
1625 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001626 if (cur->arg1 != NULL)
1627 xmlFree((xmlChar *)cur->arg1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001628 cur->arg1 = NULL;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001629 if (cur->arg2 != NULL)
1630 xmlFree((xmlChar *)cur->arg2);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001631 cur->arg2 = NULL;
1632 cur->flags = 0;
1633 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001634}
1635
Daniel Veillard42f12e92003-03-07 18:32:59 +00001636/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001637 * xmlRelaxNGDocumentPush:
1638 * @ctxt: the parser context
1639 * @value: the element doc
1640 *
1641 * Pushes a new doc on top of the doc stack
1642 *
1643 * Returns 0 in case of error, the index in the stack otherwise
1644 */
1645static int
1646xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1647 xmlRelaxNGDocumentPtr value)
1648{
1649 if (ctxt->docTab == NULL) {
1650 ctxt->docMax = 4;
1651 ctxt->docNr = 0;
1652 ctxt->docTab = (xmlRelaxNGDocumentPtr *) xmlMalloc(
1653 ctxt->docMax * sizeof(ctxt->docTab[0]));
1654 if (ctxt->docTab == NULL) {
1655 xmlGenericError(xmlGenericErrorContext, "malloc failed !\n");
1656 return (0);
1657 }
1658 }
1659 if (ctxt->docNr >= ctxt->docMax) {
1660 ctxt->docMax *= 2;
1661 ctxt->docTab =
1662 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1663 ctxt->docMax *
1664 sizeof(ctxt->docTab[0]));
1665 if (ctxt->docTab == NULL) {
1666 xmlGenericError(xmlGenericErrorContext, "realloc failed !\n");
1667 return (0);
1668 }
1669 }
1670 ctxt->docTab[ctxt->docNr] = value;
1671 ctxt->doc = value;
1672 return (ctxt->docNr++);
1673}
1674
1675/**
1676 * xmlRelaxNGDocumentPop:
1677 * @ctxt: the parser context
1678 *
1679 * Pops the top doc from the doc stack
1680 *
1681 * Returns the doc just removed
1682 */
1683static xmlRelaxNGDocumentPtr
1684xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1685{
1686 xmlRelaxNGDocumentPtr ret;
1687
1688 if (ctxt->docNr <= 0)
1689 return (0);
1690 ctxt->docNr--;
1691 if (ctxt->docNr > 0)
1692 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1693 else
1694 ctxt->doc = NULL;
1695 ret = ctxt->docTab[ctxt->docNr];
1696 ctxt->docTab[ctxt->docNr] = 0;
1697 return (ret);
1698}
1699
1700/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001701 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001702 * @ctxt: the parser context
1703 * @URL: the normalized URL
1704 * @ns: the inherited ns if any
1705 *
1706 * First lookup if the document is already loaded into the parser context,
1707 * check against recursion. If not found the resource is loaded and
1708 * the content is preprocessed before being returned back to the caller.
1709 *
1710 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1711 */
1712static xmlRelaxNGDocumentPtr
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001713xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *URL,
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001714 const xmlChar *ns) {
1715 xmlRelaxNGDocumentPtr ret = NULL;
1716 xmlDocPtr doc;
1717 xmlNodePtr root;
1718 int i;
1719
1720 /*
1721 * check against recursion in the stack
1722 */
1723 for (i = 0;i < ctxt->docNr;i++) {
1724 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1725 if (ctxt->error != NULL)
1726 ctxt->error(ctxt->userData,
1727 "Detected an externalRef recursion for %s\n",
1728 URL);
1729 ctxt->nbErrors++;
1730 return(NULL);
1731 }
1732 }
1733
1734 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001735 * load the document
1736 */
1737 doc = xmlParseFile((const char *) URL);
1738 if (doc == NULL) {
1739 if (ctxt->error != NULL)
1740 ctxt->error(ctxt->userData,
1741 "xmlRelaxNG: could not load %s\n", URL);
1742 ctxt->nbErrors++;
1743 return (NULL);
1744 }
1745
1746 /*
1747 * Allocate the document structures and register it first.
1748 */
1749 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1750 if (ret == NULL) {
1751 if (ctxt->error != NULL)
1752 ctxt->error(ctxt->userData,
1753 "xmlRelaxNG: allocate memory for doc %s\n", URL);
1754 ctxt->nbErrors++;
1755 xmlFreeDoc(doc);
1756 return (NULL);
1757 }
1758 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1759 ret->doc = doc;
1760 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001761 ret->next = ctxt->documents;
1762 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001763
1764 /*
1765 * transmit the ns if needed
1766 */
1767 if (ns != NULL) {
1768 root = xmlDocGetRootElement(doc);
1769 if (root != NULL) {
1770 if (xmlHasProp(root, BAD_CAST"ns") == NULL) {
1771 xmlSetProp(root, BAD_CAST"ns", ns);
1772 }
1773 }
1774 }
1775
1776 /*
1777 * push it on the stack and register it in the hash table
1778 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001779 xmlRelaxNGDocumentPush(ctxt, ret);
1780
1781 /*
1782 * Some preprocessing of the document content
1783 */
1784 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1785 if (doc == NULL) {
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001786 ctxt->doc = NULL;
1787 return(NULL);
1788 }
1789
1790 xmlRelaxNGDocumentPop(ctxt);
1791
1792 return(ret);
1793}
1794
1795/************************************************************************
1796 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00001797 * Error functions *
1798 * *
1799 ************************************************************************/
1800
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001801#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
1802#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
1803#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
1804#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
1805#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001806
Daniel Veillardfd573f12003-03-16 17:52:32 +00001807#ifdef DEBUG
Daniel Veillard231d7912003-02-09 14:22:17 +00001808static const char *
1809xmlRelaxNGDefName(xmlRelaxNGDefinePtr def) {
1810 if (def == NULL)
1811 return("none");
1812 switch(def->type) {
1813 case XML_RELAXNG_EMPTY: return("empty");
1814 case XML_RELAXNG_NOT_ALLOWED: return("notAllowed");
1815 case XML_RELAXNG_EXCEPT: return("except");
1816 case XML_RELAXNG_TEXT: return("text");
1817 case XML_RELAXNG_ELEMENT: return("element");
1818 case XML_RELAXNG_DATATYPE: return("datatype");
1819 case XML_RELAXNG_VALUE: return("value");
1820 case XML_RELAXNG_LIST: return("list");
1821 case XML_RELAXNG_ATTRIBUTE: return("attribute");
1822 case XML_RELAXNG_DEF: return("def");
1823 case XML_RELAXNG_REF: return("ref");
1824 case XML_RELAXNG_EXTERNALREF: return("externalRef");
1825 case XML_RELAXNG_PARENTREF: return("parentRef");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001826 case XML_RELAXNG_OPTIONAL: return("optional");
1827 case XML_RELAXNG_ZEROORMORE: return("zeroOrMore");
Daniel Veillard231d7912003-02-09 14:22:17 +00001828 case XML_RELAXNG_ONEORMORE: return("oneOrMore");
1829 case XML_RELAXNG_CHOICE: return("choice");
1830 case XML_RELAXNG_GROUP: return("group");
1831 case XML_RELAXNG_INTERLEAVE: return("interleave");
1832 case XML_RELAXNG_START: return("start");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001833 case XML_RELAXNG_NOOP: return("noop");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00001834 case XML_RELAXNG_PARAM: return("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00001835 }
1836 return("unknown");
1837}
Daniel Veillardfd573f12003-03-16 17:52:32 +00001838#endif
Daniel Veillardd2298792003-02-14 16:54:11 +00001839
Daniel Veillard6eadf632003-01-23 18:29:16 +00001840/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001841 * xmlRelaxNGGetErrorString:
1842 * @err: the error code
1843 * @arg1: the first string argument
1844 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00001845 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00001846 * computes a formatted error string for the given error code and args
1847 *
1848 * Returns the error string, it must be deallocated by the caller
1849 */
1850static xmlChar *
Daniel Veillardfd573f12003-03-16 17:52:32 +00001851xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar *arg1,
1852 const xmlChar *arg2) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00001853 char msg[1000];
1854
1855 if (arg1 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001856 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001857 if (arg2 == NULL)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001858 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00001859
1860 msg[0] = 0;
1861 switch (err) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001862 case XML_RELAXNG_OK:
1863 return(NULL);
1864 case XML_RELAXNG_ERR_MEMORY:
1865 return(xmlCharStrdup("out of memory"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001866 case XML_RELAXNG_ERR_TYPE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00001867 snprintf(msg, 1000, "failed to validate type %s", arg1);
1868 break;
1869 case XML_RELAXNG_ERR_TYPEVAL:
Daniel Veillardc4c21552003-03-29 10:53:38 +00001870 snprintf(msg, 1000, "Type %s doesn't allow value '%s'", arg1, arg2);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001871 break;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001872 case XML_RELAXNG_ERR_DUPID:
1873 snprintf(msg, 1000, "ID %s redefined", arg1);
1874 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001875 case XML_RELAXNG_ERR_TYPECMP:
1876 snprintf(msg, 1000, "failed to compare type %s", arg1);
1877 break;
1878 case XML_RELAXNG_ERR_NOSTATE:
1879 return(xmlCharStrdup("Internal error: no state"));
1880 case XML_RELAXNG_ERR_NODEFINE:
1881 return(xmlCharStrdup("Internal error: no define"));
Daniel Veillard952379b2003-03-17 15:37:12 +00001882 case XML_RELAXNG_ERR_INTERNAL:
1883 snprintf(msg, 1000, "Internal error: %s", arg1);
1884 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001885 case XML_RELAXNG_ERR_LISTEXTRA:
1886 snprintf(msg, 1000, "Extra data in list: %s", arg1);
1887 break;
1888 case XML_RELAXNG_ERR_INTERNODATA:
1889 return(xmlCharStrdup("Internal: interleave block has no data"));
1890 case XML_RELAXNG_ERR_INTERSEQ:
1891 return(xmlCharStrdup("Invalid sequence in interleave"));
1892 case XML_RELAXNG_ERR_INTEREXTRA:
1893 snprintf(msg, 1000, "Extra element %s in interleave", arg1);
1894 break;
1895 case XML_RELAXNG_ERR_ELEMNAME:
1896 snprintf(msg, 1000, "Expecting element %s, got %s", arg1, arg2);
1897 break;
1898 case XML_RELAXNG_ERR_ELEMNONS:
1899 snprintf(msg, 1000, "Expecting a namespace for element %s", arg1);
1900 break;
1901 case XML_RELAXNG_ERR_ELEMWRONGNS:
1902 snprintf(msg, 1000, "Element %s has wrong namespace: expecting %s",
1903 arg1, arg2);
1904 break;
1905 case XML_RELAXNG_ERR_ELEMEXTRANS:
1906 snprintf(msg, 1000, "Expecting no namespace for element %s", arg1);
1907 break;
1908 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
1909 snprintf(msg, 1000, "Expecting element %s to be empty", arg1);
1910 break;
1911 case XML_RELAXNG_ERR_NOELEM:
1912 snprintf(msg, 1000, "Expecting an element %s, got nothing", arg1);
1913 break;
1914 case XML_RELAXNG_ERR_NOTELEM:
1915 return(xmlCharStrdup("Expecting an element got text"));
1916 case XML_RELAXNG_ERR_ATTRVALID:
1917 snprintf(msg, 1000, "Element %s failed to validate attributes",
1918 arg1);
1919 break;
1920 case XML_RELAXNG_ERR_CONTENTVALID:
1921 snprintf(msg, 1000, "Element %s failed to validate content",
1922 arg1);
1923 break;
1924 case XML_RELAXNG_ERR_EXTRACONTENT:
1925 snprintf(msg, 1000, "Element %s has extra content: %s",
1926 arg1, arg2);
1927 break;
1928 case XML_RELAXNG_ERR_INVALIDATTR:
1929 snprintf(msg, 1000, "Invalid attribute %s for element %s",
1930 arg1, arg2);
1931 break;
Daniel Veillardc4c21552003-03-29 10:53:38 +00001932 case XML_RELAXNG_ERR_LACKDATA:
1933 snprintf(msg, 1000, "Datatype element %s contains no data",
1934 arg1);
1935 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001936 case XML_RELAXNG_ERR_DATAELEM:
1937 snprintf(msg, 1000, "Datatype element %s has child elements",
1938 arg1);
1939 break;
1940 case XML_RELAXNG_ERR_VALELEM:
1941 snprintf(msg, 1000, "Value element %s has child elements",
1942 arg1);
1943 break;
1944 case XML_RELAXNG_ERR_LISTELEM:
1945 snprintf(msg, 1000, "List element %s has child elements",
1946 arg1);
1947 break;
1948 case XML_RELAXNG_ERR_DATATYPE:
1949 snprintf(msg, 1000, "Error validating datatype %s",
1950 arg1);
1951 break;
1952 case XML_RELAXNG_ERR_VALUE:
1953 snprintf(msg, 1000, "Error validating value %s",
1954 arg1);
1955 break;
1956 case XML_RELAXNG_ERR_LIST:
1957 return(xmlCharStrdup("Error validating list"));
1958 case XML_RELAXNG_ERR_NOGRAMMAR:
1959 return(xmlCharStrdup("No top grammar defined"));
1960 case XML_RELAXNG_ERR_EXTRADATA:
1961 return(xmlCharStrdup("Extra data in the document"));
1962 default:
1963 TODO
Daniel Veillard42f12e92003-03-07 18:32:59 +00001964 }
1965 if (msg[0] == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00001966 snprintf(msg, 1000, "Unknown error code %d", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001967 }
1968 msg[1000] = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001969 return(xmlStrdup((xmlChar *) msg));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001970}
1971
1972/**
1973 * xmlRelaxNGValidErrorContext:
1974 * @ctxt: the validation context
1975 * @node: the node
1976 * @child: the node child generating the problem.
1977 *
1978 * Dump informations about the kocation of the error in the instance
Daniel Veillard6eadf632003-01-23 18:29:16 +00001979 */
1980static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001981xmlRelaxNGValidErrorContext(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node,
1982 xmlNodePtr child)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001983{
1984 int line = 0;
1985 const xmlChar *file = NULL;
1986 const xmlChar *name = NULL;
1987 const char *type = "error";
1988
1989 if ((ctxt == NULL) || (ctxt->error == NULL))
1990 return;
1991
1992 if (child != NULL)
1993 node = child;
1994
1995 if (node != NULL) {
1996 if ((node->type == XML_DOCUMENT_NODE) ||
1997 (node->type == XML_HTML_DOCUMENT_NODE)) {
1998 xmlDocPtr doc = (xmlDocPtr) node;
1999
2000 file = doc->URL;
2001 } else {
2002 /*
2003 * Try to find contextual informations to report
2004 */
2005 if (node->type == XML_ELEMENT_NODE) {
2006 line = (int) node->content;
2007 } else if ((node->prev != NULL) &&
2008 (node->prev->type == XML_ELEMENT_NODE)) {
2009 line = (int) node->prev->content;
2010 } else if ((node->parent != NULL) &&
2011 (node->parent->type == XML_ELEMENT_NODE)) {
2012 line = (int) node->parent->content;
2013 }
2014 if ((node->doc != NULL) && (node->doc->URL != NULL))
2015 file = node->doc->URL;
2016 if (node->name != NULL)
2017 name = node->name;
2018 }
2019 }
2020
Daniel Veillard42f12e92003-03-07 18:32:59 +00002021 type = "RNG validity error";
Daniel Veillard6eadf632003-01-23 18:29:16 +00002022
2023 if ((file != NULL) && (line != 0) && (name != NULL))
2024 ctxt->error(ctxt->userData, "%s: file %s line %d element %s\n",
2025 type, file, line, name);
2026 else if ((file != NULL) && (name != NULL))
2027 ctxt->error(ctxt->userData, "%s: file %s element %s\n",
2028 type, file, name);
2029 else if ((file != NULL) && (line != 0))
2030 ctxt->error(ctxt->userData, "%s: file %s line %d\n", type, file, line);
2031 else if (file != NULL)
2032 ctxt->error(ctxt->userData, "%s: file %s\n", type, file);
2033 else if (name != NULL)
2034 ctxt->error(ctxt->userData, "%s: element %s\n", type, name);
2035 else
2036 ctxt->error(ctxt->userData, "%s\n", type);
2037}
Daniel Veillard42f12e92003-03-07 18:32:59 +00002038
2039/**
2040 * xmlRelaxNGShowValidError:
2041 * @ctxt: the validation context
2042 * @err: the error number
2043 * @node: the node
2044 * @child: the node child generating the problem.
2045 * @arg1: the first argument
2046 * @arg2: the second argument
2047 *
2048 * Show a validation error.
2049 */
2050static void
2051xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
2052 xmlNodePtr node, xmlNodePtr child,
2053 const xmlChar *arg1, const xmlChar *arg2)
2054{
2055 xmlChar *msg;
2056
2057 if (ctxt->error == NULL)
2058 return;
2059
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002060#ifdef DEBUG_ERROR
2061 xmlGenericError(xmlGenericErrorContext,
2062 "Show error %d\n", err);
2063#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002064 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2065 if (msg == NULL)
2066 return;
2067
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002068 if (ctxt->errNo == XML_RELAXNG_OK)
2069 ctxt->errNo = err;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002070 xmlRelaxNGValidErrorContext(ctxt, node, child);
2071 ctxt->error(ctxt->userData, "%s\n", msg);
2072 xmlFree(msg);
2073}
2074
2075/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002076 * xmlRelaxNGPopErrors:
2077 * @ctxt: the validation context
2078 * @level: the error level in the stack
2079 *
2080 * pop and discard all errors until the given level is reached
2081 */
2082static void
2083xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level) {
2084 int i;
2085 xmlRelaxNGValidErrorPtr err;
2086
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002087#ifdef DEBUG_ERROR
2088 xmlGenericError(xmlGenericErrorContext,
2089 "Pop errors till level %d\n", level);
2090#endif
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002091 for (i = level;i < ctxt->errNr;i++) {
2092 err = &ctxt->errTab[i];
2093 if (err->flags & ERROR_IS_DUP) {
2094 if (err->arg1 != NULL)
2095 xmlFree((xmlChar *)err->arg1);
2096 err->arg1 = NULL;
2097 if (err->arg2 != NULL)
2098 xmlFree((xmlChar *)err->arg2);
2099 err->arg2 = NULL;
2100 err->flags = 0;
2101 }
2102 }
2103 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002104 if (ctxt->errNr <= 0)
2105 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002106}
2107/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002108 * xmlRelaxNGDumpValidError:
2109 * @ctxt: the validation context
2110 *
2111 * Show all validation error over a given index.
2112 */
2113static void
2114xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard580ced82003-03-21 21:22:48 +00002115 int i, j;
2116 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002117
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002118#ifdef DEBUG_ERROR
2119 xmlGenericError(xmlGenericErrorContext,
2120 "Dumping error stack %d errors\n", ctxt->errNr);
2121#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002122 for (i = 0;i < ctxt->errNr;i++) {
2123 err = &ctxt->errTab[i];
Daniel Veillard580ced82003-03-21 21:22:48 +00002124 for (j = 0;j < i;j++) {
2125 dup = &ctxt->errTab[j];
2126 if ((err->err == dup->err) && (err->node == dup->node) &&
2127 (xmlStrEqual(err->arg1, dup->arg1)) &&
2128 (xmlStrEqual(err->arg2, dup->arg2))) {
2129 goto skip;
2130 }
2131 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002132 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2133 err->arg1, err->arg2);
Daniel Veillard580ced82003-03-21 21:22:48 +00002134skip:
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002135 if (err->flags & ERROR_IS_DUP) {
2136 if (err->arg1 != NULL)
2137 xmlFree((xmlChar *)err->arg1);
2138 err->arg1 = NULL;
2139 if (err->arg2 != NULL)
2140 xmlFree((xmlChar *)err->arg2);
2141 err->arg2 = NULL;
2142 err->flags = 0;
2143 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002144 }
2145 ctxt->errNr = 0;
2146}
2147/**
2148 * xmlRelaxNGAddValidError:
2149 * @ctxt: the validation context
2150 * @err: the error number
2151 * @arg1: the first argument
2152 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002153 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002154 *
2155 * Register a validation error, either generating it if it's sure
2156 * or stacking it for later handling if unsure.
2157 */
2158static void
2159xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt, xmlRelaxNGValidErr err,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002160 const xmlChar *arg1, const xmlChar *arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002161{
2162 if ((ctxt == NULL) || (ctxt->error == NULL))
2163 return;
2164
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002165#ifdef DEBUG_ERROR
2166 xmlGenericError(xmlGenericErrorContext,
2167 "Adding error %d\n", err);
2168#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002169 /*
2170 * generate the error directly
2171 */
2172 if (((ctxt->flags & 1) == 0) || (ctxt->flags & 2)) {
2173 xmlNodePtr node, seq;
2174 /*
2175 * Flush first any stacked error which might be the
2176 * real cause of the problem.
2177 */
2178 if (ctxt->errNr != 0)
2179 xmlRelaxNGDumpValidError(ctxt);
2180 if (ctxt->state != NULL) {
2181 node = ctxt->state->node;
2182 seq = ctxt->state->seq;
2183 } else {
2184 node = seq = NULL;
2185 }
2186 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2187 }
2188 /*
2189 * Stack the error for later processing if needed
2190 */
2191 else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002192 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002193 }
2194}
2195
Daniel Veillard6eadf632003-01-23 18:29:16 +00002196
2197/************************************************************************
2198 * *
2199 * Type library hooks *
2200 * *
2201 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002202static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2203 const xmlChar *str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002204
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002205/**
2206 * xmlRelaxNGSchemaTypeHave:
2207 * @data: data needed for the library
2208 * @type: the type name
2209 *
2210 * Check if the given type is provided by
2211 * the W3C XMLSchema Datatype library.
2212 *
2213 * Returns 1 if yes, 0 if no and -1 in case of error.
2214 */
2215static int
2216xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002217 const xmlChar *type) {
2218 xmlSchemaTypePtr typ;
2219
2220 if (type == NULL)
2221 return(-1);
2222 typ = xmlSchemaGetPredefinedType(type,
2223 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2224 if (typ == NULL)
2225 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002226 return(1);
2227}
2228
2229/**
2230 * xmlRelaxNGSchemaTypeCheck:
2231 * @data: data needed for the library
2232 * @type: the type name
2233 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002234 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002235 *
2236 * Check if the given type and value are validated by
2237 * the W3C XMLSchema Datatype library.
2238 *
2239 * Returns 1 if yes, 0 if no and -1 in case of error.
2240 */
2241static int
2242xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002243 const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002244 const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002245 void **result,
2246 xmlNodePtr node) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002247 xmlSchemaTypePtr typ;
2248 int ret;
2249
2250 /*
2251 * TODO: the type should be cached ab provided back, interface subject
2252 * to changes.
2253 * TODO: handle facets, may require an additional interface and keep
2254 * the value returned from the validation.
2255 */
2256 if ((type == NULL) || (value == NULL))
2257 return(-1);
2258 typ = xmlSchemaGetPredefinedType(type,
2259 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2260 if (typ == NULL)
2261 return(-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002262 ret = xmlSchemaValPredefTypeNode(typ, value,
2263 (xmlSchemaValPtr *) result, node);
2264 if (ret == 2) /* special ID error code */
2265 return(2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002266 if (ret == 0)
2267 return(1);
2268 if (ret > 0)
2269 return(0);
2270 return(-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002271}
2272
2273/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002274 * xmlRelaxNGSchemaFacetCheck:
2275 * @data: data needed for the library
2276 * @type: the type name
2277 * @facet: the facet name
2278 * @val: the facet value
2279 * @strval: the string value
2280 * @value: the value to check
2281 *
2282 * Function provided by a type library to check a value facet
2283 *
2284 * Returns 1 if yes, 0 if no and -1 in case of error.
2285 */
2286static int
Daniel Veillard42f12e92003-03-07 18:32:59 +00002287xmlRelaxNGSchemaFacetCheck (void *data ATTRIBUTE_UNUSED, const xmlChar *type,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002288 const xmlChar *facetname, const xmlChar *val,
2289 const xmlChar *strval, void *value) {
2290 xmlSchemaFacetPtr facet;
2291 xmlSchemaTypePtr typ;
2292 int ret;
2293
2294 if ((type == NULL) || (strval == NULL))
2295 return(-1);
2296 typ = xmlSchemaGetPredefinedType(type,
2297 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2298 if (typ == NULL)
2299 return(-1);
2300
2301 facet = xmlSchemaNewFacet();
2302 if (facet == NULL)
2303 return(-1);
2304
2305 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2306 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2307 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2308 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2309 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2310 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2311 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2312 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2313 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2314 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2315 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2316 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2317 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2318 facet->type = XML_SCHEMA_FACET_PATTERN;
2319 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2320 facet->type = XML_SCHEMA_FACET_ENUMERATION;
2321 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2322 facet->type = XML_SCHEMA_FACET_WHITESPACE;
2323 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2324 facet->type = XML_SCHEMA_FACET_LENGTH;
2325 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2326 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2327 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2328 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2329 } else {
2330 xmlSchemaFreeFacet(facet);
2331 return(-1);
2332 }
2333 facet->value = xmlStrdup(val);
2334 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2335 if (ret != 0) {
2336 xmlSchemaFreeFacet(facet);
2337 return(-1);
2338 }
2339 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2340 xmlSchemaFreeFacet(facet);
2341 if (ret != 0)
2342 return(-1);
2343 return(0);
2344}
2345
2346/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002347 * xmlRelaxNGSchemaFreeValue:
2348 * @data: data needed for the library
2349 * @value: the value to free
2350 *
2351 * Function provided by a type library to free a Schemas value
2352 *
2353 * Returns 1 if yes, 0 if no and -1 in case of error.
2354 */
2355static void
2356xmlRelaxNGSchemaFreeValue (void *data ATTRIBUTE_UNUSED, void *value) {
2357 xmlSchemaFreeValue(value);
2358}
2359
2360/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002361 * xmlRelaxNGSchemaTypeCompare:
2362 * @data: data needed for the library
2363 * @type: the type name
2364 * @value1: the first value
2365 * @value2: the second value
2366 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002367 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002368 * Datatype library.
2369 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002370 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002371 */
2372static int
2373xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002374 const xmlChar *type,
2375 const xmlChar *value1,
2376 xmlNodePtr ctxt1,
2377 void *comp1,
2378 const xmlChar *value2,
2379 xmlNodePtr ctxt2) {
Daniel Veillard80b19092003-03-28 13:29:53 +00002380 int ret;
2381 xmlSchemaTypePtr typ;
2382 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2383
2384 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2385 return(-1);
2386 typ = xmlSchemaGetPredefinedType(type,
2387 BAD_CAST "http://www.w3.org/2001/XMLSchema");
2388 if (typ == NULL)
2389 return(-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002390 if (comp1 == NULL) {
2391 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2392 if (ret != 0)
2393 return(-1);
2394 if (res1 == NULL)
2395 return(-1);
2396 } else {
2397 res1 = (xmlSchemaValPtr) comp1;
2398 }
2399 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002400 if (ret != 0) {
2401 xmlSchemaFreeValue(res1);
2402 return(-1);
2403 }
2404 if (res1 == NULL) {
2405 xmlSchemaFreeValue(res1);
2406 return(-1);
2407 }
2408 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002409 if (res1 != (xmlSchemaValPtr) comp1)
2410 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002411 xmlSchemaFreeValue(res2);
2412 if (ret == -2)
2413 return(-1);
2414 if (ret == 0)
2415 return(1);
2416 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002417}
2418
2419/**
2420 * xmlRelaxNGDefaultTypeHave:
2421 * @data: data needed for the library
2422 * @type: the type name
2423 *
2424 * Check if the given type is provided by
2425 * the default datatype library.
2426 *
2427 * Returns 1 if yes, 0 if no and -1 in case of error.
2428 */
2429static int
2430xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar *type) {
2431 if (type == NULL)
2432 return(-1);
2433 if (xmlStrEqual(type, BAD_CAST "string"))
2434 return(1);
2435 if (xmlStrEqual(type, BAD_CAST "token"))
2436 return(1);
2437 return(0);
2438}
2439
2440/**
2441 * xmlRelaxNGDefaultTypeCheck:
2442 * @data: data needed for the library
2443 * @type: the type name
2444 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002445 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002446 *
2447 * Check if the given type and value are validated by
2448 * the default datatype library.
2449 *
2450 * Returns 1 if yes, 0 if no and -1 in case of error.
2451 */
2452static int
2453xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2454 const xmlChar *type ATTRIBUTE_UNUSED,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002455 const xmlChar *value ATTRIBUTE_UNUSED,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002456 void **result ATTRIBUTE_UNUSED,
2457 xmlNodePtr node ATTRIBUTE_UNUSED) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002458 if (value == NULL)
2459 return(-1);
2460 if (xmlStrEqual(type, BAD_CAST "string"))
2461 return(1);
2462 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002463 return(1);
2464 }
2465
2466 return(0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002467}
2468
2469/**
2470 * xmlRelaxNGDefaultTypeCompare:
2471 * @data: data needed for the library
2472 * @type: the type name
2473 * @value1: the first value
2474 * @value2: the second value
2475 *
2476 * Compare two values accordingly a type from the default
2477 * datatype library.
2478 *
2479 * Returns 1 if yes, 0 if no and -1 in case of error.
2480 */
2481static int
2482xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002483 const xmlChar *type,
2484 const xmlChar *value1,
2485 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2486 void *comp1 ATTRIBUTE_UNUSED,
2487 const xmlChar *value2,
2488 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00002489 int ret = -1;
2490
2491 if (xmlStrEqual(type, BAD_CAST "string")) {
2492 ret = xmlStrEqual(value1, value2);
2493 } else if (xmlStrEqual(type, BAD_CAST "token")) {
2494 if (!xmlStrEqual(value1, value2)) {
2495 xmlChar *nval, *nvalue;
2496
2497 /*
2498 * TODO: trivial optimizations are possible by
2499 * computing at compile-time
2500 */
2501 nval = xmlRelaxNGNormalize(NULL, value1);
2502 nvalue = xmlRelaxNGNormalize(NULL, value2);
2503
Daniel Veillardd4310742003-02-18 21:12:46 +00002504 if ((nval == NULL) || (nvalue == NULL))
Daniel Veillardea3f3982003-01-26 19:45:18 +00002505 ret = -1;
Daniel Veillardd4310742003-02-18 21:12:46 +00002506 else if (xmlStrEqual(nval, nvalue))
2507 ret = 1;
2508 else
2509 ret = 0;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002510 if (nval != NULL)
2511 xmlFree(nval);
2512 if (nvalue != NULL)
2513 xmlFree(nvalue);
Daniel Veillardd4310742003-02-18 21:12:46 +00002514 } else
2515 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002516 }
2517 return(ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002518}
2519
2520static int xmlRelaxNGTypeInitialized = 0;
2521static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2522
2523/**
2524 * xmlRelaxNGFreeTypeLibrary:
2525 * @lib: the type library structure
2526 * @namespace: the URI bound to the library
2527 *
2528 * Free the structure associated to the type library
2529 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002530static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002531xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2532 const xmlChar *namespace ATTRIBUTE_UNUSED) {
2533 if (lib == NULL)
2534 return;
2535 if (lib->namespace != NULL)
2536 xmlFree((xmlChar *)lib->namespace);
2537 xmlFree(lib);
2538}
2539
2540/**
2541 * xmlRelaxNGRegisterTypeLibrary:
2542 * @namespace: the URI bound to the library
2543 * @data: data associated to the library
2544 * @have: the provide function
2545 * @check: the checking function
2546 * @comp: the comparison function
2547 *
2548 * Register a new type library
2549 *
2550 * Returns 0 in case of success and -1 in case of error.
2551 */
2552static int
2553xmlRelaxNGRegisterTypeLibrary(const xmlChar *namespace, void *data,
2554 xmlRelaxNGTypeHave have, xmlRelaxNGTypeCheck check,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002555 xmlRelaxNGTypeCompare comp, xmlRelaxNGFacetCheck facet,
2556 xmlRelaxNGTypeFree freef) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002557 xmlRelaxNGTypeLibraryPtr lib;
2558 int ret;
2559
2560 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2561 (check == NULL) || (comp == NULL))
2562 return(-1);
2563 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2564 xmlGenericError(xmlGenericErrorContext,
2565 "Relax-NG types library '%s' already registered\n",
2566 namespace);
2567 return(-1);
2568 }
2569 lib = (xmlRelaxNGTypeLibraryPtr) xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2570 if (lib == NULL) {
2571 xmlGenericError(xmlGenericErrorContext,
2572 "Relax-NG types library '%s' malloc() failed\n",
2573 namespace);
2574 return (-1);
2575 }
2576 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2577 lib->namespace = xmlStrdup(namespace);
2578 lib->data = data;
2579 lib->have = have;
2580 lib->comp = comp;
2581 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002582 lib->facet = facet;
2583 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002584 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2585 if (ret < 0) {
2586 xmlGenericError(xmlGenericErrorContext,
2587 "Relax-NG types library failed to register '%s'\n",
2588 namespace);
2589 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2590 return(-1);
2591 }
2592 return(0);
2593}
2594
2595/**
2596 * xmlRelaxNGInitTypes:
2597 *
2598 * Initilize the default type libraries.
2599 *
2600 * Returns 0 in case of success and -1 in case of error.
2601 */
2602static int
Daniel Veillard6eadf632003-01-23 18:29:16 +00002603xmlRelaxNGInitTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002604 if (xmlRelaxNGTypeInitialized != 0)
2605 return(0);
2606 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2607 if (xmlRelaxNGRegisteredTypes == NULL) {
2608 xmlGenericError(xmlGenericErrorContext,
2609 "Failed to allocate sh table for Relax-NG types\n");
2610 return(-1);
2611 }
2612 xmlRelaxNGRegisterTypeLibrary(
2613 BAD_CAST "http://www.w3.org/2001/XMLSchema-datatypes",
2614 NULL,
2615 xmlRelaxNGSchemaTypeHave,
2616 xmlRelaxNGSchemaTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002617 xmlRelaxNGSchemaTypeCompare,
2618 xmlRelaxNGSchemaFacetCheck,
Daniel Veillard80b19092003-03-28 13:29:53 +00002619 xmlRelaxNGSchemaFreeValue);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002620 xmlRelaxNGRegisterTypeLibrary(
2621 xmlRelaxNGNs,
2622 NULL,
2623 xmlRelaxNGDefaultTypeHave,
2624 xmlRelaxNGDefaultTypeCheck,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002625 xmlRelaxNGDefaultTypeCompare,
2626 NULL,
2627 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002628 xmlRelaxNGTypeInitialized = 1;
2629 return(0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002630}
2631
2632/**
2633 * xmlRelaxNGCleanupTypes:
2634 *
2635 * Cleanup the default Schemas type library associated to RelaxNG
2636 */
2637void
2638xmlRelaxNGCleanupTypes(void) {
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002639 if (xmlRelaxNGTypeInitialized == 0)
2640 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002641 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002642 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2643 xmlRelaxNGFreeTypeLibrary);
2644 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002645}
2646
2647/************************************************************************
2648 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002649 * Compiling element content into regexp *
2650 * *
2651 * Sometime the element content can be compiled into a pure regexp, *
2652 * This allows a faster execution and streamability at that level *
2653 * *
2654 ************************************************************************/
2655
2656/**
2657 * xmlRelaxNGIsCompileable:
2658 * @define: the definition to check
2659 *
2660 * Check if a definition is nullable.
2661 *
2662 * Returns 1 if yes, 0 if no and -1 in case of error
2663 */
2664static int
2665xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def) {
2666 if (def == NULL) {
2667 return(-1);
2668 }
2669 switch(def->type) {
2670 case XML_RELAXNG_REF:
2671 case XML_RELAXNG_EXTERNALREF:
2672 case XML_RELAXNG_PARENTREF:
2673 case XML_RELAXNG_NOOP:
2674 case XML_RELAXNG_START:
2675 return(xmlRelaxNGIsCompileable(def->content));
2676 case XML_RELAXNG_TEXT:
2677 case XML_RELAXNG_DATATYPE:
2678 case XML_RELAXNG_LIST:
2679 case XML_RELAXNG_PARAM:
2680 case XML_RELAXNG_VALUE:
2681
2682 case XML_RELAXNG_EMPTY:
2683 case XML_RELAXNG_ELEMENT:
2684 return(1);
2685 case XML_RELAXNG_OPTIONAL:
2686 case XML_RELAXNG_ZEROORMORE:
2687 case XML_RELAXNG_ONEORMORE:
2688 case XML_RELAXNG_CHOICE:
2689 case XML_RELAXNG_GROUP:
2690 case XML_RELAXNG_DEF: {
2691 xmlRelaxNGDefinePtr list;
2692 int ret;
2693
2694 list = def->content;
2695 while (list != NULL) {
2696 ret = xmlRelaxNGIsCompileable(list);
2697 if (ret != 1)
2698 return(ret);
2699 list = list->next;
2700 }
2701 return(1);
2702 }
2703 case XML_RELAXNG_EXCEPT:
2704 case XML_RELAXNG_ATTRIBUTE:
2705 case XML_RELAXNG_INTERLEAVE:
2706 return(0);
2707 case XML_RELAXNG_NOT_ALLOWED:
2708 return(-1);
2709 }
2710 return(-1);
2711}
2712
2713/************************************************************************
2714 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002715 * Parsing functions *
2716 * *
2717 ************************************************************************/
2718
2719static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(
2720 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
2721static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(
2722 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
2723static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(
Daniel Veillard154877e2003-01-30 12:17:05 +00002724 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes, int group);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00002725static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(
2726 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002727static xmlRelaxNGPtr xmlRelaxNGParseDocument(
2728 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00002729static int xmlRelaxNGParseGrammarContent(
2730 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00002731static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(
2732 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
2733 xmlRelaxNGDefinePtr def);
Daniel Veillard419a7682003-02-03 23:22:49 +00002734static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(
2735 xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes);
Daniel Veillardfd573f12003-03-16 17:52:32 +00002736static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
2737 xmlRelaxNGDefinePtr define, xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002738
2739
Daniel Veillard249d7bb2003-03-19 21:02:29 +00002740#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00002741
2742/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00002743 * xmlRelaxNGIsNullable:
2744 * @define: the definition to verify
2745 *
2746 * Check if a definition is nullable.
2747 *
2748 * Returns 1 if yes, 0 if no and -1 in case of error
2749 */
2750static int
2751xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define) {
2752 int ret;
2753 if (define == NULL)
2754 return(-1);
2755
Daniel Veillarde063f482003-03-21 16:53:17 +00002756 if (define->dflags & IS_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00002757 return(1);
Daniel Veillarde063f482003-03-21 16:53:17 +00002758 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillardfd573f12003-03-16 17:52:32 +00002759 return(0);
2760 switch (define->type) {
2761 case XML_RELAXNG_EMPTY:
2762 case XML_RELAXNG_TEXT:
2763 ret = 1; break;
2764 case XML_RELAXNG_NOOP:
2765 case XML_RELAXNG_DEF:
2766 case XML_RELAXNG_REF:
2767 case XML_RELAXNG_EXTERNALREF:
2768 case XML_RELAXNG_PARENTREF:
2769 case XML_RELAXNG_ONEORMORE:
2770 ret = xmlRelaxNGIsNullable(define->content);
2771 break;
2772 case XML_RELAXNG_EXCEPT:
2773 case XML_RELAXNG_NOT_ALLOWED:
2774 case XML_RELAXNG_ELEMENT:
2775 case XML_RELAXNG_DATATYPE:
2776 case XML_RELAXNG_PARAM:
2777 case XML_RELAXNG_VALUE:
2778 case XML_RELAXNG_LIST:
2779 case XML_RELAXNG_ATTRIBUTE:
2780 ret = 0; break;
2781 case XML_RELAXNG_CHOICE: {
2782 xmlRelaxNGDefinePtr list = define->content;
2783
2784 while (list != NULL) {
2785 ret = xmlRelaxNGIsNullable(list);
2786 if (ret != 0)
2787 goto done;
2788 list = list->next;
2789 }
2790 ret = 0; break;
2791 }
2792 case XML_RELAXNG_START:
2793 case XML_RELAXNG_INTERLEAVE:
2794 case XML_RELAXNG_GROUP: {
2795 xmlRelaxNGDefinePtr list = define->content;
2796
2797 while (list != NULL) {
2798 ret = xmlRelaxNGIsNullable(list);
2799 if (ret != 1)
2800 goto done;
2801 list = list->next;
2802 }
2803 return(1);
2804 }
2805 default:
2806 return(-1);
2807 }
2808done:
2809 if (ret == 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00002810 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00002811 if (ret == 1)
Daniel Veillarde063f482003-03-21 16:53:17 +00002812 define->dflags |= IS_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00002813 return(ret);
2814}
2815
2816/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00002817 * xmlRelaxNGIsBlank:
2818 * @str: a string
2819 *
2820 * Check if a string is ignorable c.f. 4.2. Whitespace
2821 *
2822 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
2823 */
2824static int
2825xmlRelaxNGIsBlank(xmlChar *str) {
2826 if (str == NULL)
2827 return(1);
2828 while (*str != 0) {
2829 if (!(IS_BLANK(*str))) return(0);
2830 str++;
2831 }
2832 return(1);
2833}
2834
Daniel Veillard6eadf632003-01-23 18:29:16 +00002835/**
2836 * xmlRelaxNGGetDataTypeLibrary:
2837 * @ctxt: a Relax-NG parser context
2838 * @node: the current data or value element
2839 *
2840 * Applies algorithm from 4.3. datatypeLibrary attribute
2841 *
2842 * Returns the datatypeLibary value or NULL if not found
2843 */
2844static xmlChar *
2845xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
2846 xmlNodePtr node) {
2847 xmlChar *ret, *escape;
2848
Daniel Veillard6eadf632003-01-23 18:29:16 +00002849 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
2850 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
2851 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00002852 if (ret[0] == 0) {
2853 xmlFree(ret);
2854 return(NULL);
2855 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002856 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
Daniel Veillard6eadf632003-01-23 18:29:16 +00002857 if (escape == NULL) {
2858 return(ret);
2859 }
2860 xmlFree(ret);
2861 return(escape);
2862 }
2863 }
2864 node = node->parent;
2865 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillarde5b110b2003-02-04 14:43:39 +00002866 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
2867 if (ret != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00002868 if (ret[0] == 0) {
2869 xmlFree(ret);
2870 return(NULL);
2871 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00002872 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
2873 if (escape == NULL) {
2874 return(ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002875 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00002876 xmlFree(ret);
2877 return(escape);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002878 }
2879 node = node->parent;
2880 }
2881 return(NULL);
2882}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002883
2884/**
Daniel Veillardedc91922003-01-26 00:52:04 +00002885 * xmlRelaxNGParseValue:
2886 * @ctxt: a Relax-NG parser context
2887 * @node: the data node.
2888 *
2889 * parse the content of a RelaxNG value node.
2890 *
2891 * Returns the definition pointer or NULL in case of error
2892 */
2893static xmlRelaxNGDefinePtr
2894xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
2895 xmlRelaxNGDefinePtr def = NULL;
2896 xmlRelaxNGTypeLibraryPtr lib;
2897 xmlChar *type;
2898 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002899 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00002900
Daniel Veillardfd573f12003-03-16 17:52:32 +00002901 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00002902 if (def == NULL)
2903 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00002904 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00002905
2906 type = xmlGetProp(node, BAD_CAST "type");
2907 if (type != NULL) {
Daniel Veillardd2298792003-02-14 16:54:11 +00002908 xmlRelaxNGNormExtSpace(type);
2909 if (xmlValidateNCName(type, 0)) {
2910 if (ctxt->error != NULL)
2911 ctxt->error(ctxt->userData,
2912 "value type '%s' is not an NCName\n",
2913 type);
2914 ctxt->nbErrors++;
2915 }
Daniel Veillardedc91922003-01-26 00:52:04 +00002916 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
2917 if (library == NULL)
2918 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
2919
2920 def->name = type;
2921 def->ns = library;
2922
2923 lib = (xmlRelaxNGTypeLibraryPtr)
2924 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
2925 if (lib == NULL) {
2926 if (ctxt->error != NULL)
2927 ctxt->error(ctxt->userData,
2928 "Use of unregistered type library '%s'\n",
2929 library);
2930 ctxt->nbErrors++;
2931 def->data = NULL;
2932 } else {
2933 def->data = lib;
2934 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00002935 if (ctxt->error != NULL)
2936 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00002937 "Internal error with type library '%s': no 'have'\n",
2938 library);
2939 ctxt->nbErrors++;
2940 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002941 success = lib->have(lib->data, def->name);
2942 if (success != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00002943 if (ctxt->error != NULL)
2944 ctxt->error(ctxt->userData,
Daniel Veillardedc91922003-01-26 00:52:04 +00002945 "Error type '%s' is not exported by type library '%s'\n",
2946 def->name, library);
2947 ctxt->nbErrors++;
2948 }
2949 }
2950 }
2951 }
2952 if (node->children == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00002953 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00002954 } else if (((node->children->type != XML_TEXT_NODE) &&
2955 (node->children->type != XML_CDATA_SECTION_NODE)) ||
Daniel Veillardedc91922003-01-26 00:52:04 +00002956 (node->children->next != NULL)) {
2957 if (ctxt->error != NULL)
2958 ctxt->error(ctxt->userData,
2959 "Expecting a single text value for <value>content\n");
2960 ctxt->nbErrors++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002961 } else if (def != NULL) {
Daniel Veillardedc91922003-01-26 00:52:04 +00002962 def->value = xmlNodeGetContent(node);
2963 if (def->value == NULL) {
2964 if (ctxt->error != NULL)
2965 ctxt->error(ctxt->userData,
2966 "Element <value> has no content\n");
2967 ctxt->nbErrors++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002968 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
2969 void *val = NULL;
2970
2971 success = lib->check(lib->data, def->name, def->value, &val, node);
2972 if (success != 1) {
2973 if (ctxt->error != NULL)
2974 ctxt->error(ctxt->userData,
2975 "Value '%s' is not acceptable for type '%s'\n",
2976 def->value, def->name);
2977 ctxt->nbErrors++;
2978 } else {
2979 if (val != NULL)
2980 def->attrs = val;
2981 }
Daniel Veillardedc91922003-01-26 00:52:04 +00002982 }
2983 }
2984 /* TODO check ahead of time that the value is okay per the type */
2985 return(def);
2986}
2987
2988/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002989 * xmlRelaxNGParseData:
2990 * @ctxt: a Relax-NG parser context
2991 * @node: the data node.
2992 *
2993 * parse the content of a RelaxNG data node.
2994 *
2995 * Returns the definition pointer or NULL in case of error
2996 */
2997static xmlRelaxNGDefinePtr
2998xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillard416589a2003-02-17 17:25:42 +00002999 xmlRelaxNGDefinePtr def = NULL, except, last = NULL;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003000 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003001 xmlRelaxNGTypeLibraryPtr lib;
3002 xmlChar *type;
3003 xmlChar *library;
3004 xmlNodePtr content;
3005 int tmp;
3006
3007 type = xmlGetProp(node, BAD_CAST "type");
3008 if (type == NULL) {
3009 if (ctxt->error != NULL)
3010 ctxt->error(ctxt->userData,
3011 "data has no type\n");
3012 ctxt->nbErrors++;
3013 return(NULL);
3014 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003015 xmlRelaxNGNormExtSpace(type);
3016 if (xmlValidateNCName(type, 0)) {
3017 if (ctxt->error != NULL)
3018 ctxt->error(ctxt->userData,
3019 "data type '%s' is not an NCName\n",
3020 type);
3021 ctxt->nbErrors++;
3022 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003023 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3024 if (library == NULL)
3025 library = xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3026
Daniel Veillardfd573f12003-03-16 17:52:32 +00003027 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003028 if (def == NULL) {
3029 xmlFree(type);
3030 return(NULL);
3031 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003032 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003033 def->name = type;
3034 def->ns = library;
3035
3036 lib = (xmlRelaxNGTypeLibraryPtr)
3037 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3038 if (lib == NULL) {
3039 if (ctxt->error != NULL)
3040 ctxt->error(ctxt->userData,
3041 "Use of unregistered type library '%s'\n",
3042 library);
3043 ctxt->nbErrors++;
3044 def->data = NULL;
3045 } else {
3046 def->data = lib;
3047 if (lib->have == NULL) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003048 if (ctxt->error != NULL)
3049 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003050 "Internal error with type library '%s': no 'have'\n",
3051 library);
3052 ctxt->nbErrors++;
3053 } else {
3054 tmp = lib->have(lib->data, def->name);
3055 if (tmp != 1) {
Daniel Veillard1703c5f2003-02-10 14:28:44 +00003056 if (ctxt->error != NULL)
3057 ctxt->error(ctxt->userData,
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003058 "Error type '%s' is not exported by type library '%s'\n",
3059 def->name, library);
3060 ctxt->nbErrors++;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00003061 } else if ((xmlStrEqual(library, BAD_CAST
3062 "http://www.w3.org/2001/XMLSchema-datatypes")) &&
3063 ((xmlStrEqual(def->name, BAD_CAST "IDREF")) ||
3064 (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3065 ctxt->idref = 1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003066 }
3067 }
3068 }
3069 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003070
3071 /*
3072 * Handle optional params
3073 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003074 while (content != NULL) {
Daniel Veillard416589a2003-02-17 17:25:42 +00003075 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3076 break;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003077 if (xmlStrEqual(library,
3078 BAD_CAST"http://relaxng.org/ns/structure/1.0")) {
3079 if (ctxt->error != NULL)
3080 ctxt->error(ctxt->userData,
3081 "Type library '%s' does not allow type parameters\n",
3082 library);
3083 ctxt->nbErrors++;
3084 content = content->next;
3085 while ((content != NULL) &&
3086 (xmlStrEqual(content->name, BAD_CAST "param")))
3087 content = content->next;
3088 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003089 param = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003090 if (param != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003091 param->type = XML_RELAXNG_PARAM;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003092 param->name = xmlGetProp(content, BAD_CAST "name");
3093 if (param->name == NULL) {
3094 if (ctxt->error != NULL)
3095 ctxt->error(ctxt->userData,
3096 "param has no name\n");
3097 ctxt->nbErrors++;
3098 }
3099 param->value = xmlNodeGetContent(content);
3100 if (lastparam == NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003101 def->attrs = lastparam = param;
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003102 } else {
3103 lastparam->next = param;
3104 lastparam = param;
3105 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003106 if (lib != NULL) {
3107 }
Daniel Veillard8fe98712003-02-19 00:19:14 +00003108 }
Daniel Veillard4c5cf702003-02-21 15:40:34 +00003109 content = content->next;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003110 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003111 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003112 /*
3113 * Handle optional except
3114 */
3115 if ((content != NULL) && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3116 xmlNodePtr child;
3117 xmlRelaxNGDefinePtr tmp2, last2 = NULL;
3118
Daniel Veillardfd573f12003-03-16 17:52:32 +00003119 except = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard416589a2003-02-17 17:25:42 +00003120 if (except == NULL) {
3121 return(def);
3122 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003123 except->type = XML_RELAXNG_EXCEPT;
Daniel Veillard416589a2003-02-17 17:25:42 +00003124 child = content->children;
3125 if (last == NULL) {
3126 def->content = except;
3127 } else {
3128 last->next = except;
3129 }
3130 if (child == NULL) {
3131 if (ctxt->error != NULL)
3132 ctxt->error(ctxt->userData,
3133 "except has no content\n");
3134 ctxt->nbErrors++;
3135 }
3136 while (child != NULL) {
3137 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3138 if (tmp2 != NULL) {
3139 if (last2 == NULL) {
3140 except->content = last2 = tmp2;
3141 } else {
3142 last2->next = tmp2;
3143 last2 = tmp2;
3144 }
3145 }
3146 child = child->next;
3147 }
3148 content = content->next;
3149 }
3150 /*
3151 * Check there is no unhandled data
3152 */
3153 if (content != NULL) {
3154 if (ctxt->error != NULL)
3155 ctxt->error(ctxt->userData,
3156 "Element data has unexpected content %s\n", content->name);
3157 ctxt->nbErrors++;
3158 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003159
3160 return(def);
3161}
3162
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003163static const xmlChar *invalidName = BAD_CAST "\1";
3164
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003165/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003166 * xmlRelaxNGCompareNameClasses:
3167 * @defs1: the first element/attribute defs
3168 * @defs2: the second element/attribute defs
3169 * @name: the restriction on the name
3170 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003171 *
3172 * Compare the 2 lists of element definitions. The comparison is
3173 * that if both lists do not accept the same QNames, it returns 1
3174 * If the 2 lists can accept the same QName the comparison returns 0
3175 *
3176 * Returns 1 disttinct, 0 if equal
3177 */
3178static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003179xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3180 xmlRelaxNGDefinePtr def2) {
3181 int ret = 1;
3182 xmlNode node;
3183 xmlNs ns;
3184 xmlRelaxNGValidCtxt ctxt;
3185 ctxt.flags = FLAGS_IGNORABLE;
3186
Daniel Veillard42f12e92003-03-07 18:32:59 +00003187 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3188
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003189 if ((def1->type == XML_RELAXNG_ELEMENT) ||
3190 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3191 if (def2->type == XML_RELAXNG_TEXT)
3192 return(1);
3193 if (def1->name != NULL) {
3194 node.name = def1->name;
3195 } else {
3196 node.name = invalidName;
3197 }
3198 node.ns = &ns;
3199 if (def1->ns != NULL) {
3200 if (def1->ns[0] == 0) {
3201 node.ns = NULL;
3202 } else {
3203 ns.href = def1->ns;
3204 }
3205 } else {
3206 ns.href = invalidName;
3207 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003208 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003209 if (def1->nameClass != NULL) {
3210 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3211 } else {
3212 ret = 0;
3213 }
3214 } else {
3215 ret = 1;
3216 }
3217 } else if (def1->type == XML_RELAXNG_TEXT) {
3218 if (def2->type == XML_RELAXNG_TEXT)
3219 return(0);
3220 return(1);
3221 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003222 TODO
3223 ret = 0;
3224 } else {
3225 TODO
3226 ret = 0;
3227 }
3228 if (ret == 0)
3229 return(ret);
3230 if ((def2->type == XML_RELAXNG_ELEMENT) ||
3231 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3232 if (def2->name != NULL) {
3233 node.name = def2->name;
3234 } else {
3235 node.name = invalidName;
3236 }
3237 node.ns = &ns;
3238 if (def2->ns != NULL) {
3239 if (def2->ns[0] == 0) {
3240 node.ns = NULL;
3241 } else {
3242 ns.href = def2->ns;
3243 }
3244 } else {
3245 ns.href = invalidName;
3246 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003247 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003248 if (def2->nameClass != NULL) {
3249 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3250 } else {
3251 ret = 0;
3252 }
3253 } else {
3254 ret = 1;
3255 }
3256 } else {
3257 TODO
3258 ret = 0;
3259 }
3260
3261 return(ret);
3262}
3263
3264/**
3265 * xmlRelaxNGCompareElemDefLists:
3266 * @ctxt: a Relax-NG parser context
3267 * @defs1: the first list of element/attribute defs
3268 * @defs2: the second list of element/attribute defs
3269 *
3270 * Compare the 2 lists of element or attribute definitions. The comparison
3271 * is that if both lists do not accept the same QNames, it returns 1
3272 * If the 2 lists can accept the same QName the comparison returns 0
3273 *
3274 * Returns 1 disttinct, 0 if equal
3275 */
3276static int
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003277xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3278 xmlRelaxNGDefinePtr *def1,
3279 xmlRelaxNGDefinePtr *def2) {
3280 xmlRelaxNGDefinePtr *basedef2 = def2;
3281
Daniel Veillard154877e2003-01-30 12:17:05 +00003282 if ((def1 == NULL) || (def2 == NULL))
3283 return(1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003284 if ((*def1 == NULL) || (*def2 == NULL))
3285 return(1);
3286 while (*def1 != NULL) {
3287 while ((*def2) != NULL) {
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003288 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3289 return(0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003290 def2++;
3291 }
3292 def2 = basedef2;
3293 def1++;
3294 }
3295 return(1);
3296}
3297
3298/**
3299 * xmlRelaxNGGetElements:
3300 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003301 * @def: the definition definition
3302 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003303 *
3304 * Compute the list of top elements a definition can generate
3305 *
3306 * Returns a list of elements or NULL if none was found.
3307 */
3308static xmlRelaxNGDefinePtr *
3309xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003310 xmlRelaxNGDefinePtr def,
3311 int eora) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003312 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003313 int len = 0;
3314 int max = 0;
3315
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003316 /*
3317 * Don't run that check in case of error. Infinite recursion
3318 * becomes possible.
3319 */
3320 if (ctxt->nbErrors != 0)
3321 return(NULL);
3322
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003323 parent = NULL;
3324 cur = def;
3325 while (cur != NULL) {
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003326 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3327 (cur->type == XML_RELAXNG_TEXT))) ||
3328 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003329 if (ret == NULL) {
3330 max = 10;
3331 ret = (xmlRelaxNGDefinePtr *)
3332 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3333 if (ret == NULL) {
3334 if (ctxt->error != NULL)
3335 ctxt->error(ctxt->userData,
3336 "Out of memory in element search\n");
3337 ctxt->nbErrors++;
3338 return(NULL);
3339 }
3340 } else if (max <= len) {
3341 max *= 2;
3342 ret = xmlRealloc(ret, (max + 1) * sizeof(xmlRelaxNGDefinePtr));
3343 if (ret == NULL) {
3344 if (ctxt->error != NULL)
3345 ctxt->error(ctxt->userData,
3346 "Out of memory in element search\n");
3347 ctxt->nbErrors++;
3348 return(NULL);
3349 }
3350 }
Daniel Veillardb08c9812003-01-28 23:09:49 +00003351 ret[len++] = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003352 ret[len] = NULL;
3353 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
3354 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3355 (cur->type == XML_RELAXNG_GROUP) ||
3356 (cur->type == XML_RELAXNG_ONEORMORE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00003357 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3358 (cur->type == XML_RELAXNG_OPTIONAL) ||
Daniel Veillard952379b2003-03-17 15:37:12 +00003359 (cur->type == XML_RELAXNG_PARENTREF) ||
Daniel Veillardb08c9812003-01-28 23:09:49 +00003360 (cur->type == XML_RELAXNG_REF) ||
3361 (cur->type == XML_RELAXNG_DEF)) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003362 /*
3363 * Don't go within elements or attributes or string values.
3364 * Just gather the element top list
3365 */
3366 if (cur->content != NULL) {
3367 parent = cur;
3368 cur = cur->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003369 tmp = cur;
3370 while (tmp != NULL) {
3371 tmp->parent = parent;
3372 tmp = tmp->next;
3373 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003374 continue;
3375 }
3376 }
Daniel Veillard154877e2003-01-30 12:17:05 +00003377 if (cur == def)
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003378 break;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003379 if (cur->next != NULL) {
3380 cur = cur->next;
3381 continue;
3382 }
3383 do {
3384 cur = cur->parent;
3385 if (cur == NULL) break;
3386 if (cur == def) return(ret);
3387 if (cur->next != NULL) {
3388 cur = cur->next;
3389 break;
3390 }
3391 } while (cur != NULL);
3392 }
3393 return(ret);
3394}
3395
3396/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003397 * xmlRelaxNGCheckChoiceDeterminism:
3398 * @ctxt: a Relax-NG parser context
3399 * @def: the choice definition
3400 *
3401 * Also used to find indeterministic pattern in choice
3402 */
3403static void
3404xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
3405 xmlRelaxNGDefinePtr def) {
3406 xmlRelaxNGDefinePtr **list;
3407 xmlRelaxNGDefinePtr cur;
3408 int nbchild = 0, i, j, ret;
3409 int is_nullable = 0;
3410 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003411 xmlHashTablePtr triage = NULL;
3412 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003413
3414 if ((def == NULL) ||
3415 (def->type != XML_RELAXNG_CHOICE))
3416 return;
3417
Daniel Veillarde063f482003-03-21 16:53:17 +00003418 if (def->dflags & IS_PROCESSED)
3419 return;
3420
Daniel Veillardfd573f12003-03-16 17:52:32 +00003421 /*
3422 * Don't run that check in case of error. Infinite recursion
3423 * becomes possible.
3424 */
3425 if (ctxt->nbErrors != 0)
3426 return;
3427
3428 is_nullable = xmlRelaxNGIsNullable(def);
3429
3430 cur = def->content;
3431 while (cur != NULL) {
3432 nbchild++;
3433 cur = cur->next;
3434 }
3435
3436 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3437 sizeof(xmlRelaxNGDefinePtr *));
3438 if (list == NULL) {
3439 if (ctxt->error != NULL)
3440 ctxt->error(ctxt->userData,
3441 "Out of memory in choice computation\n");
3442 ctxt->nbErrors++;
3443 return;
3444 }
3445 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00003446 /*
3447 * a bit strong but safe
3448 */
3449 if (is_nullable == 0) {
3450 triage = xmlHashCreate(10);
3451 } else {
3452 is_triable = 0;
3453 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003454 cur = def->content;
3455 while (cur != NULL) {
3456 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
Daniel Veillarde063f482003-03-21 16:53:17 +00003457 if ((list[i] == NULL) || (list[i][0] == NULL)) {
3458 is_triable = 0;
3459 } else if (is_triable == 1) {
3460 xmlRelaxNGDefinePtr *tmp;
3461 int res;
3462
3463 tmp = list[i];
3464 while ((*tmp != NULL) && (is_triable == 1)) {
3465 if ((*tmp)->type == XML_RELAXNG_TEXT) {
3466 res = xmlHashAddEntry2(triage,
3467 BAD_CAST "#text", NULL,
3468 (void *)cur);
3469 if (res != 0)
3470 is_triable = -1;
3471 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
3472 ((*tmp)->name != NULL)) {
3473 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3474 res = xmlHashAddEntry2(triage,
3475 (*tmp)->name, NULL,
3476 (void *)cur);
3477 else
3478 res = xmlHashAddEntry2(triage,
3479 (*tmp)->name, (*tmp)->ns,
3480 (void *)cur);
3481 if (res != 0)
3482 is_triable = -1;
3483 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
3484 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3485 res = xmlHashAddEntry2(triage,
3486 BAD_CAST "#any", NULL,
3487 (void *)cur);
3488 else
3489 res = xmlHashAddEntry2(triage,
3490 BAD_CAST "#any", (*tmp)->ns,
3491 (void *)cur);
3492 if (res != 0)
3493 is_triable = -1;
3494 } else {
3495 is_triable = -1;
3496 }
3497 tmp++;
3498 }
3499 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003500 i++;
3501 cur = cur->next;
3502 }
3503
3504 for (i = 0;i < nbchild;i++) {
3505 if (list[i] == NULL)
3506 continue;
3507 for (j = 0;j < i;j++) {
3508 if (list[j] == NULL)
3509 continue;
3510 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3511 if (ret == 0) {
3512 is_indeterminist = 1;
3513 }
3514 }
3515 }
3516 for (i = 0;i < nbchild;i++) {
3517 if (list[i] != NULL)
3518 xmlFree(list[i]);
3519 }
3520
3521 xmlFree(list);
3522 if (is_indeterminist) {
Daniel Veillarde063f482003-03-21 16:53:17 +00003523 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003524 }
Daniel Veillarde063f482003-03-21 16:53:17 +00003525 if (is_triable == 1) {
3526 def->dflags |= IS_TRIABLE;
3527 def->data = triage;
3528 } else if (triage != NULL) {
3529 xmlHashFree(triage, NULL);
3530 }
3531 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003532}
3533
3534/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003535 * xmlRelaxNGCheckGroupAttrs:
3536 * @ctxt: a Relax-NG parser context
3537 * @def: the group definition
3538 *
3539 * Detects violations of rule 7.3
3540 */
3541static void
3542xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
3543 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00003544 xmlRelaxNGDefinePtr **list;
3545 xmlRelaxNGDefinePtr cur;
3546 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003547
3548 if ((def == NULL) ||
3549 ((def->type != XML_RELAXNG_GROUP) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00003550 (def->type != XML_RELAXNG_ELEMENT)))
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003551 return;
3552
Daniel Veillarde063f482003-03-21 16:53:17 +00003553 if (def->dflags & IS_PROCESSED)
3554 return;
3555
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003556 /*
3557 * Don't run that check in case of error. Infinite recursion
3558 * becomes possible.
3559 */
3560 if (ctxt->nbErrors != 0)
3561 return;
3562
Daniel Veillardfd573f12003-03-16 17:52:32 +00003563 cur = def->attrs;
3564 while (cur != NULL) {
3565 nbchild++;
3566 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003567 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003568 cur = def->content;
3569 while (cur != NULL) {
3570 nbchild++;
3571 cur = cur->next;
3572 }
3573
3574 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
3575 sizeof(xmlRelaxNGDefinePtr *));
3576 if (list == NULL) {
3577 if (ctxt->error != NULL)
3578 ctxt->error(ctxt->userData,
3579 "Out of memory in group computation\n");
3580 ctxt->nbErrors++;
3581 return;
3582 }
3583 i = 0;
3584 cur = def->attrs;
3585 while (cur != NULL) {
3586 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
3587 i++;
3588 cur = cur->next;
3589 }
3590 cur = def->content;
3591 while (cur != NULL) {
3592 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
3593 i++;
3594 cur = cur->next;
3595 }
3596
3597 for (i = 0;i < nbchild;i++) {
3598 if (list[i] == NULL)
3599 continue;
3600 for (j = 0;j < i;j++) {
3601 if (list[j] == NULL)
3602 continue;
3603 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
3604 if (ret == 0) {
3605 if (ctxt->error != NULL)
3606 ctxt->error(ctxt->userData,
3607 "Attributes conflicts in group\n");
3608 ctxt->nbErrors++;
3609 }
3610 }
3611 }
3612 for (i = 0;i < nbchild;i++) {
3613 if (list[i] != NULL)
3614 xmlFree(list[i]);
3615 }
3616
3617 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00003618 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003619}
3620
3621/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003622 * xmlRelaxNGComputeInterleaves:
3623 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003624 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00003625 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003626 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00003627 * A lot of work for preprocessing interleave definitions
3628 * is potentially needed to get a decent execution speed at runtime
3629 * - trying to get a total order on the element nodes generated
3630 * by the interleaves, order the list of interleave definitions
3631 * following that order.
3632 * - if <text/> is used to handle mixed content, it is better to
3633 * flag this in the define and simplify the runtime checking
3634 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003635 */
3636static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00003637xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
3638 xmlRelaxNGParserCtxtPtr ctxt,
3639 xmlChar *name ATTRIBUTE_UNUSED) {
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003640 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003641
Daniel Veillardfd573f12003-03-16 17:52:32 +00003642 xmlRelaxNGPartitionPtr partitions = NULL;
3643 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
3644 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003645 int i,j,ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003646 int nbgroups = 0;
3647 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003648 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003649 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003650
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003651 /*
3652 * Don't run that check in case of error. Infinite recursion
3653 * becomes possible.
3654 */
3655 if (ctxt->nbErrors != 0)
3656 return;
3657
Daniel Veillardfd573f12003-03-16 17:52:32 +00003658#ifdef DEBUG_INTERLEAVE
3659 xmlGenericError(xmlGenericErrorContext,
3660 "xmlRelaxNGComputeInterleaves(%s)\n",
3661 name);
3662#endif
3663 cur = def->content;
3664 while (cur != NULL) {
3665 nbchild++;
3666 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003667 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003668
3669#ifdef DEBUG_INTERLEAVE
3670 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
3671#endif
3672 groups = (xmlRelaxNGInterleaveGroupPtr *)
3673 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
3674 if (groups == NULL)
3675 goto error;
3676 cur = def->content;
3677 while (cur != NULL) {
3678 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
3679 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
3680 if (groups[nbgroups] == NULL)
3681 goto error;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003682 if (cur->type == XML_RELAXNG_TEXT)
3683 is_mixed++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003684 groups[nbgroups]->rule = cur;
3685 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
3686 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
3687 nbgroups++;
3688 cur = cur->next;
3689 }
3690#ifdef DEBUG_INTERLEAVE
3691 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
3692#endif
3693
3694 /*
3695 * Let's check that all rules makes a partitions according to 7.4
3696 */
3697 partitions = (xmlRelaxNGPartitionPtr)
3698 xmlMalloc(sizeof(xmlRelaxNGPartition));
3699 if (partitions == NULL)
3700 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00003701 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00003702 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003703 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003704 for (i = 0;i < nbgroups;i++) {
3705 group = groups[i];
3706 for (j = i+1;j < nbgroups;j++) {
3707 if (groups[j] == NULL)
3708 continue;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003709
Daniel Veillardfd573f12003-03-16 17:52:32 +00003710 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
3711 groups[j]->defs);
3712 if (ret == 0) {
3713 if (ctxt->error != NULL)
3714 ctxt->error(ctxt->userData,
3715 "Element or text conflicts in interleave\n");
3716 ctxt->nbErrors++;
3717 }
3718 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
3719 groups[j]->attrs);
3720 if (ret == 0) {
3721 if (ctxt->error != NULL)
3722 ctxt->error(ctxt->userData,
3723 "Attributes conflicts in interleave\n");
3724 ctxt->nbErrors++;
3725 }
3726 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003727 tmp = group->defs;
3728 if ((tmp != NULL) && (*tmp != NULL)) {
3729 while (*tmp != NULL) {
3730 if ((*tmp)->type == XML_RELAXNG_TEXT) {
3731 res = xmlHashAddEntry2(partitions->triage,
3732 BAD_CAST "#text", NULL,
3733 (void *)(i + 1));
3734 if (res != 0)
3735 is_determinist = -1;
3736 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
3737 ((*tmp)->name != NULL)) {
3738 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3739 res = xmlHashAddEntry2(partitions->triage,
3740 (*tmp)->name, NULL,
3741 (void *)(i + 1));
3742 else
3743 res = xmlHashAddEntry2(partitions->triage,
3744 (*tmp)->name, (*tmp)->ns,
3745 (void *)(i + 1));
3746 if (res != 0)
3747 is_determinist = -1;
3748 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
3749 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
3750 res = xmlHashAddEntry2(partitions->triage,
3751 BAD_CAST "#any", NULL,
3752 (void *)(i + 1));
3753 else
3754 res = xmlHashAddEntry2(partitions->triage,
3755 BAD_CAST "#any", (*tmp)->ns,
3756 (void *)(i + 1));
3757 if ((*tmp)->nameClass != NULL)
3758 is_determinist = 2;
3759 if (res != 0)
3760 is_determinist = -1;
3761 } else {
3762 is_determinist = -1;
3763 }
3764 tmp++;
3765 }
3766 } else {
3767 is_determinist = 0;
3768 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003769 }
3770 partitions->groups = groups;
3771
3772 /*
3773 * and save the partition list back in the def
3774 */
3775 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003776 if (is_mixed != 0)
Daniel Veillarde063f482003-03-21 16:53:17 +00003777 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00003778 if (is_determinist == 1)
3779 partitions->flags = IS_DETERMINIST;
3780 if (is_determinist == 2)
3781 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003782 return;
3783
3784error:
3785 if (ctxt->error != NULL)
3786 ctxt->error(ctxt->userData,
3787 "Out of memory in interleave computation\n");
3788 ctxt->nbErrors++;
3789 if (groups != NULL) {
3790 for (i = 0;i < nbgroups;i++)
3791 if (groups[i] != NULL) {
3792 if (groups[i]->defs != NULL)
3793 xmlFree(groups[i]->defs);
3794 xmlFree(groups[i]);
3795 }
3796 xmlFree(groups);
3797 }
3798 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003799}
3800
3801/**
3802 * xmlRelaxNGParseInterleave:
3803 * @ctxt: a Relax-NG parser context
3804 * @node: the data node.
3805 *
3806 * parse the content of a RelaxNG interleave node.
3807 *
3808 * Returns the definition pointer or NULL in case of error
3809 */
3810static xmlRelaxNGDefinePtr
3811xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3812 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003813 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003814 xmlNodePtr child;
3815
Daniel Veillardfd573f12003-03-16 17:52:32 +00003816 def = xmlRelaxNGNewDefine(ctxt, node);
3817 if (def == NULL) {
3818 return(NULL);
3819 }
3820 def->type = XML_RELAXNG_INTERLEAVE;
3821
3822 if (ctxt->interleaves == NULL)
3823 ctxt->interleaves = xmlHashCreate(10);
3824 if (ctxt->interleaves == NULL) {
3825 if (ctxt->error != NULL)
3826 ctxt->error(ctxt->userData,
3827 "Failed to create interleaves hash table\n");
3828 ctxt->nbErrors++;
3829 } else {
3830 char name[32];
3831
3832 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
3833 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
3834 if (ctxt->error != NULL)
3835 ctxt->error(ctxt->userData,
3836 "Failed to add %s to hash table\n", name);
3837 ctxt->nbErrors++;
3838 }
3839 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003840 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00003841 if (child == NULL) {
3842 if (ctxt->error != NULL)
3843 ctxt->error(ctxt->userData, "Element interleave is empty\n");
3844 ctxt->nbErrors++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00003845 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003846 while (child != NULL) {
3847 if (IS_RELAXNG(child, "element")) {
3848 cur = xmlRelaxNGParseElement(ctxt, child);
3849 } else {
3850 cur = xmlRelaxNGParsePattern(ctxt, child);
3851 }
3852 if (cur != NULL) {
3853 cur->parent = def;
3854 if (last == NULL) {
3855 def->content = last = cur;
3856 } else {
3857 last->next = cur;
3858 last = cur;
3859 }
3860 }
3861 child = child->next;
3862 }
3863
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003864 return(def);
3865}
Daniel Veillard6eadf632003-01-23 18:29:16 +00003866
3867/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00003868 * xmlRelaxNGParseInclude:
3869 * @ctxt: a Relax-NG parser context
3870 * @node: the include node
3871 *
3872 * Integrate the content of an include node in the current grammar
3873 *
3874 * Returns 0 in case of success or -1 in case of error
3875 */
3876static int
3877xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3878 xmlRelaxNGIncludePtr incl;
3879 xmlNodePtr root;
3880 int ret = 0, tmp;
3881
3882 incl = node->_private;
3883 if (incl == NULL) {
3884 if (ctxt->error != NULL)
3885 ctxt->error(ctxt->userData,
3886 "Include node has no data\n");
3887 ctxt->nbErrors++;
3888 return(-1);
3889 }
3890 root = xmlDocGetRootElement(incl->doc);
3891 if (root == NULL) {
3892 if (ctxt->error != NULL)
3893 ctxt->error(ctxt->userData,
3894 "Include document is empty\n");
3895 ctxt->nbErrors++;
3896 return(-1);
3897 }
3898 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
3899 if (ctxt->error != NULL)
3900 ctxt->error(ctxt->userData,
3901 "Include document root is not a grammar\n");
3902 ctxt->nbErrors++;
3903 return(-1);
3904 }
3905
3906 /*
3907 * Merge the definition from both the include and the internal list
3908 */
3909 if (root->children != NULL) {
3910 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
3911 if (tmp != 0)
3912 ret = -1;
3913 }
3914 if (node->children != NULL) {
3915 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
3916 if (tmp != 0)
3917 ret = -1;
3918 }
3919 return(ret);
3920}
3921
3922/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00003923 * xmlRelaxNGParseDefine:
3924 * @ctxt: a Relax-NG parser context
3925 * @node: the define node
3926 *
3927 * parse the content of a RelaxNG define element node.
3928 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00003929 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00003930 */
3931static int
3932xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
3933 xmlChar *name;
3934 int ret = 0, tmp;
3935 xmlRelaxNGDefinePtr def;
3936 const xmlChar *olddefine;
3937
3938 name = xmlGetProp(node, BAD_CAST "name");
3939 if (name == NULL) {
3940 if (ctxt->error != NULL)
3941 ctxt->error(ctxt->userData,
3942 "define has no name\n");
3943 ctxt->nbErrors++;
3944 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00003945 xmlRelaxNGNormExtSpace(name);
3946 if (xmlValidateNCName(name, 0)) {
3947 if (ctxt->error != NULL)
3948 ctxt->error(ctxt->userData,
3949 "define name '%s' is not an NCName\n",
3950 name);
3951 ctxt->nbErrors++;
3952 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003953 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard276be4a2003-01-24 01:03:34 +00003954 if (def == NULL) {
3955 xmlFree(name);
3956 return(-1);
3957 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003958 def->type = XML_RELAXNG_DEF;
Daniel Veillard276be4a2003-01-24 01:03:34 +00003959 def->name = name;
3960 if (node->children == NULL) {
3961 if (ctxt->error != NULL)
3962 ctxt->error(ctxt->userData,
3963 "define has no children\n");
3964 ctxt->nbErrors++;
3965 } else {
3966 olddefine = ctxt->define;
3967 ctxt->define = name;
Daniel Veillard154877e2003-01-30 12:17:05 +00003968 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
Daniel Veillard276be4a2003-01-24 01:03:34 +00003969 ctxt->define = olddefine;
3970 }
3971 if (ctxt->grammar->defs == NULL)
3972 ctxt->grammar->defs = xmlHashCreate(10);
3973 if (ctxt->grammar->defs == NULL) {
3974 if (ctxt->error != NULL)
3975 ctxt->error(ctxt->userData,
3976 "Could not create definition hash\n");
3977 ctxt->nbErrors++;
3978 ret = -1;
Daniel Veillard276be4a2003-01-24 01:03:34 +00003979 } else {
3980 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
3981 if (tmp < 0) {
Daniel Veillard154877e2003-01-30 12:17:05 +00003982 xmlRelaxNGDefinePtr prev;
3983
3984 prev = xmlHashLookup(ctxt->grammar->defs, name);
3985 if (prev == NULL) {
3986 if (ctxt->error != NULL)
3987 ctxt->error(ctxt->userData,
3988 "Internal error on define aggregation of %s\n",
3989 name);
3990 ctxt->nbErrors++;
3991 ret = -1;
Daniel Veillard154877e2003-01-30 12:17:05 +00003992 } else {
3993 while (prev->nextHash != NULL)
3994 prev = prev->nextHash;
3995 prev->nextHash = def;
3996 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00003997 }
3998 }
3999 }
4000 return(ret);
4001}
4002
4003/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004004 * xmlRelaxNGProcessExternalRef:
4005 * @ctxt: the parser context
4006 * @node: the externlRef node
4007 *
4008 * Process and compile an externlRef node
4009 *
4010 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4011 */
4012static xmlRelaxNGDefinePtr
4013xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4014 xmlRelaxNGDocumentPtr docu;
4015 xmlNodePtr root, tmp;
4016 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004017 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004018 xmlRelaxNGDefinePtr def;
4019
4020 docu = node->_private;
4021 if (docu != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004022 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004023 if (def == NULL)
4024 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004025 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004026
4027 if (docu->content == NULL) {
4028 /*
4029 * Then do the parsing for good
4030 */
4031 root = xmlDocGetRootElement(docu->doc);
4032 if (root == NULL) {
4033 if (ctxt->error != NULL)
4034 ctxt->error(ctxt->userData,
4035 "xmlRelaxNGParse: %s is empty\n",
4036 ctxt->URL);
4037 ctxt->nbErrors++;
4038 return (NULL);
4039 }
4040 /*
4041 * ns transmission rules
4042 */
4043 ns = xmlGetProp(root, BAD_CAST "ns");
4044 if (ns == NULL) {
4045 tmp = node;
4046 while ((tmp != NULL) &&
4047 (tmp->type == XML_ELEMENT_NODE)) {
4048 ns = xmlGetProp(tmp, BAD_CAST "ns");
4049 if (ns != NULL) {
4050 break;
4051 }
4052 tmp = tmp->parent;
4053 }
4054 if (ns != NULL) {
4055 xmlSetProp(root, BAD_CAST "ns", ns);
4056 newNs = 1;
4057 xmlFree(ns);
4058 }
4059 } else {
4060 xmlFree(ns);
4061 }
4062
4063 /*
4064 * Parsing to get a precompiled schemas.
4065 */
Daniel Veillard77648bb2003-02-20 15:03:22 +00004066 oldflags = ctxt->flags;
4067 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004068 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard77648bb2003-02-20 15:03:22 +00004069 ctxt->flags = oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004070 if ((docu->schema != NULL) &&
4071 (docu->schema->topgrammar != NULL)) {
4072 docu->content = docu->schema->topgrammar->start;
4073 }
4074
4075 /*
4076 * the externalRef may be reused in a different ns context
4077 */
4078 if (newNs == 1) {
4079 xmlUnsetProp(root, BAD_CAST "ns");
4080 }
4081 }
4082 def->content = docu->content;
4083 } else {
4084 def = NULL;
4085 }
4086 return(def);
4087}
4088
4089/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004090 * xmlRelaxNGParsePattern:
4091 * @ctxt: a Relax-NG parser context
4092 * @node: the pattern node.
4093 *
4094 * parse the content of a RelaxNG pattern node.
4095 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004096 * Returns the definition pointer or NULL in case of error or if no
4097 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004098 */
4099static xmlRelaxNGDefinePtr
4100xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4101 xmlRelaxNGDefinePtr def = NULL;
4102
Daniel Veillardd2298792003-02-14 16:54:11 +00004103 if (node == NULL) {
4104 return(NULL);
4105 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004106 if (IS_RELAXNG(node, "element")) {
4107 def = xmlRelaxNGParseElement(ctxt, node);
4108 } else if (IS_RELAXNG(node, "attribute")) {
4109 def = xmlRelaxNGParseAttribute(ctxt, node);
4110 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004111 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004112 if (def == NULL)
4113 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004114 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00004115 if (node->children != NULL) {
4116 if (ctxt->error != NULL)
4117 ctxt->error(ctxt->userData, "empty: had a child node\n");
4118 ctxt->nbErrors++;
4119 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004120 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004121 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004122 if (def == NULL)
4123 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004124 def->type = XML_RELAXNG_TEXT;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004125 if (node->children != NULL) {
4126 if (ctxt->error != NULL)
4127 ctxt->error(ctxt->userData, "text: had a child node\n");
4128 ctxt->nbErrors++;
4129 }
4130 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004131 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004132 if (def == NULL)
4133 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004134 def->type = XML_RELAXNG_ZEROORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004135 if (node->children == NULL) {
4136 if (ctxt->error != NULL)
4137 ctxt->error(ctxt->userData,
4138 "Element %s is empty\n", node->name);
4139 ctxt->nbErrors++;
4140 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004141 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
Daniel Veillardd2298792003-02-14 16:54:11 +00004142 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004143 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004144 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004145 if (def == NULL)
4146 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004147 def->type = XML_RELAXNG_ONEORMORE;
Daniel Veillardd2298792003-02-14 16:54:11 +00004148 if (node->children == NULL) {
4149 if (ctxt->error != NULL)
4150 ctxt->error(ctxt->userData,
4151 "Element %s is empty\n", node->name);
4152 ctxt->nbErrors++;
4153 } else {
4154 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4155 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004156 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004157 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004158 if (def == NULL)
4159 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004160 def->type = XML_RELAXNG_OPTIONAL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004161 if (node->children == NULL) {
4162 if (ctxt->error != NULL)
4163 ctxt->error(ctxt->userData,
4164 "Element %s is empty\n", node->name);
4165 ctxt->nbErrors++;
4166 } else {
4167 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4168 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004169 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004170 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004171 if (def == NULL)
4172 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004173 def->type = XML_RELAXNG_CHOICE;
4174 if (node->children == NULL) {
4175 if (ctxt->error != NULL)
4176 ctxt->error(ctxt->userData,
4177 "Element %s is empty\n", node->name);
4178 ctxt->nbErrors++;
4179 } else {
4180 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4181 }
4182 } else if (IS_RELAXNG(node, "group")) {
4183 def = xmlRelaxNGNewDefine(ctxt, node);
4184 if (def == NULL)
4185 return(NULL);
4186 def->type = XML_RELAXNG_GROUP;
4187 if (node->children == NULL) {
4188 if (ctxt->error != NULL)
4189 ctxt->error(ctxt->userData,
4190 "Element %s is empty\n", node->name);
4191 ctxt->nbErrors++;
4192 } else {
4193 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4194 }
4195 } else if (IS_RELAXNG(node, "ref")) {
4196 def = xmlRelaxNGNewDefine(ctxt, node);
4197 if (def == NULL)
4198 return(NULL);
4199 def->type = XML_RELAXNG_REF;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004200 def->name = xmlGetProp(node, BAD_CAST "name");
4201 if (def->name == NULL) {
4202 if (ctxt->error != NULL)
4203 ctxt->error(ctxt->userData,
4204 "ref has no name\n");
4205 ctxt->nbErrors++;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004206 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004207 xmlRelaxNGNormExtSpace(def->name);
4208 if (xmlValidateNCName(def->name, 0)) {
4209 if (ctxt->error != NULL)
4210 ctxt->error(ctxt->userData,
4211 "ref name '%s' is not an NCName\n",
4212 def->name);
4213 ctxt->nbErrors++;
4214 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004215 }
4216 if (node->children != NULL) {
4217 if (ctxt->error != NULL)
4218 ctxt->error(ctxt->userData,
4219 "ref is not empty\n");
4220 ctxt->nbErrors++;
4221 }
4222 if (ctxt->grammar->refs == NULL)
4223 ctxt->grammar->refs = xmlHashCreate(10);
4224 if (ctxt->grammar->refs == NULL) {
4225 if (ctxt->error != NULL)
4226 ctxt->error(ctxt->userData,
4227 "Could not create references hash\n");
4228 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004229 def = NULL;
4230 } else {
4231 int tmp;
4232
4233 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4234 if (tmp < 0) {
4235 xmlRelaxNGDefinePtr prev;
4236
4237 prev = (xmlRelaxNGDefinePtr)
4238 xmlHashLookup(ctxt->grammar->refs, def->name);
4239 if (prev == NULL) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004240 if (def->name != NULL) {
4241 if (ctxt->error != NULL)
4242 ctxt->error(ctxt->userData,
4243 "Error refs definitions '%s'\n",
4244 def->name);
4245 } else {
4246 if (ctxt->error != NULL)
4247 ctxt->error(ctxt->userData,
4248 "Error refs definitions\n");
4249 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004250 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004251 def = NULL;
4252 } else {
4253 def->nextHash = prev->nextHash;
4254 prev->nextHash = def;
4255 }
4256 }
4257 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004258 } else if (IS_RELAXNG(node, "data")) {
4259 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004260 } else if (IS_RELAXNG(node, "value")) {
4261 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004262 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004263 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004264 if (def == NULL)
4265 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004266 def->type = XML_RELAXNG_LIST;
Daniel Veillardd2298792003-02-14 16:54:11 +00004267 if (node->children == NULL) {
4268 if (ctxt->error != NULL)
4269 ctxt->error(ctxt->userData,
4270 "Element %s is empty\n", node->name);
4271 ctxt->nbErrors++;
4272 } else {
4273 def->content = xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4274 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004275 } else if (IS_RELAXNG(node, "interleave")) {
4276 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004277 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillardfebcca42003-02-16 15:44:18 +00004278 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004279 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004280 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004281 if (def == NULL)
4282 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004283 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004284 if (node->children != NULL) {
4285 if (ctxt->error != NULL)
4286 ctxt->error(ctxt->userData,
4287 "xmlRelaxNGParse: notAllowed element is not empty\n");
4288 ctxt->nbErrors++;
4289 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004290 } else if (IS_RELAXNG(node, "grammar")) {
4291 xmlRelaxNGGrammarPtr grammar, old;
4292 xmlRelaxNGGrammarPtr oldparent;
4293
Daniel Veillardc482e262003-02-26 14:48:48 +00004294#ifdef DEBUG_GRAMMAR
4295 xmlGenericError(xmlGenericErrorContext, "Found <grammar> pattern\n");
4296#endif
4297
Daniel Veillard419a7682003-02-03 23:22:49 +00004298 oldparent = ctxt->parentgrammar;
4299 old = ctxt->grammar;
4300 ctxt->parentgrammar = old;
4301 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4302 if (old != NULL) {
4303 ctxt->grammar = old;
4304 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00004305#if 0
Daniel Veillard419a7682003-02-03 23:22:49 +00004306 if (grammar != NULL) {
4307 grammar->next = old->next;
4308 old->next = grammar;
4309 }
Daniel Veillardc482e262003-02-26 14:48:48 +00004310#endif
Daniel Veillard419a7682003-02-03 23:22:49 +00004311 }
4312 if (grammar != NULL)
4313 def = grammar->start;
4314 else
4315 def = NULL;
4316 } else if (IS_RELAXNG(node, "parentRef")) {
4317 if (ctxt->parentgrammar == NULL) {
4318 if (ctxt->error != NULL)
4319 ctxt->error(ctxt->userData,
4320 "Use of parentRef without a parent grammar\n");
4321 ctxt->nbErrors++;
4322 return(NULL);
4323 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004324 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard419a7682003-02-03 23:22:49 +00004325 if (def == NULL)
4326 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004327 def->type = XML_RELAXNG_PARENTREF;
Daniel Veillard419a7682003-02-03 23:22:49 +00004328 def->name = xmlGetProp(node, BAD_CAST "name");
4329 if (def->name == NULL) {
4330 if (ctxt->error != NULL)
4331 ctxt->error(ctxt->userData,
4332 "parentRef has no name\n");
4333 ctxt->nbErrors++;
Daniel Veillardd2298792003-02-14 16:54:11 +00004334 } else {
4335 xmlRelaxNGNormExtSpace(def->name);
4336 if (xmlValidateNCName(def->name, 0)) {
4337 if (ctxt->error != NULL)
4338 ctxt->error(ctxt->userData,
4339 "parentRef name '%s' is not an NCName\n",
4340 def->name);
4341 ctxt->nbErrors++;
4342 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004343 }
4344 if (node->children != NULL) {
4345 if (ctxt->error != NULL)
4346 ctxt->error(ctxt->userData,
4347 "parentRef is not empty\n");
4348 ctxt->nbErrors++;
4349 }
4350 if (ctxt->parentgrammar->refs == NULL)
4351 ctxt->parentgrammar->refs = xmlHashCreate(10);
4352 if (ctxt->parentgrammar->refs == NULL) {
4353 if (ctxt->error != NULL)
4354 ctxt->error(ctxt->userData,
4355 "Could not create references hash\n");
4356 ctxt->nbErrors++;
4357 def = NULL;
Daniel Veillardd2298792003-02-14 16:54:11 +00004358 } else if (def->name != NULL) {
Daniel Veillard419a7682003-02-03 23:22:49 +00004359 int tmp;
4360
4361 tmp = xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4362 if (tmp < 0) {
4363 xmlRelaxNGDefinePtr prev;
4364
4365 prev = (xmlRelaxNGDefinePtr)
4366 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4367 if (prev == NULL) {
4368 if (ctxt->error != NULL)
4369 ctxt->error(ctxt->userData,
4370 "Internal error parentRef definitions '%s'\n",
4371 def->name);
4372 ctxt->nbErrors++;
4373 def = NULL;
4374 } else {
4375 def->nextHash = prev->nextHash;
4376 prev->nextHash = def;
4377 }
4378 }
4379 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004380 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004381 if (node->children == NULL) {
4382 if (ctxt->error != NULL)
4383 ctxt->error(ctxt->userData,
4384 "Mixed is empty\n");
4385 ctxt->nbErrors++;
4386 def = NULL;
4387 } else {
4388 def = xmlRelaxNGParseInterleave(ctxt, node);
4389 if (def != NULL) {
4390 xmlRelaxNGDefinePtr tmp;
4391
4392 if ((def->content != NULL) && (def->content->next != NULL)) {
4393 tmp = xmlRelaxNGNewDefine(ctxt, node);
4394 if (tmp != NULL) {
4395 tmp->type = XML_RELAXNG_GROUP;
4396 tmp->content = def->content;
4397 def->content = tmp;
4398 }
4399 }
4400
4401 tmp = xmlRelaxNGNewDefine(ctxt, node);
4402 if (tmp == NULL)
4403 return(def);
4404 tmp->type = XML_RELAXNG_TEXT;
4405 tmp->next = def->content;
4406 def->content = tmp;
4407 }
4408 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004409 } else {
4410 if (ctxt->error != NULL)
4411 ctxt->error(ctxt->userData,
4412 "Unexpected node %s is not a pattern\n",
4413 node->name);
4414 ctxt->nbErrors++;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004415 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004416 }
4417 return(def);
4418}
4419
4420/**
4421 * xmlRelaxNGParseAttribute:
4422 * @ctxt: a Relax-NG parser context
4423 * @node: the element node
4424 *
4425 * parse the content of a RelaxNG attribute node.
4426 *
4427 * Returns the definition pointer or NULL in case of error.
4428 */
4429static xmlRelaxNGDefinePtr
4430xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
Daniel Veillardd2298792003-02-14 16:54:11 +00004431 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004432 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004433 int old_flags;
4434
Daniel Veillardfd573f12003-03-16 17:52:32 +00004435 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004436 if (ret == NULL)
4437 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004438 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004439 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004440 child = node->children;
4441 if (child == NULL) {
4442 if (ctxt->error != NULL)
4443 ctxt->error(ctxt->userData,
4444 "xmlRelaxNGParseattribute: attribute has no children\n");
4445 ctxt->nbErrors++;
4446 return(ret);
4447 }
4448 old_flags = ctxt->flags;
4449 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004450 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4451 if (cur != NULL)
4452 child = child->next;
4453
Daniel Veillardd2298792003-02-14 16:54:11 +00004454 if (child != NULL) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00004455 cur = xmlRelaxNGParsePattern(ctxt, child);
4456 if (cur != NULL) {
4457 switch (cur->type) {
4458 case XML_RELAXNG_EMPTY:
4459 case XML_RELAXNG_NOT_ALLOWED:
4460 case XML_RELAXNG_TEXT:
4461 case XML_RELAXNG_ELEMENT:
4462 case XML_RELAXNG_DATATYPE:
4463 case XML_RELAXNG_VALUE:
4464 case XML_RELAXNG_LIST:
4465 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00004466 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004467 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004468 case XML_RELAXNG_DEF:
4469 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004470 case XML_RELAXNG_ZEROORMORE:
4471 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004472 case XML_RELAXNG_CHOICE:
4473 case XML_RELAXNG_GROUP:
4474 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard77648bb2003-02-20 15:03:22 +00004475 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardd2298792003-02-14 16:54:11 +00004476 ret->content = cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004477 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004478 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004479 case XML_RELAXNG_START:
Daniel Veillard8fe98712003-02-19 00:19:14 +00004480 case XML_RELAXNG_PARAM:
Daniel Veillard144fae12003-02-03 13:17:57 +00004481 case XML_RELAXNG_EXCEPT:
Daniel Veillardd2298792003-02-14 16:54:11 +00004482 if (ctxt->error != NULL)
4483 ctxt->error(ctxt->userData,
4484 "attribute has invalid content\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004485 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004486 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004487 case XML_RELAXNG_NOOP:
4488 TODO
4489 if (ctxt->error != NULL)
4490 ctxt->error(ctxt->userData,
4491 "Internal error, noop found\n");
4492 ctxt->nbErrors++;
4493 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004494 }
4495 }
4496 child = child->next;
4497 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004498 if (child != NULL) {
4499 if (ctxt->error != NULL)
4500 ctxt->error(ctxt->userData, "attribute has multiple children\n");
4501 ctxt->nbErrors++;
4502 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004503 ctxt->flags = old_flags;
4504 return(ret);
4505}
4506
4507/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004508 * xmlRelaxNGParseExceptNameClass:
4509 * @ctxt: a Relax-NG parser context
4510 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00004511 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004512 *
4513 * parse the content of a RelaxNG nameClass node.
4514 *
4515 * Returns the definition pointer or NULL in case of error.
4516 */
4517static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00004518xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
4519 xmlNodePtr node, int attr) {
4520 xmlRelaxNGDefinePtr ret, cur, last = NULL;
4521 xmlNodePtr child;
4522
Daniel Veillardd2298792003-02-14 16:54:11 +00004523 if (!IS_RELAXNG(node, "except")) {
4524 if (ctxt->error != NULL)
4525 ctxt->error(ctxt->userData,
4526 "Expecting an except node\n");
4527 ctxt->nbErrors++;
Daniel Veillard144fae12003-02-03 13:17:57 +00004528 return(NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004529 }
4530 if (node->next != NULL) {
4531 if (ctxt->error != NULL)
4532 ctxt->error(ctxt->userData,
4533 "exceptNameClass allows only a single except node\n");
4534 ctxt->nbErrors++;
4535 }
Daniel Veillard144fae12003-02-03 13:17:57 +00004536 if (node->children == NULL) {
4537 if (ctxt->error != NULL)
4538 ctxt->error(ctxt->userData,
4539 "except has no content\n");
4540 ctxt->nbErrors++;
4541 return(NULL);
4542 }
4543
Daniel Veillardfd573f12003-03-16 17:52:32 +00004544 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00004545 if (ret == NULL)
4546 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004547 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00004548 child = node->children;
4549 while (child != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004550 cur = xmlRelaxNGNewDefine(ctxt, child);
Daniel Veillard144fae12003-02-03 13:17:57 +00004551 if (cur == NULL)
4552 break;
4553 if (attr)
4554 cur->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004555 else
4556 cur->type = XML_RELAXNG_ELEMENT;
Daniel Veillard144fae12003-02-03 13:17:57 +00004557
Daniel Veillard419a7682003-02-03 23:22:49 +00004558 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard144fae12003-02-03 13:17:57 +00004559 if (last == NULL) {
4560 ret->content = cur;
4561 } else {
4562 last->next = cur;
4563 }
4564 last = cur;
4565 }
4566 child = child->next;
4567 }
4568
4569 return(ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004570}
4571
4572/**
4573 * xmlRelaxNGParseNameClass:
4574 * @ctxt: a Relax-NG parser context
4575 * @node: the nameClass node
4576 * @def: the current definition
4577 *
4578 * parse the content of a RelaxNG nameClass node.
4579 *
4580 * Returns the definition pointer or NULL in case of error.
4581 */
4582static xmlRelaxNGDefinePtr
4583xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
4584 xmlRelaxNGDefinePtr def) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004585 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004586 xmlChar *val;
4587
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004588 ret = def;
4589 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
4590 (IS_RELAXNG(node, "nsName"))) {
4591 if ((def->type != XML_RELAXNG_ELEMENT) &&
4592 (def->type != XML_RELAXNG_ATTRIBUTE)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004593 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004594 if (ret == NULL)
4595 return(NULL);
4596 ret->parent = def;
4597 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
4598 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004599 else
4600 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004601 }
4602 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004603 if (IS_RELAXNG(node, "name")) {
4604 val = xmlNodeGetContent(node);
Daniel Veillardd2298792003-02-14 16:54:11 +00004605 xmlRelaxNGNormExtSpace(val);
4606 if (xmlValidateNCName(val, 0)) {
4607 if (ctxt->error != NULL) {
4608 if (node->parent != NULL)
4609 ctxt->error(ctxt->userData,
4610 "Element %s name '%s' is not an NCName\n",
4611 node->parent->name, val);
4612 else
4613 ctxt->error(ctxt->userData,
4614 "name '%s' is not an NCName\n",
4615 val);
4616 }
4617 ctxt->nbErrors++;
4618 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004619 ret->name = val;
4620 val = xmlGetProp(node, BAD_CAST "ns");
4621 ret->ns = val;
Daniel Veillard416589a2003-02-17 17:25:42 +00004622 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4623 (val != NULL) &&
4624 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
4625 ctxt->error(ctxt->userData,
4626 "Attribute with namespace '%s' is not allowed\n",
4627 val);
4628 ctxt->nbErrors++;
4629 }
4630 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4631 (val != NULL) &&
4632 (val[0] == 0) &&
4633 (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
4634 ctxt->error(ctxt->userData,
4635 "Attribute with QName 'xmlns' is not allowed\n",
4636 val);
4637 ctxt->nbErrors++;
4638 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004639 } else if (IS_RELAXNG(node, "anyName")) {
4640 ret->name = NULL;
4641 ret->ns = NULL;
4642 if (node->children != NULL) {
4643 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00004644 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
4645 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004646 }
4647 } else if (IS_RELAXNG(node, "nsName")) {
4648 ret->name = NULL;
4649 ret->ns = xmlGetProp(node, BAD_CAST "ns");
4650 if (ret->ns == NULL) {
4651 if (ctxt->error != NULL)
4652 ctxt->error(ctxt->userData,
4653 "nsName has no ns attribute\n");
4654 ctxt->nbErrors++;
4655 }
Daniel Veillard416589a2003-02-17 17:25:42 +00004656 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
4657 (ret->ns != NULL) &&
4658 (xmlStrEqual(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
4659 ctxt->error(ctxt->userData,
4660 "Attribute with namespace '%s' is not allowed\n",
4661 ret->ns);
4662 ctxt->nbErrors++;
4663 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004664 if (node->children != NULL) {
4665 ret->nameClass =
Daniel Veillard144fae12003-02-03 13:17:57 +00004666 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
4667 (def->type == XML_RELAXNG_ATTRIBUTE));
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004668 }
4669 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004670 xmlNodePtr child;
4671 xmlRelaxNGDefinePtr last = NULL;
4672
4673 ret = xmlRelaxNGNewDefine(ctxt, node);
4674 if (ret == NULL)
4675 return(NULL);
4676 ret->parent = def;
4677 ret->type = XML_RELAXNG_CHOICE;
4678
Daniel Veillardd2298792003-02-14 16:54:11 +00004679 if (node->children == NULL) {
4680 if (ctxt->error != NULL)
4681 ctxt->error(ctxt->userData,
4682 "Element choice is empty\n");
4683 ctxt->nbErrors++;
4684 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004685
4686 child = node->children;
4687 while (child != NULL) {
4688 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
4689 if (tmp != NULL) {
4690 if (last == NULL) {
4691 last = ret->nameClass = tmp;
4692 } else {
4693 last->next = tmp;
4694 last = tmp;
4695 }
4696 }
4697 child = child->next;
4698 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004699 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004700 } else {
4701 if (ctxt->error != NULL)
4702 ctxt->error(ctxt->userData,
Daniel Veillardfd573f12003-03-16 17:52:32 +00004703 "expecting name, anyName, nsName or choice : got %s\n",
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004704 node->name);
4705 ctxt->nbErrors++;
4706 return(NULL);
4707 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00004708 if (ret != def) {
4709 if (def->nameClass == NULL) {
4710 def->nameClass = ret;
4711 } else {
4712 tmp = def->nameClass;
4713 while (tmp->next != NULL) {
4714 tmp = tmp->next;
4715 }
4716 tmp->next = ret;
4717 }
4718 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004719 return(ret);
4720}
4721
4722/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004723 * xmlRelaxNGParseElement:
4724 * @ctxt: a Relax-NG parser context
4725 * @node: the element node
4726 *
4727 * parse the content of a RelaxNG element node.
4728 *
4729 * Returns the definition pointer or NULL in case of error.
4730 */
4731static xmlRelaxNGDefinePtr
4732xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
4733 xmlRelaxNGDefinePtr ret, cur, last;
4734 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00004735 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004736
Daniel Veillardfd573f12003-03-16 17:52:32 +00004737 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004738 if (ret == NULL)
4739 return(NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004740 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004741 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004742 child = node->children;
4743 if (child == NULL) {
4744 if (ctxt->error != NULL)
4745 ctxt->error(ctxt->userData,
4746 "xmlRelaxNGParseElement: element has no children\n");
4747 ctxt->nbErrors++;
4748 return(ret);
4749 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00004750 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
4751 if (cur != NULL)
4752 child = child->next;
4753
Daniel Veillard6eadf632003-01-23 18:29:16 +00004754 if (child == NULL) {
4755 if (ctxt->error != NULL)
4756 ctxt->error(ctxt->userData,
4757 "xmlRelaxNGParseElement: element has no content\n");
4758 ctxt->nbErrors++;
4759 return(ret);
4760 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004761 olddefine = ctxt->define;
4762 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004763 last = NULL;
4764 while (child != NULL) {
4765 cur = xmlRelaxNGParsePattern(ctxt, child);
4766 if (cur != NULL) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004767 cur->parent = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004768 switch (cur->type) {
4769 case XML_RELAXNG_EMPTY:
4770 case XML_RELAXNG_NOT_ALLOWED:
4771 case XML_RELAXNG_TEXT:
4772 case XML_RELAXNG_ELEMENT:
4773 case XML_RELAXNG_DATATYPE:
4774 case XML_RELAXNG_VALUE:
4775 case XML_RELAXNG_LIST:
4776 case XML_RELAXNG_REF:
Daniel Veillard419a7682003-02-03 23:22:49 +00004777 case XML_RELAXNG_PARENTREF:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004778 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004779 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004780 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004781 case XML_RELAXNG_ONEORMORE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00004782 case XML_RELAXNG_OPTIONAL:
Daniel Veillard6eadf632003-01-23 18:29:16 +00004783 case XML_RELAXNG_CHOICE:
4784 case XML_RELAXNG_GROUP:
4785 case XML_RELAXNG_INTERLEAVE:
4786 if (last == NULL) {
4787 ret->content = last = cur;
4788 } else {
4789 if ((last->type == XML_RELAXNG_ELEMENT) &&
4790 (ret->content == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004791 ret->content = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004792 if (ret->content != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004793 ret->content->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004794 ret->content->content = last;
4795 } else {
4796 ret->content = last;
4797 }
4798 }
4799 last->next = cur;
4800 last = cur;
4801 }
4802 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004803 case XML_RELAXNG_ATTRIBUTE:
4804 cur->next = ret->attrs;
4805 ret->attrs = cur;
4806 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004807 case XML_RELAXNG_START:
Daniel Veillard8fe98712003-02-19 00:19:14 +00004808 case XML_RELAXNG_PARAM:
Daniel Veillard144fae12003-02-03 13:17:57 +00004809 case XML_RELAXNG_EXCEPT:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004810 TODO
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004811 ctxt->nbErrors++;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004812 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004813 case XML_RELAXNG_NOOP:
4814 TODO
4815 if (ctxt->error != NULL)
4816 ctxt->error(ctxt->userData,
4817 "Internal error, noop found\n");
4818 ctxt->nbErrors++;
4819 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004820 }
4821 }
4822 child = child->next;
4823 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004824 ctxt->define = olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004825 return(ret);
4826}
4827
4828/**
4829 * xmlRelaxNGParsePatterns:
4830 * @ctxt: a Relax-NG parser context
4831 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00004832 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00004833 *
4834 * parse the content of a RelaxNG start node.
4835 *
4836 * Returns the definition pointer or NULL in case of error.
4837 */
4838static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00004839xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
4840 int group) {
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004841 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004842
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004843 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004844 while (nodes != NULL) {
4845 if (IS_RELAXNG(nodes, "element")) {
4846 cur = xmlRelaxNGParseElement(ctxt, nodes);
4847 if (def == NULL) {
4848 def = last = cur;
4849 } else {
Daniel Veillard154877e2003-01-30 12:17:05 +00004850 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
4851 (def == last)) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004852 def = xmlRelaxNGNewDefine(ctxt, nodes);
4853 def->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004854 def->content = last;
4855 }
4856 last->next = cur;
4857 last = cur;
4858 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004859 cur->parent = parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004860 } else {
4861 cur = xmlRelaxNGParsePattern(ctxt, nodes);
Daniel Veillard419a7682003-02-03 23:22:49 +00004862 if (cur != NULL) {
4863 if (def == NULL) {
4864 def = last = cur;
4865 } else {
4866 last->next = cur;
4867 last = cur;
4868 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004869 }
4870 }
4871 nodes = nodes->next;
4872 }
4873 return(def);
4874}
4875
4876/**
4877 * xmlRelaxNGParseStart:
4878 * @ctxt: a Relax-NG parser context
4879 * @nodes: start children nodes
4880 *
4881 * parse the content of a RelaxNG start node.
4882 *
4883 * Returns 0 in case of success, -1 in case of error
4884 */
4885static int
4886xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
4887 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00004888 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004889
Daniel Veillardd2298792003-02-14 16:54:11 +00004890 if (nodes == NULL) {
4891 if (ctxt->error != NULL)
4892 ctxt->error(ctxt->userData,
4893 "start has no children\n");
4894 ctxt->nbErrors++;
4895 return(-1);
4896 }
4897 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004898 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00004899 if (def == NULL)
4900 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004901 def->type = XML_RELAXNG_EMPTY;
Daniel Veillardd2298792003-02-14 16:54:11 +00004902 if (nodes->children != NULL) {
4903 if (ctxt->error != NULL)
4904 ctxt->error(ctxt->userData, "element empty is not empty\n");
Daniel Veillard1703c5f2003-02-10 14:28:44 +00004905 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004906 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004907 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00004908 def = xmlRelaxNGNewDefine(ctxt, nodes);
Daniel Veillardd2298792003-02-14 16:54:11 +00004909 if (def == NULL)
4910 return(-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004911 def->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardd2298792003-02-14 16:54:11 +00004912 if (nodes->children != NULL) {
4913 if (ctxt->error != NULL)
4914 ctxt->error(ctxt->userData,
4915 "element notAllowed is not empty\n");
4916 ctxt->nbErrors++;
4917 }
Daniel Veillardd2298792003-02-14 16:54:11 +00004918 } else {
4919 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00004920 }
4921 if (ctxt->grammar->start != NULL) {
4922 last = ctxt->grammar->start;
4923 while (last->next != NULL)
4924 last = last->next;
4925 last->next = def;
4926 } else {
Daniel Veillardd2298792003-02-14 16:54:11 +00004927 ctxt->grammar->start = def;
4928 }
4929 nodes = nodes->next;
4930 if (nodes != NULL) {
4931 if (ctxt->error != NULL)
4932 ctxt->error(ctxt->userData,
4933 "start more than one children\n");
4934 ctxt->nbErrors++;
4935 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004936 }
4937 return(ret);
4938}
4939
4940/**
4941 * xmlRelaxNGParseGrammarContent:
4942 * @ctxt: a Relax-NG parser context
4943 * @nodes: grammar children nodes
4944 *
4945 * parse the content of a RelaxNG grammar node.
4946 *
4947 * Returns 0 in case of success, -1 in case of error
4948 */
4949static int
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004950xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00004951{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004952 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004953
4954 if (nodes == NULL) {
4955 if (ctxt->error != NULL)
4956 ctxt->error(ctxt->userData,
4957 "grammar has no children\n");
4958 ctxt->nbErrors++;
4959 return(-1);
4960 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004961 while (nodes != NULL) {
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004962 if (IS_RELAXNG(nodes, "start")) {
4963 if (nodes->children == NULL) {
4964 if (ctxt->error != NULL)
4965 ctxt->error(ctxt->userData,
Daniel Veillardd2298792003-02-14 16:54:11 +00004966 "start has no children\n");
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004967 ctxt->nbErrors++;
4968 } else {
4969 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
4970 if (tmp != 0)
4971 ret = -1;
4972 }
4973 } else if (IS_RELAXNG(nodes, "define")) {
4974 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
4975 if (tmp != 0)
4976 ret = -1;
4977 } else if (IS_RELAXNG(nodes, "include")) {
4978 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
4979 if (tmp != 0)
4980 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004981 } else {
4982 if (ctxt->error != NULL)
4983 ctxt->error(ctxt->userData,
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004984 "grammar has unexpected child %s\n", nodes->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004985 ctxt->nbErrors++;
4986 ret = -1;
4987 }
4988 nodes = nodes->next;
4989 }
4990 return (ret);
4991}
4992
4993/**
4994 * xmlRelaxNGCheckReference:
4995 * @ref: the ref
4996 * @ctxt: a Relax-NG parser context
4997 * @name: the name associated to the defines
4998 *
4999 * Applies the 4.17. combine attribute rule for all the define
5000 * element of a given grammar using the same name.
5001 */
5002static void
5003xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
5004 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5005 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005006 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005007
5008 grammar = ctxt->grammar;
5009 if (grammar == NULL) {
5010 if (ctxt->error != NULL)
5011 ctxt->error(ctxt->userData,
5012 "Internal error: no grammar in CheckReference %s\n",
5013 name);
5014 ctxt->nbErrors++;
5015 return;
5016 }
5017 if (ref->content != NULL) {
5018 if (ctxt->error != NULL)
5019 ctxt->error(ctxt->userData,
5020 "Internal error: reference has content in CheckReference %s\n",
5021 name);
5022 ctxt->nbErrors++;
5023 return;
5024 }
5025 if (grammar->defs != NULL) {
5026 def = xmlHashLookup(grammar->defs, name);
5027 if (def != NULL) {
Daniel Veillard276be4a2003-01-24 01:03:34 +00005028 cur = ref;
5029 while (cur != NULL) {
5030 cur->content = def;
5031 cur = cur->nextHash;
5032 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005033 } else {
Daniel Veillardd4310742003-02-18 21:12:46 +00005034 if (ctxt->error != NULL)
5035 ctxt->error(ctxt->userData,
5036 "Reference %s has no matching definition\n",
5037 name);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00005038 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005039 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005040 } else {
5041 if (ctxt->error != NULL)
5042 ctxt->error(ctxt->userData,
5043 "Reference %s has no matching definition\n",
5044 name);
5045 ctxt->nbErrors++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005046 }
5047 /*
5048 * TODO: make a closure and verify there is no loop !
5049 */
5050}
5051
5052/**
5053 * xmlRelaxNGCheckCombine:
5054 * @define: the define(s) list
5055 * @ctxt: a Relax-NG parser context
5056 * @name: the name associated to the defines
5057 *
5058 * Applies the 4.17. combine attribute rule for all the define
5059 * element of a given grammar using the same name.
5060 */
5061static void
5062xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
5063 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar *name) {
5064 xmlChar *combine;
5065 int choiceOrInterleave = -1;
5066 int missing = 0;
5067 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5068
5069 if (define->nextHash == NULL)
5070 return;
5071 cur = define;
5072 while (cur != NULL) {
5073 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5074 if (combine != NULL) {
5075 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5076 if (choiceOrInterleave == -1)
5077 choiceOrInterleave = 1;
5078 else if (choiceOrInterleave == 0) {
5079 if (ctxt->error != NULL)
5080 ctxt->error(ctxt->userData,
5081 "Defines for %s use both 'choice' and 'interleave'\n",
5082 name);
5083 ctxt->nbErrors++;
5084 }
Daniel Veillard154877e2003-01-30 12:17:05 +00005085 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005086 if (choiceOrInterleave == -1)
5087 choiceOrInterleave = 0;
5088 else if (choiceOrInterleave == 1) {
5089 if (ctxt->error != NULL)
5090 ctxt->error(ctxt->userData,
5091 "Defines for %s use both 'choice' and 'interleave'\n",
5092 name);
5093 ctxt->nbErrors++;
5094 }
5095 } else {
5096 if (ctxt->error != NULL)
5097 ctxt->error(ctxt->userData,
5098 "Defines for %s use unknown combine value '%s''\n",
5099 name, combine);
5100 ctxt->nbErrors++;
5101 }
5102 xmlFree(combine);
5103 } else {
5104 if (missing == 0)
5105 missing = 1;
5106 else {
5107 if (ctxt->error != NULL)
5108 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005109 "Some defines for %s needs the combine attribute\n",
Daniel Veillard6eadf632003-01-23 18:29:16 +00005110 name);
5111 ctxt->nbErrors++;
5112 }
5113 }
5114
5115 cur = cur->nextHash;
5116 }
5117#ifdef DEBUG
5118 xmlGenericError(xmlGenericErrorContext,
5119 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5120 name, choiceOrInterleave);
5121#endif
5122 if (choiceOrInterleave == -1)
5123 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005124 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005125 if (cur == NULL)
5126 return;
5127 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005128 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005129 else
5130 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005131 tmp = define;
5132 last = NULL;
5133 while (tmp != NULL) {
5134 if (tmp->content != NULL) {
5135 if (tmp->content->next != NULL) {
5136 /*
5137 * we need first to create a wrapper.
5138 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00005139 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005140 if (tmp2 == NULL)
5141 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005142 tmp2->type = XML_RELAXNG_GROUP;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005143 tmp2->content = tmp->content;
5144 } else {
5145 tmp2 = tmp->content;
5146 }
5147 if (last == NULL) {
5148 cur->content = tmp2;
5149 } else {
5150 last->next = tmp2;
5151 }
5152 last = tmp2;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005153 }
Daniel Veillard952379b2003-03-17 15:37:12 +00005154 tmp->content = cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005155 tmp = tmp->nextHash;
5156 }
5157 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005158 if (choiceOrInterleave == 0) {
5159 if (ctxt->interleaves == NULL)
5160 ctxt->interleaves = xmlHashCreate(10);
5161 if (ctxt->interleaves == NULL) {
5162 if (ctxt->error != NULL)
5163 ctxt->error(ctxt->userData,
5164 "Failed to create interleaves hash table\n");
5165 ctxt->nbErrors++;
5166 } else {
5167 char tmpname[32];
5168
5169 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5170 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5171 if (ctxt->error != NULL)
5172 ctxt->error(ctxt->userData,
5173 "Failed to add %s to hash table\n", tmpname);
5174 ctxt->nbErrors++;
5175 }
5176 }
5177 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005178}
5179
5180/**
5181 * xmlRelaxNGCombineStart:
5182 * @ctxt: a Relax-NG parser context
5183 * @grammar: the grammar
5184 *
5185 * Applies the 4.17. combine rule for all the start
5186 * element of a given grammar.
5187 */
5188static void
5189xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5190 xmlRelaxNGGrammarPtr grammar) {
5191 xmlRelaxNGDefinePtr starts;
5192 xmlChar *combine;
5193 int choiceOrInterleave = -1;
5194 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005195 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005196
Daniel Veillard2df2de22003-02-17 23:34:33 +00005197 starts = grammar->start;
5198 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard6eadf632003-01-23 18:29:16 +00005199 return;
5200 cur = starts;
5201 while (cur != NULL) {
Daniel Veillard2df2de22003-02-17 23:34:33 +00005202 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5203 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5204 combine = NULL;
5205 if (ctxt->error != NULL)
5206 ctxt->error(ctxt->userData,
5207 "Internal error: start element not found\n");
5208 ctxt->nbErrors++;
5209 } else {
5210 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5211 }
5212
Daniel Veillard6eadf632003-01-23 18:29:16 +00005213 if (combine != NULL) {
5214 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5215 if (choiceOrInterleave == -1)
5216 choiceOrInterleave = 1;
5217 else if (choiceOrInterleave == 0) {
5218 if (ctxt->error != NULL)
5219 ctxt->error(ctxt->userData,
5220 "<start> use both 'choice' and 'interleave'\n");
5221 ctxt->nbErrors++;
5222 }
Daniel Veillard2df2de22003-02-17 23:34:33 +00005223 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00005224 if (choiceOrInterleave == -1)
5225 choiceOrInterleave = 0;
5226 else if (choiceOrInterleave == 1) {
5227 if (ctxt->error != NULL)
5228 ctxt->error(ctxt->userData,
5229 "<start> use both 'choice' and 'interleave'\n");
5230 ctxt->nbErrors++;
5231 }
5232 } else {
5233 if (ctxt->error != NULL)
5234 ctxt->error(ctxt->userData,
5235 "<start> uses unknown combine value '%s''\n", combine);
5236 ctxt->nbErrors++;
5237 }
5238 xmlFree(combine);
5239 } else {
5240 if (missing == 0)
5241 missing = 1;
5242 else {
5243 if (ctxt->error != NULL)
5244 ctxt->error(ctxt->userData,
Daniel Veillardc482e262003-02-26 14:48:48 +00005245 "Some <start> element miss the combine attribute\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00005246 ctxt->nbErrors++;
5247 }
5248 }
5249
Daniel Veillard2df2de22003-02-17 23:34:33 +00005250 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005251 }
5252#ifdef DEBUG
5253 xmlGenericError(xmlGenericErrorContext,
5254 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5255 choiceOrInterleave);
5256#endif
5257 if (choiceOrInterleave == -1)
5258 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005259 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005260 if (cur == NULL)
5261 return;
5262 if (choiceOrInterleave == 0)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005263 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005264 else
5265 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005266 cur->content = grammar->start;
5267 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005268 if (choiceOrInterleave == 0) {
5269 if (ctxt->interleaves == NULL)
5270 ctxt->interleaves = xmlHashCreate(10);
5271 if (ctxt->interleaves == NULL) {
5272 if (ctxt->error != NULL)
5273 ctxt->error(ctxt->userData,
5274 "Failed to create interleaves hash table\n");
5275 ctxt->nbErrors++;
5276 } else {
5277 char tmpname[32];
5278
5279 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5280 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) < 0) {
5281 if (ctxt->error != NULL)
5282 ctxt->error(ctxt->userData,
5283 "Failed to add %s to hash table\n", tmpname);
5284 ctxt->nbErrors++;
5285 }
5286 }
5287 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005288}
5289
5290/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005291 * xmlRelaxNGCheckCycles:
5292 * @ctxt: a Relax-NG parser context
5293 * @nodes: grammar children nodes
5294 * @depth: the counter
5295 *
5296 * Check for cycles.
5297 *
5298 * Returns 0 if check passed, and -1 in case of error
5299 */
5300static int
5301xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5302 xmlRelaxNGDefinePtr cur, int depth) {
5303 int ret = 0;
5304
5305 while ((ret == 0) && (cur != NULL)) {
5306 if ((cur->type == XML_RELAXNG_REF) ||
5307 (cur->type == XML_RELAXNG_PARENTREF)) {
5308 if (cur->depth == -1) {
5309 cur->depth = depth;
5310 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5311 cur->depth = -2;
5312 } else if (depth == cur->depth) {
5313 if (ctxt->error != NULL)
5314 ctxt->error(ctxt->userData,
5315 "Detected a cycle in %s references\n", cur->name);
5316 ctxt->nbErrors++;
5317 return(-1);
5318 }
5319 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5320 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5321 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005322 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
Daniel Veillardd4310742003-02-18 21:12:46 +00005323 }
5324 cur = cur->next;
5325 }
5326 return(ret);
5327}
5328
5329/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005330 * xmlRelaxNGTryUnlink:
5331 * @ctxt: a Relax-NG parser context
5332 * @cur: the definition to unlink
5333 * @parent: the parent definition
5334 * @prev: the previous sibling definition
5335 *
5336 * Try to unlink a definition. If not possble make it a NOOP
5337 *
5338 * Returns the new prev definition
5339 */
5340static xmlRelaxNGDefinePtr
5341xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5342 xmlRelaxNGDefinePtr cur,
5343 xmlRelaxNGDefinePtr parent,
5344 xmlRelaxNGDefinePtr prev) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005345 if (prev != NULL) {
5346 prev->next = cur->next;
5347 } else {
5348 if (parent != NULL) {
5349 if (parent->content == cur)
5350 parent->content = cur->next;
5351 else if (parent->attrs == cur)
5352 parent->attrs = cur->next;
5353 else if (parent->nameClass == cur)
5354 parent->nameClass = cur->next;
5355 } else {
5356 cur->type = XML_RELAXNG_NOOP;
5357 prev = cur;
5358 }
5359 }
5360 return(prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005361}
5362
5363/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005364 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005365 * @ctxt: a Relax-NG parser context
5366 * @nodes: grammar children nodes
5367 *
5368 * Check for simplification of empty and notAllowed
5369 */
5370static void
5371xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5372 xmlRelaxNGDefinePtr cur,
5373 xmlRelaxNGDefinePtr parent) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005374 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005375
Daniel Veillardfd573f12003-03-16 17:52:32 +00005376 while (cur != NULL) {
5377 if ((cur->type == XML_RELAXNG_REF) ||
5378 (cur->type == XML_RELAXNG_PARENTREF)) {
5379 if (cur->depth != -3) {
5380 cur->depth = -3;
5381 xmlRelaxNGSimplify(ctxt, cur->content, cur);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005382 }
5383 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005384 cur->parent = parent;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005385 if ((parent != NULL) &&
5386 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5387 (parent->type == XML_RELAXNG_LIST) ||
5388 (parent->type == XML_RELAXNG_GROUP) ||
5389 (parent->type == XML_RELAXNG_INTERLEAVE) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00005390 (parent->type == XML_RELAXNG_ONEORMORE) ||
5391 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005392 parent->type = XML_RELAXNG_NOT_ALLOWED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005393 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005394 }
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005395 if ((parent != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00005396 (parent->type == XML_RELAXNG_CHOICE)) {
5397 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5398 } else
5399 prev = cur;
5400 } else if (cur->type == XML_RELAXNG_EMPTY){
5401 cur->parent = parent;
5402 if ((parent != NULL) &&
5403 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5404 (parent->type == XML_RELAXNG_ZEROORMORE))) {
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005405 parent->type = XML_RELAXNG_EMPTY;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005406 break;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005407 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005408 if ((parent != NULL) &&
5409 ((parent->type == XML_RELAXNG_GROUP) ||
5410 (parent->type == XML_RELAXNG_INTERLEAVE))) {
5411 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5412 } else
5413 prev = cur;
5414 } else {
5415 cur->parent = parent;
5416 if (cur->content != NULL)
5417 xmlRelaxNGSimplify(ctxt, cur->content, cur);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00005418 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
Daniel Veillardfd573f12003-03-16 17:52:32 +00005419 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5420 if (cur->nameClass != NULL)
5421 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5422 /*
5423 * This may result in a simplification
5424 */
5425 if ((cur->type == XML_RELAXNG_GROUP) ||
5426 (cur->type == XML_RELAXNG_INTERLEAVE)) {
5427 if (cur->content == NULL)
5428 cur->type = XML_RELAXNG_EMPTY;
5429 else if (cur->content->next == NULL) {
5430 if ((parent == NULL) && (prev == NULL)) {
5431 cur->type = XML_RELAXNG_NOOP;
5432 } else if (prev == NULL) {
5433 parent->content = cur->content;
5434 cur->content->next = cur->next;
5435 cur = cur->content;
5436 } else {
5437 cur->content->next = cur->next;
5438 prev->next = cur->content;
5439 cur = cur->content;
5440 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005441 }
5442 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005443 /*
5444 * the current node may have been transformed back
5445 */
5446 if ((cur->type == XML_RELAXNG_EXCEPT) &&
5447 (cur->content != NULL) &&
5448 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
5449 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5450 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5451 if ((parent != NULL) &&
5452 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5453 (parent->type == XML_RELAXNG_LIST) ||
5454 (parent->type == XML_RELAXNG_GROUP) ||
5455 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5456 (parent->type == XML_RELAXNG_ONEORMORE) ||
5457 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5458 parent->type = XML_RELAXNG_NOT_ALLOWED;
5459 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005460 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005461 if ((parent != NULL) &&
5462 (parent->type == XML_RELAXNG_CHOICE)) {
5463 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5464 } else
5465 prev = cur;
5466 } else if (cur->type == XML_RELAXNG_EMPTY){
5467 if ((parent != NULL) &&
5468 ((parent->type == XML_RELAXNG_ONEORMORE) ||
5469 (parent->type == XML_RELAXNG_ZEROORMORE))) {
5470 parent->type = XML_RELAXNG_EMPTY;
5471 break;
5472 }
5473 if ((parent != NULL) &&
5474 ((parent->type == XML_RELAXNG_GROUP) ||
5475 (parent->type == XML_RELAXNG_INTERLEAVE) ||
5476 (parent->type == XML_RELAXNG_CHOICE))) {
5477 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5478 } else
5479 prev = cur;
5480 } else {
5481 prev = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005482 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005483 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005484 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005485 }
5486}
5487
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005488/**
5489 * xmlRelaxNGGroupContentType:
5490 * @ct1: the first content type
5491 * @ct2: the second content type
5492 *
5493 * Try to group 2 content types
5494 *
5495 * Returns the content type
5496 */
5497static xmlRelaxNGContentType
5498xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
5499 xmlRelaxNGContentType ct2) {
5500 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
5501 (ct2 == XML_RELAXNG_CONTENT_ERROR))
5502 return(XML_RELAXNG_CONTENT_ERROR);
5503 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
5504 return(ct2);
5505 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
5506 return(ct1);
5507 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
5508 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
5509 return(XML_RELAXNG_CONTENT_COMPLEX);
5510 return(XML_RELAXNG_CONTENT_ERROR);
5511}
5512
5513/**
5514 * xmlRelaxNGMaxContentType:
5515 * @ct1: the first content type
5516 * @ct2: the second content type
5517 *
5518 * Compute the max content-type
5519 *
5520 * Returns the content type
5521 */
5522static xmlRelaxNGContentType
5523xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
5524 xmlRelaxNGContentType ct2) {
5525 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
5526 (ct2 == XML_RELAXNG_CONTENT_ERROR))
5527 return(XML_RELAXNG_CONTENT_ERROR);
5528 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
5529 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
5530 return(XML_RELAXNG_CONTENT_SIMPLE);
5531 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
5532 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
5533 return(XML_RELAXNG_CONTENT_COMPLEX);
5534 return(XML_RELAXNG_CONTENT_EMPTY);
5535}
Daniel Veillard77648bb2003-02-20 15:03:22 +00005536
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005537/**
5538 * xmlRelaxNGCheckRules:
5539 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005540 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00005541 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00005542 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005543 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005544 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005545 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005546 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005547 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005548static xmlRelaxNGContentType
Daniel Veillardfd573f12003-03-16 17:52:32 +00005549xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
5550 xmlRelaxNGDefinePtr cur, int flags,
5551 xmlRelaxNGType ptype) {
Daniel Veillard4c5cf702003-02-21 15:40:34 +00005552 int nflags = flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005553 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005554
Daniel Veillardfd573f12003-03-16 17:52:32 +00005555 while (cur != NULL) {
5556 ret = XML_RELAXNG_CONTENT_EMPTY;
5557 if ((cur->type == XML_RELAXNG_REF) ||
5558 (cur->type == XML_RELAXNG_PARENTREF)) {
5559 if (flags & XML_RELAXNG_IN_LIST) {
5560 if (ctxt->error != NULL)
5561 ctxt->error(ctxt->userData,
5562 "Found forbidden pattern list//ref\n");
5563 ctxt->nbErrors++;
5564 }
5565 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5566 if (ctxt->error != NULL)
5567 ctxt->error(ctxt->userData,
5568 "Found forbidden pattern data/except//ref\n");
5569 ctxt->nbErrors++;
5570 }
5571 if (cur->depth > -4) {
5572 cur->depth = -4;
5573 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
5574 flags, cur->type);
5575 cur->depth = ret - 15 ;
5576 } else if (cur->depth == -4) {
5577 ret = XML_RELAXNG_CONTENT_COMPLEX;
5578 } else {
5579 ret = (xmlRelaxNGContentType) cur->depth + 15;
5580 }
5581 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5582 /*
5583 * The 7.3 Attribute derivation rule for groups is plugged there
5584 */
5585 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
5586 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5587 if (ctxt->error != NULL)
5588 ctxt->error(ctxt->userData,
5589 "Found forbidden pattern data/except//element(ref)\n");
5590 ctxt->nbErrors++;
5591 }
5592 if (flags & XML_RELAXNG_IN_LIST) {
5593 if (ctxt->error != NULL)
5594 ctxt->error(ctxt->userData,
5595 "Found forbidden pattern list//element(ref)\n");
5596 ctxt->nbErrors++;
5597 }
5598 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5599 if (ctxt->error != NULL)
5600 ctxt->error(ctxt->userData,
5601 "Found forbidden pattern attribute//element(ref)\n");
5602 ctxt->nbErrors++;
5603 }
5604 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5605 if (ctxt->error != NULL)
5606 ctxt->error(ctxt->userData,
Daniel Veillard463a5472003-02-27 21:30:32 +00005607 "Found forbidden pattern attribute//element(ref)\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005608 ctxt->nbErrors++;
5609 }
5610 /*
5611 * reset since in the simple form elements are only child
5612 * of grammar/define
5613 */
5614 nflags = 0;
5615 ret = xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
5616 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
5617 if (ctxt->error != NULL)
5618 ctxt->error(ctxt->userData,
5619 "Element %s attributes have a content type error\n",
5620 cur->name);
5621 ctxt->nbErrors++;
5622 }
5623 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5624 if (ret == XML_RELAXNG_CONTENT_ERROR) {
5625 if (ctxt->error != NULL)
5626 ctxt->error(ctxt->userData,
5627 "Element %s has a content type error\n",
5628 cur->name);
5629 ctxt->nbErrors++;
5630 } else {
5631 ret = XML_RELAXNG_CONTENT_COMPLEX;
5632 }
5633 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
5634 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
5635 if (ctxt->error != NULL)
5636 ctxt->error(ctxt->userData,
5637 "Found forbidden pattern attribute//attribute\n");
5638 ctxt->nbErrors++;
5639 }
5640 if (flags & XML_RELAXNG_IN_LIST) {
5641 if (ctxt->error != NULL)
5642 ctxt->error(ctxt->userData,
5643 "Found forbidden pattern list//attribute\n");
5644 ctxt->nbErrors++;
5645 }
5646 if (flags & XML_RELAXNG_IN_OOMGROUP) {
5647 if (ctxt->error != NULL)
5648 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005649 "Found forbidden pattern oneOrMore//group//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005650 ctxt->nbErrors++;
5651 }
5652 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
5653 if (ctxt->error != NULL)
5654 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005655 "Found forbidden pattern oneOrMore//interleave//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005656 ctxt->nbErrors++;
5657 }
5658 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5659 if (ctxt->error != NULL)
5660 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005661 "Found forbidden pattern data/except//attribute\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005662 ctxt->nbErrors++;
5663 }
5664 if (flags & XML_RELAXNG_IN_START) {
5665 if (ctxt->error != NULL)
5666 ctxt->error(ctxt->userData,
5667 "Found forbidden pattern start//attribute\n");
5668 ctxt->nbErrors++;
5669 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00005670 if ((!(flags & XML_RELAXNG_IN_ONEORMORE)) && (cur->name == NULL)) {
5671 if (cur->ns == NULL) {
5672 if (ctxt->error != NULL)
5673 ctxt->error(ctxt->userData,
5674 "Found anyName attribute without oneOrMore ancestor\n");
5675 ctxt->nbErrors++;
5676 } else {
5677 if (ctxt->error != NULL)
5678 ctxt->error(ctxt->userData,
5679 "Found nsName attribute without oneOrMore ancestor\n");
5680 ctxt->nbErrors++;
5681 }
Daniel Veillard77648bb2003-02-20 15:03:22 +00005682 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005683 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
5684 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5685 ret = XML_RELAXNG_CONTENT_EMPTY;
5686 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
5687 (cur->type == XML_RELAXNG_ZEROORMORE)) {
5688 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5689 if (ctxt->error != NULL)
5690 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005691 "Found forbidden pattern data/except//oneOrMore\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005692 ctxt->nbErrors++;
5693 }
5694 if (flags & XML_RELAXNG_IN_START) {
5695 if (ctxt->error != NULL)
5696 ctxt->error(ctxt->userData,
5697 "Found forbidden pattern start//oneOrMore\n");
5698 ctxt->nbErrors++;
5699 }
5700 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
5701 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5702 ret = xmlRelaxNGGroupContentType(ret, ret);
5703 } else if (cur->type == XML_RELAXNG_LIST) {
5704 if (flags & XML_RELAXNG_IN_LIST) {
5705 if (ctxt->error != NULL)
5706 ctxt->error(ctxt->userData,
5707 "Found forbidden pattern list//list\n");
5708 ctxt->nbErrors++;
5709 }
5710 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5711 if (ctxt->error != NULL)
5712 ctxt->error(ctxt->userData,
5713 "Found forbidden pattern data/except//list\n");
5714 ctxt->nbErrors++;
5715 }
5716 if (flags & XML_RELAXNG_IN_START) {
5717 if (ctxt->error != NULL)
5718 ctxt->error(ctxt->userData,
5719 "Found forbidden pattern start//list\n");
5720 ctxt->nbErrors++;
5721 }
5722 nflags = flags | XML_RELAXNG_IN_LIST;
5723 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5724 } else if (cur->type == XML_RELAXNG_GROUP) {
5725 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5726 if (ctxt->error != NULL)
5727 ctxt->error(ctxt->userData,
5728 "Found forbidden pattern data/except//group\n");
5729 ctxt->nbErrors++;
5730 }
5731 if (flags & XML_RELAXNG_IN_START) {
5732 if (ctxt->error != NULL)
5733 ctxt->error(ctxt->userData,
5734 "Found forbidden pattern start//group\n");
5735 ctxt->nbErrors++;
5736 }
5737 if (flags & XML_RELAXNG_IN_ONEORMORE)
5738 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
5739 else
5740 nflags = flags;
5741 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5742 /*
5743 * The 7.3 Attribute derivation rule for groups is plugged there
5744 */
5745 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
5746 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
5747 if (flags & XML_RELAXNG_IN_LIST) {
5748 if (ctxt->error != NULL)
5749 ctxt->error(ctxt->userData,
5750 "Found forbidden pattern list//interleave\n");
5751 ctxt->nbErrors++;
5752 }
5753 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5754 if (ctxt->error != NULL)
5755 ctxt->error(ctxt->userData,
Daniel Veillard77648bb2003-02-20 15:03:22 +00005756 "Found forbidden pattern data/except//interleave\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00005757 ctxt->nbErrors++;
5758 }
5759 if (flags & XML_RELAXNG_IN_START) {
5760 if (ctxt->error != NULL)
5761 ctxt->error(ctxt->userData,
5762 "Found forbidden pattern start//interleave\n");
5763 ctxt->nbErrors++;
5764 }
5765 if (flags & XML_RELAXNG_IN_ONEORMORE)
5766 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
5767 else
5768 nflags = flags;
5769 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5770 } else if (cur->type == XML_RELAXNG_EXCEPT) {
5771 if ((cur->parent != NULL) &&
5772 (cur->parent->type == XML_RELAXNG_DATATYPE))
5773 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
5774 else
5775 nflags = flags;
5776 ret = xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
5777 } else if (cur->type == XML_RELAXNG_DATATYPE) {
5778 if (flags & XML_RELAXNG_IN_START) {
5779 if (ctxt->error != NULL)
5780 ctxt->error(ctxt->userData,
5781 "Found forbidden pattern start//data\n");
5782 ctxt->nbErrors++;
5783 }
5784 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5785 ret = XML_RELAXNG_CONTENT_SIMPLE;
5786 } else if (cur->type == XML_RELAXNG_VALUE) {
5787 if (flags & XML_RELAXNG_IN_START) {
5788 if (ctxt->error != NULL)
5789 ctxt->error(ctxt->userData,
5790 "Found forbidden pattern start//value\n");
5791 ctxt->nbErrors++;
5792 }
5793 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5794 ret = XML_RELAXNG_CONTENT_SIMPLE;
5795 } else if (cur->type == XML_RELAXNG_TEXT) {
5796 if (flags & XML_RELAXNG_IN_LIST) {
5797 if (ctxt->error != NULL)
5798 ctxt->error(ctxt->userData,
5799 "Found forbidden pattern list//text\n");
5800 ctxt->nbErrors++;
5801 }
5802 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5803 if (ctxt->error != NULL)
5804 ctxt->error(ctxt->userData,
5805 "Found forbidden pattern data/except//text\n");
5806 ctxt->nbErrors++;
5807 }
5808 if (flags & XML_RELAXNG_IN_START) {
5809 if (ctxt->error != NULL)
5810 ctxt->error(ctxt->userData,
5811 "Found forbidden pattern start//text\n");
5812 ctxt->nbErrors++;
5813 }
5814 ret = XML_RELAXNG_CONTENT_COMPLEX;
5815 } else if (cur->type == XML_RELAXNG_EMPTY) {
5816 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
5817 if (ctxt->error != NULL)
5818 ctxt->error(ctxt->userData,
5819 "Found forbidden pattern data/except//empty\n");
5820 ctxt->nbErrors++;
5821 }
5822 if (flags & XML_RELAXNG_IN_START) {
5823 if (ctxt->error != NULL)
5824 ctxt->error(ctxt->userData,
5825 "Found forbidden pattern start//empty\n");
5826 ctxt->nbErrors++;
5827 }
5828 ret = XML_RELAXNG_CONTENT_EMPTY;
5829 } else if (cur->type == XML_RELAXNG_CHOICE) {
5830 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
5831 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5832 } else {
5833 ret = xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
5834 }
5835 cur = cur->next;
5836 if (ptype == XML_RELAXNG_GROUP) {
5837 val = xmlRelaxNGGroupContentType(val, ret);
5838 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
5839 tmp = xmlRelaxNGGroupContentType(val, ret);
5840 if (tmp != XML_RELAXNG_CONTENT_ERROR)
5841 tmp = xmlRelaxNGMaxContentType(val, ret);
5842 } else if (ptype == XML_RELAXNG_CHOICE) {
5843 val = xmlRelaxNGMaxContentType(val, ret);
5844 } else if (ptype == XML_RELAXNG_LIST) {
5845 val = XML_RELAXNG_CONTENT_SIMPLE;
5846 } else if (ptype == XML_RELAXNG_EXCEPT) {
5847 if (ret == XML_RELAXNG_CONTENT_ERROR)
5848 val = XML_RELAXNG_CONTENT_ERROR;
5849 else
5850 val = XML_RELAXNG_CONTENT_SIMPLE;
5851 } else {
5852 val = xmlRelaxNGGroupContentType(val, ret);
5853 }
5854
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005855 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005856 return(val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005857}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00005858
5859/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005860 * xmlRelaxNGParseGrammar:
5861 * @ctxt: a Relax-NG parser context
5862 * @nodes: grammar children nodes
5863 *
5864 * parse a Relax-NG <grammar> node
5865 *
5866 * Returns the internal xmlRelaxNGGrammarPtr built or
5867 * NULL in case of error
5868 */
5869static xmlRelaxNGGrammarPtr
5870xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes) {
5871 xmlRelaxNGGrammarPtr ret, tmp, old;
5872
Daniel Veillardc482e262003-02-26 14:48:48 +00005873#ifdef DEBUG_GRAMMAR
5874 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
5875#endif
5876
Daniel Veillard6eadf632003-01-23 18:29:16 +00005877 ret = xmlRelaxNGNewGrammar(ctxt);
5878 if (ret == NULL)
5879 return(NULL);
5880
5881 /*
5882 * Link the new grammar in the tree
5883 */
5884 ret->parent = ctxt->grammar;
5885 if (ctxt->grammar != NULL) {
5886 tmp = ctxt->grammar->children;
5887 if (tmp == NULL) {
5888 ctxt->grammar->children = ret;
5889 } else {
5890 while (tmp->next != NULL)
5891 tmp = tmp->next;
5892 tmp->next = ret;
5893 }
5894 }
5895
5896 old = ctxt->grammar;
5897 ctxt->grammar = ret;
5898 xmlRelaxNGParseGrammarContent(ctxt, nodes);
5899 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005900 if (ctxt->grammar == NULL) {
5901 if (ctxt->error != NULL)
5902 ctxt->error(ctxt->userData,
5903 "Failed to parse <grammar> content\n");
5904 ctxt->nbErrors++;
5905 } else if (ctxt->grammar->start == NULL) {
5906 if (ctxt->error != NULL)
5907 ctxt->error(ctxt->userData,
5908 "Element <grammar> has no <start>\n");
5909 ctxt->nbErrors++;
5910 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005911
5912 /*
5913 * Apply 4.17 mergingd rules to defines and starts
5914 */
5915 xmlRelaxNGCombineStart(ctxt, ret);
5916 if (ret->defs != NULL) {
5917 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
5918 ctxt);
5919 }
5920
5921 /*
5922 * link together defines and refs in this grammar
5923 */
5924 if (ret->refs != NULL) {
5925 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
5926 ctxt);
5927 }
Daniel Veillard952379b2003-03-17 15:37:12 +00005928
Daniel Veillard6eadf632003-01-23 18:29:16 +00005929 ctxt->grammar = old;
5930 return(ret);
5931}
5932
5933/**
5934 * xmlRelaxNGParseDocument:
5935 * @ctxt: a Relax-NG parser context
5936 * @node: the root node of the RelaxNG schema
5937 *
5938 * parse a Relax-NG definition resource and build an internal
5939 * xmlRelaxNG struture which can be used to validate instances.
5940 *
5941 * Returns the internal XML RelaxNG structure built or
5942 * NULL in case of error
5943 */
5944static xmlRelaxNGPtr
5945xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
5946 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005947 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00005948 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005949
5950 if ((ctxt == NULL) || (node == NULL))
5951 return (NULL);
5952
5953 schema = xmlRelaxNGNewRelaxNG(ctxt);
5954 if (schema == NULL)
5955 return(NULL);
5956
Daniel Veillard276be4a2003-01-24 01:03:34 +00005957 olddefine = ctxt->define;
5958 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005959 if (IS_RELAXNG(node, "grammar")) {
5960 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5961 } else {
Daniel Veillardc482e262003-02-26 14:48:48 +00005962 xmlRelaxNGGrammarPtr tmp, ret;
5963
5964 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005965 if (schema->topgrammar == NULL) {
5966 return(schema);
5967 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005968 /*
5969 * Link the new grammar in the tree
5970 */
5971 ret->parent = ctxt->grammar;
5972 if (ctxt->grammar != NULL) {
5973 tmp = ctxt->grammar->children;
5974 if (tmp == NULL) {
5975 ctxt->grammar->children = ret;
5976 } else {
5977 while (tmp->next != NULL)
5978 tmp = tmp->next;
5979 tmp->next = ret;
5980 }
5981 }
Daniel Veillarde431a272003-01-29 23:02:33 +00005982 old = ctxt->grammar;
Daniel Veillardc482e262003-02-26 14:48:48 +00005983 ctxt->grammar = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005984 xmlRelaxNGParseStart(ctxt, node);
Daniel Veillarde431a272003-01-29 23:02:33 +00005985 if (old != NULL)
5986 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005987 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005988 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00005989 if (schema->topgrammar->start != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005990 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005991 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005992 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
5993 while ((schema->topgrammar->start != NULL) &&
5994 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
5995 (schema->topgrammar->start->next != NULL))
5996 schema->topgrammar->start = schema->topgrammar->start->content;
5997 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
5998 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
Daniel Veillard77648bb2003-02-20 15:03:22 +00005999 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006000 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006001
6002#ifdef DEBUG
6003 if (schema == NULL)
6004 xmlGenericError(xmlGenericErrorContext,
6005 "xmlRelaxNGParseDocument() failed\n");
6006#endif
6007
6008 return (schema);
6009}
6010
6011/************************************************************************
6012 * *
6013 * Reading RelaxNGs *
6014 * *
6015 ************************************************************************/
6016
6017/**
6018 * xmlRelaxNGNewParserCtxt:
6019 * @URL: the location of the schema
6020 *
6021 * Create an XML RelaxNGs parse context for that file/resource expected
6022 * to contain an XML RelaxNGs file.
6023 *
6024 * Returns the parser context or NULL in case of error
6025 */
6026xmlRelaxNGParserCtxtPtr
6027xmlRelaxNGNewParserCtxt(const char *URL) {
6028 xmlRelaxNGParserCtxtPtr ret;
6029
6030 if (URL == NULL)
6031 return(NULL);
6032
6033 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6034 if (ret == NULL) {
6035 xmlGenericError(xmlGenericErrorContext,
6036 "Failed to allocate new schama parser context for %s\n", URL);
6037 return (NULL);
6038 }
6039 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6040 ret->URL = xmlStrdup((const xmlChar *)URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006041 ret->error = xmlGenericError;
6042 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006043 return (ret);
6044}
6045
6046/**
6047 * xmlRelaxNGNewMemParserCtxt:
6048 * @buffer: a pointer to a char array containing the schemas
6049 * @size: the size of the array
6050 *
6051 * Create an XML RelaxNGs parse context for that memory buffer expected
6052 * to contain an XML RelaxNGs file.
6053 *
6054 * Returns the parser context or NULL in case of error
6055 */
6056xmlRelaxNGParserCtxtPtr
6057xmlRelaxNGNewMemParserCtxt(const char *buffer, int size) {
6058 xmlRelaxNGParserCtxtPtr ret;
6059
6060 if ((buffer == NULL) || (size <= 0))
6061 return(NULL);
6062
6063 ret = (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6064 if (ret == NULL) {
6065 xmlGenericError(xmlGenericErrorContext,
6066 "Failed to allocate new schama parser context\n");
6067 return (NULL);
6068 }
6069 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6070 ret->buffer = buffer;
6071 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006072 ret->error = xmlGenericError;
6073 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006074 return (ret);
6075}
6076
6077/**
6078 * xmlRelaxNGFreeParserCtxt:
6079 * @ctxt: the schema parser context
6080 *
6081 * Free the resources associated to the schema parser context
6082 */
6083void
6084xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt) {
6085 if (ctxt == NULL)
6086 return;
6087 if (ctxt->URL != NULL)
6088 xmlFree(ctxt->URL);
6089 if (ctxt->doc != NULL)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006090 xmlFreeDoc(ctxt->document);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006091 if (ctxt->interleaves != NULL)
6092 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006093 if (ctxt->documents != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006094 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006095 if (ctxt->includes != NULL)
Daniel Veillardc482e262003-02-26 14:48:48 +00006096 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006097 if (ctxt->docTab != NULL)
6098 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006099 if (ctxt->incTab != NULL)
6100 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006101 if (ctxt->defTab != NULL) {
6102 int i;
6103
6104 for (i = 0;i < ctxt->defNr;i++)
6105 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6106 xmlFree(ctxt->defTab);
6107 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006108 xmlFree(ctxt);
6109}
6110
Daniel Veillard6eadf632003-01-23 18:29:16 +00006111/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006112 * xmlRelaxNGNormExtSpace:
6113 * @value: a value
6114 *
6115 * Removes the leading and ending spaces of the value
6116 * The string is modified "in situ"
6117 */
6118static void
6119xmlRelaxNGNormExtSpace(xmlChar *value) {
6120 xmlChar *start = value;
6121 xmlChar *cur = value;
6122 if (value == NULL)
6123 return;
6124
6125 while (IS_BLANK(*cur)) cur++;
6126 if (cur == start) {
6127 do {
6128 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
6129 if (*cur == 0)
6130 return;
6131 start = cur;
6132 while (IS_BLANK(*cur)) cur++;
6133 if (*cur == 0) {
6134 *start = 0;
6135 return;
6136 }
6137 } while (1);
6138 } else {
6139 do {
6140 while ((*cur != 0) && (!IS_BLANK(*cur)))
6141 *start++ = *cur++;
6142 if (*cur == 0) {
6143 *start = 0;
6144 return;
6145 }
6146 /* don't try to normalize the inner spaces */
6147 while (IS_BLANK(*cur)) cur++;
6148 *start++ = *cur++;
6149 if (*cur == 0) {
6150 *start = 0;
6151 return;
6152 }
6153 } while (1);
6154 }
6155}
6156
6157/**
6158 * xmlRelaxNGCheckAttributes:
6159 * @ctxt: a Relax-NG parser context
6160 * @node: a Relax-NG node
6161 *
6162 * Check all the attributes on the given node
6163 */
6164static void
6165xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node) {
6166 xmlAttrPtr cur, next;
6167
6168 cur = node->properties;
6169 while (cur != NULL) {
6170 next = cur->next;
6171 if ((cur->ns == NULL) ||
6172 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6173 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6174 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6175 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6176 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6177 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
Daniel Veillard2df2de22003-02-17 23:34:33 +00006178 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
Daniel Veillardd2298792003-02-14 16:54:11 +00006179 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6180 if (ctxt->error != NULL)
6181 ctxt->error(ctxt->userData,
6182 "Attribute %s is not allowed on %s\n",
6183 cur->name, node->name);
6184 ctxt->nbErrors++;
6185 }
6186 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6187 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6188 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6189 if (ctxt->error != NULL)
6190 ctxt->error(ctxt->userData,
6191 "Attribute %s is not allowed on %s\n",
6192 cur->name, node->name);
6193 ctxt->nbErrors++;
6194 }
6195 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6196 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6197 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6198 if (ctxt->error != NULL)
6199 ctxt->error(ctxt->userData,
6200 "Attribute %s is not allowed on %s\n",
6201 cur->name, node->name);
6202 ctxt->nbErrors++;
6203 }
6204 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6205 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6206 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6207 if (ctxt->error != NULL)
6208 ctxt->error(ctxt->userData,
6209 "Attribute %s is not allowed on %s\n",
6210 cur->name, node->name);
6211 ctxt->nbErrors++;
6212 }
6213 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6214 xmlChar *val;
6215 xmlURIPtr uri;
6216
6217 val = xmlNodeListGetString(node->doc, cur->children, 1);
6218 if (val != NULL) {
6219 if (val[0] != 0) {
6220 uri = xmlParseURI((const char *) val);
6221 if (uri == NULL) {
6222 if (ctxt->error != NULL)
6223 ctxt->error(ctxt->userData,
6224 "Attribute %s contains invalid URI %s\n",
6225 cur->name, val);
6226 ctxt->nbErrors++;
6227 } else {
6228 if (uri->scheme == NULL) {
6229 if (ctxt->error != NULL)
6230 ctxt->error(ctxt->userData,
6231 "Attribute %s URI %s is not absolute\n",
6232 cur->name, val);
6233 ctxt->nbErrors++;
6234 }
6235 if (uri->fragment != NULL) {
6236 if (ctxt->error != NULL)
6237 ctxt->error(ctxt->userData,
6238 "Attribute %s URI %s has a fragment ID\n",
6239 cur->name, val);
6240 ctxt->nbErrors++;
6241 }
6242 xmlFreeURI(uri);
6243 }
6244 }
6245 xmlFree(val);
6246 }
6247 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6248 if (ctxt->error != NULL)
6249 ctxt->error(ctxt->userData,
6250 "Unknown attribute %s on %s\n",
6251 cur->name, node->name);
6252 ctxt->nbErrors++;
6253 }
6254 }
6255 cur = next;
6256 }
6257}
6258
6259/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006260 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006261 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006262 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00006263 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00006264 * Cleanup the subtree from unwanted nodes for parsing, resolve
6265 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006266 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00006267static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00006268xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root) {
Daniel Veillardc5312d72003-02-21 17:14:10 +00006269 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006270
Daniel Veillard6eadf632003-01-23 18:29:16 +00006271 delete = NULL;
6272 cur = root;
6273 while (cur != NULL) {
6274 if (delete != NULL) {
6275 xmlUnlinkNode(delete);
6276 xmlFreeNode(delete);
6277 delete = NULL;
6278 }
6279 if (cur->type == XML_ELEMENT_NODE) {
6280 /*
6281 * Simplification 4.1. Annotations
6282 */
6283 if ((cur->ns == NULL) ||
6284 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
Daniel Veillardd2298792003-02-14 16:54:11 +00006285 if ((cur->parent != NULL) &&
6286 (cur->parent->type == XML_ELEMENT_NODE) &&
6287 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6288 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6289 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6290 if (ctxt->error != NULL)
6291 ctxt->error(ctxt->userData,
6292 "element %s doesn't allow foreign elements\n",
6293 cur->parent->name);
6294 ctxt->nbErrors++;
6295 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006296 delete = cur;
6297 goto skip_children;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006298 } else {
6299 xmlRelaxNGCleanupAttributes(ctxt, cur);
6300 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6301 xmlChar *href, *ns, *base, *URL;
6302 xmlRelaxNGDocumentPtr docu;
6303 xmlNodePtr tmp;
6304
6305 ns = xmlGetProp(cur, BAD_CAST "ns");
6306 if (ns == NULL) {
6307 tmp = cur->parent;
6308 while ((tmp != NULL) &&
6309 (tmp->type == XML_ELEMENT_NODE)) {
6310 ns = xmlGetProp(tmp, BAD_CAST "ns");
6311 if (ns != NULL)
6312 break;
6313 tmp = tmp->parent;
6314 }
6315 }
6316 href = xmlGetProp(cur, BAD_CAST "href");
6317 if (href == NULL) {
6318 if (ctxt->error != NULL)
6319 ctxt->error(ctxt->userData,
6320 "xmlRelaxNGParse: externalRef has no href attribute\n");
6321 ctxt->nbErrors++;
6322 delete = cur;
6323 goto skip_children;
6324 }
6325 base = xmlNodeGetBase(cur->doc, cur);
6326 URL = xmlBuildURI(href, base);
6327 if (URL == NULL) {
6328 if (ctxt->error != NULL)
6329 ctxt->error(ctxt->userData,
6330 "Failed to compute URL for externalRef %s\n", href);
6331 ctxt->nbErrors++;
6332 if (href != NULL)
6333 xmlFree(href);
6334 if (base != NULL)
6335 xmlFree(base);
6336 delete = cur;
6337 goto skip_children;
6338 }
6339 if (href != NULL)
6340 xmlFree(href);
6341 if (base != NULL)
6342 xmlFree(base);
6343 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6344 if (docu == NULL) {
6345 if (ctxt->error != NULL)
6346 ctxt->error(ctxt->userData,
6347 "Failed to load externalRef %s\n", URL);
6348 ctxt->nbErrors++;
6349 xmlFree(URL);
6350 delete = cur;
6351 goto skip_children;
6352 }
6353 if (ns != NULL)
6354 xmlFree(ns);
6355 xmlFree(URL);
6356 cur->_private = docu;
6357 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6358 xmlChar *href, *ns, *base, *URL;
6359 xmlRelaxNGIncludePtr incl;
6360 xmlNodePtr tmp;
6361
6362 href = xmlGetProp(cur, BAD_CAST "href");
6363 if (href == NULL) {
6364 if (ctxt->error != NULL)
6365 ctxt->error(ctxt->userData,
6366 "xmlRelaxNGParse: include has no href attribute\n");
6367 ctxt->nbErrors++;
6368 delete = cur;
6369 goto skip_children;
6370 }
6371 base = xmlNodeGetBase(cur->doc, cur);
6372 URL = xmlBuildURI(href, base);
6373 if (URL == NULL) {
6374 if (ctxt->error != NULL)
6375 ctxt->error(ctxt->userData,
6376 "Failed to compute URL for include %s\n", href);
6377 ctxt->nbErrors++;
6378 if (href != NULL)
6379 xmlFree(href);
6380 if (base != NULL)
6381 xmlFree(base);
6382 delete = cur;
6383 goto skip_children;
6384 }
6385 if (href != NULL)
6386 xmlFree(href);
6387 if (base != NULL)
6388 xmlFree(base);
6389 ns = xmlGetProp(cur, BAD_CAST "ns");
6390 if (ns == NULL) {
6391 tmp = cur->parent;
6392 while ((tmp != NULL) &&
6393 (tmp->type == XML_ELEMENT_NODE)) {
6394 ns = xmlGetProp(tmp, BAD_CAST "ns");
6395 if (ns != NULL)
6396 break;
6397 tmp = tmp->parent;
6398 }
6399 }
6400 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
6401 if (ns != NULL)
6402 xmlFree(ns);
6403 if (incl == NULL) {
6404 if (ctxt->error != NULL)
6405 ctxt->error(ctxt->userData,
6406 "Failed to load include %s\n", URL);
6407 ctxt->nbErrors++;
6408 xmlFree(URL);
6409 delete = cur;
6410 goto skip_children;
6411 }
6412 xmlFree(URL);
6413 cur->_private = incl;
6414 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
6415 (xmlStrEqual(cur->name, BAD_CAST "attribute"))) {
6416 xmlChar *name, *ns;
6417 xmlNodePtr text = NULL;
6418
6419 /*
6420 * Simplification 4.8. name attribute of element
6421 * and attribute elements
6422 */
6423 name = xmlGetProp(cur, BAD_CAST "name");
6424 if (name != NULL) {
6425 if (cur->children == NULL) {
6426 text = xmlNewChild(cur, cur->ns, BAD_CAST "name",
6427 name);
6428 } else {
6429 xmlNodePtr node;
6430 node = xmlNewNode(cur->ns, BAD_CAST "name");
6431 if (node != NULL) {
6432 xmlAddPrevSibling(cur->children, node);
6433 text = xmlNewText(name);
6434 xmlAddChild(node, text);
6435 text = node;
6436 }
6437 }
6438 if (text == NULL) {
6439 if (ctxt->error != NULL)
6440 ctxt->error(ctxt->userData,
6441 "Failed to create a name %s element\n", name);
6442 ctxt->nbErrors++;
6443 }
6444 xmlUnsetProp(cur, BAD_CAST "name");
6445 xmlFree(name);
6446 ns = xmlGetProp(cur, BAD_CAST "ns");
6447 if (ns != NULL) {
6448 if (text != NULL) {
6449 xmlSetProp(text, BAD_CAST "ns", ns);
6450 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
6451 }
6452 xmlFree(ns);
6453 } else if (xmlStrEqual(cur->name,
6454 BAD_CAST "attribute")) {
6455 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
6456 }
6457 }
6458 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
6459 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
6460 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
6461 /*
6462 * Simplification 4.8. name attribute of element
6463 * and attribute elements
6464 */
6465 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
6466 xmlNodePtr node;
6467 xmlChar *ns = NULL;
6468
6469 node = cur->parent;
6470 while ((node != NULL) &&
6471 (node->type == XML_ELEMENT_NODE)) {
6472 ns = xmlGetProp(node, BAD_CAST "ns");
6473 if (ns != NULL) {
6474 break;
6475 }
6476 node = node->parent;
6477 }
6478 if (ns == NULL) {
6479 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
6480 } else {
6481 xmlSetProp(cur, BAD_CAST "ns", ns);
6482 xmlFree(ns);
6483 }
6484 }
6485 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6486 xmlChar *name, *local, *prefix;
6487
6488 /*
6489 * Simplification: 4.10. QNames
6490 */
6491 name = xmlNodeGetContent(cur);
6492 if (name != NULL) {
6493 local = xmlSplitQName2(name, &prefix);
6494 if (local != NULL) {
6495 xmlNsPtr ns;
6496
6497 ns = xmlSearchNs(cur->doc, cur, prefix);
6498 if (ns == NULL) {
6499 if (ctxt->error != NULL)
6500 ctxt->error(ctxt->userData,
6501 "xmlRelaxNGParse: no namespace for prefix %s\n", prefix);
6502 ctxt->nbErrors++;
6503 } else {
6504 xmlSetProp(cur, BAD_CAST "ns", ns->href);
6505 xmlNodeSetContent(cur, local);
6506 }
6507 xmlFree(local);
6508 xmlFree(prefix);
6509 }
6510 xmlFree(name);
6511 }
6512 }
6513 /*
6514 * 4.16
6515 */
6516 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
6517 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
6518 if (ctxt->error != NULL)
6519 ctxt->error(ctxt->userData,
6520 "Found nsName/except//nsName forbidden construct\n");
6521 ctxt->nbErrors++;
6522 }
6523 }
6524 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
6525 (cur != root)) {
6526 int oldflags = ctxt->flags;
6527
6528 /*
6529 * 4.16
6530 */
6531 if ((cur->parent != NULL) &&
6532 (xmlStrEqual(cur->parent->name, BAD_CAST "anyName"))) {
6533 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
6534 xmlRelaxNGCleanupTree(ctxt, cur);
6535 ctxt->flags = oldflags;
6536 goto skip_children;
6537 } else if ((cur->parent != NULL) &&
6538 (xmlStrEqual(cur->parent->name, BAD_CAST "nsName"))) {
6539 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
6540 xmlRelaxNGCleanupTree(ctxt, cur);
6541 ctxt->flags = oldflags;
6542 goto skip_children;
6543 }
6544 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
6545 /*
6546 * 4.16
6547 */
6548 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
6549 if (ctxt->error != NULL)
6550 ctxt->error(ctxt->userData,
6551 "Found anyName/except//anyName forbidden construct\n");
6552 ctxt->nbErrors++;
6553 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
6554 if (ctxt->error != NULL)
6555 ctxt->error(ctxt->userData,
6556 "Found nsName/except//anyName forbidden construct\n");
6557 ctxt->nbErrors++;
6558 }
6559 }
6560 /*
6561 * Thisd is not an else since "include" is transformed
6562 * into a div
6563 */
6564 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
6565 xmlChar *ns;
6566 xmlNodePtr child, ins, tmp;
6567
6568 /*
6569 * implements rule 4.11
6570 */
6571
6572 ns = xmlGetProp(cur, BAD_CAST "ns");
6573
6574 child = cur->children;
6575 ins = cur;
6576 while (child != NULL) {
6577 if (ns != NULL) {
6578 if (!xmlHasProp(child, BAD_CAST "ns")) {
6579 xmlSetProp(child, BAD_CAST "ns", ns);
6580 }
6581 }
6582 tmp = child->next;
6583 xmlUnlinkNode(child);
6584 ins = xmlAddNextSibling(ins, child);
6585 child = tmp;
6586 }
6587 if (ns != NULL)
6588 xmlFree(ns);
6589 delete = cur;
6590 goto skip_children;
6591 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006592 }
6593 }
6594 /*
6595 * Simplification 4.2 whitespaces
6596 */
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006597 else if ((cur->type == XML_TEXT_NODE) ||
6598 (cur->type == XML_CDATA_SECTION_NODE)) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00006599 if (IS_BLANK_NODE(cur)) {
6600 if (cur->parent->type == XML_ELEMENT_NODE) {
6601 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value")) &&
6602 (!xmlStrEqual(cur->parent->name, BAD_CAST "param")))
6603 delete = cur;
6604 } else {
6605 delete = cur;
6606 goto skip_children;
6607 }
6608 }
Daniel Veillard39eb88b2003-03-11 11:21:28 +00006609 } else {
Daniel Veillard6eadf632003-01-23 18:29:16 +00006610 delete = cur;
6611 goto skip_children;
6612 }
6613
6614 /*
6615 * Skip to next node
6616 */
6617 if (cur->children != NULL) {
6618 if ((cur->children->type != XML_ENTITY_DECL) &&
6619 (cur->children->type != XML_ENTITY_REF_NODE) &&
6620 (cur->children->type != XML_ENTITY_NODE)) {
6621 cur = cur->children;
6622 continue;
6623 }
6624 }
6625skip_children:
6626 if (cur->next != NULL) {
6627 cur = cur->next;
6628 continue;
6629 }
6630
6631 do {
6632 cur = cur->parent;
6633 if (cur == NULL)
6634 break;
6635 if (cur == root) {
6636 cur = NULL;
6637 break;
6638 }
6639 if (cur->next != NULL) {
6640 cur = cur->next;
6641 break;
6642 }
6643 } while (cur != NULL);
6644 }
6645 if (delete != NULL) {
6646 xmlUnlinkNode(delete);
6647 xmlFreeNode(delete);
6648 delete = NULL;
6649 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00006650}
Daniel Veillard6eadf632003-01-23 18:29:16 +00006651
Daniel Veillardc5312d72003-02-21 17:14:10 +00006652/**
6653 * xmlRelaxNGCleanupDoc:
6654 * @ctxt: a Relax-NG parser context
6655 * @doc: an xmldocPtr document pointer
6656 *
6657 * Cleanup the document from unwanted nodes for parsing, resolve
6658 * Include and externalRef lookups.
6659 *
6660 * Returns the cleaned up document or NULL in case of error
6661 */
6662static xmlDocPtr
6663xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc) {
6664 xmlNodePtr root;
6665
6666 /*
6667 * Extract the root
6668 */
6669 root = xmlDocGetRootElement(doc);
6670 if (root == NULL) {
6671 if (ctxt->error != NULL)
6672 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
6673 ctxt->URL);
6674 ctxt->nbErrors++;
6675 return (NULL);
6676 }
6677 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006678 return(doc);
6679}
6680
6681/**
6682 * xmlRelaxNGParse:
6683 * @ctxt: a Relax-NG parser context
6684 *
6685 * parse a schema definition resource and build an internal
6686 * XML Shema struture which can be used to validate instances.
6687 * *WARNING* this interface is highly subject to change
6688 *
6689 * Returns the internal XML RelaxNG structure built from the resource or
6690 * NULL in case of error
6691 */
6692xmlRelaxNGPtr
6693xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
6694{
6695 xmlRelaxNGPtr ret = NULL;
6696 xmlDocPtr doc;
6697 xmlNodePtr root;
6698
6699 xmlRelaxNGInitTypes();
6700
6701 if (ctxt == NULL)
6702 return (NULL);
6703
6704 /*
6705 * First step is to parse the input document into an DOM/Infoset
6706 */
6707 if (ctxt->URL != NULL) {
6708 doc = xmlParseFile((const char *) ctxt->URL);
6709 if (doc == NULL) {
6710 if (ctxt->error != NULL)
6711 ctxt->error(ctxt->userData,
6712 "xmlRelaxNGParse: could not load %s\n", ctxt->URL);
6713 ctxt->nbErrors++;
6714 return (NULL);
6715 }
6716 } else if (ctxt->buffer != NULL) {
6717 doc = xmlParseMemory(ctxt->buffer, ctxt->size);
6718 if (doc == NULL) {
6719 if (ctxt->error != NULL)
6720 ctxt->error(ctxt->userData,
6721 "xmlRelaxNGParse: could not parse schemas\n");
6722 ctxt->nbErrors++;
6723 return (NULL);
6724 }
6725 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
6726 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
6727 } else {
6728 if (ctxt->error != NULL)
6729 ctxt->error(ctxt->userData,
6730 "xmlRelaxNGParse: nothing to parse\n");
6731 ctxt->nbErrors++;
6732 return (NULL);
6733 }
6734 ctxt->document = doc;
6735
6736 /*
6737 * Some preprocessing of the document content
6738 */
6739 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
6740 if (doc == NULL) {
6741 xmlFreeDoc(ctxt->document);
6742 ctxt->document = NULL;
6743 return(NULL);
6744 }
6745
Daniel Veillard6eadf632003-01-23 18:29:16 +00006746 /*
6747 * Then do the parsing for good
6748 */
6749 root = xmlDocGetRootElement(doc);
6750 if (root == NULL) {
6751 if (ctxt->error != NULL)
6752 ctxt->error(ctxt->userData, "xmlRelaxNGParse: %s is empty\n",
6753 ctxt->URL);
6754 ctxt->nbErrors++;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006755 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006756 return (NULL);
6757 }
6758 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006759 if (ret == NULL) {
6760 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006761 return(NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006762 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006763
6764 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00006765 * Check the ref/defines links
6766 */
6767 /*
6768 * try to preprocess interleaves
6769 */
6770 if (ctxt->interleaves != NULL) {
6771 xmlHashScan(ctxt->interleaves,
6772 (xmlHashScanner)xmlRelaxNGComputeInterleaves, ctxt);
6773 }
6774
6775 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00006776 * if there was a parsing error return NULL
6777 */
6778 if (ctxt->nbErrors > 0) {
6779 xmlRelaxNGFree(ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006780 ctxt->document = NULL;
6781 xmlFreeDoc(doc);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006782 return(NULL);
6783 }
6784
6785 /*
6786 * Transfer the pointer for cleanup at the schema level.
6787 */
6788 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006789 ctxt->document = NULL;
6790 ret->documents = ctxt->documents;
6791 ctxt->documents = NULL;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006792
Daniel Veillarde2a5a082003-02-02 14:35:17 +00006793 ret->includes = ctxt->includes;
6794 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00006795 ret->defNr = ctxt->defNr;
6796 ret->defTab = ctxt->defTab;
6797 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00006798 if (ctxt->idref == 1)
6799 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006800
6801 return (ret);
6802}
6803
6804/**
6805 * xmlRelaxNGSetParserErrors:
6806 * @ctxt: a Relax-NG validation context
6807 * @err: the error callback
6808 * @warn: the warning callback
6809 * @ctx: contextual data for the callbacks
6810 *
6811 * Set the callback functions used to handle errors for a validation context
6812 */
6813void
6814xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
6815 xmlRelaxNGValidityErrorFunc err,
6816 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
6817 if (ctxt == NULL)
6818 return;
6819 ctxt->error = err;
6820 ctxt->warning = warn;
6821 ctxt->userData = ctx;
6822}
6823/************************************************************************
6824 * *
6825 * Dump back a compiled form *
6826 * *
6827 ************************************************************************/
6828static void xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define);
6829
6830/**
6831 * xmlRelaxNGDumpDefines:
6832 * @output: the file output
6833 * @defines: a list of define structures
6834 *
6835 * Dump a RelaxNG structure back
6836 */
6837static void
6838xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines) {
6839 while (defines != NULL) {
6840 xmlRelaxNGDumpDefine(output, defines);
6841 defines = defines->next;
6842 }
6843}
6844
6845/**
6846 * xmlRelaxNGDumpDefine:
6847 * @output: the file output
6848 * @define: a define structure
6849 *
6850 * Dump a RelaxNG structure back
6851 */
6852static void
6853xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define) {
6854 if (define == NULL)
6855 return;
6856 switch(define->type) {
6857 case XML_RELAXNG_EMPTY:
6858 fprintf(output, "<empty/>\n");
6859 break;
6860 case XML_RELAXNG_NOT_ALLOWED:
6861 fprintf(output, "<notAllowed/>\n");
6862 break;
6863 case XML_RELAXNG_TEXT:
6864 fprintf(output, "<text/>\n");
6865 break;
6866 case XML_RELAXNG_ELEMENT:
6867 fprintf(output, "<element>\n");
6868 if (define->name != NULL) {
6869 fprintf(output, "<name");
6870 if (define->ns != NULL)
6871 fprintf(output, " ns=\"%s\"", define->ns);
6872 fprintf(output, ">%s</name>\n", define->name);
6873 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006874 xmlRelaxNGDumpDefines(output, define->attrs);
6875 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006876 fprintf(output, "</element>\n");
6877 break;
6878 case XML_RELAXNG_LIST:
6879 fprintf(output, "<list>\n");
6880 xmlRelaxNGDumpDefines(output, define->content);
6881 fprintf(output, "</list>\n");
6882 break;
6883 case XML_RELAXNG_ONEORMORE:
6884 fprintf(output, "<oneOrMore>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006885 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006886 fprintf(output, "</oneOrMore>\n");
6887 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006888 case XML_RELAXNG_ZEROORMORE:
6889 fprintf(output, "<zeroOrMore>\n");
6890 xmlRelaxNGDumpDefines(output, define->content);
6891 fprintf(output, "</zeroOrMore>\n");
6892 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006893 case XML_RELAXNG_CHOICE:
6894 fprintf(output, "<choice>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006895 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006896 fprintf(output, "</choice>\n");
6897 break;
6898 case XML_RELAXNG_GROUP:
6899 fprintf(output, "<group>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006900 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006901 fprintf(output, "</group>\n");
6902 break;
6903 case XML_RELAXNG_INTERLEAVE:
6904 fprintf(output, "<interleave>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006905 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006906 fprintf(output, "</interleave>\n");
6907 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006908 case XML_RELAXNG_OPTIONAL:
6909 fprintf(output, "<optional>\n");
6910 xmlRelaxNGDumpDefines(output, define->content);
6911 fprintf(output, "</optional>\n");
6912 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006913 case XML_RELAXNG_ATTRIBUTE:
6914 fprintf(output, "<attribute>\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006915 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006916 fprintf(output, "</attribute>\n");
6917 break;
6918 case XML_RELAXNG_DEF:
6919 fprintf(output, "<define");
6920 if (define->name != NULL)
6921 fprintf(output, " name=\"%s\"", define->name);
6922 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006923 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006924 fprintf(output, "</define>\n");
6925 break;
6926 case XML_RELAXNG_REF:
6927 fprintf(output, "<ref");
6928 if (define->name != NULL)
6929 fprintf(output, " name=\"%s\"", define->name);
6930 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006931 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006932 fprintf(output, "</ref>\n");
6933 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00006934 case XML_RELAXNG_PARENTREF:
6935 fprintf(output, "<parentRef");
6936 if (define->name != NULL)
6937 fprintf(output, " name=\"%s\"", define->name);
6938 fprintf(output, ">\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006939 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard419a7682003-02-03 23:22:49 +00006940 fprintf(output, "</parentRef>\n");
6941 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006942 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard416589a2003-02-17 17:25:42 +00006943 fprintf(output, "<externalRef>");
Daniel Veillardfd573f12003-03-16 17:52:32 +00006944 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillarde431a272003-01-29 23:02:33 +00006945 fprintf(output, "</externalRef>\n");
6946 break;
6947 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00006948 case XML_RELAXNG_VALUE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00006949 TODO
Daniel Veillard6eadf632003-01-23 18:29:16 +00006950 break;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006951 case XML_RELAXNG_START:
Daniel Veillardfd573f12003-03-16 17:52:32 +00006952 case XML_RELAXNG_EXCEPT:
Daniel Veillard8fe98712003-02-19 00:19:14 +00006953 case XML_RELAXNG_PARAM:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006954 TODO
6955 break;
Daniel Veillard77648bb2003-02-20 15:03:22 +00006956 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +00006957 xmlRelaxNGDumpDefines(output, define->content);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006958 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006959 }
6960}
6961
6962/**
6963 * xmlRelaxNGDumpGrammar:
6964 * @output: the file output
6965 * @grammar: a grammar structure
6966 * @top: is this a top grammar
6967 *
6968 * Dump a RelaxNG structure back
6969 */
6970static void
6971xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
6972{
6973 if (grammar == NULL)
6974 return;
6975
6976 fprintf(output, "<grammar");
6977 if (top)
6978 fprintf(output,
6979 " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
6980 switch(grammar->combine) {
6981 case XML_RELAXNG_COMBINE_UNDEFINED:
6982 break;
6983 case XML_RELAXNG_COMBINE_CHOICE:
6984 fprintf(output, " combine=\"choice\"");
6985 break;
6986 case XML_RELAXNG_COMBINE_INTERLEAVE:
6987 fprintf(output, " combine=\"interleave\"");
6988 break;
6989 default:
6990 fprintf(output, " <!-- invalid combine value -->");
6991 }
6992 fprintf(output, ">\n");
6993 if (grammar->start == NULL) {
6994 fprintf(output, " <!-- grammar had no start -->");
6995 } else {
6996 fprintf(output, "<start>\n");
6997 xmlRelaxNGDumpDefine(output, grammar->start);
6998 fprintf(output, "</start>\n");
6999 }
7000 /* TODO ? Dump the defines ? */
7001 fprintf(output, "</grammar>\n");
7002}
7003
7004/**
7005 * xmlRelaxNGDump:
7006 * @output: the file output
7007 * @schema: a schema structure
7008 *
7009 * Dump a RelaxNG structure back
7010 */
7011void
7012xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7013{
7014 if (schema == NULL) {
7015 fprintf(output, "RelaxNG empty or failed to compile\n");
7016 return;
7017 }
7018 fprintf(output, "RelaxNG: ");
7019 if (schema->doc == NULL) {
7020 fprintf(output, "no document\n");
7021 } else if (schema->doc->URL != NULL) {
7022 fprintf(output, "%s\n", schema->doc->URL);
7023 } else {
7024 fprintf(output, "\n");
7025 }
7026 if (schema->topgrammar == NULL) {
7027 fprintf(output, "RelaxNG has no top grammar\n");
7028 return;
7029 }
7030 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7031}
7032
Daniel Veillardfebcca42003-02-16 15:44:18 +00007033/**
7034 * xmlRelaxNGDumpTree:
7035 * @output: the file output
7036 * @schema: a schema structure
7037 *
7038 * Dump the transformed RelaxNG tree.
7039 */
7040void
7041xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7042{
7043 if (schema == NULL) {
7044 fprintf(output, "RelaxNG empty or failed to compile\n");
7045 return;
7046 }
7047 if (schema->doc == NULL) {
7048 fprintf(output, "no document\n");
7049 } else {
7050 xmlDocDump(output, schema->doc);
7051 }
7052}
7053
Daniel Veillard6eadf632003-01-23 18:29:16 +00007054/************************************************************************
7055 * *
7056 * Validation implementation *
7057 * *
7058 ************************************************************************/
Daniel Veillardfd573f12003-03-16 17:52:32 +00007059static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7060 xmlRelaxNGDefinePtr define);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007061static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
7062 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007063
7064/**
7065 * xmlRelaxNGSkipIgnored:
7066 * @ctxt: a schema validation context
7067 * @node: the top node.
7068 *
7069 * Skip ignorable nodes in that context
7070 *
7071 * Returns the new sibling or NULL in case of error.
7072 */
7073static xmlNodePtr
7074xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
7075 xmlNodePtr node) {
7076 /*
7077 * TODO complete and handle entities
7078 */
7079 while ((node != NULL) &&
7080 ((node->type == XML_COMMENT_NODE) ||
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007081 (node->type == XML_PI_NODE) ||
Daniel Veillard39eb88b2003-03-11 11:21:28 +00007082 (((node->type == XML_TEXT_NODE) ||
7083 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007084 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
7085 (IS_BLANK_NODE(node)))))) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007086 node = node->next;
7087 }
7088 return(node);
7089}
7090
7091/**
Daniel Veillardedc91922003-01-26 00:52:04 +00007092 * xmlRelaxNGNormalize:
7093 * @ctxt: a schema validation context
7094 * @str: the string to normalize
7095 *
7096 * Implements the normalizeWhiteSpace( s ) function from
7097 * section 6.2.9 of the spec
7098 *
7099 * Returns the new string or NULL in case of error.
7100 */
7101static xmlChar *
7102xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *str) {
7103 xmlChar *ret, *p;
7104 const xmlChar *tmp;
7105 int len;
7106
7107 if (str == NULL)
7108 return(NULL);
7109 tmp = str;
7110 while (*tmp != 0) tmp++;
7111 len = tmp - str;
7112
7113 ret = (xmlChar *) xmlMalloc((len + 1) * sizeof(xmlChar));
7114 if (ret == NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00007115 if (ctxt != NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007116 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
Daniel Veillardea3f3982003-01-26 19:45:18 +00007117 } else {
7118 xmlGenericError(xmlGenericErrorContext,
7119 "xmlRelaxNGNormalize: out of memory\n");
7120 }
Daniel Veillardedc91922003-01-26 00:52:04 +00007121 return(NULL);
7122 }
7123 p = ret;
7124 while (IS_BLANK(*str)) str++;
7125 while (*str != 0) {
7126 if (IS_BLANK(*str)) {
7127 while (IS_BLANK(*str)) str++;
7128 if (*str == 0)
7129 break;
7130 *p++ = ' ';
7131 } else
7132 *p++ = *str++;
7133 }
7134 *p = 0;
7135 return(ret);
7136}
7137
7138/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007139 * xmlRelaxNGValidateDatatype:
7140 * @ctxt: a Relax-NG validation context
7141 * @value: the string value
7142 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007143 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007144 *
7145 * Validate the given value against the dataype
7146 *
7147 * Returns 0 if the validation succeeded or an error code.
7148 */
7149static int
7150xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar *value,
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007151 xmlRelaxNGDefinePtr define, xmlNodePtr node) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007152 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007153 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007154 void *result = NULL;
7155 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007156
7157 if ((define == NULL) || (define->data == NULL)) {
7158 return(-1);
7159 }
7160 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007161 if (lib->check != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007162 if ((define->attrs != NULL) &&
7163 (define->attrs->type == XML_RELAXNG_PARAM)) {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007164 ret = lib->check(lib->data, define->name, value, &result, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007165 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007166 ret = lib->check(lib->data, define->name, value, NULL, node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007167 }
7168 } else
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007169 ret = -1;
7170 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007171 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007172 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
7173 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007174 return(-1);
7175 } else if (ret == 1) {
7176 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007177 } else if (ret == 2) {
7178 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007179 } else {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007180 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007181 ret = -1;
7182 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007183 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007184 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
7185 if (lib->facet != NULL) {
7186 tmp = lib->facet(lib->data, define->name, cur->name,
7187 cur->value, value, result);
7188 if (tmp != 0)
7189 ret = -1;
7190 }
7191 cur = cur->next;
7192 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007193 if ((ret == 0) && (define->content != NULL)) {
7194 const xmlChar *oldvalue, *oldendvalue;
7195
7196 oldvalue = ctxt->state->value;
7197 oldendvalue = ctxt->state->endvalue;
7198 ctxt->state->value = (xmlChar *) value;
7199 ctxt->state->endvalue = NULL;
7200 ret = xmlRelaxNGValidateValue(ctxt, define->content);
7201 ctxt->state->value = (xmlChar *) oldvalue;
7202 ctxt->state->endvalue = (xmlChar *) oldendvalue;
7203 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00007204 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
7205 lib->freef(lib->data, result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00007206 return(ret);
7207}
7208
7209/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007210 * xmlRelaxNGNextValue:
7211 * @ctxt: a Relax-NG validation context
7212 *
7213 * Skip to the next value when validating within a list
7214 *
7215 * Returns 0 if the operation succeeded or an error code.
7216 */
7217static int
7218xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt) {
7219 xmlChar *cur;
7220
7221 cur = ctxt->state->value;
7222 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
7223 ctxt->state->value = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +00007224 ctxt->state->endvalue = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007225 return(0);
7226 }
7227 while (*cur != 0) cur++;
7228 while ((cur != ctxt->state->endvalue) && (*cur == 0)) cur++;
7229 if (cur == ctxt->state->endvalue)
7230 ctxt->state->value = NULL;
7231 else
7232 ctxt->state->value = cur;
7233 return(0);
7234}
7235
7236/**
7237 * xmlRelaxNGValidateValueList:
7238 * @ctxt: a Relax-NG validation context
7239 * @defines: the list of definitions to verify
7240 *
7241 * Validate the given set of definitions for the current value
7242 *
7243 * Returns 0 if the validation succeeded or an error code.
7244 */
7245static int
7246xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
7247 xmlRelaxNGDefinePtr defines) {
7248 int ret = 0;
7249
7250 while (defines != NULL) {
7251 ret = xmlRelaxNGValidateValue(ctxt, defines);
7252 if (ret != 0)
7253 break;
7254 defines = defines->next;
7255 }
7256 return(ret);
7257}
7258
7259/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00007260 * xmlRelaxNGValidateValue:
7261 * @ctxt: a Relax-NG validation context
7262 * @define: the definition to verify
7263 *
7264 * Validate the given definition for the current value
7265 *
7266 * Returns 0 if the validation succeeded or an error code.
7267 */
7268static int
7269xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
7270 xmlRelaxNGDefinePtr define) {
Daniel Veillardedc91922003-01-26 00:52:04 +00007271 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007272 xmlChar *value;
7273
7274 value = ctxt->state->value;
7275 switch (define->type) {
Daniel Veillardd4310742003-02-18 21:12:46 +00007276 case XML_RELAXNG_EMPTY: {
7277 if ((value != NULL) && (value[0] != 0)) {
7278 int idx = 0;
7279
7280 while (IS_BLANK(value[idx]))
7281 idx++;
7282 if (value[idx] != 0)
7283 ret = -1;
7284 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007285 break;
Daniel Veillardd4310742003-02-18 21:12:46 +00007286 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007287 case XML_RELAXNG_TEXT:
7288 break;
Daniel Veillardedc91922003-01-26 00:52:04 +00007289 case XML_RELAXNG_VALUE: {
7290 if (!xmlStrEqual(value, define->value)) {
7291 if (define->name != NULL) {
Daniel Veillardea3f3982003-01-26 19:45:18 +00007292 xmlRelaxNGTypeLibraryPtr lib;
7293
7294 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00007295 if ((lib != NULL) && (lib->comp != NULL)) {
7296 ret = lib->comp(lib->data, define->name,
7297 define->value, define->node,
7298 (void *) define->attrs,
7299 value, ctxt->state->node);
7300 } else
Daniel Veillardea3f3982003-01-26 19:45:18 +00007301 ret = -1;
7302 if (ret < 0) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007303 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP, define->name);
Daniel Veillardea3f3982003-01-26 19:45:18 +00007304 return(-1);
7305 } else if (ret == 1) {
7306 ret = 0;
7307 } else {
7308 ret = -1;
7309 }
Daniel Veillardedc91922003-01-26 00:52:04 +00007310 } else {
7311 xmlChar *nval, *nvalue;
7312
7313 /*
7314 * TODO: trivial optimizations are possible by
7315 * computing at compile-time
7316 */
7317 nval = xmlRelaxNGNormalize(ctxt, define->value);
7318 nvalue = xmlRelaxNGNormalize(ctxt, value);
7319
Daniel Veillardea3f3982003-01-26 19:45:18 +00007320 if ((nval == NULL) || (nvalue == NULL) ||
7321 (!xmlStrEqual(nval, nvalue)))
Daniel Veillardedc91922003-01-26 00:52:04 +00007322 ret = -1;
7323 if (nval != NULL)
7324 xmlFree(nval);
7325 if (nvalue != NULL)
7326 xmlFree(nvalue);
7327 }
7328 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007329 if (ret == 0)
7330 xmlRelaxNGNextValue(ctxt);
Daniel Veillardedc91922003-01-26 00:52:04 +00007331 break;
7332 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007333 case XML_RELAXNG_DATATYPE: {
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007334 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
7335 ctxt->state->seq);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007336 if (ret == 0)
7337 xmlRelaxNGNextValue(ctxt);
7338
7339 break;
7340 }
7341 case XML_RELAXNG_CHOICE: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007342 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007343 xmlChar *oldvalue;
7344
7345 oldflags = ctxt->flags;
7346 ctxt->flags |= FLAGS_IGNORABLE;
7347
7348 oldvalue = ctxt->state->value;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007349 while (list != NULL) {
7350 ret = xmlRelaxNGValidateValue(ctxt, list);
7351 if (ret == 0) {
7352 break;
7353 }
7354 ctxt->state->value = oldvalue;
7355 list = list->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007356 }
7357 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00007358 if (ret != 0) {
7359 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7360 xmlRelaxNGDumpValidError(ctxt);
7361 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007362 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00007363 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007364 if (ret == 0)
7365 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007366 break;
7367 }
7368 case XML_RELAXNG_LIST: {
Daniel Veillardfd573f12003-03-16 17:52:32 +00007369 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007370 xmlChar *oldvalue, *oldend, *val, *cur;
Daniel Veillard416589a2003-02-17 17:25:42 +00007371#ifdef DEBUG_LIST
7372 int nb_values = 0;
7373#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007374
7375 oldvalue = ctxt->state->value;
7376 oldend = ctxt->state->endvalue;
7377
7378 val = xmlStrdup(oldvalue);
7379 if (val == NULL) {
Daniel Veillardd4310742003-02-18 21:12:46 +00007380 val = xmlStrdup(BAD_CAST "");
7381 }
7382 if (val == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00007383 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007384 return(-1);
7385 }
7386 cur = val;
7387 while (*cur != 0) {
Daniel Veillard416589a2003-02-17 17:25:42 +00007388 if (IS_BLANK(*cur)) {
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007389 *cur = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00007390 cur++;
7391#ifdef DEBUG_LIST
7392 nb_values++;
7393#endif
7394 while (IS_BLANK(*cur))
7395 *cur++ = 0;
7396 } else
7397 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007398 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007399#ifdef DEBUG_LIST
7400 xmlGenericError(xmlGenericErrorContext,
7401 "list value: '%s' found %d items\n", oldvalue, nb_values);
7402 nb_values = 0;
7403#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007404 ctxt->state->endvalue = cur;
7405 cur = val;
7406 while ((*cur == 0) && (cur != ctxt->state->endvalue)) cur++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007407
Daniel Veillardfd573f12003-03-16 17:52:32 +00007408 ctxt->state->value = cur;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007409
Daniel Veillardfd573f12003-03-16 17:52:32 +00007410 while (list != NULL) {
7411 if (ctxt->state->value == ctxt->state->endvalue)
7412 ctxt->state->value = NULL;
7413 ret = xmlRelaxNGValidateValue(ctxt, list);
7414 if (ret != 0) {
7415#ifdef DEBUG_LIST
7416 xmlGenericError(xmlGenericErrorContext,
7417 "Failed to validate value: '%s' with %d rule\n",
7418 ctxt->state->value, nb_values);
7419#endif
7420 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00007421 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007422#ifdef DEBUG_LIST
7423 nb_values++;
7424#endif
7425 list = list->next;
7426 }
7427
7428 if ((ret == 0) && (ctxt->state->value != NULL) &&
7429 (ctxt->state->value != ctxt->state->endvalue)) {
7430 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA, ctxt->state->value);
7431 ret = -1;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007432 }
7433 xmlFree(val);
7434 ctxt->state->value = oldvalue;
7435 ctxt->state->endvalue = oldend;
7436 break;
7437 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007438 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007439 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
7440 if (ret != 0) {
7441 break;
7442 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007443 /* no break on purpose */
7444 case XML_RELAXNG_ZEROORMORE: {
7445 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007446
7447 oldflags = ctxt->flags;
7448 ctxt->flags |= FLAGS_IGNORABLE;
7449 cur = ctxt->state->value;
7450 temp = NULL;
7451 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
7452 (temp != cur)) {
7453 temp = cur;
7454 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
7455 if (ret != 0) {
7456 ctxt->state->value = temp;
7457 ret = 0;
7458 break;
7459 }
7460 cur = ctxt->state->value;
7461 }
7462 ctxt->flags = oldflags;
Daniel Veillard42f12e92003-03-07 18:32:59 +00007463 if (ret != 0) {
7464 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7465 xmlRelaxNGDumpValidError(ctxt);
7466 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007467 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard42f12e92003-03-07 18:32:59 +00007468 }
Daniel Veillardc6e997c2003-01-27 12:35:42 +00007469 break;
7470 }
Daniel Veillard416589a2003-02-17 17:25:42 +00007471 case XML_RELAXNG_EXCEPT: {
7472 xmlRelaxNGDefinePtr list;
7473
7474 list = define->content;
7475 while (list != NULL) {
7476 ret = xmlRelaxNGValidateValue(ctxt, list);
7477 if (ret == 0) {
7478 ret = -1;
7479 break;
7480 } else
7481 ret = 0;
7482 list = list->next;
7483 }
7484 break;
7485 }
Daniel Veillard463a5472003-02-27 21:30:32 +00007486 case XML_RELAXNG_DEF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00007487 case XML_RELAXNG_GROUP: {
7488 xmlRelaxNGDefinePtr list;
7489
7490 list = define->content;
7491 while (list != NULL) {
7492 ret = xmlRelaxNGValidateValue(ctxt, list);
7493 if (ret != 0) {
7494 ret = -1;
7495 break;
7496 } else
7497 ret = 0;
7498 list = list->next;
7499 }
Daniel Veillardd4310742003-02-18 21:12:46 +00007500 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007501 }
Daniel Veillard463a5472003-02-27 21:30:32 +00007502 case XML_RELAXNG_REF:
7503 case XML_RELAXNG_PARENTREF:
7504 ret = xmlRelaxNGValidateValue(ctxt, define->content);
7505 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007506 default:
7507 TODO
7508 ret = -1;
7509 }
7510 return(ret);
7511}
7512
7513/**
7514 * xmlRelaxNGValidateValueContent:
7515 * @ctxt: a Relax-NG validation context
7516 * @defines: the list of definitions to verify
7517 *
7518 * Validate the given definitions for the current value
7519 *
7520 * Returns 0 if the validation succeeded or an error code.
7521 */
7522static int
7523xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
7524 xmlRelaxNGDefinePtr defines) {
7525 int ret = 0;
7526
7527 while (defines != NULL) {
7528 ret = xmlRelaxNGValidateValue(ctxt, defines);
7529 if (ret != 0)
7530 break;
7531 defines = defines->next;
7532 }
7533 return(ret);
7534}
7535
7536/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007537 * xmlRelaxNGAttributeMatch:
7538 * @ctxt: a Relax-NG validation context
7539 * @define: the definition to check
7540 * @prop: the attribute
7541 *
7542 * Check if the attribute matches the definition nameClass
7543 *
7544 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
7545 */
7546static int
7547xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
7548 xmlRelaxNGDefinePtr define,
7549 xmlAttrPtr prop) {
7550 int ret;
7551
7552 if (define->name != NULL) {
7553 if (!xmlStrEqual(define->name, prop->name))
7554 return(0);
7555 }
7556 if (define->ns != NULL) {
7557 if (define->ns[0] == 0) {
7558 if (prop->ns != NULL)
7559 return(0);
7560 } else {
7561 if ((prop->ns == NULL) ||
7562 (!xmlStrEqual(define->ns, prop->ns->href)))
7563 return(0);
7564 }
7565 }
7566 if (define->nameClass == NULL)
7567 return(1);
7568 define = define->nameClass;
7569 if (define->type == XML_RELAXNG_EXCEPT) {
7570 xmlRelaxNGDefinePtr list;
7571
7572 list = define->content;
7573 while (list != NULL) {
7574 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
7575 if (ret == 1)
7576 return(0);
7577 if (ret < 0)
7578 return(ret);
7579 list = list->next;
7580 }
7581 } else {
7582 TODO
7583 }
7584 return(1);
7585}
7586
7587/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00007588 * xmlRelaxNGValidateAttribute:
7589 * @ctxt: a Relax-NG validation context
7590 * @define: the definition to verify
7591 *
7592 * Validate the given attribute definition for that node
7593 *
7594 * Returns 0 if the validation succeeded or an error code.
7595 */
7596static int
7597xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
7598 xmlRelaxNGDefinePtr define) {
7599 int ret = 0, i;
7600 xmlChar *value, *oldvalue;
7601 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007602 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007603
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007604 if (ctxt->state->nbAttrLeft <= 0)
7605 return(-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007606 if (define->name != NULL) {
7607 for (i = 0;i < ctxt->state->nbAttrs;i++) {
7608 tmp = ctxt->state->attrs[i];
7609 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
7610 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
7611 (tmp->ns == NULL)) ||
7612 ((tmp->ns != NULL) &&
7613 (xmlStrEqual(define->ns, tmp->ns->href)))) {
7614 prop = tmp;
7615 break;
7616 }
7617 }
7618 }
7619 if (prop != NULL) {
7620 value = xmlNodeListGetString(prop->doc, prop->children, 1);
7621 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007622 oldseq = ctxt->state->seq;
7623 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007624 ctxt->state->value = value;
Daniel Veillard231d7912003-02-09 14:22:17 +00007625 ctxt->state->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007626 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00007627 if (ctxt->state->value != NULL)
7628 value = ctxt->state->value;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007629 if (value != NULL)
7630 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00007631 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007632 ctxt->state->seq = oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007633 if (ret == 0) {
7634 /*
7635 * flag the attribute as processed
7636 */
7637 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007638 ctxt->state->nbAttrLeft--;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007639 }
7640 } else {
7641 ret = -1;
7642 }
7643#ifdef DEBUG
7644 xmlGenericError(xmlGenericErrorContext,
7645 "xmlRelaxNGValidateAttribute(%s): %d\n", define->name, ret);
7646#endif
7647 } else {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007648 for (i = 0;i < ctxt->state->nbAttrs;i++) {
7649 tmp = ctxt->state->attrs[i];
Daniel Veillard144fae12003-02-03 13:17:57 +00007650 if ((tmp != NULL) &&
Daniel Veillardfd573f12003-03-16 17:52:32 +00007651 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007652 prop = tmp;
7653 break;
7654 }
7655 }
7656 if (prop != NULL) {
7657 value = xmlNodeListGetString(prop->doc, prop->children, 1);
7658 oldvalue = ctxt->state->value;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007659 oldseq = ctxt->state->seq;
7660 ctxt->state->seq = (xmlNodePtr) prop;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007661 ctxt->state->value = value;
7662 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
Daniel Veillard231d7912003-02-09 14:22:17 +00007663 if (ctxt->state->value != NULL)
7664 value = ctxt->state->value;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007665 if (value != NULL)
7666 xmlFree(value);
Daniel Veillard231d7912003-02-09 14:22:17 +00007667 ctxt->state->value = oldvalue;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007668 ctxt->state->seq = oldseq;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007669 if (ret == 0) {
7670 /*
7671 * flag the attribute as processed
7672 */
7673 ctxt->state->attrs[i] = NULL;
Daniel Veillard1ed7f362003-02-03 10:57:45 +00007674 ctxt->state->nbAttrLeft--;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007675 }
7676 } else {
7677 ret = -1;
7678 }
7679#ifdef DEBUG
Daniel Veillard144fae12003-02-03 13:17:57 +00007680 if (define->ns != NULL) {
7681 xmlGenericError(xmlGenericErrorContext,
7682 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
7683 define->ns, ret);
7684 } else {
7685 xmlGenericError(xmlGenericErrorContext,
7686 "xmlRelaxNGValidateAttribute(anyName): %d\n",
7687 ret);
7688 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00007689#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00007690 }
7691
7692 return(ret);
7693}
7694
7695/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007696 * xmlRelaxNGValidateAttributeList:
7697 * @ctxt: a Relax-NG validation context
7698 * @define: the list of definition to verify
7699 *
7700 * Validate the given node against the list of attribute definitions
7701 *
7702 * Returns 0 if the validation succeeded or an error code.
7703 */
7704static int
7705xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7706 xmlRelaxNGDefinePtr defines) {
7707 int ret = 0;
7708 while (defines != NULL) {
7709 if (xmlRelaxNGValidateAttribute(ctxt, defines) != 0)
7710 ret = -1;
7711 defines = defines->next;
7712 }
7713 return(ret);
7714}
7715
7716/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00007717 * xmlRelaxNGNodeMatchesList:
7718 * @node: the node
7719 * @list: a NULL terminated array of definitions
7720 *
7721 * Check if a node can be matched by one of the definitions
7722 *
7723 * Returns 1 if matches 0 otherwise
7724 */
7725static int
7726xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr *list) {
7727 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00007728 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00007729
7730 if ((node == NULL) || (list == NULL))
7731 return(0);
7732
7733 cur = list[i++];
7734 while (cur != NULL) {
7735 if ((node->type == XML_ELEMENT_NODE) &&
7736 (cur->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00007737 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
7738 if (tmp == 1)
7739 return(1);
Daniel Veillard39eb88b2003-03-11 11:21:28 +00007740 } else if (((node->type == XML_TEXT_NODE) ||
7741 (node->type == XML_CDATA_SECTION_NODE)) &&
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00007742 (cur->type == XML_RELAXNG_TEXT)) {
7743 return(1);
7744 }
7745 cur = list[i++];
7746 }
7747 return(0);
7748}
7749
7750/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007751 * xmlRelaxNGValidateInterleave:
7752 * @ctxt: a Relax-NG validation context
7753 * @define: the definition to verify
7754 *
7755 * Validate an interleave definition for a node.
7756 *
7757 * Returns 0 if the validation succeeded or an error code.
7758 */
7759static int
7760xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
7761 xmlRelaxNGDefinePtr define) {
7762 int ret = 0, i, nbgroups, left;
7763 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007764 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007765
7766 xmlRelaxNGValidStatePtr oldstate;
7767 xmlRelaxNGPartitionPtr partitions;
7768 xmlRelaxNGInterleaveGroupPtr group = NULL;
7769 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
7770 xmlNodePtr *list = NULL, *lasts = NULL;
7771
7772 if (define->data != NULL) {
7773 partitions = (xmlRelaxNGPartitionPtr) define->data;
7774 nbgroups = partitions->nbgroups;
7775 left = nbgroups;
7776 } else {
7777 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
7778 return(-1);
7779 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007780 /*
7781 * Optimizations for MIXED
7782 */
7783 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00007784 if (define->dflags & IS_MIXED) {
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007785 ctxt->flags |= FLAGS_MIXED_CONTENT;
7786 if (nbgroups == 2) {
7787 /*
7788 * this is a pure <mixed> case
7789 */
7790 if (ctxt->state != NULL)
7791 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
7792 ctxt->state->seq);
7793 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
7794 ret = xmlRelaxNGValidateDefinition(ctxt,
7795 partitions->groups[1]->rule);
7796 else
7797 ret = xmlRelaxNGValidateDefinition(ctxt,
7798 partitions->groups[0]->rule);
7799 if (ret == 0) {
7800 if (ctxt->state != NULL)
7801 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
7802 ctxt->state->seq);
7803 }
7804 ctxt->flags = oldflags;
7805 return(ret);
7806 }
7807 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007808
7809 /*
7810 * Build arrays to store the first and last node of the chain
7811 * pertaining to each group
7812 */
7813 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
7814 if (list == NULL) {
7815 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7816 return(-1);
7817 }
7818 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
7819 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
7820 if (lasts == NULL) {
7821 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
7822 return(-1);
7823 }
7824 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
7825
7826 /*
7827 * Walk the sequence of children finding the right group and
7828 * sorting them in sequences.
7829 */
7830 cur = ctxt->state->seq;
7831 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
7832 start = cur;
7833 while (cur != NULL) {
7834 ctxt->state->seq = cur;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00007835 if ((partitions->triage != NULL) &&
7836 (partitions->flags & IS_DETERMINIST)) {
7837 void *tmp = NULL;
7838
7839 if ((cur->type == XML_TEXT_NODE) ||
7840 (cur->type == XML_CDATA_SECTION_NODE)) {
7841 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
7842 NULL);
7843 } else if (cur->type == XML_ELEMENT_NODE) {
7844 if (cur->ns != NULL) {
7845 tmp = xmlHashLookup2(partitions->triage, cur->name,
7846 cur->ns->href);
7847 if (tmp == NULL)
7848 tmp = xmlHashLookup2(partitions->triage,
7849 BAD_CAST "#any", cur->ns->href);
7850 } else
7851 tmp = xmlHashLookup2(partitions->triage, cur->name, NULL);
7852 if (tmp == NULL)
7853 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#any",
7854 NULL);
7855 }
7856
7857 if (tmp == NULL) {
7858 i = nbgroups;
7859 } else {
7860 i = ((long) tmp) - 1;
7861 if (partitions->flags & IS_NEEDCHECK) {
7862 group = partitions->groups[i];
7863 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
7864 i = nbgroups;
7865 }
7866 }
7867 } else {
7868 for (i = 0;i < nbgroups;i++) {
7869 group = partitions->groups[i];
7870 if (group == NULL)
7871 continue;
7872 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
7873 break;
7874 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00007875 }
7876 /*
7877 * We break as soon as an element not matched is found
7878 */
7879 if (i >= nbgroups) {
7880 break;
7881 }
7882 if (lasts[i] != NULL) {
7883 lasts[i]->next = cur;
7884 lasts[i] = cur;
7885 } else {
7886 list[i] = cur;
7887 lasts[i] = cur;
7888 }
7889 if (cur->next != NULL)
7890 lastchg = cur->next;
7891 else
7892 lastchg = cur;
7893 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
7894 }
7895 if (ret != 0) {
7896 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
7897 ret = -1;
7898 goto done;
7899 }
7900 lastelem = cur;
7901 oldstate = ctxt->state;
7902 for (i = 0;i < nbgroups;i++) {
7903 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
7904 group = partitions->groups[i];
7905 if (lasts[i] != NULL) {
7906 last = lasts[i]->next;
7907 lasts[i]->next = NULL;
7908 }
7909 ctxt->state->seq = list[i];
7910 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
7911 if (ret != 0)
7912 break;
7913 if (ctxt->state != NULL) {
7914 cur = ctxt->state->seq;
7915 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
Daniel Veillard798024a2003-03-19 10:36:09 +00007916 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007917 oldstate = ctxt->state;
7918 ctxt->state = NULL;
7919 if (cur != NULL) {
7920 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
7921 ret = -1;
7922 ctxt->state = oldstate;
7923 goto done;
7924 }
7925 } else if (ctxt->states != NULL) {
7926 int j;
7927 int found = 0;
7928
7929 for (j = 0;j < ctxt->states->nbState;j++) {
7930 cur = ctxt->states->tabState[j]->seq;
7931 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
7932 if (cur == NULL) {
7933 found = 1;
7934 break;
7935 }
7936 }
7937 if (ctxt->states->nbState > 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00007938 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007939 oldstate = ctxt->states->tabState[ctxt->states->nbState - 1];
7940 }
7941 for (j = 0;j < ctxt->states->nbState - 1;j++) {
Daniel Veillard798024a2003-03-19 10:36:09 +00007942 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[j]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007943 }
7944 xmlRelaxNGFreeStates(ctxt, ctxt->states);
7945 ctxt->states = NULL;
7946 if (found == 0) {
7947 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
7948 ret = -1;
7949 ctxt->state = oldstate;
7950 goto done;
7951 }
7952 } else {
7953 ret = -1;
7954 break;
7955 }
7956 if (lasts[i] != NULL) {
7957 lasts[i]->next = last;
7958 }
7959 }
7960 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00007961 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007962 ctxt->state = oldstate;
7963 ctxt->state->seq = lastelem;
7964 if (ret != 0) {
7965 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
7966 ret = -1;
7967 goto done;
7968 }
7969
7970done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00007971 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007972 /*
7973 * builds the next links chain from the prev one
7974 */
7975 cur = lastchg;
7976 while (cur != NULL) {
7977 if ((cur == start) || (cur->prev == NULL))
7978 break;
7979 cur->prev->next = cur;
7980 cur = cur->prev;
7981 }
7982 if (ret == 0) {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00007983 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007984 }
7985
7986 xmlFree(list);
7987 xmlFree(lasts);
7988 return(ret);
7989}
7990
7991/**
7992 * xmlRelaxNGValidateDefinitionList:
7993 * @ctxt: a Relax-NG validation context
7994 * @define: the list of definition to verify
7995 *
7996 * Validate the given node content against the (list) of definitions
7997 *
7998 * Returns 0 if the validation succeeded or an error code.
7999 */
8000static int
8001xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
8002 xmlRelaxNGDefinePtr defines) {
8003 int ret = 0, res;
8004
8005
Daniel Veillard952379b2003-03-17 15:37:12 +00008006 if (defines == NULL) {
8007 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL, BAD_CAST "NULL definition list");
8008 return(-1);
8009 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008010 while (defines != NULL) {
8011 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
8012 res = xmlRelaxNGValidateDefinition(ctxt, defines);
8013 if (res < 0)
8014 ret = -1;
8015 } else {
8016 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8017 return(-1);
8018 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008019 if (res == -1) /* continues on -2 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00008020 break;
8021 defines = defines->next;
8022 }
8023
8024 return(ret);
8025}
8026
8027/**
8028 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00008029 * @ctxt: a Relax-NG validation context
8030 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00008031 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00008032 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008033 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00008034 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008035 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00008036 */
8037static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00008038xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
8039 xmlRelaxNGDefinePtr define,
8040 xmlNodePtr elem) {
Daniel Veillard580ced82003-03-21 21:22:48 +00008041 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008042
Daniel Veillardfd573f12003-03-16 17:52:32 +00008043 if (define->name != NULL) {
8044 if (!xmlStrEqual(elem->name, define->name)) {
8045 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
8046 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008047 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008048 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008049 if ((define->ns != NULL) && (define->ns[0] != 0)) {
8050 if (elem->ns == NULL) {
8051 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS,
8052 elem->name);
8053 return(0);
8054 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
8055 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
8056 elem->name, define->ns);
8057 return(0);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008058 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008059 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
8060 (define->name == NULL)) {
8061 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
8062 elem->name);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008063 return(0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008064 } else if ((elem->ns != NULL) && (define->name != NULL)) {
8065 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS,
8066 define->name);
8067 return(0);
8068 }
8069
8070 if (define->nameClass == NULL)
8071 return(1);
8072
8073 define = define->nameClass;
8074 if (define->type == XML_RELAXNG_EXCEPT) {
8075 xmlRelaxNGDefinePtr list;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008076 if (ctxt != NULL) {
8077 oldflags = ctxt->flags;
8078 ctxt->flags |= FLAGS_IGNORABLE;
8079 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008080
8081 list = define->content;
8082 while (list != NULL) {
8083 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
8084 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008085 if (ctxt != NULL)
8086 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008087 return(0);
8088 }
8089 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008090 if (ctxt != NULL)
8091 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008092 return(ret);
8093 }
8094 list = list->next;
8095 }
8096 ret = 1;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008097 if (ctxt != NULL) {
8098 ctxt->flags = oldflags;
8099 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008100 } else if (define->type == XML_RELAXNG_CHOICE) {
8101 xmlRelaxNGDefinePtr list;
8102
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008103 if (ctxt != NULL) {
8104 oldflags = ctxt->flags;
8105 ctxt->flags |= FLAGS_IGNORABLE;
8106 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008107
8108 list = define->nameClass;
8109 while (list != NULL) {
8110 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
8111 if (ret == 1) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008112 if (ctxt != NULL)
8113 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008114 return(1);
8115 }
8116 if (ret < 0) {
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008117 if (ctxt != NULL)
8118 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008119 return(ret);
8120 }
8121 list = list->next;
8122 }
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008123 if (ctxt != NULL) {
8124 if (ret != 0) {
8125 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8126 xmlRelaxNGDumpValidError(ctxt);
8127 } else {
8128 if (ctxt->errNr > 0) xmlRelaxNGPopErrors(ctxt, 0);
8129 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008130 }
8131 ret = 0;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00008132 if (ctxt != NULL) {
8133 ctxt->flags = oldflags;
8134 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008135 } else {
8136 TODO
8137 ret = -1;
8138 }
8139 return(ret);
8140}
8141
8142/**
8143 * xmlRelaxNGValidateElementEnd:
8144 * @ctxt: a Relax-NG validation context
8145 *
8146 * Validate the end of the element, implements check that
8147 * there is nothing left not consumed in the element content
8148 * or in the attribute list.
8149 *
8150 * Returns 0 if the validation succeeded or an error code.
8151 */
8152static int
8153xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt) {
8154 int ret = 0, i;
8155 xmlRelaxNGValidStatePtr state;
8156
8157 state = ctxt->state;
8158 if (state->seq != NULL) {
8159 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
8160 if (state->seq != NULL) {
8161 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
8162 state->node->name, state->seq->name);
8163 ret = -1;
8164 }
8165 }
8166 for (i = 0;i < state->nbAttrs;i++) {
8167 if (state->attrs[i] != NULL) {
8168 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
8169 state->attrs[i]->name, state->node->name);
8170 ret = -1;
8171 }
8172 }
8173 return(ret);
8174}
8175
8176/**
8177 * xmlRelaxNGValidateState:
8178 * @ctxt: a Relax-NG validation context
8179 * @define: the definition to verify
8180 *
8181 * Validate the current state against the definition
8182 *
8183 * Returns 0 if the validation succeeded or an error code.
8184 */
8185static int
8186xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
8187 xmlRelaxNGDefinePtr define) {
8188 xmlNodePtr node;
8189 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00008190 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008191
8192 if (define == NULL) {
8193 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8194 return(-1);
8195 }
8196
8197 if (ctxt->state != NULL) {
8198 node = ctxt->state->seq;
8199 } else {
8200 node = NULL;
8201 }
8202#ifdef DEBUG
8203 for (i = 0;i < ctxt->depth;i++)
8204 xmlGenericError(xmlGenericErrorContext, " ");
8205 xmlGenericError(xmlGenericErrorContext,
8206 "Start validating %s ", xmlRelaxNGDefName(define));
8207 if (define->name != NULL)
8208 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
8209 if ((node != NULL) && (node->name != NULL))
8210 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
8211 else
8212 xmlGenericError(xmlGenericErrorContext, "\n");
8213#endif
8214 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008215 switch (define->type) {
8216 case XML_RELAXNG_EMPTY:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008217 node = xmlRelaxNGSkipIgnored(ctxt, node);
8218 ret = 0;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008219 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008220 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008221 ret = -1;
8222 break;
8223 case XML_RELAXNG_TEXT:
8224 while ((node != NULL) &&
8225 ((node->type == XML_TEXT_NODE) ||
8226 (node->type == XML_COMMENT_NODE) ||
8227 (node->type == XML_PI_NODE) ||
8228 (node->type == XML_CDATA_SECTION_NODE)))
8229 node = node->next;
8230 ctxt->state->seq = node;
8231 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008232 case XML_RELAXNG_ELEMENT:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008233 errNr = ctxt->errNr;
8234 node = xmlRelaxNGSkipIgnored(ctxt, node);
8235 if (node == NULL) {
8236 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
8237 ret = -1;
8238 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8239 xmlRelaxNGDumpValidError(ctxt);
8240 break;
8241 }
8242 if (node->type != XML_ELEMENT_NODE) {
8243 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8244 ret = -1;
8245 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8246 xmlRelaxNGDumpValidError(ctxt);
8247 break;
8248 }
8249 /*
8250 * This node was already validated successfully against
8251 * this definition.
8252 */
8253 if (node->_private == define) {
8254 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
Daniel Veillard580ced82003-03-21 21:22:48 +00008255 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008256 if (ctxt->errNr != 0) {
8257 while ((ctxt->err != NULL) &&
8258 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
8259 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
Daniel Veillard580ced82003-03-21 21:22:48 +00008260 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
8261 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008262 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
8263 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
8264 xmlRelaxNGValidErrorPop(ctxt);
8265 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008266 break;
8267 }
8268
8269 ret = xmlRelaxNGElementMatch(ctxt, define, node);
8270 if (ret <= 0) {
8271 ret = -1;
8272 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8273 xmlRelaxNGDumpValidError(ctxt);
8274 break;
8275 }
8276 ret = 0;
8277 if (ctxt->errNr != 0) {
Daniel Veillard580ced82003-03-21 21:22:48 +00008278 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008279 while ((ctxt->err != NULL) &&
8280 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
8281 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
Daniel Veillard580ced82003-03-21 21:22:48 +00008282 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
8283 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
Daniel Veillardfd573f12003-03-16 17:52:32 +00008284 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
8285 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
8286 xmlRelaxNGValidErrorPop(ctxt);
8287 }
8288 errNr = ctxt->errNr;
8289
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008290 oldflags = ctxt->flags;
8291 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
8292 ctxt->flags -= FLAGS_MIXED_CONTENT;
8293 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008294 state = xmlRelaxNGNewValidState(ctxt, node);
8295 if (state == NULL) {
8296 ret = -1;
8297 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8298 xmlRelaxNGDumpValidError(ctxt);
8299 break;
8300 }
8301
8302 oldstate = ctxt->state;
8303 ctxt->state = state;
8304 if (define->attrs != NULL) {
8305 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8306 if (tmp != 0) {
8307 ret = -1;
8308 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8309 }
8310 }
8311 if (define->content != NULL) {
8312 tmp = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8313 if (tmp != 0) {
8314 ret = -1;
8315 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID, node->name);
8316 }
8317 }
8318 if (ctxt->states != NULL) {
8319 tmp = -1;
8320
Daniel Veillardfd573f12003-03-16 17:52:32 +00008321 ctxt->flags |= FLAGS_IGNORABLE;
8322
8323 for (i = 0;i < ctxt->states->nbState;i++) {
8324 state = ctxt->states->tabState[i];
8325 ctxt->state = state;
8326
8327 if (xmlRelaxNGValidateElementEnd(ctxt) == 0)
8328 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +00008329 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008330 }
8331 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8332 ctxt->flags = oldflags;
8333 ctxt->states = NULL;
8334 if ((ret == 0) && (tmp == -1))
8335 ret = -1;
8336 } else {
8337 state = ctxt->state;
8338 if (ret == 0)
8339 ret = xmlRelaxNGValidateElementEnd(ctxt);
Daniel Veillard798024a2003-03-19 10:36:09 +00008340 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008341 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008342 if (ret == 0) {
8343 node->_private = define;
8344 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00008345 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008346 ctxt->state = oldstate;
8347 if (oldstate != NULL)
8348 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008349 if (ret != 0) {
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008350 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008351 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008352 ret = 0;
8353 } else {
8354 ret = -2;
8355 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008356 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008357 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008358 }
8359
8360#ifdef DEBUG
8361 xmlGenericError(xmlGenericErrorContext,
8362 "xmlRelaxNGValidateDefinition(): validated %s : %d",
8363 node->name, ret);
8364 if (oldstate == NULL)
8365 xmlGenericError(xmlGenericErrorContext, ": no state\n");
8366 else if (oldstate->seq == NULL)
8367 xmlGenericError(xmlGenericErrorContext, ": done\n");
8368 else if (oldstate->seq->type == XML_ELEMENT_NODE)
8369 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
8370 oldstate->seq->name);
8371 else
8372 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
8373 oldstate->seq->name, oldstate->seq->type);
8374#endif
8375 break;
8376 case XML_RELAXNG_OPTIONAL: {
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008377 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008378 oldflags = ctxt->flags;
8379 ctxt->flags |= FLAGS_IGNORABLE;
8380 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
8381 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8382 if (ret != 0) {
8383 if (ctxt->state != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00008384 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008385 ctxt->state = oldstate;
8386 ctxt->flags = oldflags;
8387 ret = 0;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008388 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008389 break;
8390 }
8391 if (ctxt->states != NULL) {
8392 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
8393 } else {
8394 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
8395 if (ctxt->states == NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008396 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008397 ctxt->flags = oldflags;
8398 ret = -1;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008399 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008400 break;
8401 }
8402 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
8403 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
8404 ctxt->state = NULL;
8405 }
8406 ctxt->flags = oldflags;
8407 ret = 0;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008408 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008409 break;
8410 }
8411 case XML_RELAXNG_ONEORMORE:
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008412 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008413 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
8414 if (ret != 0) {
8415 break;
8416 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008417 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008418 /* no break on purpose */
8419 case XML_RELAXNG_ZEROORMORE: {
8420 int progress;
8421 xmlRelaxNGStatesPtr states = NULL, res = NULL;
8422 int base, j;
8423
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008424 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008425 res = xmlRelaxNGNewStates(ctxt, 1);
8426 if (res == NULL) {
8427 ret = -1;
8428 break;
8429 }
8430 /*
8431 * All the input states are also exit states
8432 */
8433 if (ctxt->state != NULL) {
8434 xmlRelaxNGAddStates(ctxt, res,
8435 xmlRelaxNGCopyValidState(ctxt, ctxt->state));
8436 } else {
8437 for (j = 0;j < ctxt->states->nbState;j++) {
8438 xmlRelaxNGAddStates(ctxt, res,
8439 xmlRelaxNGCopyValidState(ctxt,
8440 ctxt->states->tabState[j]));
8441 }
8442 }
8443 oldflags = ctxt->flags;
8444 ctxt->flags |= FLAGS_IGNORABLE;
8445 do {
8446 progress = 0;
8447 base = res->nbState;
8448
8449 if (ctxt->states != NULL) {
8450 states = ctxt->states;
8451 for (i = 0;i < states->nbState;i++) {
8452 ctxt->state = states->tabState[i];
8453 ctxt->states = NULL;
8454 ret = xmlRelaxNGValidateDefinitionList(ctxt,
8455 define->content);
8456 if (ret == 0) {
8457 if (ctxt->state != NULL) {
8458 tmp = xmlRelaxNGAddStates(ctxt, res,
8459 ctxt->state);
8460 ctxt->state = NULL;
8461 if (tmp == 1)
8462 progress = 1;
8463 } else if (ctxt->states != NULL) {
8464 for (j = 0;j < ctxt->states->nbState;j++) {
8465 tmp = xmlRelaxNGAddStates(ctxt, res,
8466 ctxt->states->tabState[j]);
8467 if (tmp == 1)
8468 progress = 1;
8469 }
8470 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8471 ctxt->states = NULL;
8472 }
8473 } else {
8474 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008475 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008476 ctxt->state = NULL;
8477 }
8478 }
8479 }
8480 } else {
8481 ret = xmlRelaxNGValidateDefinitionList(ctxt,
8482 define->content);
8483 if (ret != 0) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008484 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008485 ctxt->state = NULL;
8486 } else {
8487 base = res->nbState;
8488 if (ctxt->state != NULL) {
8489 tmp = xmlRelaxNGAddStates(ctxt, res,
8490 ctxt->state);
8491 ctxt->state = NULL;
8492 if (tmp == 1)
8493 progress = 1;
8494 } else if (ctxt->states != NULL) {
8495 for (j = 0;j < ctxt->states->nbState;j++) {
8496 tmp = xmlRelaxNGAddStates(ctxt, res,
8497 ctxt->states->tabState[j]);
8498 if (tmp == 1)
8499 progress = 1;
8500 }
8501 if (states == NULL) {
8502 states = ctxt->states;
8503 } else {
8504 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8505 }
8506 ctxt->states = NULL;
8507 }
8508 }
8509 }
8510 if (progress) {
8511 /*
8512 * Collect all the new nodes added at that step
8513 * and make them the new node set
8514 */
8515 if (res->nbState - base == 1) {
8516 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
8517 res->tabState[base]);
8518 } else {
8519 if (states == NULL) {
8520 xmlRelaxNGNewStates(ctxt, res->nbState - base);
8521 }
8522 states->nbState = 0;
8523 for (i = base;i < res->nbState;i++)
8524 xmlRelaxNGAddStates(ctxt, states,
8525 xmlRelaxNGCopyValidState(ctxt,
8526 res->tabState[i]));
8527 ctxt->states = states;
8528 }
8529 }
8530 } while (progress == 1);
8531 if (states != NULL) {
8532 xmlRelaxNGFreeStates(ctxt, states);
8533 }
8534 ctxt->states = res;
8535 ctxt->flags = oldflags;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008536 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008537 ret = 0;
8538 break;
8539 }
8540 case XML_RELAXNG_CHOICE: {
Daniel Veillard580ced82003-03-21 21:22:48 +00008541 xmlRelaxNGDefinePtr list = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008542 xmlRelaxNGStatesPtr states = NULL;
8543
Daniel Veillarde063f482003-03-21 16:53:17 +00008544 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008545
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008546 errNr = ctxt->errNr;
Daniel Veillarde063f482003-03-21 16:53:17 +00008547 if ((define->dflags & IS_TRIABLE) && (define->data != NULL)) {
8548 xmlHashTablePtr triage = (xmlHashTablePtr) define->data;
8549
8550 /*
8551 * Something we can optimize cleanly there is only one
8552 * possble branch out !
8553 */
8554 if (node == NULL) {
8555 ret = -1;
8556 break;
8557 }
8558 if ((node->type == XML_TEXT_NODE) ||
8559 (node->type == XML_CDATA_SECTION_NODE)) {
8560 list = xmlHashLookup2(triage, BAD_CAST "#text", NULL);
8561 } else if (node->type == XML_ELEMENT_NODE) {
8562 if (node->ns != NULL) {
8563 list = xmlHashLookup2(triage, node->name,
8564 node->ns->href);
8565 if (list == NULL)
8566 list = xmlHashLookup2(triage, BAD_CAST "#any",
8567 node->ns->href);
8568 } else
8569 list = xmlHashLookup2(triage, node->name, NULL);
8570 if (list == NULL)
8571 list = xmlHashLookup2(triage, BAD_CAST "#any", NULL);
8572 }
8573 if (list == NULL) {
8574 ret = -1;
8575 break;
8576 }
8577 ret = xmlRelaxNGValidateDefinition(ctxt, list);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008578 if (ret == 0) {
8579 }
Daniel Veillarde063f482003-03-21 16:53:17 +00008580 break;
8581 }
8582
8583 list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008584 oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008585 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008586
8587 while (list != NULL) {
8588 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
8589 ret = xmlRelaxNGValidateDefinition(ctxt, list);
8590 if (ret == 0) {
8591 if (states == NULL) {
8592 states = xmlRelaxNGNewStates(ctxt, 1);
8593 }
8594 if (ctxt->state != NULL) {
8595 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
8596 } else if (ctxt->states != NULL) {
8597 for (i = 0;i < ctxt->states->nbState;i++) {
8598 xmlRelaxNGAddStates(ctxt, states,
8599 ctxt->states->tabState[i]);
8600 }
8601 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8602 ctxt->states = NULL;
8603 }
8604 } else {
Daniel Veillard798024a2003-03-19 10:36:09 +00008605 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008606 }
8607 ctxt->state = oldstate;
8608 list = list->next;
8609 }
8610 if (states != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008611 xmlRelaxNGFreeValidState(ctxt,oldstate);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008612 ctxt->states = states;
8613 ctxt->state = NULL;
8614 ret = 0;
8615 } else {
8616 ctxt->states = NULL;
8617 }
8618 ctxt->flags = oldflags;
8619 if (ret != 0) {
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008620 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008621 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008622 }
8623 } else {
Daniel Veillard28c52ab2003-03-18 11:39:17 +00008624 if (ctxt->errNr > errNr) xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008625 }
8626 break;
8627 }
8628 case XML_RELAXNG_DEF:
8629 case XML_RELAXNG_GROUP:
8630 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008631 break;
8632 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008633 ret = xmlRelaxNGValidateInterleave(ctxt, define);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008634 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008635 case XML_RELAXNG_ATTRIBUTE:
8636 ret = xmlRelaxNGValidateAttribute(ctxt, define);
8637 break;
8638 case XML_RELAXNG_NOOP:
8639 case XML_RELAXNG_REF:
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008640 case XML_RELAXNG_EXTERNALREF:
Daniel Veillardfd573f12003-03-16 17:52:32 +00008641 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
8642 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00008643 case XML_RELAXNG_PARENTREF:
8644 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
8645 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008646 case XML_RELAXNG_DATATYPE: {
8647 xmlNodePtr child;
8648 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008649
Daniel Veillardfd573f12003-03-16 17:52:32 +00008650 child = node;
8651 while (child != NULL) {
8652 if (child->type == XML_ELEMENT_NODE) {
8653 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
8654 node->parent->name);
8655 ret = -1;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008656 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008657 } else if ((child->type == XML_TEXT_NODE) ||
8658 (child->type == XML_CDATA_SECTION_NODE)) {
8659 content = xmlStrcat(content, child->content);
8660 }
8661 /* TODO: handle entities ... */
8662 child = child->next;
8663 }
8664 if (ret == -1) {
8665 if (content != NULL)
8666 xmlFree(content);
8667 break;
8668 }
8669 if (content == NULL) {
8670 content = xmlStrdup(BAD_CAST "");
8671 if (content == NULL) {
8672 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8673 ret = -1;
8674 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008675 }
8676 }
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008677 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
8678 ctxt->state->seq);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008679 if (ret == -1) {
8680 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
8681 } else if (ret == 0) {
8682 ctxt->state->seq = NULL;
8683 }
8684 if (content != NULL)
8685 xmlFree(content);
8686 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008687 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008688 case XML_RELAXNG_VALUE: {
8689 xmlChar *content = NULL;
8690 xmlChar *oldvalue;
8691 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008692
Daniel Veillardfd573f12003-03-16 17:52:32 +00008693 child = node;
8694 while (child != NULL) {
8695 if (child->type == XML_ELEMENT_NODE) {
8696 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
8697 node->parent->name);
8698 ret = -1;
8699 break;
8700 } else if ((child->type == XML_TEXT_NODE) ||
8701 (child->type == XML_CDATA_SECTION_NODE)) {
8702 content = xmlStrcat(content, child->content);
8703 }
8704 /* TODO: handle entities ... */
8705 child = child->next;
8706 }
8707 if (ret == -1) {
8708 if (content != NULL)
8709 xmlFree(content);
8710 break;
8711 }
8712 if (content == NULL) {
8713 content = xmlStrdup(BAD_CAST "");
8714 if (content == NULL) {
8715 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8716 ret = -1;
8717 break;
8718 }
8719 }
8720 oldvalue = ctxt->state->value;
8721 ctxt->state->value = content;
8722 ret = xmlRelaxNGValidateValue(ctxt, define);
8723 ctxt->state->value = oldvalue;
8724 if (ret == -1) {
8725 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
8726 } else if (ret == 0) {
8727 ctxt->state->seq = NULL;
8728 }
8729 if (content != NULL)
8730 xmlFree(content);
8731 break;
8732 }
8733 case XML_RELAXNG_LIST: {
8734 xmlChar *content;
8735 xmlNodePtr child;
8736 xmlChar *oldvalue, *oldendvalue;
8737 int len;
8738
8739 /*
8740 * Make sure it's only text nodes
8741 */
8742
8743 content = NULL;
8744 child = node;
8745 while (child != NULL) {
8746 if (child->type == XML_ELEMENT_NODE) {
8747 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
8748 node->parent->name);
8749 ret = -1;
8750 break;
8751 } else if ((child->type == XML_TEXT_NODE) ||
8752 (child->type == XML_CDATA_SECTION_NODE)) {
8753 content = xmlStrcat(content, child->content);
8754 }
8755 /* TODO: handle entities ... */
8756 child = child->next;
8757 }
8758 if (ret == -1) {
8759 if (content != NULL)
8760 xmlFree(content);
8761 break;
8762 }
8763 if (content == NULL) {
8764 content = xmlStrdup(BAD_CAST "");
8765 if (content == NULL) {
8766 VALID_ERR(XML_RELAXNG_ERR_MEMORY);
8767 ret = -1;
8768 break;
8769 }
8770 }
8771 len = xmlStrlen(content);
8772 oldvalue = ctxt->state->value;
8773 oldendvalue = ctxt->state->endvalue;
8774 ctxt->state->value = content;
8775 ctxt->state->endvalue = content + len;
8776 ret = xmlRelaxNGValidateValue(ctxt, define);
8777 ctxt->state->value = oldvalue;
8778 ctxt->state->endvalue = oldendvalue;
8779 if (ret == -1) {
8780 VALID_ERR(XML_RELAXNG_ERR_LIST);
8781 } else if ((ret == 0) && (node != NULL)) {
8782 ctxt->state->seq = node->next;
8783 }
8784 if (content != NULL)
8785 xmlFree(content);
8786 break;
8787 }
8788 case XML_RELAXNG_START:
8789 case XML_RELAXNG_EXCEPT:
8790 case XML_RELAXNG_PARAM:
8791 TODO
8792 ret = -1;
8793 break;
8794 }
8795 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008796#ifdef DEBUG
Daniel Veillardfd573f12003-03-16 17:52:32 +00008797 for (i = 0;i < ctxt->depth;i++)
8798 xmlGenericError(xmlGenericErrorContext, " ");
8799 xmlGenericError(xmlGenericErrorContext,
8800 "Validating %s ", xmlRelaxNGDefName(define));
8801 if (define->name != NULL)
8802 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
8803 if (ret == 0)
8804 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
8805 else
8806 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008807#endif
Daniel Veillardfd573f12003-03-16 17:52:32 +00008808 return(ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008809}
8810
8811/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00008812 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008813 * @ctxt: a Relax-NG validation context
8814 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008815 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008816 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008817 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00008818 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008819 */
8820static int
Daniel Veillardfd573f12003-03-16 17:52:32 +00008821xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
8822 xmlRelaxNGDefinePtr define) {
8823 xmlRelaxNGStatesPtr states, res;
8824 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008825
Daniel Veillardfd573f12003-03-16 17:52:32 +00008826 /*
8827 * We should NOT have both ctxt->state and ctxt->states
8828 */
8829 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8830 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008831 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008832 ctxt->state = NULL;
8833 }
8834
8835 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
8836 if (ctxt->states != NULL) {
8837 ctxt->state = ctxt->states->tabState[0];
8838 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8839 ctxt->states = NULL;
8840 }
8841 ret = xmlRelaxNGValidateState(ctxt, define);
8842 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8843 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008844 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008845 ctxt->state = NULL;
8846 }
8847 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
8848 ctxt->state = ctxt->states->tabState[0];
8849 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8850 ctxt->states = NULL;
8851 }
8852 return(ret);
8853 }
8854
8855 states = ctxt->states;
8856 ctxt->states = NULL;
8857 res = NULL;
8858 j = 0;
8859 oldflags = ctxt->flags;
8860 ctxt->flags |= FLAGS_IGNORABLE;
8861 for (i = 0;i < states->nbState;i++) {
8862 ctxt->state = states->tabState[i];
8863 ctxt->states = NULL;
8864 ret = xmlRelaxNGValidateState(ctxt, define);
8865 /*
8866 * We should NOT have both ctxt->state and ctxt->states
8867 */
8868 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8869 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008870 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008871 ctxt->state = NULL;
8872 }
8873 if (ret == 0) {
8874 if (ctxt->states == NULL) {
8875 if (res != NULL) {
8876 /* add the state to the container */
8877 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
8878 ctxt->state = NULL;
8879 } else {
8880 /* add the state directly in states */
8881 states->tabState[j++] = ctxt->state;
8882 ctxt->state = NULL;
8883 }
8884 } else {
8885 if (res == NULL) {
8886 /* make it the new container and copy other results */
8887 res = ctxt->states;
8888 ctxt->states = NULL;
8889 for (k = 0;k < j;k++)
8890 xmlRelaxNGAddStates(ctxt, res, states->tabState[k]);
8891 } else {
8892 /* add all the new results to res and reff the container */
8893 for (k = 0;k < ctxt->states->nbState;k++)
8894 xmlRelaxNGAddStates(ctxt, res,
8895 ctxt->states->tabState[k]);
8896 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8897 ctxt->states = NULL;
8898 }
8899 }
8900 } else {
8901 if (ctxt->state != NULL) {
Daniel Veillard798024a2003-03-19 10:36:09 +00008902 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008903 ctxt->state = NULL;
8904 } else if (ctxt->states != NULL) {
8905 for (k = 0;k < ctxt->states->nbState;k++)
Daniel Veillard798024a2003-03-19 10:36:09 +00008906 xmlRelaxNGFreeValidState(ctxt,ctxt->states->tabState[k]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008907 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8908 ctxt->states = NULL;
8909 }
8910 }
8911 }
8912 ctxt->flags = oldflags;
8913 if (res != NULL) {
8914 xmlRelaxNGFreeStates(ctxt, states);
8915 ctxt->states = res;
8916 ret = 0;
8917 } else if (j > 1) {
8918 states->nbState = j;
8919 ctxt->states = states;
8920 ret =0;
8921 } else if (j == 1) {
8922 ctxt->state = states->tabState[0];
8923 xmlRelaxNGFreeStates(ctxt, states);
8924 ret = 0;
8925 } else {
8926 ret = -1;
8927 xmlRelaxNGFreeStates(ctxt, states);
8928 if (ctxt->states != NULL) {
8929 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8930 ctxt->states = NULL;
8931 }
8932 }
8933 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
8934 TODO
Daniel Veillard798024a2003-03-19 10:36:09 +00008935 xmlRelaxNGFreeValidState(ctxt,ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008936 ctxt->state = NULL;
8937 }
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008938 return(ret);
8939}
8940
Daniel Veillard1564e6e2003-03-15 21:30:25 +00008941/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008942 * xmlRelaxNGValidateDocument:
8943 * @ctxt: a Relax-NG validation context
8944 * @doc: the document
8945 *
8946 * Validate the given document
8947 *
8948 * Returns 0 if the validation succeeded or an error code.
8949 */
8950static int
8951xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
8952 int ret;
8953 xmlRelaxNGPtr schema;
8954 xmlRelaxNGGrammarPtr grammar;
8955 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008956 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008957
8958 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
8959 return(-1);
8960
Daniel Veillarda507fbf2003-03-31 16:09:37 +00008961 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008962 schema = ctxt->schema;
8963 grammar = schema->topgrammar;
8964 if (grammar == NULL) {
Daniel Veillard42f12e92003-03-07 18:32:59 +00008965 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008966 return(-1);
8967 }
8968 state = xmlRelaxNGNewValidState(ctxt, NULL);
8969 ctxt->state = state;
8970 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008971 if ((ctxt->state != NULL) && (state->seq != NULL)) {
8972 state = ctxt->state;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008973 node = state->seq;
8974 node = xmlRelaxNGSkipIgnored(ctxt, node);
8975 if (node != NULL) {
Daniel Veillardfd573f12003-03-16 17:52:32 +00008976 if (ret != -1) {
8977 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
8978 ret = -1;
8979 }
8980 }
8981 } else if (ctxt->states != NULL) {
8982 int i;
8983 int tmp = -1;
8984
8985 for (i = 0;i < ctxt->states->nbState;i++) {
8986 state = ctxt->states->tabState[i];
8987 node = state->seq;
8988 node = xmlRelaxNGSkipIgnored(ctxt, node);
8989 if (node == NULL)
8990 tmp = 0;
Daniel Veillard798024a2003-03-19 10:36:09 +00008991 xmlRelaxNGFreeValidState(ctxt,state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00008992 }
8993 if (tmp == -1) {
8994 if (ret != -1) {
8995 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
8996 ret = -1;
8997 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00008998 }
8999 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009000 if (ctxt->state != NULL) {
9001 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9002 ctxt->state = NULL;
9003 }
Daniel Veillard580ced82003-03-21 21:22:48 +00009004 if (ret != 0)
Daniel Veillardfd573f12003-03-16 17:52:32 +00009005 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +00009006#ifdef DEBUG
9007 else if (ctxt->errNr != 0) {
9008 ctxt->error(ctxt->userData, "%d Extra error messages left on stack !\n",
9009 ctxt->errNr);
9010 xmlRelaxNGDumpValidError(ctxt);
9011 }
9012#endif
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009013 if (ctxt->idref == 1) {
9014 xmlValidCtxt vctxt;
9015
9016 memset(&vctxt, 0, sizeof(xmlValidCtxt));
9017 vctxt.valid = 1;
9018 vctxt.error = ctxt->error;
9019 vctxt.warning = ctxt->warning;
9020 vctxt.userData = ctxt->userData;
9021
9022 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
9023 ret = -1;
9024 }
Daniel Veillarda507fbf2003-03-31 16:09:37 +00009025 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
9026 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009027
9028 return(ret);
9029}
9030
Daniel Veillardfd573f12003-03-16 17:52:32 +00009031/************************************************************************
9032 * *
9033 * Validation interfaces *
9034 * *
9035 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +00009036/**
9037 * xmlRelaxNGNewValidCtxt:
9038 * @schema: a precompiled XML RelaxNGs
9039 *
9040 * Create an XML RelaxNGs validation context based on the given schema
9041 *
9042 * Returns the validation context or NULL in case of error
9043 */
9044xmlRelaxNGValidCtxtPtr
9045xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema) {
9046 xmlRelaxNGValidCtxtPtr ret;
9047
9048 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
9049 if (ret == NULL) {
9050 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardfd573f12003-03-16 17:52:32 +00009051 "Failed to allocate new schema validation context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00009052 return (NULL);
9053 }
9054 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
9055 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00009056 ret->error = xmlGenericError;
9057 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +00009058 ret->errNr = 0;
9059 ret->errMax = 0;
9060 ret->err = NULL;
9061 ret->errTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009062 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +00009063 ret->states = NULL;
9064 ret->freeState = NULL;
9065 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +00009066 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009067 return (ret);
9068}
9069
9070/**
9071 * xmlRelaxNGFreeValidCtxt:
9072 * @ctxt: the schema validation context
9073 *
9074 * Free the resources associated to the schema validation context
9075 */
9076void
9077xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt) {
Daniel Veillard798024a2003-03-19 10:36:09 +00009078 int k;
9079
Daniel Veillard6eadf632003-01-23 18:29:16 +00009080 if (ctxt == NULL)
9081 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009082 if (ctxt->states != NULL)
Daniel Veillard798024a2003-03-19 10:36:09 +00009083 xmlRelaxNGFreeStates(NULL, ctxt->states);
9084 if (ctxt->freeState != NULL) {
9085 for (k = 0;k < ctxt->freeState->nbState;k++) {
9086 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
9087 }
9088 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
9089 }
Daniel Veillard798024a2003-03-19 10:36:09 +00009090 if (ctxt->freeStates != NULL) {
9091 for (k = 0;k < ctxt->freeStatesNr;k++) {
9092 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
9093 }
9094 xmlFree(ctxt->freeStates);
9095 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00009096 if (ctxt->errTab != NULL)
9097 xmlFree(ctxt->errTab);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009098 xmlFree(ctxt);
9099}
9100
9101/**
9102 * xmlRelaxNGSetValidErrors:
9103 * @ctxt: a Relax-NG validation context
9104 * @err: the error function
9105 * @warn: the warning function
9106 * @ctx: the functions context
9107 *
9108 * Set the error and warning callback informations
9109 */
9110void
9111xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
9112 xmlRelaxNGValidityErrorFunc err,
9113 xmlRelaxNGValidityWarningFunc warn, void *ctx) {
9114 if (ctxt == NULL)
9115 return;
9116 ctxt->error = err;
9117 ctxt->warning = warn;
9118 ctxt->userData = ctx;
9119}
9120
9121/**
9122 * xmlRelaxNGValidateDoc:
9123 * @ctxt: a Relax-NG validation context
9124 * @doc: a parsed document tree
9125 *
9126 * Validate a document tree in memory.
9127 *
9128 * Returns 0 if the document is valid, a positive error code
9129 * number otherwise and -1 in case of internal or API error.
9130 */
9131int
9132xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc) {
9133 int ret;
9134
9135 if ((ctxt == NULL) || (doc == NULL))
9136 return(-1);
9137
9138 ctxt->doc = doc;
9139
9140 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +00009141 /*
9142 * TODO: build error codes
9143 */
9144 if (ret == -1)
9145 return(1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009146 return(ret);
9147}
9148
9149#endif /* LIBXML_SCHEMAS_ENABLED */
9150