blob: 312a2c860f90d4d3696335de132b478c436bba10 [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 */
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700128 XML_RELAXNG_DATATYPE, /* external data type definition */
129 XML_RELAXNG_PARAM, /* external data type parameter */
130 XML_RELAXNG_VALUE, /* value from an external data type definition */
Daniel Veillard4c004142003-10-07 11:33:24 +0000131 XML_RELAXNG_LIST, /* a list of patterns */
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700132 XML_RELAXNG_ATTRIBUTE, /* an attribute following a pattern */
Daniel Veillard4c004142003-10-07 11:33:24 +0000133 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 */
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700231 int defMax; /* number of defines allocated */
Daniel Veillard4c004142003-10-07 11:33:24 +0000232 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
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700433 * @extra: extra information
Daniel Veillard4c004142003-10-07 11:33:24 +0000434 *
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
Haibo Huangcfd91dc2020-07-30 23:01:33 -0700468 * @extra: extra information
Daniel Veillard4c004142003-10-07 11:33:24 +0000469 *
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
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001131 * @states: the container
Daniel Veillardfd573f12003-03-16 17:52:32 +00001132 *
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 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001343 * Returns 1 if equal, 0 otherwise
Daniel Veillardfd573f12003-03-16 17:52:32 +00001344 */
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 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07001412 * Semi private function used to pass information to a parser context
Daniel Veillard03c2f0a2004-01-25 19:54:59 +00001413 * 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 }
Elliott Hughes7fbecab2019-01-10 16:42:03 -08001576 if (xmlRelaxNGRemoveRedefine(ctxt, URL, tmp->children, name) == 1) {
1577 found = 1;
1578 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001579 }
1580 tmp = tmp2;
Daniel Veillard5add8682003-03-10 13:13:58 +00001581 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001582 return (found);
Daniel Veillard5add8682003-03-10 13:13:58 +00001583}
1584
1585/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001586 * xmlRelaxNGLoadInclude:
1587 * @ctxt: the parser context
1588 * @URL: the normalized URL
1589 * @node: the include node.
Daniel Veillard416589a2003-02-17 17:25:42 +00001590 * @ns: the namespace passed from the context.
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001591 *
1592 * First lookup if the document is already loaded into the parser context,
1593 * check against recursion. If not found the resource is loaded and
1594 * the content is preprocessed before being returned back to the caller.
1595 *
1596 * Returns the xmlRelaxNGIncludePtr or NULL in case of error
1597 */
1598static xmlRelaxNGIncludePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001599xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1600 xmlNodePtr node, const xmlChar * ns)
1601{
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001602 xmlRelaxNGIncludePtr ret = NULL;
1603 xmlDocPtr doc;
1604 int i;
Daniel Veillard5add8682003-03-10 13:13:58 +00001605 xmlNodePtr root, cur;
1606
1607#ifdef DEBUG_INCLUDE
1608 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001609 "xmlRelaxNGLoadInclude(%s)\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001610#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001611
1612 /*
1613 * check against recursion in the stack
1614 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001615 for (i = 0; i < ctxt->incNr; i++) {
1616 if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1617 xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1618 "Detected an Include recursion for %s\n", URL,
1619 NULL);
1620 return (NULL);
1621 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001622 }
1623
1624 /*
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001625 * load the document
1626 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001627 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001628 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001629 xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1630 "xmlRelaxNG: could not load %s\n", URL, NULL);
1631 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001632 }
Daniel Veillard5add8682003-03-10 13:13:58 +00001633#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001634 xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001635#endif
1636
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001637 /*
1638 * Allocate the document structures and register it first.
1639 */
1640 ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1641 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001642 xmlRngPErrMemory(ctxt, "allocating include\n");
1643 xmlFreeDoc(doc);
1644 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001645 }
1646 memset(ret, 0, sizeof(xmlRelaxNGInclude));
1647 ret->doc = doc;
1648 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001649 ret->next = ctxt->includes;
1650 ctxt->includes = ret;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001651
1652 /*
Daniel Veillard416589a2003-02-17 17:25:42 +00001653 * transmit the ns if needed
1654 */
1655 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001656 root = xmlDocGetRootElement(doc);
1657 if (root != NULL) {
1658 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1659 xmlSetProp(root, BAD_CAST "ns", ns);
1660 }
1661 }
Daniel Veillard416589a2003-02-17 17:25:42 +00001662 }
1663
1664 /*
Daniel Veillardc482e262003-02-26 14:48:48 +00001665 * push it on the stack
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001666 */
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001667 xmlRelaxNGIncludePush(ctxt, ret);
1668
1669 /*
1670 * Some preprocessing of the document content, this include recursing
1671 * in the include stack.
1672 */
Daniel Veillard5add8682003-03-10 13:13:58 +00001673#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001674 xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001675#endif
1676
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001677 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1678 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001679 ctxt->inc = NULL;
1680 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001681 }
1682
1683 /*
1684 * Pop up the include from the stack
1685 */
1686 xmlRelaxNGIncludePop(ctxt);
1687
Daniel Veillard5add8682003-03-10 13:13:58 +00001688#ifdef DEBUG_INCLUDE
Daniel Veillard4c004142003-10-07 11:33:24 +00001689 xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
Daniel Veillard5add8682003-03-10 13:13:58 +00001690#endif
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001691 /*
1692 * Check that the top element is a grammar
1693 */
1694 root = xmlDocGetRootElement(doc);
1695 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001696 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1697 "xmlRelaxNG: included document is empty %s\n", URL,
1698 NULL);
1699 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001700 }
1701 if (!IS_RELAXNG(root, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001702 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1703 "xmlRelaxNG: included document %s root is not a grammar\n",
1704 URL, NULL);
1705 return (NULL);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001706 }
1707
1708 /*
1709 * Elimination of redefined rules in the include.
1710 */
1711 cur = node->children;
1712 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001713 if (IS_RELAXNG(cur, "start")) {
1714 int found = 0;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001715
Daniel Veillard4c004142003-10-07 11:33:24 +00001716 found =
1717 xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1718 if (!found) {
1719 xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1720 "xmlRelaxNG: include %s has a start but not the included grammar\n",
1721 URL, NULL);
1722 }
1723 } else if (IS_RELAXNG(cur, "define")) {
1724 xmlChar *name;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001725
Daniel Veillard4c004142003-10-07 11:33:24 +00001726 name = xmlGetProp(cur, BAD_CAST "name");
1727 if (name == NULL) {
1728 xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1729 "xmlRelaxNG: include %s has define without name\n",
1730 URL, NULL);
1731 } else {
1732 int found;
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001733
Daniel Veillard4c004142003-10-07 11:33:24 +00001734 xmlRelaxNGNormExtSpace(name);
1735 found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1736 root->children, name);
1737 if (!found) {
1738 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1739 "xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1740 URL, name);
1741 }
1742 xmlFree(name);
1743 }
1744 }
Elliott Hughes7fbecab2019-01-10 16:42:03 -08001745 if (IS_RELAXNG(cur, "div") && cur->children != NULL) {
1746 cur = cur->children;
1747 } else {
1748 if (cur->next != NULL) {
1749 cur = cur->next;
1750 } else {
1751 while (cur->parent != node && cur->parent->next == NULL) {
1752 cur = cur->parent;
1753 }
1754 cur = cur->parent != node ? cur->parent->next : NULL;
1755 }
1756 }
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001757 }
1758
1759
Daniel Veillard4c004142003-10-07 11:33:24 +00001760 return (ret);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001761}
1762
1763/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00001764 * xmlRelaxNGValidErrorPush:
1765 * @ctxt: the validation context
1766 * @err: the error code
1767 * @arg1: the first string argument
1768 * @arg2: the second string argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001769 * @dup: arg need to be duplicated
Daniel Veillard42f12e92003-03-07 18:32:59 +00001770 *
1771 * Pushes a new error on top of the error stack
1772 *
1773 * Returns 0 in case of error, the index in the stack otherwise
1774 */
1775static int
Daniel Veillard4c004142003-10-07 11:33:24 +00001776xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1777 xmlRelaxNGValidErr err, const xmlChar * arg1,
1778 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00001779{
1780 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00001781
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001782#ifdef DEBUG_ERROR
1783 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00001784 "Pushing error %d at %d on stack\n", err, ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00001785#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00001786 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001787 ctxt->errMax = 8;
1788 ctxt->errNr = 0;
1789 ctxt->errTab =
1790 (xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1791 sizeof
1792 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001793 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001794 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001795 return (0);
1796 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001797 ctxt->err = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001798 }
1799 if (ctxt->errNr >= ctxt->errMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001800 ctxt->errMax *= 2;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001801 ctxt->errTab =
1802 (xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001803 ctxt->errMax *
1804 sizeof
1805 (xmlRelaxNGValidError));
Daniel Veillard42f12e92003-03-07 18:32:59 +00001806 if (ctxt->errTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001807 xmlRngVErrMemory(ctxt, "pushing error\n");
Daniel Veillard42f12e92003-03-07 18:32:59 +00001808 return (0);
1809 }
Daniel Veillard4c004142003-10-07 11:33:24 +00001810 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
Daniel Veillard42f12e92003-03-07 18:32:59 +00001811 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00001812 if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00001813 (ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1814 return (ctxt->errNr);
Daniel Veillard42f12e92003-03-07 18:32:59 +00001815 cur = &ctxt->errTab[ctxt->errNr];
1816 cur->err = err;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001817 if (dup) {
1818 cur->arg1 = xmlStrdup(arg1);
1819 cur->arg2 = xmlStrdup(arg2);
Daniel Veillard4c004142003-10-07 11:33:24 +00001820 cur->flags = ERROR_IS_DUP;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001821 } else {
1822 cur->arg1 = arg1;
1823 cur->arg2 = arg2;
Daniel Veillard4c004142003-10-07 11:33:24 +00001824 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001825 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001826 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001827 cur->node = ctxt->state->node;
1828 cur->seq = ctxt->state->seq;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001829 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00001830 cur->node = NULL;
1831 cur->seq = NULL;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001832 }
1833 ctxt->err = cur;
1834 return (ctxt->errNr++);
1835}
1836
1837/**
1838 * xmlRelaxNGValidErrorPop:
1839 * @ctxt: the validation context
1840 *
1841 * Pops the top error from the error stack
Daniel Veillard42f12e92003-03-07 18:32:59 +00001842 */
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001843static void
Daniel Veillard42f12e92003-03-07 18:32:59 +00001844xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1845{
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001846 xmlRelaxNGValidErrorPtr cur;
Daniel Veillard42f12e92003-03-07 18:32:59 +00001847
Daniel Veillard580ced82003-03-21 21:22:48 +00001848 if (ctxt->errNr <= 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001849 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001850 return;
Daniel Veillard580ced82003-03-21 21:22:48 +00001851 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001852 ctxt->errNr--;
1853 if (ctxt->errNr > 0)
1854 ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1855 else
1856 ctxt->err = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001857 cur = &ctxt->errTab[ctxt->errNr];
1858 if (cur->flags & ERROR_IS_DUP) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001859 if (cur->arg1 != NULL)
1860 xmlFree((xmlChar *) cur->arg1);
1861 cur->arg1 = NULL;
1862 if (cur->arg2 != NULL)
1863 xmlFree((xmlChar *) cur->arg2);
1864 cur->arg2 = NULL;
1865 cur->flags = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00001866 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00001867}
1868
Daniel Veillard42f12e92003-03-07 18:32:59 +00001869/**
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001870 * xmlRelaxNGDocumentPush:
1871 * @ctxt: the parser context
1872 * @value: the element doc
1873 *
1874 * Pushes a new doc on top of the doc stack
1875 *
1876 * Returns 0 in case of error, the index in the stack otherwise
1877 */
1878static int
1879xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00001880 xmlRelaxNGDocumentPtr value)
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001881{
1882 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001883 ctxt->docMax = 4;
1884 ctxt->docNr = 0;
1885 ctxt->docTab =
1886 (xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1887 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001888 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001889 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001890 return (0);
1891 }
1892 }
1893 if (ctxt->docNr >= ctxt->docMax) {
1894 ctxt->docMax *= 2;
1895 ctxt->docTab =
1896 (xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00001897 ctxt->docMax *
1898 sizeof(ctxt->docTab[0]));
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001899 if (ctxt->docTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001900 xmlRngPErrMemory(ctxt, "adding document\n");
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001901 return (0);
1902 }
1903 }
1904 ctxt->docTab[ctxt->docNr] = value;
1905 ctxt->doc = value;
1906 return (ctxt->docNr++);
1907}
1908
1909/**
1910 * xmlRelaxNGDocumentPop:
1911 * @ctxt: the parser context
1912 *
1913 * Pops the top doc from the doc stack
1914 *
1915 * Returns the doc just removed
1916 */
1917static xmlRelaxNGDocumentPtr
1918xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1919{
1920 xmlRelaxNGDocumentPtr ret;
1921
1922 if (ctxt->docNr <= 0)
Daniel Veillard24505b02005-07-28 23:49:35 +00001923 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001924 ctxt->docNr--;
1925 if (ctxt->docNr > 0)
1926 ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1927 else
1928 ctxt->doc = NULL;
1929 ret = ctxt->docTab[ctxt->docNr];
Daniel Veillard24505b02005-07-28 23:49:35 +00001930 ctxt->docTab[ctxt->docNr] = NULL;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001931 return (ret);
1932}
1933
1934/**
Daniel Veillarda9d912d2003-02-01 17:43:10 +00001935 * xmlRelaxNGLoadExternalRef:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001936 * @ctxt: the parser context
1937 * @URL: the normalized URL
1938 * @ns: the inherited ns if any
1939 *
1940 * First lookup if the document is already loaded into the parser context,
1941 * check against recursion. If not found the resource is loaded and
1942 * the content is preprocessed before being returned back to the caller.
1943 *
1944 * Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1945 */
1946static xmlRelaxNGDocumentPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00001947xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1948 const xmlChar * URL, const xmlChar * ns)
1949{
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001950 xmlRelaxNGDocumentPtr ret = NULL;
1951 xmlDocPtr doc;
1952 xmlNodePtr root;
1953 int i;
1954
1955 /*
1956 * check against recursion in the stack
1957 */
Daniel Veillard4c004142003-10-07 11:33:24 +00001958 for (i = 0; i < ctxt->docNr; i++) {
1959 if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1960 xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1961 "Detected an externalRef recursion for %s\n", URL,
1962 NULL);
1963 return (NULL);
1964 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001965 }
1966
1967 /*
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001968 * load the document
1969 */
Daniel Veillard87247e82004-01-13 20:42:02 +00001970 doc = xmlReadFile((const char *) URL,NULL,0);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001971 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001972 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1973 "xmlRelaxNG: could not load %s\n", URL, NULL);
1974 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001975 }
1976
1977 /*
1978 * Allocate the document structures and register it first.
1979 */
1980 ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1981 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001982 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1983 "xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1984 xmlFreeDoc(doc);
1985 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001986 }
1987 memset(ret, 0, sizeof(xmlRelaxNGDocument));
1988 ret->doc = doc;
1989 ret->href = xmlStrdup(URL);
Daniel Veillardc482e262003-02-26 14:48:48 +00001990 ret->next = ctxt->documents;
Daniel Veillard81c51e12009-08-14 18:52:10 +02001991 ret->externalRef = 1;
Daniel Veillardc482e262003-02-26 14:48:48 +00001992 ctxt->documents = ret;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00001993
1994 /*
1995 * transmit the ns if needed
1996 */
1997 if (ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00001998 root = xmlDocGetRootElement(doc);
1999 if (root != NULL) {
2000 if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
2001 xmlSetProp(root, BAD_CAST "ns", ns);
2002 }
2003 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002004 }
2005
2006 /*
2007 * push it on the stack and register it in the hash table
2008 */
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002009 xmlRelaxNGDocumentPush(ctxt, ret);
2010
2011 /*
2012 * Some preprocessing of the document content
2013 */
2014 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2015 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002016 ctxt->doc = NULL;
2017 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002018 }
2019
2020 xmlRelaxNGDocumentPop(ctxt);
2021
Daniel Veillard4c004142003-10-07 11:33:24 +00002022 return (ret);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00002023}
2024
2025/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002026 * *
2027 * Error functions *
2028 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002029 ************************************************************************/
2030
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002031#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2032#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2033#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2034#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2035#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002036
Daniel Veillard231d7912003-02-09 14:22:17 +00002037static const char *
Daniel Veillard4c004142003-10-07 11:33:24 +00002038xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2039{
Daniel Veillard231d7912003-02-09 14:22:17 +00002040 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002041 return ("none");
2042 switch (def->type) {
2043 case XML_RELAXNG_EMPTY:
2044 return ("empty");
2045 case XML_RELAXNG_NOT_ALLOWED:
2046 return ("notAllowed");
2047 case XML_RELAXNG_EXCEPT:
2048 return ("except");
2049 case XML_RELAXNG_TEXT:
2050 return ("text");
2051 case XML_RELAXNG_ELEMENT:
2052 return ("element");
2053 case XML_RELAXNG_DATATYPE:
2054 return ("datatype");
2055 case XML_RELAXNG_VALUE:
2056 return ("value");
2057 case XML_RELAXNG_LIST:
2058 return ("list");
2059 case XML_RELAXNG_ATTRIBUTE:
2060 return ("attribute");
2061 case XML_RELAXNG_DEF:
2062 return ("def");
2063 case XML_RELAXNG_REF:
2064 return ("ref");
2065 case XML_RELAXNG_EXTERNALREF:
2066 return ("externalRef");
2067 case XML_RELAXNG_PARENTREF:
2068 return ("parentRef");
2069 case XML_RELAXNG_OPTIONAL:
2070 return ("optional");
2071 case XML_RELAXNG_ZEROORMORE:
2072 return ("zeroOrMore");
2073 case XML_RELAXNG_ONEORMORE:
2074 return ("oneOrMore");
2075 case XML_RELAXNG_CHOICE:
2076 return ("choice");
2077 case XML_RELAXNG_GROUP:
2078 return ("group");
2079 case XML_RELAXNG_INTERLEAVE:
2080 return ("interleave");
2081 case XML_RELAXNG_START:
2082 return ("start");
2083 case XML_RELAXNG_NOOP:
2084 return ("noop");
2085 case XML_RELAXNG_PARAM:
2086 return ("param");
Daniel Veillard231d7912003-02-09 14:22:17 +00002087 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002088 return ("unknown");
Daniel Veillard231d7912003-02-09 14:22:17 +00002089}
Daniel Veillardd2298792003-02-14 16:54:11 +00002090
Daniel Veillard6eadf632003-01-23 18:29:16 +00002091/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002092 * xmlRelaxNGGetErrorString:
2093 * @err: the error code
2094 * @arg1: the first string argument
2095 * @arg2: the second string argument
Daniel Veillard6eadf632003-01-23 18:29:16 +00002096 *
Daniel Veillard42f12e92003-03-07 18:32:59 +00002097 * computes a formatted error string for the given error code and args
2098 *
2099 * Returns the error string, it must be deallocated by the caller
2100 */
2101static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00002102xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2103 const xmlChar * arg2)
2104{
Daniel Veillard42f12e92003-03-07 18:32:59 +00002105 char msg[1000];
Chun-wei Fand77e5fc2016-05-31 21:04:50 +08002106 xmlChar *result;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002107
2108 if (arg1 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002109 arg1 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002110 if (arg2 == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002111 arg2 = BAD_CAST "";
Daniel Veillard42f12e92003-03-07 18:32:59 +00002112
2113 msg[0] = 0;
2114 switch (err) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002115 case XML_RELAXNG_OK:
2116 return (NULL);
2117 case XML_RELAXNG_ERR_MEMORY:
2118 return (xmlCharStrdup("out of memory\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002119 case XML_RELAXNG_ERR_TYPE:
Daniel Veillard4c004142003-10-07 11:33:24 +00002120 snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2121 break;
2122 case XML_RELAXNG_ERR_TYPEVAL:
2123 snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2124 arg2);
2125 break;
2126 case XML_RELAXNG_ERR_DUPID:
2127 snprintf(msg, 1000, "ID %s redefined\n", arg1);
2128 break;
2129 case XML_RELAXNG_ERR_TYPECMP:
2130 snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2131 break;
2132 case XML_RELAXNG_ERR_NOSTATE:
2133 return (xmlCharStrdup("Internal error: no state\n"));
2134 case XML_RELAXNG_ERR_NODEFINE:
2135 return (xmlCharStrdup("Internal error: no define\n"));
2136 case XML_RELAXNG_ERR_INTERNAL:
2137 snprintf(msg, 1000, "Internal error: %s\n", arg1);
2138 break;
2139 case XML_RELAXNG_ERR_LISTEXTRA:
2140 snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2141 break;
2142 case XML_RELAXNG_ERR_INTERNODATA:
2143 return (xmlCharStrdup
2144 ("Internal: interleave block has no data\n"));
2145 case XML_RELAXNG_ERR_INTERSEQ:
2146 return (xmlCharStrdup("Invalid sequence in interleave\n"));
2147 case XML_RELAXNG_ERR_INTEREXTRA:
2148 snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2149 break;
2150 case XML_RELAXNG_ERR_ELEMNAME:
2151 snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2152 arg2);
2153 break;
2154 case XML_RELAXNG_ERR_ELEMNONS:
2155 snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2156 arg1);
2157 break;
2158 case XML_RELAXNG_ERR_ELEMWRONGNS:
2159 snprintf(msg, 1000,
2160 "Element %s has wrong namespace: expecting %s\n", arg1,
2161 arg2);
2162 break;
2163 case XML_RELAXNG_ERR_ELEMWRONG:
2164 snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2165 break;
2166 case XML_RELAXNG_ERR_TEXTWRONG:
2167 snprintf(msg, 1000,
2168 "Did not expect text in element %s content\n", arg1);
2169 break;
2170 case XML_RELAXNG_ERR_ELEMEXTRANS:
2171 snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2172 arg1);
2173 break;
2174 case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2175 snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2176 break;
2177 case XML_RELAXNG_ERR_NOELEM:
2178 snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2179 arg1);
2180 break;
2181 case XML_RELAXNG_ERR_NOTELEM:
2182 return (xmlCharStrdup("Expecting an element got text\n"));
2183 case XML_RELAXNG_ERR_ATTRVALID:
2184 snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2185 arg1);
2186 break;
2187 case XML_RELAXNG_ERR_CONTENTVALID:
2188 snprintf(msg, 1000, "Element %s failed to validate content\n",
2189 arg1);
2190 break;
2191 case XML_RELAXNG_ERR_EXTRACONTENT:
2192 snprintf(msg, 1000, "Element %s has extra content: %s\n",
2193 arg1, arg2);
2194 break;
2195 case XML_RELAXNG_ERR_INVALIDATTR:
2196 snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2197 arg1, arg2);
2198 break;
2199 case XML_RELAXNG_ERR_LACKDATA:
2200 snprintf(msg, 1000, "Datatype element %s contains no data\n",
2201 arg1);
2202 break;
2203 case XML_RELAXNG_ERR_DATAELEM:
2204 snprintf(msg, 1000, "Datatype element %s has child elements\n",
2205 arg1);
2206 break;
2207 case XML_RELAXNG_ERR_VALELEM:
2208 snprintf(msg, 1000, "Value element %s has child elements\n",
2209 arg1);
2210 break;
2211 case XML_RELAXNG_ERR_LISTELEM:
2212 snprintf(msg, 1000, "List element %s has child elements\n",
2213 arg1);
2214 break;
2215 case XML_RELAXNG_ERR_DATATYPE:
2216 snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2217 break;
2218 case XML_RELAXNG_ERR_VALUE:
2219 snprintf(msg, 1000, "Error validating value %s\n", arg1);
2220 break;
2221 case XML_RELAXNG_ERR_LIST:
2222 return (xmlCharStrdup("Error validating list\n"));
2223 case XML_RELAXNG_ERR_NOGRAMMAR:
2224 return (xmlCharStrdup("No top grammar defined\n"));
2225 case XML_RELAXNG_ERR_EXTRADATA:
2226 return (xmlCharStrdup("Extra data in the document\n"));
2227 default:
2228 return (xmlCharStrdup("Unknown error !\n"));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002229 }
2230 if (msg[0] == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002231 snprintf(msg, 1000, "Unknown error code %d\n", err);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002232 }
Daniel Veillardadbb0e62003-05-10 20:02:45 +00002233 msg[1000 - 1] = 0;
Chun-wei Fand77e5fc2016-05-31 21:04:50 +08002234 result = xmlCharStrdup(msg);
David Kilzer502f6a62016-05-23 14:58:41 +08002235 return (xmlEscapeFormatString(&result));
Daniel Veillard42f12e92003-03-07 18:32:59 +00002236}
2237
2238/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002239 * xmlRelaxNGShowValidError:
2240 * @ctxt: the validation context
2241 * @err: the error number
2242 * @node: the node
2243 * @child: the node child generating the problem.
2244 * @arg1: the first argument
2245 * @arg2: the second argument
2246 *
2247 * Show a validation error.
2248 */
2249static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002250xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2251 xmlRelaxNGValidErr err, xmlNodePtr node,
2252 xmlNodePtr child, const xmlChar * arg1,
2253 const xmlChar * arg2)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002254{
2255 xmlChar *msg;
2256
Daniel Veillardb30ca312005-09-04 13:50:03 +00002257 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillardf03a8cd2005-09-04 12:01:57 +00002258 return;
2259
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002260#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002261 xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002262#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002263 msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2264 if (msg == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002265 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002266
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002267 if (ctxt->errNo == XML_RELAXNG_OK)
Daniel Veillard4c004142003-10-07 11:33:24 +00002268 ctxt->errNo = err;
2269 xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2270 (const char *) msg, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002271 xmlFree(msg);
2272}
2273
2274/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002275 * xmlRelaxNGPopErrors:
2276 * @ctxt: the validation context
2277 * @level: the error level in the stack
2278 *
2279 * pop and discard all errors until the given level is reached
2280 */
2281static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002282xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2283{
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002284 int i;
2285 xmlRelaxNGValidErrorPtr err;
2286
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002287#ifdef DEBUG_ERROR
2288 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002289 "Pop errors till level %d\n", level);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002290#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002291 for (i = level; i < ctxt->errNr; i++) {
2292 err = &ctxt->errTab[i];
2293 if (err->flags & ERROR_IS_DUP) {
2294 if (err->arg1 != NULL)
2295 xmlFree((xmlChar *) err->arg1);
2296 err->arg1 = NULL;
2297 if (err->arg2 != NULL)
2298 xmlFree((xmlChar *) err->arg2);
2299 err->arg2 = NULL;
2300 err->flags = 0;
2301 }
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002302 }
2303 ctxt->errNr = level;
Daniel Veillard580ced82003-03-21 21:22:48 +00002304 if (ctxt->errNr <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002305 ctxt->err = NULL;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002306}
Daniel Veillard4c004142003-10-07 11:33:24 +00002307
Daniel Veillard28c52ab2003-03-18 11:39:17 +00002308/**
Daniel Veillard42f12e92003-03-07 18:32:59 +00002309 * xmlRelaxNGDumpValidError:
2310 * @ctxt: the validation context
2311 *
2312 * Show all validation error over a given index.
2313 */
2314static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002315xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2316{
Daniel Veillard5f1946a2003-03-31 16:38:16 +00002317 int i, j, k;
Daniel Veillard580ced82003-03-21 21:22:48 +00002318 xmlRelaxNGValidErrorPtr err, dup;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002319
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002320#ifdef DEBUG_ERROR
2321 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00002322 "Dumping error stack %d errors\n", ctxt->errNr);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002323#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002324 for (i = 0, k = 0; i < ctxt->errNr; i++) {
2325 err = &ctxt->errTab[i];
2326 if (k < MAX_ERROR) {
2327 for (j = 0; j < i; j++) {
2328 dup = &ctxt->errTab[j];
2329 if ((err->err == dup->err) && (err->node == dup->node) &&
2330 (xmlStrEqual(err->arg1, dup->arg1)) &&
2331 (xmlStrEqual(err->arg2, dup->arg2))) {
2332 goto skip;
2333 }
2334 }
2335 xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2336 err->arg1, err->arg2);
2337 k++;
2338 }
2339 skip:
2340 if (err->flags & ERROR_IS_DUP) {
2341 if (err->arg1 != NULL)
2342 xmlFree((xmlChar *) err->arg1);
2343 err->arg1 = NULL;
2344 if (err->arg2 != NULL)
2345 xmlFree((xmlChar *) err->arg2);
2346 err->arg2 = NULL;
2347 err->flags = 0;
2348 }
Daniel Veillard42f12e92003-03-07 18:32:59 +00002349 }
2350 ctxt->errNr = 0;
2351}
Daniel Veillard4c004142003-10-07 11:33:24 +00002352
Daniel Veillard42f12e92003-03-07 18:32:59 +00002353/**
2354 * xmlRelaxNGAddValidError:
2355 * @ctxt: the validation context
2356 * @err: the error number
2357 * @arg1: the first argument
2358 * @arg2: the second argument
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002359 * @dup: need to dup the args
Daniel Veillard42f12e92003-03-07 18:32:59 +00002360 *
2361 * Register a validation error, either generating it if it's sure
2362 * or stacking it for later handling if unsure.
2363 */
2364static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002365xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2366 xmlRelaxNGValidErr err, const xmlChar * arg1,
2367 const xmlChar * arg2, int dup)
Daniel Veillard42f12e92003-03-07 18:32:59 +00002368{
Daniel Veillardb30ca312005-09-04 13:50:03 +00002369 if (ctxt == NULL)
2370 return;
2371 if (ctxt->flags & FLAGS_NOERROR)
Daniel Veillard4c004142003-10-07 11:33:24 +00002372 return;
Daniel Veillard42f12e92003-03-07 18:32:59 +00002373
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002374#ifdef DEBUG_ERROR
Daniel Veillard4c004142003-10-07 11:33:24 +00002375 xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
Daniel Veillarda507fbf2003-03-31 16:09:37 +00002376#endif
Daniel Veillard42f12e92003-03-07 18:32:59 +00002377 /*
2378 * generate the error directly
2379 */
William M. Brack60929622004-03-27 17:54:18 +00002380 if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002381 (ctxt->flags & FLAGS_NEGATIVE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002382 xmlNodePtr node, seq;
2383
2384 /*
2385 * Flush first any stacked error which might be the
2386 * real cause of the problem.
2387 */
2388 if (ctxt->errNr != 0)
2389 xmlRelaxNGDumpValidError(ctxt);
2390 if (ctxt->state != NULL) {
2391 node = ctxt->state->node;
2392 seq = ctxt->state->seq;
2393 } else {
2394 node = seq = NULL;
2395 }
Daniel Veillardec18c962009-08-26 18:37:43 +02002396 if ((node == NULL) && (seq == NULL)) {
2397 node = ctxt->pnode;
2398 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002399 xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002400 }
2401 /*
2402 * Stack the error for later processing if needed
2403 */
2404 else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002405 xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
Daniel Veillard42f12e92003-03-07 18:32:59 +00002406 }
2407}
2408
Daniel Veillard6eadf632003-01-23 18:29:16 +00002409
2410/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002411 * *
2412 * Type library hooks *
2413 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00002414 ************************************************************************/
Daniel Veillardea3f3982003-01-26 19:45:18 +00002415static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00002416 const xmlChar * str);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002417
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002418/**
2419 * xmlRelaxNGSchemaTypeHave:
2420 * @data: data needed for the library
2421 * @type: the type name
2422 *
2423 * Check if the given type is provided by
2424 * the W3C XMLSchema Datatype library.
2425 *
2426 * Returns 1 if yes, 0 if no and -1 in case of error.
2427 */
2428static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002429xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2430{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002431 xmlSchemaTypePtr typ;
2432
2433 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002434 return (-1);
2435 typ = xmlSchemaGetPredefinedType(type,
2436 BAD_CAST
2437 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002438 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002439 return (0);
2440 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002441}
2442
2443/**
2444 * xmlRelaxNGSchemaTypeCheck:
2445 * @data: data needed for the library
2446 * @type: the type name
2447 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002448 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002449 *
2450 * Check if the given type and value are validated by
2451 * the W3C XMLSchema Datatype library.
2452 *
2453 * Returns 1 if yes, 0 if no and -1 in case of error.
2454 */
2455static int
2456xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002457 const xmlChar * type,
2458 const xmlChar * value,
2459 void **result, xmlNodePtr node)
2460{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002461 xmlSchemaTypePtr typ;
2462 int ret;
2463
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002464 if ((type == NULL) || (value == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002465 return (-1);
2466 typ = xmlSchemaGetPredefinedType(type,
2467 BAD_CAST
2468 "http://www.w3.org/2001/XMLSchema");
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002469 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002470 return (-1);
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002471 ret = xmlSchemaValPredefTypeNode(typ, value,
Daniel Veillard4c004142003-10-07 11:33:24 +00002472 (xmlSchemaValPtr *) result, node);
2473 if (ret == 2) /* special ID error code */
2474 return (2);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002475 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002476 return (1);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00002477 if (ret > 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002478 return (0);
2479 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002480}
2481
2482/**
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002483 * xmlRelaxNGSchemaFacetCheck:
2484 * @data: data needed for the library
2485 * @type: the type name
2486 * @facet: the facet name
2487 * @val: the facet value
2488 * @strval: the string value
2489 * @value: the value to check
2490 *
2491 * Function provided by a type library to check a value facet
2492 *
2493 * Returns 1 if yes, 0 if no and -1 in case of error.
2494 */
2495static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002496xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2497 const xmlChar * type, const xmlChar * facetname,
2498 const xmlChar * val, const xmlChar * strval,
2499 void *value)
2500{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002501 xmlSchemaFacetPtr facet;
2502 xmlSchemaTypePtr typ;
2503 int ret;
2504
2505 if ((type == NULL) || (strval == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002506 return (-1);
2507 typ = xmlSchemaGetPredefinedType(type,
2508 BAD_CAST
2509 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002510 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002511 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002512
2513 facet = xmlSchemaNewFacet();
2514 if (facet == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002515 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002516
Daniel Veillard4c004142003-10-07 11:33:24 +00002517 if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002518 facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002519 } else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002520 facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002521 } else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002522 facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002523 } else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002524 facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002525 } else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002526 facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002527 } else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002528 facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
Daniel Veillard4c004142003-10-07 11:33:24 +00002529 } else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002530 facet->type = XML_SCHEMA_FACET_PATTERN;
Daniel Veillard4c004142003-10-07 11:33:24 +00002531 } else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002532 facet->type = XML_SCHEMA_FACET_ENUMERATION;
Daniel Veillard4c004142003-10-07 11:33:24 +00002533 } else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002534 facet->type = XML_SCHEMA_FACET_WHITESPACE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002535 } else if (xmlStrEqual(facetname, BAD_CAST "length")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002536 facet->type = XML_SCHEMA_FACET_LENGTH;
Daniel Veillard4c004142003-10-07 11:33:24 +00002537 } else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002538 facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2539 } else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2540 facet->type = XML_SCHEMA_FACET_MINLENGTH;
2541 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002542 xmlSchemaFreeFacet(facet);
2543 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002544 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00002545 facet->value = val;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002546 ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2547 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002548 xmlSchemaFreeFacet(facet);
2549 return (-1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002550 }
2551 ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2552 xmlSchemaFreeFacet(facet);
2553 if (ret != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002554 return (-1);
2555 return (0);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002556}
2557
2558/**
Daniel Veillard80b19092003-03-28 13:29:53 +00002559 * xmlRelaxNGSchemaFreeValue:
2560 * @data: data needed for the library
2561 * @value: the value to free
2562 *
2563 * Function provided by a type library to free a Schemas value
2564 *
2565 * Returns 1 if yes, 0 if no and -1 in case of error.
2566 */
2567static void
Daniel Veillard4c004142003-10-07 11:33:24 +00002568xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2569{
Daniel Veillard80b19092003-03-28 13:29:53 +00002570 xmlSchemaFreeValue(value);
2571}
2572
2573/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002574 * xmlRelaxNGSchemaTypeCompare:
2575 * @data: data needed for the library
2576 * @type: the type name
2577 * @value1: the first value
2578 * @value2: the second value
2579 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002580 * Compare two values for equality accordingly a type from the W3C XMLSchema
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002581 * Datatype library.
2582 *
Daniel Veillard80b19092003-03-28 13:29:53 +00002583 * Returns 1 if equal, 0 if no and -1 in case of error.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002584 */
2585static int
2586xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002587 const xmlChar * type,
2588 const xmlChar * value1,
2589 xmlNodePtr ctxt1,
2590 void *comp1,
2591 const xmlChar * value2, xmlNodePtr ctxt2)
2592{
Daniel Veillard80b19092003-03-28 13:29:53 +00002593 int ret;
2594 xmlSchemaTypePtr typ;
2595 xmlSchemaValPtr res1 = NULL, res2 = NULL;
2596
2597 if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00002598 return (-1);
2599 typ = xmlSchemaGetPredefinedType(type,
2600 BAD_CAST
2601 "http://www.w3.org/2001/XMLSchema");
Daniel Veillard80b19092003-03-28 13:29:53 +00002602 if (typ == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002603 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002604 if (comp1 == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002605 ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2606 if (ret != 0)
2607 return (-1);
2608 if (res1 == NULL)
2609 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002610 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00002611 res1 = (xmlSchemaValPtr) comp1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002612 }
2613 ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
Daniel Veillard80b19092003-03-28 13:29:53 +00002614 if (ret != 0) {
Gaurav7d4e2592013-09-30 11:27:41 +08002615 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillardf4644032005-06-13 11:41:31 +00002616 xmlSchemaFreeValue(res1);
Daniel Veillard4c004142003-10-07 11:33:24 +00002617 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002618 }
Daniel Veillard80b19092003-03-28 13:29:53 +00002619 ret = xmlSchemaCompareValues(res1, res2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002620 if (res1 != (xmlSchemaValPtr) comp1)
Daniel Veillard4c004142003-10-07 11:33:24 +00002621 xmlSchemaFreeValue(res1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002622 xmlSchemaFreeValue(res2);
2623 if (ret == -2)
Daniel Veillard4c004142003-10-07 11:33:24 +00002624 return (-1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002625 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002626 return (1);
2627 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002628}
Daniel Veillard4c004142003-10-07 11:33:24 +00002629
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002630/**
2631 * xmlRelaxNGDefaultTypeHave:
2632 * @data: data needed for the library
2633 * @type: the type name
2634 *
2635 * Check if the given type is provided by
2636 * the default datatype library.
2637 *
2638 * Returns 1 if yes, 0 if no and -1 in case of error.
2639 */
2640static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002641xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2642 const xmlChar * type)
2643{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002644 if (type == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002645 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002646 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002647 return (1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002648 if (xmlStrEqual(type, BAD_CAST "token"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002649 return (1);
2650 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002651}
2652
2653/**
2654 * xmlRelaxNGDefaultTypeCheck:
2655 * @data: data needed for the library
2656 * @type: the type name
2657 * @value: the value to check
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002658 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002659 *
2660 * Check if the given type and value are validated by
2661 * the default datatype library.
2662 *
2663 * Returns 1 if yes, 0 if no and -1 in case of error.
2664 */
2665static int
2666xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002667 const xmlChar * type ATTRIBUTE_UNUSED,
2668 const xmlChar * value ATTRIBUTE_UNUSED,
2669 void **result ATTRIBUTE_UNUSED,
2670 xmlNodePtr node ATTRIBUTE_UNUSED)
2671{
Daniel Veillardd4310742003-02-18 21:12:46 +00002672 if (value == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002673 return (-1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002674 if (xmlStrEqual(type, BAD_CAST "string"))
Daniel Veillard4c004142003-10-07 11:33:24 +00002675 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002676 if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002677 return (1);
Daniel Veillardd4310742003-02-18 21:12:46 +00002678 }
2679
Daniel Veillard4c004142003-10-07 11:33:24 +00002680 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002681}
2682
2683/**
2684 * xmlRelaxNGDefaultTypeCompare:
2685 * @data: data needed for the library
2686 * @type: the type name
2687 * @value1: the first value
2688 * @value2: the second value
2689 *
2690 * Compare two values accordingly a type from the default
2691 * datatype library.
2692 *
2693 * Returns 1 if yes, 0 if no and -1 in case of error.
2694 */
2695static int
2696xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00002697 const xmlChar * type,
2698 const xmlChar * value1,
2699 xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2700 void *comp1 ATTRIBUTE_UNUSED,
2701 const xmlChar * value2,
2702 xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2703{
Daniel Veillardea3f3982003-01-26 19:45:18 +00002704 int ret = -1;
2705
2706 if (xmlStrEqual(type, BAD_CAST "string")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002707 ret = xmlStrEqual(value1, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002708 } else if (xmlStrEqual(type, BAD_CAST "token")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002709 if (!xmlStrEqual(value1, value2)) {
2710 xmlChar *nval, *nvalue;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002711
Daniel Veillard4c004142003-10-07 11:33:24 +00002712 /*
2713 * TODO: trivial optimizations are possible by
2714 * computing at compile-time
2715 */
2716 nval = xmlRelaxNGNormalize(NULL, value1);
2717 nvalue = xmlRelaxNGNormalize(NULL, value2);
Daniel Veillardea3f3982003-01-26 19:45:18 +00002718
Daniel Veillard4c004142003-10-07 11:33:24 +00002719 if ((nval == NULL) || (nvalue == NULL))
2720 ret = -1;
2721 else if (xmlStrEqual(nval, nvalue))
2722 ret = 1;
2723 else
2724 ret = 0;
2725 if (nval != NULL)
2726 xmlFree(nval);
2727 if (nvalue != NULL)
2728 xmlFree(nvalue);
2729 } else
2730 ret = 1;
Daniel Veillardea3f3982003-01-26 19:45:18 +00002731 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002732 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002733}
Daniel Veillard4c004142003-10-07 11:33:24 +00002734
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002735static int xmlRelaxNGTypeInitialized = 0;
2736static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2737
2738/**
2739 * xmlRelaxNGFreeTypeLibrary:
2740 * @lib: the type library structure
2741 * @namespace: the URI bound to the library
2742 *
2743 * Free the structure associated to the type library
2744 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00002745static void
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01002746xmlRelaxNGFreeTypeLibrary(void *payload,
Daniel Veillard4c004142003-10-07 11:33:24 +00002747 const xmlChar * namespace ATTRIBUTE_UNUSED)
2748{
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01002749 xmlRelaxNGTypeLibraryPtr lib = (xmlRelaxNGTypeLibraryPtr) payload;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002750 if (lib == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002751 return;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002752 if (lib->namespace != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00002753 xmlFree((xmlChar *) lib->namespace);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002754 xmlFree(lib);
2755}
2756
2757/**
2758 * xmlRelaxNGRegisterTypeLibrary:
2759 * @namespace: the URI bound to the library
2760 * @data: data associated to the library
2761 * @have: the provide function
2762 * @check: the checking function
2763 * @comp: the comparison function
2764 *
2765 * Register a new type library
2766 *
2767 * Returns 0 in case of success and -1 in case of error.
2768 */
2769static int
Daniel Veillard4c004142003-10-07 11:33:24 +00002770xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2771 xmlRelaxNGTypeHave have,
2772 xmlRelaxNGTypeCheck check,
2773 xmlRelaxNGTypeCompare comp,
2774 xmlRelaxNGFacetCheck facet,
2775 xmlRelaxNGTypeFree freef)
2776{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002777 xmlRelaxNGTypeLibraryPtr lib;
2778 int ret;
2779
2780 if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00002781 (check == NULL) || (comp == NULL))
2782 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002783 if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002784 xmlGenericError(xmlGenericErrorContext,
2785 "Relax-NG types library '%s' already registered\n",
2786 namespace);
2787 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002788 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002789 lib =
2790 (xmlRelaxNGTypeLibraryPtr)
2791 xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002792 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002793 xmlRngVErrMemory(NULL, "adding types library\n");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002794 return (-1);
2795 }
2796 memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2797 lib->namespace = xmlStrdup(namespace);
2798 lib->data = data;
2799 lib->have = have;
2800 lib->comp = comp;
2801 lib->check = check;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00002802 lib->facet = facet;
2803 lib->freef = freef;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002804 ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2805 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002806 xmlGenericError(xmlGenericErrorContext,
2807 "Relax-NG types library failed to register '%s'\n",
2808 namespace);
2809 xmlRelaxNGFreeTypeLibrary(lib, namespace);
2810 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002811 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002812 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002813}
2814
2815/**
2816 * xmlRelaxNGInitTypes:
2817 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002818 * Initialize the default type libraries.
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002819 *
2820 * Returns 0 in case of success and -1 in case of error.
2821 */
Daniel Veillarddd6d3002004-11-03 14:20:29 +00002822int
Daniel Veillard4c004142003-10-07 11:33:24 +00002823xmlRelaxNGInitTypes(void)
2824{
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002825 if (xmlRelaxNGTypeInitialized != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002826 return (0);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002827 xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2828 if (xmlRelaxNGRegisteredTypes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002829 xmlGenericError(xmlGenericErrorContext,
2830 "Failed to allocate sh table for Relax-NG types\n");
2831 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002832 }
Daniel Veillard4c004142003-10-07 11:33:24 +00002833 xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2834 "http://www.w3.org/2001/XMLSchema-datatypes",
2835 NULL, xmlRelaxNGSchemaTypeHave,
2836 xmlRelaxNGSchemaTypeCheck,
2837 xmlRelaxNGSchemaTypeCompare,
2838 xmlRelaxNGSchemaFacetCheck,
2839 xmlRelaxNGSchemaFreeValue);
2840 xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2841 xmlRelaxNGDefaultTypeHave,
2842 xmlRelaxNGDefaultTypeCheck,
2843 xmlRelaxNGDefaultTypeCompare, NULL,
2844 NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002845 xmlRelaxNGTypeInitialized = 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00002846 return (0);
Daniel Veillard6eadf632003-01-23 18:29:16 +00002847}
2848
2849/**
2850 * xmlRelaxNGCleanupTypes:
2851 *
2852 * Cleanup the default Schemas type library associated to RelaxNG
2853 */
Daniel Veillard4c004142003-10-07 11:33:24 +00002854void
2855xmlRelaxNGCleanupTypes(void)
2856{
Daniel Veillarda84c0b32003-06-02 16:58:46 +00002857 xmlSchemaCleanupTypes();
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002858 if (xmlRelaxNGTypeInitialized == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00002859 return;
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01002860 xmlHashFree(xmlRelaxNGRegisteredTypes, xmlRelaxNGFreeTypeLibrary);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00002861 xmlRelaxNGTypeInitialized = 0;
Daniel Veillard6eadf632003-01-23 18:29:16 +00002862}
2863
2864/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002865 * *
2866 * Compiling element content into regexp *
2867 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002868 * Sometime the element content can be compiled into a pure regexp, *
2869 * This allows a faster execution and streamability at that level *
Daniel Veillardf8e3db02012-09-11 13:26:36 +08002870 * *
Daniel Veillard952379b2003-03-17 15:37:12 +00002871 ************************************************************************/
2872
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02002873/* from automata.c but not exported */
2874void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
2875
2876
Daniel Veillard52b48c72003-04-13 19:53:42 +00002877static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2878 xmlRelaxNGDefinePtr def);
2879
Daniel Veillard952379b2003-03-17 15:37:12 +00002880/**
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002881 * xmlRelaxNGIsCompilable:
Daniel Veillard952379b2003-03-17 15:37:12 +00002882 * @define: the definition to check
2883 *
2884 * Check if a definition is nullable.
2885 *
2886 * Returns 1 if yes, 0 if no and -1 in case of error
2887 */
2888static int
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002889xmlRelaxNGIsCompilable(xmlRelaxNGDefinePtr def)
Daniel Veillard4c004142003-10-07 11:33:24 +00002890{
Daniel Veillard52b48c72003-04-13 19:53:42 +00002891 int ret = -1;
2892
Daniel Veillard952379b2003-03-17 15:37:12 +00002893 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00002894 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00002895 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00002896 if ((def->type != XML_RELAXNG_ELEMENT) &&
2897 (def->dflags & IS_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002898 return (1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002899 if ((def->type != XML_RELAXNG_ELEMENT) &&
2900 (def->dflags & IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002901 return (0);
2902 switch (def->type) {
Daniel Veillard952379b2003-03-17 15:37:12 +00002903 case XML_RELAXNG_NOOP:
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002904 ret = xmlRelaxNGIsCompilable(def->content);
Daniel Veillard4c004142003-10-07 11:33:24 +00002905 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002906 case XML_RELAXNG_TEXT:
Daniel Veillard952379b2003-03-17 15:37:12 +00002907 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00002908 ret = 1;
2909 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00002910 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00002911 /*
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002912 * Check if the element content is compilable
Daniel Veillard4c004142003-10-07 11:33:24 +00002913 */
2914 if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2915 ((def->dflags & IS_COMPILABLE) == 0)) {
2916 xmlRelaxNGDefinePtr list;
2917
2918 list = def->content;
2919 while (list != NULL) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002920 ret = xmlRelaxNGIsCompilable(list);
Daniel Veillard4c004142003-10-07 11:33:24 +00002921 if (ret != 1)
2922 break;
2923 list = list->next;
2924 }
William M. Brack60929622004-03-27 17:54:18 +00002925 /*
2926 * Because the routine is recursive, we must guard against
2927 * discovering both COMPILABLE and NOT_COMPILABLE
2928 */
2929 if (ret == 0) {
2930 def->dflags &= ~IS_COMPILABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +00002931 def->dflags |= IS_NOT_COMPILABLE;
William M. Brack60929622004-03-27 17:54:18 +00002932 }
2933 if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
Daniel Veillard4c004142003-10-07 11:33:24 +00002934 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00002935#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +00002936 if (ret == 1) {
2937 xmlGenericError(xmlGenericErrorContext,
2938 "element content for %s is compilable\n",
2939 def->name);
2940 } else if (ret == 0) {
2941 xmlGenericError(xmlGenericErrorContext,
2942 "element content for %s is not compilable\n",
2943 def->name);
2944 } else {
2945 xmlGenericError(xmlGenericErrorContext,
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002946 "Problem in RelaxNGIsCompilable for element %s\n",
Daniel Veillard4c004142003-10-07 11:33:24 +00002947 def->name);
2948 }
Daniel Veillardd94849b2003-07-28 13:02:24 +00002949#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00002950 }
2951 /*
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002952 * All elements return a compilable status unless they
Daniel Veillard4c004142003-10-07 11:33:24 +00002953 * are generic like anyName
2954 */
2955 if ((def->nameClass != NULL) || (def->name == NULL))
2956 ret = 0;
2957 else
2958 ret = 1;
2959 return (ret);
Daniel Veillard2134ab12003-07-23 19:56:29 +00002960 case XML_RELAXNG_REF:
2961 case XML_RELAXNG_EXTERNALREF:
2962 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00002963 if (def->depth == -20) {
2964 return (1);
2965 } else {
2966 xmlRelaxNGDefinePtr list;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002967
Daniel Veillard4c004142003-10-07 11:33:24 +00002968 def->depth = -20;
2969 list = def->content;
2970 while (list != NULL) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002971 ret = xmlRelaxNGIsCompilable(list);
Daniel Veillard4c004142003-10-07 11:33:24 +00002972 if (ret != 1)
2973 break;
2974 list = list->next;
2975 }
2976 }
2977 break;
Daniel Veillard2134ab12003-07-23 19:56:29 +00002978 case XML_RELAXNG_START:
Daniel Veillard952379b2003-03-17 15:37:12 +00002979 case XML_RELAXNG_OPTIONAL:
2980 case XML_RELAXNG_ZEROORMORE:
2981 case XML_RELAXNG_ONEORMORE:
2982 case XML_RELAXNG_CHOICE:
2983 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00002984 case XML_RELAXNG_DEF:{
2985 xmlRelaxNGDefinePtr list;
Daniel Veillard952379b2003-03-17 15:37:12 +00002986
Daniel Veillard4c004142003-10-07 11:33:24 +00002987 list = def->content;
2988 while (list != NULL) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07002989 ret = xmlRelaxNGIsCompilable(list);
Daniel Veillard4c004142003-10-07 11:33:24 +00002990 if (ret != 1)
2991 break;
2992 list = list->next;
2993 }
2994 break;
2995 }
Daniel Veillard952379b2003-03-17 15:37:12 +00002996 case XML_RELAXNG_EXCEPT:
2997 case XML_RELAXNG_ATTRIBUTE:
2998 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard52b48c72003-04-13 19:53:42 +00002999 case XML_RELAXNG_DATATYPE:
3000 case XML_RELAXNG_LIST:
3001 case XML_RELAXNG_PARAM:
3002 case XML_RELAXNG_VALUE:
Daniel Veillard952379b2003-03-17 15:37:12 +00003003 case XML_RELAXNG_NOT_ALLOWED:
William M. Brack7e29c0a2004-04-02 09:07:22 +00003004 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00003005 break;
Daniel Veillard952379b2003-03-17 15:37:12 +00003006 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003007 if (ret == 0)
3008 def->dflags |= IS_NOT_COMPILABLE;
3009 if (ret == 1)
3010 def->dflags |= IS_COMPILABLE;
Daniel Veillardd94849b2003-07-28 13:02:24 +00003011#ifdef DEBUG_COMPILE
3012 if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003013 xmlGenericError(xmlGenericErrorContext,
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003014 "RelaxNGIsCompilable %s : true\n",
Daniel Veillard4c004142003-10-07 11:33:24 +00003015 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003016 } else if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003017 xmlGenericError(xmlGenericErrorContext,
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003018 "RelaxNGIsCompilable %s : false\n",
Daniel Veillard4c004142003-10-07 11:33:24 +00003019 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003020 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003021 xmlGenericError(xmlGenericErrorContext,
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003022 "Problem in RelaxNGIsCompilable %s\n",
Daniel Veillard4c004142003-10-07 11:33:24 +00003023 xmlRelaxNGDefName(def));
Daniel Veillardd94849b2003-07-28 13:02:24 +00003024 }
3025#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003026 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003027}
3028
3029/**
3030 * xmlRelaxNGCompile:
3031 * ctxt: the RelaxNG parser context
3032 * @define: the definition tree to compile
3033 *
3034 * Compile the set of definitions, it works recursively, till the
3035 * element boundaries, where it tries to compile the content if possible
3036 *
3037 * Returns 0 if success and -1 in case of error
3038 */
3039static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003040xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3041{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003042 int ret = 0;
3043 xmlRelaxNGDefinePtr list;
3044
Daniel Veillard4c004142003-10-07 11:33:24 +00003045 if ((ctxt == NULL) || (def == NULL))
3046 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003047
Daniel Veillard4c004142003-10-07 11:33:24 +00003048 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003049 case XML_RELAXNG_START:
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003050 if ((xmlRelaxNGIsCompilable(def) == 1) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003051 xmlAutomataPtr oldam = ctxt->am;
3052 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003053
3054 def->depth = -25;
3055
Daniel Veillard4c004142003-10-07 11:33:24 +00003056 list = def->content;
3057 ctxt->am = xmlNewAutomata();
3058 if (ctxt->am == NULL)
3059 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003060
3061 /*
3062 * assume identical strings but not same pointer are different
3063 * atoms, needed for non-determinism detection
3064 * That way if 2 elements with the same name are in a choice
3065 * branch the automata is found non-deterministic and
3066 * we fallback to the normal validation which does the right
3067 * thing of exploring both choices.
3068 */
3069 xmlAutomataSetFlags(ctxt->am, 1);
3070
Daniel Veillard4c004142003-10-07 11:33:24 +00003071 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3072 while (list != NULL) {
3073 xmlRelaxNGCompile(ctxt, list);
3074 list = list->next;
3075 }
3076 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
Noam9313ae82012-05-15 11:03:46 +08003077 if (xmlAutomataIsDeterminist(ctxt->am))
3078 def->contModel = xmlAutomataCompile(ctxt->am);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003079
Daniel Veillard4c004142003-10-07 11:33:24 +00003080 xmlFreeAutomata(ctxt->am);
3081 ctxt->state = oldstate;
3082 ctxt->am = oldam;
3083 }
3084 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003085 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003086 if ((ctxt->am != NULL) && (def->name != NULL)) {
3087 ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3088 ctxt->state, NULL,
3089 def->name, def->ns,
3090 def);
3091 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003092 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003093 xmlAutomataPtr oldam = ctxt->am;
3094 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003095
3096 def->depth = -25;
3097
Daniel Veillard4c004142003-10-07 11:33:24 +00003098 list = def->content;
3099 ctxt->am = xmlNewAutomata();
3100 if (ctxt->am == NULL)
3101 return (-1);
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003102 xmlAutomataSetFlags(ctxt->am, 1);
Daniel Veillard4c004142003-10-07 11:33:24 +00003103 ctxt->state = xmlAutomataGetInitState(ctxt->am);
3104 while (list != NULL) {
3105 xmlRelaxNGCompile(ctxt, list);
3106 list = list->next;
3107 }
3108 xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3109 def->contModel = xmlAutomataCompile(ctxt->am);
3110 if (!xmlRegexpIsDeterminist(def->contModel)) {
Daniel Veillard1ba2aca2009-08-31 16:47:39 +02003111#ifdef DEBUG_COMPILE
3112 xmlGenericError(xmlGenericErrorContext,
3113 "Content model not determinist %s\n",
3114 def->name);
3115#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003116 /*
3117 * we can only use the automata if it is determinist
3118 */
3119 xmlRegFreeRegexp(def->contModel);
3120 def->contModel = NULL;
3121 }
3122 xmlFreeAutomata(ctxt->am);
3123 ctxt->state = oldstate;
3124 ctxt->am = oldam;
3125 } else {
3126 xmlAutomataPtr oldam = ctxt->am;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003127
Daniel Veillard4c004142003-10-07 11:33:24 +00003128 /*
3129 * we can't build the content model for this element content
3130 * but it still might be possible to build it for some of its
3131 * children, recurse.
3132 */
3133 ret = xmlRelaxNGTryCompile(ctxt, def);
3134 ctxt->am = oldam;
3135 }
3136 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003137 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003138 ret = xmlRelaxNGCompile(ctxt, def->content);
3139 break;
3140 case XML_RELAXNG_OPTIONAL:{
3141 xmlAutomataStatePtr oldstate = ctxt->state;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003142
Daniel Veillardfd780772009-08-26 18:35:29 +02003143 list = def->content;
3144 while (list != NULL) {
3145 xmlRelaxNGCompile(ctxt, list);
3146 list = list->next;
3147 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003148 xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3149 break;
3150 }
3151 case XML_RELAXNG_ZEROORMORE:{
3152 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003153
Daniel Veillard4c004142003-10-07 11:33:24 +00003154 ctxt->state =
3155 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3156 oldstate = ctxt->state;
3157 list = def->content;
3158 while (list != NULL) {
3159 xmlRelaxNGCompile(ctxt, list);
3160 list = list->next;
3161 }
3162 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3163 ctxt->state =
3164 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3165 break;
3166 }
3167 case XML_RELAXNG_ONEORMORE:{
3168 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003169
Daniel Veillard4c004142003-10-07 11:33:24 +00003170 list = def->content;
3171 while (list != NULL) {
3172 xmlRelaxNGCompile(ctxt, list);
3173 list = list->next;
3174 }
3175 oldstate = ctxt->state;
3176 list = def->content;
3177 while (list != NULL) {
3178 xmlRelaxNGCompile(ctxt, list);
3179 list = list->next;
3180 }
3181 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3182 ctxt->state =
3183 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3184 break;
3185 }
3186 case XML_RELAXNG_CHOICE:{
3187 xmlAutomataStatePtr target = NULL;
3188 xmlAutomataStatePtr oldstate = ctxt->state;
3189
3190 list = def->content;
3191 while (list != NULL) {
3192 ctxt->state = oldstate;
3193 ret = xmlRelaxNGCompile(ctxt, list);
3194 if (ret != 0)
3195 break;
3196 if (target == NULL)
3197 target = ctxt->state;
3198 else {
3199 xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3200 target);
3201 }
3202 list = list->next;
3203 }
3204 ctxt->state = target;
3205
3206 break;
3207 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003208 case XML_RELAXNG_REF:
3209 case XML_RELAXNG_EXTERNALREF:
3210 case XML_RELAXNG_PARENTREF:
Daniel Veillard52b48c72003-04-13 19:53:42 +00003211 case XML_RELAXNG_GROUP:
3212 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003213 list = def->content;
3214 while (list != NULL) {
3215 ret = xmlRelaxNGCompile(ctxt, list);
3216 if (ret != 0)
3217 break;
3218 list = list->next;
3219 }
3220 break;
3221 case XML_RELAXNG_TEXT:{
3222 xmlAutomataStatePtr oldstate;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003223
Daniel Veillard4c004142003-10-07 11:33:24 +00003224 ctxt->state =
3225 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3226 oldstate = ctxt->state;
3227 xmlRelaxNGCompile(ctxt, def->content);
3228 xmlAutomataNewTransition(ctxt->am, ctxt->state,
3229 ctxt->state, BAD_CAST "#text",
3230 NULL);
3231 ctxt->state =
3232 xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3233 break;
3234 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003235 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00003236 ctxt->state =
3237 xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3238 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003239 case XML_RELAXNG_EXCEPT:
3240 case XML_RELAXNG_ATTRIBUTE:
3241 case XML_RELAXNG_INTERLEAVE:
3242 case XML_RELAXNG_NOT_ALLOWED:
3243 case XML_RELAXNG_DATATYPE:
3244 case XML_RELAXNG_LIST:
3245 case XML_RELAXNG_PARAM:
3246 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003247 /* This should not happen and generate an internal error */
3248 fprintf(stderr, "RNG internal error trying to compile %s\n",
3249 xmlRelaxNGDefName(def));
3250 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003251 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003252 return (ret);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003253}
3254
3255/**
3256 * xmlRelaxNGTryCompile:
3257 * ctxt: the RelaxNG parser context
3258 * @define: the definition tree to compile
3259 *
3260 * Try to compile the set of definitions, it works recursively,
3261 * possibly ignoring parts which cannot be compiled.
3262 *
3263 * Returns 0 if success and -1 in case of error
3264 */
3265static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003266xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3267{
Daniel Veillard52b48c72003-04-13 19:53:42 +00003268 int ret = 0;
3269 xmlRelaxNGDefinePtr list;
3270
Daniel Veillard4c004142003-10-07 11:33:24 +00003271 if ((ctxt == NULL) || (def == NULL))
3272 return (-1);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003273
3274 if ((def->type == XML_RELAXNG_START) ||
3275 (def->type == XML_RELAXNG_ELEMENT)) {
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003276 ret = xmlRelaxNGIsCompilable(def);
Daniel Veillard4c004142003-10-07 11:33:24 +00003277 if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3278 ctxt->am = NULL;
3279 ret = xmlRelaxNGCompile(ctxt, def);
Daniel Veillard2134ab12003-07-23 19:56:29 +00003280#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00003281 if (ret == 0) {
3282 if (def->type == XML_RELAXNG_START)
3283 xmlGenericError(xmlGenericErrorContext,
3284 "compiled the start\n");
3285 else
3286 xmlGenericError(xmlGenericErrorContext,
3287 "compiled element %s\n", def->name);
3288 } else {
3289 if (def->type == XML_RELAXNG_START)
3290 xmlGenericError(xmlGenericErrorContext,
3291 "failed to compile the start\n");
3292 else
3293 xmlGenericError(xmlGenericErrorContext,
3294 "failed to compile element %s\n",
3295 def->name);
3296 }
Daniel Veillard2134ab12003-07-23 19:56:29 +00003297#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00003298 return (ret);
3299 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00003300 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003301 switch (def->type) {
Daniel Veillard52b48c72003-04-13 19:53:42 +00003302 case XML_RELAXNG_NOOP:
Daniel Veillard4c004142003-10-07 11:33:24 +00003303 ret = xmlRelaxNGTryCompile(ctxt, def->content);
3304 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003305 case XML_RELAXNG_TEXT:
3306 case XML_RELAXNG_DATATYPE:
3307 case XML_RELAXNG_LIST:
3308 case XML_RELAXNG_PARAM:
3309 case XML_RELAXNG_VALUE:
3310 case XML_RELAXNG_EMPTY:
3311 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003312 ret = 0;
3313 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003314 case XML_RELAXNG_OPTIONAL:
3315 case XML_RELAXNG_ZEROORMORE:
3316 case XML_RELAXNG_ONEORMORE:
3317 case XML_RELAXNG_CHOICE:
3318 case XML_RELAXNG_GROUP:
3319 case XML_RELAXNG_DEF:
Daniel Veillard2134ab12003-07-23 19:56:29 +00003320 case XML_RELAXNG_START:
3321 case XML_RELAXNG_REF:
3322 case XML_RELAXNG_EXTERNALREF:
3323 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00003324 list = def->content;
3325 while (list != NULL) {
3326 ret = xmlRelaxNGTryCompile(ctxt, list);
3327 if (ret != 0)
3328 break;
3329 list = list->next;
3330 }
3331 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003332 case XML_RELAXNG_EXCEPT:
3333 case XML_RELAXNG_ATTRIBUTE:
3334 case XML_RELAXNG_INTERLEAVE:
3335 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00003336 ret = 0;
3337 break;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003338 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003339 return (ret);
Daniel Veillard952379b2003-03-17 15:37:12 +00003340}
3341
3342/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08003343 * *
3344 * Parsing functions *
3345 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00003346 ************************************************************************/
3347
Daniel Veillard4c004142003-10-07 11:33:24 +00003348static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3349 ctxt, xmlNodePtr node);
3350static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3351 ctxt, xmlNodePtr node);
3352static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3353 ctxt, xmlNodePtr nodes,
3354 int group);
3355static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3356 ctxt, xmlNodePtr node);
3357static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3358 xmlNodePtr node);
3359static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3360 xmlNodePtr nodes);
3361static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3362 ctxt, xmlNodePtr node,
3363 xmlRelaxNGDefinePtr
3364 def);
3365static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3366 ctxt, xmlNodePtr nodes);
3367static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3368 xmlRelaxNGDefinePtr define,
3369 xmlNodePtr elem);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003370
3371
Daniel Veillard249d7bb2003-03-19 21:02:29 +00003372#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
Daniel Veillard6eadf632003-01-23 18:29:16 +00003373
3374/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00003375 * xmlRelaxNGIsNullable:
3376 * @define: the definition to verify
3377 *
3378 * Check if a definition is nullable.
3379 *
3380 * Returns 1 if yes, 0 if no and -1 in case of error
3381 */
3382static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003383xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3384{
Daniel Veillardfd573f12003-03-16 17:52:32 +00003385 int ret;
Daniel Veillard4c004142003-10-07 11:33:24 +00003386
Daniel Veillardfd573f12003-03-16 17:52:32 +00003387 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003388 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003389
Daniel Veillarde063f482003-03-21 16:53:17 +00003390 if (define->dflags & IS_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003391 return (1);
Daniel Veillarde063f482003-03-21 16:53:17 +00003392 if (define->dflags & IS_NOT_NULLABLE)
Daniel Veillard4c004142003-10-07 11:33:24 +00003393 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003394 switch (define->type) {
3395 case XML_RELAXNG_EMPTY:
3396 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00003397 ret = 1;
3398 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003399 case XML_RELAXNG_NOOP:
3400 case XML_RELAXNG_DEF:
3401 case XML_RELAXNG_REF:
3402 case XML_RELAXNG_EXTERNALREF:
3403 case XML_RELAXNG_PARENTREF:
3404 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003405 ret = xmlRelaxNGIsNullable(define->content);
3406 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003407 case XML_RELAXNG_EXCEPT:
3408 case XML_RELAXNG_NOT_ALLOWED:
3409 case XML_RELAXNG_ELEMENT:
3410 case XML_RELAXNG_DATATYPE:
3411 case XML_RELAXNG_PARAM:
3412 case XML_RELAXNG_VALUE:
3413 case XML_RELAXNG_LIST:
3414 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003415 ret = 0;
3416 break;
3417 case XML_RELAXNG_CHOICE:{
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 != 0)
3423 goto done;
3424 list = list->next;
3425 }
3426 ret = 0;
3427 break;
3428 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003429 case XML_RELAXNG_START:
3430 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00003431 case XML_RELAXNG_GROUP:{
3432 xmlRelaxNGDefinePtr list = define->content;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003433
Daniel Veillard4c004142003-10-07 11:33:24 +00003434 while (list != NULL) {
3435 ret = xmlRelaxNGIsNullable(list);
3436 if (ret != 1)
3437 goto done;
3438 list = list->next;
3439 }
3440 return (1);
3441 }
3442 default:
3443 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003444 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003445 done:
Daniel Veillardfd573f12003-03-16 17:52:32 +00003446 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003447 define->dflags |= IS_NOT_NULLABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00003448 if (ret == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00003449 define->dflags |= IS_NULLABLE;
3450 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003451}
3452
3453/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00003454 * xmlRelaxNGIsBlank:
3455 * @str: a string
3456 *
3457 * Check if a string is ignorable c.f. 4.2. Whitespace
3458 *
3459 * Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3460 */
3461static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003462xmlRelaxNGIsBlank(xmlChar * str)
3463{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003464 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003465 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003466 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00003467 if (!(IS_BLANK_CH(*str)))
Daniel Veillard4c004142003-10-07 11:33:24 +00003468 return (0);
3469 str++;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003470 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003471 return (1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003472}
3473
Daniel Veillard6eadf632003-01-23 18:29:16 +00003474/**
3475 * xmlRelaxNGGetDataTypeLibrary:
3476 * @ctxt: a Relax-NG parser context
3477 * @node: the current data or value element
3478 *
3479 * Applies algorithm from 4.3. datatypeLibrary attribute
3480 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003481 * Returns the datatypeLibrary value or NULL if not found
Daniel Veillard6eadf632003-01-23 18:29:16 +00003482 */
3483static xmlChar *
3484xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00003485 xmlNodePtr node)
3486{
Daniel Veillard6eadf632003-01-23 18:29:16 +00003487 xmlChar *ret, *escape;
3488
Daniel Veillardd44b9362009-09-07 12:15:08 +02003489 if (node == NULL)
3490 return(NULL);
3491
Daniel Veillard6eadf632003-01-23 18:29:16 +00003492 if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003493 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3494 if (ret != NULL) {
3495 if (ret[0] == 0) {
3496 xmlFree(ret);
3497 return (NULL);
3498 }
3499 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3500 if (escape == NULL) {
3501 return (ret);
3502 }
3503 xmlFree(ret);
3504 return (escape);
3505 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00003506 }
3507 node = node->parent;
3508 while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003509 ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3510 if (ret != NULL) {
3511 if (ret[0] == 0) {
3512 xmlFree(ret);
3513 return (NULL);
3514 }
3515 escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3516 if (escape == NULL) {
3517 return (ret);
3518 }
3519 xmlFree(ret);
3520 return (escape);
3521 }
3522 node = node->parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00003523 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003524 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00003525}
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003526
3527/**
Daniel Veillardedc91922003-01-26 00:52:04 +00003528 * xmlRelaxNGParseValue:
3529 * @ctxt: a Relax-NG parser context
3530 * @node: the data node.
3531 *
3532 * parse the content of a RelaxNG value node.
3533 *
3534 * Returns the definition pointer or NULL in case of error
3535 */
3536static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003537xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3538{
Daniel Veillardedc91922003-01-26 00:52:04 +00003539 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillard5f1946a2003-03-31 16:38:16 +00003540 xmlRelaxNGTypeLibraryPtr lib = NULL;
Daniel Veillardedc91922003-01-26 00:52:04 +00003541 xmlChar *type;
3542 xmlChar *library;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003543 int success = 0;
Daniel Veillardedc91922003-01-26 00:52:04 +00003544
Daniel Veillardfd573f12003-03-16 17:52:32 +00003545 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00003546 if (def == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003547 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00003548 def->type = XML_RELAXNG_VALUE;
Daniel Veillardedc91922003-01-26 00:52:04 +00003549
3550 type = xmlGetProp(node, BAD_CAST "type");
3551 if (type != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003552 xmlRelaxNGNormExtSpace(type);
3553 if (xmlValidateNCName(type, 0)) {
3554 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3555 "value type '%s' is not an NCName\n", type, NULL);
3556 }
3557 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3558 if (library == NULL)
3559 library =
3560 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillardedc91922003-01-26 00:52:04 +00003561
Daniel Veillard4c004142003-10-07 11:33:24 +00003562 def->name = type;
3563 def->ns = library;
Daniel Veillardedc91922003-01-26 00:52:04 +00003564
Daniel Veillard4c004142003-10-07 11:33:24 +00003565 lib = (xmlRelaxNGTypeLibraryPtr)
3566 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3567 if (lib == NULL) {
3568 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3569 "Use of unregistered type library '%s'\n", library,
3570 NULL);
3571 def->data = NULL;
3572 } else {
3573 def->data = lib;
3574 if (lib->have == NULL) {
3575 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3576 "Internal error with type library '%s': no 'have'\n",
3577 library, NULL);
3578 } else {
3579 success = lib->have(lib->data, def->name);
3580 if (success != 1) {
3581 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3582 "Error type '%s' is not exported by type library '%s'\n",
3583 def->name, library);
3584 }
3585 }
3586 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003587 }
3588 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003589 def->value = xmlStrdup(BAD_CAST "");
Daniel Veillard39eb88b2003-03-11 11:21:28 +00003590 } else if (((node->children->type != XML_TEXT_NODE) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00003591 (node->children->type != XML_CDATA_SECTION_NODE)) ||
3592 (node->children->next != NULL)) {
3593 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3594 "Expecting a single text value for <value>content\n",
3595 NULL, NULL);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003596 } else if (def != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003597 def->value = xmlNodeGetContent(node);
3598 if (def->value == NULL) {
3599 xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3600 "Element <value> has no content\n", NULL, NULL);
3601 } else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3602 void *val = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003603
Daniel Veillard4c004142003-10-07 11:33:24 +00003604 success =
3605 lib->check(lib->data, def->name, def->value, &val, node);
3606 if (success != 1) {
3607 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3608 "Value '%s' is not acceptable for type '%s'\n",
3609 def->value, def->name);
3610 } else {
3611 if (val != NULL)
3612 def->attrs = val;
3613 }
3614 }
Daniel Veillardedc91922003-01-26 00:52:04 +00003615 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003616 return (def);
Daniel Veillardedc91922003-01-26 00:52:04 +00003617}
3618
3619/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003620 * xmlRelaxNGParseData:
3621 * @ctxt: a Relax-NG parser context
3622 * @node: the data node.
3623 *
3624 * parse the content of a RelaxNG data node.
3625 *
3626 * Returns the definition pointer or NULL in case of error
3627 */
3628static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00003629xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3630{
Daniel Veillard14b56432006-03-09 18:41:40 +00003631 xmlRelaxNGDefinePtr def = NULL, except;
Daniel Veillard8fe98712003-02-19 00:19:14 +00003632 xmlRelaxNGDefinePtr param, lastparam = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003633 xmlRelaxNGTypeLibraryPtr lib;
3634 xmlChar *type;
3635 xmlChar *library;
3636 xmlNodePtr content;
3637 int tmp;
3638
3639 type = xmlGetProp(node, BAD_CAST "type");
3640 if (type == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003641 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3642 NULL);
3643 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003644 }
Daniel Veillardd2298792003-02-14 16:54:11 +00003645 xmlRelaxNGNormExtSpace(type);
3646 if (xmlValidateNCName(type, 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003647 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3648 "data type '%s' is not an NCName\n", type, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00003649 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003650 library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3651 if (library == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00003652 library =
3653 xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003654
Daniel Veillardfd573f12003-03-16 17:52:32 +00003655 def = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003656 if (def == NULL) {
Elliott Hughesecdab2a2022-02-23 14:33:50 -08003657 xmlFree(library);
Daniel Veillard4c004142003-10-07 11:33:24 +00003658 xmlFree(type);
3659 return (NULL);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003660 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003661 def->type = XML_RELAXNG_DATATYPE;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003662 def->name = type;
3663 def->ns = library;
3664
3665 lib = (xmlRelaxNGTypeLibraryPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00003666 xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003667 if (lib == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003668 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3669 "Use of unregistered type library '%s'\n", library,
3670 NULL);
3671 def->data = NULL;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003672 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003673 def->data = lib;
3674 if (lib->have == NULL) {
3675 xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3676 "Internal error with type library '%s': no 'have'\n",
3677 library, NULL);
3678 } else {
3679 tmp = lib->have(lib->data, def->name);
3680 if (tmp != 1) {
3681 xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3682 "Error type '%s' is not exported by type library '%s'\n",
3683 def->name, library);
3684 } else
3685 if ((xmlStrEqual
3686 (library,
3687 BAD_CAST
3688 "http://www.w3.org/2001/XMLSchema-datatypes"))
3689 && ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3690 || (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3691 ctxt->idref = 1;
3692 }
3693 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003694 }
3695 content = node->children;
Daniel Veillard416589a2003-02-17 17:25:42 +00003696
3697 /*
3698 * Handle optional params
3699 */
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003700 while (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003701 if (!xmlStrEqual(content->name, BAD_CAST "param"))
3702 break;
3703 if (xmlStrEqual(library,
3704 BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3705 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3706 "Type library '%s' does not allow type parameters\n",
3707 library, NULL);
3708 content = content->next;
3709 while ((content != NULL) &&
3710 (xmlStrEqual(content->name, BAD_CAST "param")))
3711 content = content->next;
3712 } else {
3713 param = xmlRelaxNGNewDefine(ctxt, node);
3714 if (param != NULL) {
3715 param->type = XML_RELAXNG_PARAM;
3716 param->name = xmlGetProp(content, BAD_CAST "name");
3717 if (param->name == NULL) {
3718 xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3719 "param has no name\n", NULL, NULL);
3720 }
3721 param->value = xmlNodeGetContent(content);
3722 if (lastparam == NULL) {
3723 def->attrs = lastparam = param;
3724 } else {
3725 lastparam->next = param;
3726 lastparam = param;
3727 }
3728 if (lib != NULL) {
3729 }
3730 }
3731 content = content->next;
3732 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003733 }
Daniel Veillard416589a2003-02-17 17:25:42 +00003734 /*
3735 * Handle optional except
3736 */
Daniel Veillard4c004142003-10-07 11:33:24 +00003737 if ((content != NULL)
3738 && (xmlStrEqual(content->name, BAD_CAST "except"))) {
3739 xmlNodePtr child;
Daniel Veillard14b56432006-03-09 18:41:40 +00003740 xmlRelaxNGDefinePtr tmp2, last = NULL;
Daniel Veillard416589a2003-02-17 17:25:42 +00003741
Daniel Veillard4c004142003-10-07 11:33:24 +00003742 except = xmlRelaxNGNewDefine(ctxt, node);
3743 if (except == NULL) {
3744 return (def);
3745 }
3746 except->type = XML_RELAXNG_EXCEPT;
3747 child = content->children;
Daniel Veillard14b56432006-03-09 18:41:40 +00003748 def->content = except;
Daniel Veillard4c004142003-10-07 11:33:24 +00003749 if (child == NULL) {
3750 xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3751 "except has no content\n", NULL, NULL);
3752 }
3753 while (child != NULL) {
3754 tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3755 if (tmp2 != NULL) {
Daniel Veillard14b56432006-03-09 18:41:40 +00003756 if (last == NULL) {
3757 except->content = last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003758 } else {
Daniel Veillard14b56432006-03-09 18:41:40 +00003759 last->next = tmp2;
3760 last = tmp2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003761 }
3762 }
3763 child = child->next;
3764 }
3765 content = content->next;
Daniel Veillard416589a2003-02-17 17:25:42 +00003766 }
3767 /*
3768 * Check there is no unhandled data
3769 */
3770 if (content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003771 xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3772 "Element data has unexpected content %s\n",
3773 content->name, NULL);
Daniel Veillard416589a2003-02-17 17:25:42 +00003774 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003775
Daniel Veillard4c004142003-10-07 11:33:24 +00003776 return (def);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00003777}
3778
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003779static const xmlChar *invalidName = BAD_CAST "\1";
3780
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003781/**
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003782 * xmlRelaxNGCompareNameClasses:
3783 * @defs1: the first element/attribute defs
3784 * @defs2: the second element/attribute defs
3785 * @name: the restriction on the name
3786 * @ns: the restriction on the namespace
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003787 *
3788 * Compare the 2 lists of element definitions. The comparison is
3789 * that if both lists do not accept the same QNames, it returns 1
3790 * If the 2 lists can accept the same QName the comparison returns 0
3791 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003792 * Returns 1 distinct, 0 if equal
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003793 */
3794static int
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003795xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
Daniel Veillard4c004142003-10-07 11:33:24 +00003796 xmlRelaxNGDefinePtr def2)
3797{
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003798 int ret = 1;
3799 xmlNode node;
3800 xmlNs ns;
3801 xmlRelaxNGValidCtxt ctxt;
Daniel Veillard4c004142003-10-07 11:33:24 +00003802
Daniel Veillard42f12e92003-03-07 18:32:59 +00003803 memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3804
Daniel Veillardb30ca312005-09-04 13:50:03 +00003805 ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3806
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003807 if ((def1->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003808 (def1->type == XML_RELAXNG_ATTRIBUTE)) {
3809 if (def2->type == XML_RELAXNG_TEXT)
3810 return (1);
3811 if (def1->name != NULL) {
3812 node.name = def1->name;
3813 } else {
3814 node.name = invalidName;
3815 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003816 if (def1->ns != NULL) {
3817 if (def1->ns[0] == 0) {
3818 node.ns = NULL;
3819 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003820 node.ns = &ns;
Daniel Veillard4c004142003-10-07 11:33:24 +00003821 ns.href = def1->ns;
3822 }
3823 } else {
William M. Bracka74a6ff2004-04-02 14:03:22 +00003824 node.ns = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00003825 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003826 if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003827 if (def1->nameClass != NULL) {
3828 ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3829 } else {
3830 ret = 0;
3831 }
3832 } else {
3833 ret = 1;
3834 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003835 } else if (def1->type == XML_RELAXNG_TEXT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003836 if (def2->type == XML_RELAXNG_TEXT)
3837 return (0);
3838 return (1);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003839 } else if (def1->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard2fab2352015-03-16 08:38:36 +08003840 ret = xmlRelaxNGCompareNameClasses(def1->content, def2);
3841 if (ret == 0)
3842 ret = 1;
3843 else if (ret == 1)
3844 ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003845 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003846 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003847 }
3848 if (ret == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003849 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003850 if ((def2->type == XML_RELAXNG_ELEMENT) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00003851 (def2->type == XML_RELAXNG_ATTRIBUTE)) {
3852 if (def2->name != NULL) {
3853 node.name = def2->name;
3854 } else {
3855 node.name = invalidName;
3856 }
3857 node.ns = &ns;
3858 if (def2->ns != NULL) {
3859 if (def2->ns[0] == 0) {
3860 node.ns = NULL;
3861 } else {
3862 ns.href = def2->ns;
3863 }
3864 } else {
3865 ns.href = invalidName;
3866 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00003867 if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003868 if (def2->nameClass != NULL) {
3869 ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3870 } else {
3871 ret = 0;
3872 }
3873 } else {
3874 ret = 1;
3875 }
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003876 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00003877 TODO ret = 0;
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003878 }
3879
Daniel Veillard4c004142003-10-07 11:33:24 +00003880 return (ret);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003881}
3882
3883/**
3884 * xmlRelaxNGCompareElemDefLists:
3885 * @ctxt: a Relax-NG parser context
3886 * @defs1: the first list of element/attribute defs
3887 * @defs2: the second list of element/attribute defs
3888 *
3889 * Compare the 2 lists of element or attribute definitions. The comparison
3890 * is that if both lists do not accept the same QNames, it returns 1
3891 * If the 2 lists can accept the same QName the comparison returns 0
3892 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07003893 * Returns 1 distinct, 0 if equal
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00003894 */
3895static int
Daniel Veillard4c004142003-10-07 11:33:24 +00003896xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3897 ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3898 xmlRelaxNGDefinePtr * def2)
3899{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003900 xmlRelaxNGDefinePtr *basedef2 = def2;
Daniel Veillard4c004142003-10-07 11:33:24 +00003901
Daniel Veillard154877e2003-01-30 12:17:05 +00003902 if ((def1 == NULL) || (def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003903 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003904 if ((*def1 == NULL) || (*def2 == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00003905 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003906 while (*def1 != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003907 while ((*def2) != NULL) {
3908 if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3909 return (0);
3910 def2++;
3911 }
3912 def2 = basedef2;
3913 def1++;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003914 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003915 return (1);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003916}
3917
3918/**
Daniel Veillardce192eb2003-04-16 15:58:05 +00003919 * xmlRelaxNGGenerateAttributes:
3920 * @ctxt: a Relax-NG parser context
3921 * @def: the definition definition
3922 *
3923 * Check if the definition can only generate attributes
3924 *
3925 * Returns 1 if yes, 0 if no and -1 in case of error.
3926 */
3927static int
3928xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00003929 xmlRelaxNGDefinePtr def)
3930{
Daniel Veillardce192eb2003-04-16 15:58:05 +00003931 xmlRelaxNGDefinePtr parent, cur, tmp;
3932
3933 /*
3934 * Don't run that check in case of error. Infinite recursion
3935 * becomes possible.
3936 */
3937 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00003938 return (-1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003939
3940 parent = NULL;
3941 cur = def;
3942 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00003943 if ((cur->type == XML_RELAXNG_ELEMENT) ||
3944 (cur->type == XML_RELAXNG_TEXT) ||
3945 (cur->type == XML_RELAXNG_DATATYPE) ||
3946 (cur->type == XML_RELAXNG_PARAM) ||
3947 (cur->type == XML_RELAXNG_LIST) ||
3948 (cur->type == XML_RELAXNG_VALUE) ||
3949 (cur->type == XML_RELAXNG_EMPTY))
3950 return (0);
3951 if ((cur->type == XML_RELAXNG_CHOICE) ||
3952 (cur->type == XML_RELAXNG_INTERLEAVE) ||
3953 (cur->type == XML_RELAXNG_GROUP) ||
3954 (cur->type == XML_RELAXNG_ONEORMORE) ||
3955 (cur->type == XML_RELAXNG_ZEROORMORE) ||
3956 (cur->type == XML_RELAXNG_OPTIONAL) ||
3957 (cur->type == XML_RELAXNG_PARENTREF) ||
3958 (cur->type == XML_RELAXNG_EXTERNALREF) ||
3959 (cur->type == XML_RELAXNG_REF) ||
3960 (cur->type == XML_RELAXNG_DEF)) {
3961 if (cur->content != NULL) {
3962 parent = cur;
3963 cur = cur->content;
3964 tmp = cur;
3965 while (tmp != NULL) {
3966 tmp->parent = parent;
3967 tmp = tmp->next;
3968 }
3969 continue;
3970 }
3971 }
3972 if (cur == def)
3973 break;
3974 if (cur->next != NULL) {
3975 cur = cur->next;
3976 continue;
3977 }
3978 do {
3979 cur = cur->parent;
3980 if (cur == NULL)
3981 break;
3982 if (cur == def)
3983 return (1);
3984 if (cur->next != NULL) {
3985 cur = cur->next;
3986 break;
3987 }
3988 } while (cur != NULL);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003989 }
Daniel Veillard4c004142003-10-07 11:33:24 +00003990 return (1);
Daniel Veillardce192eb2003-04-16 15:58:05 +00003991}
Daniel Veillard4c004142003-10-07 11:33:24 +00003992
Daniel Veillardce192eb2003-04-16 15:58:05 +00003993/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003994 * xmlRelaxNGGetElements:
3995 * @ctxt: a Relax-NG parser context
Daniel Veillard44e1dd02003-02-21 23:23:28 +00003996 * @def: the definition definition
Elliott Hughes7fbecab2019-01-10 16:42:03 -08003997 * @eora: gather elements (0), attributes (1) or elements and text (2)
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00003998 *
3999 * Compute the list of top elements a definition can generate
4000 *
4001 * Returns a list of elements or NULL if none was found.
4002 */
4003static xmlRelaxNGDefinePtr *
4004xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004005 xmlRelaxNGDefinePtr def, int eora)
4006{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004007 xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004008 int len = 0;
4009 int max = 0;
4010
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004011 /*
4012 * Don't run that check in case of error. Infinite recursion
4013 * becomes possible.
4014 */
4015 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004016 return (NULL);
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004017
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004018 parent = NULL;
4019 cur = def;
4020 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004021 if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
4022 (cur->type == XML_RELAXNG_TEXT))) ||
Elliott Hughes7fbecab2019-01-10 16:42:03 -08004023 ((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE)) ||
4024 ((eora == 2) && ((cur->type == XML_RELAXNG_DATATYPE) ||
4025 (cur->type == XML_RELAXNG_ELEMENT) ||
4026 (cur->type == XML_RELAXNG_LIST) ||
4027 (cur->type == XML_RELAXNG_TEXT) ||
4028 (cur->type == XML_RELAXNG_VALUE)))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004029 if (ret == NULL) {
4030 max = 10;
4031 ret = (xmlRelaxNGDefinePtr *)
4032 xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
4033 if (ret == NULL) {
4034 xmlRngPErrMemory(ctxt, "getting element list\n");
4035 return (NULL);
4036 }
4037 } else if (max <= len) {
Daniel Veillard079f6a72004-09-23 13:15:03 +00004038 xmlRelaxNGDefinePtr *temp;
4039
Daniel Veillard4c004142003-10-07 11:33:24 +00004040 max *= 2;
Daniel Veillard079f6a72004-09-23 13:15:03 +00004041 temp = xmlRealloc(ret,
Daniel Veillard4c004142003-10-07 11:33:24 +00004042 (max + 1) * sizeof(xmlRelaxNGDefinePtr));
Daniel Veillard079f6a72004-09-23 13:15:03 +00004043 if (temp == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004044 xmlRngPErrMemory(ctxt, "getting element list\n");
Daniel Veillard079f6a72004-09-23 13:15:03 +00004045 xmlFree(ret);
Daniel Veillard4c004142003-10-07 11:33:24 +00004046 return (NULL);
4047 }
Daniel Veillard079f6a72004-09-23 13:15:03 +00004048 ret = temp;
Daniel Veillard4c004142003-10-07 11:33:24 +00004049 }
4050 ret[len++] = cur;
4051 ret[len] = NULL;
4052 } else if ((cur->type == XML_RELAXNG_CHOICE) ||
4053 (cur->type == XML_RELAXNG_INTERLEAVE) ||
4054 (cur->type == XML_RELAXNG_GROUP) ||
4055 (cur->type == XML_RELAXNG_ONEORMORE) ||
4056 (cur->type == XML_RELAXNG_ZEROORMORE) ||
4057 (cur->type == XML_RELAXNG_OPTIONAL) ||
4058 (cur->type == XML_RELAXNG_PARENTREF) ||
4059 (cur->type == XML_RELAXNG_REF) ||
William M. Brack236c8c02004-03-20 11:32:36 +00004060 (cur->type == XML_RELAXNG_DEF) ||
4061 (cur->type == XML_RELAXNG_EXTERNALREF)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004062 /*
4063 * Don't go within elements or attributes or string values.
4064 * Just gather the element top list
4065 */
4066 if (cur->content != NULL) {
4067 parent = cur;
4068 cur = cur->content;
4069 tmp = cur;
4070 while (tmp != NULL) {
4071 tmp->parent = parent;
4072 tmp = tmp->next;
4073 }
4074 continue;
4075 }
4076 }
4077 if (cur == def)
4078 break;
4079 if (cur->next != NULL) {
4080 cur = cur->next;
4081 continue;
4082 }
4083 do {
4084 cur = cur->parent;
4085 if (cur == NULL)
4086 break;
4087 if (cur == def)
4088 return (ret);
4089 if (cur->next != NULL) {
4090 cur = cur->next;
4091 break;
4092 }
4093 } while (cur != NULL);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004094 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004095 return (ret);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004096}
Daniel Veillard4c004142003-10-07 11:33:24 +00004097
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004098/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004099 * xmlRelaxNGCheckChoiceDeterminism:
4100 * @ctxt: a Relax-NG parser context
4101 * @def: the choice definition
4102 *
4103 * Also used to find indeterministic pattern in choice
4104 */
4105static void
4106xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004107 xmlRelaxNGDefinePtr def)
4108{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004109 xmlRelaxNGDefinePtr **list;
4110 xmlRelaxNGDefinePtr cur;
4111 int nbchild = 0, i, j, ret;
4112 int is_nullable = 0;
4113 int is_indeterminist = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004114 xmlHashTablePtr triage = NULL;
4115 int is_triable = 1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004116
Daniel Veillard4c004142003-10-07 11:33:24 +00004117 if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4118 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004119
Daniel Veillarde063f482003-03-21 16:53:17 +00004120 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004121 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004122
Daniel Veillardfd573f12003-03-16 17:52:32 +00004123 /*
4124 * Don't run that check in case of error. Infinite recursion
4125 * becomes possible.
4126 */
4127 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004128 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004129
4130 is_nullable = xmlRelaxNGIsNullable(def);
4131
4132 cur = def->content;
4133 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004134 nbchild++;
4135 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004136 }
4137
4138 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004139 sizeof(xmlRelaxNGDefinePtr
4140 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004141 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004142 xmlRngPErrMemory(ctxt, "building choice\n");
4143 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004144 }
4145 i = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004146 /*
4147 * a bit strong but safe
4148 */
4149 if (is_nullable == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004150 triage = xmlHashCreate(10);
Daniel Veillarde063f482003-03-21 16:53:17 +00004151 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004152 is_triable = 0;
Daniel Veillarde063f482003-03-21 16:53:17 +00004153 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004154 cur = def->content;
4155 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004156 list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4157 if ((list[i] == NULL) || (list[i][0] == NULL)) {
4158 is_triable = 0;
4159 } else if (is_triable == 1) {
4160 xmlRelaxNGDefinePtr *tmp;
4161 int res;
Daniel Veillarde063f482003-03-21 16:53:17 +00004162
Daniel Veillard4c004142003-10-07 11:33:24 +00004163 tmp = list[i];
4164 while ((*tmp != NULL) && (is_triable == 1)) {
4165 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4166 res = xmlHashAddEntry2(triage,
4167 BAD_CAST "#text", NULL,
4168 (void *) cur);
4169 if (res != 0)
4170 is_triable = -1;
4171 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4172 ((*tmp)->name != NULL)) {
4173 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4174 res = xmlHashAddEntry2(triage,
4175 (*tmp)->name, NULL,
4176 (void *) cur);
4177 else
4178 res = xmlHashAddEntry2(triage,
4179 (*tmp)->name, (*tmp)->ns,
4180 (void *) cur);
4181 if (res != 0)
4182 is_triable = -1;
4183 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4184 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4185 res = xmlHashAddEntry2(triage,
4186 BAD_CAST "#any", NULL,
4187 (void *) cur);
4188 else
4189 res = xmlHashAddEntry2(triage,
4190 BAD_CAST "#any", (*tmp)->ns,
4191 (void *) cur);
4192 if (res != 0)
4193 is_triable = -1;
4194 } else {
4195 is_triable = -1;
4196 }
4197 tmp++;
4198 }
4199 }
4200 i++;
4201 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004202 }
4203
Daniel Veillard4c004142003-10-07 11:33:24 +00004204 for (i = 0; i < nbchild; i++) {
4205 if (list[i] == NULL)
4206 continue;
4207 for (j = 0; j < i; j++) {
4208 if (list[j] == NULL)
4209 continue;
4210 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4211 if (ret == 0) {
4212 is_indeterminist = 1;
4213 }
4214 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004215 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004216 for (i = 0; i < nbchild; i++) {
4217 if (list[i] != NULL)
4218 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004219 }
4220
4221 xmlFree(list);
4222 if (is_indeterminist) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004223 def->dflags |= IS_INDETERMINIST;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004224 }
Daniel Veillarde063f482003-03-21 16:53:17 +00004225 if (is_triable == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004226 def->dflags |= IS_TRIABLE;
4227 def->data = triage;
Daniel Veillarde063f482003-03-21 16:53:17 +00004228 } else if (triage != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004229 xmlHashFree(triage, NULL);
Daniel Veillarde063f482003-03-21 16:53:17 +00004230 }
4231 def->dflags |= IS_PROCESSED;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004232}
4233
4234/**
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004235 * xmlRelaxNGCheckGroupAttrs:
4236 * @ctxt: a Relax-NG parser context
4237 * @def: the group definition
4238 *
4239 * Detects violations of rule 7.3
4240 */
4241static void
4242xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00004243 xmlRelaxNGDefinePtr def)
4244{
Daniel Veillardfd573f12003-03-16 17:52:32 +00004245 xmlRelaxNGDefinePtr **list;
4246 xmlRelaxNGDefinePtr cur;
4247 int nbchild = 0, i, j, ret;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004248
4249 if ((def == NULL) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00004250 ((def->type != XML_RELAXNG_GROUP) &&
4251 (def->type != XML_RELAXNG_ELEMENT)))
4252 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004253
Daniel Veillarde063f482003-03-21 16:53:17 +00004254 if (def->dflags & IS_PROCESSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004255 return;
Daniel Veillarde063f482003-03-21 16:53:17 +00004256
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004257 /*
4258 * Don't run that check in case of error. Infinite recursion
4259 * becomes possible.
4260 */
4261 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004262 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004263
Daniel Veillardfd573f12003-03-16 17:52:32 +00004264 cur = def->attrs;
4265 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004266 nbchild++;
4267 cur = cur->next;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004268 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004269 cur = def->content;
4270 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004271 nbchild++;
4272 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004273 }
4274
4275 list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
Daniel Veillard4c004142003-10-07 11:33:24 +00004276 sizeof(xmlRelaxNGDefinePtr
4277 *));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004278 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004279 xmlRngPErrMemory(ctxt, "building group\n");
4280 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004281 }
4282 i = 0;
4283 cur = def->attrs;
4284 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004285 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4286 i++;
4287 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004288 }
4289 cur = def->content;
4290 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004291 list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4292 i++;
4293 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004294 }
4295
Daniel Veillard4c004142003-10-07 11:33:24 +00004296 for (i = 0; i < nbchild; i++) {
4297 if (list[i] == NULL)
4298 continue;
4299 for (j = 0; j < i; j++) {
4300 if (list[j] == NULL)
4301 continue;
4302 ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4303 if (ret == 0) {
4304 xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4305 "Attributes conflicts in group\n", NULL, NULL);
4306 }
4307 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004308 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004309 for (i = 0; i < nbchild; i++) {
4310 if (list[i] != NULL)
4311 xmlFree(list[i]);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004312 }
4313
4314 xmlFree(list);
Daniel Veillarde063f482003-03-21 16:53:17 +00004315 def->dflags |= IS_PROCESSED;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004316}
4317
4318/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00004319 * xmlRelaxNGComputeInterleaves:
4320 * @def: the interleave definition
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004321 * @ctxt: a Relax-NG parser context
Daniel Veillardfd573f12003-03-16 17:52:32 +00004322 * @name: the definition name
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004323 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00004324 * A lot of work for preprocessing interleave definitions
4325 * is potentially needed to get a decent execution speed at runtime
4326 * - trying to get a total order on the element nodes generated
4327 * by the interleaves, order the list of interleave definitions
4328 * following that order.
4329 * - if <text/> is used to handle mixed content, it is better to
4330 * flag this in the define and simplify the runtime checking
4331 * algorithm
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004332 */
4333static void
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01004334xmlRelaxNGComputeInterleaves(void *payload, void *data,
4335 const xmlChar * name ATTRIBUTE_UNUSED)
Daniel Veillard4c004142003-10-07 11:33:24 +00004336{
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01004337 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4338 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004339 xmlRelaxNGDefinePtr cur, *tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004340
Daniel Veillardfd573f12003-03-16 17:52:32 +00004341 xmlRelaxNGPartitionPtr partitions = NULL;
4342 xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4343 xmlRelaxNGInterleaveGroupPtr group;
Daniel Veillard4c004142003-10-07 11:33:24 +00004344 int i, j, ret, res;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004345 int nbgroups = 0;
4346 int nbchild = 0;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004347 int is_mixed = 0;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004348 int is_determinist = 1;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004349
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004350 /*
4351 * Don't run that check in case of error. Infinite recursion
4352 * becomes possible.
4353 */
4354 if (ctxt->nbErrors != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004355 return;
Daniel Veillard44e1dd02003-02-21 23:23:28 +00004356
Daniel Veillardfd573f12003-03-16 17:52:32 +00004357#ifdef DEBUG_INTERLEAVE
4358 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00004359 "xmlRelaxNGComputeInterleaves(%s)\n", name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004360#endif
4361 cur = def->content;
4362 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004363 nbchild++;
4364 cur = cur->next;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004365 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004366
Daniel Veillardfd573f12003-03-16 17:52:32 +00004367#ifdef DEBUG_INTERLEAVE
4368 xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4369#endif
4370 groups = (xmlRelaxNGInterleaveGroupPtr *)
Daniel Veillard4c004142003-10-07 11:33:24 +00004371 xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004372 if (groups == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004373 goto error;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004374 cur = def->content;
4375 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004376 groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4377 xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4378 if (groups[nbgroups] == NULL)
4379 goto error;
4380 if (cur->type == XML_RELAXNG_TEXT)
4381 is_mixed++;
4382 groups[nbgroups]->rule = cur;
Elliott Hughes7fbecab2019-01-10 16:42:03 -08004383 groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 2);
Daniel Veillard4c004142003-10-07 11:33:24 +00004384 groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4385 nbgroups++;
4386 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004387 }
4388#ifdef DEBUG_INTERLEAVE
4389 xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4390#endif
4391
4392 /*
4393 * Let's check that all rules makes a partitions according to 7.4
4394 */
4395 partitions = (xmlRelaxNGPartitionPtr)
Daniel Veillard4c004142003-10-07 11:33:24 +00004396 xmlMalloc(sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004397 if (partitions == NULL)
4398 goto error;
Daniel Veillard20863822003-03-22 17:51:47 +00004399 memset(partitions, 0, sizeof(xmlRelaxNGPartition));
Daniel Veillardfd573f12003-03-16 17:52:32 +00004400 partitions->nbgroups = nbgroups;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004401 partitions->triage = xmlHashCreate(nbgroups);
Daniel Veillard4c004142003-10-07 11:33:24 +00004402 for (i = 0; i < nbgroups; i++) {
4403 group = groups[i];
4404 for (j = i + 1; j < nbgroups; j++) {
4405 if (groups[j] == NULL)
4406 continue;
4407
4408 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4409 groups[j]->defs);
4410 if (ret == 0) {
4411 xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4412 "Element or text conflicts in interleave\n",
4413 NULL, NULL);
4414 }
4415 ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4416 groups[j]->attrs);
4417 if (ret == 0) {
4418 xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4419 "Attributes conflicts in interleave\n", NULL,
4420 NULL);
4421 }
4422 }
4423 tmp = group->defs;
4424 if ((tmp != NULL) && (*tmp != NULL)) {
4425 while (*tmp != NULL) {
4426 if ((*tmp)->type == XML_RELAXNG_TEXT) {
4427 res = xmlHashAddEntry2(partitions->triage,
4428 BAD_CAST "#text", NULL,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004429 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004430 if (res != 0)
4431 is_determinist = -1;
4432 } else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4433 ((*tmp)->name != NULL)) {
4434 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4435 res = xmlHashAddEntry2(partitions->triage,
4436 (*tmp)->name, NULL,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004437 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004438 else
4439 res = xmlHashAddEntry2(partitions->triage,
4440 (*tmp)->name, (*tmp)->ns,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004441 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004442 if (res != 0)
4443 is_determinist = -1;
4444 } else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4445 if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4446 res = xmlHashAddEntry2(partitions->triage,
4447 BAD_CAST "#any", NULL,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004448 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004449 else
4450 res = xmlHashAddEntry2(partitions->triage,
4451 BAD_CAST "#any", (*tmp)->ns,
Nick Wellnhoferd422b952017-10-09 13:37:42 +02004452 (void *) (ptrdiff_t) (i + 1));
Daniel Veillard4c004142003-10-07 11:33:24 +00004453 if ((*tmp)->nameClass != NULL)
4454 is_determinist = 2;
4455 if (res != 0)
4456 is_determinist = -1;
4457 } else {
4458 is_determinist = -1;
4459 }
4460 tmp++;
4461 }
4462 } else {
4463 is_determinist = 0;
4464 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004465 }
4466 partitions->groups = groups;
4467
4468 /*
4469 * and save the partition list back in the def
4470 */
4471 def->data = partitions;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00004472 if (is_mixed != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00004473 def->dflags |= IS_MIXED;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004474 if (is_determinist == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00004475 partitions->flags = IS_DETERMINIST;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00004476 if (is_determinist == 2)
Daniel Veillard4c004142003-10-07 11:33:24 +00004477 partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004478 return;
4479
Daniel Veillard4c004142003-10-07 11:33:24 +00004480 error:
4481 xmlRngPErrMemory(ctxt, "in interleave computation\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004482 if (groups != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004483 for (i = 0; i < nbgroups; i++)
4484 if (groups[i] != NULL) {
4485 if (groups[i]->defs != NULL)
4486 xmlFree(groups[i]->defs);
4487 xmlFree(groups[i]);
4488 }
4489 xmlFree(groups);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004490 }
4491 xmlRelaxNGFreePartition(partitions);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004492}
4493
4494/**
4495 * xmlRelaxNGParseInterleave:
4496 * @ctxt: a Relax-NG parser context
4497 * @node: the data node.
4498 *
4499 * parse the content of a RelaxNG interleave node.
4500 *
4501 * Returns the definition pointer or NULL in case of error
4502 */
4503static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004504xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4505{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004506 xmlRelaxNGDefinePtr def = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004507 xmlRelaxNGDefinePtr last = NULL, cur;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004508 xmlNodePtr child;
4509
Daniel Veillardfd573f12003-03-16 17:52:32 +00004510 def = xmlRelaxNGNewDefine(ctxt, node);
4511 if (def == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004512 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004513 }
4514 def->type = XML_RELAXNG_INTERLEAVE;
4515
4516 if (ctxt->interleaves == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00004517 ctxt->interleaves = xmlHashCreate(10);
Daniel Veillardfd573f12003-03-16 17:52:32 +00004518 if (ctxt->interleaves == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004519 xmlRngPErrMemory(ctxt, "create interleaves\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00004520 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004521 char name[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00004522
Daniel Veillard4c004142003-10-07 11:33:24 +00004523 snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4524 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4525 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4526 "Failed to add %s to hash table\n",
4527 (const xmlChar *) name, NULL);
4528 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004529 }
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004530 child = node->children;
Daniel Veillardd2298792003-02-14 16:54:11 +00004531 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004532 xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4533 "Element interleave is empty\n", NULL, NULL);
Daniel Veillard1564e6e2003-03-15 21:30:25 +00004534 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004535 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004536 if (IS_RELAXNG(child, "element")) {
4537 cur = xmlRelaxNGParseElement(ctxt, child);
4538 } else {
4539 cur = xmlRelaxNGParsePattern(ctxt, child);
4540 }
4541 if (cur != NULL) {
4542 cur->parent = def;
4543 if (last == NULL) {
4544 def->content = last = cur;
4545 } else {
4546 last->next = cur;
4547 last = cur;
4548 }
4549 }
4550 child = child->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00004551 }
4552
Daniel Veillard4c004142003-10-07 11:33:24 +00004553 return (def);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00004554}
Daniel Veillard6eadf632003-01-23 18:29:16 +00004555
4556/**
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004557 * xmlRelaxNGParseInclude:
4558 * @ctxt: a Relax-NG parser context
4559 * @node: the include node
4560 *
4561 * Integrate the content of an include node in the current grammar
4562 *
4563 * Returns 0 in case of success or -1 in case of error
4564 */
4565static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004566xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4567{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004568 xmlRelaxNGIncludePtr incl;
4569 xmlNodePtr root;
4570 int ret = 0, tmp;
4571
Daniel Veillard807daf82004-02-22 22:13:27 +00004572 incl = node->psvi;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004573 if (incl == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004574 xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4575 "Include node has no data\n", NULL, NULL);
4576 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004577 }
4578 root = xmlDocGetRootElement(incl->doc);
4579 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004580 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4581 NULL, NULL);
4582 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004583 }
4584 if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004585 xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4586 "Include document root is not a grammar\n", NULL, NULL);
4587 return (-1);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004588 }
4589
4590 /*
4591 * Merge the definition from both the include and the internal list
4592 */
4593 if (root->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004594 tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4595 if (tmp != 0)
4596 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004597 }
4598 if (node->children != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004599 tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4600 if (tmp != 0)
4601 ret = -1;
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004602 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004603 return (ret);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004604}
4605
4606/**
Daniel Veillard276be4a2003-01-24 01:03:34 +00004607 * xmlRelaxNGParseDefine:
4608 * @ctxt: a Relax-NG parser context
4609 * @node: the define node
4610 *
4611 * parse the content of a RelaxNG define element node.
4612 *
Daniel Veillarde2a5a082003-02-02 14:35:17 +00004613 * Returns 0 in case of success or -1 in case of error
Daniel Veillard276be4a2003-01-24 01:03:34 +00004614 */
4615static int
Daniel Veillard4c004142003-10-07 11:33:24 +00004616xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4617{
Daniel Veillard276be4a2003-01-24 01:03:34 +00004618 xmlChar *name;
4619 int ret = 0, tmp;
4620 xmlRelaxNGDefinePtr def;
4621 const xmlChar *olddefine;
4622
4623 name = xmlGetProp(node, BAD_CAST "name");
4624 if (name == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004625 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4626 "define has no name\n", NULL, NULL);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004627 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004628 xmlRelaxNGNormExtSpace(name);
4629 if (xmlValidateNCName(name, 0)) {
4630 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4631 "define name '%s' is not an NCName\n", name, NULL);
4632 }
4633 def = xmlRelaxNGNewDefine(ctxt, node);
4634 if (def == NULL) {
4635 xmlFree(name);
4636 return (-1);
4637 }
4638 def->type = XML_RELAXNG_DEF;
4639 def->name = name;
4640 if (node->children == NULL) {
4641 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4642 "define has no children\n", NULL, NULL);
4643 } else {
4644 olddefine = ctxt->define;
4645 ctxt->define = name;
4646 def->content =
4647 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4648 ctxt->define = olddefine;
4649 }
4650 if (ctxt->grammar->defs == NULL)
4651 ctxt->grammar->defs = xmlHashCreate(10);
4652 if (ctxt->grammar->defs == NULL) {
4653 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4654 "Could not create definition hash\n", NULL, NULL);
4655 ret = -1;
4656 } else {
4657 tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4658 if (tmp < 0) {
4659 xmlRelaxNGDefinePtr prev;
Daniel Veillard154877e2003-01-30 12:17:05 +00004660
Daniel Veillard4c004142003-10-07 11:33:24 +00004661 prev = xmlHashLookup(ctxt->grammar->defs, name);
4662 if (prev == NULL) {
4663 xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4664 "Internal error on define aggregation of %s\n",
4665 name, NULL);
4666 ret = -1;
4667 } else {
4668 while (prev->nextHash != NULL)
4669 prev = prev->nextHash;
4670 prev->nextHash = def;
4671 }
4672 }
4673 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00004674 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004675 return (ret);
Daniel Veillard276be4a2003-01-24 01:03:34 +00004676}
4677
4678/**
Daniel Veillard81c51e12009-08-14 18:52:10 +02004679 * xmlRelaxNGParseImportRef:
4680 * @payload: the parser context
4681 * @data: the current grammar
4682 * @name: the reference name
4683 *
4684 * Import import one references into the current grammar
4685 */
4686static void
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01004687xmlRelaxNGParseImportRef(void *payload, void *data, const xmlChar *name) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02004688 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
4689 xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
4690 int tmp;
4691
Daniel Veillardaa422d92009-09-24 11:31:48 +02004692 def->dflags |= IS_EXTERNAL_REF;
4693
Daniel Veillard81c51e12009-08-14 18:52:10 +02004694 tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
4695 if (tmp < 0) {
4696 xmlRelaxNGDefinePtr prev;
4697
4698 prev = (xmlRelaxNGDefinePtr)
4699 xmlHashLookup(ctxt->grammar->refs, def->name);
4700 if (prev == NULL) {
4701 if (def->name != NULL) {
4702 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4703 "Error refs definitions '%s'\n",
4704 def->name, NULL);
4705 } else {
4706 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4707 "Error refs definitions\n",
4708 NULL, NULL);
4709 }
4710 } else {
4711 def->nextHash = prev->nextHash;
4712 prev->nextHash = def;
4713 }
4714 }
4715}
4716
4717/**
4718 * xmlRelaxNGParseImportRefs:
4719 * @ctxt: the parser context
4720 * @grammar: the sub grammar
4721 *
4722 * Import references from the subgrammar into the current grammar
4723 *
4724 * Returns 0 in case of success, -1 in case of failure
4725 */
4726static int
4727xmlRelaxNGParseImportRefs(xmlRelaxNGParserCtxtPtr ctxt,
4728 xmlRelaxNGGrammarPtr grammar) {
4729 if ((ctxt == NULL) || (grammar == NULL) || (ctxt->grammar == NULL))
4730 return(-1);
4731 if (grammar->refs == NULL)
4732 return(0);
4733 if (ctxt->grammar->refs == NULL)
4734 ctxt->grammar->refs = xmlHashCreate(10);
4735 if (ctxt->grammar->refs == NULL) {
4736 xmlRngPErr(ctxt, NULL, XML_RNGP_REF_CREATE_FAILED,
4737 "Could not create references hash\n", NULL, NULL);
4738 return(-1);
4739 }
4740 xmlHashScan(grammar->refs, xmlRelaxNGParseImportRef, ctxt);
Daniel Veillardec18c962009-08-26 18:37:43 +02004741 return(0);
Daniel Veillard81c51e12009-08-14 18:52:10 +02004742}
4743
4744/**
Daniel Veillardfebcca42003-02-16 15:44:18 +00004745 * xmlRelaxNGProcessExternalRef:
4746 * @ctxt: the parser context
Haibo Huangcfd91dc2020-07-30 23:01:33 -07004747 * @node: the externalRef node
Daniel Veillardfebcca42003-02-16 15:44:18 +00004748 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07004749 * Process and compile an externalRef node
Daniel Veillardfebcca42003-02-16 15:44:18 +00004750 *
4751 * Returns the xmlRelaxNGDefinePtr or NULL in case of error
4752 */
4753static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004754xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4755{
Daniel Veillardfebcca42003-02-16 15:44:18 +00004756 xmlRelaxNGDocumentPtr docu;
4757 xmlNodePtr root, tmp;
4758 xmlChar *ns;
Daniel Veillard77648bb2003-02-20 15:03:22 +00004759 int newNs = 0, oldflags;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004760 xmlRelaxNGDefinePtr def;
4761
Daniel Veillard807daf82004-02-22 22:13:27 +00004762 docu = node->psvi;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004763 if (docu != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004764 def = xmlRelaxNGNewDefine(ctxt, node);
4765 if (def == NULL)
4766 return (NULL);
4767 def->type = XML_RELAXNG_EXTERNALREF;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004768
Daniel Veillard4c004142003-10-07 11:33:24 +00004769 if (docu->content == NULL) {
4770 /*
4771 * Then do the parsing for good
4772 */
4773 root = xmlDocGetRootElement(docu->doc);
4774 if (root == NULL) {
4775 xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4776 "xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4777 NULL);
4778 return (NULL);
4779 }
4780 /*
4781 * ns transmission rules
4782 */
4783 ns = xmlGetProp(root, BAD_CAST "ns");
4784 if (ns == NULL) {
4785 tmp = node;
4786 while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4787 ns = xmlGetProp(tmp, BAD_CAST "ns");
4788 if (ns != NULL) {
4789 break;
4790 }
4791 tmp = tmp->parent;
4792 }
4793 if (ns != NULL) {
4794 xmlSetProp(root, BAD_CAST "ns", ns);
4795 newNs = 1;
4796 xmlFree(ns);
4797 }
4798 } else {
4799 xmlFree(ns);
4800 }
Daniel Veillardfebcca42003-02-16 15:44:18 +00004801
Daniel Veillard4c004142003-10-07 11:33:24 +00004802 /*
4803 * Parsing to get a precompiled schemas.
4804 */
4805 oldflags = ctxt->flags;
4806 ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4807 docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4808 ctxt->flags = oldflags;
4809 if ((docu->schema != NULL) &&
4810 (docu->schema->topgrammar != NULL)) {
4811 docu->content = docu->schema->topgrammar->start;
Daniel Veillard81c51e12009-08-14 18:52:10 +02004812 if (docu->schema->topgrammar->refs)
4813 xmlRelaxNGParseImportRefs(ctxt, docu->schema->topgrammar);
Daniel Veillard4c004142003-10-07 11:33:24 +00004814 }
4815
4816 /*
4817 * the externalRef may be reused in a different ns context
4818 */
4819 if (newNs == 1) {
4820 xmlUnsetProp(root, BAD_CAST "ns");
4821 }
4822 }
4823 def->content = docu->content;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004824 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00004825 def = NULL;
Daniel Veillardfebcca42003-02-16 15:44:18 +00004826 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004827 return (def);
Daniel Veillardfebcca42003-02-16 15:44:18 +00004828}
4829
4830/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00004831 * xmlRelaxNGParsePattern:
4832 * @ctxt: a Relax-NG parser context
4833 * @node: the pattern node.
4834 *
4835 * parse the content of a RelaxNG pattern node.
4836 *
Daniel Veillard276be4a2003-01-24 01:03:34 +00004837 * Returns the definition pointer or NULL in case of error or if no
4838 * pattern is generated.
Daniel Veillard6eadf632003-01-23 18:29:16 +00004839 */
4840static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00004841xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4842{
Daniel Veillard6eadf632003-01-23 18:29:16 +00004843 xmlRelaxNGDefinePtr def = NULL;
4844
Daniel Veillardd2298792003-02-14 16:54:11 +00004845 if (node == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004846 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00004847 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004848 if (IS_RELAXNG(node, "element")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004849 def = xmlRelaxNGParseElement(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004850 } else if (IS_RELAXNG(node, "attribute")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004851 def = xmlRelaxNGParseAttribute(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00004852 } else if (IS_RELAXNG(node, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004853 def = xmlRelaxNGNewDefine(ctxt, node);
4854 if (def == NULL)
4855 return (NULL);
4856 def->type = XML_RELAXNG_EMPTY;
4857 if (node->children != NULL) {
4858 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4859 "empty: had a child node\n", NULL, NULL);
4860 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004861 } else if (IS_RELAXNG(node, "text")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004862 def = xmlRelaxNGNewDefine(ctxt, node);
4863 if (def == NULL)
4864 return (NULL);
4865 def->type = XML_RELAXNG_TEXT;
4866 if (node->children != NULL) {
4867 xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4868 "text: had a child node\n", NULL, NULL);
4869 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004870 } else if (IS_RELAXNG(node, "zeroOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004871 def = xmlRelaxNGNewDefine(ctxt, node);
4872 if (def == NULL)
4873 return (NULL);
4874 def->type = XML_RELAXNG_ZEROORMORE;
4875 if (node->children == NULL) {
4876 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4877 "Element %s is empty\n", node->name, NULL);
4878 } else {
4879 def->content =
4880 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4881 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004882 } else if (IS_RELAXNG(node, "oneOrMore")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004883 def = xmlRelaxNGNewDefine(ctxt, node);
4884 if (def == NULL)
4885 return (NULL);
4886 def->type = XML_RELAXNG_ONEORMORE;
4887 if (node->children == NULL) {
4888 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4889 "Element %s is empty\n", node->name, NULL);
4890 } else {
4891 def->content =
4892 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4893 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004894 } else if (IS_RELAXNG(node, "optional")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004895 def = xmlRelaxNGNewDefine(ctxt, node);
4896 if (def == NULL)
4897 return (NULL);
4898 def->type = XML_RELAXNG_OPTIONAL;
4899 if (node->children == NULL) {
4900 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4901 "Element %s is empty\n", node->name, NULL);
4902 } else {
4903 def->content =
4904 xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4905 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00004906 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004907 def = xmlRelaxNGNewDefine(ctxt, node);
4908 if (def == NULL)
4909 return (NULL);
4910 def->type = XML_RELAXNG_CHOICE;
4911 if (node->children == NULL) {
4912 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4913 "Element %s is empty\n", node->name, NULL);
4914 } else {
4915 def->content =
4916 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4917 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004918 } else if (IS_RELAXNG(node, "group")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004919 def = xmlRelaxNGNewDefine(ctxt, node);
4920 if (def == NULL)
4921 return (NULL);
4922 def->type = XML_RELAXNG_GROUP;
4923 if (node->children == NULL) {
4924 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4925 "Element %s is empty\n", node->name, NULL);
4926 } else {
4927 def->content =
4928 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4929 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004930 } else if (IS_RELAXNG(node, "ref")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004931 def = xmlRelaxNGNewDefine(ctxt, node);
4932 if (def == NULL)
4933 return (NULL);
4934 def->type = XML_RELAXNG_REF;
4935 def->name = xmlGetProp(node, BAD_CAST "name");
4936 if (def->name == NULL) {
4937 xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4938 NULL, NULL);
4939 } else {
4940 xmlRelaxNGNormExtSpace(def->name);
4941 if (xmlValidateNCName(def->name, 0)) {
4942 xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4943 "ref name '%s' is not an NCName\n", def->name,
4944 NULL);
4945 }
4946 }
4947 if (node->children != NULL) {
4948 xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4949 NULL, NULL);
4950 }
4951 if (ctxt->grammar->refs == NULL)
4952 ctxt->grammar->refs = xmlHashCreate(10);
4953 if (ctxt->grammar->refs == NULL) {
4954 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4955 "Could not create references hash\n", NULL, NULL);
4956 def = NULL;
4957 } else {
4958 int tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004959
Daniel Veillard4c004142003-10-07 11:33:24 +00004960 tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4961 if (tmp < 0) {
4962 xmlRelaxNGDefinePtr prev;
Daniel Veillard6eadf632003-01-23 18:29:16 +00004963
Daniel Veillard4c004142003-10-07 11:33:24 +00004964 prev = (xmlRelaxNGDefinePtr)
4965 xmlHashLookup(ctxt->grammar->refs, def->name);
4966 if (prev == NULL) {
4967 if (def->name != NULL) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004968 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4969 "Error refs definitions '%s'\n",
4970 def->name, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004971 } else {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00004972 xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4973 "Error refs definitions\n",
4974 NULL, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00004975 }
Daniel Veillard4c004142003-10-07 11:33:24 +00004976 def = NULL;
4977 } else {
4978 def->nextHash = prev->nextHash;
4979 prev->nextHash = def;
4980 }
4981 }
4982 }
Daniel Veillarddd1655c2003-01-25 18:01:32 +00004983 } else if (IS_RELAXNG(node, "data")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004984 def = xmlRelaxNGParseData(ctxt, node);
Daniel Veillardedc91922003-01-26 00:52:04 +00004985 } else if (IS_RELAXNG(node, "value")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004986 def = xmlRelaxNGParseValue(ctxt, node);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00004987 } else if (IS_RELAXNG(node, "list")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00004988 def = xmlRelaxNGNewDefine(ctxt, node);
4989 if (def == NULL)
4990 return (NULL);
4991 def->type = XML_RELAXNG_LIST;
4992 if (node->children == NULL) {
4993 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4994 "Element %s is empty\n", node->name, NULL);
4995 } else {
4996 def->content =
4997 xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4998 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00004999 } else if (IS_RELAXNG(node, "interleave")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005000 def = xmlRelaxNGParseInterleave(ctxt, node);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00005001 } else if (IS_RELAXNG(node, "externalRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005002 def = xmlRelaxNGProcessExternalRef(ctxt, node);
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005003 } else if (IS_RELAXNG(node, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005004 def = xmlRelaxNGNewDefine(ctxt, node);
5005 if (def == NULL)
5006 return (NULL);
5007 def->type = XML_RELAXNG_NOT_ALLOWED;
5008 if (node->children != NULL) {
5009 xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5010 "xmlRelaxNGParse: notAllowed element is not empty\n",
5011 NULL, NULL);
5012 }
Daniel Veillard419a7682003-02-03 23:22:49 +00005013 } else if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005014 xmlRelaxNGGrammarPtr grammar, old;
5015 xmlRelaxNGGrammarPtr oldparent;
Daniel Veillard419a7682003-02-03 23:22:49 +00005016
Daniel Veillardc482e262003-02-26 14:48:48 +00005017#ifdef DEBUG_GRAMMAR
Daniel Veillard4c004142003-10-07 11:33:24 +00005018 xmlGenericError(xmlGenericErrorContext,
5019 "Found <grammar> pattern\n");
Daniel Veillardc482e262003-02-26 14:48:48 +00005020#endif
5021
Daniel Veillard4c004142003-10-07 11:33:24 +00005022 oldparent = ctxt->parentgrammar;
5023 old = ctxt->grammar;
5024 ctxt->parentgrammar = old;
5025 grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
5026 if (old != NULL) {
5027 ctxt->grammar = old;
5028 ctxt->parentgrammar = oldparent;
Daniel Veillardc482e262003-02-26 14:48:48 +00005029#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00005030 if (grammar != NULL) {
5031 grammar->next = old->next;
5032 old->next = grammar;
5033 }
Daniel Veillardc482e262003-02-26 14:48:48 +00005034#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00005035 }
5036 if (grammar != NULL)
5037 def = grammar->start;
5038 else
5039 def = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00005040 } else if (IS_RELAXNG(node, "parentRef")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005041 if (ctxt->parentgrammar == NULL) {
5042 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
5043 "Use of parentRef without a parent grammar\n", NULL,
5044 NULL);
5045 return (NULL);
5046 }
5047 def = xmlRelaxNGNewDefine(ctxt, node);
5048 if (def == NULL)
5049 return (NULL);
5050 def->type = XML_RELAXNG_PARENTREF;
5051 def->name = xmlGetProp(node, BAD_CAST "name");
5052 if (def->name == NULL) {
5053 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
5054 "parentRef has no name\n", NULL, NULL);
5055 } else {
5056 xmlRelaxNGNormExtSpace(def->name);
5057 if (xmlValidateNCName(def->name, 0)) {
5058 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
5059 "parentRef name '%s' is not an NCName\n",
5060 def->name, NULL);
5061 }
5062 }
5063 if (node->children != NULL) {
5064 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
5065 "parentRef is not empty\n", NULL, NULL);
5066 }
5067 if (ctxt->parentgrammar->refs == NULL)
5068 ctxt->parentgrammar->refs = xmlHashCreate(10);
5069 if (ctxt->parentgrammar->refs == NULL) {
5070 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5071 "Could not create references hash\n", NULL, NULL);
5072 def = NULL;
5073 } else if (def->name != NULL) {
5074 int tmp;
Daniel Veillard419a7682003-02-03 23:22:49 +00005075
Daniel Veillard4c004142003-10-07 11:33:24 +00005076 tmp =
5077 xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
5078 if (tmp < 0) {
5079 xmlRelaxNGDefinePtr prev;
Daniel Veillard419a7682003-02-03 23:22:49 +00005080
Daniel Veillard4c004142003-10-07 11:33:24 +00005081 prev = (xmlRelaxNGDefinePtr)
5082 xmlHashLookup(ctxt->parentgrammar->refs, def->name);
5083 if (prev == NULL) {
5084 xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
5085 "Internal error parentRef definitions '%s'\n",
5086 def->name, NULL);
5087 def = NULL;
5088 } else {
5089 def->nextHash = prev->nextHash;
5090 prev->nextHash = def;
5091 }
5092 }
5093 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005094 } else if (IS_RELAXNG(node, "mixed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005095 if (node->children == NULL) {
5096 xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
5097 NULL, NULL);
5098 def = NULL;
5099 } else {
5100 def = xmlRelaxNGParseInterleave(ctxt, node);
5101 if (def != NULL) {
5102 xmlRelaxNGDefinePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005103
Daniel Veillard4c004142003-10-07 11:33:24 +00005104 if ((def->content != NULL) && (def->content->next != NULL)) {
5105 tmp = xmlRelaxNGNewDefine(ctxt, node);
5106 if (tmp != NULL) {
5107 tmp->type = XML_RELAXNG_GROUP;
5108 tmp->content = def->content;
5109 def->content = tmp;
5110 }
5111 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005112
Daniel Veillard4c004142003-10-07 11:33:24 +00005113 tmp = xmlRelaxNGNewDefine(ctxt, node);
5114 if (tmp == NULL)
5115 return (def);
5116 tmp->type = XML_RELAXNG_TEXT;
5117 tmp->next = def->content;
5118 def->content = tmp;
5119 }
5120 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005121 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005122 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
5123 "Unexpected node %s is not a pattern\n", node->name,
5124 NULL);
5125 def = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005126 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005127 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005128}
5129
5130/**
5131 * xmlRelaxNGParseAttribute:
5132 * @ctxt: a Relax-NG parser context
5133 * @node: the element node
5134 *
5135 * parse the content of a RelaxNG attribute node.
5136 *
5137 * Returns the definition pointer or NULL in case of error.
5138 */
5139static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005140xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5141{
Daniel Veillardd2298792003-02-14 16:54:11 +00005142 xmlRelaxNGDefinePtr ret, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005143 xmlNodePtr child;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005144 int old_flags;
5145
Daniel Veillardfd573f12003-03-16 17:52:32 +00005146 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005147 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005148 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005149 ret->type = XML_RELAXNG_ATTRIBUTE;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005150 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005151 child = node->children;
5152 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005153 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5154 "xmlRelaxNGParseattribute: attribute has no children\n",
5155 NULL, NULL);
5156 return (ret);
5157 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005158 old_flags = ctxt->flags;
5159 ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005160 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5161 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005162 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005163
Daniel Veillardd2298792003-02-14 16:54:11 +00005164 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005165 cur = xmlRelaxNGParsePattern(ctxt, child);
5166 if (cur != NULL) {
5167 switch (cur->type) {
5168 case XML_RELAXNG_EMPTY:
5169 case XML_RELAXNG_NOT_ALLOWED:
5170 case XML_RELAXNG_TEXT:
5171 case XML_RELAXNG_ELEMENT:
5172 case XML_RELAXNG_DATATYPE:
5173 case XML_RELAXNG_VALUE:
5174 case XML_RELAXNG_LIST:
5175 case XML_RELAXNG_REF:
5176 case XML_RELAXNG_PARENTREF:
5177 case XML_RELAXNG_EXTERNALREF:
5178 case XML_RELAXNG_DEF:
5179 case XML_RELAXNG_ONEORMORE:
5180 case XML_RELAXNG_ZEROORMORE:
5181 case XML_RELAXNG_OPTIONAL:
5182 case XML_RELAXNG_CHOICE:
5183 case XML_RELAXNG_GROUP:
5184 case XML_RELAXNG_INTERLEAVE:
5185 case XML_RELAXNG_ATTRIBUTE:
5186 ret->content = cur;
5187 cur->parent = ret;
5188 break;
5189 case XML_RELAXNG_START:
5190 case XML_RELAXNG_PARAM:
5191 case XML_RELAXNG_EXCEPT:
5192 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5193 "attribute has invalid content\n", NULL,
5194 NULL);
5195 break;
5196 case XML_RELAXNG_NOOP:
5197 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5198 "RNG Internal error, noop found in attribute\n",
5199 NULL, NULL);
5200 break;
5201 }
5202 }
5203 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005204 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005205 if (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005206 xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5207 "attribute has multiple children\n", NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005208 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005209 ctxt->flags = old_flags;
Daniel Veillard4c004142003-10-07 11:33:24 +00005210 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005211}
5212
5213/**
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005214 * xmlRelaxNGParseExceptNameClass:
5215 * @ctxt: a Relax-NG parser context
5216 * @node: the except node
Daniel Veillard144fae12003-02-03 13:17:57 +00005217 * @attr: 1 if within an attribute, 0 if within an element
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005218 *
5219 * parse the content of a RelaxNG nameClass node.
5220 *
5221 * Returns the definition pointer or NULL in case of error.
5222 */
5223static xmlRelaxNGDefinePtr
Daniel Veillard144fae12003-02-03 13:17:57 +00005224xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005225 xmlNodePtr node, int attr)
5226{
Daniel Veillard144fae12003-02-03 13:17:57 +00005227 xmlRelaxNGDefinePtr ret, cur, last = NULL;
5228 xmlNodePtr child;
5229
Daniel Veillardd2298792003-02-14 16:54:11 +00005230 if (!IS_RELAXNG(node, "except")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005231 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5232 "Expecting an except node\n", NULL, NULL);
5233 return (NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005234 }
5235 if (node->next != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005236 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5237 "exceptNameClass allows only a single except node\n",
5238 NULL, NULL);
Daniel Veillardd2298792003-02-14 16:54:11 +00005239 }
Daniel Veillard144fae12003-02-03 13:17:57 +00005240 if (node->children == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005241 xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5242 NULL, NULL);
5243 return (NULL);
Daniel Veillard144fae12003-02-03 13:17:57 +00005244 }
5245
Daniel Veillardfd573f12003-03-16 17:52:32 +00005246 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard144fae12003-02-03 13:17:57 +00005247 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005248 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005249 ret->type = XML_RELAXNG_EXCEPT;
Daniel Veillard144fae12003-02-03 13:17:57 +00005250 child = node->children;
5251 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005252 cur = xmlRelaxNGNewDefine(ctxt, child);
5253 if (cur == NULL)
5254 break;
5255 if (attr)
5256 cur->type = XML_RELAXNG_ATTRIBUTE;
5257 else
5258 cur->type = XML_RELAXNG_ELEMENT;
5259
Daniel Veillard419a7682003-02-03 23:22:49 +00005260 if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005261 if (last == NULL) {
5262 ret->content = cur;
5263 } else {
5264 last->next = cur;
5265 }
5266 last = cur;
5267 }
5268 child = child->next;
Daniel Veillard144fae12003-02-03 13:17:57 +00005269 }
5270
Daniel Veillard4c004142003-10-07 11:33:24 +00005271 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005272}
5273
5274/**
5275 * xmlRelaxNGParseNameClass:
5276 * @ctxt: a Relax-NG parser context
5277 * @node: the nameClass node
5278 * @def: the current definition
5279 *
5280 * parse the content of a RelaxNG nameClass node.
5281 *
5282 * Returns the definition pointer or NULL in case of error.
5283 */
5284static xmlRelaxNGDefinePtr
5285xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
Daniel Veillard4c004142003-10-07 11:33:24 +00005286 xmlRelaxNGDefinePtr def)
5287{
Daniel Veillardfd573f12003-03-16 17:52:32 +00005288 xmlRelaxNGDefinePtr ret, tmp;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005289 xmlChar *val;
5290
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005291 ret = def;
Daniel Veillard4c004142003-10-07 11:33:24 +00005292 if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005293 (IS_RELAXNG(node, "nsName"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005294 if ((def->type != XML_RELAXNG_ELEMENT) &&
5295 (def->type != XML_RELAXNG_ATTRIBUTE)) {
5296 ret = xmlRelaxNGNewDefine(ctxt, node);
5297 if (ret == NULL)
5298 return (NULL);
5299 ret->parent = def;
5300 if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5301 ret->type = XML_RELAXNG_ATTRIBUTE;
5302 else
5303 ret->type = XML_RELAXNG_ELEMENT;
5304 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005305 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005306 if (IS_RELAXNG(node, "name")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005307 val = xmlNodeGetContent(node);
5308 xmlRelaxNGNormExtSpace(val);
5309 if (xmlValidateNCName(val, 0)) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005310 if (node->parent != NULL)
5311 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5312 "Element %s name '%s' is not an NCName\n",
5313 node->parent->name, val);
5314 else
5315 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5316 "name '%s' is not an NCName\n",
5317 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005318 }
5319 ret->name = val;
5320 val = xmlGetProp(node, BAD_CAST "ns");
5321 ret->ns = val;
5322 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5323 (val != NULL) &&
5324 (xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005325 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
Daniel Veillard4c004142003-10-07 11:33:24 +00005326 "Attribute with namespace '%s' is not allowed\n",
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005327 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005328 }
5329 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5330 (val != NULL) &&
5331 (val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
Daniel Veillard6edbfbb2003-10-07 12:17:44 +00005332 xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5333 "Attribute with QName 'xmlns' is not allowed\n",
5334 val, NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005335 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005336 } else if (IS_RELAXNG(node, "anyName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005337 ret->name = NULL;
5338 ret->ns = NULL;
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, "nsName")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005346 ret->name = NULL;
5347 ret->ns = xmlGetProp(node, BAD_CAST "ns");
5348 if (ret->ns == NULL) {
5349 xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5350 "nsName has no ns attribute\n", NULL, NULL);
5351 }
5352 if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5353 (ret->ns != NULL) &&
5354 (xmlStrEqual
5355 (ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5356 xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5357 "Attribute with namespace '%s' is not allowed\n",
5358 ret->ns, NULL);
5359 }
5360 if (node->children != NULL) {
5361 ret->nameClass =
5362 xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5363 (def->type ==
5364 XML_RELAXNG_ATTRIBUTE));
5365 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005366 } else if (IS_RELAXNG(node, "choice")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005367 xmlNodePtr child;
5368 xmlRelaxNGDefinePtr last = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005369
Elliott Hughes7fbecab2019-01-10 16:42:03 -08005370 if (def->type == XML_RELAXNG_CHOICE) {
5371 ret = def;
5372 } else {
5373 ret = xmlRelaxNGNewDefine(ctxt, node);
5374 if (ret == NULL)
5375 return (NULL);
5376 ret->parent = def;
5377 ret->type = XML_RELAXNG_CHOICE;
5378 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005379
Daniel Veillard4c004142003-10-07 11:33:24 +00005380 if (node->children == NULL) {
5381 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5382 "Element choice is empty\n", NULL, NULL);
5383 } else {
Daniel Veillardfd573f12003-03-16 17:52:32 +00005384
Daniel Veillard4c004142003-10-07 11:33:24 +00005385 child = node->children;
5386 while (child != NULL) {
5387 tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5388 if (tmp != NULL) {
5389 if (last == NULL) {
Elliott Hughes7fbecab2019-01-10 16:42:03 -08005390 last = tmp;
Daniel Veillard4c004142003-10-07 11:33:24 +00005391 } else {
5392 last->next = tmp;
5393 last = tmp;
5394 }
5395 }
5396 child = child->next;
5397 }
5398 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005399 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005400 xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5401 "expecting name, anyName, nsName or choice : got %s\n",
Ben Waltona7a6a4b2010-03-15 10:06:36 +01005402 (node == NULL ? (const xmlChar *) "nothing" : node->name),
5403 NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005404 return (NULL);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005405 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005406 if (ret != def) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005407 if (def->nameClass == NULL) {
5408 def->nameClass = ret;
5409 } else {
5410 tmp = def->nameClass;
5411 while (tmp->next != NULL) {
5412 tmp = tmp->next;
5413 }
5414 tmp->next = ret;
5415 }
Daniel Veillard2e9b1652003-02-19 13:29:45 +00005416 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005417 return (ret);
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005418}
5419
5420/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00005421 * xmlRelaxNGParseElement:
5422 * @ctxt: a Relax-NG parser context
5423 * @node: the element node
5424 *
5425 * parse the content of a RelaxNG element node.
5426 *
5427 * Returns the definition pointer or NULL in case of error.
5428 */
5429static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00005430xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5431{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005432 xmlRelaxNGDefinePtr ret, cur, last;
5433 xmlNodePtr child;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005434 const xmlChar *olddefine;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005435
Daniel Veillardfd573f12003-03-16 17:52:32 +00005436 ret = xmlRelaxNGNewDefine(ctxt, node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005437 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005438 return (NULL);
Daniel Veillardfd573f12003-03-16 17:52:32 +00005439 ret->type = XML_RELAXNG_ELEMENT;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005440 ret->parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005441 child = node->children;
5442 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005443 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5444 "xmlRelaxNGParseElement: element has no children\n",
5445 NULL, NULL);
5446 return (ret);
5447 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005448 cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5449 if (cur != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005450 child = child->next;
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00005451
Daniel Veillard6eadf632003-01-23 18:29:16 +00005452 if (child == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005453 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5454 "xmlRelaxNGParseElement: element has no content\n",
5455 NULL, NULL);
5456 return (ret);
5457 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005458 olddefine = ctxt->define;
5459 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005460 last = NULL;
5461 while (child != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005462 cur = xmlRelaxNGParsePattern(ctxt, child);
5463 if (cur != NULL) {
5464 cur->parent = ret;
5465 switch (cur->type) {
5466 case XML_RELAXNG_EMPTY:
5467 case XML_RELAXNG_NOT_ALLOWED:
5468 case XML_RELAXNG_TEXT:
5469 case XML_RELAXNG_ELEMENT:
5470 case XML_RELAXNG_DATATYPE:
5471 case XML_RELAXNG_VALUE:
5472 case XML_RELAXNG_LIST:
5473 case XML_RELAXNG_REF:
5474 case XML_RELAXNG_PARENTREF:
5475 case XML_RELAXNG_EXTERNALREF:
5476 case XML_RELAXNG_DEF:
5477 case XML_RELAXNG_ZEROORMORE:
5478 case XML_RELAXNG_ONEORMORE:
5479 case XML_RELAXNG_OPTIONAL:
5480 case XML_RELAXNG_CHOICE:
5481 case XML_RELAXNG_GROUP:
5482 case XML_RELAXNG_INTERLEAVE:
5483 if (last == NULL) {
5484 ret->content = last = cur;
5485 } else {
5486 if ((last->type == XML_RELAXNG_ELEMENT) &&
5487 (ret->content == last)) {
5488 ret->content = xmlRelaxNGNewDefine(ctxt, node);
5489 if (ret->content != NULL) {
5490 ret->content->type = XML_RELAXNG_GROUP;
5491 ret->content->content = last;
5492 } else {
5493 ret->content = last;
5494 }
5495 }
5496 last->next = cur;
5497 last = cur;
5498 }
5499 break;
5500 case XML_RELAXNG_ATTRIBUTE:
5501 cur->next = ret->attrs;
5502 ret->attrs = cur;
5503 break;
5504 case XML_RELAXNG_START:
5505 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5506 "RNG Internal error, start found in element\n",
5507 NULL, NULL);
5508 break;
5509 case XML_RELAXNG_PARAM:
5510 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5511 "RNG Internal error, param found in element\n",
5512 NULL, NULL);
5513 break;
5514 case XML_RELAXNG_EXCEPT:
5515 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5516 "RNG Internal error, except found in element\n",
5517 NULL, NULL);
5518 break;
5519 case XML_RELAXNG_NOOP:
5520 xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5521 "RNG Internal error, noop found in element\n",
5522 NULL, NULL);
5523 break;
5524 }
5525 }
5526 child = child->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005527 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00005528 ctxt->define = olddefine;
Daniel Veillard4c004142003-10-07 11:33:24 +00005529 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005530}
5531
5532/**
5533 * xmlRelaxNGParsePatterns:
5534 * @ctxt: a Relax-NG parser context
5535 * @nodes: list of nodes
Daniel Veillard154877e2003-01-30 12:17:05 +00005536 * @group: use an implicit <group> for elements
Daniel Veillard6eadf632003-01-23 18:29:16 +00005537 *
5538 * parse the content of a RelaxNG start node.
5539 *
5540 * Returns the definition pointer or NULL in case of error.
5541 */
5542static xmlRelaxNGDefinePtr
Daniel Veillard154877e2003-01-30 12:17:05 +00005543xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
Daniel Veillard4c004142003-10-07 11:33:24 +00005544 int group)
5545{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005546 xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005547
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00005548 parent = ctxt->def;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005549 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005550 if (IS_RELAXNG(nodes, "element")) {
5551 cur = xmlRelaxNGParseElement(ctxt, nodes);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005552 if (cur == NULL)
5553 return (NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005554 if (def == NULL) {
5555 def = last = cur;
5556 } else {
5557 if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5558 (def == last)) {
5559 def = xmlRelaxNGNewDefine(ctxt, nodes);
Haibo Huangcfd91dc2020-07-30 23:01:33 -07005560 if (def == NULL)
5561 return (NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00005562 def->type = XML_RELAXNG_GROUP;
5563 def->content = last;
5564 }
5565 last->next = cur;
5566 last = cur;
5567 }
5568 cur->parent = parent;
5569 } else {
5570 cur = xmlRelaxNGParsePattern(ctxt, nodes);
5571 if (cur != NULL) {
5572 if (def == NULL) {
5573 def = last = cur;
5574 } else {
5575 last->next = cur;
5576 last = cur;
5577 }
5578 }
5579 }
5580 nodes = nodes->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005581 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005582 return (def);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005583}
5584
5585/**
5586 * xmlRelaxNGParseStart:
5587 * @ctxt: a Relax-NG parser context
5588 * @nodes: start children nodes
5589 *
5590 * parse the content of a RelaxNG start node.
5591 *
5592 * Returns 0 in case of success, -1 in case of error
5593 */
5594static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005595xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5596{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005597 int ret = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005598 xmlRelaxNGDefinePtr def = NULL, last;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005599
Daniel Veillardd2298792003-02-14 16:54:11 +00005600 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005601 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5602 NULL, NULL);
5603 return (-1);
Daniel Veillardd2298792003-02-14 16:54:11 +00005604 }
5605 if (IS_RELAXNG(nodes, "empty")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005606 def = xmlRelaxNGNewDefine(ctxt, nodes);
5607 if (def == NULL)
5608 return (-1);
5609 def->type = XML_RELAXNG_EMPTY;
5610 if (nodes->children != NULL) {
5611 xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5612 "element empty is not empty\n", NULL, NULL);
5613 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005614 } else if (IS_RELAXNG(nodes, "notAllowed")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005615 def = xmlRelaxNGNewDefine(ctxt, nodes);
5616 if (def == NULL)
5617 return (-1);
5618 def->type = XML_RELAXNG_NOT_ALLOWED;
5619 if (nodes->children != NULL) {
5620 xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5621 "element notAllowed is not empty\n", NULL, NULL);
5622 }
Daniel Veillardd2298792003-02-14 16:54:11 +00005623 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005624 def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
Daniel Veillard2df2de22003-02-17 23:34:33 +00005625 }
5626 if (ctxt->grammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005627 last = ctxt->grammar->start;
5628 while (last->next != NULL)
5629 last = last->next;
5630 last->next = def;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005631 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005632 ctxt->grammar->start = def;
Daniel Veillardd2298792003-02-14 16:54:11 +00005633 }
5634 nodes = nodes->next;
5635 if (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005636 xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5637 "start more than one children\n", NULL, NULL);
5638 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005639 }
Daniel Veillard4c004142003-10-07 11:33:24 +00005640 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005641}
5642
5643/**
5644 * xmlRelaxNGParseGrammarContent:
5645 * @ctxt: a Relax-NG parser context
5646 * @nodes: grammar children nodes
5647 *
5648 * parse the content of a RelaxNG grammar node.
5649 *
5650 * Returns 0 in case of success, -1 in case of error
5651 */
5652static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005653xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5654 xmlNodePtr nodes)
Daniel Veillard6eadf632003-01-23 18:29:16 +00005655{
Daniel Veillarde2a5a082003-02-02 14:35:17 +00005656 int ret = 0, tmp;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005657
5658 if (nodes == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005659 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5660 "grammar has no children\n", NULL, NULL);
5661 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005662 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005663 while (nodes != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005664 if (IS_RELAXNG(nodes, "start")) {
5665 if (nodes->children == NULL) {
5666 xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5667 "start has no children\n", NULL, NULL);
5668 } else {
5669 tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5670 if (tmp != 0)
5671 ret = -1;
5672 }
5673 } else if (IS_RELAXNG(nodes, "define")) {
5674 tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5675 if (tmp != 0)
5676 ret = -1;
5677 } else if (IS_RELAXNG(nodes, "include")) {
5678 tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5679 if (tmp != 0)
5680 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005681 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005682 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5683 "grammar has unexpected child %s\n", nodes->name,
5684 NULL);
5685 ret = -1;
5686 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005687 nodes = nodes->next;
5688 }
5689 return (ret);
5690}
5691
5692/**
5693 * xmlRelaxNGCheckReference:
5694 * @ref: the ref
5695 * @ctxt: a Relax-NG parser context
5696 * @name: the name associated to the defines
5697 *
5698 * Applies the 4.17. combine attribute rule for all the define
5699 * element of a given grammar using the same name.
5700 */
5701static void
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01005702xmlRelaxNGCheckReference(void *payload, void *data, const xmlChar * name)
Daniel Veillard4c004142003-10-07 11:33:24 +00005703{
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01005704 xmlRelaxNGDefinePtr ref = (xmlRelaxNGDefinePtr) payload;
5705 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005706 xmlRelaxNGGrammarPtr grammar;
Daniel Veillard276be4a2003-01-24 01:03:34 +00005707 xmlRelaxNGDefinePtr def, cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005708
Daniel Veillardaa422d92009-09-24 11:31:48 +02005709 /*
5710 * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
5711 */
5712 if (ref->dflags & IS_EXTERNAL_REF)
5713 return;
5714
Daniel Veillard6eadf632003-01-23 18:29:16 +00005715 grammar = ctxt->grammar;
5716 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005717 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5718 "Internal error: no grammar in CheckReference %s\n",
5719 name, NULL);
5720 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005721 }
5722 if (ref->content != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005723 xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5724 "Internal error: reference has content in CheckReference %s\n",
5725 name, NULL);
5726 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005727 }
5728 if (grammar->defs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005729 def = xmlHashLookup(grammar->defs, name);
5730 if (def != NULL) {
5731 cur = ref;
5732 while (cur != NULL) {
5733 cur->content = def;
5734 cur = cur->nextHash;
5735 }
5736 } else {
5737 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5738 "Reference %s has no matching definition\n", name,
5739 NULL);
5740 }
Daniel Veillardd4310742003-02-18 21:12:46 +00005741 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00005742 xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5743 "Reference %s has no matching definition\n", name,
5744 NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005745 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005746}
5747
5748/**
5749 * xmlRelaxNGCheckCombine:
5750 * @define: the define(s) list
5751 * @ctxt: a Relax-NG parser context
5752 * @name: the name associated to the defines
5753 *
5754 * Applies the 4.17. combine attribute rule for all the define
5755 * element of a given grammar using the same name.
5756 */
5757static void
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01005758xmlRelaxNGCheckCombine(void *payload, void *data, const xmlChar * name)
Daniel Veillard4c004142003-10-07 11:33:24 +00005759{
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01005760 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) payload;
5761 xmlRelaxNGParserCtxtPtr ctxt = (xmlRelaxNGParserCtxtPtr) data;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005762 xmlChar *combine;
5763 int choiceOrInterleave = -1;
5764 int missing = 0;
5765 xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5766
5767 if (define->nextHash == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005768 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005769 cur = define;
5770 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005771 combine = xmlGetProp(cur->node, BAD_CAST "combine");
5772 if (combine != NULL) {
5773 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5774 if (choiceOrInterleave == -1)
5775 choiceOrInterleave = 1;
5776 else if (choiceOrInterleave == 0) {
5777 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5778 "Defines for %s use both 'choice' and 'interleave'\n",
5779 name, NULL);
5780 }
5781 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5782 if (choiceOrInterleave == -1)
5783 choiceOrInterleave = 0;
5784 else if (choiceOrInterleave == 1) {
5785 xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5786 "Defines for %s use both 'choice' and 'interleave'\n",
5787 name, NULL);
5788 }
5789 } else {
5790 xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5791 "Defines for %s use unknown combine value '%s''\n",
5792 name, combine);
5793 }
5794 xmlFree(combine);
5795 } else {
5796 if (missing == 0)
5797 missing = 1;
5798 else {
5799 xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5800 "Some defines for %s needs the combine attribute\n",
5801 name, NULL);
5802 }
5803 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005804
Daniel Veillard4c004142003-10-07 11:33:24 +00005805 cur = cur->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005806 }
5807#ifdef DEBUG
5808 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005809 "xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5810 name, choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005811#endif
5812 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005813 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005814 cur = xmlRelaxNGNewDefine(ctxt, define->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005815 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005816 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005817 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005818 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005819 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005820 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005821 tmp = define;
5822 last = NULL;
5823 while (tmp != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005824 if (tmp->content != NULL) {
5825 if (tmp->content->next != NULL) {
5826 /*
5827 * we need first to create a wrapper.
5828 */
5829 tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5830 if (tmp2 == NULL)
5831 break;
5832 tmp2->type = XML_RELAXNG_GROUP;
5833 tmp2->content = tmp->content;
5834 } else {
5835 tmp2 = tmp->content;
5836 }
5837 if (last == NULL) {
5838 cur->content = tmp2;
5839 } else {
5840 last->next = tmp2;
5841 }
5842 last = tmp2;
5843 }
5844 tmp->content = cur;
5845 tmp = tmp->nextHash;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005846 }
5847 define->content = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005848 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005849 if (ctxt->interleaves == NULL)
5850 ctxt->interleaves = xmlHashCreate(10);
5851 if (ctxt->interleaves == NULL) {
5852 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5853 "Failed to create interleaves hash table\n", NULL,
5854 NULL);
5855 } else {
5856 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005857
Daniel Veillard4c004142003-10-07 11:33:24 +00005858 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5859 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5860 0) {
5861 xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5862 "Failed to add %s to hash table\n",
5863 (const xmlChar *) tmpname, NULL);
5864 }
5865 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005866 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005867}
5868
5869/**
5870 * xmlRelaxNGCombineStart:
5871 * @ctxt: a Relax-NG parser context
5872 * @grammar: the grammar
5873 *
5874 * Applies the 4.17. combine rule for all the start
5875 * element of a given grammar.
5876 */
5877static void
5878xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00005879 xmlRelaxNGGrammarPtr grammar)
5880{
Daniel Veillard6eadf632003-01-23 18:29:16 +00005881 xmlRelaxNGDefinePtr starts;
5882 xmlChar *combine;
5883 int choiceOrInterleave = -1;
5884 int missing = 0;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005885 xmlRelaxNGDefinePtr cur;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005886
Daniel Veillard2df2de22003-02-17 23:34:33 +00005887 starts = grammar->start;
5888 if ((starts == NULL) || (starts->next == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00005889 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005890 cur = starts;
5891 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005892 if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5893 (!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5894 combine = NULL;
5895 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5896 "Internal error: start element not found\n", NULL,
5897 NULL);
5898 } else {
5899 combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5900 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005901
Daniel Veillard4c004142003-10-07 11:33:24 +00005902 if (combine != NULL) {
5903 if (xmlStrEqual(combine, BAD_CAST "choice")) {
5904 if (choiceOrInterleave == -1)
5905 choiceOrInterleave = 1;
5906 else if (choiceOrInterleave == 0) {
5907 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5908 "<start> use both 'choice' and 'interleave'\n",
5909 NULL, NULL);
5910 }
5911 } else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5912 if (choiceOrInterleave == -1)
5913 choiceOrInterleave = 0;
5914 else if (choiceOrInterleave == 1) {
5915 xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5916 "<start> use both 'choice' and 'interleave'\n",
5917 NULL, NULL);
5918 }
5919 } else {
5920 xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5921 "<start> uses unknown combine value '%s''\n",
5922 combine, NULL);
5923 }
5924 xmlFree(combine);
5925 } else {
5926 if (missing == 0)
5927 missing = 1;
5928 else {
5929 xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5930 "Some <start> element miss the combine attribute\n",
5931 NULL, NULL);
5932 }
5933 }
5934
5935 cur = cur->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005936 }
5937#ifdef DEBUG
5938 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00005939 "xmlRelaxNGCombineStart(): merging <start>: %d\n",
5940 choiceOrInterleave);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005941#endif
5942 if (choiceOrInterleave == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +00005943 choiceOrInterleave = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005944 cur = xmlRelaxNGNewDefine(ctxt, starts->node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00005945 if (cur == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00005946 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00005947 if (choiceOrInterleave == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00005948 cur->type = XML_RELAXNG_INTERLEAVE;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005949 else
Daniel Veillard4c004142003-10-07 11:33:24 +00005950 cur->type = XML_RELAXNG_CHOICE;
Daniel Veillard2df2de22003-02-17 23:34:33 +00005951 cur->content = grammar->start;
5952 grammar->start = cur;
Daniel Veillardfd573f12003-03-16 17:52:32 +00005953 if (choiceOrInterleave == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005954 if (ctxt->interleaves == NULL)
5955 ctxt->interleaves = xmlHashCreate(10);
5956 if (ctxt->interleaves == NULL) {
5957 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5958 "Failed to create interleaves hash table\n", NULL,
5959 NULL);
5960 } else {
5961 char tmpname[32];
Daniel Veillardfd573f12003-03-16 17:52:32 +00005962
Daniel Veillard4c004142003-10-07 11:33:24 +00005963 snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5964 if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5965 0) {
5966 xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5967 "Failed to add %s to hash table\n",
5968 (const xmlChar *) tmpname, NULL);
5969 }
5970 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00005971 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00005972}
5973
5974/**
Daniel Veillardd4310742003-02-18 21:12:46 +00005975 * xmlRelaxNGCheckCycles:
5976 * @ctxt: a Relax-NG parser context
5977 * @nodes: grammar children nodes
5978 * @depth: the counter
5979 *
5980 * Check for cycles.
5981 *
5982 * Returns 0 if check passed, and -1 in case of error
5983 */
5984static int
Daniel Veillard4c004142003-10-07 11:33:24 +00005985xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5986 xmlRelaxNGDefinePtr cur, int depth)
5987{
Daniel Veillardd4310742003-02-18 21:12:46 +00005988 int ret = 0;
5989
5990 while ((ret == 0) && (cur != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00005991 if ((cur->type == XML_RELAXNG_REF) ||
5992 (cur->type == XML_RELAXNG_PARENTREF)) {
5993 if (cur->depth == -1) {
5994 cur->depth = depth;
5995 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5996 cur->depth = -2;
5997 } else if (depth == cur->depth) {
5998 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5999 "Detected a cycle in %s references\n",
6000 cur->name, NULL);
6001 return (-1);
6002 }
6003 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6004 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
6005 } else {
6006 ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
6007 }
6008 cur = cur->next;
Daniel Veillardd4310742003-02-18 21:12:46 +00006009 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006010 return (ret);
Daniel Veillardd4310742003-02-18 21:12:46 +00006011}
6012
6013/**
Daniel Veillard77648bb2003-02-20 15:03:22 +00006014 * xmlRelaxNGTryUnlink:
6015 * @ctxt: a Relax-NG parser context
6016 * @cur: the definition to unlink
6017 * @parent: the parent definition
6018 * @prev: the previous sibling definition
6019 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006020 * Try to unlink a definition. If not possible make it a NOOP
Daniel Veillard77648bb2003-02-20 15:03:22 +00006021 *
6022 * Returns the new prev definition
6023 */
6024static xmlRelaxNGDefinePtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006025xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
6026 xmlRelaxNGDefinePtr cur,
6027 xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
6028{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006029 if (prev != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006030 prev->next = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006031 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006032 if (parent != NULL) {
6033 if (parent->content == cur)
6034 parent->content = cur->next;
6035 else if (parent->attrs == cur)
6036 parent->attrs = cur->next;
6037 else if (parent->nameClass == cur)
6038 parent->nameClass = cur->next;
6039 } else {
6040 cur->type = XML_RELAXNG_NOOP;
6041 prev = cur;
6042 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006043 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006044 return (prev);
Daniel Veillard77648bb2003-02-20 15:03:22 +00006045}
6046
6047/**
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006048 * xmlRelaxNGSimplify:
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006049 * @ctxt: a Relax-NG parser context
6050 * @nodes: grammar children nodes
6051 *
6052 * Check for simplification of empty and notAllowed
6053 */
6054static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006055xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
6056 xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
6057{
Daniel Veillardfd573f12003-03-16 17:52:32 +00006058 xmlRelaxNGDefinePtr prev = NULL;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006059
Daniel Veillardfd573f12003-03-16 17:52:32 +00006060 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006061 if ((cur->type == XML_RELAXNG_REF) ||
6062 (cur->type == XML_RELAXNG_PARENTREF)) {
6063 if (cur->depth != -3) {
6064 cur->depth = -3;
6065 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6066 }
6067 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6068 cur->parent = parent;
6069 if ((parent != NULL) &&
6070 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6071 (parent->type == XML_RELAXNG_LIST) ||
6072 (parent->type == XML_RELAXNG_GROUP) ||
6073 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6074 (parent->type == XML_RELAXNG_ONEORMORE) ||
6075 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6076 parent->type = XML_RELAXNG_NOT_ALLOWED;
6077 break;
6078 }
6079 if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
6080 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6081 } else
6082 prev = cur;
6083 } else if (cur->type == XML_RELAXNG_EMPTY) {
6084 cur->parent = parent;
6085 if ((parent != NULL) &&
6086 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6087 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6088 parent->type = XML_RELAXNG_EMPTY;
6089 break;
6090 }
6091 if ((parent != NULL) &&
6092 ((parent->type == XML_RELAXNG_GROUP) ||
6093 (parent->type == XML_RELAXNG_INTERLEAVE))) {
6094 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6095 } else
6096 prev = cur;
6097 } else {
6098 cur->parent = parent;
6099 if (cur->content != NULL)
6100 xmlRelaxNGSimplify(ctxt, cur->content, cur);
6101 if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
6102 xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
6103 if (cur->nameClass != NULL)
6104 xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
6105 /*
6106 * On Elements, try to move attribute only generating rules on
6107 * the attrs rules.
6108 */
6109 if (cur->type == XML_RELAXNG_ELEMENT) {
6110 int attronly;
6111 xmlRelaxNGDefinePtr tmp, pre;
Daniel Veillardce192eb2003-04-16 15:58:05 +00006112
Daniel Veillard4c004142003-10-07 11:33:24 +00006113 while (cur->content != NULL) {
6114 attronly =
6115 xmlRelaxNGGenerateAttributes(ctxt, cur->content);
6116 if (attronly == 1) {
6117 /*
6118 * migrate cur->content to attrs
6119 */
6120 tmp = cur->content;
6121 cur->content = tmp->next;
6122 tmp->next = cur->attrs;
6123 cur->attrs = tmp;
6124 } else {
6125 /*
6126 * cur->content can generate elements or text
6127 */
6128 break;
6129 }
6130 }
6131 pre = cur->content;
6132 while ((pre != NULL) && (pre->next != NULL)) {
6133 tmp = pre->next;
6134 attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
6135 if (attronly == 1) {
6136 /*
6137 * migrate tmp to attrs
6138 */
6139 pre->next = tmp->next;
6140 tmp->next = cur->attrs;
6141 cur->attrs = tmp;
6142 } else {
6143 pre = tmp;
6144 }
6145 }
6146 }
6147 /*
6148 * This may result in a simplification
6149 */
6150 if ((cur->type == XML_RELAXNG_GROUP) ||
6151 (cur->type == XML_RELAXNG_INTERLEAVE)) {
6152 if (cur->content == NULL)
6153 cur->type = XML_RELAXNG_EMPTY;
6154 else if (cur->content->next == NULL) {
6155 if ((parent == NULL) && (prev == NULL)) {
6156 cur->type = XML_RELAXNG_NOOP;
6157 } else if (prev == NULL) {
6158 parent->content = cur->content;
6159 cur->content->next = cur->next;
6160 cur = cur->content;
6161 } else {
6162 cur->content->next = cur->next;
6163 prev->next = cur->content;
6164 cur = cur->content;
6165 }
6166 }
6167 }
6168 /*
6169 * the current node may have been transformed back
6170 */
6171 if ((cur->type == XML_RELAXNG_EXCEPT) &&
6172 (cur->content != NULL) &&
6173 (cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6174 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6175 } else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6176 if ((parent != NULL) &&
6177 ((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6178 (parent->type == XML_RELAXNG_LIST) ||
6179 (parent->type == XML_RELAXNG_GROUP) ||
6180 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6181 (parent->type == XML_RELAXNG_ONEORMORE) ||
6182 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6183 parent->type = XML_RELAXNG_NOT_ALLOWED;
6184 break;
6185 }
6186 if ((parent != NULL) &&
6187 (parent->type == XML_RELAXNG_CHOICE)) {
6188 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6189 } else
6190 prev = cur;
6191 } else if (cur->type == XML_RELAXNG_EMPTY) {
6192 if ((parent != NULL) &&
6193 ((parent->type == XML_RELAXNG_ONEORMORE) ||
6194 (parent->type == XML_RELAXNG_ZEROORMORE))) {
6195 parent->type = XML_RELAXNG_EMPTY;
6196 break;
6197 }
6198 if ((parent != NULL) &&
6199 ((parent->type == XML_RELAXNG_GROUP) ||
6200 (parent->type == XML_RELAXNG_INTERLEAVE) ||
6201 (parent->type == XML_RELAXNG_CHOICE))) {
6202 prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6203 } else
6204 prev = cur;
6205 } else {
6206 prev = cur;
6207 }
6208 }
6209 cur = cur->next;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006210 }
6211}
6212
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006213/**
6214 * xmlRelaxNGGroupContentType:
6215 * @ct1: the first content type
6216 * @ct2: the second content type
6217 *
6218 * Try to group 2 content types
6219 *
6220 * Returns the content type
6221 */
6222static xmlRelaxNGContentType
6223xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006224 xmlRelaxNGContentType ct2)
6225{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006226 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006227 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6228 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006229 if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006230 return (ct2);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006231 if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
Daniel Veillard4c004142003-10-07 11:33:24 +00006232 return (ct1);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006233 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00006234 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6235 return (XML_RELAXNG_CONTENT_COMPLEX);
6236 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006237}
6238
6239/**
6240 * xmlRelaxNGMaxContentType:
6241 * @ct1: the first content type
6242 * @ct2: the second content type
6243 *
6244 * Compute the max content-type
6245 *
6246 * Returns the content type
6247 */
6248static xmlRelaxNGContentType
6249xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
Daniel Veillard4c004142003-10-07 11:33:24 +00006250 xmlRelaxNGContentType ct2)
6251{
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006252 if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006253 (ct2 == XML_RELAXNG_CONTENT_ERROR))
6254 return (XML_RELAXNG_CONTENT_ERROR);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006255 if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006256 (ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6257 return (XML_RELAXNG_CONTENT_SIMPLE);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006258 if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00006259 (ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6260 return (XML_RELAXNG_CONTENT_COMPLEX);
6261 return (XML_RELAXNG_CONTENT_EMPTY);
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006262}
Daniel Veillard77648bb2003-02-20 15:03:22 +00006263
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006264/**
6265 * xmlRelaxNGCheckRules:
6266 * @ctxt: a Relax-NG parser context
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006267 * @cur: the current definition
Daniel Veillard77648bb2003-02-20 15:03:22 +00006268 * @flags: some accumulated flags
Daniel Veillardfd573f12003-03-16 17:52:32 +00006269 * @ptype: the parent type
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006270 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006271 * Check for rules in section 7.1 and 7.2
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006272 *
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006273 * Returns the content type of @cur
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006274 */
Daniel Veillard4c5cf702003-02-21 15:40:34 +00006275static xmlRelaxNGContentType
Daniel Veillard4c004142003-10-07 11:33:24 +00006276xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6277 xmlRelaxNGDefinePtr cur, int flags,
6278 xmlRelaxNGType ptype)
6279{
Daniel Veillardd44b9362009-09-07 12:15:08 +02006280 int nflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00006281 xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006282
Daniel Veillardfd573f12003-03-16 17:52:32 +00006283 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006284 ret = XML_RELAXNG_CONTENT_EMPTY;
6285 if ((cur->type == XML_RELAXNG_REF) ||
6286 (cur->type == XML_RELAXNG_PARENTREF)) {
Daniel Veillard63d68a32005-03-31 13:50:00 +00006287 /*
6288 * This should actually be caught by list//element(ref) at the
6289 * element boundaries, c.f. Bug #159968 local refs are dropped
6290 * in step 4.19.
6291 */
6292#if 0
Daniel Veillard4c004142003-10-07 11:33:24 +00006293 if (flags & XML_RELAXNG_IN_LIST) {
6294 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6295 "Found forbidden pattern list//ref\n", NULL,
6296 NULL);
6297 }
Daniel Veillard63d68a32005-03-31 13:50:00 +00006298#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00006299 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6300 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6301 "Found forbidden pattern data/except//ref\n",
6302 NULL, NULL);
6303 }
Daniel Veillard81c51e12009-08-14 18:52:10 +02006304 if (cur->content == NULL) {
6305 if (cur->type == XML_RELAXNG_PARENTREF)
6306 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6307 "Internal found no define for parent refs\n",
6308 NULL, NULL);
6309 else
6310 xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_NO_DEF,
6311 "Internal found no define for ref %s\n",
Daniel Veillarda4f27cb2009-08-21 17:34:17 +02006312 (cur->name ? cur->name: BAD_CAST "null"), NULL);
Daniel Veillard81c51e12009-08-14 18:52:10 +02006313 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006314 if (cur->depth > -4) {
6315 cur->depth = -4;
6316 ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6317 flags, cur->type);
6318 cur->depth = ret - 15;
6319 } else if (cur->depth == -4) {
6320 ret = XML_RELAXNG_CONTENT_COMPLEX;
6321 } else {
6322 ret = (xmlRelaxNGContentType) (cur->depth + 15);
6323 }
6324 } else if (cur->type == XML_RELAXNG_ELEMENT) {
6325 /*
6326 * The 7.3 Attribute derivation rule for groups is plugged there
6327 */
6328 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6329 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6330 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6331 "Found forbidden pattern data/except//element(ref)\n",
6332 NULL, NULL);
6333 }
6334 if (flags & XML_RELAXNG_IN_LIST) {
6335 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6336 "Found forbidden pattern list//element(ref)\n",
6337 NULL, NULL);
6338 }
6339 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6340 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6341 "Found forbidden pattern attribute//element(ref)\n",
6342 NULL, NULL);
6343 }
6344 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6345 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6346 "Found forbidden pattern attribute//element(ref)\n",
6347 NULL, NULL);
6348 }
6349 /*
6350 * reset since in the simple form elements are only child
6351 * of grammar/define
6352 */
6353 nflags = 0;
6354 ret =
6355 xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6356 if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6357 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6358 "Element %s attributes have a content type error\n",
6359 cur->name, NULL);
6360 }
6361 ret =
6362 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6363 cur->type);
6364 if (ret == XML_RELAXNG_CONTENT_ERROR) {
6365 xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6366 "Element %s has a content type error\n",
6367 cur->name, NULL);
6368 } else {
6369 ret = XML_RELAXNG_CONTENT_COMPLEX;
6370 }
6371 } else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6372 if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6373 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6374 "Found forbidden pattern attribute//attribute\n",
6375 NULL, NULL);
6376 }
6377 if (flags & XML_RELAXNG_IN_LIST) {
6378 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6379 "Found forbidden pattern list//attribute\n",
6380 NULL, NULL);
6381 }
6382 if (flags & XML_RELAXNG_IN_OOMGROUP) {
6383 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6384 "Found forbidden pattern oneOrMore//group//attribute\n",
6385 NULL, NULL);
6386 }
6387 if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6388 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6389 "Found forbidden pattern oneOrMore//interleave//attribute\n",
6390 NULL, NULL);
6391 }
6392 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6393 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6394 "Found forbidden pattern data/except//attribute\n",
6395 NULL, NULL);
6396 }
6397 if (flags & XML_RELAXNG_IN_START) {
6398 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6399 "Found forbidden pattern start//attribute\n",
6400 NULL, NULL);
6401 }
6402 if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006403 && cur->name == NULL
6404 /* following is checking alternative name class readiness
6405 in case it went the "choice" route */
6406 && cur->nameClass == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006407 if (cur->ns == NULL) {
6408 xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6409 "Found anyName attribute without oneOrMore ancestor\n",
6410 NULL, NULL);
6411 } else {
6412 xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6413 "Found nsName attribute without oneOrMore ancestor\n",
6414 NULL, NULL);
6415 }
6416 }
6417 nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6418 xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6419 ret = XML_RELAXNG_CONTENT_EMPTY;
6420 } else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6421 (cur->type == XML_RELAXNG_ZEROORMORE)) {
6422 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6423 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6424 "Found forbidden pattern data/except//oneOrMore\n",
6425 NULL, NULL);
6426 }
6427 if (flags & XML_RELAXNG_IN_START) {
6428 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6429 "Found forbidden pattern start//oneOrMore\n",
6430 NULL, NULL);
6431 }
6432 nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6433 ret =
6434 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6435 cur->type);
6436 ret = xmlRelaxNGGroupContentType(ret, ret);
6437 } else if (cur->type == XML_RELAXNG_LIST) {
6438 if (flags & XML_RELAXNG_IN_LIST) {
6439 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6440 "Found forbidden pattern list//list\n", NULL,
6441 NULL);
6442 }
6443 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6444 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6445 "Found forbidden pattern data/except//list\n",
6446 NULL, NULL);
6447 }
6448 if (flags & XML_RELAXNG_IN_START) {
6449 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6450 "Found forbidden pattern start//list\n", NULL,
6451 NULL);
6452 }
6453 nflags = flags | XML_RELAXNG_IN_LIST;
6454 ret =
6455 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6456 cur->type);
6457 } else if (cur->type == XML_RELAXNG_GROUP) {
6458 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6459 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6460 "Found forbidden pattern data/except//group\n",
6461 NULL, NULL);
6462 }
6463 if (flags & XML_RELAXNG_IN_START) {
6464 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6465 "Found forbidden pattern start//group\n", NULL,
6466 NULL);
6467 }
6468 if (flags & XML_RELAXNG_IN_ONEORMORE)
6469 nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6470 else
6471 nflags = flags;
6472 ret =
6473 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6474 cur->type);
6475 /*
6476 * The 7.3 Attribute derivation rule for groups is plugged there
6477 */
6478 xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6479 } else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6480 if (flags & XML_RELAXNG_IN_LIST) {
6481 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6482 "Found forbidden pattern list//interleave\n",
6483 NULL, NULL);
6484 }
6485 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6486 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6487 "Found forbidden pattern data/except//interleave\n",
6488 NULL, NULL);
6489 }
6490 if (flags & XML_RELAXNG_IN_START) {
6491 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6492 "Found forbidden pattern start//interleave\n",
6493 NULL, NULL);
6494 }
6495 if (flags & XML_RELAXNG_IN_ONEORMORE)
6496 nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6497 else
6498 nflags = flags;
6499 ret =
6500 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6501 cur->type);
6502 } else if (cur->type == XML_RELAXNG_EXCEPT) {
6503 if ((cur->parent != NULL) &&
6504 (cur->parent->type == XML_RELAXNG_DATATYPE))
6505 nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6506 else
6507 nflags = flags;
6508 ret =
6509 xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6510 cur->type);
6511 } else if (cur->type == XML_RELAXNG_DATATYPE) {
6512 if (flags & XML_RELAXNG_IN_START) {
6513 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6514 "Found forbidden pattern start//data\n", NULL,
6515 NULL);
6516 }
6517 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6518 ret = XML_RELAXNG_CONTENT_SIMPLE;
6519 } else if (cur->type == XML_RELAXNG_VALUE) {
6520 if (flags & XML_RELAXNG_IN_START) {
6521 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6522 "Found forbidden pattern start//value\n", NULL,
6523 NULL);
6524 }
6525 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6526 ret = XML_RELAXNG_CONTENT_SIMPLE;
6527 } else if (cur->type == XML_RELAXNG_TEXT) {
6528 if (flags & XML_RELAXNG_IN_LIST) {
6529 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6530 "Found forbidden pattern list//text\n", NULL,
6531 NULL);
6532 }
6533 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6534 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6535 "Found forbidden pattern data/except//text\n",
6536 NULL, NULL);
6537 }
6538 if (flags & XML_RELAXNG_IN_START) {
6539 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6540 "Found forbidden pattern start//text\n", NULL,
6541 NULL);
6542 }
6543 ret = XML_RELAXNG_CONTENT_COMPLEX;
6544 } else if (cur->type == XML_RELAXNG_EMPTY) {
6545 if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6546 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6547 "Found forbidden pattern data/except//empty\n",
6548 NULL, NULL);
6549 }
6550 if (flags & XML_RELAXNG_IN_START) {
6551 xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6552 "Found forbidden pattern start//empty\n", NULL,
6553 NULL);
6554 }
6555 ret = XML_RELAXNG_CONTENT_EMPTY;
6556 } else if (cur->type == XML_RELAXNG_CHOICE) {
6557 xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6558 ret =
6559 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6560 } else {
6561 ret =
6562 xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6563 }
6564 cur = cur->next;
6565 if (ptype == XML_RELAXNG_GROUP) {
6566 val = xmlRelaxNGGroupContentType(val, ret);
6567 } else if (ptype == XML_RELAXNG_INTERLEAVE) {
Daniel Veillard594e5df2009-09-07 14:58:47 +02006568 /*
6569 * TODO: scan complain that tmp is never used, seems on purpose
6570 * need double-checking
6571 */
Daniel Veillard4c004142003-10-07 11:33:24 +00006572 tmp = xmlRelaxNGGroupContentType(val, ret);
6573 if (tmp != XML_RELAXNG_CONTENT_ERROR)
6574 tmp = xmlRelaxNGMaxContentType(val, ret);
6575 } else if (ptype == XML_RELAXNG_CHOICE) {
6576 val = xmlRelaxNGMaxContentType(val, ret);
6577 } else if (ptype == XML_RELAXNG_LIST) {
6578 val = XML_RELAXNG_CONTENT_SIMPLE;
6579 } else if (ptype == XML_RELAXNG_EXCEPT) {
6580 if (ret == XML_RELAXNG_CONTENT_ERROR)
6581 val = XML_RELAXNG_CONTENT_ERROR;
6582 else
6583 val = XML_RELAXNG_CONTENT_SIMPLE;
6584 } else {
6585 val = xmlRelaxNGGroupContentType(val, ret);
6586 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00006587
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006588 }
Daniel Veillard4c004142003-10-07 11:33:24 +00006589 return (val);
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006590}
Daniel Veillard1c745ad2003-02-20 00:11:02 +00006591
6592/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006593 * xmlRelaxNGParseGrammar:
6594 * @ctxt: a Relax-NG parser context
6595 * @nodes: grammar children nodes
6596 *
6597 * parse a Relax-NG <grammar> node
6598 *
6599 * Returns the internal xmlRelaxNGGrammarPtr built or
6600 * NULL in case of error
6601 */
6602static xmlRelaxNGGrammarPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006603xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6604{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006605 xmlRelaxNGGrammarPtr ret, tmp, old;
6606
Daniel Veillardc482e262003-02-26 14:48:48 +00006607#ifdef DEBUG_GRAMMAR
6608 xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6609#endif
6610
Daniel Veillard6eadf632003-01-23 18:29:16 +00006611 ret = xmlRelaxNGNewGrammar(ctxt);
6612 if (ret == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006613 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006614
6615 /*
6616 * Link the new grammar in the tree
6617 */
6618 ret->parent = ctxt->grammar;
6619 if (ctxt->grammar != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006620 tmp = ctxt->grammar->children;
6621 if (tmp == NULL) {
6622 ctxt->grammar->children = ret;
6623 } else {
6624 while (tmp->next != NULL)
6625 tmp = tmp->next;
6626 tmp->next = ret;
6627 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006628 }
6629
6630 old = ctxt->grammar;
6631 ctxt->grammar = ret;
6632 xmlRelaxNGParseGrammarContent(ctxt, nodes);
6633 ctxt->grammar = ret;
Daniel Veillard2df2de22003-02-17 23:34:33 +00006634 if (ctxt->grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006635 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6636 "Failed to parse <grammar> content\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006637 } else if (ctxt->grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006638 xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6639 "Element <grammar> has no <start>\n", NULL, NULL);
Daniel Veillard2df2de22003-02-17 23:34:33 +00006640 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006641
6642 /*
Jan Pokornýacace882014-06-09 23:45:24 +02006643 * Apply 4.17 merging rules to defines and starts
Daniel Veillard6eadf632003-01-23 18:29:16 +00006644 */
6645 xmlRelaxNGCombineStart(ctxt, ret);
6646 if (ret->defs != NULL) {
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01006647 xmlHashScan(ret->defs, xmlRelaxNGCheckCombine, ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006648 }
6649
6650 /*
6651 * link together defines and refs in this grammar
6652 */
6653 if (ret->refs != NULL) {
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01006654 xmlHashScan(ret->refs, xmlRelaxNGCheckReference, ctxt);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006655 }
Daniel Veillard952379b2003-03-17 15:37:12 +00006656
Daniel Veillard81c51e12009-08-14 18:52:10 +02006657
Daniel Veillard25a1ce92008-06-02 16:04:12 +00006658 /* @@@@ */
6659
Daniel Veillard6eadf632003-01-23 18:29:16 +00006660 ctxt->grammar = old;
Daniel Veillard4c004142003-10-07 11:33:24 +00006661 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006662}
6663
6664/**
6665 * xmlRelaxNGParseDocument:
6666 * @ctxt: a Relax-NG parser context
6667 * @node: the root node of the RelaxNG schema
6668 *
6669 * parse a Relax-NG definition resource and build an internal
Haibo Huangcfd91dc2020-07-30 23:01:33 -07006670 * xmlRelaxNG structure which can be used to validate instances.
Daniel Veillard6eadf632003-01-23 18:29:16 +00006671 *
6672 * Returns the internal XML RelaxNG structure built or
6673 * NULL in case of error
6674 */
6675static xmlRelaxNGPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006676xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6677{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006678 xmlRelaxNGPtr schema = NULL;
Daniel Veillard276be4a2003-01-24 01:03:34 +00006679 const xmlChar *olddefine;
Daniel Veillarde431a272003-01-29 23:02:33 +00006680 xmlRelaxNGGrammarPtr old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006681
6682 if ((ctxt == NULL) || (node == NULL))
6683 return (NULL);
6684
6685 schema = xmlRelaxNGNewRelaxNG(ctxt);
6686 if (schema == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006687 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006688
Daniel Veillard276be4a2003-01-24 01:03:34 +00006689 olddefine = ctxt->define;
6690 ctxt->define = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006691 if (IS_RELAXNG(node, "grammar")) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006692 schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
Daniel Veillard42870f42014-07-26 21:04:54 +08006693 if (schema->topgrammar == NULL) {
6694 xmlRelaxNGFree(schema);
6695 return (NULL);
6696 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006697 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006698 xmlRelaxNGGrammarPtr tmp, ret;
Daniel Veillardc482e262003-02-26 14:48:48 +00006699
Daniel Veillard4c004142003-10-07 11:33:24 +00006700 schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6701 if (schema->topgrammar == NULL) {
Daniel Veillard42870f42014-07-26 21:04:54 +08006702 xmlRelaxNGFree(schema);
6703 return (NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00006704 }
6705 /*
6706 * Link the new grammar in the tree
6707 */
6708 ret->parent = ctxt->grammar;
6709 if (ctxt->grammar != NULL) {
6710 tmp = ctxt->grammar->children;
6711 if (tmp == NULL) {
6712 ctxt->grammar->children = ret;
6713 } else {
6714 while (tmp->next != NULL)
6715 tmp = tmp->next;
6716 tmp->next = ret;
6717 }
6718 }
6719 old = ctxt->grammar;
6720 ctxt->grammar = ret;
6721 xmlRelaxNGParseStart(ctxt, node);
6722 if (old != NULL)
6723 ctxt->grammar = old;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006724 }
Daniel Veillard276be4a2003-01-24 01:03:34 +00006725 ctxt->define = olddefine;
Daniel Veillardd4310742003-02-18 21:12:46 +00006726 if (schema->topgrammar->start != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006727 xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6728 if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6729 xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6730 while ((schema->topgrammar->start != NULL) &&
6731 (schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6732 (schema->topgrammar->start->next != NULL))
6733 schema->topgrammar->start =
6734 schema->topgrammar->start->content;
6735 xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6736 XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6737 }
Daniel Veillardd4310742003-02-18 21:12:46 +00006738 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00006739#ifdef DEBUG
6740 if (schema == NULL)
6741 xmlGenericError(xmlGenericErrorContext,
6742 "xmlRelaxNGParseDocument() failed\n");
6743#endif
6744
6745 return (schema);
6746}
6747
6748/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08006749 * *
6750 * Reading RelaxNGs *
6751 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00006752 ************************************************************************/
6753
6754/**
6755 * xmlRelaxNGNewParserCtxt:
6756 * @URL: the location of the schema
6757 *
6758 * Create an XML RelaxNGs parse context for that file/resource 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 +00006764xmlRelaxNGNewParserCtxt(const char *URL)
6765{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006766 xmlRelaxNGParserCtxtPtr ret;
6767
6768 if (URL == NULL)
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));
Daniel Veillard4c004142003-10-07 11:33:24 +00006778 ret->URL = xmlStrdup((const xmlChar *) URL);
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006779 ret->error = xmlGenericError;
6780 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006781 return (ret);
6782}
6783
6784/**
6785 * xmlRelaxNGNewMemParserCtxt:
6786 * @buffer: a pointer to a char array containing the schemas
6787 * @size: the size of the array
6788 *
6789 * Create an XML RelaxNGs parse context for that memory buffer expected
6790 * to contain an XML RelaxNGs file.
6791 *
6792 * Returns the parser context or NULL in case of error
6793 */
6794xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006795xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6796{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006797 xmlRelaxNGParserCtxtPtr ret;
6798
6799 if ((buffer == NULL) || (size <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00006800 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006801
Daniel Veillard4c004142003-10-07 11:33:24 +00006802 ret =
6803 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard6eadf632003-01-23 18:29:16 +00006804 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006805 xmlRngPErrMemory(NULL, "building parser\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00006806 return (NULL);
6807 }
6808 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6809 ret->buffer = buffer;
6810 ret->size = size;
Daniel Veillard1703c5f2003-02-10 14:28:44 +00006811 ret->error = xmlGenericError;
6812 ret->userData = xmlGenericErrorContext;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006813 return (ret);
6814}
6815
6816/**
Daniel Veillard33300b42003-04-17 09:09:19 +00006817 * xmlRelaxNGNewDocParserCtxt:
6818 * @doc: a preparsed document tree
6819 *
6820 * Create an XML RelaxNGs parser context for that document.
6821 * Note: since the process of compiling a RelaxNG schemas modifies the
6822 * document, the @doc parameter is duplicated internally.
6823 *
6824 * Returns the parser context or NULL in case of error
6825 */
6826xmlRelaxNGParserCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00006827xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6828{
Daniel Veillard33300b42003-04-17 09:09:19 +00006829 xmlRelaxNGParserCtxtPtr ret;
6830 xmlDocPtr copy;
6831
6832 if (doc == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006833 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006834 copy = xmlCopyDoc(doc, 1);
6835 if (copy == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006836 return (NULL);
Daniel Veillard33300b42003-04-17 09:09:19 +00006837
Daniel Veillard4c004142003-10-07 11:33:24 +00006838 ret =
6839 (xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
Daniel Veillard33300b42003-04-17 09:09:19 +00006840 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006841 xmlRngPErrMemory(NULL, "building parser\n");
Elliott Hughesecdab2a2022-02-23 14:33:50 -08006842 xmlFreeDoc(copy);
Daniel Veillard33300b42003-04-17 09:09:19 +00006843 return (NULL);
6844 }
6845 memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6846 ret->document = copy;
Daniel Veillard42595322004-11-08 10:52:06 +00006847 ret->freedoc = 1;
Daniel Veillard33300b42003-04-17 09:09:19 +00006848 ret->userData = xmlGenericErrorContext;
6849 return (ret);
6850}
6851
6852/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00006853 * xmlRelaxNGFreeParserCtxt:
6854 * @ctxt: the schema parser context
6855 *
6856 * Free the resources associated to the schema parser context
6857 */
6858void
Daniel Veillard4c004142003-10-07 11:33:24 +00006859xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6860{
Daniel Veillard6eadf632003-01-23 18:29:16 +00006861 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006862 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00006863 if (ctxt->URL != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006864 xmlFree(ctxt->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006865 if (ctxt->doc != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006866 xmlRelaxNGFreeDocument(ctxt->doc);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00006867 if (ctxt->interleaves != NULL)
6868 xmlHashFree(ctxt->interleaves, NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006869 if (ctxt->documents != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006870 xmlRelaxNGFreeDocumentList(ctxt->documents);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00006871 if (ctxt->includes != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006872 xmlRelaxNGFreeIncludeList(ctxt->includes);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00006873 if (ctxt->docTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006874 xmlFree(ctxt->docTab);
Daniel Veillarda9d912d2003-02-01 17:43:10 +00006875 if (ctxt->incTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00006876 xmlFree(ctxt->incTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006877 if (ctxt->defTab != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006878 int i;
Daniel Veillard419a7682003-02-03 23:22:49 +00006879
Daniel Veillard4c004142003-10-07 11:33:24 +00006880 for (i = 0; i < ctxt->defNr; i++)
6881 xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6882 xmlFree(ctxt->defTab);
Daniel Veillard419a7682003-02-03 23:22:49 +00006883 }
Daniel Veillard42595322004-11-08 10:52:06 +00006884 if ((ctxt->document != NULL) && (ctxt->freedoc))
6885 xmlFreeDoc(ctxt->document);
Daniel Veillard6eadf632003-01-23 18:29:16 +00006886 xmlFree(ctxt);
6887}
6888
Daniel Veillard6eadf632003-01-23 18:29:16 +00006889/**
Daniel Veillardd2298792003-02-14 16:54:11 +00006890 * xmlRelaxNGNormExtSpace:
6891 * @value: a value
6892 *
6893 * Removes the leading and ending spaces of the value
6894 * The string is modified "in situ"
6895 */
6896static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006897xmlRelaxNGNormExtSpace(xmlChar * value)
6898{
Daniel Veillardd2298792003-02-14 16:54:11 +00006899 xmlChar *start = value;
6900 xmlChar *cur = value;
Daniel Veillardd2298792003-02-14 16:54:11 +00006901
Daniel Veillard4c004142003-10-07 11:33:24 +00006902 if (value == NULL)
6903 return;
6904
William M. Brack76e95df2003-10-18 16:20:14 +00006905 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006906 cur++;
Daniel Veillardd2298792003-02-14 16:54:11 +00006907 if (cur == start) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006908 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006909 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006910 cur++;
6911 if (*cur == 0)
6912 return;
6913 start = cur;
William M. Brack76e95df2003-10-18 16:20:14 +00006914 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006915 cur++;
6916 if (*cur == 0) {
6917 *start = 0;
6918 return;
6919 }
6920 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006921 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00006922 do {
William M. Brack76e95df2003-10-18 16:20:14 +00006923 while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
Daniel Veillard4c004142003-10-07 11:33:24 +00006924 *start++ = *cur++;
6925 if (*cur == 0) {
6926 *start = 0;
6927 return;
6928 }
6929 /* don't try to normalize the inner spaces */
William M. Brack76e95df2003-10-18 16:20:14 +00006930 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00006931 cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006932 if (*cur == 0) {
6933 *start = 0;
6934 return;
6935 }
Daniel Veillard4aede2e2003-10-17 12:43:59 +00006936 *start++ = *cur++;
Daniel Veillard4c004142003-10-07 11:33:24 +00006937 } while (1);
Daniel Veillardd2298792003-02-14 16:54:11 +00006938 }
6939}
6940
6941/**
Daniel Veillard8de5c0b2004-10-07 13:14:19 +00006942 * xmlRelaxNGCleanupAttributes:
Daniel Veillardd2298792003-02-14 16:54:11 +00006943 * @ctxt: a Relax-NG parser context
6944 * @node: a Relax-NG node
6945 *
6946 * Check all the attributes on the given node
6947 */
6948static void
Daniel Veillard4c004142003-10-07 11:33:24 +00006949xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6950{
Daniel Veillardd2298792003-02-14 16:54:11 +00006951 xmlAttrPtr cur, next;
6952
6953 cur = node->properties;
6954 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00006955 next = cur->next;
6956 if ((cur->ns == NULL) ||
6957 (xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6958 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6959 if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6960 (!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6961 (!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6962 (!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6963 (!xmlStrEqual(node->name, BAD_CAST "param")) &&
6964 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6965 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6966 "Attribute %s is not allowed on %s\n",
6967 cur->name, node->name);
6968 }
6969 } else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6970 if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6971 (!xmlStrEqual(node->name, BAD_CAST "data"))) {
6972 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6973 "Attribute %s is not allowed on %s\n",
6974 cur->name, node->name);
6975 }
6976 } else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6977 if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6978 (!xmlStrEqual(node->name, BAD_CAST "include"))) {
6979 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6980 "Attribute %s is not allowed on %s\n",
6981 cur->name, node->name);
6982 }
6983 } else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6984 if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6985 (!xmlStrEqual(node->name, BAD_CAST "define"))) {
6986 xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6987 "Attribute %s is not allowed on %s\n",
6988 cur->name, node->name);
6989 }
6990 } else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6991 xmlChar *val;
6992 xmlURIPtr uri;
Daniel Veillardd2298792003-02-14 16:54:11 +00006993
Daniel Veillard4c004142003-10-07 11:33:24 +00006994 val = xmlNodeListGetString(node->doc, cur->children, 1);
6995 if (val != NULL) {
6996 if (val[0] != 0) {
6997 uri = xmlParseURI((const char *) val);
6998 if (uri == NULL) {
6999 xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
7000 "Attribute %s contains invalid URI %s\n",
7001 cur->name, val);
7002 } else {
7003 if (uri->scheme == NULL) {
7004 xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
7005 "Attribute %s URI %s is not absolute\n",
7006 cur->name, val);
7007 }
7008 if (uri->fragment != NULL) {
7009 xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
7010 "Attribute %s URI %s has a fragment ID\n",
7011 cur->name, val);
7012 }
7013 xmlFreeURI(uri);
7014 }
7015 }
7016 xmlFree(val);
7017 }
7018 } else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
7019 xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
7020 "Unknown attribute %s on %s\n", cur->name,
7021 node->name);
7022 }
7023 }
7024 cur = next;
Daniel Veillardd2298792003-02-14 16:54:11 +00007025 }
7026}
7027
7028/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00007029 * xmlRelaxNGCleanupTree:
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007030 * @ctxt: a Relax-NG parser context
Daniel Veillardc5312d72003-02-21 17:14:10 +00007031 * @root: an xmlNodePtr subtree
Daniel Veillard6eadf632003-01-23 18:29:16 +00007032 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00007033 * Cleanup the subtree from unwanted nodes for parsing, resolve
7034 * Include and externalRef lookups.
Daniel Veillard6eadf632003-01-23 18:29:16 +00007035 */
Daniel Veillardc5312d72003-02-21 17:14:10 +00007036static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007037xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
7038{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007039 xmlNodePtr cur, delete;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007040
Daniel Veillard6eadf632003-01-23 18:29:16 +00007041 delete = NULL;
7042 cur = root;
7043 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007044 if (delete != NULL) {
7045 xmlUnlinkNode(delete);
7046 xmlFreeNode(delete);
7047 delete = NULL;
7048 }
7049 if (cur->type == XML_ELEMENT_NODE) {
7050 /*
7051 * Simplification 4.1. Annotations
7052 */
7053 if ((cur->ns == NULL) ||
7054 (!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
7055 if ((cur->parent != NULL) &&
7056 (cur->parent->type == XML_ELEMENT_NODE) &&
7057 ((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
7058 (xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
7059 (xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
7060 xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
7061 "element %s doesn't allow foreign elements\n",
7062 cur->parent->name, NULL);
7063 }
7064 delete = cur;
7065 goto skip_children;
7066 } else {
7067 xmlRelaxNGCleanupAttributes(ctxt, cur);
7068 if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
7069 xmlChar *href, *ns, *base, *URL;
7070 xmlRelaxNGDocumentPtr docu;
7071 xmlNodePtr tmp;
Daniel Veillard6dc91962004-03-22 19:10:02 +00007072 xmlURIPtr uri;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007073
Daniel Veillard4c004142003-10-07 11:33:24 +00007074 ns = xmlGetProp(cur, BAD_CAST "ns");
7075 if (ns == NULL) {
7076 tmp = cur->parent;
7077 while ((tmp != NULL) &&
7078 (tmp->type == XML_ELEMENT_NODE)) {
7079 ns = xmlGetProp(tmp, BAD_CAST "ns");
7080 if (ns != NULL)
7081 break;
7082 tmp = tmp->parent;
7083 }
7084 }
7085 href = xmlGetProp(cur, BAD_CAST "href");
7086 if (href == NULL) {
7087 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7088 "xmlRelaxNGParse: externalRef has no href attribute\n",
7089 NULL, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007090 if (ns != NULL)
7091 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007092 delete = cur;
7093 goto skip_children;
7094 }
Daniel Veillard6dc91962004-03-22 19:10:02 +00007095 uri = xmlParseURI((const char *) href);
7096 if (uri == NULL) {
7097 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7098 "Incorrect URI for externalRef %s\n",
7099 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007100 if (ns != NULL)
7101 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007102 if (href != NULL)
7103 xmlFree(href);
7104 delete = cur;
7105 goto skip_children;
7106 }
7107 if (uri->fragment != NULL) {
7108 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7109 "Fragment forbidden in URI for externalRef %s\n",
7110 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007111 if (ns != NULL)
7112 xmlFree(ns);
Daniel Veillard6dc91962004-03-22 19:10:02 +00007113 xmlFreeURI(uri);
7114 if (href != NULL)
7115 xmlFree(href);
7116 delete = cur;
7117 goto skip_children;
7118 }
7119 xmlFreeURI(uri);
Daniel Veillard4c004142003-10-07 11:33:24 +00007120 base = xmlNodeGetBase(cur->doc, cur);
7121 URL = xmlBuildURI(href, base);
7122 if (URL == NULL) {
7123 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7124 "Failed to compute URL for externalRef %s\n",
7125 href, NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007126 if (ns != NULL)
7127 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007128 if (href != NULL)
7129 xmlFree(href);
7130 if (base != NULL)
7131 xmlFree(base);
7132 delete = cur;
7133 goto skip_children;
7134 }
7135 if (href != NULL)
7136 xmlFree(href);
7137 if (base != NULL)
7138 xmlFree(base);
7139 docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
7140 if (docu == NULL) {
7141 xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
7142 "Failed to load externalRef %s\n", URL,
7143 NULL);
Daniel Veillardf2e066a2005-06-30 13:04:44 +00007144 if (ns != NULL)
7145 xmlFree(ns);
Daniel Veillard4c004142003-10-07 11:33:24 +00007146 xmlFree(URL);
7147 delete = cur;
7148 goto skip_children;
7149 }
7150 if (ns != NULL)
7151 xmlFree(ns);
7152 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007153 cur->psvi = docu;
Daniel Veillard4c004142003-10-07 11:33:24 +00007154 } else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
7155 xmlChar *href, *ns, *base, *URL;
7156 xmlRelaxNGIncludePtr incl;
7157 xmlNodePtr tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007158
Daniel Veillard4c004142003-10-07 11:33:24 +00007159 href = xmlGetProp(cur, BAD_CAST "href");
7160 if (href == NULL) {
7161 xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
7162 "xmlRelaxNGParse: include has no href attribute\n",
7163 NULL, NULL);
7164 delete = cur;
7165 goto skip_children;
7166 }
7167 base = xmlNodeGetBase(cur->doc, cur);
7168 URL = xmlBuildURI(href, base);
7169 if (URL == NULL) {
7170 xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7171 "Failed to compute URL for include %s\n",
7172 href, NULL);
7173 if (href != NULL)
7174 xmlFree(href);
7175 if (base != NULL)
7176 xmlFree(base);
7177 delete = cur;
7178 goto skip_children;
7179 }
7180 if (href != NULL)
7181 xmlFree(href);
7182 if (base != NULL)
7183 xmlFree(base);
7184 ns = xmlGetProp(cur, BAD_CAST "ns");
7185 if (ns == NULL) {
7186 tmp = cur->parent;
7187 while ((tmp != NULL) &&
7188 (tmp->type == XML_ELEMENT_NODE)) {
7189 ns = xmlGetProp(tmp, BAD_CAST "ns");
7190 if (ns != NULL)
7191 break;
7192 tmp = tmp->parent;
7193 }
7194 }
7195 incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7196 if (ns != NULL)
7197 xmlFree(ns);
7198 if (incl == NULL) {
7199 xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7200 "Failed to load include %s\n", URL,
7201 NULL);
7202 xmlFree(URL);
7203 delete = cur;
7204 goto skip_children;
7205 }
7206 xmlFree(URL);
Daniel Veillard807daf82004-02-22 22:13:27 +00007207 cur->psvi = incl;
Daniel Veillard4c004142003-10-07 11:33:24 +00007208 } else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7209 (xmlStrEqual(cur->name, BAD_CAST "attribute")))
7210 {
7211 xmlChar *name, *ns;
7212 xmlNodePtr text = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007213
Daniel Veillard4c004142003-10-07 11:33:24 +00007214 /*
7215 * Simplification 4.8. name attribute of element
7216 * and attribute elements
7217 */
7218 name = xmlGetProp(cur, BAD_CAST "name");
7219 if (name != NULL) {
7220 if (cur->children == NULL) {
7221 text =
7222 xmlNewChild(cur, cur->ns, BAD_CAST "name",
7223 name);
7224 } else {
7225 xmlNodePtr node;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007226
Daniel Veillard03a53c32004-10-26 16:06:51 +00007227 node = xmlNewDocNode(cur->doc, cur->ns,
7228 BAD_CAST "name", NULL);
Daniel Veillard4c004142003-10-07 11:33:24 +00007229 if (node != NULL) {
7230 xmlAddPrevSibling(cur->children, node);
7231 text = xmlNewText(name);
7232 xmlAddChild(node, text);
7233 text = node;
7234 }
7235 }
7236 if (text == NULL) {
7237 xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7238 "Failed to create a name %s element\n",
7239 name, NULL);
7240 }
7241 xmlUnsetProp(cur, BAD_CAST "name");
7242 xmlFree(name);
7243 ns = xmlGetProp(cur, BAD_CAST "ns");
7244 if (ns != NULL) {
7245 if (text != NULL) {
7246 xmlSetProp(text, BAD_CAST "ns", ns);
7247 /* xmlUnsetProp(cur, BAD_CAST "ns"); */
7248 }
7249 xmlFree(ns);
7250 } else if (xmlStrEqual(cur->name,
7251 BAD_CAST "attribute")) {
7252 xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7253 }
7254 }
7255 } else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7256 (xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7257 (xmlStrEqual(cur->name, BAD_CAST "value"))) {
7258 /*
7259 * Simplification 4.8. name attribute of element
7260 * and attribute elements
7261 */
7262 if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7263 xmlNodePtr node;
7264 xmlChar *ns = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007265
Daniel Veillard4c004142003-10-07 11:33:24 +00007266 node = cur->parent;
7267 while ((node != NULL) &&
7268 (node->type == XML_ELEMENT_NODE)) {
7269 ns = xmlGetProp(node, BAD_CAST "ns");
7270 if (ns != NULL) {
7271 break;
7272 }
7273 node = node->parent;
7274 }
7275 if (ns == NULL) {
7276 xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7277 } else {
7278 xmlSetProp(cur, BAD_CAST "ns", ns);
7279 xmlFree(ns);
7280 }
7281 }
7282 if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7283 xmlChar *name, *local, *prefix;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007284
Daniel Veillard4c004142003-10-07 11:33:24 +00007285 /*
7286 * Simplification: 4.10. QNames
7287 */
7288 name = xmlNodeGetContent(cur);
7289 if (name != NULL) {
7290 local = xmlSplitQName2(name, &prefix);
7291 if (local != NULL) {
7292 xmlNsPtr ns;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007293
Daniel Veillard4c004142003-10-07 11:33:24 +00007294 ns = xmlSearchNs(cur->doc, cur, prefix);
7295 if (ns == NULL) {
7296 xmlRngPErr(ctxt, cur,
7297 XML_RNGP_PREFIX_UNDEFINED,
7298 "xmlRelaxNGParse: no namespace for prefix %s\n",
7299 prefix, NULL);
7300 } else {
7301 xmlSetProp(cur, BAD_CAST "ns",
7302 ns->href);
7303 xmlNodeSetContent(cur, local);
7304 }
7305 xmlFree(local);
7306 xmlFree(prefix);
7307 }
7308 xmlFree(name);
7309 }
7310 }
7311 /*
7312 * 4.16
7313 */
7314 if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7315 if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7316 xmlRngPErr(ctxt, cur,
7317 XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7318 "Found nsName/except//nsName forbidden construct\n",
7319 NULL, NULL);
7320 }
7321 }
7322 } else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7323 (cur != root)) {
7324 int oldflags = ctxt->flags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007325
Daniel Veillard4c004142003-10-07 11:33:24 +00007326 /*
7327 * 4.16
7328 */
7329 if ((cur->parent != NULL) &&
7330 (xmlStrEqual
7331 (cur->parent->name, BAD_CAST "anyName"))) {
7332 ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7333 xmlRelaxNGCleanupTree(ctxt, cur);
7334 ctxt->flags = oldflags;
7335 goto skip_children;
7336 } else if ((cur->parent != NULL) &&
7337 (xmlStrEqual
7338 (cur->parent->name, BAD_CAST "nsName"))) {
7339 ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7340 xmlRelaxNGCleanupTree(ctxt, cur);
7341 ctxt->flags = oldflags;
7342 goto skip_children;
7343 }
7344 } else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7345 /*
7346 * 4.16
7347 */
7348 if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7349 xmlRngPErr(ctxt, cur,
7350 XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7351 "Found anyName/except//anyName forbidden construct\n",
7352 NULL, NULL);
7353 } else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7354 xmlRngPErr(ctxt, cur,
7355 XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7356 "Found nsName/except//anyName forbidden construct\n",
7357 NULL, NULL);
7358 }
7359 }
7360 /*
Jan Pokornýacace882014-06-09 23:45:24 +02007361 * This is not an else since "include" is transformed
Daniel Veillard4c004142003-10-07 11:33:24 +00007362 * into a div
7363 */
7364 if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7365 xmlChar *ns;
7366 xmlNodePtr child, ins, tmp;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007367
Daniel Veillard4c004142003-10-07 11:33:24 +00007368 /*
7369 * implements rule 4.11
7370 */
Daniel Veillard6eadf632003-01-23 18:29:16 +00007371
Daniel Veillard4c004142003-10-07 11:33:24 +00007372 ns = xmlGetProp(cur, BAD_CAST "ns");
7373
7374 child = cur->children;
7375 ins = cur;
7376 while (child != NULL) {
7377 if (ns != NULL) {
7378 if (!xmlHasProp(child, BAD_CAST "ns")) {
7379 xmlSetProp(child, BAD_CAST "ns", ns);
7380 }
7381 }
7382 tmp = child->next;
7383 xmlUnlinkNode(child);
7384 ins = xmlAddNextSibling(ins, child);
7385 child = tmp;
7386 }
7387 if (ns != NULL)
7388 xmlFree(ns);
William M. Brack8eabb052004-06-07 14:15:54 +00007389 /*
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007390 * Since we are about to delete cur, if its nsDef is non-NULL we
William M. Brack8eabb052004-06-07 14:15:54 +00007391 * need to preserve it (it contains the ns definitions for the
7392 * children we just moved). We'll just stick it on to the end
7393 * of cur->parent's list, since it's never going to be re-serialized
7394 * (bug 143738).
7395 */
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007396 if ((cur->nsDef != NULL) && (cur->parent != NULL)) {
William M. Brack8eabb052004-06-07 14:15:54 +00007397 xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7398 while (parDef->next != NULL)
7399 parDef = parDef->next;
7400 parDef->next = cur->nsDef;
7401 cur->nsDef = NULL;
7402 }
Daniel Veillard4c004142003-10-07 11:33:24 +00007403 delete = cur;
7404 goto skip_children;
7405 }
7406 }
7407 }
7408 /*
7409 * Simplification 4.2 whitespaces
7410 */
7411 else if ((cur->type == XML_TEXT_NODE) ||
7412 (cur->type == XML_CDATA_SECTION_NODE)) {
7413 if (IS_BLANK_NODE(cur)) {
Gaurav Gupta54c4b1a2014-07-14 16:14:44 +08007414 if ((cur->parent != NULL) &&
7415 (cur->parent->type == XML_ELEMENT_NODE)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007416 if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7417 &&
7418 (!xmlStrEqual
7419 (cur->parent->name, BAD_CAST "param")))
7420 delete = cur;
7421 } else {
7422 delete = cur;
7423 goto skip_children;
7424 }
7425 }
7426 } else {
7427 delete = cur;
7428 goto skip_children;
7429 }
7430
7431 /*
7432 * Skip to next node
7433 */
7434 if (cur->children != NULL) {
7435 if ((cur->children->type != XML_ENTITY_DECL) &&
7436 (cur->children->type != XML_ENTITY_REF_NODE) &&
7437 (cur->children->type != XML_ENTITY_NODE)) {
7438 cur = cur->children;
7439 continue;
7440 }
7441 }
7442 skip_children:
7443 if (cur->next != NULL) {
7444 cur = cur->next;
7445 continue;
7446 }
7447
7448 do {
7449 cur = cur->parent;
7450 if (cur == NULL)
7451 break;
7452 if (cur == root) {
7453 cur = NULL;
7454 break;
7455 }
7456 if (cur->next != NULL) {
7457 cur = cur->next;
7458 break;
7459 }
7460 } while (cur != NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007461 }
7462 if (delete != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007463 xmlUnlinkNode(delete);
7464 xmlFreeNode(delete);
7465 delete = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007466 }
Daniel Veillardc5312d72003-02-21 17:14:10 +00007467}
Daniel Veillard6eadf632003-01-23 18:29:16 +00007468
Daniel Veillardc5312d72003-02-21 17:14:10 +00007469/**
7470 * xmlRelaxNGCleanupDoc:
7471 * @ctxt: a Relax-NG parser context
7472 * @doc: an xmldocPtr document pointer
7473 *
7474 * Cleanup the document from unwanted nodes for parsing, resolve
7475 * Include and externalRef lookups.
7476 *
7477 * Returns the cleaned up document or NULL in case of error
7478 */
7479static xmlDocPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00007480xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7481{
Daniel Veillardc5312d72003-02-21 17:14:10 +00007482 xmlNodePtr root;
7483
7484 /*
7485 * Extract the root
7486 */
7487 root = xmlDocGetRootElement(doc);
7488 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007489 xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7490 ctxt->URL, NULL);
Daniel Veillardc5312d72003-02-21 17:14:10 +00007491 return (NULL);
7492 }
7493 xmlRelaxNGCleanupTree(ctxt, root);
Daniel Veillard4c004142003-10-07 11:33:24 +00007494 return (doc);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007495}
7496
7497/**
7498 * xmlRelaxNGParse:
7499 * @ctxt: a Relax-NG parser context
7500 *
7501 * parse a schema definition resource and build an internal
Haibo Huangcfd91dc2020-07-30 23:01:33 -07007502 * XML Schema structure which can be used to validate instances.
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007503 *
7504 * Returns the internal XML RelaxNG structure built from the resource or
7505 * NULL in case of error
7506 */
7507xmlRelaxNGPtr
7508xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7509{
7510 xmlRelaxNGPtr ret = NULL;
7511 xmlDocPtr doc;
7512 xmlNodePtr root;
7513
7514 xmlRelaxNGInitTypes();
7515
7516 if (ctxt == NULL)
7517 return (NULL);
7518
7519 /*
7520 * First step is to parse the input document into an DOM/Infoset
7521 */
7522 if (ctxt->URL != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007523 doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007524 if (doc == NULL) {
7525 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7526 "xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7527 NULL);
7528 return (NULL);
7529 }
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007530 } else if (ctxt->buffer != NULL) {
Daniel Veillard87247e82004-01-13 20:42:02 +00007531 doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
Daniel Veillard4c004142003-10-07 11:33:24 +00007532 if (doc == NULL) {
7533 xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7534 "xmlRelaxNGParse: could not parse schemas\n", NULL,
7535 NULL);
7536 return (NULL);
7537 }
7538 doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7539 ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
Daniel Veillard33300b42003-04-17 09:09:19 +00007540 } else if (ctxt->document != NULL) {
7541 doc = ctxt->document;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007542 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007543 xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7544 "xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7545 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007546 }
7547 ctxt->document = doc;
7548
7549 /*
7550 * Some preprocessing of the document content
7551 */
7552 doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7553 if (doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007554 xmlFreeDoc(ctxt->document);
7555 ctxt->document = NULL;
7556 return (NULL);
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007557 }
7558
Daniel Veillard6eadf632003-01-23 18:29:16 +00007559 /*
7560 * Then do the parsing for good
7561 */
7562 root = xmlDocGetRootElement(doc);
7563 if (root == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007564 xmlRngPErr(ctxt, (xmlNodePtr) doc,
7565 XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
William M. Brack700f9872006-05-06 03:16:22 +00007566 (ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007567
Daniel Veillard3f845a92006-04-13 07:33:44 +00007568 xmlFreeDoc(ctxt->document);
7569 ctxt->document = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007570 return (NULL);
7571 }
7572 ret = xmlRelaxNGParseDocument(ctxt, root);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007573 if (ret == NULL) {
Daniel Veillard3f845a92006-04-13 07:33:44 +00007574 xmlFreeDoc(ctxt->document);
7575 ctxt->document = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007576 return (NULL);
Daniel Veillard3ebc7d42003-02-24 17:17:58 +00007577 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00007578
7579 /*
Daniel Veillardfd573f12003-03-16 17:52:32 +00007580 * Check the ref/defines links
7581 */
7582 /*
7583 * try to preprocess interleaves
7584 */
7585 if (ctxt->interleaves != NULL) {
Nick Wellnhofere03f0a12017-11-09 16:42:47 +01007586 xmlHashScan(ctxt->interleaves, xmlRelaxNGComputeInterleaves, ctxt);
Daniel Veillardfd573f12003-03-16 17:52:32 +00007587 }
7588
7589 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007590 * if there was a parsing error return NULL
7591 */
7592 if (ctxt->nbErrors > 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007593 xmlRelaxNGFree(ret);
7594 ctxt->document = NULL;
7595 xmlFreeDoc(doc);
7596 return (NULL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007597 }
7598
7599 /*
Daniel Veillard52b48c72003-04-13 19:53:42 +00007600 * try to compile (parts of) the schemas
7601 */
Daniel Veillardce192eb2003-04-16 15:58:05 +00007602 if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7603 if (ret->topgrammar->start->type != XML_RELAXNG_START) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007604 xmlRelaxNGDefinePtr def;
Daniel Veillardf4e55762003-04-15 23:32:22 +00007605
Daniel Veillard4c004142003-10-07 11:33:24 +00007606 def = xmlRelaxNGNewDefine(ctxt, NULL);
7607 if (def != NULL) {
7608 def->type = XML_RELAXNG_START;
7609 def->content = ret->topgrammar->start;
7610 ret->topgrammar->start = def;
7611 }
7612 }
7613 xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
Daniel Veillardf4e55762003-04-15 23:32:22 +00007614 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00007615
7616 /*
Daniel Veillard6eadf632003-01-23 18:29:16 +00007617 * Transfer the pointer for cleanup at the schema level.
7618 */
7619 ret->doc = doc;
Daniel Veillardd41f4f42003-01-29 21:07:52 +00007620 ctxt->document = NULL;
7621 ret->documents = ctxt->documents;
7622 ctxt->documents = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00007623
Daniel Veillarde2a5a082003-02-02 14:35:17 +00007624 ret->includes = ctxt->includes;
7625 ctxt->includes = NULL;
Daniel Veillard419a7682003-02-03 23:22:49 +00007626 ret->defNr = ctxt->defNr;
7627 ret->defTab = ctxt->defTab;
7628 ctxt->defTab = NULL;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00007629 if (ctxt->idref == 1)
Daniel Veillard4c004142003-10-07 11:33:24 +00007630 ret->idref = 1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007631
7632 return (ret);
7633}
Daniel Veillard4c004142003-10-07 11:33:24 +00007634
Daniel Veillard6eadf632003-01-23 18:29:16 +00007635/**
7636 * xmlRelaxNGSetParserErrors:
7637 * @ctxt: a Relax-NG validation context
7638 * @err: the error callback
7639 * @warn: the warning callback
7640 * @ctx: contextual data for the callbacks
7641 *
7642 * Set the callback functions used to handle errors for a validation context
7643 */
7644void
7645xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007646 xmlRelaxNGValidityErrorFunc err,
7647 xmlRelaxNGValidityWarningFunc warn, void *ctx)
7648{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007649 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007650 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007651 ctxt->error = err;
7652 ctxt->warning = warn;
Daniel Veillardb30ca312005-09-04 13:50:03 +00007653 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007654 ctxt->userData = ctx;
7655}
Daniel Veillard409a8142003-07-18 15:16:57 +00007656
7657/**
7658 * xmlRelaxNGGetParserErrors:
7659 * @ctxt: a Relax-NG validation context
7660 * @err: the error callback result
7661 * @warn: the warning callback result
7662 * @ctx: contextual data for the callbacks result
7663 *
7664 * Get the callback information used to handle errors for a validation context
7665 *
7666 * Returns -1 in case of failure, 0 otherwise.
7667 */
7668int
7669xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00007670 xmlRelaxNGValidityErrorFunc * err,
7671 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7672{
Daniel Veillard409a8142003-07-18 15:16:57 +00007673 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007674 return (-1);
7675 if (err != NULL)
7676 *err = ctxt->error;
7677 if (warn != NULL)
7678 *warn = ctxt->warning;
7679 if (ctx != NULL)
7680 *ctx = ctxt->userData;
7681 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +00007682}
7683
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007684/**
7685 * xmlRelaxNGSetParserStructuredErrors:
7686 * @ctxt: a Relax-NG parser context
7687 * @serror: the error callback
7688 * @ctx: contextual data for the callbacks
7689 *
7690 * Set the callback functions used to handle errors for a parsing context
7691 */
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007692void
Daniel Veillardb2f8f1d2006-04-28 16:30:48 +00007693xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
Kasimier T. Buchcika930fbe2006-01-09 16:28:20 +00007694 xmlStructuredErrorFunc serror,
7695 void *ctx)
7696{
7697 if (ctxt == NULL)
7698 return;
7699 ctxt->serror = serror;
7700 ctxt->error = NULL;
7701 ctxt->warning = NULL;
7702 ctxt->userData = ctx;
7703}
7704
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007705#ifdef LIBXML_OUTPUT_ENABLED
Daniel Veillard4c004142003-10-07 11:33:24 +00007706
Daniel Veillard6eadf632003-01-23 18:29:16 +00007707/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007708 * *
7709 * Dump back a compiled form *
7710 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007711 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007712static void xmlRelaxNGDumpDefine(FILE * output,
7713 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007714
7715/**
7716 * xmlRelaxNGDumpDefines:
7717 * @output: the file output
7718 * @defines: a list of define structures
7719 *
7720 * Dump a RelaxNG structure back
7721 */
7722static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007723xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7724{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007725 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007726 xmlRelaxNGDumpDefine(output, defines);
7727 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007728 }
7729}
7730
7731/**
7732 * xmlRelaxNGDumpDefine:
7733 * @output: the file output
7734 * @define: a define structure
7735 *
7736 * Dump a RelaxNG structure back
7737 */
7738static void
Daniel Veillard4c004142003-10-07 11:33:24 +00007739xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7740{
Daniel Veillard6eadf632003-01-23 18:29:16 +00007741 if (define == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007742 return;
7743 switch (define->type) {
Daniel Veillard6eadf632003-01-23 18:29:16 +00007744 case XML_RELAXNG_EMPTY:
Daniel Veillard4c004142003-10-07 11:33:24 +00007745 fprintf(output, "<empty/>\n");
7746 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007747 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillard4c004142003-10-07 11:33:24 +00007748 fprintf(output, "<notAllowed/>\n");
7749 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007750 case XML_RELAXNG_TEXT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007751 fprintf(output, "<text/>\n");
7752 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007753 case XML_RELAXNG_ELEMENT:
Daniel Veillard4c004142003-10-07 11:33:24 +00007754 fprintf(output, "<element>\n");
7755 if (define->name != NULL) {
7756 fprintf(output, "<name");
7757 if (define->ns != NULL)
7758 fprintf(output, " ns=\"%s\"", define->ns);
7759 fprintf(output, ">%s</name>\n", define->name);
7760 }
7761 xmlRelaxNGDumpDefines(output, define->attrs);
7762 xmlRelaxNGDumpDefines(output, define->content);
7763 fprintf(output, "</element>\n");
7764 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007765 case XML_RELAXNG_LIST:
Daniel Veillard4c004142003-10-07 11:33:24 +00007766 fprintf(output, "<list>\n");
7767 xmlRelaxNGDumpDefines(output, define->content);
7768 fprintf(output, "</list>\n");
7769 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007770 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007771 fprintf(output, "<oneOrMore>\n");
7772 xmlRelaxNGDumpDefines(output, define->content);
7773 fprintf(output, "</oneOrMore>\n");
7774 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00007775 case XML_RELAXNG_ZEROORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007776 fprintf(output, "<zeroOrMore>\n");
7777 xmlRelaxNGDumpDefines(output, define->content);
7778 fprintf(output, "</zeroOrMore>\n");
7779 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007780 case XML_RELAXNG_CHOICE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007781 fprintf(output, "<choice>\n");
7782 xmlRelaxNGDumpDefines(output, define->content);
7783 fprintf(output, "</choice>\n");
7784 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007785 case XML_RELAXNG_GROUP:
Daniel Veillard4c004142003-10-07 11:33:24 +00007786 fprintf(output, "<group>\n");
7787 xmlRelaxNGDumpDefines(output, define->content);
7788 fprintf(output, "</group>\n");
7789 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007790 case XML_RELAXNG_INTERLEAVE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007791 fprintf(output, "<interleave>\n");
7792 xmlRelaxNGDumpDefines(output, define->content);
7793 fprintf(output, "</interleave>\n");
7794 break;
7795 case XML_RELAXNG_OPTIONAL:
7796 fprintf(output, "<optional>\n");
7797 xmlRelaxNGDumpDefines(output, define->content);
7798 fprintf(output, "</optional>\n");
7799 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007800 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007801 fprintf(output, "<attribute>\n");
7802 xmlRelaxNGDumpDefines(output, define->content);
7803 fprintf(output, "</attribute>\n");
7804 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007805 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007806 fprintf(output, "<define");
7807 if (define->name != NULL)
7808 fprintf(output, " name=\"%s\"", define->name);
7809 fprintf(output, ">\n");
7810 xmlRelaxNGDumpDefines(output, define->content);
7811 fprintf(output, "</define>\n");
7812 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007813 case XML_RELAXNG_REF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007814 fprintf(output, "<ref");
7815 if (define->name != NULL)
7816 fprintf(output, " name=\"%s\"", define->name);
7817 fprintf(output, ">\n");
7818 xmlRelaxNGDumpDefines(output, define->content);
7819 fprintf(output, "</ref>\n");
7820 break;
Daniel Veillard419a7682003-02-03 23:22:49 +00007821 case XML_RELAXNG_PARENTREF:
Daniel Veillard4c004142003-10-07 11:33:24 +00007822 fprintf(output, "<parentRef");
7823 if (define->name != NULL)
7824 fprintf(output, " name=\"%s\"", define->name);
7825 fprintf(output, ">\n");
7826 xmlRelaxNGDumpDefines(output, define->content);
7827 fprintf(output, "</parentRef>\n");
7828 break;
7829 case XML_RELAXNG_EXTERNALREF:
7830 fprintf(output, "<externalRef>");
7831 xmlRelaxNGDumpDefines(output, define->content);
7832 fprintf(output, "</externalRef>\n");
7833 break;
Daniel Veillarde431a272003-01-29 23:02:33 +00007834 case XML_RELAXNG_DATATYPE:
Daniel Veillard6eadf632003-01-23 18:29:16 +00007835 case XML_RELAXNG_VALUE:
Daniel Veillard4c004142003-10-07 11:33:24 +00007836 TODO break;
7837 case XML_RELAXNG_START:
7838 case XML_RELAXNG_EXCEPT:
7839 case XML_RELAXNG_PARAM:
7840 TODO break;
7841 case XML_RELAXNG_NOOP:
7842 xmlRelaxNGDumpDefines(output, define->content);
7843 break;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007844 }
7845}
Daniel Veillard4c004142003-10-07 11:33:24 +00007846
Daniel Veillard6eadf632003-01-23 18:29:16 +00007847/**
7848 * xmlRelaxNGDumpGrammar:
7849 * @output: the file output
7850 * @grammar: a grammar structure
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007851 * @top: is this a top grammar
Daniel Veillard6eadf632003-01-23 18:29:16 +00007852 *
7853 * Dump a RelaxNG structure back
7854 */
7855static void
7856xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7857{
7858 if (grammar == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00007859 return;
7860
Daniel Veillard6eadf632003-01-23 18:29:16 +00007861 fprintf(output, "<grammar");
7862 if (top)
Daniel Veillard4c004142003-10-07 11:33:24 +00007863 fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7864 switch (grammar->combine) {
7865 case XML_RELAXNG_COMBINE_UNDEFINED:
7866 break;
7867 case XML_RELAXNG_COMBINE_CHOICE:
7868 fprintf(output, " combine=\"choice\"");
7869 break;
7870 case XML_RELAXNG_COMBINE_INTERLEAVE:
7871 fprintf(output, " combine=\"interleave\"");
7872 break;
7873 default:
7874 fprintf(output, " <!-- invalid combine value -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007875 }
7876 fprintf(output, ">\n");
7877 if (grammar->start == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007878 fprintf(output, " <!-- grammar had no start -->");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007879 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007880 fprintf(output, "<start>\n");
7881 xmlRelaxNGDumpDefine(output, grammar->start);
7882 fprintf(output, "</start>\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007883 }
7884 /* TODO ? Dump the defines ? */
7885 fprintf(output, "</grammar>\n");
7886}
7887
7888/**
7889 * xmlRelaxNGDump:
7890 * @output: the file output
7891 * @schema: a schema structure
7892 *
7893 * Dump a RelaxNG structure back
7894 */
7895void
7896xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7897{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007898 if (output == NULL)
7899 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007900 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007901 fprintf(output, "RelaxNG empty or failed to compile\n");
7902 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007903 }
7904 fprintf(output, "RelaxNG: ");
7905 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007906 fprintf(output, "no document\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007907 } else if (schema->doc->URL != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007908 fprintf(output, "%s\n", schema->doc->URL);
Daniel Veillard6eadf632003-01-23 18:29:16 +00007909 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007910 fprintf(output, "\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +00007911 }
7912 if (schema->topgrammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007913 fprintf(output, "RelaxNG has no top grammar\n");
7914 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +00007915 }
7916 xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7917}
7918
Daniel Veillardfebcca42003-02-16 15:44:18 +00007919/**
7920 * xmlRelaxNGDumpTree:
7921 * @output: the file output
7922 * @schema: a schema structure
7923 *
7924 * Dump the transformed RelaxNG tree.
7925 */
7926void
7927xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7928{
Daniel Veillardce682bc2004-11-05 17:22:25 +00007929 if (output == NULL)
7930 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007931 if (schema == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007932 fprintf(output, "RelaxNG empty or failed to compile\n");
7933 return;
Daniel Veillardfebcca42003-02-16 15:44:18 +00007934 }
7935 if (schema->doc == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007936 fprintf(output, "no document\n");
Daniel Veillardfebcca42003-02-16 15:44:18 +00007937 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00007938 xmlDocDump(output, schema->doc);
Daniel Veillardfebcca42003-02-16 15:44:18 +00007939 }
7940}
Daniel Veillarda9cce9c2003-09-29 13:20:24 +00007941#endif /* LIBXML_OUTPUT_ENABLED */
Daniel Veillardfebcca42003-02-16 15:44:18 +00007942
Daniel Veillard6eadf632003-01-23 18:29:16 +00007943/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08007944 * *
7945 * Validation of compiled content *
7946 * *
Daniel Veillard6eadf632003-01-23 18:29:16 +00007947 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00007948static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7949 xmlRelaxNGDefinePtr define);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007950
7951/**
7952 * xmlRelaxNGValidateCompiledCallback:
7953 * @exec: the regular expression instance
7954 * @token: the token which matched
7955 * @transdata: callback data, the define for the subelement if available
7956 @ @inputdata: callback data, the Relax NG validation context
7957 *
7958 * Handle the callback and if needed validate the element children.
7959 */
Daniel Veillard4c004142003-10-07 11:33:24 +00007960static void
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007961xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00007962 const xmlChar * token,
7963 void *transdata, void *inputdata)
7964{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007965 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7966 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7967 int ret;
7968
7969#ifdef DEBUG_COMPILE
7970 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00007971 "Compiled callback for: '%s'\n", token);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007972#endif
7973 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007974 fprintf(stderr, "callback on %s missing context\n", token);
Daniel Veillard4c004142003-10-07 11:33:24 +00007975 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007976 }
7977 if (define == NULL) {
7978 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00007979 return;
7980 fprintf(stderr, "callback on %s missing define\n", token);
7981 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7982 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7983 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007984 }
7985 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007986 fprintf(stderr, "callback on %s missing info\n", token);
7987 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7988 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7989 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007990 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00007991 fprintf(stderr, "callback on %s define is not element\n", token);
7992 if (ctxt->errNo == XML_RELAXNG_OK)
7993 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7994 return;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007995 }
7996 ret = xmlRelaxNGValidateDefinition(ctxt, define);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00007997 if (ret != 0)
7998 ctxt->perr = ret;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00007999}
8000
8001/**
8002 * xmlRelaxNGValidateCompiledContent:
8003 * @ctxt: the RelaxNG validation context
8004 * @regexp: the regular expression as compiled
8005 * @content: list of children to test against the regexp
8006 *
8007 * Validate the content model of an element or start using the regexp
8008 *
8009 * Returns 0 in case of success, -1 in case of error.
8010 */
8011static int
8012xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008013 xmlRegexpPtr regexp, xmlNodePtr content)
8014{
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008015 xmlRegExecCtxtPtr exec;
8016 xmlNodePtr cur;
Daniel Veillard62163602003-04-17 09:36:38 +00008017 int ret = 0;
Daniel Veillard14b56432006-03-09 18:41:40 +00008018 int oldperr;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008019
8020 if ((ctxt == NULL) || (regexp == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008021 return (-1);
Daniel Veillard14b56432006-03-09 18:41:40 +00008022 oldperr = ctxt->perr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008023 exec = xmlRegNewExecCtxt(regexp,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008024 xmlRelaxNGValidateCompiledCallback, ctxt);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008025 ctxt->perr = 0;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008026 cur = content;
8027 while (cur != NULL) {
8028 ctxt->state->seq = cur;
Daniel Veillard4c004142003-10-07 11:33:24 +00008029 switch (cur->type) {
8030 case XML_TEXT_NODE:
8031 case XML_CDATA_SECTION_NODE:
8032 if (xmlIsBlankNode(cur))
8033 break;
8034 ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
8035 if (ret < 0) {
8036 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
8037 cur->parent->name);
8038 }
8039 break;
8040 case XML_ELEMENT_NODE:
8041 if (cur->ns != NULL) {
8042 ret = xmlRegExecPushString2(exec, cur->name,
8043 cur->ns->href, ctxt);
8044 } else {
8045 ret = xmlRegExecPushString(exec, cur->name, ctxt);
8046 }
8047 if (ret < 0) {
8048 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
8049 }
8050 break;
8051 default:
8052 break;
8053 }
8054 if (ret < 0)
8055 break;
8056 /*
8057 * Switch to next element
8058 */
8059 cur = cur->next;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008060 }
8061 ret = xmlRegExecPushString(exec, NULL, NULL);
8062 if (ret == 1) {
8063 ret = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008064 ctxt->state->seq = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008065 } else if (ret == 0) {
8066 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008067 * TODO: get some of the names needed to exit the current state of exec
8068 */
8069 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8070 ret = -1;
8071 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8072 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008073 } else {
8074 ret = -1;
8075 }
8076 xmlRegFreeExecCtxt(exec);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +00008077 /*
8078 * There might be content model errors outside of the pure
8079 * regexp validation, e.g. for attribute values.
8080 */
8081 if ((ret == 0) && (ctxt->perr != 0)) {
8082 ret = ctxt->perr;
8083 }
8084 ctxt->perr = oldperr;
Daniel Veillard4c004142003-10-07 11:33:24 +00008085 return (ret);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008086}
8087
8088/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008089 * *
8090 * Progressive validation of when possible *
8091 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008092 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008093static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
8094 xmlRelaxNGDefinePtr defines);
8095static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
William M. Brack272693c2003-11-14 16:20:34 +00008096 int dolog);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00008097static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008098
8099/**
8100 * xmlRelaxNGElemPush:
8101 * @ctxt: the validation context
8102 * @exec: the regexp runtime for the new content model
8103 *
8104 * Push a new regexp for the current node content model on the stack
8105 *
8106 * Returns 0 in case of success and -1 in case of error.
8107 */
8108static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008109xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
8110{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008111 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008112 ctxt->elemMax = 10;
8113 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
8114 sizeof
8115 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008116 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008117 xmlRngVErrMemory(ctxt, "validating\n");
8118 return (-1);
8119 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008120 }
8121 if (ctxt->elemNr >= ctxt->elemMax) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008122 ctxt->elemMax *= 2;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008123 ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
Daniel Veillard4c004142003-10-07 11:33:24 +00008124 ctxt->elemMax *
8125 sizeof
8126 (xmlRegExecCtxtPtr));
Daniel Veillardf4e55762003-04-15 23:32:22 +00008127 if (ctxt->elemTab == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008128 xmlRngVErrMemory(ctxt, "validating\n");
8129 return (-1);
8130 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008131 }
8132 ctxt->elemTab[ctxt->elemNr++] = exec;
8133 ctxt->elem = exec;
Daniel Veillard4c004142003-10-07 11:33:24 +00008134 return (0);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008135}
8136
8137/**
8138 * xmlRelaxNGElemPop:
8139 * @ctxt: the validation context
8140 *
8141 * Pop the regexp of the current node content model from the stack
8142 *
8143 * Returns the exec or NULL if empty
8144 */
8145static xmlRegExecCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +00008146xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
8147{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008148 xmlRegExecCtxtPtr ret;
8149
Daniel Veillard4c004142003-10-07 11:33:24 +00008150 if (ctxt->elemNr <= 0)
8151 return (NULL);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008152 ctxt->elemNr--;
8153 ret = ctxt->elemTab[ctxt->elemNr];
8154 ctxt->elemTab[ctxt->elemNr] = NULL;
Daniel Veillard4c004142003-10-07 11:33:24 +00008155 if (ctxt->elemNr > 0)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008156 ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
8157 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008158 ctxt->elem = NULL;
8159 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008160}
8161
8162/**
8163 * xmlRelaxNGValidateProgressiveCallback:
8164 * @exec: the regular expression instance
8165 * @token: the token which matched
8166 * @transdata: callback data, the define for the subelement if available
8167 @ @inputdata: callback data, the Relax NG validation context
8168 *
8169 * Handle the callback and if needed validate the element children.
Haibo Huangcfd91dc2020-07-30 23:01:33 -07008170 * some of the in/out information are passed via the context in @inputdata.
Daniel Veillardf4e55762003-04-15 23:32:22 +00008171 */
Daniel Veillard4c004142003-10-07 11:33:24 +00008172static void
8173xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8174 ATTRIBUTE_UNUSED,
8175 const xmlChar * token,
8176 void *transdata, void *inputdata)
8177{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008178 xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8179 xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008180 xmlRelaxNGValidStatePtr state, oldstate;
Daniel Veillard14b56432006-03-09 18:41:40 +00008181 xmlNodePtr node;
Daniel Veillardc3ca5ba2003-05-09 22:26:28 +00008182 int ret = 0, oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008183
8184#ifdef DEBUG_PROGRESSIVE
8185 xmlGenericError(xmlGenericErrorContext,
Daniel Veillard4c004142003-10-07 11:33:24 +00008186 "Progressive callback for: '%s'\n", token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008187#endif
8188 if (ctxt == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008189 fprintf(stderr, "callback on %s missing context\n", token);
8190 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008191 }
Daniel Veillard14b56432006-03-09 18:41:40 +00008192 node = ctxt->pnode;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008193 ctxt->pstate = 1;
8194 if (define == NULL) {
8195 if (token[0] == '#')
Daniel Veillard4c004142003-10-07 11:33:24 +00008196 return;
8197 fprintf(stderr, "callback on %s missing define\n", token);
8198 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8199 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8200 ctxt->pstate = -1;
8201 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008202 }
8203 if ((ctxt == NULL) || (define == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008204 fprintf(stderr, "callback on %s missing info\n", token);
8205 if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8206 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8207 ctxt->pstate = -1;
8208 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008209 } else if (define->type != XML_RELAXNG_ELEMENT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008210 fprintf(stderr, "callback on %s define is not element\n", token);
8211 if (ctxt->errNo == XML_RELAXNG_OK)
8212 ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8213 ctxt->pstate = -1;
8214 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008215 }
8216 if (node->type != XML_ELEMENT_NODE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008217 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8218 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8219 xmlRelaxNGDumpValidError(ctxt);
8220 ctxt->pstate = -1;
8221 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008222 }
8223 if (define->contModel == NULL) {
8224 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008225 * this node cannot be validated in a streamable fashion
8226 */
Daniel Veillardf4e55762003-04-15 23:32:22 +00008227#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008228 xmlGenericError(xmlGenericErrorContext,
8229 "Element '%s' validation is not streamable\n",
8230 token);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008231#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008232 ctxt->pstate = 0;
8233 ctxt->pdef = define;
8234 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008235 }
8236 exec = xmlRegNewExecCtxt(define->contModel,
Daniel Veillard4c004142003-10-07 11:33:24 +00008237 xmlRelaxNGValidateProgressiveCallback, ctxt);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008238 if (exec == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008239 ctxt->pstate = -1;
8240 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008241 }
8242 xmlRelaxNGElemPush(ctxt, exec);
8243
8244 /*
8245 * Validate the attributes part of the content.
8246 */
8247 state = xmlRelaxNGNewValidState(ctxt, node);
8248 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008249 ctxt->pstate = -1;
8250 return;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008251 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008252 oldstate = ctxt->state;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008253 ctxt->state = state;
8254 if (define->attrs != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008255 ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8256 if (ret != 0) {
8257 ctxt->pstate = -1;
8258 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8259 }
Daniel Veillardf4e55762003-04-15 23:32:22 +00008260 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008261 if (ctxt->state != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008262 ctxt->state->seq = NULL;
8263 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8264 if (ret != 0) {
8265 ctxt->pstate = -1;
8266 }
8267 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardce192eb2003-04-16 15:58:05 +00008268 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008269 int tmp = -1, i;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008270
8271 oldflags = ctxt->flags;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008272
Daniel Veillard4c004142003-10-07 11:33:24 +00008273 for (i = 0; i < ctxt->states->nbState; i++) {
8274 state = ctxt->states->tabState[i];
8275 ctxt->state = state;
8276 ctxt->state->seq = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +00008277
Daniel Veillard4c004142003-10-07 11:33:24 +00008278 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8279 tmp = 0;
8280 break;
8281 }
8282 }
8283 if (tmp != 0) {
8284 /*
8285 * validation error, log the message for the "best" one
8286 */
8287 ctxt->flags |= FLAGS_IGNORABLE;
8288 xmlRelaxNGLogBestError(ctxt);
8289 }
8290 for (i = 0; i < ctxt->states->nbState; i++) {
8291 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8292 }
8293 xmlRelaxNGFreeStates(ctxt, ctxt->states);
8294 ctxt->states = NULL;
8295 if ((ret == 0) && (tmp == -1))
8296 ctxt->pstate = -1;
8297 ctxt->flags = oldflags;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008298 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008299 if (ctxt->pstate == -1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008300 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8301 xmlRelaxNGDumpValidError(ctxt);
8302 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00008303 }
8304 ctxt->state = oldstate;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008305}
8306
8307/**
8308 * xmlRelaxNGValidatePushElement:
8309 * @ctxt: the validation context
8310 * @doc: a document instance
8311 * @elem: an element instance
8312 *
8313 * Push a new element start on the RelaxNG validation stack.
8314 *
8315 * returns 1 if no validation problem was found or 0 if validating the
8316 * element requires a full node, and -1 in case of error.
8317 */
8318int
Daniel Veillard33300b42003-04-17 09:09:19 +00008319xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8320 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillardf4e55762003-04-15 23:32:22 +00008321 xmlNodePtr elem)
8322{
8323 int ret = 1;
8324
8325 if ((ctxt == NULL) || (elem == NULL))
8326 return (-1);
8327
8328#ifdef DEBUG_PROGRESSIVE
8329 xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8330#endif
8331 if (ctxt->elem == 0) {
8332 xmlRelaxNGPtr schema;
8333 xmlRelaxNGGrammarPtr grammar;
8334 xmlRegExecCtxtPtr exec;
8335 xmlRelaxNGDefinePtr define;
8336
8337 schema = ctxt->schema;
8338 if (schema == NULL) {
8339 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8340 return (-1);
8341 }
8342 grammar = schema->topgrammar;
8343 if ((grammar == NULL) || (grammar->start == NULL)) {
8344 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8345 return (-1);
8346 }
8347 define = grammar->start;
8348 if (define->contModel == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008349 ctxt->pdef = define;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008350 return (0);
8351 }
8352 exec = xmlRegNewExecCtxt(define->contModel,
8353 xmlRelaxNGValidateProgressiveCallback,
8354 ctxt);
8355 if (exec == NULL) {
8356 return (-1);
8357 }
8358 xmlRelaxNGElemPush(ctxt, exec);
8359 }
8360 ctxt->pnode = elem;
8361 ctxt->pstate = 0;
8362 if (elem->ns != NULL) {
8363 ret =
8364 xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8365 ctxt);
8366 } else {
8367 ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8368 }
8369 if (ret < 0) {
8370 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8371 } else {
8372 if (ctxt->pstate == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008373 ret = 0;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008374 else if (ctxt->pstate < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008375 ret = -1;
8376 else
8377 ret = 1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008378 }
8379#ifdef DEBUG_PROGRESSIVE
8380 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008381 xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8382 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008383#endif
8384 return (ret);
8385}
8386
8387/**
8388 * xmlRelaxNGValidatePushCData:
8389 * @ctxt: the RelaxNG validation context
8390 * @data: some character data read
Michael Woodfb27e2c2012-09-28 08:59:33 +02008391 * @len: the length of the data
Daniel Veillardf4e55762003-04-15 23:32:22 +00008392 *
8393 * check the CData parsed for validation in the current stack
8394 *
8395 * returns 1 if no validation problem was found or -1 otherwise
8396 */
8397int
8398xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +00008399 const xmlChar * data, int len ATTRIBUTE_UNUSED)
Daniel Veillardf4e55762003-04-15 23:32:22 +00008400{
8401 int ret = 1;
8402
8403 if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8404 return (-1);
8405
8406#ifdef DEBUG_PROGRESSIVE
8407 xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8408#endif
8409
8410 while (*data != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008411 if (!IS_BLANK_CH(*data))
Daniel Veillardf4e55762003-04-15 23:32:22 +00008412 break;
8413 data++;
8414 }
8415 if (*data == 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008416 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008417
8418 ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8419 if (ret < 0) {
Daniel Veillard33300b42003-04-17 09:09:19 +00008420 VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008421#ifdef DEBUG_PROGRESSIVE
Daniel Veillard4c004142003-10-07 11:33:24 +00008422 xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
Daniel Veillardf4e55762003-04-15 23:32:22 +00008423#endif
8424
Daniel Veillard4c004142003-10-07 11:33:24 +00008425 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008426 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008427 return (1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008428}
8429
8430/**
8431 * xmlRelaxNGValidatePopElement:
8432 * @ctxt: the RelaxNG validation context
8433 * @doc: a document instance
8434 * @elem: an element instance
8435 *
8436 * Pop the element end from the RelaxNG validation stack.
8437 *
8438 * returns 1 if no validation problem was found or 0 otherwise
8439 */
8440int
8441xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8442 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008443 xmlNodePtr elem)
8444{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008445 int ret;
8446 xmlRegExecCtxtPtr exec;
8447
Daniel Veillard4c004142003-10-07 11:33:24 +00008448 if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8449 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008450#ifdef DEBUG_PROGRESSIVE
8451 xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8452#endif
8453 /*
8454 * verify that we reached a terminal state of the content model.
8455 */
8456 exec = xmlRelaxNGElemPop(ctxt);
8457 ret = xmlRegExecPushString(exec, NULL, NULL);
8458 if (ret == 0) {
8459 /*
Daniel Veillard4c004142003-10-07 11:33:24 +00008460 * TODO: get some of the names needed to exit the current state of exec
8461 */
8462 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8463 ret = -1;
Daniel Veillardf4e55762003-04-15 23:32:22 +00008464 } else if (ret < 0) {
8465 ret = -1;
8466 } else {
8467 ret = 1;
8468 }
8469 xmlRegFreeExecCtxt(exec);
8470#ifdef DEBUG_PROGRESSIVE
8471 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008472 xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8473 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008474#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008475 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008476}
8477
8478/**
8479 * xmlRelaxNGValidateFullElement:
8480 * @ctxt: the validation context
8481 * @doc: a document instance
8482 * @elem: an element instance
8483 *
8484 * Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8485 * 0 and the content of the node has been expanded.
8486 *
8487 * returns 1 if no validation problem was found or -1 in case of error.
8488 */
8489int
Daniel Veillard33300b42003-04-17 09:09:19 +00008490xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8491 xmlDocPtr doc ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008492 xmlNodePtr elem)
8493{
Daniel Veillardf4e55762003-04-15 23:32:22 +00008494 int ret;
8495 xmlRelaxNGValidStatePtr state;
8496
Daniel Veillard4c004142003-10-07 11:33:24 +00008497 if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8498 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008499#ifdef DEBUG_PROGRESSIVE
8500 xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8501#endif
8502 state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8503 if (state == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008504 return (-1);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008505 }
8506 state->seq = elem;
8507 ctxt->state = state;
8508 ctxt->errNo = XML_RELAXNG_OK;
8509 ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
Daniel Veillard4c004142003-10-07 11:33:24 +00008510 if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8511 ret = -1;
8512 else
8513 ret = 1;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02008514 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008515 ctxt->state = NULL;
8516#ifdef DEBUG_PROGRESSIVE
8517 if (ret < 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008518 xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8519 elem->name);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008520#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008521 return (ret);
Daniel Veillardf4e55762003-04-15 23:32:22 +00008522}
8523
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008524/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +08008525 * *
8526 * Generic interpreted validation implementation *
8527 * *
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00008528 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +00008529static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8530 xmlRelaxNGDefinePtr define);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008531
8532/**
8533 * xmlRelaxNGSkipIgnored:
8534 * @ctxt: a schema validation context
8535 * @node: the top node.
8536 *
8537 * Skip ignorable nodes in that context
8538 *
8539 * Returns the new sibling or NULL in case of error.
8540 */
8541static xmlNodePtr
8542xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
Daniel Veillard4c004142003-10-07 11:33:24 +00008543 xmlNodePtr node)
8544{
Daniel Veillard6eadf632003-01-23 18:29:16 +00008545 /*
8546 * TODO complete and handle entities
8547 */
8548 while ((node != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00008549 ((node->type == XML_COMMENT_NODE) ||
8550 (node->type == XML_PI_NODE) ||
William M. Brack7217c862004-03-15 02:43:56 +00008551 (node->type == XML_XINCLUDE_START) ||
8552 (node->type == XML_XINCLUDE_END) ||
Daniel Veillard4c004142003-10-07 11:33:24 +00008553 (((node->type == XML_TEXT_NODE) ||
8554 (node->type == XML_CDATA_SECTION_NODE)) &&
8555 ((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8556 (IS_BLANK_NODE(node)))))) {
8557 node = node->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008558 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008559 return (node);
Daniel Veillard6eadf632003-01-23 18:29:16 +00008560}
8561
8562/**
Daniel Veillardedc91922003-01-26 00:52:04 +00008563 * xmlRelaxNGNormalize:
8564 * @ctxt: a schema validation context
8565 * @str: the string to normalize
8566 *
8567 * Implements the normalizeWhiteSpace( s ) function from
8568 * section 6.2.9 of the spec
8569 *
8570 * Returns the new string or NULL in case of error.
8571 */
8572static xmlChar *
Daniel Veillard4c004142003-10-07 11:33:24 +00008573xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8574{
Daniel Veillardedc91922003-01-26 00:52:04 +00008575 xmlChar *ret, *p;
8576 const xmlChar *tmp;
8577 int len;
Daniel Veillard4c004142003-10-07 11:33:24 +00008578
Daniel Veillardedc91922003-01-26 00:52:04 +00008579 if (str == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00008580 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008581 tmp = str;
Daniel Veillard4c004142003-10-07 11:33:24 +00008582 while (*tmp != 0)
8583 tmp++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008584 len = tmp - str;
8585
Daniel Veillard3c908dc2003-04-19 00:07:51 +00008586 ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
Daniel Veillardedc91922003-01-26 00:52:04 +00008587 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008588 xmlRngVErrMemory(ctxt, "validating\n");
8589 return (NULL);
Daniel Veillardedc91922003-01-26 00:52:04 +00008590 }
8591 p = ret;
William M. Brack76e95df2003-10-18 16:20:14 +00008592 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008593 str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008594 while (*str != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008595 if (IS_BLANK_CH(*str)) {
8596 while (IS_BLANK_CH(*str))
Daniel Veillard4c004142003-10-07 11:33:24 +00008597 str++;
8598 if (*str == 0)
8599 break;
8600 *p++ = ' ';
8601 } else
8602 *p++ = *str++;
Daniel Veillardedc91922003-01-26 00:52:04 +00008603 }
8604 *p = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +00008605 return (ret);
Daniel Veillardedc91922003-01-26 00:52:04 +00008606}
8607
8608/**
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008609 * xmlRelaxNGValidateDatatype:
8610 * @ctxt: a Relax-NG validation context
8611 * @value: the string value
8612 * @type: the datatype definition
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008613 * @node: the node
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008614 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -07008615 * Validate the given value against the datatype
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008616 *
8617 * Returns 0 if the validation succeeded or an error code.
8618 */
8619static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008620xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8621 const xmlChar * value,
8622 xmlRelaxNGDefinePtr define, xmlNodePtr node)
8623{
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008624 int ret, tmp;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008625 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008626 void *result = NULL;
8627 xmlRelaxNGDefinePtr cur;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008628
8629 if ((define == NULL) || (define->data == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008630 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008631 }
8632 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008633 if (lib->check != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008634 if ((define->attrs != NULL) &&
8635 (define->attrs->type == XML_RELAXNG_PARAM)) {
8636 ret =
8637 lib->check(lib->data, define->name, value, &result, node);
8638 } else {
8639 ret = lib->check(lib->data, define->name, value, NULL, node);
8640 }
8641 } else
8642 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008643 if (ret < 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008644 VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8645 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8646 lib->freef(lib->data, result);
8647 return (-1);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008648 } else if (ret == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008649 ret = 0;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00008650 } else if (ret == 2) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008651 VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008652 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00008653 VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8654 ret = -1;
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008655 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008656 cur = define->attrs;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008657 while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008658 if (lib->facet != NULL) {
8659 tmp = lib->facet(lib->data, define->name, cur->name,
8660 cur->value, value, result);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008661 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00008662 ret = -1;
8663 }
8664 cur = cur->next;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008665 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008666 if ((ret == 0) && (define->content != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008667 const xmlChar *oldvalue, *oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008668
Daniel Veillard4c004142003-10-07 11:33:24 +00008669 oldvalue = ctxt->state->value;
8670 oldendvalue = ctxt->state->endvalue;
8671 ctxt->state->value = (xmlChar *) value;
8672 ctxt->state->endvalue = NULL;
8673 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8674 ctxt->state->value = (xmlChar *) oldvalue;
8675 ctxt->state->endvalue = (xmlChar *) oldendvalue;
Daniel Veillard416589a2003-02-17 17:25:42 +00008676 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00008677 if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00008678 lib->freef(lib->data, result);
8679 return (ret);
Daniel Veillarddd1655c2003-01-25 18:01:32 +00008680}
8681
8682/**
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008683 * xmlRelaxNGNextValue:
8684 * @ctxt: a Relax-NG validation context
8685 *
8686 * Skip to the next value when validating within a list
8687 *
8688 * Returns 0 if the operation succeeded or an error code.
8689 */
8690static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008691xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8692{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008693 xmlChar *cur;
8694
8695 cur = ctxt->state->value;
8696 if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008697 ctxt->state->value = NULL;
8698 ctxt->state->endvalue = NULL;
8699 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008700 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008701 while (*cur != 0)
8702 cur++;
8703 while ((cur != ctxt->state->endvalue) && (*cur == 0))
8704 cur++;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008705 if (cur == ctxt->state->endvalue)
Daniel Veillard4c004142003-10-07 11:33:24 +00008706 ctxt->state->value = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008707 else
Daniel Veillard4c004142003-10-07 11:33:24 +00008708 ctxt->state->value = cur;
8709 return (0);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008710}
8711
8712/**
8713 * xmlRelaxNGValidateValueList:
8714 * @ctxt: a Relax-NG validation context
8715 * @defines: the list of definitions to verify
8716 *
8717 * Validate the given set of definitions for the current value
8718 *
8719 * Returns 0 if the validation succeeded or an error code.
8720 */
8721static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008722xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8723 xmlRelaxNGDefinePtr defines)
8724{
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008725 int ret = 0;
8726
8727 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008728 ret = xmlRelaxNGValidateValue(ctxt, defines);
8729 if (ret != 0)
8730 break;
8731 defines = defines->next;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008732 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008733 return (ret);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008734}
8735
8736/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00008737 * xmlRelaxNGValidateValue:
8738 * @ctxt: a Relax-NG validation context
8739 * @define: the definition to verify
8740 *
8741 * Validate the given definition for the current value
8742 *
8743 * Returns 0 if the validation succeeded or an error code.
8744 */
8745static int
Daniel Veillard4c004142003-10-07 11:33:24 +00008746xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8747 xmlRelaxNGDefinePtr define)
8748{
Daniel Veillardedc91922003-01-26 00:52:04 +00008749 int ret = 0, oldflags;
Daniel Veillard6eadf632003-01-23 18:29:16 +00008750 xmlChar *value;
8751
8752 value = ctxt->state->value;
8753 switch (define->type) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008754 case XML_RELAXNG_EMPTY:{
8755 if ((value != NULL) && (value[0] != 0)) {
8756 int idx = 0;
Daniel Veillardd4310742003-02-18 21:12:46 +00008757
William M. Brack76e95df2003-10-18 16:20:14 +00008758 while (IS_BLANK_CH(value[idx]))
Daniel Veillard4c004142003-10-07 11:33:24 +00008759 idx++;
8760 if (value[idx] != 0)
8761 ret = -1;
8762 }
8763 break;
8764 }
8765 case XML_RELAXNG_TEXT:
8766 break;
8767 case XML_RELAXNG_VALUE:{
8768 if (!xmlStrEqual(value, define->value)) {
8769 if (define->name != NULL) {
8770 xmlRelaxNGTypeLibraryPtr lib;
Daniel Veillardedc91922003-01-26 00:52:04 +00008771
Daniel Veillard4c004142003-10-07 11:33:24 +00008772 lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8773 if ((lib != NULL) && (lib->comp != NULL)) {
8774 ret = lib->comp(lib->data, define->name,
8775 define->value, define->node,
8776 (void *) define->attrs,
8777 value, ctxt->state->node);
8778 } else
8779 ret = -1;
8780 if (ret < 0) {
8781 VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8782 define->name);
8783 return (-1);
8784 } else if (ret == 1) {
8785 ret = 0;
8786 } else {
8787 ret = -1;
8788 }
8789 } else {
8790 xmlChar *nval, *nvalue;
Daniel Veillardedc91922003-01-26 00:52:04 +00008791
Daniel Veillard4c004142003-10-07 11:33:24 +00008792 /*
8793 * TODO: trivial optimizations are possible by
8794 * computing at compile-time
8795 */
8796 nval = xmlRelaxNGNormalize(ctxt, define->value);
8797 nvalue = xmlRelaxNGNormalize(ctxt, value);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008798
Daniel Veillard4c004142003-10-07 11:33:24 +00008799 if ((nval == NULL) || (nvalue == NULL) ||
8800 (!xmlStrEqual(nval, nvalue)))
8801 ret = -1;
8802 if (nval != NULL)
8803 xmlFree(nval);
8804 if (nvalue != NULL)
8805 xmlFree(nvalue);
8806 }
8807 }
8808 if (ret == 0)
8809 xmlRelaxNGNextValue(ctxt);
8810 break;
8811 }
8812 case XML_RELAXNG_DATATYPE:{
8813 ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8814 ctxt->state->seq);
8815 if (ret == 0)
8816 xmlRelaxNGNextValue(ctxt);
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008817
Daniel Veillard4c004142003-10-07 11:33:24 +00008818 break;
8819 }
8820 case XML_RELAXNG_CHOICE:{
8821 xmlRelaxNGDefinePtr list = define->content;
8822 xmlChar *oldvalue;
8823
8824 oldflags = ctxt->flags;
8825 ctxt->flags |= FLAGS_IGNORABLE;
8826
8827 oldvalue = ctxt->state->value;
8828 while (list != NULL) {
8829 ret = xmlRelaxNGValidateValue(ctxt, list);
8830 if (ret == 0) {
8831 break;
8832 }
8833 ctxt->state->value = oldvalue;
8834 list = list->next;
8835 }
8836 ctxt->flags = oldflags;
8837 if (ret != 0) {
8838 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8839 xmlRelaxNGDumpValidError(ctxt);
8840 } else {
8841 if (ctxt->errNr > 0)
8842 xmlRelaxNGPopErrors(ctxt, 0);
8843 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008844 break;
8845 }
8846 case XML_RELAXNG_LIST:{
8847 xmlRelaxNGDefinePtr list = define->content;
8848 xmlChar *oldvalue, *oldend, *val, *cur;
8849
Daniel Veillard416589a2003-02-17 17:25:42 +00008850#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008851 int nb_values = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00008852#endif
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008853
Daniel Veillard4c004142003-10-07 11:33:24 +00008854 oldvalue = ctxt->state->value;
8855 oldend = ctxt->state->endvalue;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008856
Daniel Veillard4c004142003-10-07 11:33:24 +00008857 val = xmlStrdup(oldvalue);
8858 if (val == NULL) {
8859 val = xmlStrdup(BAD_CAST "");
8860 }
8861 if (val == NULL) {
8862 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8863 return (-1);
8864 }
8865 cur = val;
8866 while (*cur != 0) {
William M. Brack76e95df2003-10-18 16:20:14 +00008867 if (IS_BLANK_CH(*cur)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00008868 *cur = 0;
8869 cur++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008870#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008871 nb_values++;
Daniel Veillard416589a2003-02-17 17:25:42 +00008872#endif
William M. Brack76e95df2003-10-18 16:20:14 +00008873 while (IS_BLANK_CH(*cur))
Daniel Veillard4c004142003-10-07 11:33:24 +00008874 *cur++ = 0;
8875 } else
8876 cur++;
8877 }
Daniel Veillard416589a2003-02-17 17:25:42 +00008878#ifdef DEBUG_LIST
Daniel Veillard4c004142003-10-07 11:33:24 +00008879 xmlGenericError(xmlGenericErrorContext,
8880 "list value: '%s' found %d items\n",
8881 oldvalue, nb_values);
8882 nb_values = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008883#endif
Daniel Veillard4c004142003-10-07 11:33:24 +00008884 ctxt->state->endvalue = cur;
8885 cur = val;
8886 while ((*cur == 0) && (cur != ctxt->state->endvalue))
8887 cur++;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008888
Daniel Veillard4c004142003-10-07 11:33:24 +00008889 ctxt->state->value = cur;
8890
8891 while (list != NULL) {
8892 if (ctxt->state->value == ctxt->state->endvalue)
8893 ctxt->state->value = NULL;
8894 ret = xmlRelaxNGValidateValue(ctxt, list);
8895 if (ret != 0) {
8896#ifdef DEBUG_LIST
8897 xmlGenericError(xmlGenericErrorContext,
8898 "Failed to validate value: '%s' with %d rule\n",
8899 ctxt->state->value, nb_values);
8900#endif
8901 break;
8902 }
8903#ifdef DEBUG_LIST
8904 nb_values++;
8905#endif
8906 list = list->next;
8907 }
8908
8909 if ((ret == 0) && (ctxt->state->value != NULL) &&
8910 (ctxt->state->value != ctxt->state->endvalue)) {
8911 VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8912 ctxt->state->value);
8913 ret = -1;
8914 }
8915 xmlFree(val);
8916 ctxt->state->value = oldvalue;
8917 ctxt->state->endvalue = oldend;
8918 break;
8919 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00008920 case XML_RELAXNG_ONEORMORE:
Daniel Veillard4c004142003-10-07 11:33:24 +00008921 ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8922 if (ret != 0) {
8923 break;
8924 }
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +02008925 /* Falls through. */
Daniel Veillard4c004142003-10-07 11:33:24 +00008926 case XML_RELAXNG_ZEROORMORE:{
8927 xmlChar *cur, *temp;
Daniel Veillardc6e997c2003-01-27 12:35:42 +00008928
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008929 if ((ctxt->state->value == NULL) ||
8930 (*ctxt->state->value == 0)) {
8931 ret = 0;
8932 break;
8933 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008934 oldflags = ctxt->flags;
8935 ctxt->flags |= FLAGS_IGNORABLE;
8936 cur = ctxt->state->value;
8937 temp = NULL;
8938 while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8939 (temp != cur)) {
8940 temp = cur;
8941 ret =
8942 xmlRelaxNGValidateValueList(ctxt, define->content);
8943 if (ret != 0) {
8944 ctxt->state->value = temp;
8945 ret = 0;
8946 break;
8947 }
8948 cur = ctxt->state->value;
8949 }
8950 ctxt->flags = oldflags;
Daniel Veillard14b56432006-03-09 18:41:40 +00008951 if (ctxt->errNr > 0)
8952 xmlRelaxNGPopErrors(ctxt, 0);
Daniel Veillard4c004142003-10-07 11:33:24 +00008953 break;
8954 }
Daniel Veillard7dd0d912011-11-10 18:08:33 +08008955 case XML_RELAXNG_OPTIONAL:{
8956 xmlChar *temp;
8957
8958 if ((ctxt->state->value == NULL) ||
8959 (*ctxt->state->value == 0)) {
8960 ret = 0;
8961 break;
8962 }
8963 oldflags = ctxt->flags;
8964 ctxt->flags |= FLAGS_IGNORABLE;
8965 temp = ctxt->state->value;
8966 ret = xmlRelaxNGValidateValue(ctxt, define->content);
8967 ctxt->flags = oldflags;
8968 if (ret != 0) {
8969 ctxt->state->value = temp;
8970 if (ctxt->errNr > 0)
8971 xmlRelaxNGPopErrors(ctxt, 0);
8972 ret = 0;
8973 break;
8974 }
8975 if (ctxt->errNr > 0)
8976 xmlRelaxNGPopErrors(ctxt, 0);
8977 break;
8978 }
Daniel Veillard4c004142003-10-07 11:33:24 +00008979 case XML_RELAXNG_EXCEPT:{
8980 xmlRelaxNGDefinePtr list;
Daniel Veillard416589a2003-02-17 17:25:42 +00008981
Daniel Veillard4c004142003-10-07 11:33:24 +00008982 list = define->content;
8983 while (list != NULL) {
8984 ret = xmlRelaxNGValidateValue(ctxt, list);
8985 if (ret == 0) {
8986 ret = -1;
8987 break;
8988 } else
8989 ret = 0;
8990 list = list->next;
8991 }
8992 break;
8993 }
Daniel Veillard463a5472003-02-27 21:30:32 +00008994 case XML_RELAXNG_DEF:
Daniel Veillard4c004142003-10-07 11:33:24 +00008995 case XML_RELAXNG_GROUP:{
8996 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00008997
Daniel Veillard4c004142003-10-07 11:33:24 +00008998 list = define->content;
8999 while (list != NULL) {
9000 ret = xmlRelaxNGValidateValue(ctxt, list);
9001 if (ret != 0) {
9002 ret = -1;
9003 break;
9004 } else
9005 ret = 0;
9006 list = list->next;
9007 }
9008 break;
9009 }
Daniel Veillard463a5472003-02-27 21:30:32 +00009010 case XML_RELAXNG_REF:
9011 case XML_RELAXNG_PARENTREF:
Daniel Veillard25a1ce92008-06-02 16:04:12 +00009012 if (define->content == NULL) {
Daniel Veillard81c51e12009-08-14 18:52:10 +02009013 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9014 ret = -1;
9015 } else {
9016 ret = xmlRelaxNGValidateValue(ctxt, define->content);
9017 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009018 break;
9019 default:
9020 TODO ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009021 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009022 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009023}
9024
9025/**
9026 * xmlRelaxNGValidateValueContent:
9027 * @ctxt: a Relax-NG validation context
9028 * @defines: the list of definitions to verify
9029 *
9030 * Validate the given definitions for the current value
9031 *
9032 * Returns 0 if the validation succeeded or an error code.
9033 */
9034static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009035xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
9036 xmlRelaxNGDefinePtr defines)
9037{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009038 int ret = 0;
9039
9040 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009041 ret = xmlRelaxNGValidateValue(ctxt, defines);
9042 if (ret != 0)
9043 break;
9044 defines = defines->next;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009045 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009046 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009047}
9048
9049/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009050 * xmlRelaxNGAttributeMatch:
9051 * @ctxt: a Relax-NG validation context
9052 * @define: the definition to check
9053 * @prop: the attribute
9054 *
9055 * Check if the attribute matches the definition nameClass
9056 *
9057 * Returns 1 if the attribute matches, 0 if no, or -1 in case of error
9058 */
9059static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009060xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
9061 xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
9062{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009063 int ret;
9064
9065 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009066 if (!xmlStrEqual(define->name, prop->name))
9067 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009068 }
9069 if (define->ns != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009070 if (define->ns[0] == 0) {
9071 if (prop->ns != NULL)
9072 return (0);
9073 } else {
9074 if ((prop->ns == NULL) ||
9075 (!xmlStrEqual(define->ns, prop->ns->href)))
9076 return (0);
9077 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009078 }
9079 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009080 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009081 define = define->nameClass;
9082 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009083 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009084
Daniel Veillard4c004142003-10-07 11:33:24 +00009085 list = define->content;
9086 while (list != NULL) {
9087 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9088 if (ret == 1)
9089 return (0);
9090 if (ret < 0)
9091 return (ret);
9092 list = list->next;
9093 }
Shaun McCance6473a412013-10-23 14:51:33 -04009094 } else if (define->type == XML_RELAXNG_CHOICE) {
9095 xmlRelaxNGDefinePtr list;
9096
9097 list = define->nameClass;
9098 while (list != NULL) {
9099 ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
9100 if (ret == 1)
9101 return (1);
9102 if (ret < 0)
9103 return (ret);
9104 list = list->next;
9105 }
9106 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009107 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009108 TODO}
9109 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009110}
9111
9112/**
Daniel Veillard6eadf632003-01-23 18:29:16 +00009113 * xmlRelaxNGValidateAttribute:
9114 * @ctxt: a Relax-NG validation context
9115 * @define: the definition to verify
9116 *
9117 * Validate the given attribute definition for that node
9118 *
9119 * Returns 0 if the validation succeeded or an error code.
9120 */
9121static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009122xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
9123 xmlRelaxNGDefinePtr define)
9124{
Daniel Veillard6eadf632003-01-23 18:29:16 +00009125 int ret = 0, i;
9126 xmlChar *value, *oldvalue;
9127 xmlAttrPtr prop = NULL, tmp;
Daniel Veillardc3da18a2003-03-18 00:31:04 +00009128 xmlNodePtr oldseq;
Daniel Veillard6eadf632003-01-23 18:29:16 +00009129
Daniel Veillard1ed7f362003-02-03 10:57:45 +00009130 if (ctxt->state->nbAttrLeft <= 0)
Daniel Veillard4c004142003-10-07 11:33:24 +00009131 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009132 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009133 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9134 tmp = ctxt->state->attrs[i];
9135 if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
9136 if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
9137 (tmp->ns == NULL)) ||
9138 ((tmp->ns != NULL) &&
9139 (xmlStrEqual(define->ns, tmp->ns->href)))) {
9140 prop = tmp;
9141 break;
9142 }
9143 }
9144 }
9145 if (prop != NULL) {
9146 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9147 oldvalue = ctxt->state->value;
9148 oldseq = ctxt->state->seq;
9149 ctxt->state->seq = (xmlNodePtr) prop;
9150 ctxt->state->value = value;
9151 ctxt->state->endvalue = NULL;
9152 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9153 if (ctxt->state->value != NULL)
9154 value = ctxt->state->value;
9155 if (value != NULL)
9156 xmlFree(value);
9157 ctxt->state->value = oldvalue;
9158 ctxt->state->seq = oldseq;
9159 if (ret == 0) {
9160 /*
9161 * flag the attribute as processed
9162 */
9163 ctxt->state->attrs[i] = NULL;
9164 ctxt->state->nbAttrLeft--;
9165 }
9166 } else {
9167 ret = -1;
9168 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009169#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009170 xmlGenericError(xmlGenericErrorContext,
9171 "xmlRelaxNGValidateAttribute(%s): %d\n",
9172 define->name, ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009173#endif
9174 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009175 for (i = 0; i < ctxt->state->nbAttrs; i++) {
9176 tmp = ctxt->state->attrs[i];
9177 if ((tmp != NULL) &&
9178 (xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
9179 prop = tmp;
9180 break;
9181 }
9182 }
9183 if (prop != NULL) {
9184 value = xmlNodeListGetString(prop->doc, prop->children, 1);
9185 oldvalue = ctxt->state->value;
9186 oldseq = ctxt->state->seq;
9187 ctxt->state->seq = (xmlNodePtr) prop;
9188 ctxt->state->value = value;
9189 ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
9190 if (ctxt->state->value != NULL)
9191 value = ctxt->state->value;
9192 if (value != NULL)
9193 xmlFree(value);
9194 ctxt->state->value = oldvalue;
9195 ctxt->state->seq = oldseq;
9196 if (ret == 0) {
9197 /*
9198 * flag the attribute as processed
9199 */
9200 ctxt->state->attrs[i] = NULL;
9201 ctxt->state->nbAttrLeft--;
9202 }
9203 } else {
9204 ret = -1;
9205 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009206#ifdef DEBUG
Daniel Veillard4c004142003-10-07 11:33:24 +00009207 if (define->ns != NULL) {
9208 xmlGenericError(xmlGenericErrorContext,
9209 "xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
9210 define->ns, ret);
9211 } else {
9212 xmlGenericError(xmlGenericErrorContext,
9213 "xmlRelaxNGValidateAttribute(anyName): %d\n",
9214 ret);
9215 }
Daniel Veillard3b2e4e12003-02-03 08:52:58 +00009216#endif
Daniel Veillard6eadf632003-01-23 18:29:16 +00009217 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009218
9219 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +00009220}
9221
9222/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009223 * xmlRelaxNGValidateAttributeList:
9224 * @ctxt: a Relax-NG validation context
9225 * @define: the list of definition to verify
9226 *
9227 * Validate the given node against the list of attribute definitions
9228 *
9229 * Returns 0 if the validation succeeded or an error code.
9230 */
9231static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009232xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9233 xmlRelaxNGDefinePtr defines)
9234{
Daniel Veillardce192eb2003-04-16 15:58:05 +00009235 int ret = 0, res;
9236 int needmore = 0;
9237 xmlRelaxNGDefinePtr cur;
9238
9239 cur = defines;
9240 while (cur != NULL) {
9241 if (cur->type == XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009242 if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9243 ret = -1;
9244 } else
9245 needmore = 1;
Daniel Veillardce192eb2003-04-16 15:58:05 +00009246 cur = cur->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009247 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009248 if (!needmore)
Daniel Veillard4c004142003-10-07 11:33:24 +00009249 return (ret);
Daniel Veillardce192eb2003-04-16 15:58:05 +00009250 cur = defines;
9251 while (cur != NULL) {
9252 if (cur->type != XML_RELAXNG_ATTRIBUTE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009253 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9254 res = xmlRelaxNGValidateDefinition(ctxt, cur);
9255 if (res < 0)
9256 ret = -1;
9257 } else {
9258 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9259 return (-1);
9260 }
9261 if (res == -1) /* continues on -2 */
9262 break;
9263 }
Daniel Veillardce192eb2003-04-16 15:58:05 +00009264 cur = cur->next;
9265 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009266
9267 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009268}
9269
9270/**
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009271 * xmlRelaxNGNodeMatchesList:
9272 * @node: the node
9273 * @list: a NULL terminated array of definitions
9274 *
9275 * Check if a node can be matched by one of the definitions
9276 *
9277 * Returns 1 if matches 0 otherwise
9278 */
9279static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009280xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9281{
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009282 xmlRelaxNGDefinePtr cur;
Daniel Veillard0e3d3ce2003-03-21 12:43:18 +00009283 int i = 0, tmp;
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009284
9285 if ((node == NULL) || (list == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +00009286 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009287
9288 cur = list[i++];
9289 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009290 if ((node->type == XML_ELEMENT_NODE) &&
9291 (cur->type == XML_RELAXNG_ELEMENT)) {
9292 tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9293 if (tmp == 1)
9294 return (1);
9295 } else if (((node->type == XML_TEXT_NODE) ||
9296 (node->type == XML_CDATA_SECTION_NODE)) &&
Elliott Hughes7fbecab2019-01-10 16:42:03 -08009297 ((cur->type == XML_RELAXNG_DATATYPE) ||
9298 (cur->type == XML_RELAXNG_LIST) ||
9299 (cur->type == XML_RELAXNG_TEXT) ||
9300 (cur->type == XML_RELAXNG_VALUE))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009301 return (1);
9302 }
9303 cur = list[i++];
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009304 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009305 return (0);
Daniel Veillard76fc5ed2003-01-28 20:58:15 +00009306}
9307
9308/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009309 * xmlRelaxNGValidateInterleave:
9310 * @ctxt: a Relax-NG validation context
9311 * @define: the definition to verify
9312 *
9313 * Validate an interleave definition for a node.
9314 *
9315 * Returns 0 if the validation succeeded or an error code.
9316 */
9317static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009318xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9319 xmlRelaxNGDefinePtr define)
9320{
William M. Brack779af002003-08-01 15:55:39 +00009321 int ret = 0, i, nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009322 int errNr = ctxt->errNr;
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009323 int oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009324
9325 xmlRelaxNGValidStatePtr oldstate;
9326 xmlRelaxNGPartitionPtr partitions;
9327 xmlRelaxNGInterleaveGroupPtr group = NULL;
9328 xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9329 xmlNodePtr *list = NULL, *lasts = NULL;
9330
9331 if (define->data != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009332 partitions = (xmlRelaxNGPartitionPtr) define->data;
9333 nbgroups = partitions->nbgroups;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009334 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009335 VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9336 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009337 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009338 /*
9339 * Optimizations for MIXED
9340 */
9341 oldflags = ctxt->flags;
Daniel Veillarde063f482003-03-21 16:53:17 +00009342 if (define->dflags & IS_MIXED) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009343 ctxt->flags |= FLAGS_MIXED_CONTENT;
9344 if (nbgroups == 2) {
9345 /*
9346 * this is a pure <mixed> case
9347 */
9348 if (ctxt->state != NULL)
9349 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9350 ctxt->state->seq);
9351 if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9352 ret = xmlRelaxNGValidateDefinition(ctxt,
9353 partitions->groups[1]->
9354 rule);
9355 else
9356 ret = xmlRelaxNGValidateDefinition(ctxt,
9357 partitions->groups[0]->
9358 rule);
9359 if (ret == 0) {
9360 if (ctxt->state != NULL)
9361 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9362 ctxt->state->
9363 seq);
9364 }
9365 ctxt->flags = oldflags;
9366 return (ret);
9367 }
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009368 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009369
9370 /*
9371 * Build arrays to store the first and last node of the chain
9372 * pertaining to each group
9373 */
9374 list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9375 if (list == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009376 xmlRngVErrMemory(ctxt, "validating\n");
9377 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009378 }
9379 memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9380 lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9381 if (lasts == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009382 xmlRngVErrMemory(ctxt, "validating\n");
9383 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009384 }
9385 memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9386
9387 /*
9388 * Walk the sequence of children finding the right group and
9389 * sorting them in sequences.
9390 */
9391 cur = ctxt->state->seq;
9392 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9393 start = cur;
9394 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009395 ctxt->state->seq = cur;
9396 if ((partitions->triage != NULL) &&
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009397 (partitions->flags & IS_DETERMINIST)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009398 void *tmp = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009399
Daniel Veillard4c004142003-10-07 11:33:24 +00009400 if ((cur->type == XML_TEXT_NODE) ||
9401 (cur->type == XML_CDATA_SECTION_NODE)) {
9402 tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9403 NULL);
9404 } else if (cur->type == XML_ELEMENT_NODE) {
9405 if (cur->ns != NULL) {
9406 tmp = xmlHashLookup2(partitions->triage, cur->name,
9407 cur->ns->href);
9408 if (tmp == NULL)
9409 tmp = xmlHashLookup2(partitions->triage,
9410 BAD_CAST "#any",
9411 cur->ns->href);
9412 } else
9413 tmp =
9414 xmlHashLookup2(partitions->triage, cur->name,
9415 NULL);
9416 if (tmp == NULL)
9417 tmp =
9418 xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9419 NULL);
9420 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009421
Daniel Veillard4c004142003-10-07 11:33:24 +00009422 if (tmp == NULL) {
9423 i = nbgroups;
9424 } else {
Nick Wellnhoferd422b952017-10-09 13:37:42 +02009425 i = ((ptrdiff_t) tmp) - 1;
Daniel Veillard4c004142003-10-07 11:33:24 +00009426 if (partitions->flags & IS_NEEDCHECK) {
9427 group = partitions->groups[i];
9428 if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9429 i = nbgroups;
9430 }
9431 }
9432 } else {
9433 for (i = 0; i < nbgroups; i++) {
9434 group = partitions->groups[i];
9435 if (group == NULL)
9436 continue;
9437 if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9438 break;
9439 }
9440 }
9441 /*
9442 * We break as soon as an element not matched is found
9443 */
9444 if (i >= nbgroups) {
9445 break;
9446 }
9447 if (lasts[i] != NULL) {
9448 lasts[i]->next = cur;
9449 lasts[i] = cur;
9450 } else {
9451 list[i] = cur;
9452 lasts[i] = cur;
9453 }
9454 if (cur->next != NULL)
9455 lastchg = cur->next;
9456 else
9457 lastchg = cur;
9458 cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009459 }
9460 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009461 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9462 ret = -1;
9463 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009464 }
9465 lastelem = cur;
9466 oldstate = ctxt->state;
Daniel Veillard4c004142003-10-07 11:33:24 +00009467 for (i = 0; i < nbgroups; i++) {
9468 ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
Gaurav Gupta6d753992014-07-14 16:01:10 +08009469 if (ctxt->state == NULL) {
9470 ret = -1;
9471 break;
9472 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009473 group = partitions->groups[i];
9474 if (lasts[i] != NULL) {
9475 last = lasts[i]->next;
9476 lasts[i]->next = NULL;
9477 }
9478 ctxt->state->seq = list[i];
9479 ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9480 if (ret != 0)
9481 break;
9482 if (ctxt->state != NULL) {
9483 cur = ctxt->state->seq;
9484 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9485 xmlRelaxNGFreeValidState(ctxt, oldstate);
9486 oldstate = ctxt->state;
9487 ctxt->state = NULL;
Haibo Huangcfd91dc2020-07-30 23:01:33 -07009488 if (cur != NULL
9489 /* there's a nasty violation of context-free unambiguities,
9490 since in open-name-class context, interleave in the
9491 production shall finish without caring about anything
9492 else that is OK to follow in that case -- it would
9493 otherwise get marked as "extra content" and would
9494 hence fail the validation, hence this perhaps
9495 dirty attempt to rectify such a situation */
9496 && (define->parent->type != XML_RELAXNG_DEF
9497 || !xmlStrEqual(define->parent->name,
9498 (const xmlChar *) "open-name-class"))) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009499 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9500 ret = -1;
9501 ctxt->state = oldstate;
9502 goto done;
9503 }
9504 } else if (ctxt->states != NULL) {
9505 int j;
9506 int found = 0;
Daniel Veillard87254c82006-02-19 15:27:17 +00009507 int best = -1;
9508 int lowattr = -1;
9509
9510 /*
9511 * PBM: what happen if there is attributes checks in the interleaves
9512 */
Daniel Veillardfd573f12003-03-16 17:52:32 +00009513
Daniel Veillard4c004142003-10-07 11:33:24 +00009514 for (j = 0; j < ctxt->states->nbState; j++) {
9515 cur = ctxt->states->tabState[j]->seq;
9516 cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9517 if (cur == NULL) {
Daniel Veillard87254c82006-02-19 15:27:17 +00009518 if (found == 0) {
9519 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9520 best = j;
9521 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009522 found = 1;
Daniel Veillard87254c82006-02-19 15:27:17 +00009523 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9524 /* try to keep the latest one to mach old heuristic */
9525 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9526 best = j;
9527 }
9528 if (lowattr == 0)
9529 break;
9530 } else if (found == 0) {
9531 if (lowattr == -1) {
9532 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9533 best = j;
9534 } else
9535 if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9536 /* try to keep the latest one to mach old heuristic */
9537 lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9538 best = j;
9539 }
9540 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009541 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009542 /*
9543 * BIG PBM: here we pick only one restarting point :-(
9544 */
Daniel Veillard4c004142003-10-07 11:33:24 +00009545 if (ctxt->states->nbState > 0) {
9546 xmlRelaxNGFreeValidState(ctxt, oldstate);
Daniel Veillard87254c82006-02-19 15:27:17 +00009547 if (best != -1) {
9548 oldstate = ctxt->states->tabState[best];
9549 ctxt->states->tabState[best] = NULL;
9550 } else {
9551 oldstate =
9552 ctxt->states->tabState[ctxt->states->nbState - 1];
9553 ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
Daniel Veillard9fcd4622009-08-14 16:16:31 +02009554 ctxt->states->nbState--;
Daniel Veillard87254c82006-02-19 15:27:17 +00009555 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009556 }
Daniel Veillard87254c82006-02-19 15:27:17 +00009557 for (j = 0; j < ctxt->states->nbState ; j++) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009558 xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9559 }
9560 xmlRelaxNGFreeStates(ctxt, ctxt->states);
9561 ctxt->states = NULL;
9562 if (found == 0) {
Daniel Veillard76d36452009-09-07 11:19:33 +02009563 if (cur == NULL) {
Ben Waltona7a6a4b2010-03-15 10:06:36 +01009564 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA,
9565 (const xmlChar *) "noname");
Daniel Veillard76d36452009-09-07 11:19:33 +02009566 } else {
9567 VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9568 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009569 ret = -1;
9570 ctxt->state = oldstate;
9571 goto done;
9572 }
9573 } else {
9574 ret = -1;
9575 break;
9576 }
9577 if (lasts[i] != NULL) {
9578 lasts[i]->next = last;
9579 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009580 }
9581 if (ctxt->state != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009582 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009583 ctxt->state = oldstate;
9584 ctxt->state->seq = lastelem;
9585 if (ret != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009586 VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9587 ret = -1;
9588 goto done;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009589 }
9590
Daniel Veillard4c004142003-10-07 11:33:24 +00009591 done:
Daniel Veillard249d7bb2003-03-19 21:02:29 +00009592 ctxt->flags = oldflags;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009593 /*
9594 * builds the next links chain from the prev one
9595 */
9596 cur = lastchg;
9597 while (cur != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009598 if ((cur == start) || (cur->prev == NULL))
9599 break;
9600 cur->prev->next = cur;
9601 cur = cur->prev;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009602 }
9603 if (ret == 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009604 if (ctxt->errNr > errNr)
9605 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009606 }
9607
9608 xmlFree(list);
9609 xmlFree(lasts);
Daniel Veillard4c004142003-10-07 11:33:24 +00009610 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009611}
9612
9613/**
9614 * xmlRelaxNGValidateDefinitionList:
9615 * @ctxt: a Relax-NG validation context
9616 * @define: the list of definition to verify
9617 *
9618 * Validate the given node content against the (list) of definitions
9619 *
9620 * Returns 0 if the validation succeeded or an error code.
9621 */
9622static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009623xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9624 xmlRelaxNGDefinePtr defines)
9625{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009626 int ret = 0, res;
9627
9628
Daniel Veillard952379b2003-03-17 15:37:12 +00009629 if (defines == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009630 VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9631 BAD_CAST "NULL definition list");
9632 return (-1);
Daniel Veillard952379b2003-03-17 15:37:12 +00009633 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009634 while (defines != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009635 if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9636 res = xmlRelaxNGValidateDefinition(ctxt, defines);
9637 if (res < 0)
9638 ret = -1;
9639 } else {
9640 VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9641 return (-1);
9642 }
9643 if (res == -1) /* continues on -2 */
9644 break;
9645 defines = defines->next;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009646 }
9647
Daniel Veillard4c004142003-10-07 11:33:24 +00009648 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009649}
9650
9651/**
9652 * xmlRelaxNGElementMatch:
Daniel Veillard416589a2003-02-17 17:25:42 +00009653 * @ctxt: a Relax-NG validation context
9654 * @define: the definition to check
Daniel Veillardfd573f12003-03-16 17:52:32 +00009655 * @elem: the element
Daniel Veillard416589a2003-02-17 17:25:42 +00009656 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009657 * Check if the element matches the definition nameClass
Daniel Veillard416589a2003-02-17 17:25:42 +00009658 *
Daniel Veillardfd573f12003-03-16 17:52:32 +00009659 * Returns 1 if the element matches, 0 if no, or -1 in case of error
Daniel Veillard416589a2003-02-17 17:25:42 +00009660 */
9661static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009662xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9663 xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9664{
Daniel Veillard580ced82003-03-21 21:22:48 +00009665 int ret = 0, oldflags = 0;
Daniel Veillard416589a2003-02-17 17:25:42 +00009666
Daniel Veillardfd573f12003-03-16 17:52:32 +00009667 if (define->name != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009668 if (!xmlStrEqual(elem->name, define->name)) {
9669 VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9670 return (0);
9671 }
Daniel Veillard6eadf632003-01-23 18:29:16 +00009672 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009673 if ((define->ns != NULL) && (define->ns[0] != 0)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009674 if (elem->ns == NULL) {
9675 VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9676 return (0);
9677 } else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9678 VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9679 elem->name, define->ns);
9680 return (0);
9681 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009682 } else if ((elem->ns != NULL) && (define->ns != NULL) &&
Daniel Veillard4c004142003-10-07 11:33:24 +00009683 (define->name == NULL)) {
9684 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9685 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009686 } else if ((elem->ns != NULL) && (define->name != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009687 VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9688 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009689 }
9690
9691 if (define->nameClass == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +00009692 return (1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009693
9694 define = define->nameClass;
9695 if (define->type == XML_RELAXNG_EXCEPT) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009696 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009697
Daniel Veillard4c004142003-10-07 11:33:24 +00009698 if (ctxt != NULL) {
9699 oldflags = ctxt->flags;
9700 ctxt->flags |= FLAGS_IGNORABLE;
9701 }
9702
9703 list = define->content;
9704 while (list != NULL) {
9705 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9706 if (ret == 1) {
9707 if (ctxt != NULL)
9708 ctxt->flags = oldflags;
9709 return (0);
9710 }
9711 if (ret < 0) {
9712 if (ctxt != NULL)
9713 ctxt->flags = oldflags;
9714 return (ret);
9715 }
9716 list = list->next;
9717 }
9718 ret = 1;
9719 if (ctxt != NULL) {
9720 ctxt->flags = oldflags;
9721 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009722 } else if (define->type == XML_RELAXNG_CHOICE) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009723 xmlRelaxNGDefinePtr list;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009724
Daniel Veillard4c004142003-10-07 11:33:24 +00009725 if (ctxt != NULL) {
9726 oldflags = ctxt->flags;
9727 ctxt->flags |= FLAGS_IGNORABLE;
9728 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009729
Daniel Veillard4c004142003-10-07 11:33:24 +00009730 list = define->nameClass;
9731 while (list != NULL) {
9732 ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9733 if (ret == 1) {
9734 if (ctxt != NULL)
9735 ctxt->flags = oldflags;
9736 return (1);
9737 }
9738 if (ret < 0) {
9739 if (ctxt != NULL)
9740 ctxt->flags = oldflags;
9741 return (ret);
9742 }
9743 list = list->next;
9744 }
9745 if (ctxt != NULL) {
9746 if (ret != 0) {
9747 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9748 xmlRelaxNGDumpValidError(ctxt);
9749 } else {
9750 if (ctxt->errNr > 0)
9751 xmlRelaxNGPopErrors(ctxt, 0);
9752 }
9753 }
9754 ret = 0;
9755 if (ctxt != NULL) {
9756 ctxt->flags = oldflags;
9757 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009758 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +00009759 TODO ret = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009760 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009761 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009762}
9763
9764/**
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009765 * xmlRelaxNGBestState:
9766 * @ctxt: a Relax-NG validation context
9767 *
9768 * Find the "best" state in the ctxt->states list of states to report
9769 * errors about. I.e. a state with no element left in the child list
9770 * or the one with the less attributes left.
Haibo Huangcfd91dc2020-07-30 23:01:33 -07009771 * This is called only if a validation error was detected
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009772 *
9773 * Returns the index of the "best" state or -1 in case of error
9774 */
9775static int
Daniel Veillard4c004142003-10-07 11:33:24 +00009776xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9777{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009778 xmlRelaxNGValidStatePtr state;
9779 int i, tmp;
9780 int best = -1;
9781 int value = 1000000;
9782
9783 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9784 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009785 return (-1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009786
Daniel Veillard4c004142003-10-07 11:33:24 +00009787 for (i = 0; i < ctxt->states->nbState; i++) {
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009788 state = ctxt->states->tabState[i];
Daniel Veillard4c004142003-10-07 11:33:24 +00009789 if (state == NULL)
9790 continue;
9791 if (state->seq != NULL) {
9792 if ((best == -1) || (value > 100000)) {
9793 value = 100000;
9794 best = i;
9795 }
9796 } else {
9797 tmp = state->nbAttrLeft;
9798 if ((best == -1) || (value > tmp)) {
9799 value = tmp;
9800 best = i;
9801 }
9802 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009803 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009804 return (best);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009805}
9806
9807/**
9808 * xmlRelaxNGLogBestError:
9809 * @ctxt: a Relax-NG validation context
9810 *
9811 * Find the "best" state in the ctxt->states list of states to report
9812 * errors about and log it.
9813 */
9814static void
Daniel Veillard4c004142003-10-07 11:33:24 +00009815xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9816{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009817 int best;
9818
9819 if ((ctxt == NULL) || (ctxt->states == NULL) ||
9820 (ctxt->states->nbState <= 0))
Daniel Veillard4c004142003-10-07 11:33:24 +00009821 return;
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009822
9823 best = xmlRelaxNGBestState(ctxt);
9824 if ((best >= 0) && (best < ctxt->states->nbState)) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009825 ctxt->state = ctxt->states->tabState[best];
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009826
Daniel Veillard4c004142003-10-07 11:33:24 +00009827 xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009828 }
9829}
9830
9831/**
Daniel Veillardfd573f12003-03-16 17:52:32 +00009832 * xmlRelaxNGValidateElementEnd:
9833 * @ctxt: a Relax-NG validation context
William M. Brack272693c2003-11-14 16:20:34 +00009834 * @dolog: indicate that error logging should be done
Daniel Veillardfd573f12003-03-16 17:52:32 +00009835 *
9836 * Validate the end of the element, implements check that
9837 * there is nothing left not consumed in the element content
9838 * or in the attribute list.
9839 *
9840 * Returns 0 if the validation succeeded or an error code.
9841 */
9842static int
William M. Brack272693c2003-11-14 16:20:34 +00009843xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
Daniel Veillard4c004142003-10-07 11:33:24 +00009844{
Daniel Veillard1ac24d32003-08-27 14:15:15 +00009845 int i;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009846 xmlRelaxNGValidStatePtr state;
9847
9848 state = ctxt->state;
9849 if (state->seq != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009850 state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9851 if (state->seq != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009852 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009853 VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9854 state->node->name, state->seq->name);
9855 }
9856 return (-1);
9857 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009858 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009859 for (i = 0; i < state->nbAttrs; i++) {
9860 if (state->attrs[i] != NULL) {
William M. Brack272693c2003-11-14 16:20:34 +00009861 if (dolog) {
Daniel Veillard4c004142003-10-07 11:33:24 +00009862 VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9863 state->attrs[i]->name, state->node->name);
9864 }
9865 return (-1 - i);
9866 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009867 }
Daniel Veillard4c004142003-10-07 11:33:24 +00009868 return (0);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009869}
9870
9871/**
9872 * xmlRelaxNGValidateState:
9873 * @ctxt: a Relax-NG validation context
9874 * @define: the definition to verify
9875 *
9876 * Validate the current state against the definition
9877 *
9878 * Returns 0 if the validation succeeded or an error code.
9879 */
9880static int
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009881xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9882 xmlRelaxNGDefinePtr define)
9883{
Daniel Veillardfd573f12003-03-16 17:52:32 +00009884 xmlNodePtr node;
9885 int ret = 0, i, tmp, oldflags, errNr;
Daniel Veillardbbb78b52003-03-21 01:24:45 +00009886 xmlRelaxNGValidStatePtr oldstate = NULL, state;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009887
9888 if (define == NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009889 VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9890 return (-1);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009891 }
9892
9893 if (ctxt->state != NULL) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009894 node = ctxt->state->seq;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009895 } else {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009896 node = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009897 }
9898#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009899 for (i = 0; i < ctxt->depth; i++)
9900 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009901 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009902 "Start validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +00009903 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009904 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009905 if ((node != NULL) && (node->name != NULL))
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009906 xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +00009907 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009908 xmlGenericError(xmlGenericErrorContext, "\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +00009909#endif
9910 ctxt->depth++;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009911 switch (define->type) {
9912 case XML_RELAXNG_EMPTY:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009913 ret = 0;
9914 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009915 case XML_RELAXNG_NOT_ALLOWED:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009916 ret = -1;
9917 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009918 case XML_RELAXNG_TEXT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009919 while ((node != NULL) &&
9920 ((node->type == XML_TEXT_NODE) ||
9921 (node->type == XML_COMMENT_NODE) ||
9922 (node->type == XML_PI_NODE) ||
9923 (node->type == XML_CDATA_SECTION_NODE)))
9924 node = node->next;
9925 ctxt->state->seq = node;
9926 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +00009927 case XML_RELAXNG_ELEMENT:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009928 errNr = ctxt->errNr;
9929 node = xmlRelaxNGSkipIgnored(ctxt, node);
9930 if (node == NULL) {
9931 VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9932 ret = -1;
9933 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9934 xmlRelaxNGDumpValidError(ctxt);
9935 break;
9936 }
9937 if (node->type != XML_ELEMENT_NODE) {
9938 VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9939 ret = -1;
9940 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9941 xmlRelaxNGDumpValidError(ctxt);
9942 break;
9943 }
9944 /*
9945 * This node was already validated successfully against
9946 * this definition.
9947 */
Daniel Veillard807daf82004-02-22 22:13:27 +00009948 if (node->psvi == define) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009949 ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9950 if (ctxt->errNr > errNr)
9951 xmlRelaxNGPopErrors(ctxt, errNr);
9952 if (ctxt->errNr != 0) {
9953 while ((ctxt->err != NULL) &&
9954 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9955 && (xmlStrEqual(ctxt->err->arg2, node->name)))
9956 ||
9957 ((ctxt->err->err ==
9958 XML_RELAXNG_ERR_ELEMEXTRANS)
9959 && (xmlStrEqual(ctxt->err->arg1, node->name)))
9960 || (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9961 || (ctxt->err->err ==
9962 XML_RELAXNG_ERR_NOTELEM)))
9963 xmlRelaxNGValidErrorPop(ctxt);
9964 }
9965 break;
9966 }
Daniel Veillardfd573f12003-03-16 17:52:32 +00009967
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009968 ret = xmlRelaxNGElementMatch(ctxt, define, node);
9969 if (ret <= 0) {
9970 ret = -1;
9971 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9972 xmlRelaxNGDumpValidError(ctxt);
9973 break;
9974 }
9975 ret = 0;
9976 if (ctxt->errNr != 0) {
9977 if (ctxt->errNr > errNr)
9978 xmlRelaxNGPopErrors(ctxt, errNr);
9979 while ((ctxt->err != NULL) &&
9980 (((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9981 (xmlStrEqual(ctxt->err->arg2, node->name))) ||
9982 ((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9983 (xmlStrEqual(ctxt->err->arg1, node->name))) ||
9984 (ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9985 (ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9986 xmlRelaxNGValidErrorPop(ctxt);
9987 }
9988 errNr = ctxt->errNr;
Daniel Veillardfd573f12003-03-16 17:52:32 +00009989
Daniel Veillardc58f4ef2003-04-14 16:11:26 +00009990 oldflags = ctxt->flags;
9991 if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9992 ctxt->flags -= FLAGS_MIXED_CONTENT;
9993 }
9994 state = xmlRelaxNGNewValidState(ctxt, node);
9995 if (state == NULL) {
9996 ret = -1;
9997 if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9998 xmlRelaxNGDumpValidError(ctxt);
9999 break;
10000 }
Daniel Veillard7fe1f3a2003-03-31 22:13:33 +000010001
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010002 oldstate = ctxt->state;
10003 ctxt->state = state;
10004 if (define->attrs != NULL) {
10005 tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
10006 if (tmp != 0) {
10007 ret = -1;
10008 VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
10009 }
10010 }
10011 if (define->contModel != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010012 xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
10013 xmlRelaxNGStatesPtr tmpstates = ctxt->states;
10014 xmlNodePtr nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010015
Daniel Veillard4c004142003-10-07 11:33:24 +000010016 nstate = xmlRelaxNGNewValidState(ctxt, node);
10017 ctxt->state = nstate;
10018 ctxt->states = NULL;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010019
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010020 tmp = xmlRelaxNGValidateCompiledContent(ctxt,
10021 define->contModel,
10022 ctxt->state->seq);
Daniel Veillard4c004142003-10-07 11:33:24 +000010023 nseq = ctxt->state->seq;
10024 ctxt->state = tmpstate;
10025 ctxt->states = tmpstates;
10026 xmlRelaxNGFreeValidState(ctxt, nstate);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010027
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010028#ifdef DEBUG_COMPILE
Daniel Veillard4c004142003-10-07 11:33:24 +000010029 xmlGenericError(xmlGenericErrorContext,
10030 "Validating content of '%s' : %d\n",
10031 define->name, tmp);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010032#endif
Daniel Veillardce192eb2003-04-16 15:58:05 +000010033 if (tmp != 0)
Daniel Veillard4c004142003-10-07 11:33:24 +000010034 ret = -1;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010035
10036 if (ctxt->states != NULL) {
10037 tmp = -1;
10038
Daniel Veillardce192eb2003-04-16 15:58:05 +000010039 for (i = 0; i < ctxt->states->nbState; i++) {
10040 state = ctxt->states->tabState[i];
10041 ctxt->state = state;
Daniel Veillard4c004142003-10-07 11:33:24 +000010042 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010043
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010044 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardce192eb2003-04-16 15:58:05 +000010045 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010046 break;
10047 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010048 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010049 if (tmp != 0) {
10050 /*
10051 * validation error, log the message for the "best" one
10052 */
10053 ctxt->flags |= FLAGS_IGNORABLE;
10054 xmlRelaxNGLogBestError(ctxt);
10055 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010056 for (i = 0; i < ctxt->states->nbState; i++) {
10057 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010058 ctxt->states->
10059 tabState[i]);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010060 }
10061 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10062 ctxt->flags = oldflags;
10063 ctxt->states = NULL;
10064 if ((ret == 0) && (tmp == -1))
10065 ret = -1;
10066 } else {
10067 state = ctxt->state;
Daniel Veillardd8ed1052007-06-12 09:24:46 +000010068 if (ctxt->state != NULL)
10069 ctxt->state->seq = nseq;
Daniel Veillardce192eb2003-04-16 15:58:05 +000010070 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010071 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardce192eb2003-04-16 15:58:05 +000010072 xmlRelaxNGFreeValidState(ctxt, state);
10073 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010074 } else {
10075 if (define->content != NULL) {
10076 tmp = xmlRelaxNGValidateDefinitionList(ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000010077 define->
10078 content);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010079 if (tmp != 0) {
10080 ret = -1;
10081 if (ctxt->state == NULL) {
10082 ctxt->state = oldstate;
10083 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10084 node->name);
10085 ctxt->state = NULL;
10086 } else {
10087 VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
10088 node->name);
10089 }
10090
10091 }
10092 }
10093 if (ctxt->states != NULL) {
10094 tmp = -1;
10095
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010096 for (i = 0; i < ctxt->states->nbState; i++) {
10097 state = ctxt->states->tabState[i];
10098 ctxt->state = state;
10099
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010100 if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010101 tmp = 0;
Daniel Veillard4c004142003-10-07 11:33:24 +000010102 break;
10103 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010104 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010105 if (tmp != 0) {
10106 /*
10107 * validation error, log the message for the "best" one
10108 */
10109 ctxt->flags |= FLAGS_IGNORABLE;
10110 xmlRelaxNGLogBestError(ctxt);
10111 }
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010112 for (i = 0; i < ctxt->states->nbState; i++) {
10113 xmlRelaxNGFreeValidState(ctxt,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010114 ctxt->states->tabState[i]);
10115 ctxt->states->tabState[i] = NULL;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010116 }
10117 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10118 ctxt->flags = oldflags;
10119 ctxt->states = NULL;
10120 if ((ret == 0) && (tmp == -1))
10121 ret = -1;
10122 } else {
10123 state = ctxt->state;
10124 if (ret == 0)
Daniel Veillard1ac24d32003-08-27 14:15:15 +000010125 ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010126 xmlRelaxNGFreeValidState(ctxt, state);
10127 }
10128 }
10129 if (ret == 0) {
Daniel Veillard807daf82004-02-22 22:13:27 +000010130 node->psvi = define;
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010131 }
10132 ctxt->flags = oldflags;
10133 ctxt->state = oldstate;
10134 if (oldstate != NULL)
10135 oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
10136 if (ret != 0) {
10137 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10138 xmlRelaxNGDumpValidError(ctxt);
10139 ret = 0;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010140#if 0
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010141 } else {
10142 ret = -2;
Daniel Veillardfa0d0942006-10-13 16:30:56 +000010143#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010144 }
10145 } else {
10146 if (ctxt->errNr > errNr)
10147 xmlRelaxNGPopErrors(ctxt, errNr);
10148 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010149
10150#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010151 xmlGenericError(xmlGenericErrorContext,
10152 "xmlRelaxNGValidateDefinition(): validated %s : %d",
10153 node->name, ret);
10154 if (oldstate == NULL)
10155 xmlGenericError(xmlGenericErrorContext, ": no state\n");
10156 else if (oldstate->seq == NULL)
10157 xmlGenericError(xmlGenericErrorContext, ": done\n");
10158 else if (oldstate->seq->type == XML_ELEMENT_NODE)
10159 xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
10160 oldstate->seq->name);
10161 else
10162 xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
10163 oldstate->seq->name, oldstate->seq->type);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010164#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010165 break;
10166 case XML_RELAXNG_OPTIONAL:{
10167 errNr = ctxt->errNr;
10168 oldflags = ctxt->flags;
10169 ctxt->flags |= FLAGS_IGNORABLE;
10170 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10171 ret =
10172 xmlRelaxNGValidateDefinitionList(ctxt,
10173 define->content);
10174 if (ret != 0) {
10175 if (ctxt->state != NULL)
10176 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10177 ctxt->state = oldstate;
10178 ctxt->flags = oldflags;
10179 ret = 0;
10180 if (ctxt->errNr > errNr)
10181 xmlRelaxNGPopErrors(ctxt, errNr);
10182 break;
10183 }
10184 if (ctxt->states != NULL) {
10185 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10186 } else {
10187 ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
10188 if (ctxt->states == NULL) {
10189 xmlRelaxNGFreeValidState(ctxt, oldstate);
10190 ctxt->flags = oldflags;
10191 ret = -1;
10192 if (ctxt->errNr > errNr)
10193 xmlRelaxNGPopErrors(ctxt, errNr);
10194 break;
10195 }
10196 xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
10197 xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
10198 ctxt->state = NULL;
10199 }
10200 ctxt->flags = oldflags;
10201 ret = 0;
10202 if (ctxt->errNr > errNr)
10203 xmlRelaxNGPopErrors(ctxt, errNr);
10204 break;
10205 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010206 case XML_RELAXNG_ONEORMORE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010207 errNr = ctxt->errNr;
10208 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10209 if (ret != 0) {
10210 break;
10211 }
10212 if (ctxt->errNr > errNr)
10213 xmlRelaxNGPopErrors(ctxt, errNr);
J. Peter Mugaasd2c329a2017-10-21 13:49:31 +020010214 /* Falls through. */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010215 case XML_RELAXNG_ZEROORMORE:{
10216 int progress;
10217 xmlRelaxNGStatesPtr states = NULL, res = NULL;
10218 int base, j;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010219
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010220 errNr = ctxt->errNr;
10221 res = xmlRelaxNGNewStates(ctxt, 1);
10222 if (res == NULL) {
10223 ret = -1;
10224 break;
10225 }
10226 /*
10227 * All the input states are also exit states
10228 */
10229 if (ctxt->state != NULL) {
10230 xmlRelaxNGAddStates(ctxt, res,
10231 xmlRelaxNGCopyValidState(ctxt,
10232 ctxt->
10233 state));
10234 } else {
10235 for (j = 0; j < ctxt->states->nbState; j++) {
10236 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010237 xmlRelaxNGCopyValidState(ctxt,
10238 ctxt->states->tabState[j]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010239 }
10240 }
10241 oldflags = ctxt->flags;
10242 ctxt->flags |= FLAGS_IGNORABLE;
10243 do {
10244 progress = 0;
10245 base = res->nbState;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010246
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010247 if (ctxt->states != NULL) {
10248 states = ctxt->states;
10249 for (i = 0; i < states->nbState; i++) {
10250 ctxt->state = states->tabState[i];
10251 ctxt->states = NULL;
10252 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10253 define->
10254 content);
10255 if (ret == 0) {
10256 if (ctxt->state != NULL) {
10257 tmp = xmlRelaxNGAddStates(ctxt, res,
10258 ctxt->state);
10259 ctxt->state = NULL;
10260 if (tmp == 1)
10261 progress = 1;
10262 } else if (ctxt->states != NULL) {
10263 for (j = 0; j < ctxt->states->nbState;
10264 j++) {
10265 tmp =
10266 xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010267 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010268 if (tmp == 1)
10269 progress = 1;
10270 }
10271 xmlRelaxNGFreeStates(ctxt,
10272 ctxt->states);
10273 ctxt->states = NULL;
10274 }
10275 } else {
10276 if (ctxt->state != NULL) {
10277 xmlRelaxNGFreeValidState(ctxt,
10278 ctxt->state);
10279 ctxt->state = NULL;
10280 }
10281 }
10282 }
10283 } else {
10284 ret = xmlRelaxNGValidateDefinitionList(ctxt,
10285 define->
10286 content);
10287 if (ret != 0) {
10288 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10289 ctxt->state = NULL;
10290 } else {
10291 base = res->nbState;
10292 if (ctxt->state != NULL) {
10293 tmp = xmlRelaxNGAddStates(ctxt, res,
10294 ctxt->state);
10295 ctxt->state = NULL;
10296 if (tmp == 1)
10297 progress = 1;
10298 } else if (ctxt->states != NULL) {
10299 for (j = 0; j < ctxt->states->nbState; j++) {
10300 tmp = xmlRelaxNGAddStates(ctxt, res,
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010301 ctxt->states->tabState[j]);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010302 if (tmp == 1)
10303 progress = 1;
10304 }
10305 if (states == NULL) {
10306 states = ctxt->states;
10307 } else {
10308 xmlRelaxNGFreeStates(ctxt,
10309 ctxt->states);
10310 }
10311 ctxt->states = NULL;
10312 }
10313 }
10314 }
10315 if (progress) {
10316 /*
10317 * Collect all the new nodes added at that step
10318 * and make them the new node set
10319 */
10320 if (res->nbState - base == 1) {
10321 ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10322 res->
10323 tabState
10324 [base]);
10325 } else {
10326 if (states == NULL) {
10327 xmlRelaxNGNewStates(ctxt,
10328 res->nbState - base);
Daniel Veillard14b56432006-03-09 18:41:40 +000010329 states = ctxt->states;
10330 if (states == NULL) {
10331 progress = 0;
10332 break;
10333 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010334 }
10335 states->nbState = 0;
10336 for (i = base; i < res->nbState; i++)
10337 xmlRelaxNGAddStates(ctxt, states,
10338 xmlRelaxNGCopyValidState
Daniel Veillard9fcd4622009-08-14 16:16:31 +020010339 (ctxt, res->tabState[i]));
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010340 ctxt->states = states;
10341 }
10342 }
10343 } while (progress == 1);
10344 if (states != NULL) {
10345 xmlRelaxNGFreeStates(ctxt, states);
10346 }
10347 ctxt->states = res;
10348 ctxt->flags = oldflags;
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010349#if 0
10350 /*
Daniel Veillard4c004142003-10-07 11:33:24 +000010351 * errors may have to be propagated back...
10352 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010353 if (ctxt->errNr > errNr)
10354 xmlRelaxNGPopErrors(ctxt, errNr);
Daniel Veillardc1ffa0a2003-08-26 13:56:48 +000010355#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010356 ret = 0;
10357 break;
10358 }
10359 case XML_RELAXNG_CHOICE:{
10360 xmlRelaxNGDefinePtr list = NULL;
10361 xmlRelaxNGStatesPtr states = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010362
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010363 node = xmlRelaxNGSkipIgnored(ctxt, node);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010364
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010365 errNr = ctxt->errNr;
Daniel Veillard9186a1f2005-01-15 12:38:10 +000010366 if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10367 (node != NULL)) {
10368 /*
10369 * node == NULL can't be optimized since IS_TRIABLE
10370 * doesn't account for choice which may lead to
10371 * only attributes.
10372 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010373 xmlHashTablePtr triage =
10374 (xmlHashTablePtr) define->data;
Daniel Veillarde063f482003-03-21 16:53:17 +000010375
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010376 /*
10377 * Something we can optimize cleanly there is only one
Haibo Huangcfd91dc2020-07-30 23:01:33 -070010378 * possible branch out !
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010379 */
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010380 if ((node->type == XML_TEXT_NODE) ||
10381 (node->type == XML_CDATA_SECTION_NODE)) {
10382 list =
10383 xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10384 } else if (node->type == XML_ELEMENT_NODE) {
10385 if (node->ns != NULL) {
10386 list = xmlHashLookup2(triage, node->name,
10387 node->ns->href);
10388 if (list == NULL)
10389 list =
10390 xmlHashLookup2(triage, BAD_CAST "#any",
10391 node->ns->href);
10392 } else
10393 list =
10394 xmlHashLookup2(triage, node->name, NULL);
10395 if (list == NULL)
10396 list =
10397 xmlHashLookup2(triage, BAD_CAST "#any",
10398 NULL);
10399 }
10400 if (list == NULL) {
10401 ret = -1;
William M. Brack2f076062004-03-21 11:21:14 +000010402 VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010403 break;
10404 }
10405 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10406 if (ret == 0) {
10407 }
10408 break;
10409 }
Daniel Veillarde063f482003-03-21 16:53:17 +000010410
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010411 list = define->content;
10412 oldflags = ctxt->flags;
10413 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010414
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010415 while (list != NULL) {
10416 oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10417 ret = xmlRelaxNGValidateDefinition(ctxt, list);
10418 if (ret == 0) {
10419 if (states == NULL) {
10420 states = xmlRelaxNGNewStates(ctxt, 1);
10421 }
10422 if (ctxt->state != NULL) {
10423 xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10424 } else if (ctxt->states != NULL) {
10425 for (i = 0; i < ctxt->states->nbState; i++) {
10426 xmlRelaxNGAddStates(ctxt, states,
10427 ctxt->states->
10428 tabState[i]);
10429 }
10430 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10431 ctxt->states = NULL;
10432 }
10433 } else {
10434 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10435 }
10436 ctxt->state = oldstate;
10437 list = list->next;
10438 }
10439 if (states != NULL) {
10440 xmlRelaxNGFreeValidState(ctxt, oldstate);
10441 ctxt->states = states;
10442 ctxt->state = NULL;
10443 ret = 0;
10444 } else {
10445 ctxt->states = NULL;
10446 }
10447 ctxt->flags = oldflags;
10448 if (ret != 0) {
10449 if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10450 xmlRelaxNGDumpValidError(ctxt);
10451 }
10452 } else {
10453 if (ctxt->errNr > errNr)
10454 xmlRelaxNGPopErrors(ctxt, errNr);
10455 }
10456 break;
10457 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010458 case XML_RELAXNG_DEF:
10459 case XML_RELAXNG_GROUP:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010460 ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10461 break;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010462 case XML_RELAXNG_INTERLEAVE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010463 ret = xmlRelaxNGValidateInterleave(ctxt, define);
10464 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010465 case XML_RELAXNG_ATTRIBUTE:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010466 ret = xmlRelaxNGValidateAttribute(ctxt, define);
10467 break;
Daniel Veillardf4e55762003-04-15 23:32:22 +000010468 case XML_RELAXNG_START:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010469 case XML_RELAXNG_NOOP:
Daniel Veillardfd573f12003-03-16 17:52:32 +000010470 case XML_RELAXNG_REF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010471 case XML_RELAXNG_EXTERNALREF:
Daniel Veillard952379b2003-03-17 15:37:12 +000010472 case XML_RELAXNG_PARENTREF:
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010473 ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10474 break;
10475 case XML_RELAXNG_DATATYPE:{
10476 xmlNodePtr child;
10477 xmlChar *content = NULL;
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_DATAELEM,
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 ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10507 ctxt->state->seq);
10508 if (ret == -1) {
10509 VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10510 } else if (ret == 0) {
10511 ctxt->state->seq = NULL;
10512 }
10513 if (content != NULL)
10514 xmlFree(content);
10515 break;
10516 }
10517 case XML_RELAXNG_VALUE:{
10518 xmlChar *content = NULL;
10519 xmlChar *oldvalue;
10520 xmlNodePtr child;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010521
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010522 child = node;
10523 while (child != NULL) {
10524 if (child->type == XML_ELEMENT_NODE) {
10525 VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10526 node->parent->name);
10527 ret = -1;
10528 break;
10529 } else if ((child->type == XML_TEXT_NODE) ||
10530 (child->type == XML_CDATA_SECTION_NODE)) {
10531 content = xmlStrcat(content, child->content);
10532 }
10533 /* TODO: handle entities ... */
10534 child = child->next;
10535 }
10536 if (ret == -1) {
10537 if (content != NULL)
10538 xmlFree(content);
10539 break;
10540 }
10541 if (content == NULL) {
10542 content = xmlStrdup(BAD_CAST "");
10543 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010544 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010545 ret = -1;
10546 break;
10547 }
10548 }
10549 oldvalue = ctxt->state->value;
10550 ctxt->state->value = content;
10551 ret = xmlRelaxNGValidateValue(ctxt, define);
10552 ctxt->state->value = oldvalue;
10553 if (ret == -1) {
10554 VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10555 } else if (ret == 0) {
10556 ctxt->state->seq = NULL;
10557 }
10558 if (content != NULL)
10559 xmlFree(content);
10560 break;
10561 }
10562 case XML_RELAXNG_LIST:{
10563 xmlChar *content;
10564 xmlNodePtr child;
10565 xmlChar *oldvalue, *oldendvalue;
10566 int len;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010567
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010568 /*
10569 * Make sure it's only text nodes
10570 */
10571
10572 content = NULL;
10573 child = node;
10574 while (child != NULL) {
10575 if (child->type == XML_ELEMENT_NODE) {
10576 VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10577 node->parent->name);
10578 ret = -1;
10579 break;
10580 } else if ((child->type == XML_TEXT_NODE) ||
10581 (child->type == XML_CDATA_SECTION_NODE)) {
10582 content = xmlStrcat(content, child->content);
10583 }
10584 /* TODO: handle entities ... */
10585 child = child->next;
10586 }
10587 if (ret == -1) {
10588 if (content != NULL)
10589 xmlFree(content);
10590 break;
10591 }
10592 if (content == NULL) {
10593 content = xmlStrdup(BAD_CAST "");
10594 if (content == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010595 xmlRngVErrMemory(ctxt, "validating\n");
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010596 ret = -1;
10597 break;
10598 }
10599 }
10600 len = xmlStrlen(content);
10601 oldvalue = ctxt->state->value;
10602 oldendvalue = ctxt->state->endvalue;
10603 ctxt->state->value = content;
10604 ctxt->state->endvalue = content + len;
10605 ret = xmlRelaxNGValidateValue(ctxt, define);
10606 ctxt->state->value = oldvalue;
10607 ctxt->state->endvalue = oldendvalue;
10608 if (ret == -1) {
10609 VALID_ERR(XML_RELAXNG_ERR_LIST);
10610 } else if ((ret == 0) && (node != NULL)) {
10611 ctxt->state->seq = node->next;
10612 }
10613 if (content != NULL)
10614 xmlFree(content);
10615 break;
10616 }
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010617 case XML_RELAXNG_EXCEPT:
10618 case XML_RELAXNG_PARAM:
10619 TODO ret = -1;
10620 break;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010621 }
10622 ctxt->depth--;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010623#ifdef DEBUG
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010624 for (i = 0; i < ctxt->depth; i++)
10625 xmlGenericError(xmlGenericErrorContext, " ");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010626 xmlGenericError(xmlGenericErrorContext,
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010627 "Validating %s ", xmlRelaxNGDefName(define));
Daniel Veillardfd573f12003-03-16 17:52:32 +000010628 if (define->name != NULL)
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010629 xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010630 if (ret == 0)
Haibo Huangcfd91dc2020-07-30 23:01:33 -070010631 xmlGenericError(xmlGenericErrorContext, "succeeded\n");
Daniel Veillardfd573f12003-03-16 17:52:32 +000010632 else
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010633 xmlGenericError(xmlGenericErrorContext, "failed\n");
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010634#endif
Daniel Veillardc58f4ef2003-04-14 16:11:26 +000010635 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010636}
10637
10638/**
Daniel Veillardfd573f12003-03-16 17:52:32 +000010639 * xmlRelaxNGValidateDefinition:
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010640 * @ctxt: a Relax-NG validation context
10641 * @define: the definition to verify
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010642 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010643 * Validate the current node lists against the definition
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010644 *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010645 * Returns 0 if the validation succeeded or an error code.
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010646 */
10647static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010648xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10649 xmlRelaxNGDefinePtr define)
10650{
Daniel Veillardfd573f12003-03-16 17:52:32 +000010651 xmlRelaxNGStatesPtr states, res;
10652 int i, j, k, ret, oldflags;
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010653
Daniel Veillardfd573f12003-03-16 17:52:32 +000010654 /*
10655 * We should NOT have both ctxt->state and ctxt->states
10656 */
10657 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010658 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10659 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010660 }
10661
10662 if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010663 if (ctxt->states != NULL) {
10664 ctxt->state = ctxt->states->tabState[0];
10665 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10666 ctxt->states = NULL;
10667 }
10668 ret = xmlRelaxNGValidateState(ctxt, define);
10669 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10670 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10671 ctxt->state = NULL;
10672 }
10673 if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10674 ctxt->state = ctxt->states->tabState[0];
10675 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10676 ctxt->states = NULL;
10677 }
10678 return (ret);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010679 }
10680
10681 states = ctxt->states;
10682 ctxt->states = NULL;
10683 res = NULL;
10684 j = 0;
10685 oldflags = ctxt->flags;
10686 ctxt->flags |= FLAGS_IGNORABLE;
Daniel Veillard4c004142003-10-07 11:33:24 +000010687 for (i = 0; i < states->nbState; i++) {
10688 ctxt->state = states->tabState[i];
10689 ctxt->states = NULL;
10690 ret = xmlRelaxNGValidateState(ctxt, define);
10691 /*
10692 * We should NOT have both ctxt->state and ctxt->states
10693 */
10694 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10695 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10696 ctxt->state = NULL;
10697 }
10698 if (ret == 0) {
10699 if (ctxt->states == NULL) {
10700 if (res != NULL) {
10701 /* add the state to the container */
10702 xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10703 ctxt->state = NULL;
10704 } else {
10705 /* add the state directly in states */
10706 states->tabState[j++] = ctxt->state;
10707 ctxt->state = NULL;
10708 }
10709 } else {
10710 if (res == NULL) {
10711 /* make it the new container and copy other results */
10712 res = ctxt->states;
10713 ctxt->states = NULL;
10714 for (k = 0; k < j; k++)
10715 xmlRelaxNGAddStates(ctxt, res,
10716 states->tabState[k]);
10717 } else {
10718 /* add all the new results to res and reff the container */
10719 for (k = 0; k < ctxt->states->nbState; k++)
10720 xmlRelaxNGAddStates(ctxt, res,
10721 ctxt->states->tabState[k]);
10722 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10723 ctxt->states = NULL;
10724 }
10725 }
10726 } else {
10727 if (ctxt->state != NULL) {
10728 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10729 ctxt->state = NULL;
10730 } else if (ctxt->states != NULL) {
10731 for (k = 0; k < ctxt->states->nbState; k++)
10732 xmlRelaxNGFreeValidState(ctxt,
10733 ctxt->states->tabState[k]);
10734 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10735 ctxt->states = NULL;
10736 }
10737 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010738 }
10739 ctxt->flags = oldflags;
10740 if (res != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010741 xmlRelaxNGFreeStates(ctxt, states);
10742 ctxt->states = res;
10743 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010744 } else if (j > 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010745 states->nbState = j;
10746 ctxt->states = states;
10747 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010748 } else if (j == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010749 ctxt->state = states->tabState[0];
10750 xmlRelaxNGFreeStates(ctxt, states);
10751 ret = 0;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010752 } else {
Daniel Veillard4c004142003-10-07 11:33:24 +000010753 ret = -1;
10754 xmlRelaxNGFreeStates(ctxt, states);
10755 if (ctxt->states != NULL) {
10756 xmlRelaxNGFreeStates(ctxt, ctxt->states);
10757 ctxt->states = NULL;
10758 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010759 }
10760 if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010761 TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10762 ctxt->state = NULL;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010763 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010764 return (ret);
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010765}
10766
Daniel Veillard1564e6e2003-03-15 21:30:25 +000010767/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000010768 * xmlRelaxNGValidateDocument:
10769 * @ctxt: a Relax-NG validation context
10770 * @doc: the document
10771 *
10772 * Validate the given document
10773 *
10774 * Returns 0 if the validation succeeded or an error code.
10775 */
10776static int
Daniel Veillard4c004142003-10-07 11:33:24 +000010777xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10778{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010779 int ret;
10780 xmlRelaxNGPtr schema;
10781 xmlRelaxNGGrammarPtr grammar;
10782 xmlRelaxNGValidStatePtr state;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010783 xmlNodePtr node;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010784
10785 if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000010786 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010787
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010788 ctxt->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010789 schema = ctxt->schema;
10790 grammar = schema->topgrammar;
10791 if (grammar == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010792 VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10793 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010794 }
10795 state = xmlRelaxNGNewValidState(ctxt, NULL);
10796 ctxt->state = state;
10797 ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
Daniel Veillardfd573f12003-03-16 17:52:32 +000010798 if ((ctxt->state != NULL) && (state->seq != NULL)) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010799 state = ctxt->state;
10800 node = state->seq;
10801 node = xmlRelaxNGSkipIgnored(ctxt, node);
10802 if (node != NULL) {
10803 if (ret != -1) {
10804 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10805 ret = -1;
10806 }
10807 }
Daniel Veillardfd573f12003-03-16 17:52:32 +000010808 } else if (ctxt->states != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010809 int i;
10810 int tmp = -1;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010811
Daniel Veillard4c004142003-10-07 11:33:24 +000010812 for (i = 0; i < ctxt->states->nbState; i++) {
10813 state = ctxt->states->tabState[i];
10814 node = state->seq;
10815 node = xmlRelaxNGSkipIgnored(ctxt, node);
10816 if (node == NULL)
10817 tmp = 0;
10818 xmlRelaxNGFreeValidState(ctxt, state);
10819 }
10820 if (tmp == -1) {
10821 if (ret != -1) {
10822 VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10823 ret = -1;
10824 }
10825 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010826 }
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010827 if (ctxt->state != NULL) {
10828 xmlRelaxNGFreeValidState(ctxt, ctxt->state);
Daniel Veillard4c004142003-10-07 11:33:24 +000010829 ctxt->state = NULL;
Daniel Veillardbbb78b52003-03-21 01:24:45 +000010830 }
Daniel Veillard4c004142003-10-07 11:33:24 +000010831 if (ret != 0)
10832 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010833#ifdef DEBUG
10834 else if (ctxt->errNr != 0) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010835 ctxt->error(ctxt->userData,
10836 "%d Extra error messages left on stack !\n",
10837 ctxt->errNr);
10838 xmlRelaxNGDumpValidError(ctxt);
Daniel Veillard580ced82003-03-21 21:22:48 +000010839 }
10840#endif
Daniel Veillardf54cd532004-02-25 11:52:31 +000010841#ifdef LIBXML_VALID_ENABLED
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010842 if (ctxt->idref == 1) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010843 xmlValidCtxt vctxt;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010844
Daniel Veillard4c004142003-10-07 11:33:24 +000010845 memset(&vctxt, 0, sizeof(xmlValidCtxt));
10846 vctxt.valid = 1;
10847 vctxt.error = ctxt->error;
10848 vctxt.warning = ctxt->warning;
10849 vctxt.userData = ctxt->userData;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010850
Daniel Veillard4c004142003-10-07 11:33:24 +000010851 if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10852 ret = -1;
Daniel Veillardc3da18a2003-03-18 00:31:04 +000010853 }
Daniel Veillardf54cd532004-02-25 11:52:31 +000010854#endif /* LIBXML_VALID_ENABLED */
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010855 if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
Daniel Veillard4c004142003-10-07 11:33:24 +000010856 ret = -1;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010857
Daniel Veillard4c004142003-10-07 11:33:24 +000010858 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000010859}
10860
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020010861/**
10862 * xmlRelaxNGCleanPSVI:
10863 * @node: an input element or document
10864 *
10865 * Call this routine to speed up XPath computation on static documents.
10866 * This stamps all the element nodes with the document order
10867 * Like for line information, the order is kept in the element->content
10868 * field, the value stored is actually - the node number (starting at -1)
10869 * to be able to differentiate from line numbers.
10870 *
10871 * Returns the number of elements found in the document or -1 in case
10872 * of error.
10873 */
10874static void
10875xmlRelaxNGCleanPSVI(xmlNodePtr node) {
10876 xmlNodePtr cur;
10877
10878 if ((node == NULL) ||
10879 ((node->type != XML_ELEMENT_NODE) &&
10880 (node->type != XML_DOCUMENT_NODE) &&
10881 (node->type != XML_HTML_DOCUMENT_NODE)))
10882 return;
10883 if (node->type == XML_ELEMENT_NODE)
10884 node->psvi = NULL;
10885
10886 cur = node->children;
10887 while (cur != NULL) {
10888 if (cur->type == XML_ELEMENT_NODE) {
10889 cur->psvi = NULL;
10890 if (cur->children != NULL) {
10891 cur = cur->children;
10892 continue;
10893 }
10894 }
10895 if (cur->next != NULL) {
10896 cur = cur->next;
10897 continue;
10898 }
10899 do {
10900 cur = cur->parent;
10901 if (cur == NULL)
10902 break;
10903 if (cur == node) {
10904 cur = NULL;
10905 break;
10906 }
10907 if (cur->next != NULL) {
10908 cur = cur->next;
10909 break;
10910 }
10911 } while (cur != NULL);
10912 }
10913 return;
10914}
Daniel Veillardfd573f12003-03-16 17:52:32 +000010915/************************************************************************
Daniel Veillardf8e3db02012-09-11 13:26:36 +080010916 * *
10917 * Validation interfaces *
10918 * *
Daniel Veillardfd573f12003-03-16 17:52:32 +000010919 ************************************************************************/
Daniel Veillard4c004142003-10-07 11:33:24 +000010920
Daniel Veillard6eadf632003-01-23 18:29:16 +000010921/**
10922 * xmlRelaxNGNewValidCtxt:
10923 * @schema: a precompiled XML RelaxNGs
10924 *
10925 * Create an XML RelaxNGs validation context based on the given schema
10926 *
10927 * Returns the validation context or NULL in case of error
10928 */
10929xmlRelaxNGValidCtxtPtr
Daniel Veillard4c004142003-10-07 11:33:24 +000010930xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10931{
Daniel Veillard6eadf632003-01-23 18:29:16 +000010932 xmlRelaxNGValidCtxtPtr ret;
10933
10934 ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10935 if (ret == NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010936 xmlRngVErrMemory(NULL, "building context\n");
Daniel Veillard6eadf632003-01-23 18:29:16 +000010937 return (NULL);
10938 }
10939 memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10940 ret->schema = schema;
Daniel Veillard1703c5f2003-02-10 14:28:44 +000010941 ret->error = xmlGenericError;
10942 ret->userData = xmlGenericErrorContext;
Daniel Veillard42f12e92003-03-07 18:32:59 +000010943 ret->errNr = 0;
10944 ret->errMax = 0;
10945 ret->err = NULL;
10946 ret->errTab = NULL;
Daniel Veillardb30ca312005-09-04 13:50:03 +000010947 if (schema != NULL)
10948 ret->idref = schema->idref;
Daniel Veillard798024a2003-03-19 10:36:09 +000010949 ret->states = NULL;
10950 ret->freeState = NULL;
10951 ret->freeStates = NULL;
Daniel Veillarda507fbf2003-03-31 16:09:37 +000010952 ret->errNo = XML_RELAXNG_OK;
Daniel Veillard6eadf632003-01-23 18:29:16 +000010953 return (ret);
10954}
10955
10956/**
10957 * xmlRelaxNGFreeValidCtxt:
10958 * @ctxt: the schema validation context
10959 *
10960 * Free the resources associated to the schema validation context
10961 */
10962void
Daniel Veillard4c004142003-10-07 11:33:24 +000010963xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10964{
Daniel Veillard798024a2003-03-19 10:36:09 +000010965 int k;
10966
Daniel Veillard6eadf632003-01-23 18:29:16 +000010967 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010968 return;
Daniel Veillardfd573f12003-03-16 17:52:32 +000010969 if (ctxt->states != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010970 xmlRelaxNGFreeStates(NULL, ctxt->states);
Daniel Veillard798024a2003-03-19 10:36:09 +000010971 if (ctxt->freeState != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010972 for (k = 0; k < ctxt->freeState->nbState; k++) {
10973 xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10974 }
10975 xmlRelaxNGFreeStates(NULL, ctxt->freeState);
Daniel Veillard798024a2003-03-19 10:36:09 +000010976 }
Daniel Veillard798024a2003-03-19 10:36:09 +000010977 if (ctxt->freeStates != NULL) {
Daniel Veillard4c004142003-10-07 11:33:24 +000010978 for (k = 0; k < ctxt->freeStatesNr; k++) {
10979 xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10980 }
10981 xmlFree(ctxt->freeStates);
Daniel Veillard798024a2003-03-19 10:36:09 +000010982 }
Daniel Veillard42f12e92003-03-07 18:32:59 +000010983 if (ctxt->errTab != NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000010984 xmlFree(ctxt->errTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010985 if (ctxt->elemTab != NULL) {
10986 xmlRegExecCtxtPtr exec;
10987
Daniel Veillard4c004142003-10-07 11:33:24 +000010988 exec = xmlRelaxNGElemPop(ctxt);
10989 while (exec != NULL) {
10990 xmlRegFreeExecCtxt(exec);
10991 exec = xmlRelaxNGElemPop(ctxt);
10992 }
10993 xmlFree(ctxt->elemTab);
Daniel Veillardf4e55762003-04-15 23:32:22 +000010994 }
Daniel Veillard6eadf632003-01-23 18:29:16 +000010995 xmlFree(ctxt);
10996}
10997
10998/**
10999 * xmlRelaxNGSetValidErrors:
11000 * @ctxt: a Relax-NG validation context
11001 * @err: the error function
11002 * @warn: the warning function
11003 * @ctx: the functions context
11004 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -070011005 * Set the error and warning callback information
Daniel Veillard6eadf632003-01-23 18:29:16 +000011006 */
11007void
11008xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000011009 xmlRelaxNGValidityErrorFunc err,
11010 xmlRelaxNGValidityWarningFunc warn, void *ctx)
11011{
Daniel Veillard6eadf632003-01-23 18:29:16 +000011012 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000011013 return;
Daniel Veillard6eadf632003-01-23 18:29:16 +000011014 ctxt->error = err;
11015 ctxt->warning = warn;
11016 ctxt->userData = ctx;
Daniel Veillardb30ca312005-09-04 13:50:03 +000011017 ctxt->serror = NULL;
Daniel Veillard6eadf632003-01-23 18:29:16 +000011018}
11019
11020/**
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000011021 * xmlRelaxNGSetValidStructuredErrors:
11022 * @ctxt: a Relax-NG validation context
11023 * @serror: the structured error function
11024 * @ctx: the functions context
11025 *
11026 * Set the structured error callback
11027 */
11028void
11029xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillardb30ca312005-09-04 13:50:03 +000011030 xmlStructuredErrorFunc serror, void *ctx)
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000011031{
11032 if (ctxt == NULL)
11033 return;
Daniel Veillardb30ca312005-09-04 13:50:03 +000011034 ctxt->serror = serror;
Daniel Veillardda0aa4c2005-07-13 23:07:49 +000011035 ctxt->error = NULL;
11036 ctxt->warning = NULL;
11037 ctxt->userData = ctx;
11038}
11039
11040/**
Daniel Veillard409a8142003-07-18 15:16:57 +000011041 * xmlRelaxNGGetValidErrors:
11042 * @ctxt: a Relax-NG validation context
11043 * @err: the error function result
11044 * @warn: the warning function result
11045 * @ctx: the functions context result
11046 *
Haibo Huangcfd91dc2020-07-30 23:01:33 -070011047 * Get the error and warning callback information
Daniel Veillard409a8142003-07-18 15:16:57 +000011048 *
11049 * Returns -1 in case of error and 0 otherwise
11050 */
11051int
11052xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
Daniel Veillard4c004142003-10-07 11:33:24 +000011053 xmlRelaxNGValidityErrorFunc * err,
11054 xmlRelaxNGValidityWarningFunc * warn, void **ctx)
11055{
Daniel Veillard409a8142003-07-18 15:16:57 +000011056 if (ctxt == NULL)
Daniel Veillard4c004142003-10-07 11:33:24 +000011057 return (-1);
11058 if (err != NULL)
11059 *err = ctxt->error;
11060 if (warn != NULL)
11061 *warn = ctxt->warning;
11062 if (ctx != NULL)
11063 *ctx = ctxt->userData;
11064 return (0);
Daniel Veillard409a8142003-07-18 15:16:57 +000011065}
11066
11067/**
Daniel Veillard6eadf632003-01-23 18:29:16 +000011068 * xmlRelaxNGValidateDoc:
11069 * @ctxt: a Relax-NG validation context
11070 * @doc: a parsed document tree
11071 *
11072 * Validate a document tree in memory.
11073 *
11074 * Returns 0 if the document is valid, a positive error code
11075 * number otherwise and -1 in case of internal or API error.
11076 */
11077int
Daniel Veillard4c004142003-10-07 11:33:24 +000011078xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
11079{
Daniel Veillard6eadf632003-01-23 18:29:16 +000011080 int ret;
11081
11082 if ((ctxt == NULL) || (doc == NULL))
Daniel Veillard4c004142003-10-07 11:33:24 +000011083 return (-1);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011084
11085 ctxt->doc = doc;
11086
11087 ret = xmlRelaxNGValidateDocument(ctxt, doc);
Daniel Veillard71531f32003-02-05 13:19:53 +000011088 /*
Daniel Veillarda4f27cb2009-08-21 17:34:17 +020011089 * Remove all left PSVI
11090 */
11091 xmlRelaxNGCleanPSVI((xmlNodePtr) doc);
11092
11093 /*
Daniel Veillard71531f32003-02-05 13:19:53 +000011094 * TODO: build error codes
11095 */
11096 if (ret == -1)
Daniel Veillard4c004142003-10-07 11:33:24 +000011097 return (1);
11098 return (ret);
Daniel Veillard6eadf632003-01-23 18:29:16 +000011099}
11100
11101#endif /* LIBXML_SCHEMAS_ENABLED */