blob: b12e1ae3635f07460bd39fb88bc27f080eaf39de [file] [log] [blame]
Daniel Veillard6eadf632003-01-23 18:29:16 +00001/*
2 * relaxng.c : implementation of the Relax-NG handling and validity checking
3 *
4 * See Copyright for the status of this software.
5 *
6 * Daniel Veillard <veillard@redhat.com>
7 */
8
Daniel Veillardd41f4f42003-01-29 21:07:52 +00009/**
10 * TODO:
Daniel Veillardf4b4f982003-02-13 11:02:08 +000011 * - add support for DTD compatibility spec
12 * http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
Daniel Veillardac297932003-04-17 12:55:35 +000013 * - report better mem allocations pbms at runtime and abort immediately.
Daniel Veillardd41f4f42003-01-29 21:07:52 +000014 */
15
Daniel Veillard6eadf632003-01-23 18:29:16 +000016#define IN_LIBXML
17#include "libxml.h"
18
19#ifdef LIBXML_SCHEMAS_ENABLED
20
21#include <string.h>
22#include <stdio.h>
Nick Wellnhoferd422b952017-10-09 13:37:42 +020023#include <stddef.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000024#include <libxml/xmlmemory.h>
25#include <libxml/parser.h>
26#include <libxml/parserInternals.h>
27#include <libxml/hash.h>
28#include <libxml/uri.h>
29
30#include <libxml/relaxng.h>
31
32#include <libxml/xmlschemastypes.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlregexp.h>
Daniel Veillardc6e997c2003-01-27 12:35:42 +000035#include <libxml/xmlschemastypes.h>
Daniel Veillard6eadf632003-01-23 18:29:16 +000036
37/*
38 * The Relax-NG namespace
39 */
40static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
41 "http://relaxng.org/ns/structure/1.0";
42
Daniel Veillard264cee62012-08-13 12:40:53 +080043#define IS_RELAXNG(node, typ) \
Daniel Veillard6eadf632003-01-23 18:29:16 +000044 ((node != NULL) && (node->ns != NULL) && \
Daniel Veillard264cee62012-08-13 12:40:53 +080045 (node->type == XML_ELEMENT_NODE) && \
46 (xmlStrEqual(node->name, (const xmlChar *) typ)) && \
Daniel Veillard6eadf632003-01-23 18:29:16 +000047 (xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
48
49
Daniel Veillard23a47d62008-06-25 04:11:24 +000050#if 0
Daniel Veillard87254c82006-02-19 15:27:17 +000051#define DEBUG 1
Daniel Veillard4c004142003-10-07 11:33:24 +000052
Daniel Veillard87254c82006-02-19 15:27:17 +000053#define DEBUG_GRAMMAR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000054
Daniel Veillard87254c82006-02-19 15:27:17 +000055#define DEBUG_CONTENT 1
Daniel Veillard4c004142003-10-07 11:33:24 +000056
Daniel Veillard87254c82006-02-19 15:27:17 +000057#define DEBUG_TYPE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000058
Daniel Veillard87254c82006-02-19 15:27:17 +000059#define DEBUG_VALID 1
Daniel Veillard4c004142003-10-07 11:33:24 +000060
Daniel Veillard87254c82006-02-19 15:27:17 +000061#define DEBUG_INTERLEAVE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000062
Daniel Veillard87254c82006-02-19 15:27:17 +000063#define DEBUG_LIST 1
Daniel Veillard4c004142003-10-07 11:33:24 +000064
Daniel Veillardf8e3db02012-09-11 13:26:36 +080065#define DEBUG_INCLUDE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000066
Daniel Veillard87254c82006-02-19 15:27:17 +000067#define DEBUG_ERROR 1
Daniel Veillard4c004142003-10-07 11:33:24 +000068
Daniel Veillard87254c82006-02-19 15:27:17 +000069#define DEBUG_COMPILE 1
Daniel Veillard4c004142003-10-07 11:33:24 +000070
Daniel Veillard87254c82006-02-19 15:27:17 +000071#define DEBUG_PROGRESSIVE 1
72#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +000073
Daniel Veillard5f1946a2003-03-31 16:38:16 +000074#define MAX_ERROR 5
75
Daniel Veillardf8e3db02012-09-11 13:26:36 +080076#define TODO \
Daniel Veillard6eadf632003-01-23 18:29:16 +000077 xmlGenericError(xmlGenericErrorContext, \
78 "Unimplemented block at %s:%d\n", \
79 __FILE__, __LINE__);
80
81typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
82typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
83
84typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
85typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
86
Daniel Veillardd41f4f42003-01-29 21:07:52 +000087typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
88typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
89
Daniel Veillarda9d912d2003-02-01 17:43:10 +000090typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
91typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
92
Daniel Veillard6eadf632003-01-23 18:29:16 +000093typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +000094 XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
95 XML_RELAXNG_COMBINE_CHOICE, /* choice */
96 XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
Daniel Veillard6eadf632003-01-23 18:29:16 +000097} xmlRelaxNGCombine;
98
Daniel Veillard4c5cf702003-02-21 15:40:34 +000099typedef enum {
100 XML_RELAXNG_CONTENT_ERROR = -1,
101 XML_RELAXNG_CONTENT_EMPTY = 0,
102 XML_RELAXNG_CONTENT_SIMPLE,
103 XML_RELAXNG_CONTENT_COMPLEX
104} xmlRelaxNGContentType;
105
Daniel Veillard6eadf632003-01-23 18:29:16 +0000106typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
107typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
108
109struct _xmlRelaxNGGrammar {
Daniel Veillard4c004142003-10-07 11:33:24 +0000110 xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
111 xmlRelaxNGGrammarPtr children; /* the children grammar if any */
112 xmlRelaxNGGrammarPtr next; /* the next grammar if any */
113 xmlRelaxNGDefinePtr start; /* <start> content */
114 xmlRelaxNGCombine combine; /* the default combine value */
115 xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
116 xmlHashTablePtr defs; /* define* */
117 xmlHashTablePtr refs; /* references */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000118};
119
120
Daniel Veillard6eadf632003-01-23 18:29:16 +0000121typedef enum {
Daniel Veillard4c004142003-10-07 11:33:24 +0000122 XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
123 XML_RELAXNG_EMPTY = 0, /* an empty pattern */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000124 XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
Daniel Veillard4c004142003-10-07 11:33:24 +0000125 XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
126 XML_RELAXNG_TEXT, /* textual content */
127 XML_RELAXNG_ELEMENT, /* an element */
128 XML_RELAXNG_DATATYPE, /* extenal data type definition */
129 XML_RELAXNG_PARAM, /* extenal data type parameter */
130 XML_RELAXNG_VALUE, /* value from an extenal data type definition */
131 XML_RELAXNG_LIST, /* a list of patterns */
132 XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
133 XML_RELAXNG_DEF, /* a definition */
134 XML_RELAXNG_REF, /* reference to a definition */
135 XML_RELAXNG_EXTERNALREF, /* reference to an external def */
136 XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
137 XML_RELAXNG_OPTIONAL, /* optional patterns */
138 XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
139 XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
140 XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
141 XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
142 XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
143 XML_RELAXNG_START /* Used to keep track of starts on grammars */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000144} xmlRelaxNGType;
145
Daniel Veillard52b48c72003-04-13 19:53:42 +0000146#define IS_NULLABLE (1 << 0)
147#define IS_NOT_NULLABLE (1 << 1)
148#define IS_INDETERMINIST (1 << 2)
149#define IS_MIXED (1 << 3)
150#define IS_TRIABLE (1 << 4)
151#define IS_PROCESSED (1 << 5)
152#define IS_COMPILABLE (1 << 6)
153#define IS_NOT_COMPILABLE (1 << 7)
Daniel Veillardaa422d92009-09-24 11:31:48 +0200154#define IS_EXTERNAL_REF (1 << 8)
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000155
Daniel Veillard6eadf632003-01-23 18:29:16 +0000156struct _xmlRelaxNGDefine {
Daniel Veillard4c004142003-10-07 11:33:24 +0000157 xmlRelaxNGType type; /* the type of definition */
158 xmlNodePtr node; /* the node in the source */
159 xmlChar *name; /* the element local name if present */
160 xmlChar *ns; /* the namespace local name if present */
161 xmlChar *value; /* value when available */
162 void *data; /* data lib or specific pointer */
163 xmlRelaxNGDefinePtr content; /* the expected content */
164 xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
165 xmlRelaxNGDefinePtr next; /* list within grouping sequences */
166 xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
167 xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
168 xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
169 short depth; /* used for the cycle detection */
170 short dflags; /* define related flags */
171 xmlRegexpPtr contModel; /* a compiled content model if available */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000172};
173
174/**
175 * _xmlRelaxNG:
176 *
177 * A RelaxNGs definition
178 */
179struct _xmlRelaxNG {
Daniel Veillard4c004142003-10-07 11:33:24 +0000180 void *_private; /* unused by the library for users or bindings */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000181 xmlRelaxNGGrammarPtr topgrammar;
182 xmlDocPtr doc;
183
Daniel Veillard4c004142003-10-07 11:33:24 +0000184 int idref; /* requires idref checking */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000185
Daniel Veillard4c004142003-10-07 11:33:24 +0000186 xmlHashTablePtr defs; /* define */
187 xmlHashTablePtr refs; /* references */
188 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
189 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
190 int defNr; /* number of defines used */
191 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000192
Daniel Veillard6eadf632003-01-23 18:29:16 +0000193};
194
Daniel Veillard77648bb2003-02-20 15:03:22 +0000195#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
196#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
197#define XML_RELAXNG_IN_LIST (1 << 2)
198#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
199#define XML_RELAXNG_IN_START (1 << 4)
200#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
201#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
202#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
Daniel Veillardc5312d72003-02-21 17:14:10 +0000203#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
204#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000205
206struct _xmlRelaxNGParserCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000207 void *userData; /* user specific data block */
208 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
209 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000210 xmlStructuredErrorFunc serror;
Daniel Veillard42f12e92003-03-07 18:32:59 +0000211 xmlRelaxNGValidErr err;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000212
Daniel Veillard4c004142003-10-07 11:33:24 +0000213 xmlRelaxNGPtr schema; /* The schema in use */
214 xmlRelaxNGGrammarPtr grammar; /* the current grammar */
215 xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
216 int flags; /* parser flags */
217 int nbErrors; /* number of errors at parse time */
218 int nbWarnings; /* number of warnings at parse time */
219 const xmlChar *define; /* the current define scope */
220 xmlRelaxNGDefinePtr def; /* the current define */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000221
Daniel Veillard4c004142003-10-07 11:33:24 +0000222 int nbInterleaves;
223 xmlHashTablePtr interleaves; /* keep track of all the interleaves */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000224
Daniel Veillard4c004142003-10-07 11:33:24 +0000225 xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
226 xmlRelaxNGIncludePtr includes; /* all the includes loaded */
227 xmlChar *URL;
228 xmlDocPtr document;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000229
Daniel Veillard4c004142003-10-07 11:33:24 +0000230 int defNr; /* number of defines used */
231 int defMax; /* number of defines aloocated */
232 xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
Daniel Veillard419a7682003-02-03 23:22:49 +0000233
Daniel Veillard4c004142003-10-07 11:33:24 +0000234 const char *buffer;
235 int size;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000236
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000237 /* the document stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000238 xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
239 int docNr; /* Depth of the parsing stack */
240 int docMax; /* Max depth of the parsing stack */
241 xmlRelaxNGDocumentPtr *docTab; /* array of docs */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000242
243 /* the include stack */
Daniel Veillard4c004142003-10-07 11:33:24 +0000244 xmlRelaxNGIncludePtr inc; /* Current parsed include */
245 int incNr; /* Depth of the include parsing stack */
246 int incMax; /* Max depth of the parsing stack */
247 xmlRelaxNGIncludePtr *incTab; /* array of incs */
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000248
Daniel Veillard4c004142003-10-07 11:33:24 +0000249 int idref; /* requires idref checking */
Daniel Veillard52b48c72003-04-13 19:53:42 +0000250
251 /* used to compile content models */
Daniel Veillard4c004142003-10-07 11:33:24 +0000252 xmlAutomataPtr am; /* the automata */
253 xmlAutomataStatePtr state; /* used to build the automata */
Daniel Veillard03c2f0a2004-01-25 19:54:59 +0000254
255 int crng; /* compact syntax and other flags */
Daniel Veillard42595322004-11-08 10:52:06 +0000256 int freedoc; /* need to free the document */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000257};
258
259#define FLAGS_IGNORABLE 1
260#define FLAGS_NEGATIVE 2
Daniel Veillard249d7bb2003-03-19 21:02:29 +0000261#define FLAGS_MIXED_CONTENT 4
Daniel Veillardb30ca312005-09-04 13:50:03 +0000262#define FLAGS_NOERROR 8
Daniel Veillard6eadf632003-01-23 18:29:16 +0000263
264/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000265 * xmlRelaxNGInterleaveGroup:
266 *
267 * A RelaxNGs partition set associated to lists of definitions
268 */
269typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
270typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
271struct _xmlRelaxNGInterleaveGroup {
Daniel Veillard4c004142003-10-07 11:33:24 +0000272 xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
273 xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
274 xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000275};
276
Daniel Veillardbbb78b52003-03-21 01:24:45 +0000277#define IS_DETERMINIST 1
278#define IS_NEEDCHECK 2
Daniel Veillard4c004142003-10-07 11:33:24 +0000279
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000280/**
281 * xmlRelaxNGPartitions:
282 *
283 * A RelaxNGs partition associated to an interleave group
284 */
285typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
286typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
287struct _xmlRelaxNGPartition {
Daniel Veillard4c004142003-10-07 11:33:24 +0000288 int nbgroups; /* number of groups in the partitions */
289 xmlHashTablePtr triage; /* hash table used to direct nodes to the
290 * right group when possible */
291 int flags; /* determinist ? */
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000292 xmlRelaxNGInterleaveGroupPtr *groups;
293};
294
295/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000296 * xmlRelaxNGValidState:
297 *
298 * A RelaxNGs validation state
299 */
300#define MAX_ATTR 20
301typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
302typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
303struct _xmlRelaxNGValidState {
Daniel Veillard4c004142003-10-07 11:33:24 +0000304 xmlNodePtr node; /* the current node */
305 xmlNodePtr seq; /* the sequence of children left to validate */
306 int nbAttrs; /* the number of attributes */
307 int maxAttrs; /* the size of attrs */
308 int nbAttrLeft; /* the number of attributes left to validate */
309 xmlChar *value; /* the value when operating on string */
310 xmlChar *endvalue; /* the end value when operating on string */
311 xmlAttrPtr *attrs; /* the array of attributes */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000312};
313
314/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000315 * xmlRelaxNGStates:
316 *
317 * A RelaxNGs container for validation state
318 */
319typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
320typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
321struct _xmlRelaxNGStates {
Daniel Veillard4c004142003-10-07 11:33:24 +0000322 int nbState; /* the number of states */
323 int maxState; /* the size of the array */
Daniel Veillardfd573f12003-03-16 17:52:32 +0000324 xmlRelaxNGValidStatePtr *tabState;
325};
326
Daniel Veillardc3da18a2003-03-18 00:31:04 +0000327#define ERROR_IS_DUP 1
Daniel Veillard4c004142003-10-07 11:33:24 +0000328
Daniel Veillardfd573f12003-03-16 17:52:32 +0000329/**
Daniel Veillard42f12e92003-03-07 18:32:59 +0000330 * xmlRelaxNGValidError:
331 *
332 * A RelaxNGs validation error
333 */
334typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
335typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
336struct _xmlRelaxNGValidError {
Daniel Veillard4c004142003-10-07 11:33:24 +0000337 xmlRelaxNGValidErr err; /* the error number */
338 int flags; /* flags */
339 xmlNodePtr node; /* the current node */
340 xmlNodePtr seq; /* the current child */
341 const xmlChar *arg1; /* first arg */
342 const xmlChar *arg2; /* second arg */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000343};
344
345/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000346 * xmlRelaxNGValidCtxt:
347 *
348 * A RelaxNGs validation context
349 */
350
351struct _xmlRelaxNGValidCtxt {
Daniel Veillard4c004142003-10-07 11:33:24 +0000352 void *userData; /* user specific data block */
353 xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
354 xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
Daniel Veillard659e71e2003-10-10 14:10:40 +0000355 xmlStructuredErrorFunc serror;
Daniel Veillard4c004142003-10-07 11:33:24 +0000356 int nbErrors; /* number of errors in validation */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000357
Daniel Veillard4c004142003-10-07 11:33:24 +0000358 xmlRelaxNGPtr schema; /* The schema in use */
359 xmlDocPtr doc; /* the document being validated */
360 int flags; /* validation flags */
361 int depth; /* validation depth */
362 int idref; /* requires idref checking */
363 int errNo; /* the first error found */
Daniel Veillard42f12e92003-03-07 18:32:59 +0000364
365 /*
366 * Errors accumulated in branches may have to be stacked to be
367 * provided back when it's sure they affect validation.
368 */
369 xmlRelaxNGValidErrorPtr err; /* Last error */
Daniel Veillard4c004142003-10-07 11:33:24 +0000370 int errNr; /* Depth of the error stack */
371 int errMax; /* Max depth of the error stack */
372 xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
Daniel Veillard1564e6e2003-03-15 21:30:25 +0000373
Daniel Veillard4c004142003-10-07 11:33:24 +0000374 xmlRelaxNGValidStatePtr state; /* the current validation state */
375 xmlRelaxNGStatesPtr states; /* the accumulated state list */
Daniel Veillard798024a2003-03-19 10:36:09 +0000376
Daniel Veillard4c004142003-10-07 11:33:24 +0000377 xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
378 int freeStatesNr;
379 int freeStatesMax;
380 xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
Daniel Veillardf4e55762003-04-15 23:32:22 +0000381
382 /*
383 * This is used for "progressive" validation
384 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000385 xmlRegExecCtxtPtr elem; /* the current element regexp */
386 int elemNr; /* the number of element validated */
387 int elemMax; /* the max depth of elements */
388 xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
389 int pstate; /* progressive state */
390 xmlNodePtr pnode; /* the current node */
391 xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
392 int perr; /* signal error in content model
393 * outside the regexp */
Daniel Veillard6eadf632003-01-23 18:29:16 +0000394};
395
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000396/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000397 * xmlRelaxNGInclude:
398 *
399 * Structure associated to a RelaxNGs document element
400 */
401struct _xmlRelaxNGInclude {
Daniel Veillard4c004142003-10-07 11:33:24 +0000402 xmlRelaxNGIncludePtr next; /* keep a chain of includes */
403 xmlChar *href; /* the normalized href value */
404 xmlDocPtr doc; /* the associated XML document */
405 xmlRelaxNGDefinePtr content; /* the definitions */
406 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000407};
408
409/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000410 * xmlRelaxNGDocument:
411 *
412 * Structure associated to a RelaxNGs document element
413 */
414struct _xmlRelaxNGDocument {
Daniel Veillardc482e262003-02-26 14:48:48 +0000415 xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
Daniel Veillard4c004142003-10-07 11:33:24 +0000416 xmlChar *href; /* the normalized href value */
417 xmlDocPtr doc; /* the associated XML document */
418 xmlRelaxNGDefinePtr content; /* the definitions */
419 xmlRelaxNGPtr schema; /* the schema */
Daniel Veillard81c51e12009-08-14 18:52:10 +0200420 int externalRef; /* 1 if an external ref */
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000421};
422
Daniel Veillard3ebc7d42003-02-24 17:17:58 +0000423
Daniel Veillard6eadf632003-01-23 18:29:16 +0000424/************************************************************************
Daniel Veillard4c004142003-10-07 11:33:24 +0000425 * *
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800426 * Some factorized error routines *
Daniel Veillard4c004142003-10-07 11:33:24 +0000427 * *
428 ************************************************************************/
429
430/**
431 * xmlRngPErrMemory:
432 * @ctxt: an Relax-NG parser context
433 * @extra: extra informations
434 *
435 * Handle a redefinition of attribute error
436 */
437static void
438xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
439{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000440 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000441 xmlGenericErrorFunc channel = NULL;
442 void *data = NULL;
443
444 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000445 if (ctxt->serror != NULL)
446 schannel = ctxt->serror;
447 else
448 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000449 data = ctxt->userData;
450 ctxt->nbErrors++;
451 }
452 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000453 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000454 NULL, NULL, XML_FROM_RELAXNGP,
455 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
456 NULL, NULL, 0, 0,
457 "Memory allocation failed : %s\n", extra);
458 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000459 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000460 NULL, NULL, XML_FROM_RELAXNGP,
461 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
462 NULL, NULL, 0, 0, "Memory allocation failed\n");
463}
464
465/**
466 * xmlRngVErrMemory:
467 * @ctxt: a Relax-NG validation context
468 * @extra: extra informations
469 *
470 * Handle a redefinition of attribute error
471 */
472static void
473xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
474{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000475 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000476 xmlGenericErrorFunc channel = NULL;
477 void *data = NULL;
478
479 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000480 if (ctxt->serror != NULL)
481 schannel = ctxt->serror;
482 else
483 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000484 data = ctxt->userData;
485 ctxt->nbErrors++;
486 }
487 if (extra)
Daniel Veillard659e71e2003-10-10 14:10:40 +0000488 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000489 NULL, NULL, XML_FROM_RELAXNGV,
490 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
491 NULL, NULL, 0, 0,
492 "Memory allocation failed : %s\n", extra);
493 else
Daniel Veillard659e71e2003-10-10 14:10:40 +0000494 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000495 NULL, NULL, XML_FROM_RELAXNGV,
496 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
497 NULL, NULL, 0, 0, "Memory allocation failed\n");
498}
499
500/**
501 * xmlRngPErr:
502 * @ctxt: a Relax-NG parser context
503 * @node: the node raising the error
504 * @error: the error code
505 * @msg: message
506 * @str1: extra info
507 * @str2: extra info
508 *
509 * Handle a Relax NG Parsing error
510 */
David Kilzer4472c3a2016-05-13 15:13:17 +0800511static void LIBXML_ATTR_FORMAT(4,0)
Daniel Veillard4c004142003-10-07 11:33:24 +0000512xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
513 const char *msg, const xmlChar * str1, const xmlChar * str2)
514{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000515 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000516 xmlGenericErrorFunc channel = NULL;
517 void *data = NULL;
518
519 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000520 if (ctxt->serror != NULL)
521 schannel = ctxt->serror;
522 else
523 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000524 data = ctxt->userData;
525 ctxt->nbErrors++;
526 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000527 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000528 NULL, node, XML_FROM_RELAXNGP,
529 error, XML_ERR_ERROR, NULL, 0,
530 (const char *) str1, (const char *) str2, NULL, 0, 0,
531 msg, str1, str2);
532}
533
534/**
535 * xmlRngVErr:
536 * @ctxt: a Relax-NG validation context
537 * @node: the node raising the error
538 * @error: the error code
539 * @msg: message
540 * @str1: extra info
541 * @str2: extra info
542 *
543 * Handle a Relax NG Validation error
544 */
David Kilzer4472c3a2016-05-13 15:13:17 +0800545static void LIBXML_ATTR_FORMAT(4,0)
Daniel Veillard4c004142003-10-07 11:33:24 +0000546xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
547 const char *msg, const xmlChar * str1, const xmlChar * str2)
548{
Daniel Veillard659e71e2003-10-10 14:10:40 +0000549 xmlStructuredErrorFunc schannel = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +0000550 xmlGenericErrorFunc channel = NULL;
551 void *data = NULL;
552
553 if (ctxt != NULL) {
Daniel Veillardb30ca312005-09-04 13:50:03 +0000554 if (ctxt->serror != NULL)
555 schannel = ctxt->serror;
556 else
557 channel = ctxt->error;
Daniel Veillard4c004142003-10-07 11:33:24 +0000558 data = ctxt->userData;
559 ctxt->nbErrors++;
560 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000561 __xmlRaiseError(schannel, channel, data,
Daniel Veillard4c004142003-10-07 11:33:24 +0000562 NULL, node, XML_FROM_RELAXNGV,
563 error, XML_ERR_ERROR, NULL, 0,
564 (const char *) str1, (const char *) str2, NULL, 0, 0,
565 msg, str1, str2);
566}
567
568/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800569 * *
570 * Preliminary type checking interfaces *
571 * *
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000572 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +0000573
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000574/**
575 * xmlRelaxNGTypeHave:
576 * @data: data needed for the library
577 * @type: the type name
578 * @value: the value to check
579 *
580 * Function provided by a type library to check if a type is exported
581 *
582 * Returns 1 if yes, 0 if no and -1 in case of error.
583 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000584typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000585
586/**
587 * xmlRelaxNGTypeCheck:
588 * @data: data needed for the library
589 * @type: the type name
590 * @value: the value to check
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000591 * @result: place to store the result if needed
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000592 *
593 * Function provided by a type library to check if a value match a type
594 *
595 * Returns 1 if yes, 0 if no and -1 in case of error.
596 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000597typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
598 const xmlChar * value, void **result,
599 xmlNodePtr node);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000600
601/**
602 * xmlRelaxNGFacetCheck:
603 * @data: data needed for the library
604 * @type: the type name
605 * @facet: the facet name
606 * @val: the facet value
607 * @strval: the string value
608 * @value: the value to check
609 *
610 * Function provided by a type library to check a value facet
611 *
612 * Returns 1 if yes, 0 if no and -1 in case of error.
613 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000614typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
615 const xmlChar * facet,
616 const xmlChar * val,
617 const xmlChar * strval, void *value);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000618
619/**
620 * xmlRelaxNGTypeFree:
621 * @data: data needed for the library
622 * @result: the value to free
623 *
624 * Function provided by a type library to free a returned result
625 */
626typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000627
628/**
629 * xmlRelaxNGTypeCompare:
630 * @data: data needed for the library
631 * @type: the type name
632 * @value1: the first value
633 * @value2: the second value
634 *
635 * Function provided by a type library to compare two values accordingly
636 * to a type.
637 *
638 * Returns 1 if yes, 0 if no and -1 in case of error.
639 */
Daniel Veillard4c004142003-10-07 11:33:24 +0000640typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
641 const xmlChar * value1,
642 xmlNodePtr ctxt1,
643 void *comp1,
644 const xmlChar * value2,
645 xmlNodePtr ctxt2);
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000646typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
647typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
648struct _xmlRelaxNGTypeLibrary {
Daniel Veillard4c004142003-10-07 11:33:24 +0000649 const xmlChar *namespace; /* the datatypeLibrary value */
650 void *data; /* data needed for the library */
651 xmlRelaxNGTypeHave have; /* the export function */
652 xmlRelaxNGTypeCheck check; /* the checking function */
653 xmlRelaxNGTypeCompare comp; /* the compare function */
654 xmlRelaxNGFacetCheck facet; /* the facet check function */
655 xmlRelaxNGTypeFree freef; /* the freeing function */
Daniel Veillarddd1655c2003-01-25 18:01:32 +0000656};
657
658/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +0800659 * *
660 * Allocation functions *
661 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +0000662 ************************************************************************/
Daniel Veillard6eadf632003-01-23 18:29:16 +0000663static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
664static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
Daniel Veillard4c004142003-10-07 11:33:24 +0000665static void xmlRelaxNGNormExtSpace(xmlChar * value);
Daniel Veillardc482e262003-02-26 14:48:48 +0000666static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
Daniel Veillard4c004142003-10-07 11:33:24 +0000667static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
668 ATTRIBUTE_UNUSED,
669 xmlRelaxNGValidStatePtr state1,
670 xmlRelaxNGValidStatePtr state2);
Daniel Veillard798024a2003-03-19 10:36:09 +0000671static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +0000672 xmlRelaxNGValidStatePtr state);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000673
674/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000675 * xmlRelaxNGFreeDocument:
676 * @docu: a document structure
677 *
678 * Deallocate a RelaxNG document structure.
679 */
680static void
681xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
682{
683 if (docu == NULL)
684 return;
685
686 if (docu->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000687 xmlFree(docu->href);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000688 if (docu->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000689 xmlFreeDoc(docu->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000690 if (docu->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000691 xmlRelaxNGFreeInnerSchema(docu->schema);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000692 xmlFree(docu);
693}
694
695/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000696 * xmlRelaxNGFreeDocumentList:
697 * @docu: a list of document structure
698 *
699 * Deallocate a RelaxNG document structures.
700 */
701static void
702xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
703{
704 xmlRelaxNGDocumentPtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000705
Daniel Veillardc482e262003-02-26 14:48:48 +0000706 while (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000707 next = docu->next;
708 xmlRelaxNGFreeDocument(docu);
709 docu = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000710 }
711}
712
713/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000714 * xmlRelaxNGFreeInclude:
715 * @incl: a include structure
716 *
717 * Deallocate a RelaxNG include structure.
718 */
719static void
720xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
721{
722 if (incl == NULL)
723 return;
724
725 if (incl->href != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000726 xmlFree(incl->href);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000727 if (incl->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000728 xmlFreeDoc(incl->doc);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000729 if (incl->schema != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000730 xmlRelaxNGFree(incl->schema);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000731 xmlFree(incl);
732}
733
734/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000735 * xmlRelaxNGFreeIncludeList:
736 * @incl: a include structure list
737 *
738 * Deallocate a RelaxNG include structure.
739 */
740static void
741xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
742{
743 xmlRelaxNGIncludePtr next;
Daniel Veillard4c004142003-10-07 11:33:24 +0000744
Daniel Veillardc482e262003-02-26 14:48:48 +0000745 while (incl != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000746 next = incl->next;
747 xmlRelaxNGFreeInclude(incl);
748 incl = next;
Daniel Veillardc482e262003-02-26 14:48:48 +0000749 }
750}
751
752/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000753 * xmlRelaxNGNewRelaxNG:
754 * @ctxt: a Relax-NG validation context (optional)
755 *
756 * Allocate a new RelaxNG structure.
757 *
758 * Returns the newly allocated structure or NULL in case or error
759 */
760static xmlRelaxNGPtr
761xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
762{
763 xmlRelaxNGPtr ret;
764
765 ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
766 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000767 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000768 return (NULL);
769 }
770 memset(ret, 0, sizeof(xmlRelaxNG));
771
772 return (ret);
773}
774
775/**
Daniel Veillardc482e262003-02-26 14:48:48 +0000776 * xmlRelaxNGFreeInnerSchema:
777 * @schema: a schema structure
778 *
779 * Deallocate a RelaxNG schema structure.
780 */
781static void
782xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
783{
784 if (schema == NULL)
785 return;
786
787 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000788 xmlFreeDoc(schema->doc);
Daniel Veillardc482e262003-02-26 14:48:48 +0000789 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000790 int i;
Daniel Veillardc482e262003-02-26 14:48:48 +0000791
Daniel Veillard4c004142003-10-07 11:33:24 +0000792 for (i = 0; i < schema->defNr; i++)
793 xmlRelaxNGFreeDefine(schema->defTab[i]);
794 xmlFree(schema->defTab);
Daniel Veillardc482e262003-02-26 14:48:48 +0000795 }
796
797 xmlFree(schema);
798}
799
800/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000801 * xmlRelaxNGFree:
802 * @schema: a schema structure
803 *
804 * Deallocate a RelaxNG structure.
805 */
806void
807xmlRelaxNGFree(xmlRelaxNGPtr schema)
808{
809 if (schema == NULL)
810 return;
811
Daniel Veillard6eadf632003-01-23 18:29:16 +0000812 if (schema->topgrammar != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000813 xmlRelaxNGFreeGrammar(schema->topgrammar);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000814 if (schema->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000815 xmlFreeDoc(schema->doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +0000816 if (schema->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000817 xmlRelaxNGFreeDocumentList(schema->documents);
Daniel Veillarda9d912d2003-02-01 17:43:10 +0000818 if (schema->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000819 xmlRelaxNGFreeIncludeList(schema->includes);
Daniel Veillard419a7682003-02-03 23:22:49 +0000820 if (schema->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000821 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +0000822
Daniel Veillard4c004142003-10-07 11:33:24 +0000823 for (i = 0; i < schema->defNr; i++)
824 xmlRelaxNGFreeDefine(schema->defTab[i]);
825 xmlFree(schema->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +0000826 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000827
828 xmlFree(schema);
829}
830
831/**
832 * xmlRelaxNGNewGrammar:
833 * @ctxt: a Relax-NG validation context (optional)
834 *
835 * Allocate a new RelaxNG grammar.
836 *
837 * Returns the newly allocated structure or NULL in case or error
838 */
839static xmlRelaxNGGrammarPtr
840xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
841{
842 xmlRelaxNGGrammarPtr ret;
843
844 ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
845 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000846 xmlRngPErrMemory(ctxt, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000847 return (NULL);
848 }
849 memset(ret, 0, sizeof(xmlRelaxNGGrammar));
850
851 return (ret);
852}
853
854/**
855 * xmlRelaxNGFreeGrammar:
856 * @grammar: a grammar structure
857 *
858 * Deallocate a RelaxNG grammar structure.
859 */
860static void
861xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
862{
863 if (grammar == NULL)
864 return;
865
Daniel Veillardc482e262003-02-26 14:48:48 +0000866 if (grammar->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000867 xmlRelaxNGFreeGrammar(grammar->children);
Daniel Veillardc482e262003-02-26 14:48:48 +0000868 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000869 if (grammar->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000870 xmlRelaxNGFreeGrammar(grammar->next);
Daniel Veillard419a7682003-02-03 23:22:49 +0000871 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000872 if (grammar->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000873 xmlHashFree(grammar->refs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000874 }
875 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000876 xmlHashFree(grammar->defs, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000877 }
878
879 xmlFree(grammar);
880}
881
882/**
883 * xmlRelaxNGNewDefine:
884 * @ctxt: a Relax-NG validation context
885 * @node: the node in the input document.
886 *
887 * Allocate a new RelaxNG define.
888 *
889 * Returns the newly allocated structure or NULL in case or error
890 */
891static xmlRelaxNGDefinePtr
Daniel Veillardfd573f12003-03-16 17:52:32 +0000892xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
Daniel Veillard6eadf632003-01-23 18:29:16 +0000893{
894 xmlRelaxNGDefinePtr ret;
895
Daniel Veillard419a7682003-02-03 23:22:49 +0000896 if (ctxt->defMax == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000897 ctxt->defMax = 16;
898 ctxt->defNr = 0;
899 ctxt->defTab = (xmlRelaxNGDefinePtr *)
900 xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
901 if (ctxt->defTab == NULL) {
902 xmlRngPErrMemory(ctxt, "allocating define\n");
903 return (NULL);
904 }
Daniel Veillard419a7682003-02-03 23:22:49 +0000905 } else if (ctxt->defMax <= ctxt->defNr) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000906 xmlRelaxNGDefinePtr *tmp;
907
908 ctxt->defMax *= 2;
909 tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
910 ctxt->defMax *
911 sizeof
912 (xmlRelaxNGDefinePtr));
913 if (tmp == NULL) {
914 xmlRngPErrMemory(ctxt, "allocating define\n");
915 return (NULL);
916 }
917 ctxt->defTab = tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +0000918 }
Daniel Veillard6eadf632003-01-23 18:29:16 +0000919 ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
920 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000921 xmlRngPErrMemory(ctxt, "allocating define\n");
922 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000923 }
924 memset(ret, 0, sizeof(xmlRelaxNGDefine));
Daniel Veillard419a7682003-02-03 23:22:49 +0000925 ctxt->defTab[ctxt->defNr++] = ret;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000926 ret->node = node;
Daniel Veillardd4310742003-02-18 21:12:46 +0000927 ret->depth = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +0000928 return (ret);
929}
930
931/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000932 * xmlRelaxNGFreePartition:
933 * @partitions: a partition set structure
934 *
935 * Deallocate RelaxNG partition set structures.
936 */
937static void
Daniel Veillard4c004142003-10-07 11:33:24 +0000938xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
939{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000940 xmlRelaxNGInterleaveGroupPtr group;
941 int j;
942
943 if (partitions != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +0000944 if (partitions->groups != NULL) {
945 for (j = 0; j < partitions->nbgroups; j++) {
946 group = partitions->groups[j];
947 if (group != NULL) {
948 if (group->defs != NULL)
949 xmlFree(group->defs);
950 if (group->attrs != NULL)
951 xmlFree(group->attrs);
952 xmlFree(group);
953 }
954 }
955 xmlFree(partitions->groups);
956 }
957 if (partitions->triage != NULL) {
958 xmlHashFree(partitions->triage, NULL);
959 }
960 xmlFree(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000961 }
962}
Daniel Veillard4c004142003-10-07 11:33:24 +0000963
Daniel Veillard76fc5ed2003-01-28 20:58:15 +0000964/**
Daniel Veillard6eadf632003-01-23 18:29:16 +0000965 * xmlRelaxNGFreeDefine:
966 * @define: a define structure
967 *
968 * Deallocate a RelaxNG define structure.
969 */
970static void
971xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
972{
973 if (define == NULL)
974 return;
975
Daniel Veillard4c004142003-10-07 11:33:24 +0000976 if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
977 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000978
Daniel Veillard4c004142003-10-07 11:33:24 +0000979 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
980 if ((lib != NULL) && (lib->freef != NULL))
981 lib->freef(lib->data, (void *) define->attrs);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000982 }
Daniel Veillard4c004142003-10-07 11:33:24 +0000983 if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
984 xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
985 if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
986 xmlHashFree((xmlHashTablePtr) define->data, NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000987 if (define->name != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000988 xmlFree(define->name);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000989 if (define->ns != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000990 xmlFree(define->ns);
Daniel Veillardedc91922003-01-26 00:52:04 +0000991 if (define->value != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +0000992 xmlFree(define->value);
Daniel Veillard52b48c72003-04-13 19:53:42 +0000993 if (define->contModel != NULL)
994 xmlRegFreeRegexp(define->contModel);
Daniel Veillard6eadf632003-01-23 18:29:16 +0000995 xmlFree(define);
996}
997
998/**
Daniel Veillardfd573f12003-03-16 17:52:32 +0000999 * xmlRelaxNGNewStates:
1000 * @ctxt: a Relax-NG validation context
1001 * @size: the default size for the container
1002 *
1003 * Allocate a new RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001004 *
1005 * Returns the newly allocated structure or NULL in case or error
1006 */
1007static xmlRelaxNGStatesPtr
1008xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1009{
1010 xmlRelaxNGStatesPtr ret;
1011
Daniel Veillard798024a2003-03-19 10:36:09 +00001012 if ((ctxt != NULL) &&
Daniel Veillard9fcd4622009-08-14 16:16:31 +02001013 (ctxt->freeStates != NULL) && (ctxt->freeStatesNr > 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001014 ctxt->freeStatesNr--;
1015 ret = ctxt->freeStates[ctxt->freeStatesNr];
1016 ret->nbState = 0;
1017 return (ret);
Daniel Veillard798024a2003-03-19 10:36:09 +00001018 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001019 if (size < 16)
1020 size = 16;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001021
1022 ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
Daniel Veillard4c004142003-10-07 11:33:24 +00001023 (size -
1024 1) *
1025 sizeof(xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001026 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001027 xmlRngVErrMemory(ctxt, "allocating states\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00001028 return (NULL);
1029 }
1030 ret->nbState = 0;
1031 ret->maxState = size;
Daniel Veillard4c004142003-10-07 11:33:24 +00001032 ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1033 sizeof
1034 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001035 if (ret->tabState == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001036 xmlRngVErrMemory(ctxt, "allocating states\n");
1037 xmlFree(ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001038 return (NULL);
1039 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001040 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001041}
1042
1043/**
Daniel Veillard798024a2003-03-19 10:36:09 +00001044 * xmlRelaxNGAddStateUniq:
1045 * @ctxt: a Relax-NG validation context
1046 * @states: the states container
1047 * @state: the validation state
1048 *
1049 * Add a RelaxNG validation state to the container without checking
1050 * for unicity.
1051 *
1052 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1053 */
1054static int
1055xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001056 xmlRelaxNGStatesPtr states,
1057 xmlRelaxNGValidStatePtr state)
Daniel Veillard798024a2003-03-19 10:36:09 +00001058{
1059 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001060 return (-1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001061 }
1062 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001063 xmlRelaxNGValidStatePtr *tmp;
1064 int size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001065
Daniel Veillard4c004142003-10-07 11:33:24 +00001066 size = states->maxState * 2;
1067 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1068 (size) *
1069 sizeof
1070 (xmlRelaxNGValidStatePtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001071 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001072 xmlRngVErrMemory(ctxt, "adding states\n");
1073 return (-1);
1074 }
1075 states->tabState = tmp;
1076 states->maxState = size;
Daniel Veillard798024a2003-03-19 10:36:09 +00001077 }
1078 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001079 return (1);
Daniel Veillard798024a2003-03-19 10:36:09 +00001080}
1081
1082/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001083 * xmlRelaxNGAddState:
1084 * @ctxt: a Relax-NG validation context
1085 * @states: the states container
1086 * @state: the validation state
1087 *
1088 * Add a RelaxNG validation state to the container
1089 *
1090 * Return 1 in case of success and 0 if this is a duplicate and -1 on error
1091 */
1092static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001093xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1094 xmlRelaxNGStatesPtr states,
1095 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001096{
1097 int i;
1098
Gaurav Gupta7d2e8c92014-07-14 16:08:28 +08001099 if (state == NULL || states == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001100 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001101 }
1102 if (states->nbState >= states->maxState) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001103 xmlRelaxNGValidStatePtr *tmp;
1104 int size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001105
Daniel Veillard4c004142003-10-07 11:33:24 +00001106 size = states->maxState * 2;
1107 tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1108 (size) *
1109 sizeof
1110 (xmlRelaxNGValidStatePtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001111 if (tmp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001112 xmlRngVErrMemory(ctxt, "adding states\n");
1113 return (-1);
1114 }
1115 states->tabState = tmp;
1116 states->maxState = size;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001117 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001118 for (i = 0; i < states->nbState; i++) {
1119 if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1120 xmlRelaxNGFreeValidState(ctxt, state);
1121 return (0);
1122 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00001123 }
1124 states->tabState[states->nbState++] = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00001125 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001126}
1127
1128/**
1129 * xmlRelaxNGFreeStates:
1130 * @ctxt: a Relax-NG validation context
1131 * @states: teh container
1132 *
1133 * Free a RelaxNG validation state container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001134 */
1135static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001136xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001137 xmlRelaxNGStatesPtr states)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001138{
Daniel Veillard798024a2003-03-19 10:36:09 +00001139 if (states == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001140 return;
Daniel Veillard798024a2003-03-19 10:36:09 +00001141 if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001142 ctxt->freeStatesMax = 40;
1143 ctxt->freeStatesNr = 0;
1144 ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1145 xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1146 if (ctxt->freeStates == NULL) {
1147 xmlRngVErrMemory(ctxt, "storing states\n");
1148 }
1149 } else if ((ctxt != NULL)
1150 && (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1151 xmlRelaxNGStatesPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001152
Daniel Veillard4c004142003-10-07 11:33:24 +00001153 tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1154 2 * ctxt->freeStatesMax *
1155 sizeof
1156 (xmlRelaxNGStatesPtr));
1157 if (tmp == NULL) {
1158 xmlRngVErrMemory(ctxt, "storing states\n");
1159 xmlFree(states->tabState);
1160 xmlFree(states);
1161 return;
1162 }
1163 ctxt->freeStates = tmp;
1164 ctxt->freeStatesMax *= 2;
Daniel Veillard798024a2003-03-19 10:36:09 +00001165 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001166 if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001167 xmlFree(states->tabState);
1168 xmlFree(states);
Daniel Veillard798024a2003-03-19 10:36:09 +00001169 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001170 ctxt->freeStates[ctxt->freeStatesNr++] = states;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001171 }
1172}
1173
1174/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001175 * xmlRelaxNGNewValidState:
1176 * @ctxt: a Relax-NG validation context
1177 * @node: the current node or NULL for the document
1178 *
1179 * Allocate a new RelaxNG validation state
1180 *
1181 * Returns the newly allocated structure or NULL in case or error
1182 */
1183static xmlRelaxNGValidStatePtr
1184xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1185{
1186 xmlRelaxNGValidStatePtr ret;
1187 xmlAttrPtr attr;
1188 xmlAttrPtr attrs[MAX_ATTR];
1189 int nbAttrs = 0;
1190 xmlNodePtr root = NULL;
1191
1192 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001193 root = xmlDocGetRootElement(ctxt->doc);
1194 if (root == NULL)
1195 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00001196 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001197 attr = node->properties;
1198 while (attr != NULL) {
1199 if (nbAttrs < MAX_ATTR)
1200 attrs[nbAttrs++] = attr;
1201 else
1202 nbAttrs++;
1203 attr = attr->next;
1204 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001205 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001206 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1207 ctxt->freeState->nbState--;
1208 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001209 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001210 ret =
1211 (xmlRelaxNGValidStatePtr)
1212 xmlMalloc(sizeof(xmlRelaxNGValidState));
1213 if (ret == NULL) {
1214 xmlRngVErrMemory(ctxt, "allocating states\n");
1215 return (NULL);
1216 }
1217 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillard6eadf632003-01-23 18:29:16 +00001218 }
Daniel Veillarde5b110b2003-02-04 14:43:39 +00001219 ret->value = NULL;
1220 ret->endvalue = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001221 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001222 ret->node = (xmlNodePtr) ctxt->doc;
1223 ret->seq = root;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001224 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001225 ret->node = node;
1226 ret->seq = node->children;
Daniel Veillard798024a2003-03-19 10:36:09 +00001227 }
1228 ret->nbAttrs = 0;
1229 if (nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001230 if (ret->attrs == NULL) {
1231 if (nbAttrs < 4)
1232 ret->maxAttrs = 4;
1233 else
1234 ret->maxAttrs = nbAttrs;
1235 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1236 sizeof(xmlAttrPtr));
1237 if (ret->attrs == NULL) {
1238 xmlRngVErrMemory(ctxt, "allocating states\n");
1239 return (ret);
1240 }
1241 } else if (ret->maxAttrs < nbAttrs) {
1242 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001243
Daniel Veillard4c004142003-10-07 11:33:24 +00001244 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1245 sizeof(xmlAttrPtr));
1246 if (tmp == NULL) {
1247 xmlRngVErrMemory(ctxt, "allocating states\n");
1248 return (ret);
1249 }
1250 ret->attrs = tmp;
1251 ret->maxAttrs = nbAttrs;
1252 }
1253 ret->nbAttrs = nbAttrs;
1254 if (nbAttrs < MAX_ATTR) {
1255 memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1256 } else {
1257 attr = node->properties;
1258 nbAttrs = 0;
1259 while (attr != NULL) {
1260 ret->attrs[nbAttrs++] = attr;
1261 attr = attr->next;
1262 }
1263 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001264 }
Daniel Veillard1ed7f362003-02-03 10:57:45 +00001265 ret->nbAttrLeft = ret->nbAttrs;
Daniel Veillard6eadf632003-01-23 18:29:16 +00001266 return (ret);
1267}
1268
1269/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00001270 * xmlRelaxNGCopyValidState:
1271 * @ctxt: a Relax-NG validation context
1272 * @state: a validation state
1273 *
1274 * Copy the validation state
1275 *
1276 * Returns the newly allocated structure or NULL in case or error
1277 */
1278static xmlRelaxNGValidStatePtr
1279xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001280 xmlRelaxNGValidStatePtr state)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001281{
1282 xmlRelaxNGValidStatePtr ret;
Daniel Veillard798024a2003-03-19 10:36:09 +00001283 unsigned int maxAttrs;
1284 xmlAttrPtr *attrs;
Daniel Veillardfd573f12003-03-16 17:52:32 +00001285
1286 if (state == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001287 return (NULL);
1288 if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1289 ctxt->freeState->nbState--;
1290 ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
Daniel Veillard798024a2003-03-19 10:36:09 +00001291 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001292 ret =
1293 (xmlRelaxNGValidStatePtr)
1294 xmlMalloc(sizeof(xmlRelaxNGValidState));
1295 if (ret == NULL) {
1296 xmlRngVErrMemory(ctxt, "allocating states\n");
1297 return (NULL);
1298 }
1299 memset(ret, 0, sizeof(xmlRelaxNGValidState));
Daniel Veillardfd573f12003-03-16 17:52:32 +00001300 }
Daniel Veillard798024a2003-03-19 10:36:09 +00001301 attrs = ret->attrs;
1302 maxAttrs = ret->maxAttrs;
1303 memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1304 ret->attrs = attrs;
1305 ret->maxAttrs = maxAttrs;
1306 if (state->nbAttrs > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001307 if (ret->attrs == NULL) {
1308 ret->maxAttrs = state->maxAttrs;
1309 ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1310 sizeof(xmlAttrPtr));
1311 if (ret->attrs == NULL) {
1312 xmlRngVErrMemory(ctxt, "allocating states\n");
1313 ret->nbAttrs = 0;
1314 return (ret);
1315 }
1316 } else if (ret->maxAttrs < state->nbAttrs) {
1317 xmlAttrPtr *tmp;
Daniel Veillard798024a2003-03-19 10:36:09 +00001318
Daniel Veillard4c004142003-10-07 11:33:24 +00001319 tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1320 sizeof(xmlAttrPtr));
1321 if (tmp == NULL) {
1322 xmlRngVErrMemory(ctxt, "allocating states\n");
1323 ret->nbAttrs = 0;
1324 return (ret);
1325 }
1326 ret->maxAttrs = state->maxAttrs;
1327 ret->attrs = tmp;
1328 }
1329 memcpy(ret->attrs, state->attrs,
1330 state->nbAttrs * sizeof(xmlAttrPtr));
Daniel Veillard798024a2003-03-19 10:36:09 +00001331 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001332 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001333}
1334
1335/**
1336 * xmlRelaxNGEqualValidState:
1337 * @ctxt: a Relax-NG validation context
1338 * @state1: a validation state
1339 * @state2: a validation state
1340 *
1341 * Compare the validation states for equality
1342 *
1343 * Returns 1 if equald, 0 otherwise
1344 */
1345static int
1346xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00001347 xmlRelaxNGValidStatePtr state1,
1348 xmlRelaxNGValidStatePtr state2)
Daniel Veillardfd573f12003-03-16 17:52:32 +00001349{
1350 int i;
1351
1352 if ((state1 == NULL) || (state2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00001353 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001354 if (state1 == state2)
Daniel Veillard4c004142003-10-07 11:33:24 +00001355 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001356 if (state1->node != state2->node)
Daniel Veillard4c004142003-10-07 11:33:24 +00001357 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001358 if (state1->seq != state2->seq)
Daniel Veillard4c004142003-10-07 11:33:24 +00001359 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001360 if (state1->nbAttrLeft != state2->nbAttrLeft)
Daniel Veillard4c004142003-10-07 11:33:24 +00001361 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001362 if (state1->nbAttrs != state2->nbAttrs)
Daniel Veillard4c004142003-10-07 11:33:24 +00001363 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001364 if (state1->endvalue != state2->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00001365 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001366 if ((state1->value != state2->value) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001367 (!xmlStrEqual(state1->value, state2->value)))
1368 return (0);
1369 for (i = 0; i < state1->nbAttrs; i++) {
1370 if (state1->attrs[i] != state2->attrs[i])
1371 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001372 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001373 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00001374}
1375
1376/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00001377 * xmlRelaxNGFreeValidState:
1378 * @state: a validation state structure
1379 *
1380 * Deallocate a RelaxNG validation state structure.
1381 */
1382static void
Daniel Veillard798024a2003-03-19 10:36:09 +00001383xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001384 xmlRelaxNGValidStatePtr state)
Daniel Veillard6eadf632003-01-23 18:29:16 +00001385{
1386 if (state == NULL)
1387 return;
1388
Daniel Veillard798024a2003-03-19 10:36:09 +00001389 if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001390 ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
Daniel Veillard798024a2003-03-19 10:36:09 +00001391 }
1392 if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001393 if (state->attrs != NULL)
1394 xmlFree(state->attrs);
1395 xmlFree(state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001396 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001397 xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
Daniel Veillard798024a2003-03-19 10:36:09 +00001398 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00001399}
1400
1401/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001402 * *
1403 * Semi internal functions *
1404 * *
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001405 ************************************************************************/
1406
1407/**
1408 * xmlRelaxParserSetFlag:
1409 * @ctxt: a RelaxNG parser context
1410 * @flags: a set of flags values
1411 *
1412 * Semi private function used to pass informations to a parser context
1413 * which are a combination of xmlRelaxNGParserFlag .
1414 *
1415 * Returns 0 if success and -1 in case of error
1416 */
1417int
1418xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1419{
1420 if (ctxt == NULL) return(-1);
1421 if (flags & XML_RELAXNGP_FREE_DOC) {
1422 ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1423 flags -= XML_RELAXNGP_FREE_DOC;
1424 }
1425 if (flags & XML_RELAXNGP_CRNG) {
1426 ctxt->crng |= XML_RELAXNGP_CRNG;
1427 flags -= XML_RELAXNGP_CRNG;
1428 }
1429 if (flags != 0) return(-1);
1430 return(0);
1431}
1432
1433/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08001434 * *
1435 * Document functions *
1436 * *
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001437 ************************************************************************/
1438static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001439 xmlDocPtr doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001440
1441/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001442 * xmlRelaxNGIncludePush:
1443 * @ctxt: the parser context
1444 * @value: the element doc
1445 *
1446 * Pushes a new include on top of the include stack
1447 *
1448 * Returns 0 in case of error, the index in the stack otherwise
1449 */
1450static int
1451xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001452 xmlRelaxNGIncludePtr value)
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001453{
1454 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001455 ctxt->incMax = 4;
1456 ctxt->incNr = 0;
1457 ctxt->incTab =
1458 (xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1459 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001460 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001461 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001462 return (0);
1463 }
1464 }
1465 if (ctxt->incNr >= ctxt->incMax) {
1466 ctxt->incMax *= 2;
1467 ctxt->incTab =
1468 (xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001469 ctxt->incMax *
1470 sizeof(ctxt->incTab[0]));
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001471 if (ctxt->incTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001472 xmlRngPErrMemory(ctxt, "allocating include\n");
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001473 return (0);
1474 }
1475 }
1476 ctxt->incTab[ctxt->incNr] = value;
1477 ctxt->inc = value;
1478 return (ctxt->incNr++);
1479}
1480
1481/**
1482 * xmlRelaxNGIncludePop:
1483 * @ctxt: the parser context
1484 *
1485 * Pops the top include from the include stack
1486 *
1487 * Returns the include just removed
1488 */
1489static xmlRelaxNGIncludePtr
1490xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1491{
1492 xmlRelaxNGIncludePtr ret;
1493
1494 if (ctxt->incNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001495 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001496 ctxt->incNr--;
1497 if (ctxt->incNr > 0)
1498 ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1499 else
1500 ctxt->inc = NULL;
1501 ret = ctxt->incTab[ctxt->incNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001502 ctxt->incTab[ctxt->incNr] = NULL;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001503 return (ret);
1504}
1505
1506/**
Daniel Veillard5add8682003-03-10 13:13:58 +00001507 * xmlRelaxNGRemoveRedefine:
1508 * @ctxt: the parser context
1509 * @URL: the normalized URL
1510 * @target: the included target
1511 * @name: the define name to eliminate
1512 *
1513 * Applies the elimination algorithm of 4.7
1514 *
1515 * Returns 0 in case of error, 1 in case of success.
1516 */
1517static int
1518xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001519 const xmlChar * URL ATTRIBUTE_UNUSED,
1520 xmlNodePtr target, const xmlChar * name)
1521{
Daniel Veillard5add8682003-03-10 13:13:58 +00001522 int found = 0;
1523 xmlNodePtr tmp, tmp2;
1524 xmlChar *name2;
1525
1526#ifdef DEBUG_INCLUDE
Daniel Veillard952379b2003-03-17 15:37:12 +00001527 if (name == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00001528 xmlGenericError(xmlGenericErrorContext,
1529 "Elimination of <include> start from %s\n", URL);
Daniel Veillard952379b2003-03-17 15:37:12 +00001530 else
Daniel Veillard4c004142003-10-07 11:33:24 +00001531 xmlGenericError(xmlGenericErrorContext,
1532 "Elimination of <include> define %s from %s\n",
1533 name, URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001534#endif
1535 tmp = target;
1536 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001537 tmp2 = tmp->next;
1538 if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1539 found = 1;
1540 xmlUnlinkNode(tmp);
1541 xmlFreeNode(tmp);
1542 } else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1543 name2 = xmlGetProp(tmp, BAD_CAST "name");
1544 xmlRelaxNGNormExtSpace(name2);
1545 if (name2 != NULL) {
1546 if (xmlStrEqual(name, name2)) {
1547 found = 1;
1548 xmlUnlinkNode(tmp);
1549 xmlFreeNode(tmp);
1550 }
1551 xmlFree(name2);
1552 }
1553 } else if (IS_RELAXNG(tmp, "include")) {
1554 xmlChar *href = NULL;
Daniel Veillard807daf82004-02-22 22:13:27 +00001555 xmlRelaxNGDocumentPtr inc = tmp->psvi;
Daniel Veillard5add8682003-03-10 13:13:58 +00001556
Daniel Veillard4c004142003-10-07 11:33:24 +00001557 if ((inc != NULL) && (inc->doc != NULL) &&
1558 (inc->doc->children != NULL)) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001559
Daniel Veillard4c004142003-10-07 11:33:24 +00001560 if (xmlStrEqual
1561 (inc->doc->children->name, BAD_CAST "grammar")) {
Daniel Veillard5add8682003-03-10 13:13:58 +00001562#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001563 href = xmlGetProp(tmp, BAD_CAST "href");
Daniel Veillard5add8682003-03-10 13:13:58 +00001564#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001565 if (xmlRelaxNGRemoveRedefine(ctxt, href,
Shaun McCanced7eb9b52011-08-04 10:28:59 -04001566 xmlDocGetRootElement(inc->doc)->children,
1567 name) == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001568 found = 1;
1569 }
Daniel Veillard14b56432006-03-09 18:41:40 +00001570#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001571 if (href != NULL)
1572 xmlFree(href);
Daniel Veillard14b56432006-03-09 18:41:40 +00001573#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00001574 }
1575 }
1576 }
1577 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001578 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001579 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001580}
1581
1582/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001583 * xmlRelaxNGLoadInclude:
1584 * @ctxt: the parser context
1585 * @URL: the normalized URL
1586 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001587 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001588 *
1589 * First lookup if the document is already loaded into the parser context,
1590 * check against recursion. If not found the resource is loaded and
1591 * the content is preprocessed before being returned back to the caller.
1592 *
1593 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1594 */
1595static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001596xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1597 xmlNodePtr node, const xmlChar * ns)
1598{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001599 xmlRelaxNGIncludePtr ret = NULL;
1600 xmlDocPtr doc;
1601 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001602 xmlNodePtr root, cur;
1603
1604#ifdef DEBUG_INCLUDE
1605 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001606 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001607#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001608
1609 /*
1610 * check against recursion in the stack
1611 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001612 for (i = 0; i < ctxt->incNr; i++) {
1613 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1614 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1615 "Detected an Include recursion for %s\n", URL,
1616 NULL);
1617 return (NULL);
1618 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001619 }
1620
1621 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001622 * load the document
1623 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001624 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001625 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001626 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1627 "xmlRelaxNG: could not load %s\n", URL, NULL);
1628 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001629 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001630#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001631 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001632#endif
1633
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001634 /*
1635 * Allocate the document structures and register it first.
1636 */
1637 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1638 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001639 xmlRngPErrMemory(ctxt, "allocating include\n");
1640 xmlFreeDoc(doc);
1641 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001642 }
1643 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1644 ret->doc = doc;
1645 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001646 ret->next = ctxt->includes;
1647 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001648
1649 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001650 * transmit the ns if needed
1651 */
1652 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001653 root = xmlDocGetRootElement(doc);
1654 if (root != NULL) {
1655 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1656 xmlSetProp(root, BAD_CAST "ns", ns);
1657 }
1658 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001659 }
1660
1661 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001662 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001663 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001664 xmlRelaxNGIncludePush(ctxt, ret);
1665
1666 /*
1667 * Some preprocessing of the document content, this include recursing
1668 * in the include stack.
1669 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001670#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001671 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001672#endif
1673
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001674 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1675 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001676 ctxt->inc = NULL;
1677 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001678 }
1679
1680 /*
1681 * Pop up the include from the stack
1682 */
1683 xmlRelaxNGIncludePop(ctxt);
1684
Daniel Veillard5add8682003-03-10 13:13:58 +00001685#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001686 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001687#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001688 /*
1689 * Check that the top element is a grammar
1690 */
1691 root = xmlDocGetRootElement(doc);
1692 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001693 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1694 "xmlRelaxNG: included document is empty %s\n", URL,
1695 NULL);
1696 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001697 }
1698 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001699 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1700 "xmlRelaxNG: included document %s root is not a grammar\n",
1701 URL, NULL);
1702 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001703 }
1704
1705 /*
1706 * Elimination of redefined rules in the include.
1707 */
1708 cur = node->children;
1709 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001710 if (IS_RELAXNG(cur, "start")) {
1711 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001712
Daniel Veillard4c004142003-10-07 11:33:24 +00001713 found =
1714 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1715 if (!found) {
1716 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1717 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1718 URL, NULL);
1719 }
1720 } else if (IS_RELAXNG(cur, "define")) {
1721 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001722
Daniel Veillard4c004142003-10-07 11:33:24 +00001723 name = xmlGetProp(cur, BAD_CAST "name");
1724 if (name == NULL) {
1725 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1726 "xmlRelaxNG: include %s has define without name\n",
1727 URL, NULL);
1728 } else {
1729 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001730
Daniel Veillard4c004142003-10-07 11:33:24 +00001731 xmlRelaxNGNormExtSpace(name);
1732 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1733 root->children, name);
1734 if (!found) {
1735 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1736 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1737 URL, name);
1738 }
1739 xmlFree(name);
1740 }
1741 }
1742 cur = cur->next;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001743 }
1744
1745
Daniel Veillard4c004142003-10-07 11:33:24 +00001746 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001747}
1748
1749/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001750 * xmlRelaxNGValidErrorPush:
1751 * @ctxt: the validation context
1752 * @err: the error code
1753 * @arg1: the first string argument
1754 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001755 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001756 *
1757 * Pushes a new error on top of the error stack
1758 *
1759 * Returns 0 in case of error, the index in the stack otherwise
1760 */
1761static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001762xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1763 xmlRelaxNGValidErr err, const xmlChar * arg1,
1764 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001765{
1766 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001767
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001768#ifdef DEBUG_ERROR
1769 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001770 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001771#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001772 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001773 ctxt->errMax = 8;
1774 ctxt->errNr = 0;
1775 ctxt->errTab =
1776 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1777 sizeof
1778 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001779 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001780 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001781 return (0);
1782 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001783 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001784 }
1785 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001786 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001787 ctxt->errTab =
1788 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001789 ctxt->errMax *
1790 sizeof
1791 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001792 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001793 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001794 return (0);
1795 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001796 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001797 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001798 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001799 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1800 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001801 cur = &ctxt->errTab[ctxt->errNr];
1802 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001803 if (dup) {
1804 cur->arg1 = xmlStrdup(arg1);
1805 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001806 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001807 } else {
1808 cur->arg1 = arg1;
1809 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001810 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001811 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001812 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001813 cur->node = ctxt->state->node;
1814 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001815 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001816 cur->node = NULL;
1817 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001818 }
1819 ctxt->err = cur;
1820 return (ctxt->errNr++);
1821}
1822
1823/**
1824 * xmlRelaxNGValidErrorPop:
1825 * @ctxt: the validation context
1826 *
1827 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001828 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001829static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001830xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1831{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001832 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001833
Daniel Veillard580ced82003-03-21 21:22:48 +00001834 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001835 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001836 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001837 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001838 ctxt->errNr--;
1839 if (ctxt->errNr > 0)
1840 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1841 else
1842 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001843 cur = &ctxt->errTab[ctxt->errNr];
1844 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001845 if (cur->arg1 != NULL)
1846 xmlFree((xmlChar *) cur->arg1);
1847 cur->arg1 = NULL;
1848 if (cur->arg2 != NULL)
1849 xmlFree((xmlChar *) cur->arg2);
1850 cur->arg2 = NULL;
1851 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001852 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001853}
1854
Daniel Veillard42f12e92003-03-07 18:32:59 +00001855/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001856 * xmlRelaxNGDocumentPush:
1857 * @ctxt: the parser context
1858 * @value: the element doc
1859 *
1860 * Pushes a new doc on top of the doc stack
1861 *
1862 * Returns 0 in case of error, the index in the stack otherwise
1863 */
1864static int
1865xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001866 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001867{
1868 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001869 ctxt->docMax = 4;
1870 ctxt->docNr = 0;
1871 ctxt->docTab =
1872 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1873 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001874 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001875 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001876 return (0);
1877 }
1878 }
1879 if (ctxt->docNr >= ctxt->docMax) {
1880 ctxt->docMax *= 2;
1881 ctxt->docTab =
1882 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001883 ctxt->docMax *
1884 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001885 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001886 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001887 return (0);
1888 }
1889 }
1890 ctxt->docTab[ctxt->docNr] = value;
1891 ctxt->doc = value;
1892 return (ctxt->docNr++);
1893}
1894
1895/**
1896 * xmlRelaxNGDocumentPop:
1897 * @ctxt: the parser context
1898 *
1899 * Pops the top doc from the doc stack
1900 *
1901 * Returns the doc just removed
1902 */
1903static xmlRelaxNGDocumentPtr
1904xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1905{
1906 xmlRelaxNGDocumentPtr ret;
1907
1908 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001909 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001910 ctxt->docNr--;
1911 if (ctxt->docNr > 0)
1912 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1913 else
1914 ctxt->doc = NULL;
1915 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001916 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001917 return (ret);
1918}
1919
1920/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001921 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001922 * @ctxt: the parser context
1923 * @URL: the normalized URL
1924 * @ns: the inherited ns if any
1925 *
1926 * First lookup if the document is already loaded into the parser context,
1927 * check against recursion. If not found the resource is loaded and
1928 * the content is preprocessed before being returned back to the caller.
1929 *
1930 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1931 */
1932static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001933xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1934 const xmlChar * URL, const xmlChar * ns)
1935{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001936 xmlRelaxNGDocumentPtr ret = NULL;
1937 xmlDocPtr doc;
1938 xmlNodePtr root;
1939 int i;
1940
1941 /*
1942 * check against recursion in the stack
1943 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001944 for (i = 0; i < ctxt->docNr; i++) {
1945 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1946 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1947 "Detected an externalRef recursion for %s\n", URL,
1948 NULL);
1949 return (NULL);
1950 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001951 }
1952
1953 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001954 * load the document
1955 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001956 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001957 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001958 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1959 "xmlRelaxNG: could not load %s\n", URL, NULL);
1960 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001961 }
1962
1963 /*
1964 * Allocate the document structures and register it first.
1965 */
1966 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1967 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001968 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1969 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1970 xmlFreeDoc(doc);
1971 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001972 }
1973 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1974 ret->doc = doc;
1975 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001976 ret->next = ctxt->documents;
Daniel Veillard81c51e12009-08-14 18:52:10 +02001977 ret->externalRef = 1;
Daniel Veillardc482e262003-02-26 14:48:48 +00001978 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001979
1980 /*
1981 * transmit the ns if needed
1982 */
1983 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001984 root = xmlDocGetRootElement(doc);
1985 if (root != NULL) {
1986 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1987 xmlSetProp(root, BAD_CAST "ns", ns);
1988 }
1989 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001990 }
1991
1992 /*
1993 * push it on the stack and register it in the hash table
1994 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001995 xmlRelaxNGDocumentPush(ctxt, ret);
1996
1997 /*
1998 * Some preprocessing of the document content
1999 */
2000 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2001 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002002 ctxt->doc = NULL;
2003 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002004 }
2005
2006 xmlRelaxNGDocumentPop(ctxt);
2007
Daniel Veillard4c004142003-10-07 11:33:24 +00002008 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002009}
2010
2011/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002012 * *
2013 * Error functions *
2014 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002015 ************************************************************************/
2016
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002017#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2018#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2019#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2020#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2021#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002022
Daniel Veillard231d7912003-02-09 14:22:17 +00002023static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002024xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2025{
Daniel Veillard231d7912003-02-09 14:22:17 +00002026 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002027 return ("none");
2028 switch (def->type) {
2029 case XML_RELAXNG_EMPTY:
2030 return ("empty");
2031 case XML_RELAXNG_NOT_ALLOWED:
2032 return ("notAllowed");
2033 case XML_RELAXNG_EXCEPT:
2034 return ("except");
2035 case XML_RELAXNG_TEXT:
2036 return ("text");
2037 case XML_RELAXNG_ELEMENT:
2038 return ("element");
2039 case XML_RELAXNG_DATATYPE:
2040 return ("datatype");
2041 case XML_RELAXNG_VALUE:
2042 return ("value");
2043 case XML_RELAXNG_LIST:
2044 return ("list");
2045 case XML_RELAXNG_ATTRIBUTE:
2046 return ("attribute");
2047 case XML_RELAXNG_DEF:
2048 return ("def");
2049 case XML_RELAXNG_REF:
2050 return ("ref");
2051 case XML_RELAXNG_EXTERNALREF:
2052 return ("externalRef");
2053 case XML_RELAXNG_PARENTREF:
2054 return ("parentRef");
2055 case XML_RELAXNG_OPTIONAL:
2056 return ("optional");
2057 case XML_RELAXNG_ZEROORMORE:
2058 return ("zeroOrMore");
2059 case XML_RELAXNG_ONEORMORE:
2060 return ("oneOrMore");
2061 case XML_RELAXNG_CHOICE:
2062 return ("choice");
2063 case XML_RELAXNG_GROUP:
2064 return ("group");
2065 case XML_RELAXNG_INTERLEAVE:
2066 return ("interleave");
2067 case XML_RELAXNG_START:
2068 return ("start");
2069 case XML_RELAXNG_NOOP:
2070 return ("noop");
2071 case XML_RELAXNG_PARAM:
2072 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002073 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002074 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002075}
Daniel Veillardd2298792003-02-14 16:54:11 +00002076
Daniel Veillard6eadf632003-01-23 18:29:16 +00002077/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002078 * xmlRelaxNGGetErrorString:
2079 * @err: the error code
2080 * @arg1: the first string argument
2081 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002082 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002083 * computes a formatted error string for the given error code and args
2084 *
2085 * Returns the error string, it must be deallocated by the caller
2086 */
2087static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002088xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2089 const xmlChar * arg2)
2090{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002091 char msg[1000];
Chun-wei Fand77e5fc2016-05-31 21:04:50 +08002092 xmlChar *result;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002093
2094 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002095 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002096 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002097 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002098
2099 msg[0] = 0;
2100 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002101 case XML_RELAXNG_OK:
2102 return (NULL);
2103 case XML_RELAXNG_ERR_MEMORY:
2104 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002105 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002106 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2107 break;
2108 case XML_RELAXNG_ERR_TYPEVAL:
2109 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2110 arg2);
2111 break;
2112 case XML_RELAXNG_ERR_DUPID:
2113 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2114 break;
2115 case XML_RELAXNG_ERR_TYPECMP:
2116 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2117 break;
2118 case XML_RELAXNG_ERR_NOSTATE:
2119 return (xmlCharStrdup("Internal error: no state\n"));
2120 case XML_RELAXNG_ERR_NODEFINE:
2121 return (xmlCharStrdup("Internal error: no define\n"));
2122 case XML_RELAXNG_ERR_INTERNAL:
2123 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2124 break;
2125 case XML_RELAXNG_ERR_LISTEXTRA:
2126 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2127 break;
2128 case XML_RELAXNG_ERR_INTERNODATA:
2129 return (xmlCharStrdup
2130 ("Internal: interleave block has no data\n"));
2131 case XML_RELAXNG_ERR_INTERSEQ:
2132 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2133 case XML_RELAXNG_ERR_INTEREXTRA:
2134 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2135 break;
2136 case XML_RELAXNG_ERR_ELEMNAME:
2137 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2138 arg2);
2139 break;
2140 case XML_RELAXNG_ERR_ELEMNONS:
2141 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2142 arg1);
2143 break;
2144 case XML_RELAXNG_ERR_ELEMWRONGNS:
2145 snprintf(msg, 1000,
2146 "Element %s has wrong namespace: expecting %s\n", arg1,
2147 arg2);
2148 break;
2149 case XML_RELAXNG_ERR_ELEMWRONG:
2150 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2151 break;
2152 case XML_RELAXNG_ERR_TEXTWRONG:
2153 snprintf(msg, 1000,
2154 "Did not expect text in element %s content\n", arg1);
2155 break;
2156 case XML_RELAXNG_ERR_ELEMEXTRANS:
2157 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2158 arg1);
2159 break;
2160 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2161 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2162 break;
2163 case XML_RELAXNG_ERR_NOELEM:
2164 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2165 arg1);
2166 break;
2167 case XML_RELAXNG_ERR_NOTELEM:
2168 return (xmlCharStrdup("Expecting an element got text\n"));
2169 case XML_RELAXNG_ERR_ATTRVALID:
2170 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2171 arg1);
2172 break;
2173 case XML_RELAXNG_ERR_CONTENTVALID:
2174 snprintf(msg, 1000, "Element %s failed to validate content\n",
2175 arg1);
2176 break;
2177 case XML_RELAXNG_ERR_EXTRACONTENT:
2178 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2179 arg1, arg2);
2180 break;
2181 case XML_RELAXNG_ERR_INVALIDATTR:
2182 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2183 arg1, arg2);
2184 break;
2185 case XML_RELAXNG_ERR_LACKDATA:
2186 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2187 arg1);
2188 break;
2189 case XML_RELAXNG_ERR_DATAELEM:
2190 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2191 arg1);
2192 break;
2193 case XML_RELAXNG_ERR_VALELEM:
2194 snprintf(msg, 1000, "Value element %s has child elements\n",
2195 arg1);
2196 break;
2197 case XML_RELAXNG_ERR_LISTELEM:
2198 snprintf(msg, 1000, "List element %s has child elements\n",
2199 arg1);
2200 break;
2201 case XML_RELAXNG_ERR_DATATYPE:
2202 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2203 break;
2204 case XML_RELAXNG_ERR_VALUE:
2205 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2206 break;
2207 case XML_RELAXNG_ERR_LIST:
2208 return (xmlCharStrdup("Error validating list\n"));
2209 case XML_RELAXNG_ERR_NOGRAMMAR:
2210 return (xmlCharStrdup("No top grammar defined\n"));
2211 case XML_RELAXNG_ERR_EXTRADATA:
2212 return (xmlCharStrdup("Extra data in the document\n"));
2213 default:
2214 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002215 }
2216 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002217 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002218 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002219 msg[1000 - 1] = 0;
Chun-wei Fand77e5fc2016-05-31 21:04:50 +08002220 result = xmlCharStrdup(msg);
David Kilzer502f6a62016-05-23 14:58:41 +08002221 return (xmlEscapeFormatString(&result));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002222}
2223
2224/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002225 * xmlRelaxNGShowValidError:
2226 * @ctxt: the validation context
2227 * @err: the error number
2228 * @node: the node
2229 * @child: the node child generating the problem.
2230 * @arg1: the first argument
2231 * @arg2: the second argument
2232 *
2233 * Show a validation error.
2234 */
2235static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002236xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2237 xmlRelaxNGValidErr err, xmlNodePtr node,
2238 xmlNodePtr child, const xmlChar * arg1,
2239 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002240{
2241 xmlChar *msg;
2242
Daniel Veillardb30ca312005-09-04 13:50:03 +00002243 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002244 return;
2245
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002246#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002247 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002248#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002249 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2250 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002251 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002252
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002253 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002254 ctxt->errNo = err;
2255 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2256 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002257 xmlFree(msg);
2258}
2259
2260/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002261 * xmlRelaxNGPopErrors:
2262 * @ctxt: the validation context
2263 * @level: the error level in the stack
2264 *
2265 * pop and discard all errors until the given level is reached
2266 */
2267static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002268xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2269{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002270 int i;
2271 xmlRelaxNGValidErrorPtr err;
2272
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002273#ifdef DEBUG_ERROR
2274 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002275 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002276#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002277 for (i = level; i < ctxt->errNr; i++) {
2278 err = &ctxt->errTab[i];
2279 if (err->flags & ERROR_IS_DUP) {
2280 if (err->arg1 != NULL)
2281 xmlFree((xmlChar *) err->arg1);
2282 err->arg1 = NULL;
2283 if (err->arg2 != NULL)
2284 xmlFree((xmlChar *) err->arg2);
2285 err->arg2 = NULL;
2286 err->flags = 0;
2287 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002288 }
2289 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002290 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002291 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002292}
Daniel Veillard4c004142003-10-07 11:33:24 +00002293
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002294/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002295 * xmlRelaxNGDumpValidError:
2296 * @ctxt: the validation context
2297 *
2298 * Show all validation error over a given index.
2299 */
2300static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002301xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2302{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002303 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002304 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002305
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002306#ifdef DEBUG_ERROR
2307 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002308 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002309#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002310 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2311 err = &ctxt->errTab[i];
2312 if (k < MAX_ERROR) {
2313 for (j = 0; j < i; j++) {
2314 dup = &ctxt->errTab[j];
2315 if ((err->err == dup->err) && (err->node == dup->node) &&
2316 (xmlStrEqual(err->arg1, dup->arg1)) &&
2317 (xmlStrEqual(err->arg2, dup->arg2))) {
2318 goto skip;
2319 }
2320 }
2321 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2322 err->arg1, err->arg2);
2323 k++;
2324 }
2325 skip:
2326 if (err->flags & ERROR_IS_DUP) {
2327 if (err->arg1 != NULL)
2328 xmlFree((xmlChar *) err->arg1);
2329 err->arg1 = NULL;
2330 if (err->arg2 != NULL)
2331 xmlFree((xmlChar *) err->arg2);
2332 err->arg2 = NULL;
2333 err->flags = 0;
2334 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002335 }
2336 ctxt->errNr = 0;
2337}
Daniel Veillard4c004142003-10-07 11:33:24 +00002338
Daniel Veillard42f12e92003-03-07 18:32:59 +00002339/**
2340 * xmlRelaxNGAddValidError:
2341 * @ctxt: the validation context
2342 * @err: the error number
2343 * @arg1: the first argument
2344 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002345 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002346 *
2347 * Register a validation error, either generating it if it's sure
2348 * or stacking it for later handling if unsure.
2349 */
2350static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002351xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2352 xmlRelaxNGValidErr err, const xmlChar * arg1,
2353 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002354{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002355 if (ctxt == NULL)
2356 return;
2357 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002358 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002359
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002360#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002361 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002362#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002363 /*
2364 * generate the error directly
2365 */
William M. Brack60929622004-03-27 17:54:18 +00002366 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002367 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002368 xmlNodePtr node, seq;
2369
2370 /*
2371 * Flush first any stacked error which might be the
2372 * real cause of the problem.
2373 */
2374 if (ctxt->errNr != 0)
2375 xmlRelaxNGDumpValidError(ctxt);
2376 if (ctxt->state != NULL) {
2377 node = ctxt->state->node;
2378 seq = ctxt->state->seq;
2379 } else {
2380 node = seq = NULL;
2381 }
Daniel Veillardec18c962009-08-26 18:37:43 +02002382 if ((node == NULL) && (seq == NULL)) {
2383 node = ctxt->pnode;
2384 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002385 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002386 }
2387 /*
2388 * Stack the error for later processing if needed
2389 */
2390 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002391 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002392 }
2393}
2394
Daniel Veillard6eadf632003-01-23 18:29:16 +00002395
2396/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002397 * *
2398 * Type library hooks *
2399 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002400 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002401static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002402 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002403
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002404/**
2405 * xmlRelaxNGSchemaTypeHave:
2406 * @data: data needed for the library
2407 * @type: the type name
2408 *
2409 * Check if the given type is provided by
2410 * the W3C XMLSchema Datatype library.
2411 *
2412 * Returns 1 if yes, 0 if no and -1 in case of error.
2413 */
2414static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002415xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2416{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002417 xmlSchemaTypePtr typ;
2418
2419 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002420 return (-1);
2421 typ = xmlSchemaGetPredefinedType(type,
2422 BAD_CAST
2423 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002424 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002425 return (0);
2426 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002427}
2428
2429/**
2430 * xmlRelaxNGSchemaTypeCheck:
2431 * @data: data needed for the library
2432 * @type: the type name
2433 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002434 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002435 *
2436 * Check if the given type and value are validated by
2437 * the W3C XMLSchema Datatype library.
2438 *
2439 * Returns 1 if yes, 0 if no and -1 in case of error.
2440 */
2441static int
2442xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002443 const xmlChar * type,
2444 const xmlChar * value,
2445 void **result, xmlNodePtr node)
2446{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002447 xmlSchemaTypePtr typ;
2448 int ret;
2449
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002450 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002451 return (-1);
2452 typ = xmlSchemaGetPredefinedType(type,
2453 BAD_CAST
2454 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002455 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002456 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002457 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002458 (xmlSchemaValPtr *) result, node);
2459 if (ret == 2) /* special ID error code */
2460 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002461 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002462 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002463 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002464 return (0);
2465 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002466}
2467
2468/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002469 * xmlRelaxNGSchemaFacetCheck:
2470 * @data: data needed for the library
2471 * @type: the type name
2472 * @facet: the facet name
2473 * @val: the facet value
2474 * @strval: the string value
2475 * @value: the value to check
2476 *
2477 * Function provided by a type library to check a value facet
2478 *
2479 * Returns 1 if yes, 0 if no and -1 in case of error.
2480 */
2481static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002482xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2483 const xmlChar * type, const xmlChar * facetname,
2484 const xmlChar * val, const xmlChar * strval,
2485 void *value)
2486{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002487 xmlSchemaFacetPtr facet;
2488 xmlSchemaTypePtr typ;
2489 int ret;
2490
2491 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002492 return (-1);
2493 typ = xmlSchemaGetPredefinedType(type,
2494 BAD_CAST
2495 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002496 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002497 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002498
2499 facet = xmlSchemaNewFacet();
2500 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002501 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002502
Daniel Veillard4c004142003-10-07 11:33:24 +00002503 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002504 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002505 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002506 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002507 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002508 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002509 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002510 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002511 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002512 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002513 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002514 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002515 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002516 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002517 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002518 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002519 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002520 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002521 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002522 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002523 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002524 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2525 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2526 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2527 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002528 xmlSchemaFreeFacet(facet);
2529 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002530 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002531 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002532 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2533 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002534 xmlSchemaFreeFacet(facet);
2535 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002536 }
2537 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2538 xmlSchemaFreeFacet(facet);
2539 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002540 return (-1);
2541 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002542}
2543
2544/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002545 * xmlRelaxNGSchemaFreeValue:
2546 * @data: data needed for the library
2547 * @value: the value to free
2548 *
2549 * Function provided by a type library to free a Schemas value
2550 *
2551 * Returns 1 if yes, 0 if no and -1 in case of error.
2552 */
2553static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002554xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2555{
Daniel Veillard80b19092003-03-28 13:29:53 +00002556 xmlSchemaFreeValue(value);
2557}
2558
2559/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002560 * xmlRelaxNGSchemaTypeCompare:
2561 * @data: data needed for the library
2562 * @type: the type name
2563 * @value1: the first value
2564 * @value2: the second value
2565 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002566 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002567 * Datatype library.
2568 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002569 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002570 */
2571static int
2572xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002573 const xmlChar * type,
2574 const xmlChar * value1,
2575 xmlNodePtr ctxt1,
2576 void *comp1,
2577 const xmlChar * value2, xmlNodePtr ctxt2)
2578{
Daniel Veillard80b19092003-03-28 13:29:53 +00002579 int ret;
2580 xmlSchemaTypePtr typ;
2581 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2582
2583 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002584 return (-1);
2585 typ = xmlSchemaGetPredefinedType(type,
2586 BAD_CAST
2587 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002588 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002589 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002590 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002591 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2592 if (ret != 0)
2593 return (-1);
2594 if (res1 == NULL)
2595 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002596 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002597 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002598 }
2599 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002600 if (ret != 0) {
Gaurav7d4e2592013-09-30 11:27:41 +08002601 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillardf4644032005-06-13 11:41:31 +00002602 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002603 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002604 }
Daniel Veillard80b19092003-03-28 13:29:53 +00002605 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002606 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002607 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002608 xmlSchemaFreeValue(res2);
2609 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002610 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002611 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002612 return (1);
2613 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002614}
Daniel Veillard4c004142003-10-07 11:33:24 +00002615
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002616/**
2617 * xmlRelaxNGDefaultTypeHave:
2618 * @data: data needed for the library
2619 * @type: the type name
2620 *
2621 * Check if the given type is provided by
2622 * the default datatype library.
2623 *
2624 * Returns 1 if yes, 0 if no and -1 in case of error.
2625 */
2626static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002627xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2628 const xmlChar * type)
2629{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002630 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002631 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002632 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002633 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002634 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002635 return (1);
2636 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002637}
2638
2639/**
2640 * xmlRelaxNGDefaultTypeCheck:
2641 * @data: data needed for the library
2642 * @type: the type name
2643 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002644 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002645 *
2646 * Check if the given type and value are validated by
2647 * the default datatype library.
2648 *
2649 * Returns 1 if yes, 0 if no and -1 in case of error.
2650 */
2651static int
2652xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002653 const xmlChar * type ATTRIBUTE_UNUSED,
2654 const xmlChar * value ATTRIBUTE_UNUSED,
2655 void **result ATTRIBUTE_UNUSED,
2656 xmlNodePtr node ATTRIBUTE_UNUSED)
2657{
Daniel Veillardd4310742003-02-18 21:12:46 +00002658 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002659 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002660 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002661 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002662 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002663 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002664 }
2665
Daniel Veillard4c004142003-10-07 11:33:24 +00002666 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002667}
2668
2669/**
2670 * xmlRelaxNGDefaultTypeCompare:
2671 * @data: data needed for the library
2672 * @type: the type name
2673 * @value1: the first value
2674 * @value2: the second value
2675 *
2676 * Compare two values accordingly a type from the default
2677 * datatype library.
2678 *
2679 * Returns 1 if yes, 0 if no and -1 in case of error.
2680 */
2681static int
2682xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002683 const xmlChar * type,
2684 const xmlChar * value1,
2685 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2686 void *comp1 ATTRIBUTE_UNUSED,
2687 const xmlChar * value2,
2688 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2689{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002690 int ret = -1;
2691
2692 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002693 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002694 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002695 if (!xmlStrEqual(value1, value2)) {
2696 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002697
Daniel Veillard4c004142003-10-07 11:33:24 +00002698 /*
2699 * TODO: trivial optimizations are possible by
2700 * computing at compile-time
2701 */
2702 nval = xmlRelaxNGNormalize(NULL, value1);
2703 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002704
Daniel Veillard4c004142003-10-07 11:33:24 +00002705 if ((nval == NULL) || (nvalue == NULL))
2706 ret = -1;
2707 else if (xmlStrEqual(nval, nvalue))
2708 ret = 1;
2709 else
2710 ret = 0;
2711 if (nval != NULL)
2712 xmlFree(nval);
2713 if (nvalue != NULL)
2714 xmlFree(nvalue);
2715 } else
2716 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002717 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002718 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002719}
Daniel Veillard4c004142003-10-07 11:33:24 +00002720
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002721static int xmlRelaxNGTypeInitialized = 0;
2722static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2723
2724/**
2725 * xmlRelaxNGFreeTypeLibrary:
2726 * @lib: the type library structure
2727 * @namespace: the URI bound to the library
2728 *
2729 * Free the structure associated to the type library
2730 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002731static void
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002732xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
Daniel Veillard4c004142003-10-07 11:33:24 +00002733 const xmlChar * namespace ATTRIBUTE_UNUSED)
2734{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002735 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002736 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002737 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002738 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002739 xmlFree(lib);
2740}
2741
2742/**
2743 * xmlRelaxNGRegisterTypeLibrary:
2744 * @namespace: the URI bound to the library
2745 * @data: data associated to the library
2746 * @have: the provide function
2747 * @check: the checking function
2748 * @comp: the comparison function
2749 *
2750 * Register a new type library
2751 *
2752 * Returns 0 in case of success and -1 in case of error.
2753 */
2754static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002755xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2756 xmlRelaxNGTypeHave have,
2757 xmlRelaxNGTypeCheck check,
2758 xmlRelaxNGTypeCompare comp,
2759 xmlRelaxNGFacetCheck facet,
2760 xmlRelaxNGTypeFree freef)
2761{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002762 xmlRelaxNGTypeLibraryPtr lib;
2763 int ret;
2764
2765 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002766 (check == NULL) || (comp == NULL))
2767 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002768 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002769 xmlGenericError(xmlGenericErrorContext,
2770 "Relax-NG types library '%s' already registered\n",
2771 namespace);
2772 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002773 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002774 lib =
2775 (xmlRelaxNGTypeLibraryPtr)
2776 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002777 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002778 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002779 return (-1);
2780 }
2781 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2782 lib->namespace = xmlStrdup(namespace);
2783 lib->data = data;
2784 lib->have = have;
2785 lib->comp = comp;
2786 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002787 lib->facet = facet;
2788 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002789 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2790 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002791 xmlGenericError(xmlGenericErrorContext,
2792 "Relax-NG types library failed to register '%s'\n",
2793 namespace);
2794 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2795 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002796 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002797 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002798}
2799
2800/**
2801 * xmlRelaxNGInitTypes:
2802 *
2803 * Initilize the default type libraries.
2804 *
2805 * Returns 0 in case of success and -1 in case of error.
2806 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002807int
Daniel Veillard4c004142003-10-07 11:33:24 +00002808xmlRelaxNGInitTypes(void)
2809{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002810 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002811 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002812 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2813 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002814 xmlGenericError(xmlGenericErrorContext,
2815 "Failed to allocate sh table for Relax-NG types\n");
2816 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002817 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002818 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2819 "http://www.w3.org/2001/XMLSchema-datatypes",
2820 NULL, xmlRelaxNGSchemaTypeHave,
2821 xmlRelaxNGSchemaTypeCheck,
2822 xmlRelaxNGSchemaTypeCompare,
2823 xmlRelaxNGSchemaFacetCheck,
2824 xmlRelaxNGSchemaFreeValue);
2825 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2826 xmlRelaxNGDefaultTypeHave,
2827 xmlRelaxNGDefaultTypeCheck,
2828 xmlRelaxNGDefaultTypeCompare, NULL,
2829 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002830 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002831 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002832}
2833
2834/**
2835 * xmlRelaxNGCleanupTypes:
2836 *
2837 * Cleanup the default Schemas type library associated to RelaxNG
2838 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002839void
2840xmlRelaxNGCleanupTypes(void)
2841{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002842 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002843 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002844 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002845 xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
Daniel Veillard4c004142003-10-07 11:33:24 +00002846 xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002847 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002848}
2849
2850/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002851 * *
2852 * Compiling element content into regexp *
2853 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002854 * Sometime the element content can be compiled into a pure regexp, *
2855 * This allows a faster execution and streamability at that level *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002856 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002857 ************************************************************************/
2858
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002859/* from automata.c but not exported */
2860void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2861
2862
Daniel Veillard52b48c72003-04-13 19:53:42 +00002863static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2864 xmlRelaxNGDefinePtr def);
2865
Daniel Veillard952379b2003-03-17 15:37:12 +00002866/**
2867 * xmlRelaxNGIsCompileable:
2868 * @define: the definition to check
2869 *
2870 * Check if a definition is nullable.
2871 *
2872 * Returns 1 if yes, 0 if no and -1 in case of error
2873 */
2874static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002875xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2876{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002877 int ret = -1;
2878
Daniel Veillard952379b2003-03-17 15:37:12 +00002879 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002880 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002881 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002882 if ((def->type != XML_RELAXNG_ELEMENT) &&
2883 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002884 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002885 if ((def->type != XML_RELAXNG_ELEMENT) &&
2886 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002887 return (0);
2888 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002889 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002890 ret = xmlRelaxNGIsCompileable(def->content);
2891 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002892 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002893 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002894 ret = 1;
2895 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002896 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002897 /*
2898 * Check if the element content is compileable
2899 */
2900 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2901 ((def->dflags & IS_COMPILABLE) == 0)) {
2902 xmlRelaxNGDefinePtr list;
2903
2904 list = def->content;
2905 while (list != NULL) {
2906 ret = xmlRelaxNGIsCompileable(list);
2907 if (ret != 1)
2908 break;
2909 list = list->next;
2910 }
William M. Brack60929622004-03-27 17:54:18 +00002911 /*
2912 * Because the routine is recursive, we must guard against
2913 * discovering both COMPILABLE and NOT_COMPILABLE
2914 */
2915 if (ret == 0) {
2916 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002917 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002918 }
2919 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002920 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002921#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002922 if (ret == 1) {
2923 xmlGenericError(xmlGenericErrorContext,
2924 "element content for %s is compilable\n",
2925 def->name);
2926 } else if (ret == 0) {
2927 xmlGenericError(xmlGenericErrorContext,
2928 "element content for %s is not compilable\n",
2929 def->name);
2930 } else {
2931 xmlGenericError(xmlGenericErrorContext,
2932 "Problem in RelaxNGIsCompileable for element %s\n",
2933 def->name);
2934 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002935#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002936 }
2937 /*
2938 * All elements return a compileable status unless they
2939 * are generic like anyName
2940 */
2941 if ((def->nameClass != NULL) || (def->name == NULL))
2942 ret = 0;
2943 else
2944 ret = 1;
2945 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002946 case XML_RELAXNG_REF:
2947 case XML_RELAXNG_EXTERNALREF:
2948 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002949 if (def->depth == -20) {
2950 return (1);
2951 } else {
2952 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002953
Daniel Veillard4c004142003-10-07 11:33:24 +00002954 def->depth = -20;
2955 list = def->content;
2956 while (list != NULL) {
2957 ret = xmlRelaxNGIsCompileable(list);
2958 if (ret != 1)
2959 break;
2960 list = list->next;
2961 }
2962 }
2963 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002964 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002965 case XML_RELAXNG_OPTIONAL:
2966 case XML_RELAXNG_ZEROORMORE:
2967 case XML_RELAXNG_ONEORMORE:
2968 case XML_RELAXNG_CHOICE:
2969 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002970 case XML_RELAXNG_DEF:{
2971 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002972
Daniel Veillard4c004142003-10-07 11:33:24 +00002973 list = def->content;
2974 while (list != NULL) {
2975 ret = xmlRelaxNGIsCompileable(list);
2976 if (ret != 1)
2977 break;
2978 list = list->next;
2979 }
2980 break;
2981 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002982 case XML_RELAXNG_EXCEPT:
2983 case XML_RELAXNG_ATTRIBUTE:
2984 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002985 case XML_RELAXNG_DATATYPE:
2986 case XML_RELAXNG_LIST:
2987 case XML_RELAXNG_PARAM:
2988 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00002989 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00002990 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00002991 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002992 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002993 if (ret == 0)
2994 def->dflags |= IS_NOT_COMPILABLE;
2995 if (ret == 1)
2996 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002997#ifdef DEBUG_COMPILE
2998 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002999 xmlGenericError(xmlGenericErrorContext,
3000 "RelaxNGIsCompileable %s : true\n",
3001 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003002 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003003 xmlGenericError(xmlGenericErrorContext,
3004 "RelaxNGIsCompileable %s : false\n",
3005 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003006 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003007 xmlGenericError(xmlGenericErrorContext,
3008 "Problem in RelaxNGIsCompileable %s\n",
3009 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003010 }
3011#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003012 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003013}
3014
3015/**
3016 * xmlRelaxNGCompile:
3017 * ctxt: the RelaxNG parser context
3018 * @define: the definition tree to compile
3019 *
3020 * Compile the set of definitions, it works recursively, till the
3021 * element boundaries, where it tries to compile the content if possible
3022 *
3023 * Returns 0 if success and -1 in case of error
3024 */
3025static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003026xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3027{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003028 int ret = 0;
3029 xmlRelaxNGDefinePtr list;
3030
Daniel Veillard4c004142003-10-07 11:33:24 +00003031 if ((ctxt == NULL) || (def == NULL))
3032 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003033
Daniel Veillard4c004142003-10-07 11:33:24 +00003034 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003035 case XML_RELAXNG_START:
3036 if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003037 xmlAutomataPtr oldam = ctxt->am;
3038 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003039
3040 def->depth = -25;
3041
Daniel Veillard4c004142003-10-07 11:33:24 +00003042 list = def->content;
3043 ctxt->am = xmlNewAutomata();
3044 if (ctxt->am == NULL)
3045 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003046
3047 /*
3048 * assume identical strings but not same pointer are different
3049 * atoms, needed for non-determinism detection
3050 * That way if 2 elements with the same name are in a choice
3051 * branch the automata is found non-deterministic and
3052 * we fallback to the normal validation which does the right
3053 * thing of exploring both choices.
3054 */
3055 xmlAutomataSetFlags(ctxt->am, 1);
3056
Daniel Veillard4c004142003-10-07 11:33:24 +00003057 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3058 while (list != NULL) {
3059 xmlRelaxNGCompile(ctxt, list);
3060 list = list->next;
3061 }
3062 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Noam9313ae82012-05-15 11:03:46 +08003063 if (xmlAutomataIsDeterminist(ctxt->am))
3064 def->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003065
Daniel Veillard4c004142003-10-07 11:33:24 +00003066 xmlFreeAutomata(ctxt->am);
3067 ctxt->state = oldstate;
3068 ctxt->am = oldam;
3069 }
3070 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003071 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003072 if ((ctxt->am != NULL) && (def->name != NULL)) {
3073 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3074 ctxt->state, NULL,
3075 def->name, def->ns,
3076 def);
3077 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003078 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003079 xmlAutomataPtr oldam = ctxt->am;
3080 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003081
3082 def->depth = -25;
3083
Daniel Veillard4c004142003-10-07 11:33:24 +00003084 list = def->content;
3085 ctxt->am = xmlNewAutomata();
3086 if (ctxt->am == NULL)
3087 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003088 xmlAutomataSetFlags(ctxt->am, 1);
Daniel Veillard4c004142003-10-07 11:33:24 +00003089 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3090 while (list != NULL) {
3091 xmlRelaxNGCompile(ctxt, list);
3092 list = list->next;
3093 }
3094 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3095 def->contModel = xmlAutomataCompile(ctxt->am);
3096 if (!xmlRegexpIsDeterminist(def->contModel)) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003097#ifdef DEBUG_COMPILE
3098 xmlGenericError(xmlGenericErrorContext,
3099 "Content model not determinist %s\n",
3100 def->name);
3101#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003102 /*
3103 * we can only use the automata if it is determinist
3104 */
3105 xmlRegFreeRegexp(def->contModel);
3106 def->contModel = NULL;
3107 }
3108 xmlFreeAutomata(ctxt->am);
3109 ctxt->state = oldstate;
3110 ctxt->am = oldam;
3111 } else {
3112 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003113
Daniel Veillard4c004142003-10-07 11:33:24 +00003114 /*
3115 * we can't build the content model for this element content
3116 * but it still might be possible to build it for some of its
3117 * children, recurse.
3118 */
3119 ret = xmlRelaxNGTryCompile(ctxt, def);
3120 ctxt->am = oldam;
3121 }
3122 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003123 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003124 ret = xmlRelaxNGCompile(ctxt, def->content);
3125 break;
3126 case XML_RELAXNG_OPTIONAL:{
3127 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003128
Daniel Veillardfd780772009-08-26 18:35:29 +02003129 list = def->content;
3130 while (list != NULL) {
3131 xmlRelaxNGCompile(ctxt, list);
3132 list = list->next;
3133 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003134 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3135 break;
3136 }
3137 case XML_RELAXNG_ZEROORMORE:{
3138 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003139
Daniel Veillard4c004142003-10-07 11:33:24 +00003140 ctxt->state =
3141 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3142 oldstate = ctxt->state;
3143 list = def->content;
3144 while (list != NULL) {
3145 xmlRelaxNGCompile(ctxt, list);
3146 list = list->next;
3147 }
3148 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3149 ctxt->state =
3150 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3151 break;
3152 }
3153 case XML_RELAXNG_ONEORMORE:{
3154 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003155
Daniel Veillard4c004142003-10-07 11:33:24 +00003156 list = def->content;
3157 while (list != NULL) {
3158 xmlRelaxNGCompile(ctxt, list);
3159 list = list->next;
3160 }
3161 oldstate = ctxt->state;
3162 list = def->content;
3163 while (list != NULL) {
3164 xmlRelaxNGCompile(ctxt, list);
3165 list = list->next;
3166 }
3167 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3168 ctxt->state =
3169 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3170 break;
3171 }
3172 case XML_RELAXNG_CHOICE:{
3173 xmlAutomataStatePtr target = NULL;
3174 xmlAutomataStatePtr oldstate = ctxt->state;
3175
3176 list = def->content;
3177 while (list != NULL) {
3178 ctxt->state = oldstate;
3179 ret = xmlRelaxNGCompile(ctxt, list);
3180 if (ret != 0)
3181 break;
3182 if (target == NULL)
3183 target = ctxt->state;
3184 else {
3185 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3186 target);
3187 }
3188 list = list->next;
3189 }
3190 ctxt->state = target;
3191
3192 break;
3193 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003194 case XML_RELAXNG_REF:
3195 case XML_RELAXNG_EXTERNALREF:
3196 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003197 case XML_RELAXNG_GROUP:
3198 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003199 list = def->content;
3200 while (list != NULL) {
3201 ret = xmlRelaxNGCompile(ctxt, list);
3202 if (ret != 0)
3203 break;
3204 list = list->next;
3205 }
3206 break;
3207 case XML_RELAXNG_TEXT:{
3208 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003209
Daniel Veillard4c004142003-10-07 11:33:24 +00003210 ctxt->state =
3211 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3212 oldstate = ctxt->state;
3213 xmlRelaxNGCompile(ctxt, def->content);
3214 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3215 ctxt->state, BAD_CAST "#text",
3216 NULL);
3217 ctxt->state =
3218 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3219 break;
3220 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003221 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003222 ctxt->state =
3223 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3224 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003225 case XML_RELAXNG_EXCEPT:
3226 case XML_RELAXNG_ATTRIBUTE:
3227 case XML_RELAXNG_INTERLEAVE:
3228 case XML_RELAXNG_NOT_ALLOWED:
3229 case XML_RELAXNG_DATATYPE:
3230 case XML_RELAXNG_LIST:
3231 case XML_RELAXNG_PARAM:
3232 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003233 /* This should not happen and generate an internal error */
3234 fprintf(stderr, "RNG internal error trying to compile %s\n",
3235 xmlRelaxNGDefName(def));
3236 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003237 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003238 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003239}
3240
3241/**
3242 * xmlRelaxNGTryCompile:
3243 * ctxt: the RelaxNG parser context
3244 * @define: the definition tree to compile
3245 *
3246 * Try to compile the set of definitions, it works recursively,
3247 * possibly ignoring parts which cannot be compiled.
3248 *
3249 * Returns 0 if success and -1 in case of error
3250 */
3251static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003252xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3253{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003254 int ret = 0;
3255 xmlRelaxNGDefinePtr list;
3256
Daniel Veillard4c004142003-10-07 11:33:24 +00003257 if ((ctxt == NULL) || (def == NULL))
3258 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003259
3260 if ((def->type == XML_RELAXNG_START) ||
3261 (def->type == XML_RELAXNG_ELEMENT)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003262 ret = xmlRelaxNGIsCompileable(def);
3263 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3264 ctxt->am = NULL;
3265 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003266#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003267 if (ret == 0) {
3268 if (def->type == XML_RELAXNG_START)
3269 xmlGenericError(xmlGenericErrorContext,
3270 "compiled the start\n");
3271 else
3272 xmlGenericError(xmlGenericErrorContext,
3273 "compiled element %s\n", def->name);
3274 } else {
3275 if (def->type == XML_RELAXNG_START)
3276 xmlGenericError(xmlGenericErrorContext,
3277 "failed to compile the start\n");
3278 else
3279 xmlGenericError(xmlGenericErrorContext,
3280 "failed to compile element %s\n",
3281 def->name);
3282 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003283#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003284 return (ret);
3285 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003286 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003287 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003288 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003289 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3290 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003291 case XML_RELAXNG_TEXT:
3292 case XML_RELAXNG_DATATYPE:
3293 case XML_RELAXNG_LIST:
3294 case XML_RELAXNG_PARAM:
3295 case XML_RELAXNG_VALUE:
3296 case XML_RELAXNG_EMPTY:
3297 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003298 ret = 0;
3299 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003300 case XML_RELAXNG_OPTIONAL:
3301 case XML_RELAXNG_ZEROORMORE:
3302 case XML_RELAXNG_ONEORMORE:
3303 case XML_RELAXNG_CHOICE:
3304 case XML_RELAXNG_GROUP:
3305 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003306 case XML_RELAXNG_START:
3307 case XML_RELAXNG_REF:
3308 case XML_RELAXNG_EXTERNALREF:
3309 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003310 list = def->content;
3311 while (list != NULL) {
3312 ret = xmlRelaxNGTryCompile(ctxt, list);
3313 if (ret != 0)
3314 break;
3315 list = list->next;
3316 }
3317 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003318 case XML_RELAXNG_EXCEPT:
3319 case XML_RELAXNG_ATTRIBUTE:
3320 case XML_RELAXNG_INTERLEAVE:
3321 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003322 ret = 0;
3323 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003324 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003325 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003326}
3327
3328/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003329 * *
3330 * Parsing functions *
3331 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003332 ************************************************************************/
3333
Daniel Veillard4c004142003-10-07 11:33:24 +00003334static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3335 ctxt, xmlNodePtr node);
3336static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3337 ctxt, xmlNodePtr node);
3338static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3339 ctxt, xmlNodePtr nodes,
3340 int group);
3341static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3342 ctxt, xmlNodePtr node);
3343static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3344 xmlNodePtr node);
3345static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3346 xmlNodePtr nodes);
3347static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3348 ctxt, xmlNodePtr node,
3349 xmlRelaxNGDefinePtr
3350 def);
3351static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3352 ctxt, xmlNodePtr nodes);
3353static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3354 xmlRelaxNGDefinePtr define,
3355 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003356
3357
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003358#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003359
3360/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003361 * xmlRelaxNGIsNullable:
3362 * @define: the definition to verify
3363 *
3364 * Check if a definition is nullable.
3365 *
3366 * Returns 1 if yes, 0 if no and -1 in case of error
3367 */
3368static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003369xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3370{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003371 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003372
Daniel Veillardfd573f12003-03-16 17:52:32 +00003373 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003374 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003375
Daniel Veillarde063f482003-03-21 16:53:17 +00003376 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003377 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003378 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003379 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003380 switch (define->type) {
3381 case XML_RELAXNG_EMPTY:
3382 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003383 ret = 1;
3384 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003385 case XML_RELAXNG_NOOP:
3386 case XML_RELAXNG_DEF:
3387 case XML_RELAXNG_REF:
3388 case XML_RELAXNG_EXTERNALREF:
3389 case XML_RELAXNG_PARENTREF:
3390 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003391 ret = xmlRelaxNGIsNullable(define->content);
3392 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003393 case XML_RELAXNG_EXCEPT:
3394 case XML_RELAXNG_NOT_ALLOWED:
3395 case XML_RELAXNG_ELEMENT:
3396 case XML_RELAXNG_DATATYPE:
3397 case XML_RELAXNG_PARAM:
3398 case XML_RELAXNG_VALUE:
3399 case XML_RELAXNG_LIST:
3400 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003401 ret = 0;
3402 break;
3403 case XML_RELAXNG_CHOICE:{
3404 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003405
Daniel Veillard4c004142003-10-07 11:33:24 +00003406 while (list != NULL) {
3407 ret = xmlRelaxNGIsNullable(list);
3408 if (ret != 0)
3409 goto done;
3410 list = list->next;
3411 }
3412 ret = 0;
3413 break;
3414 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003415 case XML_RELAXNG_START:
3416 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003417 case XML_RELAXNG_GROUP:{
3418 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003419
Daniel Veillard4c004142003-10-07 11:33:24 +00003420 while (list != NULL) {
3421 ret = xmlRelaxNGIsNullable(list);
3422 if (ret != 1)
3423 goto done;
3424 list = list->next;
3425 }
3426 return (1);
3427 }
3428 default:
3429 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003430 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003431 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003432 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003433 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003434 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003435 define->dflags |= IS_NULLABLE;
3436 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003437}
3438
3439/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003440 * xmlRelaxNGIsBlank:
3441 * @str: a string
3442 *
3443 * Check if a string is ignorable c.f. 4.2. Whitespace
3444 *
3445 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3446 */
3447static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003448xmlRelaxNGIsBlank(xmlChar * str)
3449{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003450 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003451 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003452 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003453 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003454 return (0);
3455 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003456 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003457 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003458}
3459
Daniel Veillard6eadf632003-01-23 18:29:16 +00003460/**
3461 * xmlRelaxNGGetDataTypeLibrary:
3462 * @ctxt: a Relax-NG parser context
3463 * @node: the current data or value element
3464 *
3465 * Applies algorithm from 4.3. datatypeLibrary attribute
3466 *
3467 * Returns the datatypeLibary value or NULL if not found
3468 */
3469static xmlChar *
3470xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003471 xmlNodePtr node)
3472{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003473 xmlChar *ret, *escape;
3474
Daniel Veillardd44b9362009-09-07 12:15:08 +02003475 if (node == NULL)
3476 return(NULL);
3477
Daniel Veillard6eadf632003-01-23 18:29:16 +00003478 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003479 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3480 if (ret != NULL) {
3481 if (ret[0] == 0) {
3482 xmlFree(ret);
3483 return (NULL);
3484 }
3485 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3486 if (escape == NULL) {
3487 return (ret);
3488 }
3489 xmlFree(ret);
3490 return (escape);
3491 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003492 }
3493 node = node->parent;
3494 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003495 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3496 if (ret != NULL) {
3497 if (ret[0] == 0) {
3498 xmlFree(ret);
3499 return (NULL);
3500 }
3501 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3502 if (escape == NULL) {
3503 return (ret);
3504 }
3505 xmlFree(ret);
3506 return (escape);
3507 }
3508 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003509 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003510 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003511}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003512
3513/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003514 * xmlRelaxNGParseValue:
3515 * @ctxt: a Relax-NG parser context
3516 * @node: the data node.
3517 *
3518 * parse the content of a RelaxNG value node.
3519 *
3520 * Returns the definition pointer or NULL in case of error
3521 */
3522static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003523xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3524{
Daniel Veillardedc91922003-01-26 00:52:04 +00003525 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003526 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003527 xmlChar *type;
3528 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003529 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003530
Daniel Veillardfd573f12003-03-16 17:52:32 +00003531 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003532 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003533 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003534 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003535
3536 type = xmlGetProp(node, BAD_CAST "type");
3537 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003538 xmlRelaxNGNormExtSpace(type);
3539 if (xmlValidateNCName(type, 0)) {
3540 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3541 "value type '%s' is not an NCName\n", type, NULL);
3542 }
3543 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3544 if (library == NULL)
3545 library =
3546 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003547
Daniel Veillard4c004142003-10-07 11:33:24 +00003548 def->name = type;
3549 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003550
Daniel Veillard4c004142003-10-07 11:33:24 +00003551 lib = (xmlRelaxNGTypeLibraryPtr)
3552 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3553 if (lib == NULL) {
3554 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3555 "Use of unregistered type library '%s'\n", library,
3556 NULL);
3557 def->data = NULL;
3558 } else {
3559 def->data = lib;
3560 if (lib->have == NULL) {
3561 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3562 "Internal error with type library '%s': no 'have'\n",
3563 library, NULL);
3564 } else {
3565 success = lib->have(lib->data, def->name);
3566 if (success != 1) {
3567 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3568 "Error type '%s' is not exported by type library '%s'\n",
3569 def->name, library);
3570 }
3571 }
3572 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003573 }
3574 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003575 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003576 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003577 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3578 (node->children->next != NULL)) {
3579 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3580 "Expecting a single text value for <value>content\n",
3581 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003582 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003583 def->value = xmlNodeGetContent(node);
3584 if (def->value == NULL) {
3585 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3586 "Element <value> has no content\n", NULL, NULL);
3587 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3588 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003589
Daniel Veillard4c004142003-10-07 11:33:24 +00003590 success =
3591 lib->check(lib->data, def->name, def->value, &val, node);
3592 if (success != 1) {
3593 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3594 "Value '%s' is not acceptable for type '%s'\n",
3595 def->value, def->name);
3596 } else {
3597 if (val != NULL)
3598 def->attrs = val;
3599 }
3600 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003601 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003602 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003603}
3604
3605/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003606 * xmlRelaxNGParseData:
3607 * @ctxt: a Relax-NG parser context
3608 * @node: the data node.
3609 *
3610 * parse the content of a RelaxNG data node.
3611 *
3612 * Returns the definition pointer or NULL in case of error
3613 */
3614static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003615xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3616{
Daniel Veillard14b56432006-03-09 18:41:40 +00003617 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003618 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003619 xmlRelaxNGTypeLibraryPtr lib;
3620 xmlChar *type;
3621 xmlChar *library;
3622 xmlNodePtr content;
3623 int tmp;
3624
3625 type = xmlGetProp(node, BAD_CAST "type");
3626 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003627 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3628 NULL);
3629 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003630 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003631 xmlRelaxNGNormExtSpace(type);
3632 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003633 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3634 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003635 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003636 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3637 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003638 library =
3639 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003640
Daniel Veillardfd573f12003-03-16 17:52:32 +00003641 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003642 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003643 xmlFree(type);
3644 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003645 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003646 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003647 def->name = type;
3648 def->ns = library;
3649
3650 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003651 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003652 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003653 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3654 "Use of unregistered type library '%s'\n", library,
3655 NULL);
3656 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003657 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003658 def->data = lib;
3659 if (lib->have == NULL) {
3660 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3661 "Internal error with type library '%s': no 'have'\n",
3662 library, NULL);
3663 } else {
3664 tmp = lib->have(lib->data, def->name);
3665 if (tmp != 1) {
3666 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3667 "Error type '%s' is not exported by type library '%s'\n",
3668 def->name, library);
3669 } else
3670 if ((xmlStrEqual
3671 (library,
3672 BAD_CAST
3673 "http://www.w3.org/2001/XMLSchema-datatypes"))
3674 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3675 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3676 ctxt->idref = 1;
3677 }
3678 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003679 }
3680 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003681
3682 /*
3683 * Handle optional params
3684 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003685 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003686 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3687 break;
3688 if (xmlStrEqual(library,
3689 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3690 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3691 "Type library '%s' does not allow type parameters\n",
3692 library, NULL);
3693 content = content->next;
3694 while ((content != NULL) &&
3695 (xmlStrEqual(content->name, BAD_CAST "param")))
3696 content = content->next;
3697 } else {
3698 param = xmlRelaxNGNewDefine(ctxt, node);
3699 if (param != NULL) {
3700 param->type = XML_RELAXNG_PARAM;
3701 param->name = xmlGetProp(content, BAD_CAST "name");
3702 if (param->name == NULL) {
3703 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3704 "param has no name\n", NULL, NULL);
3705 }
3706 param->value = xmlNodeGetContent(content);
3707 if (lastparam == NULL) {
3708 def->attrs = lastparam = param;
3709 } else {
3710 lastparam->next = param;
3711 lastparam = param;
3712 }
3713 if (lib != NULL) {
3714 }
3715 }
3716 content = content->next;
3717 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003718 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003719 /*
3720 * Handle optional except
3721 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003722 if ((content != NULL)
3723 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3724 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003725 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003726
Daniel Veillard4c004142003-10-07 11:33:24 +00003727 except = xmlRelaxNGNewDefine(ctxt, node);
3728 if (except == NULL) {
3729 return (def);
3730 }
3731 except->type = XML_RELAXNG_EXCEPT;
3732 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003733 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003734 if (child == NULL) {
3735 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3736 "except has no content\n", NULL, NULL);
3737 }
3738 while (child != NULL) {
3739 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3740 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003741 if (last == NULL) {
3742 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003743 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003744 last->next = tmp2;
3745 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003746 }
3747 }
3748 child = child->next;
3749 }
3750 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003751 }
3752 /*
3753 * Check there is no unhandled data
3754 */
3755 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003756 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3757 "Element data has unexpected content %s\n",
3758 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003759 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003760
Daniel Veillard4c004142003-10-07 11:33:24 +00003761 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003762}
3763
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003764static const xmlChar *invalidName = BAD_CAST "\1";
3765
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003766/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003767 * xmlRelaxNGCompareNameClasses:
3768 * @defs1: the first element/attribute defs
3769 * @defs2: the second element/attribute defs
3770 * @name: the restriction on the name
3771 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003772 *
3773 * Compare the 2 lists of element definitions. The comparison is
3774 * that if both lists do not accept the same QNames, it returns 1
3775 * If the 2 lists can accept the same QName the comparison returns 0
3776 *
3777 * Returns 1 disttinct, 0 if equal
3778 */
3779static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003780xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003781 xmlRelaxNGDefinePtr def2)
3782{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003783 int ret = 1;
3784 xmlNode node;
3785 xmlNs ns;
3786 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003787
Daniel Veillard42f12e92003-03-07 18:32:59 +00003788 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3789
Daniel Veillardb30ca312005-09-04 13:50:03 +00003790 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3791
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003792 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003793 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3794 if (def2->type == XML_RELAXNG_TEXT)
3795 return (1);
3796 if (def1->name != NULL) {
3797 node.name = def1->name;
3798 } else {
3799 node.name = invalidName;
3800 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003801 if (def1->ns != NULL) {
3802 if (def1->ns[0] == 0) {
3803 node.ns = NULL;
3804 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003805 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003806 ns.href = def1->ns;
3807 }
3808 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003809 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003810 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003811 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003812 if (def1->nameClass != NULL) {
3813 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3814 } else {
3815 ret = 0;
3816 }
3817 } else {
3818 ret = 1;
3819 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003820 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003821 if (def2->type == XML_RELAXNG_TEXT)
3822 return (0);
3823 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003824 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard2fab2352015-03-16 08:38:36 +08003825 ret = xmlRelaxNGCompareNameClasses(def1->content, def2);
3826 if (ret == 0)
3827 ret = 1;
3828 else if (ret == 1)
3829 ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003830 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003831 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003832 }
3833 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003834 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003835 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003836 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3837 if (def2->name != NULL) {
3838 node.name = def2->name;
3839 } else {
3840 node.name = invalidName;
3841 }
3842 node.ns = &ns;
3843 if (def2->ns != NULL) {
3844 if (def2->ns[0] == 0) {
3845 node.ns = NULL;
3846 } else {
3847 ns.href = def2->ns;
3848 }
3849 } else {
3850 ns.href = invalidName;
3851 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003852 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003853 if (def2->nameClass != NULL) {
3854 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3855 } else {
3856 ret = 0;
3857 }
3858 } else {
3859 ret = 1;
3860 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003861 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003862 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003863 }
3864
Daniel Veillard4c004142003-10-07 11:33:24 +00003865 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003866}
3867
3868/**
3869 * xmlRelaxNGCompareElemDefLists:
3870 * @ctxt: a Relax-NG parser context
3871 * @defs1: the first list of element/attribute defs
3872 * @defs2: the second list of element/attribute defs
3873 *
3874 * Compare the 2 lists of element or attribute definitions. The comparison
3875 * is that if both lists do not accept the same QNames, it returns 1
3876 * If the 2 lists can accept the same QName the comparison returns 0
3877 *
3878 * Returns 1 disttinct, 0 if equal
3879 */
3880static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003881xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3882 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3883 xmlRelaxNGDefinePtr * def2)
3884{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003885 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003886
Daniel Veillard154877e2003-01-30 12:17:05 +00003887 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003888 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003889 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003890 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003891 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003892 while ((*def2) != NULL) {
3893 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3894 return (0);
3895 def2++;
3896 }
3897 def2 = basedef2;
3898 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003899 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003900 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003901}
3902
3903/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003904 * xmlRelaxNGGenerateAttributes:
3905 * @ctxt: a Relax-NG parser context
3906 * @def: the definition definition
3907 *
3908 * Check if the definition can only generate attributes
3909 *
3910 * Returns 1 if yes, 0 if no and -1 in case of error.
3911 */
3912static int
3913xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003914 xmlRelaxNGDefinePtr def)
3915{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003916 xmlRelaxNGDefinePtr parent, cur, tmp;
3917
3918 /*
3919 * Don't run that check in case of error. Infinite recursion
3920 * becomes possible.
3921 */
3922 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003923 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003924
3925 parent = NULL;
3926 cur = def;
3927 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003928 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3929 (cur->type == XML_RELAXNG_TEXT) ||
3930 (cur->type == XML_RELAXNG_DATATYPE) ||
3931 (cur->type == XML_RELAXNG_PARAM) ||
3932 (cur->type == XML_RELAXNG_LIST) ||
3933 (cur->type == XML_RELAXNG_VALUE) ||
3934 (cur->type == XML_RELAXNG_EMPTY))
3935 return (0);
3936 if ((cur->type == XML_RELAXNG_CHOICE) ||
3937 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3938 (cur->type == XML_RELAXNG_GROUP) ||
3939 (cur->type == XML_RELAXNG_ONEORMORE) ||
3940 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3941 (cur->type == XML_RELAXNG_OPTIONAL) ||
3942 (cur->type == XML_RELAXNG_PARENTREF) ||
3943 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3944 (cur->type == XML_RELAXNG_REF) ||
3945 (cur->type == XML_RELAXNG_DEF)) {
3946 if (cur->content != NULL) {
3947 parent = cur;
3948 cur = cur->content;
3949 tmp = cur;
3950 while (tmp != NULL) {
3951 tmp->parent = parent;
3952 tmp = tmp->next;
3953 }
3954 continue;
3955 }
3956 }
3957 if (cur == def)
3958 break;
3959 if (cur->next != NULL) {
3960 cur = cur->next;
3961 continue;
3962 }
3963 do {
3964 cur = cur->parent;
3965 if (cur == NULL)
3966 break;
3967 if (cur == def)
3968 return (1);
3969 if (cur->next != NULL) {
3970 cur = cur->next;
3971 break;
3972 }
3973 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003974 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003975 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003976}
Daniel Veillard4c004142003-10-07 11:33:24 +00003977
Daniel Veillardce192eb2003-04-16 15:58:05 +00003978/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003979 * xmlRelaxNGGetElements:
3980 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003981 * @def: the definition definition
3982 * @eora: gather elements (0) or attributes (1)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003983 *
3984 * Compute the list of top elements a definition can generate
3985 *
3986 * Returns a list of elements or NULL if none was found.
3987 */
3988static xmlRelaxNGDefinePtr *
3989xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003990 xmlRelaxNGDefinePtr def, int eora)
3991{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003992 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003993 int len = 0;
3994 int max = 0;
3995
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003996 /*
3997 * Don't run that check in case of error. Infinite recursion
3998 * becomes possible.
3999 */
4000 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004001 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004002
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004003 parent = NULL;
4004 cur = def;
4005 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004006 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4007 (cur->type == XML_RELAXNG_TEXT))) ||
4008 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
4009 if (ret == NULL) {
4010 max = 10;
4011 ret = (xmlRelaxNGDefinePtr *)
4012 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4013 if (ret == NULL) {
4014 xmlRngPErrMemory(ctxt, "getting element list\n");
4015 return (NULL);
4016 }
4017 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00004018 xmlRelaxNGDefinePtr *temp;
4019
Daniel Veillard4c004142003-10-07 11:33:24 +00004020 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00004021 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00004022 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00004023 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004024 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00004025 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00004026 return (NULL);
4027 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00004028 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00004029 }
4030 ret[len++] = cur;
4031 ret[len] = NULL;
4032 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4033 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4034 (cur->type == XML_RELAXNG_GROUP) ||
4035 (cur->type == XML_RELAXNG_ONEORMORE) ||
4036 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4037 (cur->type == XML_RELAXNG_OPTIONAL) ||
4038 (cur->type == XML_RELAXNG_PARENTREF) ||
4039 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004040 (cur->type == XML_RELAXNG_DEF) ||
4041 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004042 /*
4043 * Don't go within elements or attributes or string values.
4044 * Just gather the element top list
4045 */
4046 if (cur->content != NULL) {
4047 parent = cur;
4048 cur = cur->content;
4049 tmp = cur;
4050 while (tmp != NULL) {
4051 tmp->parent = parent;
4052 tmp = tmp->next;
4053 }
4054 continue;
4055 }
4056 }
4057 if (cur == def)
4058 break;
4059 if (cur->next != NULL) {
4060 cur = cur->next;
4061 continue;
4062 }
4063 do {
4064 cur = cur->parent;
4065 if (cur == NULL)
4066 break;
4067 if (cur == def)
4068 return (ret);
4069 if (cur->next != NULL) {
4070 cur = cur->next;
4071 break;
4072 }
4073 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004074 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004075 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004076}
Daniel Veillard4c004142003-10-07 11:33:24 +00004077
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004078/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004079 * xmlRelaxNGCheckChoiceDeterminism:
4080 * @ctxt: a Relax-NG parser context
4081 * @def: the choice definition
4082 *
4083 * Also used to find indeterministic pattern in choice
4084 */
4085static void
4086xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004087 xmlRelaxNGDefinePtr def)
4088{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004089 xmlRelaxNGDefinePtr **list;
4090 xmlRelaxNGDefinePtr cur;
4091 int nbchild = 0, i, j, ret;
4092 int is_nullable = 0;
4093 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004094 xmlHashTablePtr triage = NULL;
4095 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004096
Daniel Veillard4c004142003-10-07 11:33:24 +00004097 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4098 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004099
Daniel Veillarde063f482003-03-21 16:53:17 +00004100 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004101 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004102
Daniel Veillardfd573f12003-03-16 17:52:32 +00004103 /*
4104 * Don't run that check in case of error. Infinite recursion
4105 * becomes possible.
4106 */
4107 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004108 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004109
4110 is_nullable = xmlRelaxNGIsNullable(def);
4111
4112 cur = def->content;
4113 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004114 nbchild++;
4115 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004116 }
4117
4118 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004119 sizeof(xmlRelaxNGDefinePtr
4120 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004121 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004122 xmlRngPErrMemory(ctxt, "building choice\n");
4123 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004124 }
4125 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004126 /*
4127 * a bit strong but safe
4128 */
4129 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004130 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004131 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004132 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004133 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004134 cur = def->content;
4135 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004136 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4137 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4138 is_triable = 0;
4139 } else if (is_triable == 1) {
4140 xmlRelaxNGDefinePtr *tmp;
4141 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004142
Daniel Veillard4c004142003-10-07 11:33:24 +00004143 tmp = list[i];
4144 while ((*tmp != NULL) && (is_triable == 1)) {
4145 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4146 res = xmlHashAddEntry2(triage,
4147 BAD_CAST "#text", NULL,
4148 (void *) cur);
4149 if (res != 0)
4150 is_triable = -1;
4151 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4152 ((*tmp)->name != NULL)) {
4153 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4154 res = xmlHashAddEntry2(triage,
4155 (*tmp)->name, NULL,
4156 (void *) cur);
4157 else
4158 res = xmlHashAddEntry2(triage,
4159 (*tmp)->name, (*tmp)->ns,
4160 (void *) cur);
4161 if (res != 0)
4162 is_triable = -1;
4163 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4164 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4165 res = xmlHashAddEntry2(triage,
4166 BAD_CAST "#any", NULL,
4167 (void *) cur);
4168 else
4169 res = xmlHashAddEntry2(triage,
4170 BAD_CAST "#any", (*tmp)->ns,
4171 (void *) cur);
4172 if (res != 0)
4173 is_triable = -1;
4174 } else {
4175 is_triable = -1;
4176 }
4177 tmp++;
4178 }
4179 }
4180 i++;
4181 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004182 }
4183
Daniel Veillard4c004142003-10-07 11:33:24 +00004184 for (i = 0; i < nbchild; i++) {
4185 if (list[i] == NULL)
4186 continue;
4187 for (j = 0; j < i; j++) {
4188 if (list[j] == NULL)
4189 continue;
4190 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4191 if (ret == 0) {
4192 is_indeterminist = 1;
4193 }
4194 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004195 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004196 for (i = 0; i < nbchild; i++) {
4197 if (list[i] != NULL)
4198 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004199 }
4200
4201 xmlFree(list);
4202 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004203 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004204 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004205 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004206 def->dflags |= IS_TRIABLE;
4207 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004208 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004209 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004210 }
4211 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004212}
4213
4214/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004215 * xmlRelaxNGCheckGroupAttrs:
4216 * @ctxt: a Relax-NG parser context
4217 * @def: the group definition
4218 *
4219 * Detects violations of rule 7.3
4220 */
4221static void
4222xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004223 xmlRelaxNGDefinePtr def)
4224{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004225 xmlRelaxNGDefinePtr **list;
4226 xmlRelaxNGDefinePtr cur;
4227 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004228
4229 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004230 ((def->type != XML_RELAXNG_GROUP) &&
4231 (def->type != XML_RELAXNG_ELEMENT)))
4232 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004233
Daniel Veillarde063f482003-03-21 16:53:17 +00004234 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004235 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004236
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004237 /*
4238 * Don't run that check in case of error. Infinite recursion
4239 * becomes possible.
4240 */
4241 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004242 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004243
Daniel Veillardfd573f12003-03-16 17:52:32 +00004244 cur = def->attrs;
4245 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004246 nbchild++;
4247 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004248 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004249 cur = def->content;
4250 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004251 nbchild++;
4252 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004253 }
4254
4255 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004256 sizeof(xmlRelaxNGDefinePtr
4257 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004258 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004259 xmlRngPErrMemory(ctxt, "building group\n");
4260 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004261 }
4262 i = 0;
4263 cur = def->attrs;
4264 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004265 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4266 i++;
4267 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004268 }
4269 cur = def->content;
4270 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004271 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4272 i++;
4273 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004274 }
4275
Daniel Veillard4c004142003-10-07 11:33:24 +00004276 for (i = 0; i < nbchild; i++) {
4277 if (list[i] == NULL)
4278 continue;
4279 for (j = 0; j < i; j++) {
4280 if (list[j] == NULL)
4281 continue;
4282 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4283 if (ret == 0) {
4284 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4285 "Attributes conflicts in group\n", NULL, NULL);
4286 }
4287 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004288 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004289 for (i = 0; i < nbchild; i++) {
4290 if (list[i] != NULL)
4291 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004292 }
4293
4294 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004295 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004296}
4297
4298/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004299 * xmlRelaxNGComputeInterleaves:
4300 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004301 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004302 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004303 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004304 * A lot of work for preprocessing interleave definitions
4305 * is potentially needed to get a decent execution speed at runtime
4306 * - trying to get a total order on the element nodes generated
4307 * by the interleaves, order the list of interleave definitions
4308 * following that order.
4309 * - if <text/> is used to handle mixed content, it is better to
4310 * flag this in the define and simplify the runtime checking
4311 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004312 */
4313static void
Daniel Veillardfd573f12003-03-16 17:52:32 +00004314xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
Daniel Veillard4c004142003-10-07 11:33:24 +00004315 xmlRelaxNGParserCtxtPtr ctxt,
4316 xmlChar * name ATTRIBUTE_UNUSED)
4317{
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004318 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004319
Daniel Veillardfd573f12003-03-16 17:52:32 +00004320 xmlRelaxNGPartitionPtr partitions = NULL;
4321 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4322 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004323 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004324 int nbgroups = 0;
4325 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004326 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004327 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004328
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004329 /*
4330 * Don't run that check in case of error. Infinite recursion
4331 * becomes possible.
4332 */
4333 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004334 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004335
Daniel Veillardfd573f12003-03-16 17:52:32 +00004336#ifdef DEBUG_INTERLEAVE
4337 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004338 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004339#endif
4340 cur = def->content;
4341 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004342 nbchild++;
4343 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004344 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004345
Daniel Veillardfd573f12003-03-16 17:52:32 +00004346#ifdef DEBUG_INTERLEAVE
4347 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4348#endif
4349 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004350 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004351 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004352 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004353 cur = def->content;
4354 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004355 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4356 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4357 if (groups[nbgroups] == NULL)
4358 goto error;
4359 if (cur->type == XML_RELAXNG_TEXT)
4360 is_mixed++;
4361 groups[nbgroups]->rule = cur;
4362 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4363 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4364 nbgroups++;
4365 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004366 }
4367#ifdef DEBUG_INTERLEAVE
4368 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4369#endif
4370
4371 /*
4372 * Let's check that all rules makes a partitions according to 7.4
4373 */
4374 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004375 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004376 if (partitions == NULL)
4377 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004378 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004379 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004380 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004381 for (i = 0; i < nbgroups; i++) {
4382 group = groups[i];
4383 for (j = i + 1; j < nbgroups; j++) {
4384 if (groups[j] == NULL)
4385 continue;
4386
4387 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4388 groups[j]->defs);
4389 if (ret == 0) {
4390 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4391 "Element or text conflicts in interleave\n",
4392 NULL, NULL);
4393 }
4394 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4395 groups[j]->attrs);
4396 if (ret == 0) {
4397 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4398 "Attributes conflicts in interleave\n", NULL,
4399 NULL);
4400 }
4401 }
4402 tmp = group->defs;
4403 if ((tmp != NULL) && (*tmp != NULL)) {
4404 while (*tmp != NULL) {
4405 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4406 res = xmlHashAddEntry2(partitions->triage,
4407 BAD_CAST "#text", NULL,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004408 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004409 if (res != 0)
4410 is_determinist = -1;
4411 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4412 ((*tmp)->name != NULL)) {
4413 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4414 res = xmlHashAddEntry2(partitions->triage,
4415 (*tmp)->name, NULL,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004416 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004417 else
4418 res = xmlHashAddEntry2(partitions->triage,
4419 (*tmp)->name, (*tmp)->ns,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004420 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004421 if (res != 0)
4422 is_determinist = -1;
4423 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4424 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4425 res = xmlHashAddEntry2(partitions->triage,
4426 BAD_CAST "#any", NULL,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004427 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004428 else
4429 res = xmlHashAddEntry2(partitions->triage,
4430 BAD_CAST "#any", (*tmp)->ns,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004431 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004432 if ((*tmp)->nameClass != NULL)
4433 is_determinist = 2;
4434 if (res != 0)
4435 is_determinist = -1;
4436 } else {
4437 is_determinist = -1;
4438 }
4439 tmp++;
4440 }
4441 } else {
4442 is_determinist = 0;
4443 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004444 }
4445 partitions->groups = groups;
4446
4447 /*
4448 * and save the partition list back in the def
4449 */
4450 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004451 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004452 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004453 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004454 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004455 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004456 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004457 return;
4458
Daniel Veillard4c004142003-10-07 11:33:24 +00004459 error:
4460 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004461 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004462 for (i = 0; i < nbgroups; i++)
4463 if (groups[i] != NULL) {
4464 if (groups[i]->defs != NULL)
4465 xmlFree(groups[i]->defs);
4466 xmlFree(groups[i]);
4467 }
4468 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004469 }
4470 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004471}
4472
4473/**
4474 * xmlRelaxNGParseInterleave:
4475 * @ctxt: a Relax-NG parser context
4476 * @node: the data node.
4477 *
4478 * parse the content of a RelaxNG interleave node.
4479 *
4480 * Returns the definition pointer or NULL in case of error
4481 */
4482static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004483xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4484{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004485 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004486 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004487 xmlNodePtr child;
4488
Daniel Veillardfd573f12003-03-16 17:52:32 +00004489 def = xmlRelaxNGNewDefine(ctxt, node);
4490 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004491 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004492 }
4493 def->type = XML_RELAXNG_INTERLEAVE;
4494
4495 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004496 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004497 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004498 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004499 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004500 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004501
Daniel Veillard4c004142003-10-07 11:33:24 +00004502 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4503 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4504 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4505 "Failed to add %s to hash table\n",
4506 (const xmlChar *) name, NULL);
4507 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004508 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004509 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004510 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004511 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4512 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004513 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004514 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004515 if (IS_RELAXNG(child, "element")) {
4516 cur = xmlRelaxNGParseElement(ctxt, child);
4517 } else {
4518 cur = xmlRelaxNGParsePattern(ctxt, child);
4519 }
4520 if (cur != NULL) {
4521 cur->parent = def;
4522 if (last == NULL) {
4523 def->content = last = cur;
4524 } else {
4525 last->next = cur;
4526 last = cur;
4527 }
4528 }
4529 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004530 }
4531
Daniel Veillard4c004142003-10-07 11:33:24 +00004532 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004533}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004534
4535/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004536 * xmlRelaxNGParseInclude:
4537 * @ctxt: a Relax-NG parser context
4538 * @node: the include node
4539 *
4540 * Integrate the content of an include node in the current grammar
4541 *
4542 * Returns 0 in case of success or -1 in case of error
4543 */
4544static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004545xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4546{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004547 xmlRelaxNGIncludePtr incl;
4548 xmlNodePtr root;
4549 int ret = 0, tmp;
4550
Daniel Veillard807daf82004-02-22 22:13:27 +00004551 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004552 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004553 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4554 "Include node has no data\n", NULL, NULL);
4555 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004556 }
4557 root = xmlDocGetRootElement(incl->doc);
4558 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004559 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4560 NULL, NULL);
4561 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004562 }
4563 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004564 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4565 "Include document root is not a grammar\n", NULL, NULL);
4566 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004567 }
4568
4569 /*
4570 * Merge the definition from both the include and the internal list
4571 */
4572 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004573 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4574 if (tmp != 0)
4575 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004576 }
4577 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004578 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4579 if (tmp != 0)
4580 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004581 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004582 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004583}
4584
4585/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004586 * xmlRelaxNGParseDefine:
4587 * @ctxt: a Relax-NG parser context
4588 * @node: the define node
4589 *
4590 * parse the content of a RelaxNG define element node.
4591 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004592 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004593 */
4594static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004595xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4596{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004597 xmlChar *name;
4598 int ret = 0, tmp;
4599 xmlRelaxNGDefinePtr def;
4600 const xmlChar *olddefine;
4601
4602 name = xmlGetProp(node, BAD_CAST "name");
4603 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004604 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4605 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004606 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004607 xmlRelaxNGNormExtSpace(name);
4608 if (xmlValidateNCName(name, 0)) {
4609 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4610 "define name '%s' is not an NCName\n", name, NULL);
4611 }
4612 def = xmlRelaxNGNewDefine(ctxt, node);
4613 if (def == NULL) {
4614 xmlFree(name);
4615 return (-1);
4616 }
4617 def->type = XML_RELAXNG_DEF;
4618 def->name = name;
4619 if (node->children == NULL) {
4620 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4621 "define has no children\n", NULL, NULL);
4622 } else {
4623 olddefine = ctxt->define;
4624 ctxt->define = name;
4625 def->content =
4626 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4627 ctxt->define = olddefine;
4628 }
4629 if (ctxt->grammar->defs == NULL)
4630 ctxt->grammar->defs = xmlHashCreate(10);
4631 if (ctxt->grammar->defs == NULL) {
4632 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4633 "Could not create definition hash\n", NULL, NULL);
4634 ret = -1;
4635 } else {
4636 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4637 if (tmp < 0) {
4638 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004639
Daniel Veillard4c004142003-10-07 11:33:24 +00004640 prev = xmlHashLookup(ctxt->grammar->defs, name);
4641 if (prev == NULL) {
4642 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4643 "Internal error on define aggregation of %s\n",
4644 name, NULL);
4645 ret = -1;
4646 } else {
4647 while (prev->nextHash != NULL)
4648 prev = prev->nextHash;
4649 prev->nextHash = def;
4650 }
4651 }
4652 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004653 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004654 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004655}
4656
4657/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004658 * xmlRelaxNGParseImportRef:
4659 * @payload: the parser context
4660 * @data: the current grammar
4661 * @name: the reference name
4662 *
4663 * Import import one references into the current grammar
4664 */
4665static void
4666xmlRelaxNGParseImportRef(void *payload, void *data, xmlChar *name) {
4667 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4668 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4669 int tmp;
4670
Daniel Veillardaa422d92009-09-24 11:31:48 +02004671 def->dflags |= IS_EXTERNAL_REF;
4672
Daniel Veillard81c51e12009-08-14 18:52:10 +02004673 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4674 if (tmp < 0) {
4675 xmlRelaxNGDefinePtr prev;
4676
4677 prev = (xmlRelaxNGDefinePtr)
4678 xmlHashLookup(ctxt->grammar->refs, def->name);
4679 if (prev == NULL) {
4680 if (def->name != NULL) {
4681 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4682 "Error refs definitions '%s'\n",
4683 def->name, NULL);
4684 } else {
4685 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4686 "Error refs definitions\n",
4687 NULL, NULL);
4688 }
4689 } else {
4690 def->nextHash = prev->nextHash;
4691 prev->nextHash = def;
4692 }
4693 }
4694}
4695
4696/**
4697 * xmlRelaxNGParseImportRefs:
4698 * @ctxt: the parser context
4699 * @grammar: the sub grammar
4700 *
4701 * Import references from the subgrammar into the current grammar
4702 *
4703 * Returns 0 in case of success, -1 in case of failure
4704 */
4705static int
4706xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4707 xmlRelaxNGGrammarPtr grammar) {
4708 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4709 return(-1);
4710 if (grammar->refs == NULL)
4711 return(0);
4712 if (ctxt->grammar->refs == NULL)
4713 ctxt->grammar->refs = xmlHashCreate(10);
4714 if (ctxt->grammar->refs == NULL) {
4715 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4716 "Could not create references hash\n", NULL, NULL);
4717 return(-1);
4718 }
4719 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004720 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004721}
4722
4723/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004724 * xmlRelaxNGProcessExternalRef:
4725 * @ctxt: the parser context
4726 * @node: the externlRef node
4727 *
4728 * Process and compile an externlRef node
4729 *
4730 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4731 */
4732static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004733xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4734{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004735 xmlRelaxNGDocumentPtr docu;
4736 xmlNodePtr root, tmp;
4737 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004738 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004739 xmlRelaxNGDefinePtr def;
4740
Daniel Veillard807daf82004-02-22 22:13:27 +00004741 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004742 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004743 def = xmlRelaxNGNewDefine(ctxt, node);
4744 if (def == NULL)
4745 return (NULL);
4746 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004747
Daniel Veillard4c004142003-10-07 11:33:24 +00004748 if (docu->content == NULL) {
4749 /*
4750 * Then do the parsing for good
4751 */
4752 root = xmlDocGetRootElement(docu->doc);
4753 if (root == NULL) {
4754 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4755 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4756 NULL);
4757 return (NULL);
4758 }
4759 /*
4760 * ns transmission rules
4761 */
4762 ns = xmlGetProp(root, BAD_CAST "ns");
4763 if (ns == NULL) {
4764 tmp = node;
4765 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4766 ns = xmlGetProp(tmp, BAD_CAST "ns");
4767 if (ns != NULL) {
4768 break;
4769 }
4770 tmp = tmp->parent;
4771 }
4772 if (ns != NULL) {
4773 xmlSetProp(root, BAD_CAST "ns", ns);
4774 newNs = 1;
4775 xmlFree(ns);
4776 }
4777 } else {
4778 xmlFree(ns);
4779 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004780
Daniel Veillard4c004142003-10-07 11:33:24 +00004781 /*
4782 * Parsing to get a precompiled schemas.
4783 */
4784 oldflags = ctxt->flags;
4785 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4786 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4787 ctxt->flags = oldflags;
4788 if ((docu->schema != NULL) &&
4789 (docu->schema->topgrammar != NULL)) {
4790 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004791 if (docu->schema->topgrammar->refs)
4792 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004793 }
4794
4795 /*
4796 * the externalRef may be reused in a different ns context
4797 */
4798 if (newNs == 1) {
4799 xmlUnsetProp(root, BAD_CAST "ns");
4800 }
4801 }
4802 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004803 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004804 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004805 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004806 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004807}
4808
4809/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004810 * xmlRelaxNGParsePattern:
4811 * @ctxt: a Relax-NG parser context
4812 * @node: the pattern node.
4813 *
4814 * parse the content of a RelaxNG pattern node.
4815 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004816 * Returns the definition pointer or NULL in case of error or if no
4817 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004818 */
4819static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004820xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4821{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004822 xmlRelaxNGDefinePtr def = NULL;
4823
Daniel Veillardd2298792003-02-14 16:54:11 +00004824 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004825 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004826 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004827 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004828 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004829 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004830 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004831 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004832 def = xmlRelaxNGNewDefine(ctxt, node);
4833 if (def == NULL)
4834 return (NULL);
4835 def->type = XML_RELAXNG_EMPTY;
4836 if (node->children != NULL) {
4837 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4838 "empty: had a child node\n", NULL, NULL);
4839 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004840 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004841 def = xmlRelaxNGNewDefine(ctxt, node);
4842 if (def == NULL)
4843 return (NULL);
4844 def->type = XML_RELAXNG_TEXT;
4845 if (node->children != NULL) {
4846 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4847 "text: had a child node\n", NULL, NULL);
4848 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004849 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004850 def = xmlRelaxNGNewDefine(ctxt, node);
4851 if (def == NULL)
4852 return (NULL);
4853 def->type = XML_RELAXNG_ZEROORMORE;
4854 if (node->children == NULL) {
4855 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4856 "Element %s is empty\n", node->name, NULL);
4857 } else {
4858 def->content =
4859 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4860 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004861 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004862 def = xmlRelaxNGNewDefine(ctxt, node);
4863 if (def == NULL)
4864 return (NULL);
4865 def->type = XML_RELAXNG_ONEORMORE;
4866 if (node->children == NULL) {
4867 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4868 "Element %s is empty\n", node->name, NULL);
4869 } else {
4870 def->content =
4871 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4872 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004873 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004874 def = xmlRelaxNGNewDefine(ctxt, node);
4875 if (def == NULL)
4876 return (NULL);
4877 def->type = XML_RELAXNG_OPTIONAL;
4878 if (node->children == NULL) {
4879 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4880 "Element %s is empty\n", node->name, NULL);
4881 } else {
4882 def->content =
4883 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4884 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004885 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004886 def = xmlRelaxNGNewDefine(ctxt, node);
4887 if (def == NULL)
4888 return (NULL);
4889 def->type = XML_RELAXNG_CHOICE;
4890 if (node->children == NULL) {
4891 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4892 "Element %s is empty\n", node->name, NULL);
4893 } else {
4894 def->content =
4895 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4896 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004897 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004898 def = xmlRelaxNGNewDefine(ctxt, node);
4899 if (def == NULL)
4900 return (NULL);
4901 def->type = XML_RELAXNG_GROUP;
4902 if (node->children == NULL) {
4903 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4904 "Element %s is empty\n", node->name, NULL);
4905 } else {
4906 def->content =
4907 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4908 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004909 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004910 def = xmlRelaxNGNewDefine(ctxt, node);
4911 if (def == NULL)
4912 return (NULL);
4913 def->type = XML_RELAXNG_REF;
4914 def->name = xmlGetProp(node, BAD_CAST "name");
4915 if (def->name == NULL) {
4916 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4917 NULL, NULL);
4918 } else {
4919 xmlRelaxNGNormExtSpace(def->name);
4920 if (xmlValidateNCName(def->name, 0)) {
4921 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4922 "ref name '%s' is not an NCName\n", def->name,
4923 NULL);
4924 }
4925 }
4926 if (node->children != NULL) {
4927 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4928 NULL, NULL);
4929 }
4930 if (ctxt->grammar->refs == NULL)
4931 ctxt->grammar->refs = xmlHashCreate(10);
4932 if (ctxt->grammar->refs == NULL) {
4933 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4934 "Could not create references hash\n", NULL, NULL);
4935 def = NULL;
4936 } else {
4937 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004938
Daniel Veillard4c004142003-10-07 11:33:24 +00004939 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4940 if (tmp < 0) {
4941 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004942
Daniel Veillard4c004142003-10-07 11:33:24 +00004943 prev = (xmlRelaxNGDefinePtr)
4944 xmlHashLookup(ctxt->grammar->refs, def->name);
4945 if (prev == NULL) {
4946 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004947 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4948 "Error refs definitions '%s'\n",
4949 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004950 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004951 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4952 "Error refs definitions\n",
4953 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004954 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004955 def = NULL;
4956 } else {
4957 def->nextHash = prev->nextHash;
4958 prev->nextHash = def;
4959 }
4960 }
4961 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004962 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004963 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004964 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004965 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004966 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004967 def = xmlRelaxNGNewDefine(ctxt, node);
4968 if (def == NULL)
4969 return (NULL);
4970 def->type = XML_RELAXNG_LIST;
4971 if (node->children == NULL) {
4972 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4973 "Element %s is empty\n", node->name, NULL);
4974 } else {
4975 def->content =
4976 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4977 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004978 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004979 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00004980 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004981 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004982 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004983 def = xmlRelaxNGNewDefine(ctxt, node);
4984 if (def == NULL)
4985 return (NULL);
4986 def->type = XML_RELAXNG_NOT_ALLOWED;
4987 if (node->children != NULL) {
4988 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4989 "xmlRelaxNGParse: notAllowed element is not empty\n",
4990 NULL, NULL);
4991 }
Daniel Veillard419a7682003-02-03 23:22:49 +00004992 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004993 xmlRelaxNGGrammarPtr grammar, old;
4994 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00004995
Daniel Veillardc482e262003-02-26 14:48:48 +00004996#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00004997 xmlGenericError(xmlGenericErrorContext,
4998 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00004999#endif
5000
Daniel Veillard4c004142003-10-07 11:33:24 +00005001 oldparent = ctxt->parentgrammar;
5002 old = ctxt->grammar;
5003 ctxt->parentgrammar = old;
5004 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5005 if (old != NULL) {
5006 ctxt->grammar = old;
5007 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00005008#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00005009 if (grammar != NULL) {
5010 grammar->next = old->next;
5011 old->next = grammar;
5012 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005013#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00005014 }
5015 if (grammar != NULL)
5016 def = grammar->start;
5017 else
5018 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00005019 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005020 if (ctxt->parentgrammar == NULL) {
5021 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5022 "Use of parentRef without a parent grammar\n", NULL,
5023 NULL);
5024 return (NULL);
5025 }
5026 def = xmlRelaxNGNewDefine(ctxt, node);
5027 if (def == NULL)
5028 return (NULL);
5029 def->type = XML_RELAXNG_PARENTREF;
5030 def->name = xmlGetProp(node, BAD_CAST "name");
5031 if (def->name == NULL) {
5032 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5033 "parentRef has no name\n", NULL, NULL);
5034 } else {
5035 xmlRelaxNGNormExtSpace(def->name);
5036 if (xmlValidateNCName(def->name, 0)) {
5037 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5038 "parentRef name '%s' is not an NCName\n",
5039 def->name, NULL);
5040 }
5041 }
5042 if (node->children != NULL) {
5043 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5044 "parentRef is not empty\n", NULL, NULL);
5045 }
5046 if (ctxt->parentgrammar->refs == NULL)
5047 ctxt->parentgrammar->refs = xmlHashCreate(10);
5048 if (ctxt->parentgrammar->refs == NULL) {
5049 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5050 "Could not create references hash\n", NULL, NULL);
5051 def = NULL;
5052 } else if (def->name != NULL) {
5053 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005054
Daniel Veillard4c004142003-10-07 11:33:24 +00005055 tmp =
5056 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5057 if (tmp < 0) {
5058 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005059
Daniel Veillard4c004142003-10-07 11:33:24 +00005060 prev = (xmlRelaxNGDefinePtr)
5061 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5062 if (prev == NULL) {
5063 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5064 "Internal error parentRef definitions '%s'\n",
5065 def->name, NULL);
5066 def = NULL;
5067 } else {
5068 def->nextHash = prev->nextHash;
5069 prev->nextHash = def;
5070 }
5071 }
5072 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005073 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005074 if (node->children == NULL) {
5075 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5076 NULL, NULL);
5077 def = NULL;
5078 } else {
5079 def = xmlRelaxNGParseInterleave(ctxt, node);
5080 if (def != NULL) {
5081 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005082
Daniel Veillard4c004142003-10-07 11:33:24 +00005083 if ((def->content != NULL) && (def->content->next != NULL)) {
5084 tmp = xmlRelaxNGNewDefine(ctxt, node);
5085 if (tmp != NULL) {
5086 tmp->type = XML_RELAXNG_GROUP;
5087 tmp->content = def->content;
5088 def->content = tmp;
5089 }
5090 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005091
Daniel Veillard4c004142003-10-07 11:33:24 +00005092 tmp = xmlRelaxNGNewDefine(ctxt, node);
5093 if (tmp == NULL)
5094 return (def);
5095 tmp->type = XML_RELAXNG_TEXT;
5096 tmp->next = def->content;
5097 def->content = tmp;
5098 }
5099 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005100 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005101 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5102 "Unexpected node %s is not a pattern\n", node->name,
5103 NULL);
5104 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005105 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005106 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005107}
5108
5109/**
5110 * xmlRelaxNGParseAttribute:
5111 * @ctxt: a Relax-NG parser context
5112 * @node: the element node
5113 *
5114 * parse the content of a RelaxNG attribute node.
5115 *
5116 * Returns the definition pointer or NULL in case of error.
5117 */
5118static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005119xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5120{
Daniel Veillardd2298792003-02-14 16:54:11 +00005121 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005122 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005123 int old_flags;
5124
Daniel Veillardfd573f12003-03-16 17:52:32 +00005125 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005126 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005127 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005128 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005129 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005130 child = node->children;
5131 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005132 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5133 "xmlRelaxNGParseattribute: attribute has no children\n",
5134 NULL, NULL);
5135 return (ret);
5136 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005137 old_flags = ctxt->flags;
5138 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005139 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5140 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005141 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005142
Daniel Veillardd2298792003-02-14 16:54:11 +00005143 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005144 cur = xmlRelaxNGParsePattern(ctxt, child);
5145 if (cur != NULL) {
5146 switch (cur->type) {
5147 case XML_RELAXNG_EMPTY:
5148 case XML_RELAXNG_NOT_ALLOWED:
5149 case XML_RELAXNG_TEXT:
5150 case XML_RELAXNG_ELEMENT:
5151 case XML_RELAXNG_DATATYPE:
5152 case XML_RELAXNG_VALUE:
5153 case XML_RELAXNG_LIST:
5154 case XML_RELAXNG_REF:
5155 case XML_RELAXNG_PARENTREF:
5156 case XML_RELAXNG_EXTERNALREF:
5157 case XML_RELAXNG_DEF:
5158 case XML_RELAXNG_ONEORMORE:
5159 case XML_RELAXNG_ZEROORMORE:
5160 case XML_RELAXNG_OPTIONAL:
5161 case XML_RELAXNG_CHOICE:
5162 case XML_RELAXNG_GROUP:
5163 case XML_RELAXNG_INTERLEAVE:
5164 case XML_RELAXNG_ATTRIBUTE:
5165 ret->content = cur;
5166 cur->parent = ret;
5167 break;
5168 case XML_RELAXNG_START:
5169 case XML_RELAXNG_PARAM:
5170 case XML_RELAXNG_EXCEPT:
5171 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5172 "attribute has invalid content\n", NULL,
5173 NULL);
5174 break;
5175 case XML_RELAXNG_NOOP:
5176 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5177 "RNG Internal error, noop found in attribute\n",
5178 NULL, NULL);
5179 break;
5180 }
5181 }
5182 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005183 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005184 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005185 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5186 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005187 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005188 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005189 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005190}
5191
5192/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005193 * xmlRelaxNGParseExceptNameClass:
5194 * @ctxt: a Relax-NG parser context
5195 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005196 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005197 *
5198 * parse the content of a RelaxNG nameClass node.
5199 *
5200 * Returns the definition pointer or NULL in case of error.
5201 */
5202static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005203xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005204 xmlNodePtr node, int attr)
5205{
Daniel Veillard144fae12003-02-03 13:17:57 +00005206 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5207 xmlNodePtr child;
5208
Daniel Veillardd2298792003-02-14 16:54:11 +00005209 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005210 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5211 "Expecting an except node\n", NULL, NULL);
5212 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005213 }
5214 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005215 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5216 "exceptNameClass allows only a single except node\n",
5217 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005218 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005219 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005220 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5221 NULL, NULL);
5222 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005223 }
5224
Daniel Veillardfd573f12003-03-16 17:52:32 +00005225 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005226 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005227 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005228 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005229 child = node->children;
5230 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005231 cur = xmlRelaxNGNewDefine(ctxt, child);
5232 if (cur == NULL)
5233 break;
5234 if (attr)
5235 cur->type = XML_RELAXNG_ATTRIBUTE;
5236 else
5237 cur->type = XML_RELAXNG_ELEMENT;
5238
Daniel Veillard419a7682003-02-03 23:22:49 +00005239 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005240 if (last == NULL) {
5241 ret->content = cur;
5242 } else {
5243 last->next = cur;
5244 }
5245 last = cur;
5246 }
5247 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005248 }
5249
Daniel Veillard4c004142003-10-07 11:33:24 +00005250 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005251}
5252
5253/**
5254 * xmlRelaxNGParseNameClass:
5255 * @ctxt: a Relax-NG parser context
5256 * @node: the nameClass node
5257 * @def: the current definition
5258 *
5259 * parse the content of a RelaxNG nameClass node.
5260 *
5261 * Returns the definition pointer or NULL in case of error.
5262 */
5263static xmlRelaxNGDefinePtr
5264xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005265 xmlRelaxNGDefinePtr def)
5266{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005267 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005268 xmlChar *val;
5269
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005270 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005271 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005272 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005273 if ((def->type != XML_RELAXNG_ELEMENT) &&
5274 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5275 ret = xmlRelaxNGNewDefine(ctxt, node);
5276 if (ret == NULL)
5277 return (NULL);
5278 ret->parent = def;
5279 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5280 ret->type = XML_RELAXNG_ATTRIBUTE;
5281 else
5282 ret->type = XML_RELAXNG_ELEMENT;
5283 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005284 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005285 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005286 val = xmlNodeGetContent(node);
5287 xmlRelaxNGNormExtSpace(val);
5288 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005289 if (node->parent != NULL)
5290 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5291 "Element %s name '%s' is not an NCName\n",
5292 node->parent->name, val);
5293 else
5294 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5295 "name '%s' is not an NCName\n",
5296 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005297 }
5298 ret->name = val;
5299 val = xmlGetProp(node, BAD_CAST "ns");
5300 ret->ns = val;
5301 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5302 (val != NULL) &&
5303 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005304 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005305 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005306 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005307 }
5308 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5309 (val != NULL) &&
5310 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005311 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5312 "Attribute with QName 'xmlns' is not allowed\n",
5313 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005314 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005315 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005316 ret->name = NULL;
5317 ret->ns = NULL;
5318 if (node->children != NULL) {
5319 ret->nameClass =
5320 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5321 (def->type ==
5322 XML_RELAXNG_ATTRIBUTE));
5323 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005324 } else if (IS_RELAXNG(node, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005325 ret->name = NULL;
5326 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5327 if (ret->ns == NULL) {
5328 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5329 "nsName has no ns attribute\n", NULL, NULL);
5330 }
5331 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5332 (ret->ns != NULL) &&
5333 (xmlStrEqual
5334 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5335 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5336 "Attribute with namespace '%s' is not allowed\n",
5337 ret->ns, NULL);
5338 }
5339 if (node->children != NULL) {
5340 ret->nameClass =
5341 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5342 (def->type ==
5343 XML_RELAXNG_ATTRIBUTE));
5344 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005345 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005346 xmlNodePtr child;
5347 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005348
Daniel Veillard4c004142003-10-07 11:33:24 +00005349 ret = xmlRelaxNGNewDefine(ctxt, node);
5350 if (ret == NULL)
5351 return (NULL);
5352 ret->parent = def;
5353 ret->type = XML_RELAXNG_CHOICE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005354
Daniel Veillard4c004142003-10-07 11:33:24 +00005355 if (node->children == NULL) {
5356 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5357 "Element choice is empty\n", NULL, NULL);
5358 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005359
Daniel Veillard4c004142003-10-07 11:33:24 +00005360 child = node->children;
5361 while (child != NULL) {
5362 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5363 if (tmp != NULL) {
5364 if (last == NULL) {
5365 last = ret->nameClass = tmp;
5366 } else {
5367 last->next = tmp;
5368 last = tmp;
5369 }
5370 }
5371 child = child->next;
5372 }
5373 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005374 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005375 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5376 "expecting name, anyName, nsName or choice : got %s\n",
Ben Waltona7a6a4b2010-03-15 10:06:36 +01005377 (node == NULL ? (const xmlChar *) "nothing" : node->name),
5378 NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005379 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005380 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005381 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005382 if (def->nameClass == NULL) {
5383 def->nameClass = ret;
5384 } else {
5385 tmp = def->nameClass;
5386 while (tmp->next != NULL) {
5387 tmp = tmp->next;
5388 }
5389 tmp->next = ret;
5390 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005391 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005392 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005393}
5394
5395/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005396 * xmlRelaxNGParseElement:
5397 * @ctxt: a Relax-NG parser context
5398 * @node: the element node
5399 *
5400 * parse the content of a RelaxNG element node.
5401 *
5402 * Returns the definition pointer or NULL in case of error.
5403 */
5404static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005405xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5406{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005407 xmlRelaxNGDefinePtr ret, cur, last;
5408 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005409 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005410
Daniel Veillardfd573f12003-03-16 17:52:32 +00005411 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005412 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005413 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005414 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005415 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005416 child = node->children;
5417 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005418 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5419 "xmlRelaxNGParseElement: element has no children\n",
5420 NULL, NULL);
5421 return (ret);
5422 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005423 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5424 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005425 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005426
Daniel Veillard6eadf632003-01-23 18:29:16 +00005427 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005428 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5429 "xmlRelaxNGParseElement: element has no content\n",
5430 NULL, NULL);
5431 return (ret);
5432 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005433 olddefine = ctxt->define;
5434 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005435 last = NULL;
5436 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005437 cur = xmlRelaxNGParsePattern(ctxt, child);
5438 if (cur != NULL) {
5439 cur->parent = ret;
5440 switch (cur->type) {
5441 case XML_RELAXNG_EMPTY:
5442 case XML_RELAXNG_NOT_ALLOWED:
5443 case XML_RELAXNG_TEXT:
5444 case XML_RELAXNG_ELEMENT:
5445 case XML_RELAXNG_DATATYPE:
5446 case XML_RELAXNG_VALUE:
5447 case XML_RELAXNG_LIST:
5448 case XML_RELAXNG_REF:
5449 case XML_RELAXNG_PARENTREF:
5450 case XML_RELAXNG_EXTERNALREF:
5451 case XML_RELAXNG_DEF:
5452 case XML_RELAXNG_ZEROORMORE:
5453 case XML_RELAXNG_ONEORMORE:
5454 case XML_RELAXNG_OPTIONAL:
5455 case XML_RELAXNG_CHOICE:
5456 case XML_RELAXNG_GROUP:
5457 case XML_RELAXNG_INTERLEAVE:
5458 if (last == NULL) {
5459 ret->content = last = cur;
5460 } else {
5461 if ((last->type == XML_RELAXNG_ELEMENT) &&
5462 (ret->content == last)) {
5463 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5464 if (ret->content != NULL) {
5465 ret->content->type = XML_RELAXNG_GROUP;
5466 ret->content->content = last;
5467 } else {
5468 ret->content = last;
5469 }
5470 }
5471 last->next = cur;
5472 last = cur;
5473 }
5474 break;
5475 case XML_RELAXNG_ATTRIBUTE:
5476 cur->next = ret->attrs;
5477 ret->attrs = cur;
5478 break;
5479 case XML_RELAXNG_START:
5480 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5481 "RNG Internal error, start found in element\n",
5482 NULL, NULL);
5483 break;
5484 case XML_RELAXNG_PARAM:
5485 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5486 "RNG Internal error, param found in element\n",
5487 NULL, NULL);
5488 break;
5489 case XML_RELAXNG_EXCEPT:
5490 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5491 "RNG Internal error, except found in element\n",
5492 NULL, NULL);
5493 break;
5494 case XML_RELAXNG_NOOP:
5495 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5496 "RNG Internal error, noop found in element\n",
5497 NULL, NULL);
5498 break;
5499 }
5500 }
5501 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005502 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005503 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005504 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005505}
5506
5507/**
5508 * xmlRelaxNGParsePatterns:
5509 * @ctxt: a Relax-NG parser context
5510 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005511 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005512 *
5513 * parse the content of a RelaxNG start node.
5514 *
5515 * Returns the definition pointer or NULL in case of error.
5516 */
5517static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005518xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005519 int group)
5520{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005521 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005522
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005523 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005524 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005525 if (IS_RELAXNG(nodes, "element")) {
5526 cur = xmlRelaxNGParseElement(ctxt, nodes);
5527 if (def == NULL) {
5528 def = last = cur;
5529 } else {
5530 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5531 (def == last)) {
5532 def = xmlRelaxNGNewDefine(ctxt, nodes);
5533 def->type = XML_RELAXNG_GROUP;
5534 def->content = last;
5535 }
5536 last->next = cur;
5537 last = cur;
5538 }
5539 cur->parent = parent;
5540 } else {
5541 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5542 if (cur != NULL) {
5543 if (def == NULL) {
5544 def = last = cur;
5545 } else {
5546 last->next = cur;
5547 last = cur;
5548 }
5549 }
5550 }
5551 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005552 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005553 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005554}
5555
5556/**
5557 * xmlRelaxNGParseStart:
5558 * @ctxt: a Relax-NG parser context
5559 * @nodes: start children nodes
5560 *
5561 * parse the content of a RelaxNG start node.
5562 *
5563 * Returns 0 in case of success, -1 in case of error
5564 */
5565static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005566xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5567{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005568 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005569 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005570
Daniel Veillardd2298792003-02-14 16:54:11 +00005571 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005572 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5573 NULL, NULL);
5574 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005575 }
5576 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005577 def = xmlRelaxNGNewDefine(ctxt, nodes);
5578 if (def == NULL)
5579 return (-1);
5580 def->type = XML_RELAXNG_EMPTY;
5581 if (nodes->children != NULL) {
5582 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5583 "element empty is not empty\n", NULL, NULL);
5584 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005585 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005586 def = xmlRelaxNGNewDefine(ctxt, nodes);
5587 if (def == NULL)
5588 return (-1);
5589 def->type = XML_RELAXNG_NOT_ALLOWED;
5590 if (nodes->children != NULL) {
5591 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5592 "element notAllowed is not empty\n", NULL, NULL);
5593 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005594 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005595 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005596 }
5597 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005598 last = ctxt->grammar->start;
5599 while (last->next != NULL)
5600 last = last->next;
5601 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005602 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005603 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005604 }
5605 nodes = nodes->next;
5606 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005607 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5608 "start more than one children\n", NULL, NULL);
5609 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005610 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005611 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005612}
5613
5614/**
5615 * xmlRelaxNGParseGrammarContent:
5616 * @ctxt: a Relax-NG parser context
5617 * @nodes: grammar children nodes
5618 *
5619 * parse the content of a RelaxNG grammar node.
5620 *
5621 * Returns 0 in case of success, -1 in case of error
5622 */
5623static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005624xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5625 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005626{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005627 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005628
5629 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005630 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5631 "grammar has no children\n", NULL, NULL);
5632 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005633 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005634 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005635 if (IS_RELAXNG(nodes, "start")) {
5636 if (nodes->children == NULL) {
5637 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5638 "start has no children\n", NULL, NULL);
5639 } else {
5640 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5641 if (tmp != 0)
5642 ret = -1;
5643 }
5644 } else if (IS_RELAXNG(nodes, "define")) {
5645 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5646 if (tmp != 0)
5647 ret = -1;
5648 } else if (IS_RELAXNG(nodes, "include")) {
5649 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5650 if (tmp != 0)
5651 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005652 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005653 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5654 "grammar has unexpected child %s\n", nodes->name,
5655 NULL);
5656 ret = -1;
5657 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005658 nodes = nodes->next;
5659 }
5660 return (ret);
5661}
5662
5663/**
5664 * xmlRelaxNGCheckReference:
5665 * @ref: the ref
5666 * @ctxt: a Relax-NG parser context
5667 * @name: the name associated to the defines
5668 *
5669 * Applies the 4.17. combine attribute rule for all the define
5670 * element of a given grammar using the same name.
5671 */
5672static void
5673xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
Daniel Veillard4c004142003-10-07 11:33:24 +00005674 xmlRelaxNGParserCtxtPtr ctxt,
5675 const xmlChar * name)
5676{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005677 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005678 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005679
Daniel Veillardaa422d92009-09-24 11:31:48 +02005680 /*
5681 * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5682 */
5683 if (ref->dflags & IS_EXTERNAL_REF)
5684 return;
5685
Daniel Veillard6eadf632003-01-23 18:29:16 +00005686 grammar = ctxt->grammar;
5687 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005688 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5689 "Internal error: no grammar in CheckReference %s\n",
5690 name, NULL);
5691 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005692 }
5693 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005694 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5695 "Internal error: reference has content in CheckReference %s\n",
5696 name, NULL);
5697 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005698 }
5699 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005700 def = xmlHashLookup(grammar->defs, name);
5701 if (def != NULL) {
5702 cur = ref;
5703 while (cur != NULL) {
5704 cur->content = def;
5705 cur = cur->nextHash;
5706 }
5707 } else {
5708 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5709 "Reference %s has no matching definition\n", name,
5710 NULL);
5711 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005712 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005713 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5714 "Reference %s has no matching definition\n", name,
5715 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005716 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005717}
5718
5719/**
5720 * xmlRelaxNGCheckCombine:
5721 * @define: the define(s) list
5722 * @ctxt: a Relax-NG parser context
5723 * @name: the name associated to the defines
5724 *
5725 * Applies the 4.17. combine attribute rule for all the define
5726 * element of a given grammar using the same name.
5727 */
5728static void
5729xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
Daniel Veillard4c004142003-10-07 11:33:24 +00005730 xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5731{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005732 xmlChar *combine;
5733 int choiceOrInterleave = -1;
5734 int missing = 0;
5735 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5736
5737 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005738 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005739 cur = define;
5740 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005741 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5742 if (combine != NULL) {
5743 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5744 if (choiceOrInterleave == -1)
5745 choiceOrInterleave = 1;
5746 else if (choiceOrInterleave == 0) {
5747 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5748 "Defines for %s use both 'choice' and 'interleave'\n",
5749 name, NULL);
5750 }
5751 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5752 if (choiceOrInterleave == -1)
5753 choiceOrInterleave = 0;
5754 else if (choiceOrInterleave == 1) {
5755 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5756 "Defines for %s use both 'choice' and 'interleave'\n",
5757 name, NULL);
5758 }
5759 } else {
5760 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5761 "Defines for %s use unknown combine value '%s''\n",
5762 name, combine);
5763 }
5764 xmlFree(combine);
5765 } else {
5766 if (missing == 0)
5767 missing = 1;
5768 else {
5769 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5770 "Some defines for %s needs the combine attribute\n",
5771 name, NULL);
5772 }
5773 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005774
Daniel Veillard4c004142003-10-07 11:33:24 +00005775 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005776 }
5777#ifdef DEBUG
5778 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005779 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5780 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005781#endif
5782 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005783 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005784 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005785 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005786 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005787 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005788 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005789 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005790 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005791 tmp = define;
5792 last = NULL;
5793 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005794 if (tmp->content != NULL) {
5795 if (tmp->content->next != NULL) {
5796 /*
5797 * we need first to create a wrapper.
5798 */
5799 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5800 if (tmp2 == NULL)
5801 break;
5802 tmp2->type = XML_RELAXNG_GROUP;
5803 tmp2->content = tmp->content;
5804 } else {
5805 tmp2 = tmp->content;
5806 }
5807 if (last == NULL) {
5808 cur->content = tmp2;
5809 } else {
5810 last->next = tmp2;
5811 }
5812 last = tmp2;
5813 }
5814 tmp->content = cur;
5815 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005816 }
5817 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005818 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005819 if (ctxt->interleaves == NULL)
5820 ctxt->interleaves = xmlHashCreate(10);
5821 if (ctxt->interleaves == NULL) {
5822 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5823 "Failed to create interleaves hash table\n", NULL,
5824 NULL);
5825 } else {
5826 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005827
Daniel Veillard4c004142003-10-07 11:33:24 +00005828 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5829 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5830 0) {
5831 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5832 "Failed to add %s to hash table\n",
5833 (const xmlChar *) tmpname, NULL);
5834 }
5835 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005836 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005837}
5838
5839/**
5840 * xmlRelaxNGCombineStart:
5841 * @ctxt: a Relax-NG parser context
5842 * @grammar: the grammar
5843 *
5844 * Applies the 4.17. combine rule for all the start
5845 * element of a given grammar.
5846 */
5847static void
5848xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005849 xmlRelaxNGGrammarPtr grammar)
5850{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005851 xmlRelaxNGDefinePtr starts;
5852 xmlChar *combine;
5853 int choiceOrInterleave = -1;
5854 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005855 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005856
Daniel Veillard2df2de22003-02-17 23:34:33 +00005857 starts = grammar->start;
5858 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005859 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005860 cur = starts;
5861 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005862 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5863 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5864 combine = NULL;
5865 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5866 "Internal error: start element not found\n", NULL,
5867 NULL);
5868 } else {
5869 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5870 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005871
Daniel Veillard4c004142003-10-07 11:33:24 +00005872 if (combine != NULL) {
5873 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5874 if (choiceOrInterleave == -1)
5875 choiceOrInterleave = 1;
5876 else if (choiceOrInterleave == 0) {
5877 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5878 "<start> use both 'choice' and 'interleave'\n",
5879 NULL, NULL);
5880 }
5881 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5882 if (choiceOrInterleave == -1)
5883 choiceOrInterleave = 0;
5884 else if (choiceOrInterleave == 1) {
5885 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5886 "<start> use both 'choice' and 'interleave'\n",
5887 NULL, NULL);
5888 }
5889 } else {
5890 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5891 "<start> uses unknown combine value '%s''\n",
5892 combine, NULL);
5893 }
5894 xmlFree(combine);
5895 } else {
5896 if (missing == 0)
5897 missing = 1;
5898 else {
5899 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5900 "Some <start> element miss the combine attribute\n",
5901 NULL, NULL);
5902 }
5903 }
5904
5905 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005906 }
5907#ifdef DEBUG
5908 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005909 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5910 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005911#endif
5912 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005913 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005914 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005915 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005916 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005917 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005918 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005919 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005920 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005921 cur->content = grammar->start;
5922 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005923 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005924 if (ctxt->interleaves == NULL)
5925 ctxt->interleaves = xmlHashCreate(10);
5926 if (ctxt->interleaves == NULL) {
5927 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5928 "Failed to create interleaves hash table\n", NULL,
5929 NULL);
5930 } else {
5931 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005932
Daniel Veillard4c004142003-10-07 11:33:24 +00005933 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5934 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5935 0) {
5936 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5937 "Failed to add %s to hash table\n",
5938 (const xmlChar *) tmpname, NULL);
5939 }
5940 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005941 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005942}
5943
5944/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005945 * xmlRelaxNGCheckCycles:
5946 * @ctxt: a Relax-NG parser context
5947 * @nodes: grammar children nodes
5948 * @depth: the counter
5949 *
5950 * Check for cycles.
5951 *
5952 * Returns 0 if check passed, and -1 in case of error
5953 */
5954static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005955xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5956 xmlRelaxNGDefinePtr cur, int depth)
5957{
Daniel Veillardd4310742003-02-18 21:12:46 +00005958 int ret = 0;
5959
5960 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005961 if ((cur->type == XML_RELAXNG_REF) ||
5962 (cur->type == XML_RELAXNG_PARENTREF)) {
5963 if (cur->depth == -1) {
5964 cur->depth = depth;
5965 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5966 cur->depth = -2;
5967 } else if (depth == cur->depth) {
5968 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5969 "Detected a cycle in %s references\n",
5970 cur->name, NULL);
5971 return (-1);
5972 }
5973 } else if (cur->type == XML_RELAXNG_ELEMENT) {
5974 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5975 } else {
5976 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5977 }
5978 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00005979 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005980 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00005981}
5982
5983/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00005984 * xmlRelaxNGTryUnlink:
5985 * @ctxt: a Relax-NG parser context
5986 * @cur: the definition to unlink
5987 * @parent: the parent definition
5988 * @prev: the previous sibling definition
5989 *
5990 * Try to unlink a definition. If not possble make it a NOOP
5991 *
5992 * Returns the new prev definition
5993 */
5994static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005995xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5996 xmlRelaxNGDefinePtr cur,
5997 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5998{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005999 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006000 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006001 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006002 if (parent != NULL) {
6003 if (parent->content == cur)
6004 parent->content = cur->next;
6005 else if (parent->attrs == cur)
6006 parent->attrs = cur->next;
6007 else if (parent->nameClass == cur)
6008 parent->nameClass = cur->next;
6009 } else {
6010 cur->type = XML_RELAXNG_NOOP;
6011 prev = cur;
6012 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006013 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006014 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006015}
6016
6017/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006018 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006019 * @ctxt: a Relax-NG parser context
6020 * @nodes: grammar children nodes
6021 *
6022 * Check for simplification of empty and notAllowed
6023 */
6024static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006025xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6026 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6027{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006028 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006029
Daniel Veillardfd573f12003-03-16 17:52:32 +00006030 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006031 if ((cur->type == XML_RELAXNG_REF) ||
6032 (cur->type == XML_RELAXNG_PARENTREF)) {
6033 if (cur->depth != -3) {
6034 cur->depth = -3;
6035 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6036 }
6037 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6038 cur->parent = parent;
6039 if ((parent != NULL) &&
6040 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6041 (parent->type == XML_RELAXNG_LIST) ||
6042 (parent->type == XML_RELAXNG_GROUP) ||
6043 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6044 (parent->type == XML_RELAXNG_ONEORMORE) ||
6045 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6046 parent->type = XML_RELAXNG_NOT_ALLOWED;
6047 break;
6048 }
6049 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6050 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6051 } else
6052 prev = cur;
6053 } else if (cur->type == XML_RELAXNG_EMPTY) {
6054 cur->parent = parent;
6055 if ((parent != NULL) &&
6056 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6057 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6058 parent->type = XML_RELAXNG_EMPTY;
6059 break;
6060 }
6061 if ((parent != NULL) &&
6062 ((parent->type == XML_RELAXNG_GROUP) ||
6063 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6064 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6065 } else
6066 prev = cur;
6067 } else {
6068 cur->parent = parent;
6069 if (cur->content != NULL)
6070 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6071 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6072 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6073 if (cur->nameClass != NULL)
6074 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6075 /*
6076 * On Elements, try to move attribute only generating rules on
6077 * the attrs rules.
6078 */
6079 if (cur->type == XML_RELAXNG_ELEMENT) {
6080 int attronly;
6081 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006082
Daniel Veillard4c004142003-10-07 11:33:24 +00006083 while (cur->content != NULL) {
6084 attronly =
6085 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6086 if (attronly == 1) {
6087 /*
6088 * migrate cur->content to attrs
6089 */
6090 tmp = cur->content;
6091 cur->content = tmp->next;
6092 tmp->next = cur->attrs;
6093 cur->attrs = tmp;
6094 } else {
6095 /*
6096 * cur->content can generate elements or text
6097 */
6098 break;
6099 }
6100 }
6101 pre = cur->content;
6102 while ((pre != NULL) && (pre->next != NULL)) {
6103 tmp = pre->next;
6104 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6105 if (attronly == 1) {
6106 /*
6107 * migrate tmp to attrs
6108 */
6109 pre->next = tmp->next;
6110 tmp->next = cur->attrs;
6111 cur->attrs = tmp;
6112 } else {
6113 pre = tmp;
6114 }
6115 }
6116 }
6117 /*
6118 * This may result in a simplification
6119 */
6120 if ((cur->type == XML_RELAXNG_GROUP) ||
6121 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6122 if (cur->content == NULL)
6123 cur->type = XML_RELAXNG_EMPTY;
6124 else if (cur->content->next == NULL) {
6125 if ((parent == NULL) && (prev == NULL)) {
6126 cur->type = XML_RELAXNG_NOOP;
6127 } else if (prev == NULL) {
6128 parent->content = cur->content;
6129 cur->content->next = cur->next;
6130 cur = cur->content;
6131 } else {
6132 cur->content->next = cur->next;
6133 prev->next = cur->content;
6134 cur = cur->content;
6135 }
6136 }
6137 }
6138 /*
6139 * the current node may have been transformed back
6140 */
6141 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6142 (cur->content != NULL) &&
6143 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6144 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6145 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6146 if ((parent != NULL) &&
6147 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6148 (parent->type == XML_RELAXNG_LIST) ||
6149 (parent->type == XML_RELAXNG_GROUP) ||
6150 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6151 (parent->type == XML_RELAXNG_ONEORMORE) ||
6152 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6153 parent->type = XML_RELAXNG_NOT_ALLOWED;
6154 break;
6155 }
6156 if ((parent != NULL) &&
6157 (parent->type == XML_RELAXNG_CHOICE)) {
6158 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6159 } else
6160 prev = cur;
6161 } else if (cur->type == XML_RELAXNG_EMPTY) {
6162 if ((parent != NULL) &&
6163 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6164 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6165 parent->type = XML_RELAXNG_EMPTY;
6166 break;
6167 }
6168 if ((parent != NULL) &&
6169 ((parent->type == XML_RELAXNG_GROUP) ||
6170 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6171 (parent->type == XML_RELAXNG_CHOICE))) {
6172 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6173 } else
6174 prev = cur;
6175 } else {
6176 prev = cur;
6177 }
6178 }
6179 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006180 }
6181}
6182
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006183/**
6184 * xmlRelaxNGGroupContentType:
6185 * @ct1: the first content type
6186 * @ct2: the second content type
6187 *
6188 * Try to group 2 content types
6189 *
6190 * Returns the content type
6191 */
6192static xmlRelaxNGContentType
6193xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006194 xmlRelaxNGContentType ct2)
6195{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006196 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006197 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6198 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006199 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006200 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006201 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006202 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006203 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006204 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6205 return (XML_RELAXNG_CONTENT_COMPLEX);
6206 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006207}
6208
6209/**
6210 * xmlRelaxNGMaxContentType:
6211 * @ct1: the first content type
6212 * @ct2: the second content type
6213 *
6214 * Compute the max content-type
6215 *
6216 * Returns the content type
6217 */
6218static xmlRelaxNGContentType
6219xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006220 xmlRelaxNGContentType ct2)
6221{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006222 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006223 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6224 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006225 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006226 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6227 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006228 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006229 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6230 return (XML_RELAXNG_CONTENT_COMPLEX);
6231 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006232}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006233
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006234/**
6235 * xmlRelaxNGCheckRules:
6236 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006237 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006238 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006239 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006240 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006241 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006242 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006243 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006244 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006245static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006246xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6247 xmlRelaxNGDefinePtr cur, int flags,
6248 xmlRelaxNGType ptype)
6249{
Daniel Veillardd44b9362009-09-07 12:15:08 +02006250 int nflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006251 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006252
Daniel Veillardfd573f12003-03-16 17:52:32 +00006253 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006254 ret = XML_RELAXNG_CONTENT_EMPTY;
6255 if ((cur->type == XML_RELAXNG_REF) ||
6256 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006257 /*
6258 * This should actually be caught by list//element(ref) at the
6259 * element boundaries, c.f. Bug #159968 local refs are dropped
6260 * in step 4.19.
6261 */
6262#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006263 if (flags & XML_RELAXNG_IN_LIST) {
6264 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6265 "Found forbidden pattern list//ref\n", NULL,
6266 NULL);
6267 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006268#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006269 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6270 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6271 "Found forbidden pattern data/except//ref\n",
6272 NULL, NULL);
6273 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006274 if (cur->content == NULL) {
6275 if (cur->type == XML_RELAXNG_PARENTREF)
6276 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6277 "Internal found no define for parent refs\n",
6278 NULL, NULL);
6279 else
6280 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6281 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006282 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006283 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006284 if (cur->depth > -4) {
6285 cur->depth = -4;
6286 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6287 flags, cur->type);
6288 cur->depth = ret - 15;
6289 } else if (cur->depth == -4) {
6290 ret = XML_RELAXNG_CONTENT_COMPLEX;
6291 } else {
6292 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6293 }
6294 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6295 /*
6296 * The 7.3 Attribute derivation rule for groups is plugged there
6297 */
6298 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6299 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6300 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6301 "Found forbidden pattern data/except//element(ref)\n",
6302 NULL, NULL);
6303 }
6304 if (flags & XML_RELAXNG_IN_LIST) {
6305 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6306 "Found forbidden pattern list//element(ref)\n",
6307 NULL, NULL);
6308 }
6309 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6310 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6311 "Found forbidden pattern attribute//element(ref)\n",
6312 NULL, NULL);
6313 }
6314 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6315 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6316 "Found forbidden pattern attribute//element(ref)\n",
6317 NULL, NULL);
6318 }
6319 /*
6320 * reset since in the simple form elements are only child
6321 * of grammar/define
6322 */
6323 nflags = 0;
6324 ret =
6325 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6326 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6327 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6328 "Element %s attributes have a content type error\n",
6329 cur->name, NULL);
6330 }
6331 ret =
6332 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6333 cur->type);
6334 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6335 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6336 "Element %s has a content type error\n",
6337 cur->name, NULL);
6338 } else {
6339 ret = XML_RELAXNG_CONTENT_COMPLEX;
6340 }
6341 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6342 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6343 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6344 "Found forbidden pattern attribute//attribute\n",
6345 NULL, NULL);
6346 }
6347 if (flags & XML_RELAXNG_IN_LIST) {
6348 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6349 "Found forbidden pattern list//attribute\n",
6350 NULL, NULL);
6351 }
6352 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6353 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6354 "Found forbidden pattern oneOrMore//group//attribute\n",
6355 NULL, NULL);
6356 }
6357 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6358 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6359 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6360 NULL, NULL);
6361 }
6362 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6363 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6364 "Found forbidden pattern data/except//attribute\n",
6365 NULL, NULL);
6366 }
6367 if (flags & XML_RELAXNG_IN_START) {
6368 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6369 "Found forbidden pattern start//attribute\n",
6370 NULL, NULL);
6371 }
6372 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6373 && (cur->name == NULL)) {
6374 if (cur->ns == NULL) {
6375 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6376 "Found anyName attribute without oneOrMore ancestor\n",
6377 NULL, NULL);
6378 } else {
6379 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6380 "Found nsName attribute without oneOrMore ancestor\n",
6381 NULL, NULL);
6382 }
6383 }
6384 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6385 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6386 ret = XML_RELAXNG_CONTENT_EMPTY;
6387 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6388 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6389 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6390 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6391 "Found forbidden pattern data/except//oneOrMore\n",
6392 NULL, NULL);
6393 }
6394 if (flags & XML_RELAXNG_IN_START) {
6395 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6396 "Found forbidden pattern start//oneOrMore\n",
6397 NULL, NULL);
6398 }
6399 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6400 ret =
6401 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6402 cur->type);
6403 ret = xmlRelaxNGGroupContentType(ret, ret);
6404 } else if (cur->type == XML_RELAXNG_LIST) {
6405 if (flags & XML_RELAXNG_IN_LIST) {
6406 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6407 "Found forbidden pattern list//list\n", NULL,
6408 NULL);
6409 }
6410 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6411 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6412 "Found forbidden pattern data/except//list\n",
6413 NULL, NULL);
6414 }
6415 if (flags & XML_RELAXNG_IN_START) {
6416 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6417 "Found forbidden pattern start//list\n", NULL,
6418 NULL);
6419 }
6420 nflags = flags | XML_RELAXNG_IN_LIST;
6421 ret =
6422 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6423 cur->type);
6424 } else if (cur->type == XML_RELAXNG_GROUP) {
6425 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6426 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6427 "Found forbidden pattern data/except//group\n",
6428 NULL, NULL);
6429 }
6430 if (flags & XML_RELAXNG_IN_START) {
6431 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6432 "Found forbidden pattern start//group\n", NULL,
6433 NULL);
6434 }
6435 if (flags & XML_RELAXNG_IN_ONEORMORE)
6436 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6437 else
6438 nflags = flags;
6439 ret =
6440 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6441 cur->type);
6442 /*
6443 * The 7.3 Attribute derivation rule for groups is plugged there
6444 */
6445 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6446 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6447 if (flags & XML_RELAXNG_IN_LIST) {
6448 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6449 "Found forbidden pattern list//interleave\n",
6450 NULL, NULL);
6451 }
6452 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6453 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6454 "Found forbidden pattern data/except//interleave\n",
6455 NULL, NULL);
6456 }
6457 if (flags & XML_RELAXNG_IN_START) {
6458 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6459 "Found forbidden pattern start//interleave\n",
6460 NULL, NULL);
6461 }
6462 if (flags & XML_RELAXNG_IN_ONEORMORE)
6463 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6464 else
6465 nflags = flags;
6466 ret =
6467 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6468 cur->type);
6469 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6470 if ((cur->parent != NULL) &&
6471 (cur->parent->type == XML_RELAXNG_DATATYPE))
6472 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6473 else
6474 nflags = flags;
6475 ret =
6476 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6477 cur->type);
6478 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6479 if (flags & XML_RELAXNG_IN_START) {
6480 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6481 "Found forbidden pattern start//data\n", NULL,
6482 NULL);
6483 }
6484 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6485 ret = XML_RELAXNG_CONTENT_SIMPLE;
6486 } else if (cur->type == XML_RELAXNG_VALUE) {
6487 if (flags & XML_RELAXNG_IN_START) {
6488 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6489 "Found forbidden pattern start//value\n", NULL,
6490 NULL);
6491 }
6492 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6493 ret = XML_RELAXNG_CONTENT_SIMPLE;
6494 } else if (cur->type == XML_RELAXNG_TEXT) {
6495 if (flags & XML_RELAXNG_IN_LIST) {
6496 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6497 "Found forbidden pattern list//text\n", NULL,
6498 NULL);
6499 }
6500 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6501 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6502 "Found forbidden pattern data/except//text\n",
6503 NULL, NULL);
6504 }
6505 if (flags & XML_RELAXNG_IN_START) {
6506 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6507 "Found forbidden pattern start//text\n", NULL,
6508 NULL);
6509 }
6510 ret = XML_RELAXNG_CONTENT_COMPLEX;
6511 } else if (cur->type == XML_RELAXNG_EMPTY) {
6512 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6513 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6514 "Found forbidden pattern data/except//empty\n",
6515 NULL, NULL);
6516 }
6517 if (flags & XML_RELAXNG_IN_START) {
6518 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6519 "Found forbidden pattern start//empty\n", NULL,
6520 NULL);
6521 }
6522 ret = XML_RELAXNG_CONTENT_EMPTY;
6523 } else if (cur->type == XML_RELAXNG_CHOICE) {
6524 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6525 ret =
6526 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6527 } else {
6528 ret =
6529 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6530 }
6531 cur = cur->next;
6532 if (ptype == XML_RELAXNG_GROUP) {
6533 val = xmlRelaxNGGroupContentType(val, ret);
6534 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02006535 /*
6536 * TODO: scan complain that tmp is never used, seems on purpose
6537 * need double-checking
6538 */
Daniel Veillard4c004142003-10-07 11:33:24 +00006539 tmp = xmlRelaxNGGroupContentType(val, ret);
6540 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6541 tmp = xmlRelaxNGMaxContentType(val, ret);
6542 } else if (ptype == XML_RELAXNG_CHOICE) {
6543 val = xmlRelaxNGMaxContentType(val, ret);
6544 } else if (ptype == XML_RELAXNG_LIST) {
6545 val = XML_RELAXNG_CONTENT_SIMPLE;
6546 } else if (ptype == XML_RELAXNG_EXCEPT) {
6547 if (ret == XML_RELAXNG_CONTENT_ERROR)
6548 val = XML_RELAXNG_CONTENT_ERROR;
6549 else
6550 val = XML_RELAXNG_CONTENT_SIMPLE;
6551 } else {
6552 val = xmlRelaxNGGroupContentType(val, ret);
6553 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006554
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006555 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006556 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006557}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006558
6559/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006560 * xmlRelaxNGParseGrammar:
6561 * @ctxt: a Relax-NG parser context
6562 * @nodes: grammar children nodes
6563 *
6564 * parse a Relax-NG <grammar> node
6565 *
6566 * Returns the internal xmlRelaxNGGrammarPtr built or
6567 * NULL in case of error
6568 */
6569static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006570xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6571{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006572 xmlRelaxNGGrammarPtr ret, tmp, old;
6573
Daniel Veillardc482e262003-02-26 14:48:48 +00006574#ifdef DEBUG_GRAMMAR
6575 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6576#endif
6577
Daniel Veillard6eadf632003-01-23 18:29:16 +00006578 ret = xmlRelaxNGNewGrammar(ctxt);
6579 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006580 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006581
6582 /*
6583 * Link the new grammar in the tree
6584 */
6585 ret->parent = ctxt->grammar;
6586 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006587 tmp = ctxt->grammar->children;
6588 if (tmp == NULL) {
6589 ctxt->grammar->children = ret;
6590 } else {
6591 while (tmp->next != NULL)
6592 tmp = tmp->next;
6593 tmp->next = ret;
6594 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006595 }
6596
6597 old = ctxt->grammar;
6598 ctxt->grammar = ret;
6599 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6600 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006601 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006602 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6603 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006604 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006605 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6606 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006607 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006608
6609 /*
Jan Pokornýacace882014-06-09 23:45:24 +02006610 * Apply 4.17 merging rules to defines and starts
Daniel Veillard6eadf632003-01-23 18:29:16 +00006611 */
6612 xmlRelaxNGCombineStart(ctxt, ret);
6613 if (ret->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006614 xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6615 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006616 }
6617
6618 /*
6619 * link together defines and refs in this grammar
6620 */
6621 if (ret->refs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006622 xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6623 ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006624 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006625
Daniel Veillard81c51e12009-08-14 18:52:10 +02006626
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006627 /* @@@@ */
6628
Daniel Veillard6eadf632003-01-23 18:29:16 +00006629 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006630 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006631}
6632
6633/**
6634 * xmlRelaxNGParseDocument:
6635 * @ctxt: a Relax-NG parser context
6636 * @node: the root node of the RelaxNG schema
6637 *
6638 * parse a Relax-NG definition resource and build an internal
6639 * xmlRelaxNG struture which can be used to validate instances.
6640 *
6641 * Returns the internal XML RelaxNG structure built or
6642 * NULL in case of error
6643 */
6644static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006645xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6646{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006647 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006648 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006649 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006650
6651 if ((ctxt == NULL) || (node == NULL))
6652 return (NULL);
6653
6654 schema = xmlRelaxNGNewRelaxNG(ctxt);
6655 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006656 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006657
Daniel Veillard276be4a2003-01-24 01:03:34 +00006658 olddefine = ctxt->define;
6659 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006660 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006661 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard42870f42014-07-26 21:04:54 +08006662 if (schema->topgrammar == NULL) {
6663 xmlRelaxNGFree(schema);
6664 return (NULL);
6665 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006666 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006667 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006668
Daniel Veillard4c004142003-10-07 11:33:24 +00006669 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6670 if (schema->topgrammar == NULL) {
Daniel Veillard42870f42014-07-26 21:04:54 +08006671 xmlRelaxNGFree(schema);
6672 return (NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00006673 }
6674 /*
6675 * Link the new grammar in the tree
6676 */
6677 ret->parent = ctxt->grammar;
6678 if (ctxt->grammar != NULL) {
6679 tmp = ctxt->grammar->children;
6680 if (tmp == NULL) {
6681 ctxt->grammar->children = ret;
6682 } else {
6683 while (tmp->next != NULL)
6684 tmp = tmp->next;
6685 tmp->next = ret;
6686 }
6687 }
6688 old = ctxt->grammar;
6689 ctxt->grammar = ret;
6690 xmlRelaxNGParseStart(ctxt, node);
6691 if (old != NULL)
6692 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006693 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006694 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006695 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006696 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6697 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6698 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6699 while ((schema->topgrammar->start != NULL) &&
6700 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6701 (schema->topgrammar->start->next != NULL))
6702 schema->topgrammar->start =
6703 schema->topgrammar->start->content;
6704 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6705 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6706 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006707 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006708#ifdef DEBUG
6709 if (schema == NULL)
6710 xmlGenericError(xmlGenericErrorContext,
6711 "xmlRelaxNGParseDocument() failed\n");
6712#endif
6713
6714 return (schema);
6715}
6716
6717/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006718 * *
6719 * Reading RelaxNGs *
6720 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00006721 ************************************************************************/
6722
6723/**
6724 * xmlRelaxNGNewParserCtxt:
6725 * @URL: the location of the schema
6726 *
6727 * Create an XML RelaxNGs parse context for that file/resource expected
6728 * to contain an XML RelaxNGs file.
6729 *
6730 * Returns the parser context or NULL in case of error
6731 */
6732xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006733xmlRelaxNGNewParserCtxt(const char *URL)
6734{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006735 xmlRelaxNGParserCtxtPtr ret;
6736
6737 if (URL == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006738 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006739
Daniel Veillard4c004142003-10-07 11:33:24 +00006740 ret =
6741 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006742 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006743 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006744 return (NULL);
6745 }
6746 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard4c004142003-10-07 11:33:24 +00006747 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006748 ret->error = xmlGenericError;
6749 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006750 return (ret);
6751}
6752
6753/**
6754 * xmlRelaxNGNewMemParserCtxt:
6755 * @buffer: a pointer to a char array containing the schemas
6756 * @size: the size of the array
6757 *
6758 * Create an XML RelaxNGs parse context for that memory buffer expected
6759 * to contain an XML RelaxNGs file.
6760 *
6761 * Returns the parser context or NULL in case of error
6762 */
6763xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006764xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6765{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006766 xmlRelaxNGParserCtxtPtr ret;
6767
6768 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006769 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006770
Daniel Veillard4c004142003-10-07 11:33:24 +00006771 ret =
6772 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006773 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006774 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006775 return (NULL);
6776 }
6777 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6778 ret->buffer = buffer;
6779 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006780 ret->error = xmlGenericError;
6781 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006782 return (ret);
6783}
6784
6785/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006786 * xmlRelaxNGNewDocParserCtxt:
6787 * @doc: a preparsed document tree
6788 *
6789 * Create an XML RelaxNGs parser context for that document.
6790 * Note: since the process of compiling a RelaxNG schemas modifies the
6791 * document, the @doc parameter is duplicated internally.
6792 *
6793 * Returns the parser context or NULL in case of error
6794 */
6795xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006796xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6797{
Daniel Veillard33300b42003-04-17 09:09:19 +00006798 xmlRelaxNGParserCtxtPtr ret;
6799 xmlDocPtr copy;
6800
6801 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006802 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006803 copy = xmlCopyDoc(doc, 1);
6804 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006805 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006806
Daniel Veillard4c004142003-10-07 11:33:24 +00006807 ret =
6808 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006809 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006810 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard33300b42003-04-17 09:09:19 +00006811 return (NULL);
6812 }
6813 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6814 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006815 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006816 ret->userData = xmlGenericErrorContext;
6817 return (ret);
6818}
6819
6820/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006821 * xmlRelaxNGFreeParserCtxt:
6822 * @ctxt: the schema parser context
6823 *
6824 * Free the resources associated to the schema parser context
6825 */
6826void
Daniel Veillard4c004142003-10-07 11:33:24 +00006827xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6828{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006829 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006830 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006831 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006832 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006833 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006834 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006835 if (ctxt->interleaves != NULL)
6836 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006837 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006838 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006839 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006840 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006841 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006842 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006843 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006844 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006845 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006846 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006847
Daniel Veillard4c004142003-10-07 11:33:24 +00006848 for (i = 0; i < ctxt->defNr; i++)
6849 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6850 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006851 }
Daniel Veillard42595322004-11-08 10:52:06 +00006852 if ((ctxt->document != NULL) && (ctxt->freedoc))
6853 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006854 xmlFree(ctxt);
6855}
6856
Daniel Veillard6eadf632003-01-23 18:29:16 +00006857/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006858 * xmlRelaxNGNormExtSpace:
6859 * @value: a value
6860 *
6861 * Removes the leading and ending spaces of the value
6862 * The string is modified "in situ"
6863 */
6864static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006865xmlRelaxNGNormExtSpace(xmlChar * value)
6866{
Daniel Veillardd2298792003-02-14 16:54:11 +00006867 xmlChar *start = value;
6868 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006869
Daniel Veillard4c004142003-10-07 11:33:24 +00006870 if (value == NULL)
6871 return;
6872
William M. Brack76e95df2003-10-18 16:20:14 +00006873 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006874 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006875 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006876 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006877 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006878 cur++;
6879 if (*cur == 0)
6880 return;
6881 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006882 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006883 cur++;
6884 if (*cur == 0) {
6885 *start = 0;
6886 return;
6887 }
6888 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006889 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006890 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006891 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006892 *start++ = *cur++;
6893 if (*cur == 0) {
6894 *start = 0;
6895 return;
6896 }
6897 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006898 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006899 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006900 if (*cur == 0) {
6901 *start = 0;
6902 return;
6903 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006904 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006905 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006906 }
6907}
6908
6909/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006910 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006911 * @ctxt: a Relax-NG parser context
6912 * @node: a Relax-NG node
6913 *
6914 * Check all the attributes on the given node
6915 */
6916static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006917xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6918{
Daniel Veillardd2298792003-02-14 16:54:11 +00006919 xmlAttrPtr cur, next;
6920
6921 cur = node->properties;
6922 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006923 next = cur->next;
6924 if ((cur->ns == NULL) ||
6925 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6926 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6927 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6928 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6929 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6930 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6931 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6932 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6933 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6934 "Attribute %s is not allowed on %s\n",
6935 cur->name, node->name);
6936 }
6937 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6938 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6939 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6940 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6941 "Attribute %s is not allowed on %s\n",
6942 cur->name, node->name);
6943 }
6944 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6945 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6946 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6947 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6948 "Attribute %s is not allowed on %s\n",
6949 cur->name, node->name);
6950 }
6951 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6952 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6953 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6954 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6955 "Attribute %s is not allowed on %s\n",
6956 cur->name, node->name);
6957 }
6958 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6959 xmlChar *val;
6960 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006961
Daniel Veillard4c004142003-10-07 11:33:24 +00006962 val = xmlNodeListGetString(node->doc, cur->children, 1);
6963 if (val != NULL) {
6964 if (val[0] != 0) {
6965 uri = xmlParseURI((const char *) val);
6966 if (uri == NULL) {
6967 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6968 "Attribute %s contains invalid URI %s\n",
6969 cur->name, val);
6970 } else {
6971 if (uri->scheme == NULL) {
6972 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6973 "Attribute %s URI %s is not absolute\n",
6974 cur->name, val);
6975 }
6976 if (uri->fragment != NULL) {
6977 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6978 "Attribute %s URI %s has a fragment ID\n",
6979 cur->name, val);
6980 }
6981 xmlFreeURI(uri);
6982 }
6983 }
6984 xmlFree(val);
6985 }
6986 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6987 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6988 "Unknown attribute %s on %s\n", cur->name,
6989 node->name);
6990 }
6991 }
6992 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00006993 }
6994}
6995
6996/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00006997 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006998 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00006999 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00007000 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00007001 * Cleanup the subtree from unwanted nodes for parsing, resolve
7002 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00007003 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00007004static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007005xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
7006{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007007 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007008
Daniel Veillard6eadf632003-01-23 18:29:16 +00007009 delete = NULL;
7010 cur = root;
7011 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007012 if (delete != NULL) {
7013 xmlUnlinkNode(delete);
7014 xmlFreeNode(delete);
7015 delete = NULL;
7016 }
7017 if (cur->type == XML_ELEMENT_NODE) {
7018 /*
7019 * Simplification 4.1. Annotations
7020 */
7021 if ((cur->ns == NULL) ||
7022 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
7023 if ((cur->parent != NULL) &&
7024 (cur->parent->type == XML_ELEMENT_NODE) &&
7025 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7026 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7027 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7028 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7029 "element %s doesn't allow foreign elements\n",
7030 cur->parent->name, NULL);
7031 }
7032 delete = cur;
7033 goto skip_children;
7034 } else {
7035 xmlRelaxNGCleanupAttributes(ctxt, cur);
7036 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7037 xmlChar *href, *ns, *base, *URL;
7038 xmlRelaxNGDocumentPtr docu;
7039 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00007040 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007041
Daniel Veillard4c004142003-10-07 11:33:24 +00007042 ns = xmlGetProp(cur, BAD_CAST "ns");
7043 if (ns == NULL) {
7044 tmp = cur->parent;
7045 while ((tmp != NULL) &&
7046 (tmp->type == XML_ELEMENT_NODE)) {
7047 ns = xmlGetProp(tmp, BAD_CAST "ns");
7048 if (ns != NULL)
7049 break;
7050 tmp = tmp->parent;
7051 }
7052 }
7053 href = xmlGetProp(cur, BAD_CAST "href");
7054 if (href == NULL) {
7055 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7056 "xmlRelaxNGParse: externalRef has no href attribute\n",
7057 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007058 if (ns != NULL)
7059 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007060 delete = cur;
7061 goto skip_children;
7062 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007063 uri = xmlParseURI((const char *) href);
7064 if (uri == NULL) {
7065 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7066 "Incorrect URI for externalRef %s\n",
7067 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007068 if (ns != NULL)
7069 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007070 if (href != NULL)
7071 xmlFree(href);
7072 delete = cur;
7073 goto skip_children;
7074 }
7075 if (uri->fragment != NULL) {
7076 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7077 "Fragment forbidden in URI for externalRef %s\n",
7078 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007079 if (ns != NULL)
7080 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007081 xmlFreeURI(uri);
7082 if (href != NULL)
7083 xmlFree(href);
7084 delete = cur;
7085 goto skip_children;
7086 }
7087 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007088 base = xmlNodeGetBase(cur->doc, cur);
7089 URL = xmlBuildURI(href, base);
7090 if (URL == NULL) {
7091 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7092 "Failed to compute URL for externalRef %s\n",
7093 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007094 if (ns != NULL)
7095 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007096 if (href != NULL)
7097 xmlFree(href);
7098 if (base != NULL)
7099 xmlFree(base);
7100 delete = cur;
7101 goto skip_children;
7102 }
7103 if (href != NULL)
7104 xmlFree(href);
7105 if (base != NULL)
7106 xmlFree(base);
7107 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7108 if (docu == NULL) {
7109 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7110 "Failed to load externalRef %s\n", URL,
7111 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007112 if (ns != NULL)
7113 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007114 xmlFree(URL);
7115 delete = cur;
7116 goto skip_children;
7117 }
7118 if (ns != NULL)
7119 xmlFree(ns);
7120 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007121 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007122 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7123 xmlChar *href, *ns, *base, *URL;
7124 xmlRelaxNGIncludePtr incl;
7125 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007126
Daniel Veillard4c004142003-10-07 11:33:24 +00007127 href = xmlGetProp(cur, BAD_CAST "href");
7128 if (href == NULL) {
7129 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7130 "xmlRelaxNGParse: include has no href attribute\n",
7131 NULL, NULL);
7132 delete = cur;
7133 goto skip_children;
7134 }
7135 base = xmlNodeGetBase(cur->doc, cur);
7136 URL = xmlBuildURI(href, base);
7137 if (URL == NULL) {
7138 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7139 "Failed to compute URL for include %s\n",
7140 href, NULL);
7141 if (href != NULL)
7142 xmlFree(href);
7143 if (base != NULL)
7144 xmlFree(base);
7145 delete = cur;
7146 goto skip_children;
7147 }
7148 if (href != NULL)
7149 xmlFree(href);
7150 if (base != NULL)
7151 xmlFree(base);
7152 ns = xmlGetProp(cur, BAD_CAST "ns");
7153 if (ns == NULL) {
7154 tmp = cur->parent;
7155 while ((tmp != NULL) &&
7156 (tmp->type == XML_ELEMENT_NODE)) {
7157 ns = xmlGetProp(tmp, BAD_CAST "ns");
7158 if (ns != NULL)
7159 break;
7160 tmp = tmp->parent;
7161 }
7162 }
7163 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7164 if (ns != NULL)
7165 xmlFree(ns);
7166 if (incl == NULL) {
7167 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7168 "Failed to load include %s\n", URL,
7169 NULL);
7170 xmlFree(URL);
7171 delete = cur;
7172 goto skip_children;
7173 }
7174 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007175 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007176 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7177 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7178 {
7179 xmlChar *name, *ns;
7180 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007181
Daniel Veillard4c004142003-10-07 11:33:24 +00007182 /*
7183 * Simplification 4.8. name attribute of element
7184 * and attribute elements
7185 */
7186 name = xmlGetProp(cur, BAD_CAST "name");
7187 if (name != NULL) {
7188 if (cur->children == NULL) {
7189 text =
7190 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7191 name);
7192 } else {
7193 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007194
Daniel Veillard03a53c32004-10-26 16:06:51 +00007195 node = xmlNewDocNode(cur->doc, cur->ns,
7196 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007197 if (node != NULL) {
7198 xmlAddPrevSibling(cur->children, node);
7199 text = xmlNewText(name);
7200 xmlAddChild(node, text);
7201 text = node;
7202 }
7203 }
7204 if (text == NULL) {
7205 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7206 "Failed to create a name %s element\n",
7207 name, NULL);
7208 }
7209 xmlUnsetProp(cur, BAD_CAST "name");
7210 xmlFree(name);
7211 ns = xmlGetProp(cur, BAD_CAST "ns");
7212 if (ns != NULL) {
7213 if (text != NULL) {
7214 xmlSetProp(text, BAD_CAST "ns", ns);
7215 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7216 }
7217 xmlFree(ns);
7218 } else if (xmlStrEqual(cur->name,
7219 BAD_CAST "attribute")) {
7220 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7221 }
7222 }
7223 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7224 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7225 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7226 /*
7227 * Simplification 4.8. name attribute of element
7228 * and attribute elements
7229 */
7230 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7231 xmlNodePtr node;
7232 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007233
Daniel Veillard4c004142003-10-07 11:33:24 +00007234 node = cur->parent;
7235 while ((node != NULL) &&
7236 (node->type == XML_ELEMENT_NODE)) {
7237 ns = xmlGetProp(node, BAD_CAST "ns");
7238 if (ns != NULL) {
7239 break;
7240 }
7241 node = node->parent;
7242 }
7243 if (ns == NULL) {
7244 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7245 } else {
7246 xmlSetProp(cur, BAD_CAST "ns", ns);
7247 xmlFree(ns);
7248 }
7249 }
7250 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7251 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007252
Daniel Veillard4c004142003-10-07 11:33:24 +00007253 /*
7254 * Simplification: 4.10. QNames
7255 */
7256 name = xmlNodeGetContent(cur);
7257 if (name != NULL) {
7258 local = xmlSplitQName2(name, &prefix);
7259 if (local != NULL) {
7260 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007261
Daniel Veillard4c004142003-10-07 11:33:24 +00007262 ns = xmlSearchNs(cur->doc, cur, prefix);
7263 if (ns == NULL) {
7264 xmlRngPErr(ctxt, cur,
7265 XML_RNGP_PREFIX_UNDEFINED,
7266 "xmlRelaxNGParse: no namespace for prefix %s\n",
7267 prefix, NULL);
7268 } else {
7269 xmlSetProp(cur, BAD_CAST "ns",
7270 ns->href);
7271 xmlNodeSetContent(cur, local);
7272 }
7273 xmlFree(local);
7274 xmlFree(prefix);
7275 }
7276 xmlFree(name);
7277 }
7278 }
7279 /*
7280 * 4.16
7281 */
7282 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7283 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7284 xmlRngPErr(ctxt, cur,
7285 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7286 "Found nsName/except//nsName forbidden construct\n",
7287 NULL, NULL);
7288 }
7289 }
7290 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7291 (cur != root)) {
7292 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007293
Daniel Veillard4c004142003-10-07 11:33:24 +00007294 /*
7295 * 4.16
7296 */
7297 if ((cur->parent != NULL) &&
7298 (xmlStrEqual
7299 (cur->parent->name, BAD_CAST "anyName"))) {
7300 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7301 xmlRelaxNGCleanupTree(ctxt, cur);
7302 ctxt->flags = oldflags;
7303 goto skip_children;
7304 } else if ((cur->parent != NULL) &&
7305 (xmlStrEqual
7306 (cur->parent->name, BAD_CAST "nsName"))) {
7307 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7308 xmlRelaxNGCleanupTree(ctxt, cur);
7309 ctxt->flags = oldflags;
7310 goto skip_children;
7311 }
7312 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7313 /*
7314 * 4.16
7315 */
7316 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7317 xmlRngPErr(ctxt, cur,
7318 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7319 "Found anyName/except//anyName forbidden construct\n",
7320 NULL, NULL);
7321 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7322 xmlRngPErr(ctxt, cur,
7323 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7324 "Found nsName/except//anyName forbidden construct\n",
7325 NULL, NULL);
7326 }
7327 }
7328 /*
Jan Pokornýacace882014-06-09 23:45:24 +02007329 * This is not an else since "include" is transformed
Daniel Veillard4c004142003-10-07 11:33:24 +00007330 * into a div
7331 */
7332 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7333 xmlChar *ns;
7334 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007335
Daniel Veillard4c004142003-10-07 11:33:24 +00007336 /*
7337 * implements rule 4.11
7338 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007339
Daniel Veillard4c004142003-10-07 11:33:24 +00007340 ns = xmlGetProp(cur, BAD_CAST "ns");
7341
7342 child = cur->children;
7343 ins = cur;
7344 while (child != NULL) {
7345 if (ns != NULL) {
7346 if (!xmlHasProp(child, BAD_CAST "ns")) {
7347 xmlSetProp(child, BAD_CAST "ns", ns);
7348 }
7349 }
7350 tmp = child->next;
7351 xmlUnlinkNode(child);
7352 ins = xmlAddNextSibling(ins, child);
7353 child = tmp;
7354 }
7355 if (ns != NULL)
7356 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007357 /*
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007358 * Since we are about to delete cur, if its nsDef is non-NULL we
William M. Brack8eabb052004-06-07 14:15:54 +00007359 * need to preserve it (it contains the ns definitions for the
7360 * children we just moved). We'll just stick it on to the end
7361 * of cur->parent's list, since it's never going to be re-serialized
7362 * (bug 143738).
7363 */
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007364 if ((cur->nsDef != NULL) && (cur->parent != NULL)) {
William M. Brack8eabb052004-06-07 14:15:54 +00007365 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7366 while (parDef->next != NULL)
7367 parDef = parDef->next;
7368 parDef->next = cur->nsDef;
7369 cur->nsDef = NULL;
7370 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007371 delete = cur;
7372 goto skip_children;
7373 }
7374 }
7375 }
7376 /*
7377 * Simplification 4.2 whitespaces
7378 */
7379 else if ((cur->type == XML_TEXT_NODE) ||
7380 (cur->type == XML_CDATA_SECTION_NODE)) {
7381 if (IS_BLANK_NODE(cur)) {
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007382 if ((cur->parent != NULL) &&
7383 (cur->parent->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007384 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7385 &&
7386 (!xmlStrEqual
7387 (cur->parent->name, BAD_CAST "param")))
7388 delete = cur;
7389 } else {
7390 delete = cur;
7391 goto skip_children;
7392 }
7393 }
7394 } else {
7395 delete = cur;
7396 goto skip_children;
7397 }
7398
7399 /*
7400 * Skip to next node
7401 */
7402 if (cur->children != NULL) {
7403 if ((cur->children->type != XML_ENTITY_DECL) &&
7404 (cur->children->type != XML_ENTITY_REF_NODE) &&
7405 (cur->children->type != XML_ENTITY_NODE)) {
7406 cur = cur->children;
7407 continue;
7408 }
7409 }
7410 skip_children:
7411 if (cur->next != NULL) {
7412 cur = cur->next;
7413 continue;
7414 }
7415
7416 do {
7417 cur = cur->parent;
7418 if (cur == NULL)
7419 break;
7420 if (cur == root) {
7421 cur = NULL;
7422 break;
7423 }
7424 if (cur->next != NULL) {
7425 cur = cur->next;
7426 break;
7427 }
7428 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007429 }
7430 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007431 xmlUnlinkNode(delete);
7432 xmlFreeNode(delete);
7433 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007434 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007435}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007436
Daniel Veillardc5312d72003-02-21 17:14:10 +00007437/**
7438 * xmlRelaxNGCleanupDoc:
7439 * @ctxt: a Relax-NG parser context
7440 * @doc: an xmldocPtr document pointer
7441 *
7442 * Cleanup the document from unwanted nodes for parsing, resolve
7443 * Include and externalRef lookups.
7444 *
7445 * Returns the cleaned up document or NULL in case of error
7446 */
7447static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007448xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7449{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007450 xmlNodePtr root;
7451
7452 /*
7453 * Extract the root
7454 */
7455 root = xmlDocGetRootElement(doc);
7456 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007457 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7458 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007459 return (NULL);
7460 }
7461 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007462 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007463}
7464
7465/**
7466 * xmlRelaxNGParse:
7467 * @ctxt: a Relax-NG parser context
7468 *
7469 * parse a schema definition resource and build an internal
7470 * XML Shema struture which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007471 *
7472 * Returns the internal XML RelaxNG structure built from the resource or
7473 * NULL in case of error
7474 */
7475xmlRelaxNGPtr
7476xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7477{
7478 xmlRelaxNGPtr ret = NULL;
7479 xmlDocPtr doc;
7480 xmlNodePtr root;
7481
7482 xmlRelaxNGInitTypes();
7483
7484 if (ctxt == NULL)
7485 return (NULL);
7486
7487 /*
7488 * First step is to parse the input document into an DOM/Infoset
7489 */
7490 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007491 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007492 if (doc == NULL) {
7493 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7494 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7495 NULL);
7496 return (NULL);
7497 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007498 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007499 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007500 if (doc == NULL) {
7501 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7502 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7503 NULL);
7504 return (NULL);
7505 }
7506 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7507 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007508 } else if (ctxt->document != NULL) {
7509 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007510 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007511 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7512 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7513 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007514 }
7515 ctxt->document = doc;
7516
7517 /*
7518 * Some preprocessing of the document content
7519 */
7520 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7521 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007522 xmlFreeDoc(ctxt->document);
7523 ctxt->document = NULL;
7524 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007525 }
7526
Daniel Veillard6eadf632003-01-23 18:29:16 +00007527 /*
7528 * Then do the parsing for good
7529 */
7530 root = xmlDocGetRootElement(doc);
7531 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007532 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7533 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007534 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007535
Daniel Veillard3f845a92006-04-13 07:33:44 +00007536 xmlFreeDoc(ctxt->document);
7537 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007538 return (NULL);
7539 }
7540 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007541 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007542 xmlFreeDoc(ctxt->document);
7543 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007544 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007545 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007546
7547 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007548 * Check the ref/defines links
7549 */
7550 /*
7551 * try to preprocess interleaves
7552 */
7553 if (ctxt->interleaves != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007554 xmlHashScan(ctxt->interleaves,
7555 (xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007556 }
7557
7558 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007559 * if there was a parsing error return NULL
7560 */
7561 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007562 xmlRelaxNGFree(ret);
7563 ctxt->document = NULL;
7564 xmlFreeDoc(doc);
7565 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007566 }
7567
7568 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007569 * try to compile (parts of) the schemas
7570 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007571 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7572 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007573 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007574
Daniel Veillard4c004142003-10-07 11:33:24 +00007575 def = xmlRelaxNGNewDefine(ctxt, NULL);
7576 if (def != NULL) {
7577 def->type = XML_RELAXNG_START;
7578 def->content = ret->topgrammar->start;
7579 ret->topgrammar->start = def;
7580 }
7581 }
7582 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007583 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007584
7585 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007586 * Transfer the pointer for cleanup at the schema level.
7587 */
7588 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007589 ctxt->document = NULL;
7590 ret->documents = ctxt->documents;
7591 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007592
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007593 ret->includes = ctxt->includes;
7594 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007595 ret->defNr = ctxt->defNr;
7596 ret->defTab = ctxt->defTab;
7597 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007598 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007599 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007600
7601 return (ret);
7602}
Daniel Veillard4c004142003-10-07 11:33:24 +00007603
Daniel Veillard6eadf632003-01-23 18:29:16 +00007604/**
7605 * xmlRelaxNGSetParserErrors:
7606 * @ctxt: a Relax-NG validation context
7607 * @err: the error callback
7608 * @warn: the warning callback
7609 * @ctx: contextual data for the callbacks
7610 *
7611 * Set the callback functions used to handle errors for a validation context
7612 */
7613void
7614xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007615 xmlRelaxNGValidityErrorFunc err,
7616 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7617{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007618 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007619 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007620 ctxt->error = err;
7621 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007622 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007623 ctxt->userData = ctx;
7624}
Daniel Veillard409a8142003-07-18 15:16:57 +00007625
7626/**
7627 * xmlRelaxNGGetParserErrors:
7628 * @ctxt: a Relax-NG validation context
7629 * @err: the error callback result
7630 * @warn: the warning callback result
7631 * @ctx: contextual data for the callbacks result
7632 *
7633 * Get the callback information used to handle errors for a validation context
7634 *
7635 * Returns -1 in case of failure, 0 otherwise.
7636 */
7637int
7638xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007639 xmlRelaxNGValidityErrorFunc * err,
7640 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7641{
Daniel Veillard409a8142003-07-18 15:16:57 +00007642 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007643 return (-1);
7644 if (err != NULL)
7645 *err = ctxt->error;
7646 if (warn != NULL)
7647 *warn = ctxt->warning;
7648 if (ctx != NULL)
7649 *ctx = ctxt->userData;
7650 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007651}
7652
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007653/**
7654 * xmlRelaxNGSetParserStructuredErrors:
7655 * @ctxt: a Relax-NG parser context
7656 * @serror: the error callback
7657 * @ctx: contextual data for the callbacks
7658 *
7659 * Set the callback functions used to handle errors for a parsing context
7660 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007661void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007662xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007663 xmlStructuredErrorFunc serror,
7664 void *ctx)
7665{
7666 if (ctxt == NULL)
7667 return;
7668 ctxt->serror = serror;
7669 ctxt->error = NULL;
7670 ctxt->warning = NULL;
7671 ctxt->userData = ctx;
7672}
7673
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007674#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007675
Daniel Veillard6eadf632003-01-23 18:29:16 +00007676/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007677 * *
7678 * Dump back a compiled form *
7679 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007680 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007681static void xmlRelaxNGDumpDefine(FILE * output,
7682 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007683
7684/**
7685 * xmlRelaxNGDumpDefines:
7686 * @output: the file output
7687 * @defines: a list of define structures
7688 *
7689 * Dump a RelaxNG structure back
7690 */
7691static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007692xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7693{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007694 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007695 xmlRelaxNGDumpDefine(output, defines);
7696 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007697 }
7698}
7699
7700/**
7701 * xmlRelaxNGDumpDefine:
7702 * @output: the file output
7703 * @define: a define structure
7704 *
7705 * Dump a RelaxNG structure back
7706 */
7707static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007708xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7709{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007710 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007711 return;
7712 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007713 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007714 fprintf(output, "<empty/>\n");
7715 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007716 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007717 fprintf(output, "<notAllowed/>\n");
7718 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007719 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007720 fprintf(output, "<text/>\n");
7721 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007722 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007723 fprintf(output, "<element>\n");
7724 if (define->name != NULL) {
7725 fprintf(output, "<name");
7726 if (define->ns != NULL)
7727 fprintf(output, " ns=\"%s\"", define->ns);
7728 fprintf(output, ">%s</name>\n", define->name);
7729 }
7730 xmlRelaxNGDumpDefines(output, define->attrs);
7731 xmlRelaxNGDumpDefines(output, define->content);
7732 fprintf(output, "</element>\n");
7733 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007734 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007735 fprintf(output, "<list>\n");
7736 xmlRelaxNGDumpDefines(output, define->content);
7737 fprintf(output, "</list>\n");
7738 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007739 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007740 fprintf(output, "<oneOrMore>\n");
7741 xmlRelaxNGDumpDefines(output, define->content);
7742 fprintf(output, "</oneOrMore>\n");
7743 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007744 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007745 fprintf(output, "<zeroOrMore>\n");
7746 xmlRelaxNGDumpDefines(output, define->content);
7747 fprintf(output, "</zeroOrMore>\n");
7748 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007749 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007750 fprintf(output, "<choice>\n");
7751 xmlRelaxNGDumpDefines(output, define->content);
7752 fprintf(output, "</choice>\n");
7753 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007754 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007755 fprintf(output, "<group>\n");
7756 xmlRelaxNGDumpDefines(output, define->content);
7757 fprintf(output, "</group>\n");
7758 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007759 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007760 fprintf(output, "<interleave>\n");
7761 xmlRelaxNGDumpDefines(output, define->content);
7762 fprintf(output, "</interleave>\n");
7763 break;
7764 case XML_RELAXNG_OPTIONAL:
7765 fprintf(output, "<optional>\n");
7766 xmlRelaxNGDumpDefines(output, define->content);
7767 fprintf(output, "</optional>\n");
7768 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007769 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007770 fprintf(output, "<attribute>\n");
7771 xmlRelaxNGDumpDefines(output, define->content);
7772 fprintf(output, "</attribute>\n");
7773 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007774 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007775 fprintf(output, "<define");
7776 if (define->name != NULL)
7777 fprintf(output, " name=\"%s\"", define->name);
7778 fprintf(output, ">\n");
7779 xmlRelaxNGDumpDefines(output, define->content);
7780 fprintf(output, "</define>\n");
7781 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007782 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007783 fprintf(output, "<ref");
7784 if (define->name != NULL)
7785 fprintf(output, " name=\"%s\"", define->name);
7786 fprintf(output, ">\n");
7787 xmlRelaxNGDumpDefines(output, define->content);
7788 fprintf(output, "</ref>\n");
7789 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007790 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007791 fprintf(output, "<parentRef");
7792 if (define->name != NULL)
7793 fprintf(output, " name=\"%s\"", define->name);
7794 fprintf(output, ">\n");
7795 xmlRelaxNGDumpDefines(output, define->content);
7796 fprintf(output, "</parentRef>\n");
7797 break;
7798 case XML_RELAXNG_EXTERNALREF:
7799 fprintf(output, "<externalRef>");
7800 xmlRelaxNGDumpDefines(output, define->content);
7801 fprintf(output, "</externalRef>\n");
7802 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007803 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007804 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007805 TODO break;
7806 case XML_RELAXNG_START:
7807 case XML_RELAXNG_EXCEPT:
7808 case XML_RELAXNG_PARAM:
7809 TODO break;
7810 case XML_RELAXNG_NOOP:
7811 xmlRelaxNGDumpDefines(output, define->content);
7812 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007813 }
7814}
Daniel Veillard4c004142003-10-07 11:33:24 +00007815
Daniel Veillard6eadf632003-01-23 18:29:16 +00007816/**
7817 * xmlRelaxNGDumpGrammar:
7818 * @output: the file output
7819 * @grammar: a grammar structure
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007820 * @top: is this a top grammar
Daniel Veillard6eadf632003-01-23 18:29:16 +00007821 *
7822 * Dump a RelaxNG structure back
7823 */
7824static void
7825xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7826{
7827 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007828 return;
7829
Daniel Veillard6eadf632003-01-23 18:29:16 +00007830 fprintf(output, "<grammar");
7831 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007832 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7833 switch (grammar->combine) {
7834 case XML_RELAXNG_COMBINE_UNDEFINED:
7835 break;
7836 case XML_RELAXNG_COMBINE_CHOICE:
7837 fprintf(output, " combine=\"choice\"");
7838 break;
7839 case XML_RELAXNG_COMBINE_INTERLEAVE:
7840 fprintf(output, " combine=\"interleave\"");
7841 break;
7842 default:
7843 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007844 }
7845 fprintf(output, ">\n");
7846 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007847 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007848 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007849 fprintf(output, "<start>\n");
7850 xmlRelaxNGDumpDefine(output, grammar->start);
7851 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007852 }
7853 /* TODO ? Dump the defines ? */
7854 fprintf(output, "</grammar>\n");
7855}
7856
7857/**
7858 * xmlRelaxNGDump:
7859 * @output: the file output
7860 * @schema: a schema structure
7861 *
7862 * Dump a RelaxNG structure back
7863 */
7864void
7865xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7866{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007867 if (output == NULL)
7868 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007869 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007870 fprintf(output, "RelaxNG empty or failed to compile\n");
7871 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007872 }
7873 fprintf(output, "RelaxNG: ");
7874 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007875 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007876 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007877 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007878 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007879 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007880 }
7881 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007882 fprintf(output, "RelaxNG has no top grammar\n");
7883 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007884 }
7885 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7886}
7887
Daniel Veillardfebcca42003-02-16 15:44:18 +00007888/**
7889 * xmlRelaxNGDumpTree:
7890 * @output: the file output
7891 * @schema: a schema structure
7892 *
7893 * Dump the transformed RelaxNG tree.
7894 */
7895void
7896xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7897{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007898 if (output == NULL)
7899 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007900 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007901 fprintf(output, "RelaxNG empty or failed to compile\n");
7902 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007903 }
7904 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007905 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007906 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007907 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007908 }
7909}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007910#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007911
Daniel Veillard6eadf632003-01-23 18:29:16 +00007912/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007913 * *
7914 * Validation of compiled content *
7915 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007916 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007917static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7918 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007919
7920/**
7921 * xmlRelaxNGValidateCompiledCallback:
7922 * @exec: the regular expression instance
7923 * @token: the token which matched
7924 * @transdata: callback data, the define for the subelement if available
7925 @ @inputdata: callback data, the Relax NG validation context
7926 *
7927 * Handle the callback and if needed validate the element children.
7928 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007929static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007930xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007931 const xmlChar * token,
7932 void *transdata, void *inputdata)
7933{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007934 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7935 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7936 int ret;
7937
7938#ifdef DEBUG_COMPILE
7939 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007940 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007941#endif
7942 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007943 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007944 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007945 }
7946 if (define == NULL) {
7947 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007948 return;
7949 fprintf(stderr, "callback on %s missing define\n", token);
7950 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7951 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7952 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007953 }
7954 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007955 fprintf(stderr, "callback on %s missing info\n", token);
7956 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7957 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7958 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007959 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007960 fprintf(stderr, "callback on %s define is not element\n", token);
7961 if (ctxt->errNo == XML_RELAXNG_OK)
7962 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7963 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007964 }
7965 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007966 if (ret != 0)
7967 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007968}
7969
7970/**
7971 * xmlRelaxNGValidateCompiledContent:
7972 * @ctxt: the RelaxNG validation context
7973 * @regexp: the regular expression as compiled
7974 * @content: list of children to test against the regexp
7975 *
7976 * Validate the content model of an element or start using the regexp
7977 *
7978 * Returns 0 in case of success, -1 in case of error.
7979 */
7980static int
7981xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007982 xmlRegexpPtr regexp, xmlNodePtr content)
7983{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007984 xmlRegExecCtxtPtr exec;
7985 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00007986 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00007987 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007988
7989 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00007990 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00007991 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00007992 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007993 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007994 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007995 cur = content;
7996 while (cur != NULL) {
7997 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00007998 switch (cur->type) {
7999 case XML_TEXT_NODE:
8000 case XML_CDATA_SECTION_NODE:
8001 if (xmlIsBlankNode(cur))
8002 break;
8003 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
8004 if (ret < 0) {
8005 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
8006 cur->parent->name);
8007 }
8008 break;
8009 case XML_ELEMENT_NODE:
8010 if (cur->ns != NULL) {
8011 ret = xmlRegExecPushString2(exec, cur->name,
8012 cur->ns->href, ctxt);
8013 } else {
8014 ret = xmlRegExecPushString(exec, cur->name, ctxt);
8015 }
8016 if (ret < 0) {
8017 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
8018 }
8019 break;
8020 default:
8021 break;
8022 }
8023 if (ret < 0)
8024 break;
8025 /*
8026 * Switch to next element
8027 */
8028 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008029 }
8030 ret = xmlRegExecPushString(exec, NULL, NULL);
8031 if (ret == 1) {
8032 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008033 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008034 } else if (ret == 0) {
8035 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008036 * TODO: get some of the names needed to exit the current state of exec
8037 */
8038 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8039 ret = -1;
8040 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8041 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008042 } else {
8043 ret = -1;
8044 }
8045 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008046 /*
8047 * There might be content model errors outside of the pure
8048 * regexp validation, e.g. for attribute values.
8049 */
8050 if ((ret == 0) && (ctxt->perr != 0)) {
8051 ret = ctxt->perr;
8052 }
8053 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008054 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008055}
8056
8057/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008058 * *
8059 * Progressive validation of when possible *
8060 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008061 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008062static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8063 xmlRelaxNGDefinePtr defines);
8064static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008065 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008066static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008067
8068/**
8069 * xmlRelaxNGElemPush:
8070 * @ctxt: the validation context
8071 * @exec: the regexp runtime for the new content model
8072 *
8073 * Push a new regexp for the current node content model on the stack
8074 *
8075 * Returns 0 in case of success and -1 in case of error.
8076 */
8077static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008078xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8079{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008080 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008081 ctxt->elemMax = 10;
8082 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8083 sizeof
8084 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008085 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008086 xmlRngVErrMemory(ctxt, "validating\n");
8087 return (-1);
8088 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008089 }
8090 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008091 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008092 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008093 ctxt->elemMax *
8094 sizeof
8095 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008096 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008097 xmlRngVErrMemory(ctxt, "validating\n");
8098 return (-1);
8099 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008100 }
8101 ctxt->elemTab[ctxt->elemNr++] = exec;
8102 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008103 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008104}
8105
8106/**
8107 * xmlRelaxNGElemPop:
8108 * @ctxt: the validation context
8109 *
8110 * Pop the regexp of the current node content model from the stack
8111 *
8112 * Returns the exec or NULL if empty
8113 */
8114static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008115xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8116{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008117 xmlRegExecCtxtPtr ret;
8118
Daniel Veillard4c004142003-10-07 11:33:24 +00008119 if (ctxt->elemNr <= 0)
8120 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008121 ctxt->elemNr--;
8122 ret = ctxt->elemTab[ctxt->elemNr];
8123 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008124 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008125 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8126 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008127 ctxt->elem = NULL;
8128 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008129}
8130
8131/**
8132 * xmlRelaxNGValidateProgressiveCallback:
8133 * @exec: the regular expression instance
8134 * @token: the token which matched
8135 * @transdata: callback data, the define for the subelement if available
8136 @ @inputdata: callback data, the Relax NG validation context
8137 *
8138 * Handle the callback and if needed validate the element children.
8139 * some of the in/out informations are passed via the context in @inputdata.
8140 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008141static void
8142xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8143 ATTRIBUTE_UNUSED,
8144 const xmlChar * token,
8145 void *transdata, void *inputdata)
8146{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008147 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8148 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008149 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008150 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008151 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008152
8153#ifdef DEBUG_PROGRESSIVE
8154 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008155 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008156#endif
8157 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008158 fprintf(stderr, "callback on %s missing context\n", token);
8159 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008160 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008161 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008162 ctxt->pstate = 1;
8163 if (define == NULL) {
8164 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008165 return;
8166 fprintf(stderr, "callback on %s missing define\n", token);
8167 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8168 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8169 ctxt->pstate = -1;
8170 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008171 }
8172 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008173 fprintf(stderr, "callback on %s missing info\n", token);
8174 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8175 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8176 ctxt->pstate = -1;
8177 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008178 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008179 fprintf(stderr, "callback on %s define is not element\n", token);
8180 if (ctxt->errNo == XML_RELAXNG_OK)
8181 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8182 ctxt->pstate = -1;
8183 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008184 }
8185 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008186 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8187 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8188 xmlRelaxNGDumpValidError(ctxt);
8189 ctxt->pstate = -1;
8190 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008191 }
8192 if (define->contModel == NULL) {
8193 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008194 * this node cannot be validated in a streamable fashion
8195 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008196#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008197 xmlGenericError(xmlGenericErrorContext,
8198 "Element '%s' validation is not streamable\n",
8199 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008200#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008201 ctxt->pstate = 0;
8202 ctxt->pdef = define;
8203 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008204 }
8205 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008206 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008207 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008208 ctxt->pstate = -1;
8209 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008210 }
8211 xmlRelaxNGElemPush(ctxt, exec);
8212
8213 /*
8214 * Validate the attributes part of the content.
8215 */
8216 state = xmlRelaxNGNewValidState(ctxt, node);
8217 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008218 ctxt->pstate = -1;
8219 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008220 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008221 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008222 ctxt->state = state;
8223 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008224 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8225 if (ret != 0) {
8226 ctxt->pstate = -1;
8227 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8228 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008229 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008230 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008231 ctxt->state->seq = NULL;
8232 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8233 if (ret != 0) {
8234 ctxt->pstate = -1;
8235 }
8236 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008237 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008238 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008239
8240 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008241
Daniel Veillard4c004142003-10-07 11:33:24 +00008242 for (i = 0; i < ctxt->states->nbState; i++) {
8243 state = ctxt->states->tabState[i];
8244 ctxt->state = state;
8245 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008246
Daniel Veillard4c004142003-10-07 11:33:24 +00008247 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8248 tmp = 0;
8249 break;
8250 }
8251 }
8252 if (tmp != 0) {
8253 /*
8254 * validation error, log the message for the "best" one
8255 */
8256 ctxt->flags |= FLAGS_IGNORABLE;
8257 xmlRelaxNGLogBestError(ctxt);
8258 }
8259 for (i = 0; i < ctxt->states->nbState; i++) {
8260 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8261 }
8262 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8263 ctxt->states = NULL;
8264 if ((ret == 0) && (tmp == -1))
8265 ctxt->pstate = -1;
8266 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008267 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008268 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008269 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8270 xmlRelaxNGDumpValidError(ctxt);
8271 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008272 }
8273 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008274}
8275
8276/**
8277 * xmlRelaxNGValidatePushElement:
8278 * @ctxt: the validation context
8279 * @doc: a document instance
8280 * @elem: an element instance
8281 *
8282 * Push a new element start on the RelaxNG validation stack.
8283 *
8284 * returns 1 if no validation problem was found or 0 if validating the
8285 * element requires a full node, and -1 in case of error.
8286 */
8287int
Daniel Veillard33300b42003-04-17 09:09:19 +00008288xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8289 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008290 xmlNodePtr elem)
8291{
8292 int ret = 1;
8293
8294 if ((ctxt == NULL) || (elem == NULL))
8295 return (-1);
8296
8297#ifdef DEBUG_PROGRESSIVE
8298 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8299#endif
8300 if (ctxt->elem == 0) {
8301 xmlRelaxNGPtr schema;
8302 xmlRelaxNGGrammarPtr grammar;
8303 xmlRegExecCtxtPtr exec;
8304 xmlRelaxNGDefinePtr define;
8305
8306 schema = ctxt->schema;
8307 if (schema == NULL) {
8308 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8309 return (-1);
8310 }
8311 grammar = schema->topgrammar;
8312 if ((grammar == NULL) || (grammar->start == NULL)) {
8313 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8314 return (-1);
8315 }
8316 define = grammar->start;
8317 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008318 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008319 return (0);
8320 }
8321 exec = xmlRegNewExecCtxt(define->contModel,
8322 xmlRelaxNGValidateProgressiveCallback,
8323 ctxt);
8324 if (exec == NULL) {
8325 return (-1);
8326 }
8327 xmlRelaxNGElemPush(ctxt, exec);
8328 }
8329 ctxt->pnode = elem;
8330 ctxt->pstate = 0;
8331 if (elem->ns != NULL) {
8332 ret =
8333 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8334 ctxt);
8335 } else {
8336 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8337 }
8338 if (ret < 0) {
8339 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8340 } else {
8341 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008342 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008343 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008344 ret = -1;
8345 else
8346 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008347 }
8348#ifdef DEBUG_PROGRESSIVE
8349 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008350 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8351 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008352#endif
8353 return (ret);
8354}
8355
8356/**
8357 * xmlRelaxNGValidatePushCData:
8358 * @ctxt: the RelaxNG validation context
8359 * @data: some character data read
Michael Woodfb27e2c2012-09-28 08:59:33 +02008360 * @len: the length of the data
Daniel Veillardf4e55762003-04-15 23:32:22 +00008361 *
8362 * check the CData parsed for validation in the current stack
8363 *
8364 * returns 1 if no validation problem was found or -1 otherwise
8365 */
8366int
8367xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008368 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008369{
8370 int ret = 1;
8371
8372 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8373 return (-1);
8374
8375#ifdef DEBUG_PROGRESSIVE
8376 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8377#endif
8378
8379 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008380 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008381 break;
8382 data++;
8383 }
8384 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008385 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008386
8387 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8388 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008389 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008390#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008391 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008392#endif
8393
Daniel Veillard4c004142003-10-07 11:33:24 +00008394 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008395 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008396 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008397}
8398
8399/**
8400 * xmlRelaxNGValidatePopElement:
8401 * @ctxt: the RelaxNG validation context
8402 * @doc: a document instance
8403 * @elem: an element instance
8404 *
8405 * Pop the element end from the RelaxNG validation stack.
8406 *
8407 * returns 1 if no validation problem was found or 0 otherwise
8408 */
8409int
8410xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8411 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008412 xmlNodePtr elem)
8413{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008414 int ret;
8415 xmlRegExecCtxtPtr exec;
8416
Daniel Veillard4c004142003-10-07 11:33:24 +00008417 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8418 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008419#ifdef DEBUG_PROGRESSIVE
8420 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8421#endif
8422 /*
8423 * verify that we reached a terminal state of the content model.
8424 */
8425 exec = xmlRelaxNGElemPop(ctxt);
8426 ret = xmlRegExecPushString(exec, NULL, NULL);
8427 if (ret == 0) {
8428 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008429 * TODO: get some of the names needed to exit the current state of exec
8430 */
8431 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8432 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008433 } else if (ret < 0) {
8434 ret = -1;
8435 } else {
8436 ret = 1;
8437 }
8438 xmlRegFreeExecCtxt(exec);
8439#ifdef DEBUG_PROGRESSIVE
8440 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008441 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8442 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008443#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008444 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008445}
8446
8447/**
8448 * xmlRelaxNGValidateFullElement:
8449 * @ctxt: the validation context
8450 * @doc: a document instance
8451 * @elem: an element instance
8452 *
8453 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8454 * 0 and the content of the node has been expanded.
8455 *
8456 * returns 1 if no validation problem was found or -1 in case of error.
8457 */
8458int
Daniel Veillard33300b42003-04-17 09:09:19 +00008459xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8460 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008461 xmlNodePtr elem)
8462{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008463 int ret;
8464 xmlRelaxNGValidStatePtr state;
8465
Daniel Veillard4c004142003-10-07 11:33:24 +00008466 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8467 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008468#ifdef DEBUG_PROGRESSIVE
8469 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8470#endif
8471 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8472 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008473 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008474 }
8475 state->seq = elem;
8476 ctxt->state = state;
8477 ctxt->errNo = XML_RELAXNG_OK;
8478 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008479 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8480 ret = -1;
8481 else
8482 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008483 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008484 ctxt->state = NULL;
8485#ifdef DEBUG_PROGRESSIVE
8486 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008487 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8488 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008489#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008490 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008491}
8492
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008493/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008494 * *
8495 * Generic interpreted validation implementation *
8496 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008497 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008498static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8499 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008500
8501/**
8502 * xmlRelaxNGSkipIgnored:
8503 * @ctxt: a schema validation context
8504 * @node: the top node.
8505 *
8506 * Skip ignorable nodes in that context
8507 *
8508 * Returns the new sibling or NULL in case of error.
8509 */
8510static xmlNodePtr
8511xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008512 xmlNodePtr node)
8513{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008514 /*
8515 * TODO complete and handle entities
8516 */
8517 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008518 ((node->type == XML_COMMENT_NODE) ||
8519 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008520 (node->type == XML_XINCLUDE_START) ||
8521 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008522 (((node->type == XML_TEXT_NODE) ||
8523 (node->type == XML_CDATA_SECTION_NODE)) &&
8524 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8525 (IS_BLANK_NODE(node)))))) {
8526 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008527 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008528 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008529}
8530
8531/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008532 * xmlRelaxNGNormalize:
8533 * @ctxt: a schema validation context
8534 * @str: the string to normalize
8535 *
8536 * Implements the normalizeWhiteSpace( s ) function from
8537 * section 6.2.9 of the spec
8538 *
8539 * Returns the new string or NULL in case of error.
8540 */
8541static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008542xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8543{
Daniel Veillardedc91922003-01-26 00:52:04 +00008544 xmlChar *ret, *p;
8545 const xmlChar *tmp;
8546 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008547
Daniel Veillardedc91922003-01-26 00:52:04 +00008548 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008549 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008550 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008551 while (*tmp != 0)
8552 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008553 len = tmp - str;
8554
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008555 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008556 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008557 xmlRngVErrMemory(ctxt, "validating\n");
8558 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008559 }
8560 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008561 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008562 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008563 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008564 if (IS_BLANK_CH(*str)) {
8565 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008566 str++;
8567 if (*str == 0)
8568 break;
8569 *p++ = ' ';
8570 } else
8571 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008572 }
8573 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008574 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008575}
8576
8577/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008578 * xmlRelaxNGValidateDatatype:
8579 * @ctxt: a Relax-NG validation context
8580 * @value: the string value
8581 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008582 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008583 *
8584 * Validate the given value against the dataype
8585 *
8586 * Returns 0 if the validation succeeded or an error code.
8587 */
8588static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008589xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8590 const xmlChar * value,
8591 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8592{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008593 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008594 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008595 void *result = NULL;
8596 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008597
8598 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008599 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008600 }
8601 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008602 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008603 if ((define->attrs != NULL) &&
8604 (define->attrs->type == XML_RELAXNG_PARAM)) {
8605 ret =
8606 lib->check(lib->data, define->name, value, &result, node);
8607 } else {
8608 ret = lib->check(lib->data, define->name, value, NULL, node);
8609 }
8610 } else
8611 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008612 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008613 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8614 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8615 lib->freef(lib->data, result);
8616 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008617 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008618 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008619 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008620 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008621 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008622 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8623 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008624 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008625 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008626 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008627 if (lib->facet != NULL) {
8628 tmp = lib->facet(lib->data, define->name, cur->name,
8629 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008630 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008631 ret = -1;
8632 }
8633 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008634 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008635 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008636 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008637
Daniel Veillard4c004142003-10-07 11:33:24 +00008638 oldvalue = ctxt->state->value;
8639 oldendvalue = ctxt->state->endvalue;
8640 ctxt->state->value = (xmlChar *) value;
8641 ctxt->state->endvalue = NULL;
8642 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8643 ctxt->state->value = (xmlChar *) oldvalue;
8644 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008645 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008646 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008647 lib->freef(lib->data, result);
8648 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008649}
8650
8651/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008652 * xmlRelaxNGNextValue:
8653 * @ctxt: a Relax-NG validation context
8654 *
8655 * Skip to the next value when validating within a list
8656 *
8657 * Returns 0 if the operation succeeded or an error code.
8658 */
8659static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008660xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8661{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008662 xmlChar *cur;
8663
8664 cur = ctxt->state->value;
8665 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008666 ctxt->state->value = NULL;
8667 ctxt->state->endvalue = NULL;
8668 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008669 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008670 while (*cur != 0)
8671 cur++;
8672 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8673 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008674 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008675 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008676 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008677 ctxt->state->value = cur;
8678 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008679}
8680
8681/**
8682 * xmlRelaxNGValidateValueList:
8683 * @ctxt: a Relax-NG validation context
8684 * @defines: the list of definitions to verify
8685 *
8686 * Validate the given set of definitions for the current value
8687 *
8688 * Returns 0 if the validation succeeded or an error code.
8689 */
8690static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008691xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8692 xmlRelaxNGDefinePtr defines)
8693{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008694 int ret = 0;
8695
8696 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008697 ret = xmlRelaxNGValidateValue(ctxt, defines);
8698 if (ret != 0)
8699 break;
8700 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008701 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008702 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008703}
8704
8705/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008706 * xmlRelaxNGValidateValue:
8707 * @ctxt: a Relax-NG validation context
8708 * @define: the definition to verify
8709 *
8710 * Validate the given definition for the current value
8711 *
8712 * Returns 0 if the validation succeeded or an error code.
8713 */
8714static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008715xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8716 xmlRelaxNGDefinePtr define)
8717{
Daniel Veillardedc91922003-01-26 00:52:04 +00008718 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008719 xmlChar *value;
8720
8721 value = ctxt->state->value;
8722 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008723 case XML_RELAXNG_EMPTY:{
8724 if ((value != NULL) && (value[0] != 0)) {
8725 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008726
William M. Brack76e95df2003-10-18 16:20:14 +00008727 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008728 idx++;
8729 if (value[idx] != 0)
8730 ret = -1;
8731 }
8732 break;
8733 }
8734 case XML_RELAXNG_TEXT:
8735 break;
8736 case XML_RELAXNG_VALUE:{
8737 if (!xmlStrEqual(value, define->value)) {
8738 if (define->name != NULL) {
8739 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008740
Daniel Veillard4c004142003-10-07 11:33:24 +00008741 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8742 if ((lib != NULL) && (lib->comp != NULL)) {
8743 ret = lib->comp(lib->data, define->name,
8744 define->value, define->node,
8745 (void *) define->attrs,
8746 value, ctxt->state->node);
8747 } else
8748 ret = -1;
8749 if (ret < 0) {
8750 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8751 define->name);
8752 return (-1);
8753 } else if (ret == 1) {
8754 ret = 0;
8755 } else {
8756 ret = -1;
8757 }
8758 } else {
8759 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008760
Daniel Veillard4c004142003-10-07 11:33:24 +00008761 /*
8762 * TODO: trivial optimizations are possible by
8763 * computing at compile-time
8764 */
8765 nval = xmlRelaxNGNormalize(ctxt, define->value);
8766 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008767
Daniel Veillard4c004142003-10-07 11:33:24 +00008768 if ((nval == NULL) || (nvalue == NULL) ||
8769 (!xmlStrEqual(nval, nvalue)))
8770 ret = -1;
8771 if (nval != NULL)
8772 xmlFree(nval);
8773 if (nvalue != NULL)
8774 xmlFree(nvalue);
8775 }
8776 }
8777 if (ret == 0)
8778 xmlRelaxNGNextValue(ctxt);
8779 break;
8780 }
8781 case XML_RELAXNG_DATATYPE:{
8782 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8783 ctxt->state->seq);
8784 if (ret == 0)
8785 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008786
Daniel Veillard4c004142003-10-07 11:33:24 +00008787 break;
8788 }
8789 case XML_RELAXNG_CHOICE:{
8790 xmlRelaxNGDefinePtr list = define->content;
8791 xmlChar *oldvalue;
8792
8793 oldflags = ctxt->flags;
8794 ctxt->flags |= FLAGS_IGNORABLE;
8795
8796 oldvalue = ctxt->state->value;
8797 while (list != NULL) {
8798 ret = xmlRelaxNGValidateValue(ctxt, list);
8799 if (ret == 0) {
8800 break;
8801 }
8802 ctxt->state->value = oldvalue;
8803 list = list->next;
8804 }
8805 ctxt->flags = oldflags;
8806 if (ret != 0) {
8807 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8808 xmlRelaxNGDumpValidError(ctxt);
8809 } else {
8810 if (ctxt->errNr > 0)
8811 xmlRelaxNGPopErrors(ctxt, 0);
8812 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008813 break;
8814 }
8815 case XML_RELAXNG_LIST:{
8816 xmlRelaxNGDefinePtr list = define->content;
8817 xmlChar *oldvalue, *oldend, *val, *cur;
8818
Daniel Veillard416589a2003-02-17 17:25:42 +00008819#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008820 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008821#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008822
Daniel Veillard4c004142003-10-07 11:33:24 +00008823 oldvalue = ctxt->state->value;
8824 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008825
Daniel Veillard4c004142003-10-07 11:33:24 +00008826 val = xmlStrdup(oldvalue);
8827 if (val == NULL) {
8828 val = xmlStrdup(BAD_CAST "");
8829 }
8830 if (val == NULL) {
8831 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8832 return (-1);
8833 }
8834 cur = val;
8835 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008836 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008837 *cur = 0;
8838 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008839#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008840 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008841#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008842 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008843 *cur++ = 0;
8844 } else
8845 cur++;
8846 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008847#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008848 xmlGenericError(xmlGenericErrorContext,
8849 "list value: '%s' found %d items\n",
8850 oldvalue, nb_values);
8851 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008852#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008853 ctxt->state->endvalue = cur;
8854 cur = val;
8855 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8856 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008857
Daniel Veillard4c004142003-10-07 11:33:24 +00008858 ctxt->state->value = cur;
8859
8860 while (list != NULL) {
8861 if (ctxt->state->value == ctxt->state->endvalue)
8862 ctxt->state->value = NULL;
8863 ret = xmlRelaxNGValidateValue(ctxt, list);
8864 if (ret != 0) {
8865#ifdef DEBUG_LIST
8866 xmlGenericError(xmlGenericErrorContext,
8867 "Failed to validate value: '%s' with %d rule\n",
8868 ctxt->state->value, nb_values);
8869#endif
8870 break;
8871 }
8872#ifdef DEBUG_LIST
8873 nb_values++;
8874#endif
8875 list = list->next;
8876 }
8877
8878 if ((ret == 0) && (ctxt->state->value != NULL) &&
8879 (ctxt->state->value != ctxt->state->endvalue)) {
8880 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8881 ctxt->state->value);
8882 ret = -1;
8883 }
8884 xmlFree(val);
8885 ctxt->state->value = oldvalue;
8886 ctxt->state->endvalue = oldend;
8887 break;
8888 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008889 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008890 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8891 if (ret != 0) {
8892 break;
8893 }
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +02008894 /* Falls through. */
Daniel Veillard4c004142003-10-07 11:33:24 +00008895 case XML_RELAXNG_ZEROORMORE:{
8896 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008897
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008898 if ((ctxt->state->value == NULL) ||
8899 (*ctxt->state->value == 0)) {
8900 ret = 0;
8901 break;
8902 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008903 oldflags = ctxt->flags;
8904 ctxt->flags |= FLAGS_IGNORABLE;
8905 cur = ctxt->state->value;
8906 temp = NULL;
8907 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8908 (temp != cur)) {
8909 temp = cur;
8910 ret =
8911 xmlRelaxNGValidateValueList(ctxt, define->content);
8912 if (ret != 0) {
8913 ctxt->state->value = temp;
8914 ret = 0;
8915 break;
8916 }
8917 cur = ctxt->state->value;
8918 }
8919 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008920 if (ctxt->errNr > 0)
8921 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008922 break;
8923 }
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008924 case XML_RELAXNG_OPTIONAL:{
8925 xmlChar *temp;
8926
8927 if ((ctxt->state->value == NULL) ||
8928 (*ctxt->state->value == 0)) {
8929 ret = 0;
8930 break;
8931 }
8932 oldflags = ctxt->flags;
8933 ctxt->flags |= FLAGS_IGNORABLE;
8934 temp = ctxt->state->value;
8935 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8936 ctxt->flags = oldflags;
8937 if (ret != 0) {
8938 ctxt->state->value = temp;
8939 if (ctxt->errNr > 0)
8940 xmlRelaxNGPopErrors(ctxt, 0);
8941 ret = 0;
8942 break;
8943 }
8944 if (ctxt->errNr > 0)
8945 xmlRelaxNGPopErrors(ctxt, 0);
8946 break;
8947 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008948 case XML_RELAXNG_EXCEPT:{
8949 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008950
Daniel Veillard4c004142003-10-07 11:33:24 +00008951 list = define->content;
8952 while (list != NULL) {
8953 ret = xmlRelaxNGValidateValue(ctxt, list);
8954 if (ret == 0) {
8955 ret = -1;
8956 break;
8957 } else
8958 ret = 0;
8959 list = list->next;
8960 }
8961 break;
8962 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008963 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008964 case XML_RELAXNG_GROUP:{
8965 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008966
Daniel Veillard4c004142003-10-07 11:33:24 +00008967 list = define->content;
8968 while (list != NULL) {
8969 ret = xmlRelaxNGValidateValue(ctxt, list);
8970 if (ret != 0) {
8971 ret = -1;
8972 break;
8973 } else
8974 ret = 0;
8975 list = list->next;
8976 }
8977 break;
8978 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008979 case XML_RELAXNG_REF:
8980 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00008981 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02008982 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
8983 ret = -1;
8984 } else {
8985 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8986 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008987 break;
8988 default:
8989 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008990 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008991 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008992}
8993
8994/**
8995 * xmlRelaxNGValidateValueContent:
8996 * @ctxt: a Relax-NG validation context
8997 * @defines: the list of definitions to verify
8998 *
8999 * Validate the given definitions for the current value
9000 *
9001 * Returns 0 if the validation succeeded or an error code.
9002 */
9003static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009004xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
9005 xmlRelaxNGDefinePtr defines)
9006{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009007 int ret = 0;
9008
9009 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009010 ret = xmlRelaxNGValidateValue(ctxt, defines);
9011 if (ret != 0)
9012 break;
9013 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009014 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009015 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009016}
9017
9018/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009019 * xmlRelaxNGAttributeMatch:
9020 * @ctxt: a Relax-NG validation context
9021 * @define: the definition to check
9022 * @prop: the attribute
9023 *
9024 * Check if the attribute matches the definition nameClass
9025 *
9026 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
9027 */
9028static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009029xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
9030 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
9031{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009032 int ret;
9033
9034 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009035 if (!xmlStrEqual(define->name, prop->name))
9036 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009037 }
9038 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009039 if (define->ns[0] == 0) {
9040 if (prop->ns != NULL)
9041 return (0);
9042 } else {
9043 if ((prop->ns == NULL) ||
9044 (!xmlStrEqual(define->ns, prop->ns->href)))
9045 return (0);
9046 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009047 }
9048 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009049 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009050 define = define->nameClass;
9051 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009052 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009053
Daniel Veillard4c004142003-10-07 11:33:24 +00009054 list = define->content;
9055 while (list != NULL) {
9056 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9057 if (ret == 1)
9058 return (0);
9059 if (ret < 0)
9060 return (ret);
9061 list = list->next;
9062 }
Shaun McCance6473a412013-10-23 14:51:33 -04009063 } else if (define->type == XML_RELAXNG_CHOICE) {
9064 xmlRelaxNGDefinePtr list;
9065
9066 list = define->nameClass;
9067 while (list != NULL) {
9068 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9069 if (ret == 1)
9070 return (1);
9071 if (ret < 0)
9072 return (ret);
9073 list = list->next;
9074 }
9075 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009076 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009077 TODO}
9078 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009079}
9080
9081/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009082 * xmlRelaxNGValidateAttribute:
9083 * @ctxt: a Relax-NG validation context
9084 * @define: the definition to verify
9085 *
9086 * Validate the given attribute definition for that node
9087 *
9088 * Returns 0 if the validation succeeded or an error code.
9089 */
9090static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009091xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9092 xmlRelaxNGDefinePtr define)
9093{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009094 int ret = 0, i;
9095 xmlChar *value, *oldvalue;
9096 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009097 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009098
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009099 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009100 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009101 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009102 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9103 tmp = ctxt->state->attrs[i];
9104 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9105 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9106 (tmp->ns == NULL)) ||
9107 ((tmp->ns != NULL) &&
9108 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9109 prop = tmp;
9110 break;
9111 }
9112 }
9113 }
9114 if (prop != NULL) {
9115 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9116 oldvalue = ctxt->state->value;
9117 oldseq = ctxt->state->seq;
9118 ctxt->state->seq = (xmlNodePtr) prop;
9119 ctxt->state->value = value;
9120 ctxt->state->endvalue = NULL;
9121 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9122 if (ctxt->state->value != NULL)
9123 value = ctxt->state->value;
9124 if (value != NULL)
9125 xmlFree(value);
9126 ctxt->state->value = oldvalue;
9127 ctxt->state->seq = oldseq;
9128 if (ret == 0) {
9129 /*
9130 * flag the attribute as processed
9131 */
9132 ctxt->state->attrs[i] = NULL;
9133 ctxt->state->nbAttrLeft--;
9134 }
9135 } else {
9136 ret = -1;
9137 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009138#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009139 xmlGenericError(xmlGenericErrorContext,
9140 "xmlRelaxNGValidateAttribute(%s): %d\n",
9141 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009142#endif
9143 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009144 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9145 tmp = ctxt->state->attrs[i];
9146 if ((tmp != NULL) &&
9147 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9148 prop = tmp;
9149 break;
9150 }
9151 }
9152 if (prop != NULL) {
9153 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9154 oldvalue = ctxt->state->value;
9155 oldseq = ctxt->state->seq;
9156 ctxt->state->seq = (xmlNodePtr) prop;
9157 ctxt->state->value = value;
9158 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9159 if (ctxt->state->value != NULL)
9160 value = ctxt->state->value;
9161 if (value != NULL)
9162 xmlFree(value);
9163 ctxt->state->value = oldvalue;
9164 ctxt->state->seq = oldseq;
9165 if (ret == 0) {
9166 /*
9167 * flag the attribute as processed
9168 */
9169 ctxt->state->attrs[i] = NULL;
9170 ctxt->state->nbAttrLeft--;
9171 }
9172 } else {
9173 ret = -1;
9174 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009175#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009176 if (define->ns != NULL) {
9177 xmlGenericError(xmlGenericErrorContext,
9178 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9179 define->ns, ret);
9180 } else {
9181 xmlGenericError(xmlGenericErrorContext,
9182 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9183 ret);
9184 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009185#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009186 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009187
9188 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009189}
9190
9191/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009192 * xmlRelaxNGValidateAttributeList:
9193 * @ctxt: a Relax-NG validation context
9194 * @define: the list of definition to verify
9195 *
9196 * Validate the given node against the list of attribute definitions
9197 *
9198 * Returns 0 if the validation succeeded or an error code.
9199 */
9200static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009201xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9202 xmlRelaxNGDefinePtr defines)
9203{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009204 int ret = 0, res;
9205 int needmore = 0;
9206 xmlRelaxNGDefinePtr cur;
9207
9208 cur = defines;
9209 while (cur != NULL) {
9210 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009211 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9212 ret = -1;
9213 } else
9214 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009215 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009216 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009217 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009218 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009219 cur = defines;
9220 while (cur != NULL) {
9221 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009222 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9223 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9224 if (res < 0)
9225 ret = -1;
9226 } else {
9227 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9228 return (-1);
9229 }
9230 if (res == -1) /* continues on -2 */
9231 break;
9232 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009233 cur = cur->next;
9234 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009235
9236 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009237}
9238
9239/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009240 * xmlRelaxNGNodeMatchesList:
9241 * @node: the node
9242 * @list: a NULL terminated array of definitions
9243 *
9244 * Check if a node can be matched by one of the definitions
9245 *
9246 * Returns 1 if matches 0 otherwise
9247 */
9248static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009249xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9250{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009251 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009252 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009253
9254 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009255 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009256
9257 cur = list[i++];
9258 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009259 if ((node->type == XML_ELEMENT_NODE) &&
9260 (cur->type == XML_RELAXNG_ELEMENT)) {
9261 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9262 if (tmp == 1)
9263 return (1);
9264 } else if (((node->type == XML_TEXT_NODE) ||
9265 (node->type == XML_CDATA_SECTION_NODE)) &&
9266 (cur->type == XML_RELAXNG_TEXT)) {
9267 return (1);
9268 }
9269 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009270 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009271 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009272}
9273
9274/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009275 * xmlRelaxNGValidateInterleave:
9276 * @ctxt: a Relax-NG validation context
9277 * @define: the definition to verify
9278 *
9279 * Validate an interleave definition for a node.
9280 *
9281 * Returns 0 if the validation succeeded or an error code.
9282 */
9283static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009284xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9285 xmlRelaxNGDefinePtr define)
9286{
William M. Brack779af002003-08-01 15:55:39 +00009287 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009288 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009289 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009290
9291 xmlRelaxNGValidStatePtr oldstate;
9292 xmlRelaxNGPartitionPtr partitions;
9293 xmlRelaxNGInterleaveGroupPtr group = NULL;
9294 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9295 xmlNodePtr *list = NULL, *lasts = NULL;
9296
9297 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009298 partitions = (xmlRelaxNGPartitionPtr) define->data;
9299 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009300 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009301 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9302 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009303 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009304 /*
9305 * Optimizations for MIXED
9306 */
9307 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009308 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009309 ctxt->flags |= FLAGS_MIXED_CONTENT;
9310 if (nbgroups == 2) {
9311 /*
9312 * this is a pure <mixed> case
9313 */
9314 if (ctxt->state != NULL)
9315 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9316 ctxt->state->seq);
9317 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9318 ret = xmlRelaxNGValidateDefinition(ctxt,
9319 partitions->groups[1]->
9320 rule);
9321 else
9322 ret = xmlRelaxNGValidateDefinition(ctxt,
9323 partitions->groups[0]->
9324 rule);
9325 if (ret == 0) {
9326 if (ctxt->state != NULL)
9327 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9328 ctxt->state->
9329 seq);
9330 }
9331 ctxt->flags = oldflags;
9332 return (ret);
9333 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009334 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009335
9336 /*
9337 * Build arrays to store the first and last node of the chain
9338 * pertaining to each group
9339 */
9340 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9341 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009342 xmlRngVErrMemory(ctxt, "validating\n");
9343 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009344 }
9345 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9346 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9347 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009348 xmlRngVErrMemory(ctxt, "validating\n");
9349 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009350 }
9351 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9352
9353 /*
9354 * Walk the sequence of children finding the right group and
9355 * sorting them in sequences.
9356 */
9357 cur = ctxt->state->seq;
9358 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9359 start = cur;
9360 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009361 ctxt->state->seq = cur;
9362 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009363 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009364 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009365
Daniel Veillard4c004142003-10-07 11:33:24 +00009366 if ((cur->type == XML_TEXT_NODE) ||
9367 (cur->type == XML_CDATA_SECTION_NODE)) {
9368 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9369 NULL);
9370 } else if (cur->type == XML_ELEMENT_NODE) {
9371 if (cur->ns != NULL) {
9372 tmp = xmlHashLookup2(partitions->triage, cur->name,
9373 cur->ns->href);
9374 if (tmp == NULL)
9375 tmp = xmlHashLookup2(partitions->triage,
9376 BAD_CAST "#any",
9377 cur->ns->href);
9378 } else
9379 tmp =
9380 xmlHashLookup2(partitions->triage, cur->name,
9381 NULL);
9382 if (tmp == NULL)
9383 tmp =
9384 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9385 NULL);
9386 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009387
Daniel Veillard4c004142003-10-07 11:33:24 +00009388 if (tmp == NULL) {
9389 i = nbgroups;
9390 } else {
Nick Wellnhoferd422b952017-10-09 13:37:42 +02009391 i = ((ptrdiff_t) tmp) - 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00009392 if (partitions->flags & IS_NEEDCHECK) {
9393 group = partitions->groups[i];
9394 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9395 i = nbgroups;
9396 }
9397 }
9398 } else {
9399 for (i = 0; i < nbgroups; i++) {
9400 group = partitions->groups[i];
9401 if (group == NULL)
9402 continue;
9403 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9404 break;
9405 }
9406 }
9407 /*
9408 * We break as soon as an element not matched is found
9409 */
9410 if (i >= nbgroups) {
9411 break;
9412 }
9413 if (lasts[i] != NULL) {
9414 lasts[i]->next = cur;
9415 lasts[i] = cur;
9416 } else {
9417 list[i] = cur;
9418 lasts[i] = cur;
9419 }
9420 if (cur->next != NULL)
9421 lastchg = cur->next;
9422 else
9423 lastchg = cur;
9424 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009425 }
9426 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009427 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9428 ret = -1;
9429 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009430 }
9431 lastelem = cur;
9432 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009433 for (i = 0; i < nbgroups; i++) {
9434 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
Gaurav Gupta6d753992014-07-14 16:01:10 +08009435 if (ctxt->state == NULL) {
9436 ret = -1;
9437 break;
9438 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009439 group = partitions->groups[i];
9440 if (lasts[i] != NULL) {
9441 last = lasts[i]->next;
9442 lasts[i]->next = NULL;
9443 }
9444 ctxt->state->seq = list[i];
9445 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9446 if (ret != 0)
9447 break;
9448 if (ctxt->state != NULL) {
9449 cur = ctxt->state->seq;
9450 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9451 xmlRelaxNGFreeValidState(ctxt, oldstate);
9452 oldstate = ctxt->state;
9453 ctxt->state = NULL;
9454 if (cur != NULL) {
9455 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9456 ret = -1;
9457 ctxt->state = oldstate;
9458 goto done;
9459 }
9460 } else if (ctxt->states != NULL) {
9461 int j;
9462 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009463 int best = -1;
9464 int lowattr = -1;
9465
9466 /*
9467 * PBM: what happen if there is attributes checks in the interleaves
9468 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009469
Daniel Veillard4c004142003-10-07 11:33:24 +00009470 for (j = 0; j < ctxt->states->nbState; j++) {
9471 cur = ctxt->states->tabState[j]->seq;
9472 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9473 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009474 if (found == 0) {
9475 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9476 best = j;
9477 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009478 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009479 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9480 /* try to keep the latest one to mach old heuristic */
9481 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9482 best = j;
9483 }
9484 if (lowattr == 0)
9485 break;
9486 } else if (found == 0) {
9487 if (lowattr == -1) {
9488 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9489 best = j;
9490 } else
9491 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9492 /* try to keep the latest one to mach old heuristic */
9493 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9494 best = j;
9495 }
9496 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009497 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009498 /*
9499 * BIG PBM: here we pick only one restarting point :-(
9500 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009501 if (ctxt->states->nbState > 0) {
9502 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009503 if (best != -1) {
9504 oldstate = ctxt->states->tabState[best];
9505 ctxt->states->tabState[best] = NULL;
9506 } else {
9507 oldstate =
9508 ctxt->states->tabState[ctxt->states->nbState - 1];
9509 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009510 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009511 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009512 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009513 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009514 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9515 }
9516 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9517 ctxt->states = NULL;
9518 if (found == 0) {
Daniel Veillard76d36452009-09-07 11:19:33 +02009519 if (cur == NULL) {
Ben Waltona7a6a4b2010-03-15 10:06:36 +01009520 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
9521 (const xmlChar *) "noname");
Daniel Veillard76d36452009-09-07 11:19:33 +02009522 } else {
9523 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9524 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009525 ret = -1;
9526 ctxt->state = oldstate;
9527 goto done;
9528 }
9529 } else {
9530 ret = -1;
9531 break;
9532 }
9533 if (lasts[i] != NULL) {
9534 lasts[i]->next = last;
9535 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009536 }
9537 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009538 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009539 ctxt->state = oldstate;
9540 ctxt->state->seq = lastelem;
9541 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009542 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9543 ret = -1;
9544 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009545 }
9546
Daniel Veillard4c004142003-10-07 11:33:24 +00009547 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009548 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009549 /*
9550 * builds the next links chain from the prev one
9551 */
9552 cur = lastchg;
9553 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009554 if ((cur == start) || (cur->prev == NULL))
9555 break;
9556 cur->prev->next = cur;
9557 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009558 }
9559 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009560 if (ctxt->errNr > errNr)
9561 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009562 }
9563
9564 xmlFree(list);
9565 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009566 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009567}
9568
9569/**
9570 * xmlRelaxNGValidateDefinitionList:
9571 * @ctxt: a Relax-NG validation context
9572 * @define: the list of definition to verify
9573 *
9574 * Validate the given node content against the (list) of definitions
9575 *
9576 * Returns 0 if the validation succeeded or an error code.
9577 */
9578static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009579xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9580 xmlRelaxNGDefinePtr defines)
9581{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009582 int ret = 0, res;
9583
9584
Daniel Veillard952379b2003-03-17 15:37:12 +00009585 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009586 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9587 BAD_CAST "NULL definition list");
9588 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009589 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009590 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009591 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9592 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9593 if (res < 0)
9594 ret = -1;
9595 } else {
9596 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9597 return (-1);
9598 }
9599 if (res == -1) /* continues on -2 */
9600 break;
9601 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009602 }
9603
Daniel Veillard4c004142003-10-07 11:33:24 +00009604 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009605}
9606
9607/**
9608 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009609 * @ctxt: a Relax-NG validation context
9610 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009611 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009612 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009613 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009614 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009615 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009616 */
9617static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009618xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9619 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9620{
Daniel Veillard580ced82003-03-21 21:22:48 +00009621 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009622
Daniel Veillardfd573f12003-03-16 17:52:32 +00009623 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009624 if (!xmlStrEqual(elem->name, define->name)) {
9625 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9626 return (0);
9627 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009628 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009629 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009630 if (elem->ns == NULL) {
9631 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9632 return (0);
9633 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9634 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9635 elem->name, define->ns);
9636 return (0);
9637 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009638 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009639 (define->name == NULL)) {
9640 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9641 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009642 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009643 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9644 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009645 }
9646
9647 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009648 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009649
9650 define = define->nameClass;
9651 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009652 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009653
Daniel Veillard4c004142003-10-07 11:33:24 +00009654 if (ctxt != NULL) {
9655 oldflags = ctxt->flags;
9656 ctxt->flags |= FLAGS_IGNORABLE;
9657 }
9658
9659 list = define->content;
9660 while (list != NULL) {
9661 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9662 if (ret == 1) {
9663 if (ctxt != NULL)
9664 ctxt->flags = oldflags;
9665 return (0);
9666 }
9667 if (ret < 0) {
9668 if (ctxt != NULL)
9669 ctxt->flags = oldflags;
9670 return (ret);
9671 }
9672 list = list->next;
9673 }
9674 ret = 1;
9675 if (ctxt != NULL) {
9676 ctxt->flags = oldflags;
9677 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009678 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009679 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009680
Daniel Veillard4c004142003-10-07 11:33:24 +00009681 if (ctxt != NULL) {
9682 oldflags = ctxt->flags;
9683 ctxt->flags |= FLAGS_IGNORABLE;
9684 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009685
Daniel Veillard4c004142003-10-07 11:33:24 +00009686 list = define->nameClass;
9687 while (list != NULL) {
9688 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9689 if (ret == 1) {
9690 if (ctxt != NULL)
9691 ctxt->flags = oldflags;
9692 return (1);
9693 }
9694 if (ret < 0) {
9695 if (ctxt != NULL)
9696 ctxt->flags = oldflags;
9697 return (ret);
9698 }
9699 list = list->next;
9700 }
9701 if (ctxt != NULL) {
9702 if (ret != 0) {
9703 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9704 xmlRelaxNGDumpValidError(ctxt);
9705 } else {
9706 if (ctxt->errNr > 0)
9707 xmlRelaxNGPopErrors(ctxt, 0);
9708 }
9709 }
9710 ret = 0;
9711 if (ctxt != NULL) {
9712 ctxt->flags = oldflags;
9713 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009714 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009715 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009716 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009717 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009718}
9719
9720/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009721 * xmlRelaxNGBestState:
9722 * @ctxt: a Relax-NG validation context
9723 *
9724 * Find the "best" state in the ctxt->states list of states to report
9725 * errors about. I.e. a state with no element left in the child list
9726 * or the one with the less attributes left.
9727 * This is called only if a falidation error was detected
9728 *
9729 * Returns the index of the "best" state or -1 in case of error
9730 */
9731static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009732xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9733{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009734 xmlRelaxNGValidStatePtr state;
9735 int i, tmp;
9736 int best = -1;
9737 int value = 1000000;
9738
9739 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9740 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009741 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009742
Daniel Veillard4c004142003-10-07 11:33:24 +00009743 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009744 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009745 if (state == NULL)
9746 continue;
9747 if (state->seq != NULL) {
9748 if ((best == -1) || (value > 100000)) {
9749 value = 100000;
9750 best = i;
9751 }
9752 } else {
9753 tmp = state->nbAttrLeft;
9754 if ((best == -1) || (value > tmp)) {
9755 value = tmp;
9756 best = i;
9757 }
9758 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009759 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009760 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009761}
9762
9763/**
9764 * xmlRelaxNGLogBestError:
9765 * @ctxt: a Relax-NG validation context
9766 *
9767 * Find the "best" state in the ctxt->states list of states to report
9768 * errors about and log it.
9769 */
9770static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009771xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9772{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009773 int best;
9774
9775 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9776 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009777 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009778
9779 best = xmlRelaxNGBestState(ctxt);
9780 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009781 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009782
Daniel Veillard4c004142003-10-07 11:33:24 +00009783 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009784 }
9785}
9786
9787/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009788 * xmlRelaxNGValidateElementEnd:
9789 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009790 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009791 *
9792 * Validate the end of the element, implements check that
9793 * there is nothing left not consumed in the element content
9794 * or in the attribute list.
9795 *
9796 * Returns 0 if the validation succeeded or an error code.
9797 */
9798static int
William M. Brack272693c2003-11-14 16:20:34 +00009799xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009800{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009801 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009802 xmlRelaxNGValidStatePtr state;
9803
9804 state = ctxt->state;
9805 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009806 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9807 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009808 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009809 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9810 state->node->name, state->seq->name);
9811 }
9812 return (-1);
9813 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009814 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009815 for (i = 0; i < state->nbAttrs; i++) {
9816 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009817 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009818 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9819 state->attrs[i]->name, state->node->name);
9820 }
9821 return (-1 - i);
9822 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009823 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009824 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009825}
9826
9827/**
9828 * xmlRelaxNGValidateState:
9829 * @ctxt: a Relax-NG validation context
9830 * @define: the definition to verify
9831 *
9832 * Validate the current state against the definition
9833 *
9834 * Returns 0 if the validation succeeded or an error code.
9835 */
9836static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009837xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9838 xmlRelaxNGDefinePtr define)
9839{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009840 xmlNodePtr node;
9841 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009842 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009843
9844 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009845 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9846 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009847 }
9848
9849 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009850 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009851 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009852 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009853 }
9854#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009855 for (i = 0; i < ctxt->depth; i++)
9856 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009857 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009858 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009859 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009860 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009861 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009862 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009863 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009864 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009865#endif
9866 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009867 switch (define->type) {
9868 case XML_RELAXNG_EMPTY:
Philip Withnall57941042014-10-26 18:08:04 +00009869 xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009870 ret = 0;
9871 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009872 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009873 ret = -1;
9874 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009875 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009876 while ((node != NULL) &&
9877 ((node->type == XML_TEXT_NODE) ||
9878 (node->type == XML_COMMENT_NODE) ||
9879 (node->type == XML_PI_NODE) ||
9880 (node->type == XML_CDATA_SECTION_NODE)))
9881 node = node->next;
9882 ctxt->state->seq = node;
9883 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009884 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009885 errNr = ctxt->errNr;
9886 node = xmlRelaxNGSkipIgnored(ctxt, node);
9887 if (node == NULL) {
9888 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9889 ret = -1;
9890 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9891 xmlRelaxNGDumpValidError(ctxt);
9892 break;
9893 }
9894 if (node->type != XML_ELEMENT_NODE) {
9895 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9896 ret = -1;
9897 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9898 xmlRelaxNGDumpValidError(ctxt);
9899 break;
9900 }
9901 /*
9902 * This node was already validated successfully against
9903 * this definition.
9904 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009905 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009906 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9907 if (ctxt->errNr > errNr)
9908 xmlRelaxNGPopErrors(ctxt, errNr);
9909 if (ctxt->errNr != 0) {
9910 while ((ctxt->err != NULL) &&
9911 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9912 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9913 ||
9914 ((ctxt->err->err ==
9915 XML_RELAXNG_ERR_ELEMEXTRANS)
9916 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9917 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9918 || (ctxt->err->err ==
9919 XML_RELAXNG_ERR_NOTELEM)))
9920 xmlRelaxNGValidErrorPop(ctxt);
9921 }
9922 break;
9923 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009924
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009925 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9926 if (ret <= 0) {
9927 ret = -1;
9928 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9929 xmlRelaxNGDumpValidError(ctxt);
9930 break;
9931 }
9932 ret = 0;
9933 if (ctxt->errNr != 0) {
9934 if (ctxt->errNr > errNr)
9935 xmlRelaxNGPopErrors(ctxt, errNr);
9936 while ((ctxt->err != NULL) &&
9937 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9938 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9939 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9940 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9941 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9942 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9943 xmlRelaxNGValidErrorPop(ctxt);
9944 }
9945 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009946
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009947 oldflags = ctxt->flags;
9948 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9949 ctxt->flags -= FLAGS_MIXED_CONTENT;
9950 }
9951 state = xmlRelaxNGNewValidState(ctxt, node);
9952 if (state == NULL) {
9953 ret = -1;
9954 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9955 xmlRelaxNGDumpValidError(ctxt);
9956 break;
9957 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +00009958
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009959 oldstate = ctxt->state;
9960 ctxt->state = state;
9961 if (define->attrs != NULL) {
9962 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9963 if (tmp != 0) {
9964 ret = -1;
9965 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9966 }
9967 }
9968 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009969 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9970 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9971 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009972
Daniel Veillard4c004142003-10-07 11:33:24 +00009973 nstate = xmlRelaxNGNewValidState(ctxt, node);
9974 ctxt->state = nstate;
9975 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009976
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009977 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9978 define->contModel,
9979 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +00009980 nseq = ctxt->state->seq;
9981 ctxt->state = tmpstate;
9982 ctxt->states = tmpstates;
9983 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009984
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009985#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00009986 xmlGenericError(xmlGenericErrorContext,
9987 "Validating content of '%s' : %d\n",
9988 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009989#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +00009990 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009991 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009992
9993 if (ctxt->states != NULL) {
9994 tmp = -1;
9995
Daniel Veillardce192eb2003-04-16 15:58:05 +00009996 for (i = 0; i < ctxt->states->nbState; i++) {
9997 state = ctxt->states->tabState[i];
9998 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009999 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010000
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010001 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +000010002 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010003 break;
10004 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010005 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010006 if (tmp != 0) {
10007 /*
10008 * validation error, log the message for the "best" one
10009 */
10010 ctxt->flags |= FLAGS_IGNORABLE;
10011 xmlRelaxNGLogBestError(ctxt);
10012 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010013 for (i = 0; i < ctxt->states->nbState; i++) {
10014 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010015 ctxt->states->
10016 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010017 }
10018 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10019 ctxt->flags = oldflags;
10020 ctxt->states = NULL;
10021 if ((ret == 0) && (tmp == -1))
10022 ret = -1;
10023 } else {
10024 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +000010025 if (ctxt->state != NULL)
10026 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010027 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010028 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010029 xmlRelaxNGFreeValidState(ctxt, state);
10030 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010031 } else {
10032 if (define->content != NULL) {
10033 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010034 define->
10035 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010036 if (tmp != 0) {
10037 ret = -1;
10038 if (ctxt->state == NULL) {
10039 ctxt->state = oldstate;
10040 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10041 node->name);
10042 ctxt->state = NULL;
10043 } else {
10044 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10045 node->name);
10046 }
10047
10048 }
10049 }
10050 if (ctxt->states != NULL) {
10051 tmp = -1;
10052
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010053 for (i = 0; i < ctxt->states->nbState; i++) {
10054 state = ctxt->states->tabState[i];
10055 ctxt->state = state;
10056
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010057 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010058 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010059 break;
10060 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010061 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010062 if (tmp != 0) {
10063 /*
10064 * validation error, log the message for the "best" one
10065 */
10066 ctxt->flags |= FLAGS_IGNORABLE;
10067 xmlRelaxNGLogBestError(ctxt);
10068 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010069 for (i = 0; i < ctxt->states->nbState; i++) {
10070 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010071 ctxt->states->tabState[i]);
10072 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010073 }
10074 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10075 ctxt->flags = oldflags;
10076 ctxt->states = NULL;
10077 if ((ret == 0) && (tmp == -1))
10078 ret = -1;
10079 } else {
10080 state = ctxt->state;
10081 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010082 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010083 xmlRelaxNGFreeValidState(ctxt, state);
10084 }
10085 }
10086 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +000010087 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010088 }
10089 ctxt->flags = oldflags;
10090 ctxt->state = oldstate;
10091 if (oldstate != NULL)
10092 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10093 if (ret != 0) {
10094 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10095 xmlRelaxNGDumpValidError(ctxt);
10096 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010097#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010098 } else {
10099 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010100#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010101 }
10102 } else {
10103 if (ctxt->errNr > errNr)
10104 xmlRelaxNGPopErrors(ctxt, errNr);
10105 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010106
10107#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010108 xmlGenericError(xmlGenericErrorContext,
10109 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10110 node->name, ret);
10111 if (oldstate == NULL)
10112 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10113 else if (oldstate->seq == NULL)
10114 xmlGenericError(xmlGenericErrorContext, ": done\n");
10115 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10116 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10117 oldstate->seq->name);
10118 else
10119 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10120 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010121#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010122 break;
10123 case XML_RELAXNG_OPTIONAL:{
10124 errNr = ctxt->errNr;
10125 oldflags = ctxt->flags;
10126 ctxt->flags |= FLAGS_IGNORABLE;
10127 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10128 ret =
10129 xmlRelaxNGValidateDefinitionList(ctxt,
10130 define->content);
10131 if (ret != 0) {
10132 if (ctxt->state != NULL)
10133 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10134 ctxt->state = oldstate;
10135 ctxt->flags = oldflags;
10136 ret = 0;
10137 if (ctxt->errNr > errNr)
10138 xmlRelaxNGPopErrors(ctxt, errNr);
10139 break;
10140 }
10141 if (ctxt->states != NULL) {
10142 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10143 } else {
10144 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10145 if (ctxt->states == NULL) {
10146 xmlRelaxNGFreeValidState(ctxt, oldstate);
10147 ctxt->flags = oldflags;
10148 ret = -1;
10149 if (ctxt->errNr > errNr)
10150 xmlRelaxNGPopErrors(ctxt, errNr);
10151 break;
10152 }
10153 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10154 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10155 ctxt->state = NULL;
10156 }
10157 ctxt->flags = oldflags;
10158 ret = 0;
10159 if (ctxt->errNr > errNr)
10160 xmlRelaxNGPopErrors(ctxt, errNr);
10161 break;
10162 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010163 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010164 errNr = ctxt->errNr;
10165 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10166 if (ret != 0) {
10167 break;
10168 }
10169 if (ctxt->errNr > errNr)
10170 xmlRelaxNGPopErrors(ctxt, errNr);
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +020010171 /* Falls through. */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010172 case XML_RELAXNG_ZEROORMORE:{
10173 int progress;
10174 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10175 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010176
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010177 errNr = ctxt->errNr;
10178 res = xmlRelaxNGNewStates(ctxt, 1);
10179 if (res == NULL) {
10180 ret = -1;
10181 break;
10182 }
10183 /*
10184 * All the input states are also exit states
10185 */
10186 if (ctxt->state != NULL) {
10187 xmlRelaxNGAddStates(ctxt, res,
10188 xmlRelaxNGCopyValidState(ctxt,
10189 ctxt->
10190 state));
10191 } else {
10192 for (j = 0; j < ctxt->states->nbState; j++) {
10193 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010194 xmlRelaxNGCopyValidState(ctxt,
10195 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010196 }
10197 }
10198 oldflags = ctxt->flags;
10199 ctxt->flags |= FLAGS_IGNORABLE;
10200 do {
10201 progress = 0;
10202 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010203
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010204 if (ctxt->states != NULL) {
10205 states = ctxt->states;
10206 for (i = 0; i < states->nbState; i++) {
10207 ctxt->state = states->tabState[i];
10208 ctxt->states = NULL;
10209 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10210 define->
10211 content);
10212 if (ret == 0) {
10213 if (ctxt->state != NULL) {
10214 tmp = xmlRelaxNGAddStates(ctxt, res,
10215 ctxt->state);
10216 ctxt->state = NULL;
10217 if (tmp == 1)
10218 progress = 1;
10219 } else if (ctxt->states != NULL) {
10220 for (j = 0; j < ctxt->states->nbState;
10221 j++) {
10222 tmp =
10223 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010224 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010225 if (tmp == 1)
10226 progress = 1;
10227 }
10228 xmlRelaxNGFreeStates(ctxt,
10229 ctxt->states);
10230 ctxt->states = NULL;
10231 }
10232 } else {
10233 if (ctxt->state != NULL) {
10234 xmlRelaxNGFreeValidState(ctxt,
10235 ctxt->state);
10236 ctxt->state = NULL;
10237 }
10238 }
10239 }
10240 } else {
10241 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10242 define->
10243 content);
10244 if (ret != 0) {
10245 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10246 ctxt->state = NULL;
10247 } else {
10248 base = res->nbState;
10249 if (ctxt->state != NULL) {
10250 tmp = xmlRelaxNGAddStates(ctxt, res,
10251 ctxt->state);
10252 ctxt->state = NULL;
10253 if (tmp == 1)
10254 progress = 1;
10255 } else if (ctxt->states != NULL) {
10256 for (j = 0; j < ctxt->states->nbState; j++) {
10257 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010258 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010259 if (tmp == 1)
10260 progress = 1;
10261 }
10262 if (states == NULL) {
10263 states = ctxt->states;
10264 } else {
10265 xmlRelaxNGFreeStates(ctxt,
10266 ctxt->states);
10267 }
10268 ctxt->states = NULL;
10269 }
10270 }
10271 }
10272 if (progress) {
10273 /*
10274 * Collect all the new nodes added at that step
10275 * and make them the new node set
10276 */
10277 if (res->nbState - base == 1) {
10278 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10279 res->
10280 tabState
10281 [base]);
10282 } else {
10283 if (states == NULL) {
10284 xmlRelaxNGNewStates(ctxt,
10285 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010286 states = ctxt->states;
10287 if (states == NULL) {
10288 progress = 0;
10289 break;
10290 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010291 }
10292 states->nbState = 0;
10293 for (i = base; i < res->nbState; i++)
10294 xmlRelaxNGAddStates(ctxt, states,
10295 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010296 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010297 ctxt->states = states;
10298 }
10299 }
10300 } while (progress == 1);
10301 if (states != NULL) {
10302 xmlRelaxNGFreeStates(ctxt, states);
10303 }
10304 ctxt->states = res;
10305 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010306#if 0
10307 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010308 * errors may have to be propagated back...
10309 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010310 if (ctxt->errNr > errNr)
10311 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010312#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010313 ret = 0;
10314 break;
10315 }
10316 case XML_RELAXNG_CHOICE:{
10317 xmlRelaxNGDefinePtr list = NULL;
10318 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010319
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010320 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010321
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010322 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010323 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10324 (node != NULL)) {
10325 /*
10326 * node == NULL can't be optimized since IS_TRIABLE
10327 * doesn't account for choice which may lead to
10328 * only attributes.
10329 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010330 xmlHashTablePtr triage =
10331 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010332
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010333 /*
10334 * Something we can optimize cleanly there is only one
10335 * possble branch out !
10336 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010337 if ((node->type == XML_TEXT_NODE) ||
10338 (node->type == XML_CDATA_SECTION_NODE)) {
10339 list =
10340 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10341 } else if (node->type == XML_ELEMENT_NODE) {
10342 if (node->ns != NULL) {
10343 list = xmlHashLookup2(triage, node->name,
10344 node->ns->href);
10345 if (list == NULL)
10346 list =
10347 xmlHashLookup2(triage, BAD_CAST "#any",
10348 node->ns->href);
10349 } else
10350 list =
10351 xmlHashLookup2(triage, node->name, NULL);
10352 if (list == NULL)
10353 list =
10354 xmlHashLookup2(triage, BAD_CAST "#any",
10355 NULL);
10356 }
10357 if (list == NULL) {
10358 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010359 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010360 break;
10361 }
10362 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10363 if (ret == 0) {
10364 }
10365 break;
10366 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010367
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010368 list = define->content;
10369 oldflags = ctxt->flags;
10370 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010371
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010372 while (list != NULL) {
10373 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10374 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10375 if (ret == 0) {
10376 if (states == NULL) {
10377 states = xmlRelaxNGNewStates(ctxt, 1);
10378 }
10379 if (ctxt->state != NULL) {
10380 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10381 } else if (ctxt->states != NULL) {
10382 for (i = 0; i < ctxt->states->nbState; i++) {
10383 xmlRelaxNGAddStates(ctxt, states,
10384 ctxt->states->
10385 tabState[i]);
10386 }
10387 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10388 ctxt->states = NULL;
10389 }
10390 } else {
10391 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10392 }
10393 ctxt->state = oldstate;
10394 list = list->next;
10395 }
10396 if (states != NULL) {
10397 xmlRelaxNGFreeValidState(ctxt, oldstate);
10398 ctxt->states = states;
10399 ctxt->state = NULL;
10400 ret = 0;
10401 } else {
10402 ctxt->states = NULL;
10403 }
10404 ctxt->flags = oldflags;
10405 if (ret != 0) {
10406 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10407 xmlRelaxNGDumpValidError(ctxt);
10408 }
10409 } else {
10410 if (ctxt->errNr > errNr)
10411 xmlRelaxNGPopErrors(ctxt, errNr);
10412 }
10413 break;
10414 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010415 case XML_RELAXNG_DEF:
10416 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010417 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10418 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010419 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010420 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10421 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010422 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010423 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10424 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010425 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010426 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010427 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010428 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010429 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010430 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10431 break;
10432 case XML_RELAXNG_DATATYPE:{
10433 xmlNodePtr child;
10434 xmlChar *content = NULL;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010435
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010436 child = node;
10437 while (child != NULL) {
10438 if (child->type == XML_ELEMENT_NODE) {
10439 VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10440 node->parent->name);
10441 ret = -1;
10442 break;
10443 } else if ((child->type == XML_TEXT_NODE) ||
10444 (child->type == XML_CDATA_SECTION_NODE)) {
10445 content = xmlStrcat(content, child->content);
10446 }
10447 /* TODO: handle entities ... */
10448 child = child->next;
10449 }
10450 if (ret == -1) {
10451 if (content != NULL)
10452 xmlFree(content);
10453 break;
10454 }
10455 if (content == NULL) {
10456 content = xmlStrdup(BAD_CAST "");
10457 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010458 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010459 ret = -1;
10460 break;
10461 }
10462 }
10463 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10464 ctxt->state->seq);
10465 if (ret == -1) {
10466 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10467 } else if (ret == 0) {
10468 ctxt->state->seq = NULL;
10469 }
10470 if (content != NULL)
10471 xmlFree(content);
10472 break;
10473 }
10474 case XML_RELAXNG_VALUE:{
10475 xmlChar *content = NULL;
10476 xmlChar *oldvalue;
10477 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010478
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010479 child = node;
10480 while (child != NULL) {
10481 if (child->type == XML_ELEMENT_NODE) {
10482 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10483 node->parent->name);
10484 ret = -1;
10485 break;
10486 } else if ((child->type == XML_TEXT_NODE) ||
10487 (child->type == XML_CDATA_SECTION_NODE)) {
10488 content = xmlStrcat(content, child->content);
10489 }
10490 /* TODO: handle entities ... */
10491 child = child->next;
10492 }
10493 if (ret == -1) {
10494 if (content != NULL)
10495 xmlFree(content);
10496 break;
10497 }
10498 if (content == NULL) {
10499 content = xmlStrdup(BAD_CAST "");
10500 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010501 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010502 ret = -1;
10503 break;
10504 }
10505 }
10506 oldvalue = ctxt->state->value;
10507 ctxt->state->value = content;
10508 ret = xmlRelaxNGValidateValue(ctxt, define);
10509 ctxt->state->value = oldvalue;
10510 if (ret == -1) {
10511 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10512 } else if (ret == 0) {
10513 ctxt->state->seq = NULL;
10514 }
10515 if (content != NULL)
10516 xmlFree(content);
10517 break;
10518 }
10519 case XML_RELAXNG_LIST:{
10520 xmlChar *content;
10521 xmlNodePtr child;
10522 xmlChar *oldvalue, *oldendvalue;
10523 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010524
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010525 /*
10526 * Make sure it's only text nodes
10527 */
10528
10529 content = NULL;
10530 child = node;
10531 while (child != NULL) {
10532 if (child->type == XML_ELEMENT_NODE) {
10533 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10534 node->parent->name);
10535 ret = -1;
10536 break;
10537 } else if ((child->type == XML_TEXT_NODE) ||
10538 (child->type == XML_CDATA_SECTION_NODE)) {
10539 content = xmlStrcat(content, child->content);
10540 }
10541 /* TODO: handle entities ... */
10542 child = child->next;
10543 }
10544 if (ret == -1) {
10545 if (content != NULL)
10546 xmlFree(content);
10547 break;
10548 }
10549 if (content == NULL) {
10550 content = xmlStrdup(BAD_CAST "");
10551 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010552 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010553 ret = -1;
10554 break;
10555 }
10556 }
10557 len = xmlStrlen(content);
10558 oldvalue = ctxt->state->value;
10559 oldendvalue = ctxt->state->endvalue;
10560 ctxt->state->value = content;
10561 ctxt->state->endvalue = content + len;
10562 ret = xmlRelaxNGValidateValue(ctxt, define);
10563 ctxt->state->value = oldvalue;
10564 ctxt->state->endvalue = oldendvalue;
10565 if (ret == -1) {
10566 VALID_ERR(XML_RELAXNG_ERR_LIST);
10567 } else if ((ret == 0) && (node != NULL)) {
10568 ctxt->state->seq = node->next;
10569 }
10570 if (content != NULL)
10571 xmlFree(content);
10572 break;
10573 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010574 case XML_RELAXNG_EXCEPT:
10575 case XML_RELAXNG_PARAM:
10576 TODO ret = -1;
10577 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010578 }
10579 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010580#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010581 for (i = 0; i < ctxt->depth; i++)
10582 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010583 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010584 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010585 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010586 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010587 if (ret == 0)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010588 xmlGenericError(xmlGenericErrorContext, "suceeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010589 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010590 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010591#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010592 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010593}
10594
10595/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010596 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010597 * @ctxt: a Relax-NG validation context
10598 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010599 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010600 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010601 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010602 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010603 */
10604static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010605xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10606 xmlRelaxNGDefinePtr define)
10607{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010608 xmlRelaxNGStatesPtr states, res;
10609 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010610
Daniel Veillardfd573f12003-03-16 17:52:32 +000010611 /*
10612 * We should NOT have both ctxt->state and ctxt->states
10613 */
10614 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010615 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10616 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010617 }
10618
10619 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010620 if (ctxt->states != NULL) {
10621 ctxt->state = ctxt->states->tabState[0];
10622 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10623 ctxt->states = NULL;
10624 }
10625 ret = xmlRelaxNGValidateState(ctxt, define);
10626 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10627 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10628 ctxt->state = NULL;
10629 }
10630 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10631 ctxt->state = ctxt->states->tabState[0];
10632 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10633 ctxt->states = NULL;
10634 }
10635 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010636 }
10637
10638 states = ctxt->states;
10639 ctxt->states = NULL;
10640 res = NULL;
10641 j = 0;
10642 oldflags = ctxt->flags;
10643 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010644 for (i = 0; i < states->nbState; i++) {
10645 ctxt->state = states->tabState[i];
10646 ctxt->states = NULL;
10647 ret = xmlRelaxNGValidateState(ctxt, define);
10648 /*
10649 * We should NOT have both ctxt->state and ctxt->states
10650 */
10651 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10652 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10653 ctxt->state = NULL;
10654 }
10655 if (ret == 0) {
10656 if (ctxt->states == NULL) {
10657 if (res != NULL) {
10658 /* add the state to the container */
10659 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10660 ctxt->state = NULL;
10661 } else {
10662 /* add the state directly in states */
10663 states->tabState[j++] = ctxt->state;
10664 ctxt->state = NULL;
10665 }
10666 } else {
10667 if (res == NULL) {
10668 /* make it the new container and copy other results */
10669 res = ctxt->states;
10670 ctxt->states = NULL;
10671 for (k = 0; k < j; k++)
10672 xmlRelaxNGAddStates(ctxt, res,
10673 states->tabState[k]);
10674 } else {
10675 /* add all the new results to res and reff the container */
10676 for (k = 0; k < ctxt->states->nbState; k++)
10677 xmlRelaxNGAddStates(ctxt, res,
10678 ctxt->states->tabState[k]);
10679 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10680 ctxt->states = NULL;
10681 }
10682 }
10683 } else {
10684 if (ctxt->state != NULL) {
10685 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10686 ctxt->state = NULL;
10687 } else if (ctxt->states != NULL) {
10688 for (k = 0; k < ctxt->states->nbState; k++)
10689 xmlRelaxNGFreeValidState(ctxt,
10690 ctxt->states->tabState[k]);
10691 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10692 ctxt->states = NULL;
10693 }
10694 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010695 }
10696 ctxt->flags = oldflags;
10697 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010698 xmlRelaxNGFreeStates(ctxt, states);
10699 ctxt->states = res;
10700 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010701 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010702 states->nbState = j;
10703 ctxt->states = states;
10704 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010705 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010706 ctxt->state = states->tabState[0];
10707 xmlRelaxNGFreeStates(ctxt, states);
10708 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010709 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010710 ret = -1;
10711 xmlRelaxNGFreeStates(ctxt, states);
10712 if (ctxt->states != NULL) {
10713 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10714 ctxt->states = NULL;
10715 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010716 }
10717 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010718 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10719 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010720 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010721 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010722}
10723
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010724/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010725 * xmlRelaxNGValidateDocument:
10726 * @ctxt: a Relax-NG validation context
10727 * @doc: the document
10728 *
10729 * Validate the given document
10730 *
10731 * Returns 0 if the validation succeeded or an error code.
10732 */
10733static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010734xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10735{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010736 int ret;
10737 xmlRelaxNGPtr schema;
10738 xmlRelaxNGGrammarPtr grammar;
10739 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010740 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010741
10742 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010743 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010744
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010745 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010746 schema = ctxt->schema;
10747 grammar = schema->topgrammar;
10748 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010749 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10750 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010751 }
10752 state = xmlRelaxNGNewValidState(ctxt, NULL);
10753 ctxt->state = state;
10754 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010755 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010756 state = ctxt->state;
10757 node = state->seq;
10758 node = xmlRelaxNGSkipIgnored(ctxt, node);
10759 if (node != NULL) {
10760 if (ret != -1) {
10761 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10762 ret = -1;
10763 }
10764 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010765 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010766 int i;
10767 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010768
Daniel Veillard4c004142003-10-07 11:33:24 +000010769 for (i = 0; i < ctxt->states->nbState; i++) {
10770 state = ctxt->states->tabState[i];
10771 node = state->seq;
10772 node = xmlRelaxNGSkipIgnored(ctxt, node);
10773 if (node == NULL)
10774 tmp = 0;
10775 xmlRelaxNGFreeValidState(ctxt, state);
10776 }
10777 if (tmp == -1) {
10778 if (ret != -1) {
10779 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10780 ret = -1;
10781 }
10782 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010783 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010784 if (ctxt->state != NULL) {
10785 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010786 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010787 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010788 if (ret != 0)
10789 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010790#ifdef DEBUG
10791 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010792 ctxt->error(ctxt->userData,
10793 "%d Extra error messages left on stack !\n",
10794 ctxt->errNr);
10795 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010796 }
10797#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010798#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010799 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010800 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010801
Daniel Veillard4c004142003-10-07 11:33:24 +000010802 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10803 vctxt.valid = 1;
10804 vctxt.error = ctxt->error;
10805 vctxt.warning = ctxt->warning;
10806 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010807
Daniel Veillard4c004142003-10-07 11:33:24 +000010808 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10809 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010810 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010811#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010812 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010813 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010814
Daniel Veillard4c004142003-10-07 11:33:24 +000010815 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010816}
10817
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010818/**
10819 * xmlRelaxNGCleanPSVI:
10820 * @node: an input element or document
10821 *
10822 * Call this routine to speed up XPath computation on static documents.
10823 * This stamps all the element nodes with the document order
10824 * Like for line information, the order is kept in the element->content
10825 * field, the value stored is actually - the node number (starting at -1)
10826 * to be able to differentiate from line numbers.
10827 *
10828 * Returns the number of elements found in the document or -1 in case
10829 * of error.
10830 */
10831static void
10832xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10833 xmlNodePtr cur;
10834
10835 if ((node == NULL) ||
10836 ((node->type != XML_ELEMENT_NODE) &&
10837 (node->type != XML_DOCUMENT_NODE) &&
10838 (node->type != XML_HTML_DOCUMENT_NODE)))
10839 return;
10840 if (node->type == XML_ELEMENT_NODE)
10841 node->psvi = NULL;
10842
10843 cur = node->children;
10844 while (cur != NULL) {
10845 if (cur->type == XML_ELEMENT_NODE) {
10846 cur->psvi = NULL;
10847 if (cur->children != NULL) {
10848 cur = cur->children;
10849 continue;
10850 }
10851 }
10852 if (cur->next != NULL) {
10853 cur = cur->next;
10854 continue;
10855 }
10856 do {
10857 cur = cur->parent;
10858 if (cur == NULL)
10859 break;
10860 if (cur == node) {
10861 cur = NULL;
10862 break;
10863 }
10864 if (cur->next != NULL) {
10865 cur = cur->next;
10866 break;
10867 }
10868 } while (cur != NULL);
10869 }
10870 return;
10871}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010872/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080010873 * *
10874 * Validation interfaces *
10875 * *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010876 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010877
Daniel Veillard6eadf632003-01-23 18:29:16 +000010878/**
10879 * xmlRelaxNGNewValidCtxt:
10880 * @schema: a precompiled XML RelaxNGs
10881 *
10882 * Create an XML RelaxNGs validation context based on the given schema
10883 *
10884 * Returns the validation context or NULL in case of error
10885 */
10886xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010887xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10888{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010889 xmlRelaxNGValidCtxtPtr ret;
10890
10891 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10892 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010893 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010894 return (NULL);
10895 }
10896 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10897 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010898 ret->error = xmlGenericError;
10899 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010900 ret->errNr = 0;
10901 ret->errMax = 0;
10902 ret->err = NULL;
10903 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010904 if (schema != NULL)
10905 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010906 ret->states = NULL;
10907 ret->freeState = NULL;
10908 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010909 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010910 return (ret);
10911}
10912
10913/**
10914 * xmlRelaxNGFreeValidCtxt:
10915 * @ctxt: the schema validation context
10916 *
10917 * Free the resources associated to the schema validation context
10918 */
10919void
Daniel Veillard4c004142003-10-07 11:33:24 +000010920xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10921{
Daniel Veillard798024a2003-03-19 10:36:09 +000010922 int k;
10923
Daniel Veillard6eadf632003-01-23 18:29:16 +000010924 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010925 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010926 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010927 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010928 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010929 for (k = 0; k < ctxt->freeState->nbState; k++) {
10930 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10931 }
10932 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010933 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010934 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010935 for (k = 0; k < ctxt->freeStatesNr; k++) {
10936 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10937 }
10938 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010939 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010940 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010941 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010942 if (ctxt->elemTab != NULL) {
10943 xmlRegExecCtxtPtr exec;
10944
Daniel Veillard4c004142003-10-07 11:33:24 +000010945 exec = xmlRelaxNGElemPop(ctxt);
10946 while (exec != NULL) {
10947 xmlRegFreeExecCtxt(exec);
10948 exec = xmlRelaxNGElemPop(ctxt);
10949 }
10950 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010951 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010952 xmlFree(ctxt);
10953}
10954
10955/**
10956 * xmlRelaxNGSetValidErrors:
10957 * @ctxt: a Relax-NG validation context
10958 * @err: the error function
10959 * @warn: the warning function
10960 * @ctx: the functions context
10961 *
10962 * Set the error and warning callback informations
10963 */
10964void
10965xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010966 xmlRelaxNGValidityErrorFunc err,
10967 xmlRelaxNGValidityWarningFunc warn, void *ctx)
10968{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010969 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010970 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010971 ctxt->error = err;
10972 ctxt->warning = warn;
10973 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010974 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010975}
10976
10977/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010978 * xmlRelaxNGSetValidStructuredErrors:
10979 * @ctxt: a Relax-NG validation context
10980 * @serror: the structured error function
10981 * @ctx: the functions context
10982 *
10983 * Set the structured error callback
10984 */
10985void
10986xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000010987 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010988{
10989 if (ctxt == NULL)
10990 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010991 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000010992 ctxt->error = NULL;
10993 ctxt->warning = NULL;
10994 ctxt->userData = ctx;
10995}
10996
10997/**
Daniel Veillard409a8142003-07-18 15:16:57 +000010998 * xmlRelaxNGGetValidErrors:
10999 * @ctxt: a Relax-NG validation context
11000 * @err: the error function result
11001 * @warn: the warning function result
11002 * @ctx: the functions context result
11003 *
11004 * Get the error and warning callback informations
11005 *
11006 * Returns -1 in case of error and 0 otherwise
11007 */
11008int
11009xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000011010 xmlRelaxNGValidityErrorFunc * err,
11011 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
11012{
Daniel Veillard409a8142003-07-18 15:16:57 +000011013 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000011014 return (-1);
11015 if (err != NULL)
11016 *err = ctxt->error;
11017 if (warn != NULL)
11018 *warn = ctxt->warning;
11019 if (ctx != NULL)
11020 *ctx = ctxt->userData;
11021 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000011022}
11023
11024/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000011025 * xmlRelaxNGValidateDoc:
11026 * @ctxt: a Relax-NG validation context
11027 * @doc: a parsed document tree
11028 *
11029 * Validate a document tree in memory.
11030 *
11031 * Returns 0 if the document is valid, a positive error code
11032 * number otherwise and -1 in case of internal or API error.
11033 */
11034int
Daniel Veillard4c004142003-10-07 11:33:24 +000011035xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
11036{
Daniel Veillard6eadf632003-01-23 18:29:16 +000011037 int ret;
11038
11039 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000011040 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011041
11042 ctxt->doc = doc;
11043
11044 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000011045 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020011046 * Remove all left PSVI
11047 */
11048 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
11049
11050 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000011051 * TODO: build error codes
11052 */
11053 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000011054 return (1);
11055 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011056}
11057
Daniel Veillard5d4644e2005-04-01 13:11:58 +000011058#define bottom_relaxng
11059#include "elfgcchack.h"
Daniel Veillard6eadf632003-01-23 18:29:16 +000011060#endif /* LIBXML_SCHEMAS_ENABLED */