2
* relaxng.c : implementation of the Relax-NG handling and validity checking
4
* See Copyright for the status of this software.
6
* Daniel Veillard <veillard@redhat.com>
11
* - add support for DTD compatibility spec
12
* http://www.oasis-open.org/committees/relax-ng/compatibility-20011203.html
13
* - report better mem allocations pbms at runtime and abort immediately.
19
#ifdef LIBXML_SCHEMAS_ENABLED
23
#include <libxml/xmlmemory.h>
24
#include <libxml/parser.h>
25
#include <libxml/parserInternals.h>
26
#include <libxml/hash.h>
27
#include <libxml/uri.h>
29
#include <libxml/relaxng.h>
31
#include <libxml/xmlschemastypes.h>
32
#include <libxml/xmlautomata.h>
33
#include <libxml/xmlregexp.h>
34
#include <libxml/xmlschemastypes.h>
37
* The Relax-NG namespace
39
static const xmlChar *xmlRelaxNGNs = (const xmlChar *)
40
"http://relaxng.org/ns/structure/1.0";
42
#define IS_RELAXNG(node, type) \
43
((node != NULL) && (node->ns != NULL) && \
44
(xmlStrEqual(node->name, (const xmlChar *) type)) && \
45
(xmlStrEqual(node->ns->href, xmlRelaxNGNs)))
51
#define DEBUG_GRAMMAR 1
53
#define DEBUG_CONTENT 1
59
#define DEBUG_INTERLEAVE 1
63
#define DEBUG_INCLUDE 1
67
#define DEBUG_COMPILE 1
69
#define DEBUG_PROGRESSIVE 1
75
xmlGenericError(xmlGenericErrorContext, \
76
"Unimplemented block at %s:%d\n", \
79
typedef struct _xmlRelaxNGSchema xmlRelaxNGSchema;
80
typedef xmlRelaxNGSchema *xmlRelaxNGSchemaPtr;
82
typedef struct _xmlRelaxNGDefine xmlRelaxNGDefine;
83
typedef xmlRelaxNGDefine *xmlRelaxNGDefinePtr;
85
typedef struct _xmlRelaxNGDocument xmlRelaxNGDocument;
86
typedef xmlRelaxNGDocument *xmlRelaxNGDocumentPtr;
88
typedef struct _xmlRelaxNGInclude xmlRelaxNGInclude;
89
typedef xmlRelaxNGInclude *xmlRelaxNGIncludePtr;
92
XML_RELAXNG_COMBINE_UNDEFINED = 0, /* undefined */
93
XML_RELAXNG_COMBINE_CHOICE, /* choice */
94
XML_RELAXNG_COMBINE_INTERLEAVE /* interleave */
98
XML_RELAXNG_CONTENT_ERROR = -1,
99
XML_RELAXNG_CONTENT_EMPTY = 0,
100
XML_RELAXNG_CONTENT_SIMPLE,
101
XML_RELAXNG_CONTENT_COMPLEX
102
} xmlRelaxNGContentType;
104
typedef struct _xmlRelaxNGGrammar xmlRelaxNGGrammar;
105
typedef xmlRelaxNGGrammar *xmlRelaxNGGrammarPtr;
107
struct _xmlRelaxNGGrammar {
108
xmlRelaxNGGrammarPtr parent; /* the parent grammar if any */
109
xmlRelaxNGGrammarPtr children; /* the children grammar if any */
110
xmlRelaxNGGrammarPtr next; /* the next grammar if any */
111
xmlRelaxNGDefinePtr start; /* <start> content */
112
xmlRelaxNGCombine combine; /* the default combine value */
113
xmlRelaxNGDefinePtr startList; /* list of <start> definitions */
114
xmlHashTablePtr defs; /* define* */
115
xmlHashTablePtr refs; /* references */
120
XML_RELAXNG_NOOP = -1, /* a no operation from simplification */
121
XML_RELAXNG_EMPTY = 0, /* an empty pattern */
122
XML_RELAXNG_NOT_ALLOWED, /* not allowed top */
123
XML_RELAXNG_EXCEPT, /* except present in nameclass defs */
124
XML_RELAXNG_TEXT, /* textual content */
125
XML_RELAXNG_ELEMENT, /* an element */
126
XML_RELAXNG_DATATYPE, /* extenal data type definition */
127
XML_RELAXNG_PARAM, /* extenal data type parameter */
128
XML_RELAXNG_VALUE, /* value from an extenal data type definition */
129
XML_RELAXNG_LIST, /* a list of patterns */
130
XML_RELAXNG_ATTRIBUTE, /* an attrbute following a pattern */
131
XML_RELAXNG_DEF, /* a definition */
132
XML_RELAXNG_REF, /* reference to a definition */
133
XML_RELAXNG_EXTERNALREF, /* reference to an external def */
134
XML_RELAXNG_PARENTREF, /* reference to a def in the parent grammar */
135
XML_RELAXNG_OPTIONAL, /* optional patterns */
136
XML_RELAXNG_ZEROORMORE, /* zero or more non empty patterns */
137
XML_RELAXNG_ONEORMORE, /* one or more non empty patterns */
138
XML_RELAXNG_CHOICE, /* a choice between non empty patterns */
139
XML_RELAXNG_GROUP, /* a pair/group of non empty patterns */
140
XML_RELAXNG_INTERLEAVE, /* interleaving choice of non-empty patterns */
141
XML_RELAXNG_START /* Used to keep track of starts on grammars */
144
#define IS_NULLABLE (1 << 0)
145
#define IS_NOT_NULLABLE (1 << 1)
146
#define IS_INDETERMINIST (1 << 2)
147
#define IS_MIXED (1 << 3)
148
#define IS_TRIABLE (1 << 4)
149
#define IS_PROCESSED (1 << 5)
150
#define IS_COMPILABLE (1 << 6)
151
#define IS_NOT_COMPILABLE (1 << 7)
153
struct _xmlRelaxNGDefine {
154
xmlRelaxNGType type; /* the type of definition */
155
xmlNodePtr node; /* the node in the source */
156
xmlChar *name; /* the element local name if present */
157
xmlChar *ns; /* the namespace local name if present */
158
xmlChar *value; /* value when available */
159
void *data; /* data lib or specific pointer */
160
xmlRelaxNGDefinePtr content; /* the expected content */
161
xmlRelaxNGDefinePtr parent; /* the parent definition, if any */
162
xmlRelaxNGDefinePtr next; /* list within grouping sequences */
163
xmlRelaxNGDefinePtr attrs; /* list of attributes for elements */
164
xmlRelaxNGDefinePtr nameClass; /* the nameClass definition if any */
165
xmlRelaxNGDefinePtr nextHash; /* next define in defs/refs hash tables */
166
short depth; /* used for the cycle detection */
167
short dflags; /* define related flags */
168
xmlRegexpPtr contModel; /* a compiled content model if available */
174
* A RelaxNGs definition
177
void *_private; /* unused by the library for users or bindings */
178
xmlRelaxNGGrammarPtr topgrammar;
181
int idref; /* requires idref checking */
183
xmlHashTablePtr defs; /* define */
184
xmlHashTablePtr refs; /* references */
185
xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
186
xmlRelaxNGIncludePtr includes; /* all the includes loaded */
187
int defNr; /* number of defines used */
188
xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
192
#define XML_RELAXNG_IN_ATTRIBUTE (1 << 0)
193
#define XML_RELAXNG_IN_ONEORMORE (1 << 1)
194
#define XML_RELAXNG_IN_LIST (1 << 2)
195
#define XML_RELAXNG_IN_DATAEXCEPT (1 << 3)
196
#define XML_RELAXNG_IN_START (1 << 4)
197
#define XML_RELAXNG_IN_OOMGROUP (1 << 5)
198
#define XML_RELAXNG_IN_OOMINTERLEAVE (1 << 6)
199
#define XML_RELAXNG_IN_EXTERNALREF (1 << 7)
200
#define XML_RELAXNG_IN_ANYEXCEPT (1 << 8)
201
#define XML_RELAXNG_IN_NSEXCEPT (1 << 9)
203
struct _xmlRelaxNGParserCtxt {
204
void *userData; /* user specific data block */
205
xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
206
xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
207
xmlStructuredErrorFunc serror;
208
xmlRelaxNGValidErr err;
210
xmlRelaxNGPtr schema; /* The schema in use */
211
xmlRelaxNGGrammarPtr grammar; /* the current grammar */
212
xmlRelaxNGGrammarPtr parentgrammar; /* the parent grammar */
213
int flags; /* parser flags */
214
int nbErrors; /* number of errors at parse time */
215
int nbWarnings; /* number of warnings at parse time */
216
const xmlChar *define; /* the current define scope */
217
xmlRelaxNGDefinePtr def; /* the current define */
220
xmlHashTablePtr interleaves; /* keep track of all the interleaves */
222
xmlRelaxNGDocumentPtr documents; /* all the documents loaded */
223
xmlRelaxNGIncludePtr includes; /* all the includes loaded */
227
int defNr; /* number of defines used */
228
int defMax; /* number of defines aloocated */
229
xmlRelaxNGDefinePtr *defTab; /* pointer to the allocated definitions */
234
/* the document stack */
235
xmlRelaxNGDocumentPtr doc; /* Current parsed external ref */
236
int docNr; /* Depth of the parsing stack */
237
int docMax; /* Max depth of the parsing stack */
238
xmlRelaxNGDocumentPtr *docTab; /* array of docs */
240
/* the include stack */
241
xmlRelaxNGIncludePtr inc; /* Current parsed include */
242
int incNr; /* Depth of the include parsing stack */
243
int incMax; /* Max depth of the parsing stack */
244
xmlRelaxNGIncludePtr *incTab; /* array of incs */
246
int idref; /* requires idref checking */
248
/* used to compile content models */
249
xmlAutomataPtr am; /* the automata */
250
xmlAutomataStatePtr state; /* used to build the automata */
252
int crng; /* compact syntax and other flags */
253
int freedoc; /* need to free the document */
256
#define FLAGS_IGNORABLE 1
257
#define FLAGS_NEGATIVE 2
258
#define FLAGS_MIXED_CONTENT 4
259
#define FLAGS_NOERROR 8
262
* xmlRelaxNGInterleaveGroup:
264
* A RelaxNGs partition set associated to lists of definitions
266
typedef struct _xmlRelaxNGInterleaveGroup xmlRelaxNGInterleaveGroup;
267
typedef xmlRelaxNGInterleaveGroup *xmlRelaxNGInterleaveGroupPtr;
268
struct _xmlRelaxNGInterleaveGroup {
269
xmlRelaxNGDefinePtr rule; /* the rule to satisfy */
270
xmlRelaxNGDefinePtr *defs; /* the array of element definitions */
271
xmlRelaxNGDefinePtr *attrs; /* the array of attributes definitions */
274
#define IS_DETERMINIST 1
275
#define IS_NEEDCHECK 2
278
* xmlRelaxNGPartitions:
280
* A RelaxNGs partition associated to an interleave group
282
typedef struct _xmlRelaxNGPartition xmlRelaxNGPartition;
283
typedef xmlRelaxNGPartition *xmlRelaxNGPartitionPtr;
284
struct _xmlRelaxNGPartition {
285
int nbgroups; /* number of groups in the partitions */
286
xmlHashTablePtr triage; /* hash table used to direct nodes to the
287
* right group when possible */
288
int flags; /* determinist ? */
289
xmlRelaxNGInterleaveGroupPtr *groups;
293
* xmlRelaxNGValidState:
295
* A RelaxNGs validation state
298
typedef struct _xmlRelaxNGValidState xmlRelaxNGValidState;
299
typedef xmlRelaxNGValidState *xmlRelaxNGValidStatePtr;
300
struct _xmlRelaxNGValidState {
301
xmlNodePtr node; /* the current node */
302
xmlNodePtr seq; /* the sequence of children left to validate */
303
int nbAttrs; /* the number of attributes */
304
int maxAttrs; /* the size of attrs */
305
int nbAttrLeft; /* the number of attributes left to validate */
306
xmlChar *value; /* the value when operating on string */
307
xmlChar *endvalue; /* the end value when operating on string */
308
xmlAttrPtr *attrs; /* the array of attributes */
314
* A RelaxNGs container for validation state
316
typedef struct _xmlRelaxNGStates xmlRelaxNGStates;
317
typedef xmlRelaxNGStates *xmlRelaxNGStatesPtr;
318
struct _xmlRelaxNGStates {
319
int nbState; /* the number of states */
320
int maxState; /* the size of the array */
321
xmlRelaxNGValidStatePtr *tabState;
324
#define ERROR_IS_DUP 1
327
* xmlRelaxNGValidError:
329
* A RelaxNGs validation error
331
typedef struct _xmlRelaxNGValidError xmlRelaxNGValidError;
332
typedef xmlRelaxNGValidError *xmlRelaxNGValidErrorPtr;
333
struct _xmlRelaxNGValidError {
334
xmlRelaxNGValidErr err; /* the error number */
335
int flags; /* flags */
336
xmlNodePtr node; /* the current node */
337
xmlNodePtr seq; /* the current child */
338
const xmlChar *arg1; /* first arg */
339
const xmlChar *arg2; /* second arg */
343
* xmlRelaxNGValidCtxt:
345
* A RelaxNGs validation context
348
struct _xmlRelaxNGValidCtxt {
349
void *userData; /* user specific data block */
350
xmlRelaxNGValidityErrorFunc error; /* the callback in case of errors */
351
xmlRelaxNGValidityWarningFunc warning; /* the callback in case of warning */
352
xmlStructuredErrorFunc serror;
353
int nbErrors; /* number of errors in validation */
355
xmlRelaxNGPtr schema; /* The schema in use */
356
xmlDocPtr doc; /* the document being validated */
357
int flags; /* validation flags */
358
int depth; /* validation depth */
359
int idref; /* requires idref checking */
360
int errNo; /* the first error found */
363
* Errors accumulated in branches may have to be stacked to be
364
* provided back when it's sure they affect validation.
366
xmlRelaxNGValidErrorPtr err; /* Last error */
367
int errNr; /* Depth of the error stack */
368
int errMax; /* Max depth of the error stack */
369
xmlRelaxNGValidErrorPtr errTab; /* stack of errors */
371
xmlRelaxNGValidStatePtr state; /* the current validation state */
372
xmlRelaxNGStatesPtr states; /* the accumulated state list */
374
xmlRelaxNGStatesPtr freeState; /* the pool of free valid states */
377
xmlRelaxNGStatesPtr *freeStates; /* the pool of free state groups */
380
* This is used for "progressive" validation
382
xmlRegExecCtxtPtr elem; /* the current element regexp */
383
int elemNr; /* the number of element validated */
384
int elemMax; /* the max depth of elements */
385
xmlRegExecCtxtPtr *elemTab; /* the stack of regexp runtime */
386
int pstate; /* progressive state */
387
xmlNodePtr pnode; /* the current node */
388
xmlRelaxNGDefinePtr pdef; /* the non-streamable definition */
389
int perr; /* signal error in content model
390
* outside the regexp */
396
* Structure associated to a RelaxNGs document element
398
struct _xmlRelaxNGInclude {
399
xmlRelaxNGIncludePtr next; /* keep a chain of includes */
400
xmlChar *href; /* the normalized href value */
401
xmlDocPtr doc; /* the associated XML document */
402
xmlRelaxNGDefinePtr content; /* the definitions */
403
xmlRelaxNGPtr schema; /* the schema */
407
* xmlRelaxNGDocument:
409
* Structure associated to a RelaxNGs document element
411
struct _xmlRelaxNGDocument {
412
xmlRelaxNGDocumentPtr next; /* keep a chain of documents */
413
xmlChar *href; /* the normalized href value */
414
xmlDocPtr doc; /* the associated XML document */
415
xmlRelaxNGDefinePtr content; /* the definitions */
416
xmlRelaxNGPtr schema; /* the schema */
420
/************************************************************************
422
* Some factorized error routines *
424
************************************************************************/
428
* @ctxt: an Relax-NG parser context
429
* @extra: extra informations
431
* Handle a redefinition of attribute error
434
xmlRngPErrMemory(xmlRelaxNGParserCtxtPtr ctxt, const char *extra)
436
xmlStructuredErrorFunc schannel = NULL;
437
xmlGenericErrorFunc channel = NULL;
441
if (ctxt->serror != NULL)
442
schannel = ctxt->serror;
444
channel = ctxt->error;
445
data = ctxt->userData;
449
__xmlRaiseError(schannel, channel, data,
450
NULL, NULL, XML_FROM_RELAXNGP,
451
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
453
"Memory allocation failed : %s\n", extra);
455
__xmlRaiseError(schannel, channel, data,
456
NULL, NULL, XML_FROM_RELAXNGP,
457
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
458
NULL, NULL, 0, 0, "Memory allocation failed\n");
463
* @ctxt: a Relax-NG validation context
464
* @extra: extra informations
466
* Handle a redefinition of attribute error
469
xmlRngVErrMemory(xmlRelaxNGValidCtxtPtr ctxt, const char *extra)
471
xmlStructuredErrorFunc schannel = NULL;
472
xmlGenericErrorFunc channel = NULL;
476
if (ctxt->serror != NULL)
477
schannel = ctxt->serror;
479
channel = ctxt->error;
480
data = ctxt->userData;
484
__xmlRaiseError(schannel, channel, data,
485
NULL, NULL, XML_FROM_RELAXNGV,
486
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
488
"Memory allocation failed : %s\n", extra);
490
__xmlRaiseError(schannel, channel, data,
491
NULL, NULL, XML_FROM_RELAXNGV,
492
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
493
NULL, NULL, 0, 0, "Memory allocation failed\n");
498
* @ctxt: a Relax-NG parser context
499
* @node: the node raising the error
500
* @error: the error code
505
* Handle a Relax NG Parsing error
508
xmlRngPErr(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node, int error,
509
const char *msg, const xmlChar * str1, const xmlChar * str2)
511
xmlStructuredErrorFunc schannel = NULL;
512
xmlGenericErrorFunc channel = NULL;
516
if (ctxt->serror != NULL)
517
schannel = ctxt->serror;
519
channel = ctxt->error;
520
data = ctxt->userData;
523
__xmlRaiseError(schannel, channel, data,
524
NULL, node, XML_FROM_RELAXNGP,
525
error, XML_ERR_ERROR, NULL, 0,
526
(const char *) str1, (const char *) str2, NULL, 0, 0,
532
* @ctxt: a Relax-NG validation context
533
* @node: the node raising the error
534
* @error: the error code
539
* Handle a Relax NG Validation error
542
xmlRngVErr(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node, int error,
543
const char *msg, const xmlChar * str1, const xmlChar * str2)
545
xmlStructuredErrorFunc schannel = NULL;
546
xmlGenericErrorFunc channel = NULL;
550
if (ctxt->serror != NULL)
551
schannel = ctxt->serror;
553
channel = ctxt->error;
554
data = ctxt->userData;
557
__xmlRaiseError(schannel, channel, data,
558
NULL, node, XML_FROM_RELAXNGV,
559
error, XML_ERR_ERROR, NULL, 0,
560
(const char *) str1, (const char *) str2, NULL, 0, 0,
564
/************************************************************************
566
* Preliminary type checking interfaces *
568
************************************************************************/
571
* xmlRelaxNGTypeHave:
572
* @data: data needed for the library
573
* @type: the type name
574
* @value: the value to check
576
* Function provided by a type library to check if a type is exported
578
* Returns 1 if yes, 0 if no and -1 in case of error.
580
typedef int (*xmlRelaxNGTypeHave) (void *data, const xmlChar * type);
583
* xmlRelaxNGTypeCheck:
584
* @data: data needed for the library
585
* @type: the type name
586
* @value: the value to check
587
* @result: place to store the result if needed
589
* Function provided by a type library to check if a value match a type
591
* Returns 1 if yes, 0 if no and -1 in case of error.
593
typedef int (*xmlRelaxNGTypeCheck) (void *data, const xmlChar * type,
594
const xmlChar * value, void **result,
598
* xmlRelaxNGFacetCheck:
599
* @data: data needed for the library
600
* @type: the type name
601
* @facet: the facet name
602
* @val: the facet value
603
* @strval: the string value
604
* @value: the value to check
606
* Function provided by a type library to check a value facet
608
* Returns 1 if yes, 0 if no and -1 in case of error.
610
typedef int (*xmlRelaxNGFacetCheck) (void *data, const xmlChar * type,
611
const xmlChar * facet,
613
const xmlChar * strval, void *value);
616
* xmlRelaxNGTypeFree:
617
* @data: data needed for the library
618
* @result: the value to free
620
* Function provided by a type library to free a returned result
622
typedef void (*xmlRelaxNGTypeFree) (void *data, void *result);
625
* xmlRelaxNGTypeCompare:
626
* @data: data needed for the library
627
* @type: the type name
628
* @value1: the first value
629
* @value2: the second value
631
* Function provided by a type library to compare two values accordingly
634
* Returns 1 if yes, 0 if no and -1 in case of error.
636
typedef int (*xmlRelaxNGTypeCompare) (void *data, const xmlChar * type,
637
const xmlChar * value1,
640
const xmlChar * value2,
642
typedef struct _xmlRelaxNGTypeLibrary xmlRelaxNGTypeLibrary;
643
typedef xmlRelaxNGTypeLibrary *xmlRelaxNGTypeLibraryPtr;
644
struct _xmlRelaxNGTypeLibrary {
645
const xmlChar *namespace; /* the datatypeLibrary value */
646
void *data; /* data needed for the library */
647
xmlRelaxNGTypeHave have; /* the export function */
648
xmlRelaxNGTypeCheck check; /* the checking function */
649
xmlRelaxNGTypeCompare comp; /* the compare function */
650
xmlRelaxNGFacetCheck facet; /* the facet check function */
651
xmlRelaxNGTypeFree freef; /* the freeing function */
654
/************************************************************************
656
* Allocation functions *
658
************************************************************************/
659
static void xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar);
660
static void xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define);
661
static void xmlRelaxNGNormExtSpace(xmlChar * value);
662
static void xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema);
663
static int xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt
665
xmlRelaxNGValidStatePtr state1,
666
xmlRelaxNGValidStatePtr state2);
667
static void xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
668
xmlRelaxNGValidStatePtr state);
671
* xmlRelaxNGFreeDocument:
672
* @docu: a document structure
674
* Deallocate a RelaxNG document structure.
677
xmlRelaxNGFreeDocument(xmlRelaxNGDocumentPtr docu)
682
if (docu->href != NULL)
684
if (docu->doc != NULL)
685
xmlFreeDoc(docu->doc);
686
if (docu->schema != NULL)
687
xmlRelaxNGFreeInnerSchema(docu->schema);
692
* xmlRelaxNGFreeDocumentList:
693
* @docu: a list of document structure
695
* Deallocate a RelaxNG document structures.
698
xmlRelaxNGFreeDocumentList(xmlRelaxNGDocumentPtr docu)
700
xmlRelaxNGDocumentPtr next;
702
while (docu != NULL) {
704
xmlRelaxNGFreeDocument(docu);
710
* xmlRelaxNGFreeInclude:
711
* @incl: a include structure
713
* Deallocate a RelaxNG include structure.
716
xmlRelaxNGFreeInclude(xmlRelaxNGIncludePtr incl)
721
if (incl->href != NULL)
723
if (incl->doc != NULL)
724
xmlFreeDoc(incl->doc);
725
if (incl->schema != NULL)
726
xmlRelaxNGFree(incl->schema);
731
* xmlRelaxNGFreeIncludeList:
732
* @incl: a include structure list
734
* Deallocate a RelaxNG include structure.
737
xmlRelaxNGFreeIncludeList(xmlRelaxNGIncludePtr incl)
739
xmlRelaxNGIncludePtr next;
741
while (incl != NULL) {
743
xmlRelaxNGFreeInclude(incl);
749
* xmlRelaxNGNewRelaxNG:
750
* @ctxt: a Relax-NG validation context (optional)
752
* Allocate a new RelaxNG structure.
754
* Returns the newly allocated structure or NULL in case or error
757
xmlRelaxNGNewRelaxNG(xmlRelaxNGParserCtxtPtr ctxt)
761
ret = (xmlRelaxNGPtr) xmlMalloc(sizeof(xmlRelaxNG));
763
xmlRngPErrMemory(ctxt, NULL);
766
memset(ret, 0, sizeof(xmlRelaxNG));
772
* xmlRelaxNGFreeInnerSchema:
773
* @schema: a schema structure
775
* Deallocate a RelaxNG schema structure.
778
xmlRelaxNGFreeInnerSchema(xmlRelaxNGPtr schema)
783
if (schema->doc != NULL)
784
xmlFreeDoc(schema->doc);
785
if (schema->defTab != NULL) {
788
for (i = 0; i < schema->defNr; i++)
789
xmlRelaxNGFreeDefine(schema->defTab[i]);
790
xmlFree(schema->defTab);
798
* @schema: a schema structure
800
* Deallocate a RelaxNG structure.
803
xmlRelaxNGFree(xmlRelaxNGPtr schema)
808
if (schema->topgrammar != NULL)
809
xmlRelaxNGFreeGrammar(schema->topgrammar);
810
if (schema->doc != NULL)
811
xmlFreeDoc(schema->doc);
812
if (schema->documents != NULL)
813
xmlRelaxNGFreeDocumentList(schema->documents);
814
if (schema->includes != NULL)
815
xmlRelaxNGFreeIncludeList(schema->includes);
816
if (schema->defTab != NULL) {
819
for (i = 0; i < schema->defNr; i++)
820
xmlRelaxNGFreeDefine(schema->defTab[i]);
821
xmlFree(schema->defTab);
828
* xmlRelaxNGNewGrammar:
829
* @ctxt: a Relax-NG validation context (optional)
831
* Allocate a new RelaxNG grammar.
833
* Returns the newly allocated structure or NULL in case or error
835
static xmlRelaxNGGrammarPtr
836
xmlRelaxNGNewGrammar(xmlRelaxNGParserCtxtPtr ctxt)
838
xmlRelaxNGGrammarPtr ret;
840
ret = (xmlRelaxNGGrammarPtr) xmlMalloc(sizeof(xmlRelaxNGGrammar));
842
xmlRngPErrMemory(ctxt, NULL);
845
memset(ret, 0, sizeof(xmlRelaxNGGrammar));
851
* xmlRelaxNGFreeGrammar:
852
* @grammar: a grammar structure
854
* Deallocate a RelaxNG grammar structure.
857
xmlRelaxNGFreeGrammar(xmlRelaxNGGrammarPtr grammar)
862
if (grammar->children != NULL) {
863
xmlRelaxNGFreeGrammar(grammar->children);
865
if (grammar->next != NULL) {
866
xmlRelaxNGFreeGrammar(grammar->next);
868
if (grammar->refs != NULL) {
869
xmlHashFree(grammar->refs, NULL);
871
if (grammar->defs != NULL) {
872
xmlHashFree(grammar->defs, NULL);
879
* xmlRelaxNGNewDefine:
880
* @ctxt: a Relax-NG validation context
881
* @node: the node in the input document.
883
* Allocate a new RelaxNG define.
885
* Returns the newly allocated structure or NULL in case or error
887
static xmlRelaxNGDefinePtr
888
xmlRelaxNGNewDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
890
xmlRelaxNGDefinePtr ret;
892
if (ctxt->defMax == 0) {
895
ctxt->defTab = (xmlRelaxNGDefinePtr *)
896
xmlMalloc(ctxt->defMax * sizeof(xmlRelaxNGDefinePtr));
897
if (ctxt->defTab == NULL) {
898
xmlRngPErrMemory(ctxt, "allocating define\n");
901
} else if (ctxt->defMax <= ctxt->defNr) {
902
xmlRelaxNGDefinePtr *tmp;
905
tmp = (xmlRelaxNGDefinePtr *) xmlRealloc(ctxt->defTab,
908
(xmlRelaxNGDefinePtr));
910
xmlRngPErrMemory(ctxt, "allocating define\n");
915
ret = (xmlRelaxNGDefinePtr) xmlMalloc(sizeof(xmlRelaxNGDefine));
917
xmlRngPErrMemory(ctxt, "allocating define\n");
920
memset(ret, 0, sizeof(xmlRelaxNGDefine));
921
ctxt->defTab[ctxt->defNr++] = ret;
928
* xmlRelaxNGFreePartition:
929
* @partitions: a partition set structure
931
* Deallocate RelaxNG partition set structures.
934
xmlRelaxNGFreePartition(xmlRelaxNGPartitionPtr partitions)
936
xmlRelaxNGInterleaveGroupPtr group;
939
if (partitions != NULL) {
940
if (partitions->groups != NULL) {
941
for (j = 0; j < partitions->nbgroups; j++) {
942
group = partitions->groups[j];
944
if (group->defs != NULL)
945
xmlFree(group->defs);
946
if (group->attrs != NULL)
947
xmlFree(group->attrs);
951
xmlFree(partitions->groups);
953
if (partitions->triage != NULL) {
954
xmlHashFree(partitions->triage, NULL);
961
* xmlRelaxNGFreeDefine:
962
* @define: a define structure
964
* Deallocate a RelaxNG define structure.
967
xmlRelaxNGFreeDefine(xmlRelaxNGDefinePtr define)
972
if ((define->type == XML_RELAXNG_VALUE) && (define->attrs != NULL)) {
973
xmlRelaxNGTypeLibraryPtr lib;
975
lib = (xmlRelaxNGTypeLibraryPtr) define->data;
976
if ((lib != NULL) && (lib->freef != NULL))
977
lib->freef(lib->data, (void *) define->attrs);
979
if ((define->data != NULL) && (define->type == XML_RELAXNG_INTERLEAVE))
980
xmlRelaxNGFreePartition((xmlRelaxNGPartitionPtr) define->data);
981
if ((define->data != NULL) && (define->type == XML_RELAXNG_CHOICE))
982
xmlHashFree((xmlHashTablePtr) define->data, NULL);
983
if (define->name != NULL)
984
xmlFree(define->name);
985
if (define->ns != NULL)
987
if (define->value != NULL)
988
xmlFree(define->value);
989
if (define->contModel != NULL)
990
xmlRegFreeRegexp(define->contModel);
995
* xmlRelaxNGNewStates:
996
* @ctxt: a Relax-NG validation context
997
* @size: the default size for the container
999
* Allocate a new RelaxNG validation state container
1001
* Returns the newly allocated structure or NULL in case or error
1003
static xmlRelaxNGStatesPtr
1004
xmlRelaxNGNewStates(xmlRelaxNGValidCtxtPtr ctxt, int size)
1006
xmlRelaxNGStatesPtr ret;
1008
if ((ctxt != NULL) &&
1009
(ctxt->freeState != NULL) && (ctxt->freeStatesNr > 0)) {
1010
ctxt->freeStatesNr--;
1011
ret = ctxt->freeStates[ctxt->freeStatesNr];
1018
ret = (xmlRelaxNGStatesPtr) xmlMalloc(sizeof(xmlRelaxNGStates) +
1021
sizeof(xmlRelaxNGValidStatePtr));
1023
xmlRngVErrMemory(ctxt, "allocating states\n");
1027
ret->maxState = size;
1028
ret->tabState = (xmlRelaxNGValidStatePtr *) xmlMalloc((size) *
1030
(xmlRelaxNGValidStatePtr));
1031
if (ret->tabState == NULL) {
1032
xmlRngVErrMemory(ctxt, "allocating states\n");
1040
* xmlRelaxNGAddStateUniq:
1041
* @ctxt: a Relax-NG validation context
1042
* @states: the states container
1043
* @state: the validation state
1045
* Add a RelaxNG validation state to the container without checking
1048
* Return 1 in case of success and 0 if this is a duplicate and -1 on error
1051
xmlRelaxNGAddStatesUniq(xmlRelaxNGValidCtxtPtr ctxt,
1052
xmlRelaxNGStatesPtr states,
1053
xmlRelaxNGValidStatePtr state)
1055
if (state == NULL) {
1058
if (states->nbState >= states->maxState) {
1059
xmlRelaxNGValidStatePtr *tmp;
1062
size = states->maxState * 2;
1063
tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1066
(xmlRelaxNGValidStatePtr));
1068
xmlRngVErrMemory(ctxt, "adding states\n");
1071
states->tabState = tmp;
1072
states->maxState = size;
1074
states->tabState[states->nbState++] = state;
1079
* xmlRelaxNGAddState:
1080
* @ctxt: a Relax-NG validation context
1081
* @states: the states container
1082
* @state: the validation state
1084
* Add a RelaxNG validation state to the container
1086
* Return 1 in case of success and 0 if this is a duplicate and -1 on error
1089
xmlRelaxNGAddStates(xmlRelaxNGValidCtxtPtr ctxt,
1090
xmlRelaxNGStatesPtr states,
1091
xmlRelaxNGValidStatePtr state)
1095
if (state == NULL) {
1098
if (states->nbState >= states->maxState) {
1099
xmlRelaxNGValidStatePtr *tmp;
1102
size = states->maxState * 2;
1103
tmp = (xmlRelaxNGValidStatePtr *) xmlRealloc(states->tabState,
1106
(xmlRelaxNGValidStatePtr));
1108
xmlRngVErrMemory(ctxt, "adding states\n");
1111
states->tabState = tmp;
1112
states->maxState = size;
1114
for (i = 0; i < states->nbState; i++) {
1115
if (xmlRelaxNGEqualValidState(ctxt, state, states->tabState[i])) {
1116
xmlRelaxNGFreeValidState(ctxt, state);
1120
states->tabState[states->nbState++] = state;
1125
* xmlRelaxNGFreeStates:
1126
* @ctxt: a Relax-NG validation context
1127
* @states: teh container
1129
* Free a RelaxNG validation state container
1132
xmlRelaxNGFreeStates(xmlRelaxNGValidCtxtPtr ctxt,
1133
xmlRelaxNGStatesPtr states)
1137
if ((ctxt != NULL) && (ctxt->freeStates == NULL)) {
1138
ctxt->freeStatesMax = 40;
1139
ctxt->freeStatesNr = 0;
1140
ctxt->freeStates = (xmlRelaxNGStatesPtr *)
1141
xmlMalloc(ctxt->freeStatesMax * sizeof(xmlRelaxNGStatesPtr));
1142
if (ctxt->freeStates == NULL) {
1143
xmlRngVErrMemory(ctxt, "storing states\n");
1145
} else if ((ctxt != NULL)
1146
&& (ctxt->freeStatesNr >= ctxt->freeStatesMax)) {
1147
xmlRelaxNGStatesPtr *tmp;
1149
tmp = (xmlRelaxNGStatesPtr *) xmlRealloc(ctxt->freeStates,
1150
2 * ctxt->freeStatesMax *
1152
(xmlRelaxNGStatesPtr));
1154
xmlRngVErrMemory(ctxt, "storing states\n");
1155
xmlFree(states->tabState);
1159
ctxt->freeStates = tmp;
1160
ctxt->freeStatesMax *= 2;
1162
if ((ctxt == NULL) || (ctxt->freeStates == NULL)) {
1163
xmlFree(states->tabState);
1166
ctxt->freeStates[ctxt->freeStatesNr++] = states;
1171
* xmlRelaxNGNewValidState:
1172
* @ctxt: a Relax-NG validation context
1173
* @node: the current node or NULL for the document
1175
* Allocate a new RelaxNG validation state
1177
* Returns the newly allocated structure or NULL in case or error
1179
static xmlRelaxNGValidStatePtr
1180
xmlRelaxNGNewValidState(xmlRelaxNGValidCtxtPtr ctxt, xmlNodePtr node)
1182
xmlRelaxNGValidStatePtr ret;
1184
xmlAttrPtr attrs[MAX_ATTR];
1186
xmlNodePtr root = NULL;
1189
root = xmlDocGetRootElement(ctxt->doc);
1193
attr = node->properties;
1194
while (attr != NULL) {
1195
if (nbAttrs < MAX_ATTR)
1196
attrs[nbAttrs++] = attr;
1202
if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1203
ctxt->freeState->nbState--;
1204
ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1207
(xmlRelaxNGValidStatePtr)
1208
xmlMalloc(sizeof(xmlRelaxNGValidState));
1210
xmlRngVErrMemory(ctxt, "allocating states\n");
1213
memset(ret, 0, sizeof(xmlRelaxNGValidState));
1216
ret->endvalue = NULL;
1218
ret->node = (xmlNodePtr) ctxt->doc;
1222
ret->seq = node->children;
1226
if (ret->attrs == NULL) {
1230
ret->maxAttrs = nbAttrs;
1231
ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1232
sizeof(xmlAttrPtr));
1233
if (ret->attrs == NULL) {
1234
xmlRngVErrMemory(ctxt, "allocating states\n");
1237
} else if (ret->maxAttrs < nbAttrs) {
1240
tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, nbAttrs *
1241
sizeof(xmlAttrPtr));
1243
xmlRngVErrMemory(ctxt, "allocating states\n");
1247
ret->maxAttrs = nbAttrs;
1249
ret->nbAttrs = nbAttrs;
1250
if (nbAttrs < MAX_ATTR) {
1251
memcpy(ret->attrs, attrs, sizeof(xmlAttrPtr) * nbAttrs);
1253
attr = node->properties;
1255
while (attr != NULL) {
1256
ret->attrs[nbAttrs++] = attr;
1261
ret->nbAttrLeft = ret->nbAttrs;
1266
* xmlRelaxNGCopyValidState:
1267
* @ctxt: a Relax-NG validation context
1268
* @state: a validation state
1270
* Copy the validation state
1272
* Returns the newly allocated structure or NULL in case or error
1274
static xmlRelaxNGValidStatePtr
1275
xmlRelaxNGCopyValidState(xmlRelaxNGValidCtxtPtr ctxt,
1276
xmlRelaxNGValidStatePtr state)
1278
xmlRelaxNGValidStatePtr ret;
1279
unsigned int maxAttrs;
1284
if ((ctxt->freeState != NULL) && (ctxt->freeState->nbState > 0)) {
1285
ctxt->freeState->nbState--;
1286
ret = ctxt->freeState->tabState[ctxt->freeState->nbState];
1289
(xmlRelaxNGValidStatePtr)
1290
xmlMalloc(sizeof(xmlRelaxNGValidState));
1292
xmlRngVErrMemory(ctxt, "allocating states\n");
1295
memset(ret, 0, sizeof(xmlRelaxNGValidState));
1298
maxAttrs = ret->maxAttrs;
1299
memcpy(ret, state, sizeof(xmlRelaxNGValidState));
1301
ret->maxAttrs = maxAttrs;
1302
if (state->nbAttrs > 0) {
1303
if (ret->attrs == NULL) {
1304
ret->maxAttrs = state->maxAttrs;
1305
ret->attrs = (xmlAttrPtr *) xmlMalloc(ret->maxAttrs *
1306
sizeof(xmlAttrPtr));
1307
if (ret->attrs == NULL) {
1308
xmlRngVErrMemory(ctxt, "allocating states\n");
1312
} else if (ret->maxAttrs < state->nbAttrs) {
1315
tmp = (xmlAttrPtr *) xmlRealloc(ret->attrs, state->maxAttrs *
1316
sizeof(xmlAttrPtr));
1318
xmlRngVErrMemory(ctxt, "allocating states\n");
1322
ret->maxAttrs = state->maxAttrs;
1325
memcpy(ret->attrs, state->attrs,
1326
state->nbAttrs * sizeof(xmlAttrPtr));
1332
* xmlRelaxNGEqualValidState:
1333
* @ctxt: a Relax-NG validation context
1334
* @state1: a validation state
1335
* @state2: a validation state
1337
* Compare the validation states for equality
1339
* Returns 1 if equald, 0 otherwise
1342
xmlRelaxNGEqualValidState(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
1343
xmlRelaxNGValidStatePtr state1,
1344
xmlRelaxNGValidStatePtr state2)
1348
if ((state1 == NULL) || (state2 == NULL))
1350
if (state1 == state2)
1352
if (state1->node != state2->node)
1354
if (state1->seq != state2->seq)
1356
if (state1->nbAttrLeft != state2->nbAttrLeft)
1358
if (state1->nbAttrs != state2->nbAttrs)
1360
if (state1->endvalue != state2->endvalue)
1362
if ((state1->value != state2->value) &&
1363
(!xmlStrEqual(state1->value, state2->value)))
1365
for (i = 0; i < state1->nbAttrs; i++) {
1366
if (state1->attrs[i] != state2->attrs[i])
1373
* xmlRelaxNGFreeValidState:
1374
* @state: a validation state structure
1376
* Deallocate a RelaxNG validation state structure.
1379
xmlRelaxNGFreeValidState(xmlRelaxNGValidCtxtPtr ctxt,
1380
xmlRelaxNGValidStatePtr state)
1385
if ((ctxt != NULL) && (ctxt->freeState == NULL)) {
1386
ctxt->freeState = xmlRelaxNGNewStates(ctxt, 40);
1388
if ((ctxt == NULL) || (ctxt->freeState == NULL)) {
1389
if (state->attrs != NULL)
1390
xmlFree(state->attrs);
1393
xmlRelaxNGAddStatesUniq(ctxt, ctxt->freeState, state);
1397
/************************************************************************
1399
* Semi internal functions *
1401
************************************************************************/
1404
* xmlRelaxParserSetFlag:
1405
* @ctxt: a RelaxNG parser context
1406
* @flags: a set of flags values
1408
* Semi private function used to pass informations to a parser context
1409
* which are a combination of xmlRelaxNGParserFlag .
1411
* Returns 0 if success and -1 in case of error
1414
xmlRelaxParserSetFlag(xmlRelaxNGParserCtxtPtr ctxt, int flags)
1416
if (ctxt == NULL) return(-1);
1417
if (flags & XML_RELAXNGP_FREE_DOC) {
1418
ctxt->crng |= XML_RELAXNGP_FREE_DOC;
1419
flags -= XML_RELAXNGP_FREE_DOC;
1421
if (flags & XML_RELAXNGP_CRNG) {
1422
ctxt->crng |= XML_RELAXNGP_CRNG;
1423
flags -= XML_RELAXNGP_CRNG;
1425
if (flags != 0) return(-1);
1429
/************************************************************************
1431
* Document functions *
1433
************************************************************************/
1434
static xmlDocPtr xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt,
1438
* xmlRelaxNGIncludePush:
1439
* @ctxt: the parser context
1440
* @value: the element doc
1442
* Pushes a new include on top of the include stack
1444
* Returns 0 in case of error, the index in the stack otherwise
1447
xmlRelaxNGIncludePush(xmlRelaxNGParserCtxtPtr ctxt,
1448
xmlRelaxNGIncludePtr value)
1450
if (ctxt->incTab == NULL) {
1454
(xmlRelaxNGIncludePtr *) xmlMalloc(ctxt->incMax *
1455
sizeof(ctxt->incTab[0]));
1456
if (ctxt->incTab == NULL) {
1457
xmlRngPErrMemory(ctxt, "allocating include\n");
1461
if (ctxt->incNr >= ctxt->incMax) {
1464
(xmlRelaxNGIncludePtr *) xmlRealloc(ctxt->incTab,
1466
sizeof(ctxt->incTab[0]));
1467
if (ctxt->incTab == NULL) {
1468
xmlRngPErrMemory(ctxt, "allocating include\n");
1472
ctxt->incTab[ctxt->incNr] = value;
1474
return (ctxt->incNr++);
1478
* xmlRelaxNGIncludePop:
1479
* @ctxt: the parser context
1481
* Pops the top include from the include stack
1483
* Returns the include just removed
1485
static xmlRelaxNGIncludePtr
1486
xmlRelaxNGIncludePop(xmlRelaxNGParserCtxtPtr ctxt)
1488
xmlRelaxNGIncludePtr ret;
1490
if (ctxt->incNr <= 0)
1493
if (ctxt->incNr > 0)
1494
ctxt->inc = ctxt->incTab[ctxt->incNr - 1];
1497
ret = ctxt->incTab[ctxt->incNr];
1498
ctxt->incTab[ctxt->incNr] = NULL;
1503
* xmlRelaxNGRemoveRedefine:
1504
* @ctxt: the parser context
1505
* @URL: the normalized URL
1506
* @target: the included target
1507
* @name: the define name to eliminate
1509
* Applies the elimination algorithm of 4.7
1511
* Returns 0 in case of error, 1 in case of success.
1514
xmlRelaxNGRemoveRedefine(xmlRelaxNGParserCtxtPtr ctxt,
1515
const xmlChar * URL ATTRIBUTE_UNUSED,
1516
xmlNodePtr target, const xmlChar * name)
1519
xmlNodePtr tmp, tmp2;
1522
#ifdef DEBUG_INCLUDE
1524
xmlGenericError(xmlGenericErrorContext,
1525
"Elimination of <include> start from %s\n", URL);
1527
xmlGenericError(xmlGenericErrorContext,
1528
"Elimination of <include> define %s from %s\n",
1532
while (tmp != NULL) {
1534
if ((name == NULL) && (IS_RELAXNG(tmp, "start"))) {
1538
} else if ((name != NULL) && (IS_RELAXNG(tmp, "define"))) {
1539
name2 = xmlGetProp(tmp, BAD_CAST "name");
1540
xmlRelaxNGNormExtSpace(name2);
1541
if (name2 != NULL) {
1542
if (xmlStrEqual(name, name2)) {
1549
} else if (IS_RELAXNG(tmp, "include")) {
1550
xmlChar *href = NULL;
1551
xmlRelaxNGDocumentPtr inc = tmp->psvi;
1553
if ((inc != NULL) && (inc->doc != NULL) &&
1554
(inc->doc->children != NULL)) {
1557
(inc->doc->children->name, BAD_CAST "grammar")) {
1558
#ifdef DEBUG_INCLUDE
1559
href = xmlGetProp(tmp, BAD_CAST "href");
1561
if (xmlRelaxNGRemoveRedefine(ctxt, href,
1562
inc->doc->children->
1563
children, name) == 1) {
1566
#ifdef DEBUG_INCLUDE
1579
* xmlRelaxNGLoadInclude:
1580
* @ctxt: the parser context
1581
* @URL: the normalized URL
1582
* @node: the include node.
1583
* @ns: the namespace passed from the context.
1585
* First lookup if the document is already loaded into the parser context,
1586
* check against recursion. If not found the resource is loaded and
1587
* the content is preprocessed before being returned back to the caller.
1589
* Returns the xmlRelaxNGIncludePtr or NULL in case of error
1591
static xmlRelaxNGIncludePtr
1592
xmlRelaxNGLoadInclude(xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * URL,
1593
xmlNodePtr node, const xmlChar * ns)
1595
xmlRelaxNGIncludePtr ret = NULL;
1598
xmlNodePtr root, cur;
1600
#ifdef DEBUG_INCLUDE
1601
xmlGenericError(xmlGenericErrorContext,
1602
"xmlRelaxNGLoadInclude(%s)\n", URL);
1606
* check against recursion in the stack
1608
for (i = 0; i < ctxt->incNr; i++) {
1609
if (xmlStrEqual(ctxt->incTab[i]->href, URL)) {
1610
xmlRngPErr(ctxt, NULL, XML_RNGP_INCLUDE_RECURSE,
1611
"Detected an Include recursion for %s\n", URL,
1620
doc = xmlReadFile((const char *) URL,NULL,0);
1622
xmlRngPErr(ctxt, node, XML_RNGP_PARSE_ERROR,
1623
"xmlRelaxNG: could not load %s\n", URL, NULL);
1626
#ifdef DEBUG_INCLUDE
1627
xmlGenericError(xmlGenericErrorContext, "Parsed %s Okay\n", URL);
1631
* Allocate the document structures and register it first.
1633
ret = (xmlRelaxNGIncludePtr) xmlMalloc(sizeof(xmlRelaxNGInclude));
1635
xmlRngPErrMemory(ctxt, "allocating include\n");
1639
memset(ret, 0, sizeof(xmlRelaxNGInclude));
1641
ret->href = xmlStrdup(URL);
1642
ret->next = ctxt->includes;
1643
ctxt->includes = ret;
1646
* transmit the ns if needed
1649
root = xmlDocGetRootElement(doc);
1651
if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1652
xmlSetProp(root, BAD_CAST "ns", ns);
1658
* push it on the stack
1660
xmlRelaxNGIncludePush(ctxt, ret);
1663
* Some preprocessing of the document content, this include recursing
1664
* in the include stack.
1666
#ifdef DEBUG_INCLUDE
1667
xmlGenericError(xmlGenericErrorContext, "cleanup of %s\n", URL);
1670
doc = xmlRelaxNGCleanupDoc(ctxt, doc);
1677
* Pop up the include from the stack
1679
xmlRelaxNGIncludePop(ctxt);
1681
#ifdef DEBUG_INCLUDE
1682
xmlGenericError(xmlGenericErrorContext, "Checking of %s\n", URL);
1685
* Check that the top element is a grammar
1687
root = xmlDocGetRootElement(doc);
1689
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY,
1690
"xmlRelaxNG: included document is empty %s\n", URL,
1694
if (!IS_RELAXNG(root, "grammar")) {
1695
xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
1696
"xmlRelaxNG: included document %s root is not a grammar\n",
1702
* Elimination of redefined rules in the include.
1704
cur = node->children;
1705
while (cur != NULL) {
1706
if (IS_RELAXNG(cur, "start")) {
1710
xmlRelaxNGRemoveRedefine(ctxt, URL, root->children, NULL);
1712
xmlRngPErr(ctxt, node, XML_RNGP_START_MISSING,
1713
"xmlRelaxNG: include %s has a start but not the included grammar\n",
1716
} else if (IS_RELAXNG(cur, "define")) {
1719
name = xmlGetProp(cur, BAD_CAST "name");
1721
xmlRngPErr(ctxt, node, XML_RNGP_NAME_MISSING,
1722
"xmlRelaxNG: include %s has define without name\n",
1727
xmlRelaxNGNormExtSpace(name);
1728
found = xmlRelaxNGRemoveRedefine(ctxt, URL,
1729
root->children, name);
1731
xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_MISSING,
1732
"xmlRelaxNG: include %s has a define %s but not the included grammar\n",
1746
* xmlRelaxNGValidErrorPush:
1747
* @ctxt: the validation context
1748
* @err: the error code
1749
* @arg1: the first string argument
1750
* @arg2: the second string argument
1751
* @dup: arg need to be duplicated
1753
* Pushes a new error on top of the error stack
1755
* Returns 0 in case of error, the index in the stack otherwise
1758
xmlRelaxNGValidErrorPush(xmlRelaxNGValidCtxtPtr ctxt,
1759
xmlRelaxNGValidErr err, const xmlChar * arg1,
1760
const xmlChar * arg2, int dup)
1762
xmlRelaxNGValidErrorPtr cur;
1765
xmlGenericError(xmlGenericErrorContext,
1766
"Pushing error %d at %d on stack\n", err, ctxt->errNr);
1768
if (ctxt->errTab == NULL) {
1772
(xmlRelaxNGValidErrorPtr) xmlMalloc(ctxt->errMax *
1774
(xmlRelaxNGValidError));
1775
if (ctxt->errTab == NULL) {
1776
xmlRngVErrMemory(ctxt, "pushing error\n");
1781
if (ctxt->errNr >= ctxt->errMax) {
1784
(xmlRelaxNGValidErrorPtr) xmlRealloc(ctxt->errTab,
1787
(xmlRelaxNGValidError));
1788
if (ctxt->errTab == NULL) {
1789
xmlRngVErrMemory(ctxt, "pushing error\n");
1792
ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1794
if ((ctxt->err != NULL) && (ctxt->state != NULL) &&
1795
(ctxt->err->node == ctxt->state->node) && (ctxt->err->err == err))
1796
return (ctxt->errNr);
1797
cur = &ctxt->errTab[ctxt->errNr];
1800
cur->arg1 = xmlStrdup(arg1);
1801
cur->arg2 = xmlStrdup(arg2);
1802
cur->flags = ERROR_IS_DUP;
1808
if (ctxt->state != NULL) {
1809
cur->node = ctxt->state->node;
1810
cur->seq = ctxt->state->seq;
1816
return (ctxt->errNr++);
1820
* xmlRelaxNGValidErrorPop:
1821
* @ctxt: the validation context
1823
* Pops the top error from the error stack
1826
xmlRelaxNGValidErrorPop(xmlRelaxNGValidCtxtPtr ctxt)
1828
xmlRelaxNGValidErrorPtr cur;
1830
if (ctxt->errNr <= 0) {
1835
if (ctxt->errNr > 0)
1836
ctxt->err = &ctxt->errTab[ctxt->errNr - 1];
1839
cur = &ctxt->errTab[ctxt->errNr];
1840
if (cur->flags & ERROR_IS_DUP) {
1841
if (cur->arg1 != NULL)
1842
xmlFree((xmlChar *) cur->arg1);
1844
if (cur->arg2 != NULL)
1845
xmlFree((xmlChar *) cur->arg2);
1852
* xmlRelaxNGDocumentPush:
1853
* @ctxt: the parser context
1854
* @value: the element doc
1856
* Pushes a new doc on top of the doc stack
1858
* Returns 0 in case of error, the index in the stack otherwise
1861
xmlRelaxNGDocumentPush(xmlRelaxNGParserCtxtPtr ctxt,
1862
xmlRelaxNGDocumentPtr value)
1864
if (ctxt->docTab == NULL) {
1868
(xmlRelaxNGDocumentPtr *) xmlMalloc(ctxt->docMax *
1869
sizeof(ctxt->docTab[0]));
1870
if (ctxt->docTab == NULL) {
1871
xmlRngPErrMemory(ctxt, "adding document\n");
1875
if (ctxt->docNr >= ctxt->docMax) {
1878
(xmlRelaxNGDocumentPtr *) xmlRealloc(ctxt->docTab,
1880
sizeof(ctxt->docTab[0]));
1881
if (ctxt->docTab == NULL) {
1882
xmlRngPErrMemory(ctxt, "adding document\n");
1886
ctxt->docTab[ctxt->docNr] = value;
1888
return (ctxt->docNr++);
1892
* xmlRelaxNGDocumentPop:
1893
* @ctxt: the parser context
1895
* Pops the top doc from the doc stack
1897
* Returns the doc just removed
1899
static xmlRelaxNGDocumentPtr
1900
xmlRelaxNGDocumentPop(xmlRelaxNGParserCtxtPtr ctxt)
1902
xmlRelaxNGDocumentPtr ret;
1904
if (ctxt->docNr <= 0)
1907
if (ctxt->docNr > 0)
1908
ctxt->doc = ctxt->docTab[ctxt->docNr - 1];
1911
ret = ctxt->docTab[ctxt->docNr];
1912
ctxt->docTab[ctxt->docNr] = NULL;
1917
* xmlRelaxNGLoadExternalRef:
1918
* @ctxt: the parser context
1919
* @URL: the normalized URL
1920
* @ns: the inherited ns if any
1922
* First lookup if the document is already loaded into the parser context,
1923
* check against recursion. If not found the resource is loaded and
1924
* the content is preprocessed before being returned back to the caller.
1926
* Returns the xmlRelaxNGDocumentPtr or NULL in case of error
1928
static xmlRelaxNGDocumentPtr
1929
xmlRelaxNGLoadExternalRef(xmlRelaxNGParserCtxtPtr ctxt,
1930
const xmlChar * URL, const xmlChar * ns)
1932
xmlRelaxNGDocumentPtr ret = NULL;
1938
* check against recursion in the stack
1940
for (i = 0; i < ctxt->docNr; i++) {
1941
if (xmlStrEqual(ctxt->docTab[i]->href, URL)) {
1942
xmlRngPErr(ctxt, NULL, XML_RNGP_EXTERNALREF_RECURSE,
1943
"Detected an externalRef recursion for %s\n", URL,
1952
doc = xmlReadFile((const char *) URL,NULL,0);
1954
xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
1955
"xmlRelaxNG: could not load %s\n", URL, NULL);
1960
* Allocate the document structures and register it first.
1962
ret = (xmlRelaxNGDocumentPtr) xmlMalloc(sizeof(xmlRelaxNGDocument));
1964
xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_ERR_NO_MEMORY,
1965
"xmlRelaxNG: allocate memory for doc %s\n", URL, NULL);
1969
memset(ret, 0, sizeof(xmlRelaxNGDocument));
1971
ret->href = xmlStrdup(URL);
1972
ret->next = ctxt->documents;
1973
ctxt->documents = ret;
1976
* transmit the ns if needed
1979
root = xmlDocGetRootElement(doc);
1981
if (xmlHasProp(root, BAD_CAST "ns") == NULL) {
1982
xmlSetProp(root, BAD_CAST "ns", ns);
1988
* push it on the stack and register it in the hash table
1990
xmlRelaxNGDocumentPush(ctxt, ret);
1993
* Some preprocessing of the document content
1995
doc = xmlRelaxNGCleanupDoc(ctxt, doc);
2001
xmlRelaxNGDocumentPop(ctxt);
2006
/************************************************************************
2010
************************************************************************/
2012
#define VALID_ERR(a) xmlRelaxNGAddValidError(ctxt, a, NULL, NULL, 0);
2013
#define VALID_ERR2(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 0);
2014
#define VALID_ERR3(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 0);
2015
#define VALID_ERR2P(a, b) xmlRelaxNGAddValidError(ctxt, a, b, NULL, 1);
2016
#define VALID_ERR3P(a, b, c) xmlRelaxNGAddValidError(ctxt, a, b, c, 1);
2019
xmlRelaxNGDefName(xmlRelaxNGDefinePtr def)
2023
switch (def->type) {
2024
case XML_RELAXNG_EMPTY:
2026
case XML_RELAXNG_NOT_ALLOWED:
2027
return ("notAllowed");
2028
case XML_RELAXNG_EXCEPT:
2030
case XML_RELAXNG_TEXT:
2032
case XML_RELAXNG_ELEMENT:
2034
case XML_RELAXNG_DATATYPE:
2035
return ("datatype");
2036
case XML_RELAXNG_VALUE:
2038
case XML_RELAXNG_LIST:
2040
case XML_RELAXNG_ATTRIBUTE:
2041
return ("attribute");
2042
case XML_RELAXNG_DEF:
2044
case XML_RELAXNG_REF:
2046
case XML_RELAXNG_EXTERNALREF:
2047
return ("externalRef");
2048
case XML_RELAXNG_PARENTREF:
2049
return ("parentRef");
2050
case XML_RELAXNG_OPTIONAL:
2051
return ("optional");
2052
case XML_RELAXNG_ZEROORMORE:
2053
return ("zeroOrMore");
2054
case XML_RELAXNG_ONEORMORE:
2055
return ("oneOrMore");
2056
case XML_RELAXNG_CHOICE:
2058
case XML_RELAXNG_GROUP:
2060
case XML_RELAXNG_INTERLEAVE:
2061
return ("interleave");
2062
case XML_RELAXNG_START:
2064
case XML_RELAXNG_NOOP:
2066
case XML_RELAXNG_PARAM:
2073
* xmlRelaxNGGetErrorString:
2074
* @err: the error code
2075
* @arg1: the first string argument
2076
* @arg2: the second string argument
2078
* computes a formatted error string for the given error code and args
2080
* Returns the error string, it must be deallocated by the caller
2083
xmlRelaxNGGetErrorString(xmlRelaxNGValidErr err, const xmlChar * arg1,
2084
const xmlChar * arg2)
2095
case XML_RELAXNG_OK:
2097
case XML_RELAXNG_ERR_MEMORY:
2098
return (xmlCharStrdup("out of memory\n"));
2099
case XML_RELAXNG_ERR_TYPE:
2100
snprintf(msg, 1000, "failed to validate type %s\n", arg1);
2102
case XML_RELAXNG_ERR_TYPEVAL:
2103
snprintf(msg, 1000, "Type %s doesn't allow value '%s'\n", arg1,
2106
case XML_RELAXNG_ERR_DUPID:
2107
snprintf(msg, 1000, "ID %s redefined\n", arg1);
2109
case XML_RELAXNG_ERR_TYPECMP:
2110
snprintf(msg, 1000, "failed to compare type %s\n", arg1);
2112
case XML_RELAXNG_ERR_NOSTATE:
2113
return (xmlCharStrdup("Internal error: no state\n"));
2114
case XML_RELAXNG_ERR_NODEFINE:
2115
return (xmlCharStrdup("Internal error: no define\n"));
2116
case XML_RELAXNG_ERR_INTERNAL:
2117
snprintf(msg, 1000, "Internal error: %s\n", arg1);
2119
case XML_RELAXNG_ERR_LISTEXTRA:
2120
snprintf(msg, 1000, "Extra data in list: %s\n", arg1);
2122
case XML_RELAXNG_ERR_INTERNODATA:
2123
return (xmlCharStrdup
2124
("Internal: interleave block has no data\n"));
2125
case XML_RELAXNG_ERR_INTERSEQ:
2126
return (xmlCharStrdup("Invalid sequence in interleave\n"));
2127
case XML_RELAXNG_ERR_INTEREXTRA:
2128
snprintf(msg, 1000, "Extra element %s in interleave\n", arg1);
2130
case XML_RELAXNG_ERR_ELEMNAME:
2131
snprintf(msg, 1000, "Expecting element %s, got %s\n", arg1,
2134
case XML_RELAXNG_ERR_ELEMNONS:
2135
snprintf(msg, 1000, "Expecting a namespace for element %s\n",
2138
case XML_RELAXNG_ERR_ELEMWRONGNS:
2140
"Element %s has wrong namespace: expecting %s\n", arg1,
2143
case XML_RELAXNG_ERR_ELEMWRONG:
2144
snprintf(msg, 1000, "Did not expect element %s there\n", arg1);
2146
case XML_RELAXNG_ERR_TEXTWRONG:
2148
"Did not expect text in element %s content\n", arg1);
2150
case XML_RELAXNG_ERR_ELEMEXTRANS:
2151
snprintf(msg, 1000, "Expecting no namespace for element %s\n",
2154
case XML_RELAXNG_ERR_ELEMNOTEMPTY:
2155
snprintf(msg, 1000, "Expecting element %s to be empty\n", arg1);
2157
case XML_RELAXNG_ERR_NOELEM:
2158
snprintf(msg, 1000, "Expecting an element %s, got nothing\n",
2161
case XML_RELAXNG_ERR_NOTELEM:
2162
return (xmlCharStrdup("Expecting an element got text\n"));
2163
case XML_RELAXNG_ERR_ATTRVALID:
2164
snprintf(msg, 1000, "Element %s failed to validate attributes\n",
2167
case XML_RELAXNG_ERR_CONTENTVALID:
2168
snprintf(msg, 1000, "Element %s failed to validate content\n",
2171
case XML_RELAXNG_ERR_EXTRACONTENT:
2172
snprintf(msg, 1000, "Element %s has extra content: %s\n",
2175
case XML_RELAXNG_ERR_INVALIDATTR:
2176
snprintf(msg, 1000, "Invalid attribute %s for element %s\n",
2179
case XML_RELAXNG_ERR_LACKDATA:
2180
snprintf(msg, 1000, "Datatype element %s contains no data\n",
2183
case XML_RELAXNG_ERR_DATAELEM:
2184
snprintf(msg, 1000, "Datatype element %s has child elements\n",
2187
case XML_RELAXNG_ERR_VALELEM:
2188
snprintf(msg, 1000, "Value element %s has child elements\n",
2191
case XML_RELAXNG_ERR_LISTELEM:
2192
snprintf(msg, 1000, "List element %s has child elements\n",
2195
case XML_RELAXNG_ERR_DATATYPE:
2196
snprintf(msg, 1000, "Error validating datatype %s\n", arg1);
2198
case XML_RELAXNG_ERR_VALUE:
2199
snprintf(msg, 1000, "Error validating value %s\n", arg1);
2201
case XML_RELAXNG_ERR_LIST:
2202
return (xmlCharStrdup("Error validating list\n"));
2203
case XML_RELAXNG_ERR_NOGRAMMAR:
2204
return (xmlCharStrdup("No top grammar defined\n"));
2205
case XML_RELAXNG_ERR_EXTRADATA:
2206
return (xmlCharStrdup("Extra data in the document\n"));
2208
return (xmlCharStrdup("Unknown error !\n"));
2211
snprintf(msg, 1000, "Unknown error code %d\n", err);
2214
return (xmlStrdup((xmlChar *) msg));
2218
* xmlRelaxNGShowValidError:
2219
* @ctxt: the validation context
2220
* @err: the error number
2222
* @child: the node child generating the problem.
2223
* @arg1: the first argument
2224
* @arg2: the second argument
2226
* Show a validation error.
2229
xmlRelaxNGShowValidError(xmlRelaxNGValidCtxtPtr ctxt,
2230
xmlRelaxNGValidErr err, xmlNodePtr node,
2231
xmlNodePtr child, const xmlChar * arg1,
2232
const xmlChar * arg2)
2236
if (ctxt->flags & FLAGS_NOERROR)
2240
xmlGenericError(xmlGenericErrorContext, "Show error %d\n", err);
2242
msg = xmlRelaxNGGetErrorString(err, arg1, arg2);
2246
if (ctxt->errNo == XML_RELAXNG_OK)
2248
xmlRngVErr(ctxt, (child == NULL ? node : child), err,
2249
(const char *) msg, arg1, arg2);
2254
* xmlRelaxNGPopErrors:
2255
* @ctxt: the validation context
2256
* @level: the error level in the stack
2258
* pop and discard all errors until the given level is reached
2261
xmlRelaxNGPopErrors(xmlRelaxNGValidCtxtPtr ctxt, int level)
2264
xmlRelaxNGValidErrorPtr err;
2267
xmlGenericError(xmlGenericErrorContext,
2268
"Pop errors till level %d\n", level);
2270
for (i = level; i < ctxt->errNr; i++) {
2271
err = &ctxt->errTab[i];
2272
if (err->flags & ERROR_IS_DUP) {
2273
if (err->arg1 != NULL)
2274
xmlFree((xmlChar *) err->arg1);
2276
if (err->arg2 != NULL)
2277
xmlFree((xmlChar *) err->arg2);
2282
ctxt->errNr = level;
2283
if (ctxt->errNr <= 0)
2288
* xmlRelaxNGDumpValidError:
2289
* @ctxt: the validation context
2291
* Show all validation error over a given index.
2294
xmlRelaxNGDumpValidError(xmlRelaxNGValidCtxtPtr ctxt)
2297
xmlRelaxNGValidErrorPtr err, dup;
2300
xmlGenericError(xmlGenericErrorContext,
2301
"Dumping error stack %d errors\n", ctxt->errNr);
2303
for (i = 0, k = 0; i < ctxt->errNr; i++) {
2304
err = &ctxt->errTab[i];
2305
if (k < MAX_ERROR) {
2306
for (j = 0; j < i; j++) {
2307
dup = &ctxt->errTab[j];
2308
if ((err->err == dup->err) && (err->node == dup->node) &&
2309
(xmlStrEqual(err->arg1, dup->arg1)) &&
2310
(xmlStrEqual(err->arg2, dup->arg2))) {
2314
xmlRelaxNGShowValidError(ctxt, err->err, err->node, err->seq,
2315
err->arg1, err->arg2);
2319
if (err->flags & ERROR_IS_DUP) {
2320
if (err->arg1 != NULL)
2321
xmlFree((xmlChar *) err->arg1);
2323
if (err->arg2 != NULL)
2324
xmlFree((xmlChar *) err->arg2);
2333
* xmlRelaxNGAddValidError:
2334
* @ctxt: the validation context
2335
* @err: the error number
2336
* @arg1: the first argument
2337
* @arg2: the second argument
2338
* @dup: need to dup the args
2340
* Register a validation error, either generating it if it's sure
2341
* or stacking it for later handling if unsure.
2344
xmlRelaxNGAddValidError(xmlRelaxNGValidCtxtPtr ctxt,
2345
xmlRelaxNGValidErr err, const xmlChar * arg1,
2346
const xmlChar * arg2, int dup)
2350
if (ctxt->flags & FLAGS_NOERROR)
2354
xmlGenericError(xmlGenericErrorContext, "Adding error %d\n", err);
2357
* generate the error directly
2359
if (((ctxt->flags & FLAGS_IGNORABLE) == 0) ||
2360
(ctxt->flags & FLAGS_NEGATIVE)) {
2361
xmlNodePtr node, seq;
2364
* Flush first any stacked error which might be the
2365
* real cause of the problem.
2367
if (ctxt->errNr != 0)
2368
xmlRelaxNGDumpValidError(ctxt);
2369
if (ctxt->state != NULL) {
2370
node = ctxt->state->node;
2371
seq = ctxt->state->seq;
2375
xmlRelaxNGShowValidError(ctxt, err, node, seq, arg1, arg2);
2378
* Stack the error for later processing if needed
2381
xmlRelaxNGValidErrorPush(ctxt, err, arg1, arg2, dup);
2386
/************************************************************************
2388
* Type library hooks *
2390
************************************************************************/
2391
static xmlChar *xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt,
2392
const xmlChar * str);
2395
* xmlRelaxNGSchemaTypeHave:
2396
* @data: data needed for the library
2397
* @type: the type name
2399
* Check if the given type is provided by
2400
* the W3C XMLSchema Datatype library.
2402
* Returns 1 if yes, 0 if no and -1 in case of error.
2405
xmlRelaxNGSchemaTypeHave(void *data ATTRIBUTE_UNUSED, const xmlChar * type)
2407
xmlSchemaTypePtr typ;
2411
typ = xmlSchemaGetPredefinedType(type,
2413
"http://www.w3.org/2001/XMLSchema");
2420
* xmlRelaxNGSchemaTypeCheck:
2421
* @data: data needed for the library
2422
* @type: the type name
2423
* @value: the value to check
2426
* Check if the given type and value are validated by
2427
* the W3C XMLSchema Datatype library.
2429
* Returns 1 if yes, 0 if no and -1 in case of error.
2432
xmlRelaxNGSchemaTypeCheck(void *data ATTRIBUTE_UNUSED,
2433
const xmlChar * type,
2434
const xmlChar * value,
2435
void **result, xmlNodePtr node)
2437
xmlSchemaTypePtr typ;
2440
if ((type == NULL) || (value == NULL))
2442
typ = xmlSchemaGetPredefinedType(type,
2444
"http://www.w3.org/2001/XMLSchema");
2447
ret = xmlSchemaValPredefTypeNode(typ, value,
2448
(xmlSchemaValPtr *) result, node);
2449
if (ret == 2) /* special ID error code */
2459
* xmlRelaxNGSchemaFacetCheck:
2460
* @data: data needed for the library
2461
* @type: the type name
2462
* @facet: the facet name
2463
* @val: the facet value
2464
* @strval: the string value
2465
* @value: the value to check
2467
* Function provided by a type library to check a value facet
2469
* Returns 1 if yes, 0 if no and -1 in case of error.
2472
xmlRelaxNGSchemaFacetCheck(void *data ATTRIBUTE_UNUSED,
2473
const xmlChar * type, const xmlChar * facetname,
2474
const xmlChar * val, const xmlChar * strval,
2477
xmlSchemaFacetPtr facet;
2478
xmlSchemaTypePtr typ;
2481
if ((type == NULL) || (strval == NULL))
2483
typ = xmlSchemaGetPredefinedType(type,
2485
"http://www.w3.org/2001/XMLSchema");
2489
facet = xmlSchemaNewFacet();
2493
if (xmlStrEqual(facetname, BAD_CAST "minInclusive")) {
2494
facet->type = XML_SCHEMA_FACET_MININCLUSIVE;
2495
} else if (xmlStrEqual(facetname, BAD_CAST "minExclusive")) {
2496
facet->type = XML_SCHEMA_FACET_MINEXCLUSIVE;
2497
} else if (xmlStrEqual(facetname, BAD_CAST "maxInclusive")) {
2498
facet->type = XML_SCHEMA_FACET_MAXINCLUSIVE;
2499
} else if (xmlStrEqual(facetname, BAD_CAST "maxExclusive")) {
2500
facet->type = XML_SCHEMA_FACET_MAXEXCLUSIVE;
2501
} else if (xmlStrEqual(facetname, BAD_CAST "totalDigits")) {
2502
facet->type = XML_SCHEMA_FACET_TOTALDIGITS;
2503
} else if (xmlStrEqual(facetname, BAD_CAST "fractionDigits")) {
2504
facet->type = XML_SCHEMA_FACET_FRACTIONDIGITS;
2505
} else if (xmlStrEqual(facetname, BAD_CAST "pattern")) {
2506
facet->type = XML_SCHEMA_FACET_PATTERN;
2507
} else if (xmlStrEqual(facetname, BAD_CAST "enumeration")) {
2508
facet->type = XML_SCHEMA_FACET_ENUMERATION;
2509
} else if (xmlStrEqual(facetname, BAD_CAST "whiteSpace")) {
2510
facet->type = XML_SCHEMA_FACET_WHITESPACE;
2511
} else if (xmlStrEqual(facetname, BAD_CAST "length")) {
2512
facet->type = XML_SCHEMA_FACET_LENGTH;
2513
} else if (xmlStrEqual(facetname, BAD_CAST "maxLength")) {
2514
facet->type = XML_SCHEMA_FACET_MAXLENGTH;
2515
} else if (xmlStrEqual(facetname, BAD_CAST "minLength")) {
2516
facet->type = XML_SCHEMA_FACET_MINLENGTH;
2518
xmlSchemaFreeFacet(facet);
2522
ret = xmlSchemaCheckFacet(facet, typ, NULL, type);
2524
xmlSchemaFreeFacet(facet);
2527
ret = xmlSchemaValidateFacet(typ, facet, strval, value);
2528
xmlSchemaFreeFacet(facet);
2535
* xmlRelaxNGSchemaFreeValue:
2536
* @data: data needed for the library
2537
* @value: the value to free
2539
* Function provided by a type library to free a Schemas value
2541
* Returns 1 if yes, 0 if no and -1 in case of error.
2544
xmlRelaxNGSchemaFreeValue(void *data ATTRIBUTE_UNUSED, void *value)
2546
xmlSchemaFreeValue(value);
2550
* xmlRelaxNGSchemaTypeCompare:
2551
* @data: data needed for the library
2552
* @type: the type name
2553
* @value1: the first value
2554
* @value2: the second value
2556
* Compare two values for equality accordingly a type from the W3C XMLSchema
2559
* Returns 1 if equal, 0 if no and -1 in case of error.
2562
xmlRelaxNGSchemaTypeCompare(void *data ATTRIBUTE_UNUSED,
2563
const xmlChar * type,
2564
const xmlChar * value1,
2567
const xmlChar * value2, xmlNodePtr ctxt2)
2570
xmlSchemaTypePtr typ;
2571
xmlSchemaValPtr res1 = NULL, res2 = NULL;
2573
if ((type == NULL) || (value1 == NULL) || (value2 == NULL))
2575
typ = xmlSchemaGetPredefinedType(type,
2577
"http://www.w3.org/2001/XMLSchema");
2580
if (comp1 == NULL) {
2581
ret = xmlSchemaValPredefTypeNode(typ, value1, &res1, ctxt1);
2587
res1 = (xmlSchemaValPtr) comp1;
2589
ret = xmlSchemaValPredefTypeNode(typ, value2, &res2, ctxt2);
2591
if ((comp1 == NULL) && (res1 != NULL))
2592
xmlSchemaFreeValue(res1);
2598
ret = xmlSchemaCompareValues(res1, res2);
2599
if (res1 != (xmlSchemaValPtr) comp1)
2600
xmlSchemaFreeValue(res1);
2601
xmlSchemaFreeValue(res2);
2610
* xmlRelaxNGDefaultTypeHave:
2611
* @data: data needed for the library
2612
* @type: the type name
2614
* Check if the given type is provided by
2615
* the default datatype library.
2617
* Returns 1 if yes, 0 if no and -1 in case of error.
2620
xmlRelaxNGDefaultTypeHave(void *data ATTRIBUTE_UNUSED,
2621
const xmlChar * type)
2625
if (xmlStrEqual(type, BAD_CAST "string"))
2627
if (xmlStrEqual(type, BAD_CAST "token"))
2633
* xmlRelaxNGDefaultTypeCheck:
2634
* @data: data needed for the library
2635
* @type: the type name
2636
* @value: the value to check
2639
* Check if the given type and value are validated by
2640
* the default datatype library.
2642
* Returns 1 if yes, 0 if no and -1 in case of error.
2645
xmlRelaxNGDefaultTypeCheck(void *data ATTRIBUTE_UNUSED,
2646
const xmlChar * type ATTRIBUTE_UNUSED,
2647
const xmlChar * value ATTRIBUTE_UNUSED,
2648
void **result ATTRIBUTE_UNUSED,
2649
xmlNodePtr node ATTRIBUTE_UNUSED)
2653
if (xmlStrEqual(type, BAD_CAST "string"))
2655
if (xmlStrEqual(type, BAD_CAST "token")) {
2663
* xmlRelaxNGDefaultTypeCompare:
2664
* @data: data needed for the library
2665
* @type: the type name
2666
* @value1: the first value
2667
* @value2: the second value
2669
* Compare two values accordingly a type from the default
2672
* Returns 1 if yes, 0 if no and -1 in case of error.
2675
xmlRelaxNGDefaultTypeCompare(void *data ATTRIBUTE_UNUSED,
2676
const xmlChar * type,
2677
const xmlChar * value1,
2678
xmlNodePtr ctxt1 ATTRIBUTE_UNUSED,
2679
void *comp1 ATTRIBUTE_UNUSED,
2680
const xmlChar * value2,
2681
xmlNodePtr ctxt2 ATTRIBUTE_UNUSED)
2685
if (xmlStrEqual(type, BAD_CAST "string")) {
2686
ret = xmlStrEqual(value1, value2);
2687
} else if (xmlStrEqual(type, BAD_CAST "token")) {
2688
if (!xmlStrEqual(value1, value2)) {
2689
xmlChar *nval, *nvalue;
2692
* TODO: trivial optimizations are possible by
2693
* computing at compile-time
2695
nval = xmlRelaxNGNormalize(NULL, value1);
2696
nvalue = xmlRelaxNGNormalize(NULL, value2);
2698
if ((nval == NULL) || (nvalue == NULL))
2700
else if (xmlStrEqual(nval, nvalue))
2714
static int xmlRelaxNGTypeInitialized = 0;
2715
static xmlHashTablePtr xmlRelaxNGRegisteredTypes = NULL;
2718
* xmlRelaxNGFreeTypeLibrary:
2719
* @lib: the type library structure
2720
* @namespace: the URI bound to the library
2722
* Free the structure associated to the type library
2725
xmlRelaxNGFreeTypeLibrary(xmlRelaxNGTypeLibraryPtr lib,
2726
const xmlChar * namespace ATTRIBUTE_UNUSED)
2730
if (lib->namespace != NULL)
2731
xmlFree((xmlChar *) lib->namespace);
2736
* xmlRelaxNGRegisterTypeLibrary:
2737
* @namespace: the URI bound to the library
2738
* @data: data associated to the library
2739
* @have: the provide function
2740
* @check: the checking function
2741
* @comp: the comparison function
2743
* Register a new type library
2745
* Returns 0 in case of success and -1 in case of error.
2748
xmlRelaxNGRegisterTypeLibrary(const xmlChar * namespace, void *data,
2749
xmlRelaxNGTypeHave have,
2750
xmlRelaxNGTypeCheck check,
2751
xmlRelaxNGTypeCompare comp,
2752
xmlRelaxNGFacetCheck facet,
2753
xmlRelaxNGTypeFree freef)
2755
xmlRelaxNGTypeLibraryPtr lib;
2758
if ((xmlRelaxNGRegisteredTypes == NULL) || (namespace == NULL) ||
2759
(check == NULL) || (comp == NULL))
2761
if (xmlHashLookup(xmlRelaxNGRegisteredTypes, namespace) != NULL) {
2762
xmlGenericError(xmlGenericErrorContext,
2763
"Relax-NG types library '%s' already registered\n",
2768
(xmlRelaxNGTypeLibraryPtr)
2769
xmlMalloc(sizeof(xmlRelaxNGTypeLibrary));
2771
xmlRngVErrMemory(NULL, "adding types library\n");
2774
memset(lib, 0, sizeof(xmlRelaxNGTypeLibrary));
2775
lib->namespace = xmlStrdup(namespace);
2782
ret = xmlHashAddEntry(xmlRelaxNGRegisteredTypes, namespace, lib);
2784
xmlGenericError(xmlGenericErrorContext,
2785
"Relax-NG types library failed to register '%s'\n",
2787
xmlRelaxNGFreeTypeLibrary(lib, namespace);
2794
* xmlRelaxNGInitTypes:
2796
* Initilize the default type libraries.
2798
* Returns 0 in case of success and -1 in case of error.
2801
xmlRelaxNGInitTypes(void)
2803
if (xmlRelaxNGTypeInitialized != 0)
2805
xmlRelaxNGRegisteredTypes = xmlHashCreate(10);
2806
if (xmlRelaxNGRegisteredTypes == NULL) {
2807
xmlGenericError(xmlGenericErrorContext,
2808
"Failed to allocate sh table for Relax-NG types\n");
2811
xmlRelaxNGRegisterTypeLibrary(BAD_CAST
2812
"http://www.w3.org/2001/XMLSchema-datatypes",
2813
NULL, xmlRelaxNGSchemaTypeHave,
2814
xmlRelaxNGSchemaTypeCheck,
2815
xmlRelaxNGSchemaTypeCompare,
2816
xmlRelaxNGSchemaFacetCheck,
2817
xmlRelaxNGSchemaFreeValue);
2818
xmlRelaxNGRegisterTypeLibrary(xmlRelaxNGNs, NULL,
2819
xmlRelaxNGDefaultTypeHave,
2820
xmlRelaxNGDefaultTypeCheck,
2821
xmlRelaxNGDefaultTypeCompare, NULL,
2823
xmlRelaxNGTypeInitialized = 1;
2828
* xmlRelaxNGCleanupTypes:
2830
* Cleanup the default Schemas type library associated to RelaxNG
2833
xmlRelaxNGCleanupTypes(void)
2835
xmlSchemaCleanupTypes();
2836
if (xmlRelaxNGTypeInitialized == 0)
2838
xmlHashFree(xmlRelaxNGRegisteredTypes, (xmlHashDeallocator)
2839
xmlRelaxNGFreeTypeLibrary);
2840
xmlRelaxNGTypeInitialized = 0;
2843
/************************************************************************
2845
* Compiling element content into regexp *
2847
* Sometime the element content can be compiled into a pure regexp, *
2848
* This allows a faster execution and streamability at that level *
2850
************************************************************************/
2852
static int xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt,
2853
xmlRelaxNGDefinePtr def);
2856
* xmlRelaxNGIsCompileable:
2857
* @define: the definition to check
2859
* Check if a definition is nullable.
2861
* Returns 1 if yes, 0 if no and -1 in case of error
2864
xmlRelaxNGIsCompileable(xmlRelaxNGDefinePtr def)
2871
if ((def->type != XML_RELAXNG_ELEMENT) &&
2872
(def->dflags & IS_COMPILABLE))
2874
if ((def->type != XML_RELAXNG_ELEMENT) &&
2875
(def->dflags & IS_NOT_COMPILABLE))
2877
switch (def->type) {
2878
case XML_RELAXNG_NOOP:
2879
ret = xmlRelaxNGIsCompileable(def->content);
2881
case XML_RELAXNG_TEXT:
2882
case XML_RELAXNG_EMPTY:
2885
case XML_RELAXNG_ELEMENT:
2887
* Check if the element content is compileable
2889
if (((def->dflags & IS_NOT_COMPILABLE) == 0) &&
2890
((def->dflags & IS_COMPILABLE) == 0)) {
2891
xmlRelaxNGDefinePtr list;
2893
list = def->content;
2894
while (list != NULL) {
2895
ret = xmlRelaxNGIsCompileable(list);
2901
* Because the routine is recursive, we must guard against
2902
* discovering both COMPILABLE and NOT_COMPILABLE
2905
def->dflags &= ~IS_COMPILABLE;
2906
def->dflags |= IS_NOT_COMPILABLE;
2908
if ((ret == 1) && !(def->dflags &= IS_NOT_COMPILABLE))
2909
def->dflags |= IS_COMPILABLE;
2910
#ifdef DEBUG_COMPILE
2912
xmlGenericError(xmlGenericErrorContext,
2913
"element content for %s is compilable\n",
2915
} else if (ret == 0) {
2916
xmlGenericError(xmlGenericErrorContext,
2917
"element content for %s is not compilable\n",
2920
xmlGenericError(xmlGenericErrorContext,
2921
"Problem in RelaxNGIsCompileable for element %s\n",
2927
* All elements return a compileable status unless they
2928
* are generic like anyName
2930
if ((def->nameClass != NULL) || (def->name == NULL))
2935
case XML_RELAXNG_REF:
2936
case XML_RELAXNG_EXTERNALREF:
2937
case XML_RELAXNG_PARENTREF:
2938
if (def->depth == -20) {
2941
xmlRelaxNGDefinePtr list;
2944
list = def->content;
2945
while (list != NULL) {
2946
ret = xmlRelaxNGIsCompileable(list);
2953
case XML_RELAXNG_START:
2954
case XML_RELAXNG_OPTIONAL:
2955
case XML_RELAXNG_ZEROORMORE:
2956
case XML_RELAXNG_ONEORMORE:
2957
case XML_RELAXNG_CHOICE:
2958
case XML_RELAXNG_GROUP:
2959
case XML_RELAXNG_DEF:{
2960
xmlRelaxNGDefinePtr list;
2962
list = def->content;
2963
while (list != NULL) {
2964
ret = xmlRelaxNGIsCompileable(list);
2971
case XML_RELAXNG_EXCEPT:
2972
case XML_RELAXNG_ATTRIBUTE:
2973
case XML_RELAXNG_INTERLEAVE:
2974
case XML_RELAXNG_DATATYPE:
2975
case XML_RELAXNG_LIST:
2976
case XML_RELAXNG_PARAM:
2977
case XML_RELAXNG_VALUE:
2978
case XML_RELAXNG_NOT_ALLOWED:
2983
def->dflags |= IS_NOT_COMPILABLE;
2985
def->dflags |= IS_COMPILABLE;
2986
#ifdef DEBUG_COMPILE
2988
xmlGenericError(xmlGenericErrorContext,
2989
"RelaxNGIsCompileable %s : true\n",
2990
xmlRelaxNGDefName(def));
2991
} else if (ret == 0) {
2992
xmlGenericError(xmlGenericErrorContext,
2993
"RelaxNGIsCompileable %s : false\n",
2994
xmlRelaxNGDefName(def));
2996
xmlGenericError(xmlGenericErrorContext,
2997
"Problem in RelaxNGIsCompileable %s\n",
2998
xmlRelaxNGDefName(def));
3005
* xmlRelaxNGCompile:
3006
* ctxt: the RelaxNG parser context
3007
* @define: the definition tree to compile
3009
* Compile the set of definitions, it works recursively, till the
3010
* element boundaries, where it tries to compile the content if possible
3012
* Returns 0 if success and -1 in case of error
3015
xmlRelaxNGCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3018
xmlRelaxNGDefinePtr list;
3020
if ((ctxt == NULL) || (def == NULL))
3023
switch (def->type) {
3024
case XML_RELAXNG_START:
3025
if ((xmlRelaxNGIsCompileable(def) == 1) && (def->depth != -25)) {
3026
xmlAutomataPtr oldam = ctxt->am;
3027
xmlAutomataStatePtr oldstate = ctxt->state;
3031
list = def->content;
3032
ctxt->am = xmlNewAutomata();
3033
if (ctxt->am == NULL)
3035
ctxt->state = xmlAutomataGetInitState(ctxt->am);
3036
while (list != NULL) {
3037
xmlRelaxNGCompile(ctxt, list);
3040
xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3041
def->contModel = xmlAutomataCompile(ctxt->am);
3042
xmlRegexpIsDeterminist(def->contModel);
3044
xmlFreeAutomata(ctxt->am);
3045
ctxt->state = oldstate;
3049
case XML_RELAXNG_ELEMENT:
3050
if ((ctxt->am != NULL) && (def->name != NULL)) {
3051
ctxt->state = xmlAutomataNewTransition2(ctxt->am,
3056
if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3057
xmlAutomataPtr oldam = ctxt->am;
3058
xmlAutomataStatePtr oldstate = ctxt->state;
3062
list = def->content;
3063
ctxt->am = xmlNewAutomata();
3064
if (ctxt->am == NULL)
3066
ctxt->state = xmlAutomataGetInitState(ctxt->am);
3067
while (list != NULL) {
3068
xmlRelaxNGCompile(ctxt, list);
3071
xmlAutomataSetFinalState(ctxt->am, ctxt->state);
3072
def->contModel = xmlAutomataCompile(ctxt->am);
3073
if (!xmlRegexpIsDeterminist(def->contModel)) {
3075
* we can only use the automata if it is determinist
3077
xmlRegFreeRegexp(def->contModel);
3078
def->contModel = NULL;
3080
xmlFreeAutomata(ctxt->am);
3081
ctxt->state = oldstate;
3084
xmlAutomataPtr oldam = ctxt->am;
3087
* we can't build the content model for this element content
3088
* but it still might be possible to build it for some of its
3089
* children, recurse.
3091
ret = xmlRelaxNGTryCompile(ctxt, def);
3095
case XML_RELAXNG_NOOP:
3096
ret = xmlRelaxNGCompile(ctxt, def->content);
3098
case XML_RELAXNG_OPTIONAL:{
3099
xmlAutomataStatePtr oldstate = ctxt->state;
3101
xmlRelaxNGCompile(ctxt, def->content);
3102
xmlAutomataNewEpsilon(ctxt->am, oldstate, ctxt->state);
3105
case XML_RELAXNG_ZEROORMORE:{
3106
xmlAutomataStatePtr oldstate;
3109
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3110
oldstate = ctxt->state;
3111
list = def->content;
3112
while (list != NULL) {
3113
xmlRelaxNGCompile(ctxt, list);
3116
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3118
xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3121
case XML_RELAXNG_ONEORMORE:{
3122
xmlAutomataStatePtr oldstate;
3124
list = def->content;
3125
while (list != NULL) {
3126
xmlRelaxNGCompile(ctxt, list);
3129
oldstate = ctxt->state;
3130
list = def->content;
3131
while (list != NULL) {
3132
xmlRelaxNGCompile(ctxt, list);
3135
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, oldstate);
3137
xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3140
case XML_RELAXNG_CHOICE:{
3141
xmlAutomataStatePtr target = NULL;
3142
xmlAutomataStatePtr oldstate = ctxt->state;
3144
list = def->content;
3145
while (list != NULL) {
3146
ctxt->state = oldstate;
3147
ret = xmlRelaxNGCompile(ctxt, list);
3151
target = ctxt->state;
3153
xmlAutomataNewEpsilon(ctxt->am, ctxt->state,
3158
ctxt->state = target;
3162
case XML_RELAXNG_REF:
3163
case XML_RELAXNG_EXTERNALREF:
3164
case XML_RELAXNG_PARENTREF:
3165
case XML_RELAXNG_GROUP:
3166
case XML_RELAXNG_DEF:
3167
list = def->content;
3168
while (list != NULL) {
3169
ret = xmlRelaxNGCompile(ctxt, list);
3175
case XML_RELAXNG_TEXT:{
3176
xmlAutomataStatePtr oldstate;
3179
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3180
oldstate = ctxt->state;
3181
xmlRelaxNGCompile(ctxt, def->content);
3182
xmlAutomataNewTransition(ctxt->am, ctxt->state,
3183
ctxt->state, BAD_CAST "#text",
3186
xmlAutomataNewEpsilon(ctxt->am, oldstate, NULL);
3189
case XML_RELAXNG_EMPTY:
3191
xmlAutomataNewEpsilon(ctxt->am, ctxt->state, NULL);
3193
case XML_RELAXNG_EXCEPT:
3194
case XML_RELAXNG_ATTRIBUTE:
3195
case XML_RELAXNG_INTERLEAVE:
3196
case XML_RELAXNG_NOT_ALLOWED:
3197
case XML_RELAXNG_DATATYPE:
3198
case XML_RELAXNG_LIST:
3199
case XML_RELAXNG_PARAM:
3200
case XML_RELAXNG_VALUE:
3201
/* This should not happen and generate an internal error */
3202
fprintf(stderr, "RNG internal error trying to compile %s\n",
3203
xmlRelaxNGDefName(def));
3210
* xmlRelaxNGTryCompile:
3211
* ctxt: the RelaxNG parser context
3212
* @define: the definition tree to compile
3214
* Try to compile the set of definitions, it works recursively,
3215
* possibly ignoring parts which cannot be compiled.
3217
* Returns 0 if success and -1 in case of error
3220
xmlRelaxNGTryCompile(xmlRelaxNGParserCtxtPtr ctxt, xmlRelaxNGDefinePtr def)
3223
xmlRelaxNGDefinePtr list;
3225
if ((ctxt == NULL) || (def == NULL))
3228
if ((def->type == XML_RELAXNG_START) ||
3229
(def->type == XML_RELAXNG_ELEMENT)) {
3230
ret = xmlRelaxNGIsCompileable(def);
3231
if ((def->dflags & IS_COMPILABLE) && (def->depth != -25)) {
3233
ret = xmlRelaxNGCompile(ctxt, def);
3234
#ifdef DEBUG_PROGRESSIVE
3236
if (def->type == XML_RELAXNG_START)
3237
xmlGenericError(xmlGenericErrorContext,
3238
"compiled the start\n");
3240
xmlGenericError(xmlGenericErrorContext,
3241
"compiled element %s\n", def->name);
3243
if (def->type == XML_RELAXNG_START)
3244
xmlGenericError(xmlGenericErrorContext,
3245
"failed to compile the start\n");
3247
xmlGenericError(xmlGenericErrorContext,
3248
"failed to compile element %s\n",
3255
switch (def->type) {
3256
case XML_RELAXNG_NOOP:
3257
ret = xmlRelaxNGTryCompile(ctxt, def->content);
3259
case XML_RELAXNG_TEXT:
3260
case XML_RELAXNG_DATATYPE:
3261
case XML_RELAXNG_LIST:
3262
case XML_RELAXNG_PARAM:
3263
case XML_RELAXNG_VALUE:
3264
case XML_RELAXNG_EMPTY:
3265
case XML_RELAXNG_ELEMENT:
3268
case XML_RELAXNG_OPTIONAL:
3269
case XML_RELAXNG_ZEROORMORE:
3270
case XML_RELAXNG_ONEORMORE:
3271
case XML_RELAXNG_CHOICE:
3272
case XML_RELAXNG_GROUP:
3273
case XML_RELAXNG_DEF:
3274
case XML_RELAXNG_START:
3275
case XML_RELAXNG_REF:
3276
case XML_RELAXNG_EXTERNALREF:
3277
case XML_RELAXNG_PARENTREF:
3278
list = def->content;
3279
while (list != NULL) {
3280
ret = xmlRelaxNGTryCompile(ctxt, list);
3286
case XML_RELAXNG_EXCEPT:
3287
case XML_RELAXNG_ATTRIBUTE:
3288
case XML_RELAXNG_INTERLEAVE:
3289
case XML_RELAXNG_NOT_ALLOWED:
3296
/************************************************************************
3298
* Parsing functions *
3300
************************************************************************/
3302
static xmlRelaxNGDefinePtr xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr
3303
ctxt, xmlNodePtr node);
3304
static xmlRelaxNGDefinePtr xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr
3305
ctxt, xmlNodePtr node);
3306
static xmlRelaxNGDefinePtr xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr
3307
ctxt, xmlNodePtr nodes,
3309
static xmlRelaxNGDefinePtr xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr
3310
ctxt, xmlNodePtr node);
3311
static xmlRelaxNGPtr xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt,
3313
static int xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
3315
static xmlRelaxNGDefinePtr xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr
3316
ctxt, xmlNodePtr node,
3319
static xmlRelaxNGGrammarPtr xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr
3320
ctxt, xmlNodePtr nodes);
3321
static int xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
3322
xmlRelaxNGDefinePtr define,
3326
#define IS_BLANK_NODE(n) (xmlRelaxNGIsBlank((n)->content))
3329
* xmlRelaxNGIsNullable:
3330
* @define: the definition to verify
3332
* Check if a definition is nullable.
3334
* Returns 1 if yes, 0 if no and -1 in case of error
3337
xmlRelaxNGIsNullable(xmlRelaxNGDefinePtr define)
3344
if (define->dflags & IS_NULLABLE)
3346
if (define->dflags & IS_NOT_NULLABLE)
3348
switch (define->type) {
3349
case XML_RELAXNG_EMPTY:
3350
case XML_RELAXNG_TEXT:
3353
case XML_RELAXNG_NOOP:
3354
case XML_RELAXNG_DEF:
3355
case XML_RELAXNG_REF:
3356
case XML_RELAXNG_EXTERNALREF:
3357
case XML_RELAXNG_PARENTREF:
3358
case XML_RELAXNG_ONEORMORE:
3359
ret = xmlRelaxNGIsNullable(define->content);
3361
case XML_RELAXNG_EXCEPT:
3362
case XML_RELAXNG_NOT_ALLOWED:
3363
case XML_RELAXNG_ELEMENT:
3364
case XML_RELAXNG_DATATYPE:
3365
case XML_RELAXNG_PARAM:
3366
case XML_RELAXNG_VALUE:
3367
case XML_RELAXNG_LIST:
3368
case XML_RELAXNG_ATTRIBUTE:
3371
case XML_RELAXNG_CHOICE:{
3372
xmlRelaxNGDefinePtr list = define->content;
3374
while (list != NULL) {
3375
ret = xmlRelaxNGIsNullable(list);
3383
case XML_RELAXNG_START:
3384
case XML_RELAXNG_INTERLEAVE:
3385
case XML_RELAXNG_GROUP:{
3386
xmlRelaxNGDefinePtr list = define->content;
3388
while (list != NULL) {
3389
ret = xmlRelaxNGIsNullable(list);
3401
define->dflags |= IS_NOT_NULLABLE;
3403
define->dflags |= IS_NULLABLE;
3408
* xmlRelaxNGIsBlank:
3411
* Check if a string is ignorable c.f. 4.2. Whitespace
3413
* Returns 1 if the string is NULL or made of blanks chars, 0 otherwise
3416
xmlRelaxNGIsBlank(xmlChar * str)
3421
if (!(IS_BLANK_CH(*str)))
3429
* xmlRelaxNGGetDataTypeLibrary:
3430
* @ctxt: a Relax-NG parser context
3431
* @node: the current data or value element
3433
* Applies algorithm from 4.3. datatypeLibrary attribute
3435
* Returns the datatypeLibary value or NULL if not found
3438
xmlRelaxNGGetDataTypeLibrary(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
3441
xmlChar *ret, *escape;
3443
if ((IS_RELAXNG(node, "data")) || (IS_RELAXNG(node, "value"))) {
3444
ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3450
escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3451
if (escape == NULL) {
3458
node = node->parent;
3459
while ((node != NULL) && (node->type == XML_ELEMENT_NODE)) {
3460
ret = xmlGetProp(node, BAD_CAST "datatypeLibrary");
3466
escape = xmlURIEscapeStr(ret, BAD_CAST ":/#?");
3467
if (escape == NULL) {
3473
node = node->parent;
3479
* xmlRelaxNGParseValue:
3480
* @ctxt: a Relax-NG parser context
3481
* @node: the data node.
3483
* parse the content of a RelaxNG value node.
3485
* Returns the definition pointer or NULL in case of error
3487
static xmlRelaxNGDefinePtr
3488
xmlRelaxNGParseValue(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3490
xmlRelaxNGDefinePtr def = NULL;
3491
xmlRelaxNGTypeLibraryPtr lib = NULL;
3496
def = xmlRelaxNGNewDefine(ctxt, node);
3499
def->type = XML_RELAXNG_VALUE;
3501
type = xmlGetProp(node, BAD_CAST "type");
3503
xmlRelaxNGNormExtSpace(type);
3504
if (xmlValidateNCName(type, 0)) {
3505
xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3506
"value type '%s' is not an NCName\n", type, NULL);
3508
library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3509
if (library == NULL)
3511
xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3516
lib = (xmlRelaxNGTypeLibraryPtr)
3517
xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3519
xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3520
"Use of unregistered type library '%s'\n", library,
3525
if (lib->have == NULL) {
3526
xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3527
"Internal error with type library '%s': no 'have'\n",
3530
success = lib->have(lib->data, def->name);
3532
xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3533
"Error type '%s' is not exported by type library '%s'\n",
3534
def->name, library);
3539
if (node->children == NULL) {
3540
def->value = xmlStrdup(BAD_CAST "");
3541
} else if (((node->children->type != XML_TEXT_NODE) &&
3542
(node->children->type != XML_CDATA_SECTION_NODE)) ||
3543
(node->children->next != NULL)) {
3544
xmlRngPErr(ctxt, node, XML_RNGP_TEXT_EXPECTED,
3545
"Expecting a single text value for <value>content\n",
3547
} else if (def != NULL) {
3548
def->value = xmlNodeGetContent(node);
3549
if (def->value == NULL) {
3550
xmlRngPErr(ctxt, node, XML_RNGP_VALUE_NO_CONTENT,
3551
"Element <value> has no content\n", NULL, NULL);
3552
} else if ((lib != NULL) && (lib->check != NULL) && (success == 1)) {
3556
lib->check(lib->data, def->name, def->value, &val, node);
3558
xmlRngPErr(ctxt, node, XML_RNGP_INVALID_VALUE,
3559
"Value '%s' is not acceptable for type '%s'\n",
3560
def->value, def->name);
3571
* xmlRelaxNGParseData:
3572
* @ctxt: a Relax-NG parser context
3573
* @node: the data node.
3575
* parse the content of a RelaxNG data node.
3577
* Returns the definition pointer or NULL in case of error
3579
static xmlRelaxNGDefinePtr
3580
xmlRelaxNGParseData(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
3582
xmlRelaxNGDefinePtr def = NULL, except;
3583
xmlRelaxNGDefinePtr param, lastparam = NULL;
3584
xmlRelaxNGTypeLibraryPtr lib;
3590
type = xmlGetProp(node, BAD_CAST "type");
3592
xmlRngPErr(ctxt, node, XML_RNGP_TYPE_MISSING, "data has no type\n", NULL,
3596
xmlRelaxNGNormExtSpace(type);
3597
if (xmlValidateNCName(type, 0)) {
3598
xmlRngPErr(ctxt, node, XML_RNGP_TYPE_VALUE,
3599
"data type '%s' is not an NCName\n", type, NULL);
3601
library = xmlRelaxNGGetDataTypeLibrary(ctxt, node);
3602
if (library == NULL)
3604
xmlStrdup(BAD_CAST "http://relaxng.org/ns/structure/1.0");
3606
def = xmlRelaxNGNewDefine(ctxt, node);
3611
def->type = XML_RELAXNG_DATATYPE;
3615
lib = (xmlRelaxNGTypeLibraryPtr)
3616
xmlHashLookup(xmlRelaxNGRegisteredTypes, library);
3618
xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_TYPE_LIB,
3619
"Use of unregistered type library '%s'\n", library,
3624
if (lib->have == NULL) {
3625
xmlRngPErr(ctxt, node, XML_RNGP_ERROR_TYPE_LIB,
3626
"Internal error with type library '%s': no 'have'\n",
3629
tmp = lib->have(lib->data, def->name);
3631
xmlRngPErr(ctxt, node, XML_RNGP_TYPE_NOT_FOUND,
3632
"Error type '%s' is not exported by type library '%s'\n",
3633
def->name, library);
3638
"http://www.w3.org/2001/XMLSchema-datatypes"))
3639
&& ((xmlStrEqual(def->name, BAD_CAST "IDREF"))
3640
|| (xmlStrEqual(def->name, BAD_CAST "IDREFS")))) {
3645
content = node->children;
3648
* Handle optional params
3650
while (content != NULL) {
3651
if (!xmlStrEqual(content->name, BAD_CAST "param"))
3653
if (xmlStrEqual(library,
3654
BAD_CAST "http://relaxng.org/ns/structure/1.0")) {
3655
xmlRngPErr(ctxt, node, XML_RNGP_PARAM_FORBIDDEN,
3656
"Type library '%s' does not allow type parameters\n",
3658
content = content->next;
3659
while ((content != NULL) &&
3660
(xmlStrEqual(content->name, BAD_CAST "param")))
3661
content = content->next;
3663
param = xmlRelaxNGNewDefine(ctxt, node);
3664
if (param != NULL) {
3665
param->type = XML_RELAXNG_PARAM;
3666
param->name = xmlGetProp(content, BAD_CAST "name");
3667
if (param->name == NULL) {
3668
xmlRngPErr(ctxt, node, XML_RNGP_PARAM_NAME_MISSING,
3669
"param has no name\n", NULL, NULL);
3671
param->value = xmlNodeGetContent(content);
3672
if (lastparam == NULL) {
3673
def->attrs = lastparam = param;
3675
lastparam->next = param;
3681
content = content->next;
3685
* Handle optional except
3687
if ((content != NULL)
3688
&& (xmlStrEqual(content->name, BAD_CAST "except"))) {
3690
xmlRelaxNGDefinePtr tmp2, last = NULL;
3692
except = xmlRelaxNGNewDefine(ctxt, node);
3693
if (except == NULL) {
3696
except->type = XML_RELAXNG_EXCEPT;
3697
child = content->children;
3698
def->content = except;
3699
if (child == NULL) {
3700
xmlRngPErr(ctxt, content, XML_RNGP_EXCEPT_NO_CONTENT,
3701
"except has no content\n", NULL, NULL);
3703
while (child != NULL) {
3704
tmp2 = xmlRelaxNGParsePattern(ctxt, child);
3707
except->content = last = tmp2;
3713
child = child->next;
3715
content = content->next;
3718
* Check there is no unhandled data
3720
if (content != NULL) {
3721
xmlRngPErr(ctxt, content, XML_RNGP_DATA_CONTENT,
3722
"Element data has unexpected content %s\n",
3723
content->name, NULL);
3729
static const xmlChar *invalidName = BAD_CAST "\1";
3732
* xmlRelaxNGCompareNameClasses:
3733
* @defs1: the first element/attribute defs
3734
* @defs2: the second element/attribute defs
3735
* @name: the restriction on the name
3736
* @ns: the restriction on the namespace
3738
* Compare the 2 lists of element definitions. The comparison is
3739
* that if both lists do not accept the same QNames, it returns 1
3740
* If the 2 lists can accept the same QName the comparison returns 0
3742
* Returns 1 disttinct, 0 if equal
3745
xmlRelaxNGCompareNameClasses(xmlRelaxNGDefinePtr def1,
3746
xmlRelaxNGDefinePtr def2)
3751
xmlRelaxNGValidCtxt ctxt;
3753
memset(&ctxt, 0, sizeof(xmlRelaxNGValidCtxt));
3755
ctxt.flags = FLAGS_IGNORABLE | FLAGS_NOERROR;
3757
if ((def1->type == XML_RELAXNG_ELEMENT) ||
3758
(def1->type == XML_RELAXNG_ATTRIBUTE)) {
3759
if (def2->type == XML_RELAXNG_TEXT)
3761
if (def1->name != NULL) {
3762
node.name = def1->name;
3764
node.name = invalidName;
3766
if (def1->ns != NULL) {
3767
if (def1->ns[0] == 0) {
3776
if (xmlRelaxNGElementMatch(&ctxt, def2, &node)) {
3777
if (def1->nameClass != NULL) {
3778
ret = xmlRelaxNGCompareNameClasses(def1->nameClass, def2);
3785
} else if (def1->type == XML_RELAXNG_TEXT) {
3786
if (def2->type == XML_RELAXNG_TEXT)
3789
} else if (def1->type == XML_RELAXNG_EXCEPT) {
3796
if ((def2->type == XML_RELAXNG_ELEMENT) ||
3797
(def2->type == XML_RELAXNG_ATTRIBUTE)) {
3798
if (def2->name != NULL) {
3799
node.name = def2->name;
3801
node.name = invalidName;
3804
if (def2->ns != NULL) {
3805
if (def2->ns[0] == 0) {
3811
ns.href = invalidName;
3813
if (xmlRelaxNGElementMatch(&ctxt, def1, &node)) {
3814
if (def2->nameClass != NULL) {
3815
ret = xmlRelaxNGCompareNameClasses(def2->nameClass, def1);
3830
* xmlRelaxNGCompareElemDefLists:
3831
* @ctxt: a Relax-NG parser context
3832
* @defs1: the first list of element/attribute defs
3833
* @defs2: the second list of element/attribute defs
3835
* Compare the 2 lists of element or attribute definitions. The comparison
3836
* is that if both lists do not accept the same QNames, it returns 1
3837
* If the 2 lists can accept the same QName the comparison returns 0
3839
* Returns 1 disttinct, 0 if equal
3842
xmlRelaxNGCompareElemDefLists(xmlRelaxNGParserCtxtPtr ctxt
3843
ATTRIBUTE_UNUSED, xmlRelaxNGDefinePtr * def1,
3844
xmlRelaxNGDefinePtr * def2)
3846
xmlRelaxNGDefinePtr *basedef2 = def2;
3848
if ((def1 == NULL) || (def2 == NULL))
3850
if ((*def1 == NULL) || (*def2 == NULL))
3852
while (*def1 != NULL) {
3853
while ((*def2) != NULL) {
3854
if (xmlRelaxNGCompareNameClasses(*def1, *def2) == 0)
3865
* xmlRelaxNGGenerateAttributes:
3866
* @ctxt: a Relax-NG parser context
3867
* @def: the definition definition
3869
* Check if the definition can only generate attributes
3871
* Returns 1 if yes, 0 if no and -1 in case of error.
3874
xmlRelaxNGGenerateAttributes(xmlRelaxNGParserCtxtPtr ctxt,
3875
xmlRelaxNGDefinePtr def)
3877
xmlRelaxNGDefinePtr parent, cur, tmp;
3880
* Don't run that check in case of error. Infinite recursion
3883
if (ctxt->nbErrors != 0)
3888
while (cur != NULL) {
3889
if ((cur->type == XML_RELAXNG_ELEMENT) ||
3890
(cur->type == XML_RELAXNG_TEXT) ||
3891
(cur->type == XML_RELAXNG_DATATYPE) ||
3892
(cur->type == XML_RELAXNG_PARAM) ||
3893
(cur->type == XML_RELAXNG_LIST) ||
3894
(cur->type == XML_RELAXNG_VALUE) ||
3895
(cur->type == XML_RELAXNG_EMPTY))
3897
if ((cur->type == XML_RELAXNG_CHOICE) ||
3898
(cur->type == XML_RELAXNG_INTERLEAVE) ||
3899
(cur->type == XML_RELAXNG_GROUP) ||
3900
(cur->type == XML_RELAXNG_ONEORMORE) ||
3901
(cur->type == XML_RELAXNG_ZEROORMORE) ||
3902
(cur->type == XML_RELAXNG_OPTIONAL) ||
3903
(cur->type == XML_RELAXNG_PARENTREF) ||
3904
(cur->type == XML_RELAXNG_EXTERNALREF) ||
3905
(cur->type == XML_RELAXNG_REF) ||
3906
(cur->type == XML_RELAXNG_DEF)) {
3907
if (cur->content != NULL) {
3911
while (tmp != NULL) {
3912
tmp->parent = parent;
3920
if (cur->next != NULL) {
3930
if (cur->next != NULL) {
3934
} while (cur != NULL);
3940
* xmlRelaxNGGetElements:
3941
* @ctxt: a Relax-NG parser context
3942
* @def: the definition definition
3943
* @eora: gather elements (0) or attributes (1)
3945
* Compute the list of top elements a definition can generate
3947
* Returns a list of elements or NULL if none was found.
3949
static xmlRelaxNGDefinePtr *
3950
xmlRelaxNGGetElements(xmlRelaxNGParserCtxtPtr ctxt,
3951
xmlRelaxNGDefinePtr def, int eora)
3953
xmlRelaxNGDefinePtr *ret = NULL, parent, cur, tmp;
3958
* Don't run that check in case of error. Infinite recursion
3961
if (ctxt->nbErrors != 0)
3966
while (cur != NULL) {
3967
if (((eora == 0) && ((cur->type == XML_RELAXNG_ELEMENT) ||
3968
(cur->type == XML_RELAXNG_TEXT))) ||
3969
((eora == 1) && (cur->type == XML_RELAXNG_ATTRIBUTE))) {
3972
ret = (xmlRelaxNGDefinePtr *)
3973
xmlMalloc((max + 1) * sizeof(xmlRelaxNGDefinePtr));
3975
xmlRngPErrMemory(ctxt, "getting element list\n");
3978
} else if (max <= len) {
3979
xmlRelaxNGDefinePtr *temp;
3982
temp = xmlRealloc(ret,
3983
(max + 1) * sizeof(xmlRelaxNGDefinePtr));
3985
xmlRngPErrMemory(ctxt, "getting element list\n");
3993
} else if ((cur->type == XML_RELAXNG_CHOICE) ||
3994
(cur->type == XML_RELAXNG_INTERLEAVE) ||
3995
(cur->type == XML_RELAXNG_GROUP) ||
3996
(cur->type == XML_RELAXNG_ONEORMORE) ||
3997
(cur->type == XML_RELAXNG_ZEROORMORE) ||
3998
(cur->type == XML_RELAXNG_OPTIONAL) ||
3999
(cur->type == XML_RELAXNG_PARENTREF) ||
4000
(cur->type == XML_RELAXNG_REF) ||
4001
(cur->type == XML_RELAXNG_DEF) ||
4002
(cur->type == XML_RELAXNG_EXTERNALREF)) {
4004
* Don't go within elements or attributes or string values.
4005
* Just gather the element top list
4007
if (cur->content != NULL) {
4011
while (tmp != NULL) {
4012
tmp->parent = parent;
4020
if (cur->next != NULL) {
4030
if (cur->next != NULL) {
4034
} while (cur != NULL);
4040
* xmlRelaxNGCheckChoiceDeterminism:
4041
* @ctxt: a Relax-NG parser context
4042
* @def: the choice definition
4044
* Also used to find indeterministic pattern in choice
4047
xmlRelaxNGCheckChoiceDeterminism(xmlRelaxNGParserCtxtPtr ctxt,
4048
xmlRelaxNGDefinePtr def)
4050
xmlRelaxNGDefinePtr **list;
4051
xmlRelaxNGDefinePtr cur;
4052
int nbchild = 0, i, j, ret;
4053
int is_nullable = 0;
4054
int is_indeterminist = 0;
4055
xmlHashTablePtr triage = NULL;
4058
if ((def == NULL) || (def->type != XML_RELAXNG_CHOICE))
4061
if (def->dflags & IS_PROCESSED)
4065
* Don't run that check in case of error. Infinite recursion
4068
if (ctxt->nbErrors != 0)
4071
is_nullable = xmlRelaxNGIsNullable(def);
4074
while (cur != NULL) {
4079
list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4080
sizeof(xmlRelaxNGDefinePtr
4083
xmlRngPErrMemory(ctxt, "building choice\n");
4088
* a bit strong but safe
4090
if (is_nullable == 0) {
4091
triage = xmlHashCreate(10);
4096
while (cur != NULL) {
4097
list[i] = xmlRelaxNGGetElements(ctxt, cur, 0);
4098
if ((list[i] == NULL) || (list[i][0] == NULL)) {
4100
} else if (is_triable == 1) {
4101
xmlRelaxNGDefinePtr *tmp;
4105
while ((*tmp != NULL) && (is_triable == 1)) {
4106
if ((*tmp)->type == XML_RELAXNG_TEXT) {
4107
res = xmlHashAddEntry2(triage,
4108
BAD_CAST "#text", NULL,
4112
} else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4113
((*tmp)->name != NULL)) {
4114
if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4115
res = xmlHashAddEntry2(triage,
4119
res = xmlHashAddEntry2(triage,
4120
(*tmp)->name, (*tmp)->ns,
4124
} else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4125
if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4126
res = xmlHashAddEntry2(triage,
4127
BAD_CAST "#any", NULL,
4130
res = xmlHashAddEntry2(triage,
4131
BAD_CAST "#any", (*tmp)->ns,
4145
for (i = 0; i < nbchild; i++) {
4146
if (list[i] == NULL)
4148
for (j = 0; j < i; j++) {
4149
if (list[j] == NULL)
4151
ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4153
is_indeterminist = 1;
4157
for (i = 0; i < nbchild; i++) {
4158
if (list[i] != NULL)
4163
if (is_indeterminist) {
4164
def->dflags |= IS_INDETERMINIST;
4166
if (is_triable == 1) {
4167
def->dflags |= IS_TRIABLE;
4169
} else if (triage != NULL) {
4170
xmlHashFree(triage, NULL);
4172
def->dflags |= IS_PROCESSED;
4176
* xmlRelaxNGCheckGroupAttrs:
4177
* @ctxt: a Relax-NG parser context
4178
* @def: the group definition
4180
* Detects violations of rule 7.3
4183
xmlRelaxNGCheckGroupAttrs(xmlRelaxNGParserCtxtPtr ctxt,
4184
xmlRelaxNGDefinePtr def)
4186
xmlRelaxNGDefinePtr **list;
4187
xmlRelaxNGDefinePtr cur;
4188
int nbchild = 0, i, j, ret;
4190
if ((def == NULL) ||
4191
((def->type != XML_RELAXNG_GROUP) &&
4192
(def->type != XML_RELAXNG_ELEMENT)))
4195
if (def->dflags & IS_PROCESSED)
4199
* Don't run that check in case of error. Infinite recursion
4202
if (ctxt->nbErrors != 0)
4206
while (cur != NULL) {
4211
while (cur != NULL) {
4216
list = (xmlRelaxNGDefinePtr **) xmlMalloc(nbchild *
4217
sizeof(xmlRelaxNGDefinePtr
4220
xmlRngPErrMemory(ctxt, "building group\n");
4225
while (cur != NULL) {
4226
list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4231
while (cur != NULL) {
4232
list[i] = xmlRelaxNGGetElements(ctxt, cur, 1);
4237
for (i = 0; i < nbchild; i++) {
4238
if (list[i] == NULL)
4240
for (j = 0; j < i; j++) {
4241
if (list[j] == NULL)
4243
ret = xmlRelaxNGCompareElemDefLists(ctxt, list[i], list[j]);
4245
xmlRngPErr(ctxt, def->node, XML_RNGP_GROUP_ATTR_CONFLICT,
4246
"Attributes conflicts in group\n", NULL, NULL);
4250
for (i = 0; i < nbchild; i++) {
4251
if (list[i] != NULL)
4256
def->dflags |= IS_PROCESSED;
4260
* xmlRelaxNGComputeInterleaves:
4261
* @def: the interleave definition
4262
* @ctxt: a Relax-NG parser context
4263
* @name: the definition name
4265
* A lot of work for preprocessing interleave definitions
4266
* is potentially needed to get a decent execution speed at runtime
4267
* - trying to get a total order on the element nodes generated
4268
* by the interleaves, order the list of interleave definitions
4269
* following that order.
4270
* - if <text/> is used to handle mixed content, it is better to
4271
* flag this in the define and simplify the runtime checking
4275
xmlRelaxNGComputeInterleaves(xmlRelaxNGDefinePtr def,
4276
xmlRelaxNGParserCtxtPtr ctxt,
4277
xmlChar * name ATTRIBUTE_UNUSED)
4279
xmlRelaxNGDefinePtr cur, *tmp;
4281
xmlRelaxNGPartitionPtr partitions = NULL;
4282
xmlRelaxNGInterleaveGroupPtr *groups = NULL;
4283
xmlRelaxNGInterleaveGroupPtr group;
4288
int is_determinist = 1;
4291
* Don't run that check in case of error. Infinite recursion
4294
if (ctxt->nbErrors != 0)
4297
#ifdef DEBUG_INTERLEAVE
4298
xmlGenericError(xmlGenericErrorContext,
4299
"xmlRelaxNGComputeInterleaves(%s)\n", name);
4302
while (cur != NULL) {
4307
#ifdef DEBUG_INTERLEAVE
4308
xmlGenericError(xmlGenericErrorContext, " %d child\n", nbchild);
4310
groups = (xmlRelaxNGInterleaveGroupPtr *)
4311
xmlMalloc(nbchild * sizeof(xmlRelaxNGInterleaveGroupPtr));
4315
while (cur != NULL) {
4316
groups[nbgroups] = (xmlRelaxNGInterleaveGroupPtr)
4317
xmlMalloc(sizeof(xmlRelaxNGInterleaveGroup));
4318
if (groups[nbgroups] == NULL)
4320
if (cur->type == XML_RELAXNG_TEXT)
4322
groups[nbgroups]->rule = cur;
4323
groups[nbgroups]->defs = xmlRelaxNGGetElements(ctxt, cur, 0);
4324
groups[nbgroups]->attrs = xmlRelaxNGGetElements(ctxt, cur, 1);
4328
#ifdef DEBUG_INTERLEAVE
4329
xmlGenericError(xmlGenericErrorContext, " %d groups\n", nbgroups);
4333
* Let's check that all rules makes a partitions according to 7.4
4335
partitions = (xmlRelaxNGPartitionPtr)
4336
xmlMalloc(sizeof(xmlRelaxNGPartition));
4337
if (partitions == NULL)
4339
memset(partitions, 0, sizeof(xmlRelaxNGPartition));
4340
partitions->nbgroups = nbgroups;
4341
partitions->triage = xmlHashCreate(nbgroups);
4342
for (i = 0; i < nbgroups; i++) {
4344
for (j = i + 1; j < nbgroups; j++) {
4345
if (groups[j] == NULL)
4348
ret = xmlRelaxNGCompareElemDefLists(ctxt, group->defs,
4351
xmlRngPErr(ctxt, def->node, XML_RNGP_ELEM_TEXT_CONFLICT,
4352
"Element or text conflicts in interleave\n",
4355
ret = xmlRelaxNGCompareElemDefLists(ctxt, group->attrs,
4358
xmlRngPErr(ctxt, def->node, XML_RNGP_ATTR_CONFLICT,
4359
"Attributes conflicts in interleave\n", NULL,
4364
if ((tmp != NULL) && (*tmp != NULL)) {
4365
while (*tmp != NULL) {
4366
if ((*tmp)->type == XML_RELAXNG_TEXT) {
4367
res = xmlHashAddEntry2(partitions->triage,
4368
BAD_CAST "#text", NULL,
4369
(void *) (long) (i + 1));
4371
is_determinist = -1;
4372
} else if (((*tmp)->type == XML_RELAXNG_ELEMENT) &&
4373
((*tmp)->name != NULL)) {
4374
if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4375
res = xmlHashAddEntry2(partitions->triage,
4377
(void *) (long) (i + 1));
4379
res = xmlHashAddEntry2(partitions->triage,
4380
(*tmp)->name, (*tmp)->ns,
4381
(void *) (long) (i + 1));
4383
is_determinist = -1;
4384
} else if ((*tmp)->type == XML_RELAXNG_ELEMENT) {
4385
if (((*tmp)->ns == NULL) || ((*tmp)->ns[0] == 0))
4386
res = xmlHashAddEntry2(partitions->triage,
4387
BAD_CAST "#any", NULL,
4388
(void *) (long) (i + 1));
4390
res = xmlHashAddEntry2(partitions->triage,
4391
BAD_CAST "#any", (*tmp)->ns,
4392
(void *) (long) (i + 1));
4393
if ((*tmp)->nameClass != NULL)
4396
is_determinist = -1;
4398
is_determinist = -1;
4406
partitions->groups = groups;
4409
* and save the partition list back in the def
4411
def->data = partitions;
4413
def->dflags |= IS_MIXED;
4414
if (is_determinist == 1)
4415
partitions->flags = IS_DETERMINIST;
4416
if (is_determinist == 2)
4417
partitions->flags = IS_DETERMINIST | IS_NEEDCHECK;
4421
xmlRngPErrMemory(ctxt, "in interleave computation\n");
4422
if (groups != NULL) {
4423
for (i = 0; i < nbgroups; i++)
4424
if (groups[i] != NULL) {
4425
if (groups[i]->defs != NULL)
4426
xmlFree(groups[i]->defs);
4431
xmlRelaxNGFreePartition(partitions);
4435
* xmlRelaxNGParseInterleave:
4436
* @ctxt: a Relax-NG parser context
4437
* @node: the data node.
4439
* parse the content of a RelaxNG interleave node.
4441
* Returns the definition pointer or NULL in case of error
4443
static xmlRelaxNGDefinePtr
4444
xmlRelaxNGParseInterleave(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4446
xmlRelaxNGDefinePtr def = NULL;
4447
xmlRelaxNGDefinePtr last = NULL, cur;
4450
def = xmlRelaxNGNewDefine(ctxt, node);
4454
def->type = XML_RELAXNG_INTERLEAVE;
4456
if (ctxt->interleaves == NULL)
4457
ctxt->interleaves = xmlHashCreate(10);
4458
if (ctxt->interleaves == NULL) {
4459
xmlRngPErrMemory(ctxt, "create interleaves\n");
4463
snprintf(name, 32, "interleave%d", ctxt->nbInterleaves++);
4464
if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST name, def) < 0) {
4465
xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_ADD,
4466
"Failed to add %s to hash table\n",
4467
(const xmlChar *) name, NULL);
4470
child = node->children;
4471
if (child == NULL) {
4472
xmlRngPErr(ctxt, node, XML_RNGP_INTERLEAVE_NO_CONTENT,
4473
"Element interleave is empty\n", NULL, NULL);
4475
while (child != NULL) {
4476
if (IS_RELAXNG(child, "element")) {
4477
cur = xmlRelaxNGParseElement(ctxt, child);
4479
cur = xmlRelaxNGParsePattern(ctxt, child);
4484
def->content = last = cur;
4490
child = child->next;
4497
* xmlRelaxNGParseInclude:
4498
* @ctxt: a Relax-NG parser context
4499
* @node: the include node
4501
* Integrate the content of an include node in the current grammar
4503
* Returns 0 in case of success or -1 in case of error
4506
xmlRelaxNGParseInclude(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4508
xmlRelaxNGIncludePtr incl;
4514
xmlRngPErr(ctxt, node, XML_RNGP_INCLUDE_EMPTY,
4515
"Include node has no data\n", NULL, NULL);
4518
root = xmlDocGetRootElement(incl->doc);
4520
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY, "Include document is empty\n",
4524
if (!xmlStrEqual(root->name, BAD_CAST "grammar")) {
4525
xmlRngPErr(ctxt, node, XML_RNGP_GRAMMAR_MISSING,
4526
"Include document root is not a grammar\n", NULL, NULL);
4531
* Merge the definition from both the include and the internal list
4533
if (root->children != NULL) {
4534
tmp = xmlRelaxNGParseGrammarContent(ctxt, root->children);
4538
if (node->children != NULL) {
4539
tmp = xmlRelaxNGParseGrammarContent(ctxt, node->children);
4547
* xmlRelaxNGParseDefine:
4548
* @ctxt: a Relax-NG parser context
4549
* @node: the define node
4551
* parse the content of a RelaxNG define element node.
4553
* Returns 0 in case of success or -1 in case of error
4556
xmlRelaxNGParseDefine(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4560
xmlRelaxNGDefinePtr def;
4561
const xmlChar *olddefine;
4563
name = xmlGetProp(node, BAD_CAST "name");
4565
xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_NAME_MISSING,
4566
"define has no name\n", NULL, NULL);
4568
xmlRelaxNGNormExtSpace(name);
4569
if (xmlValidateNCName(name, 0)) {
4570
xmlRngPErr(ctxt, node, XML_RNGP_INVALID_DEFINE_NAME,
4571
"define name '%s' is not an NCName\n", name, NULL);
4573
def = xmlRelaxNGNewDefine(ctxt, node);
4578
def->type = XML_RELAXNG_DEF;
4580
if (node->children == NULL) {
4581
xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_EMPTY,
4582
"define has no children\n", NULL, NULL);
4584
olddefine = ctxt->define;
4585
ctxt->define = name;
4587
xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4588
ctxt->define = olddefine;
4590
if (ctxt->grammar->defs == NULL)
4591
ctxt->grammar->defs = xmlHashCreate(10);
4592
if (ctxt->grammar->defs == NULL) {
4593
xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4594
"Could not create definition hash\n", NULL, NULL);
4597
tmp = xmlHashAddEntry(ctxt->grammar->defs, name, def);
4599
xmlRelaxNGDefinePtr prev;
4601
prev = xmlHashLookup(ctxt->grammar->defs, name);
4603
xmlRngPErr(ctxt, node, XML_RNGP_DEFINE_CREATE_FAILED,
4604
"Internal error on define aggregation of %s\n",
4608
while (prev->nextHash != NULL)
4609
prev = prev->nextHash;
4610
prev->nextHash = def;
4619
* xmlRelaxNGProcessExternalRef:
4620
* @ctxt: the parser context
4621
* @node: the externlRef node
4623
* Process and compile an externlRef node
4625
* Returns the xmlRelaxNGDefinePtr or NULL in case of error
4627
static xmlRelaxNGDefinePtr
4628
xmlRelaxNGProcessExternalRef(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4630
xmlRelaxNGDocumentPtr docu;
4631
xmlNodePtr root, tmp;
4633
int newNs = 0, oldflags;
4634
xmlRelaxNGDefinePtr def;
4638
def = xmlRelaxNGNewDefine(ctxt, node);
4641
def->type = XML_RELAXNG_EXTERNALREF;
4643
if (docu->content == NULL) {
4645
* Then do the parsing for good
4647
root = xmlDocGetRootElement(docu->doc);
4649
xmlRngPErr(ctxt, node, XML_RNGP_EXTERNALREF_EMTPY,
4650
"xmlRelaxNGParse: %s is empty\n", ctxt->URL,
4655
* ns transmission rules
4657
ns = xmlGetProp(root, BAD_CAST "ns");
4660
while ((tmp != NULL) && (tmp->type == XML_ELEMENT_NODE)) {
4661
ns = xmlGetProp(tmp, BAD_CAST "ns");
4668
xmlSetProp(root, BAD_CAST "ns", ns);
4677
* Parsing to get a precompiled schemas.
4679
oldflags = ctxt->flags;
4680
ctxt->flags |= XML_RELAXNG_IN_EXTERNALREF;
4681
docu->schema = xmlRelaxNGParseDocument(ctxt, root);
4682
ctxt->flags = oldflags;
4683
if ((docu->schema != NULL) &&
4684
(docu->schema->topgrammar != NULL)) {
4685
docu->content = docu->schema->topgrammar->start;
4689
* the externalRef may be reused in a different ns context
4692
xmlUnsetProp(root, BAD_CAST "ns");
4695
def->content = docu->content;
4703
* xmlRelaxNGParsePattern:
4704
* @ctxt: a Relax-NG parser context
4705
* @node: the pattern node.
4707
* parse the content of a RelaxNG pattern node.
4709
* Returns the definition pointer or NULL in case of error or if no
4710
* pattern is generated.
4712
static xmlRelaxNGDefinePtr
4713
xmlRelaxNGParsePattern(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
4715
xmlRelaxNGDefinePtr def = NULL;
4720
if (IS_RELAXNG(node, "element")) {
4721
def = xmlRelaxNGParseElement(ctxt, node);
4722
} else if (IS_RELAXNG(node, "attribute")) {
4723
def = xmlRelaxNGParseAttribute(ctxt, node);
4724
} else if (IS_RELAXNG(node, "empty")) {
4725
def = xmlRelaxNGNewDefine(ctxt, node);
4728
def->type = XML_RELAXNG_EMPTY;
4729
if (node->children != NULL) {
4730
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_NOT_EMPTY,
4731
"empty: had a child node\n", NULL, NULL);
4733
} else if (IS_RELAXNG(node, "text")) {
4734
def = xmlRelaxNGNewDefine(ctxt, node);
4737
def->type = XML_RELAXNG_TEXT;
4738
if (node->children != NULL) {
4739
xmlRngPErr(ctxt, node, XML_RNGP_TEXT_HAS_CHILD,
4740
"text: had a child node\n", NULL, NULL);
4742
} else if (IS_RELAXNG(node, "zeroOrMore")) {
4743
def = xmlRelaxNGNewDefine(ctxt, node);
4746
def->type = XML_RELAXNG_ZEROORMORE;
4747
if (node->children == NULL) {
4748
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4749
"Element %s is empty\n", node->name, NULL);
4752
xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4754
} else if (IS_RELAXNG(node, "oneOrMore")) {
4755
def = xmlRelaxNGNewDefine(ctxt, node);
4758
def->type = XML_RELAXNG_ONEORMORE;
4759
if (node->children == NULL) {
4760
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4761
"Element %s is empty\n", node->name, NULL);
4764
xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4766
} else if (IS_RELAXNG(node, "optional")) {
4767
def = xmlRelaxNGNewDefine(ctxt, node);
4770
def->type = XML_RELAXNG_OPTIONAL;
4771
if (node->children == NULL) {
4772
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4773
"Element %s is empty\n", node->name, NULL);
4776
xmlRelaxNGParsePatterns(ctxt, node->children, 1);
4778
} else if (IS_RELAXNG(node, "choice")) {
4779
def = xmlRelaxNGNewDefine(ctxt, node);
4782
def->type = XML_RELAXNG_CHOICE;
4783
if (node->children == NULL) {
4784
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4785
"Element %s is empty\n", node->name, NULL);
4788
xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4790
} else if (IS_RELAXNG(node, "group")) {
4791
def = xmlRelaxNGNewDefine(ctxt, node);
4794
def->type = XML_RELAXNG_GROUP;
4795
if (node->children == NULL) {
4796
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4797
"Element %s is empty\n", node->name, NULL);
4800
xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4802
} else if (IS_RELAXNG(node, "ref")) {
4803
def = xmlRelaxNGNewDefine(ctxt, node);
4806
def->type = XML_RELAXNG_REF;
4807
def->name = xmlGetProp(node, BAD_CAST "name");
4808
if (def->name == NULL) {
4809
xmlRngPErr(ctxt, node, XML_RNGP_REF_NO_NAME, "ref has no name\n",
4812
xmlRelaxNGNormExtSpace(def->name);
4813
if (xmlValidateNCName(def->name, 0)) {
4814
xmlRngPErr(ctxt, node, XML_RNGP_REF_NAME_INVALID,
4815
"ref name '%s' is not an NCName\n", def->name,
4819
if (node->children != NULL) {
4820
xmlRngPErr(ctxt, node, XML_RNGP_REF_NOT_EMPTY, "ref is not empty\n",
4823
if (ctxt->grammar->refs == NULL)
4824
ctxt->grammar->refs = xmlHashCreate(10);
4825
if (ctxt->grammar->refs == NULL) {
4826
xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4827
"Could not create references hash\n", NULL, NULL);
4832
tmp = xmlHashAddEntry(ctxt->grammar->refs, def->name, def);
4834
xmlRelaxNGDefinePtr prev;
4836
prev = (xmlRelaxNGDefinePtr)
4837
xmlHashLookup(ctxt->grammar->refs, def->name);
4839
if (def->name != NULL) {
4840
xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4841
"Error refs definitions '%s'\n",
4844
xmlRngPErr(ctxt, node, XML_RNGP_REF_CREATE_FAILED,
4845
"Error refs definitions\n",
4850
def->nextHash = prev->nextHash;
4851
prev->nextHash = def;
4855
} else if (IS_RELAXNG(node, "data")) {
4856
def = xmlRelaxNGParseData(ctxt, node);
4857
} else if (IS_RELAXNG(node, "value")) {
4858
def = xmlRelaxNGParseValue(ctxt, node);
4859
} else if (IS_RELAXNG(node, "list")) {
4860
def = xmlRelaxNGNewDefine(ctxt, node);
4863
def->type = XML_RELAXNG_LIST;
4864
if (node->children == NULL) {
4865
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT,
4866
"Element %s is empty\n", node->name, NULL);
4869
xmlRelaxNGParsePatterns(ctxt, node->children, 0);
4871
} else if (IS_RELAXNG(node, "interleave")) {
4872
def = xmlRelaxNGParseInterleave(ctxt, node);
4873
} else if (IS_RELAXNG(node, "externalRef")) {
4874
def = xmlRelaxNGProcessExternalRef(ctxt, node);
4875
} else if (IS_RELAXNG(node, "notAllowed")) {
4876
def = xmlRelaxNGNewDefine(ctxt, node);
4879
def->type = XML_RELAXNG_NOT_ALLOWED;
4880
if (node->children != NULL) {
4881
xmlRngPErr(ctxt, node, XML_RNGP_NOTALLOWED_NOT_EMPTY,
4882
"xmlRelaxNGParse: notAllowed element is not empty\n",
4885
} else if (IS_RELAXNG(node, "grammar")) {
4886
xmlRelaxNGGrammarPtr grammar, old;
4887
xmlRelaxNGGrammarPtr oldparent;
4889
#ifdef DEBUG_GRAMMAR
4890
xmlGenericError(xmlGenericErrorContext,
4891
"Found <grammar> pattern\n");
4894
oldparent = ctxt->parentgrammar;
4895
old = ctxt->grammar;
4896
ctxt->parentgrammar = old;
4897
grammar = xmlRelaxNGParseGrammar(ctxt, node->children);
4899
ctxt->grammar = old;
4900
ctxt->parentgrammar = oldparent;
4902
if (grammar != NULL) {
4903
grammar->next = old->next;
4904
old->next = grammar;
4908
if (grammar != NULL)
4909
def = grammar->start;
4912
} else if (IS_RELAXNG(node, "parentRef")) {
4913
if (ctxt->parentgrammar == NULL) {
4914
xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_PARENT,
4915
"Use of parentRef without a parent grammar\n", NULL,
4919
def = xmlRelaxNGNewDefine(ctxt, node);
4922
def->type = XML_RELAXNG_PARENTREF;
4923
def->name = xmlGetProp(node, BAD_CAST "name");
4924
if (def->name == NULL) {
4925
xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NO_NAME,
4926
"parentRef has no name\n", NULL, NULL);
4928
xmlRelaxNGNormExtSpace(def->name);
4929
if (xmlValidateNCName(def->name, 0)) {
4930
xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NAME_INVALID,
4931
"parentRef name '%s' is not an NCName\n",
4935
if (node->children != NULL) {
4936
xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_NOT_EMPTY,
4937
"parentRef is not empty\n", NULL, NULL);
4939
if (ctxt->parentgrammar->refs == NULL)
4940
ctxt->parentgrammar->refs = xmlHashCreate(10);
4941
if (ctxt->parentgrammar->refs == NULL) {
4942
xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4943
"Could not create references hash\n", NULL, NULL);
4945
} else if (def->name != NULL) {
4949
xmlHashAddEntry(ctxt->parentgrammar->refs, def->name, def);
4951
xmlRelaxNGDefinePtr prev;
4953
prev = (xmlRelaxNGDefinePtr)
4954
xmlHashLookup(ctxt->parentgrammar->refs, def->name);
4956
xmlRngPErr(ctxt, node, XML_RNGP_PARENTREF_CREATE_FAILED,
4957
"Internal error parentRef definitions '%s'\n",
4961
def->nextHash = prev->nextHash;
4962
prev->nextHash = def;
4966
} else if (IS_RELAXNG(node, "mixed")) {
4967
if (node->children == NULL) {
4968
xmlRngPErr(ctxt, node, XML_RNGP_EMPTY_CONSTRUCT, "Mixed is empty\n",
4972
def = xmlRelaxNGParseInterleave(ctxt, node);
4974
xmlRelaxNGDefinePtr tmp;
4976
if ((def->content != NULL) && (def->content->next != NULL)) {
4977
tmp = xmlRelaxNGNewDefine(ctxt, node);
4979
tmp->type = XML_RELAXNG_GROUP;
4980
tmp->content = def->content;
4985
tmp = xmlRelaxNGNewDefine(ctxt, node);
4988
tmp->type = XML_RELAXNG_TEXT;
4989
tmp->next = def->content;
4994
xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_CONSTRUCT,
4995
"Unexpected node %s is not a pattern\n", node->name,
5003
* xmlRelaxNGParseAttribute:
5004
* @ctxt: a Relax-NG parser context
5005
* @node: the element node
5007
* parse the content of a RelaxNG attribute node.
5009
* Returns the definition pointer or NULL in case of error.
5011
static xmlRelaxNGDefinePtr
5012
xmlRelaxNGParseAttribute(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5014
xmlRelaxNGDefinePtr ret, cur;
5018
ret = xmlRelaxNGNewDefine(ctxt, node);
5021
ret->type = XML_RELAXNG_ATTRIBUTE;
5022
ret->parent = ctxt->def;
5023
child = node->children;
5024
if (child == NULL) {
5025
xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_EMPTY,
5026
"xmlRelaxNGParseattribute: attribute has no children\n",
5030
old_flags = ctxt->flags;
5031
ctxt->flags |= XML_RELAXNG_IN_ATTRIBUTE;
5032
cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5034
child = child->next;
5036
if (child != NULL) {
5037
cur = xmlRelaxNGParsePattern(ctxt, child);
5039
switch (cur->type) {
5040
case XML_RELAXNG_EMPTY:
5041
case XML_RELAXNG_NOT_ALLOWED:
5042
case XML_RELAXNG_TEXT:
5043
case XML_RELAXNG_ELEMENT:
5044
case XML_RELAXNG_DATATYPE:
5045
case XML_RELAXNG_VALUE:
5046
case XML_RELAXNG_LIST:
5047
case XML_RELAXNG_REF:
5048
case XML_RELAXNG_PARENTREF:
5049
case XML_RELAXNG_EXTERNALREF:
5050
case XML_RELAXNG_DEF:
5051
case XML_RELAXNG_ONEORMORE:
5052
case XML_RELAXNG_ZEROORMORE:
5053
case XML_RELAXNG_OPTIONAL:
5054
case XML_RELAXNG_CHOICE:
5055
case XML_RELAXNG_GROUP:
5056
case XML_RELAXNG_INTERLEAVE:
5057
case XML_RELAXNG_ATTRIBUTE:
5061
case XML_RELAXNG_START:
5062
case XML_RELAXNG_PARAM:
5063
case XML_RELAXNG_EXCEPT:
5064
xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CONTENT,
5065
"attribute has invalid content\n", NULL,
5068
case XML_RELAXNG_NOOP:
5069
xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_NOOP,
5070
"RNG Internal error, noop found in attribute\n",
5075
child = child->next;
5077
if (child != NULL) {
5078
xmlRngPErr(ctxt, node, XML_RNGP_ATTRIBUTE_CHILDREN,
5079
"attribute has multiple children\n", NULL, NULL);
5081
ctxt->flags = old_flags;
5086
* xmlRelaxNGParseExceptNameClass:
5087
* @ctxt: a Relax-NG parser context
5088
* @node: the except node
5089
* @attr: 1 if within an attribute, 0 if within an element
5091
* parse the content of a RelaxNG nameClass node.
5093
* Returns the definition pointer or NULL in case of error.
5095
static xmlRelaxNGDefinePtr
5096
xmlRelaxNGParseExceptNameClass(xmlRelaxNGParserCtxtPtr ctxt,
5097
xmlNodePtr node, int attr)
5099
xmlRelaxNGDefinePtr ret, cur, last = NULL;
5102
if (!IS_RELAXNG(node, "except")) {
5103
xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MISSING,
5104
"Expecting an except node\n", NULL, NULL);
5107
if (node->next != NULL) {
5108
xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_MULTIPLE,
5109
"exceptNameClass allows only a single except node\n",
5112
if (node->children == NULL) {
5113
xmlRngPErr(ctxt, node, XML_RNGP_EXCEPT_EMPTY, "except has no content\n",
5118
ret = xmlRelaxNGNewDefine(ctxt, node);
5121
ret->type = XML_RELAXNG_EXCEPT;
5122
child = node->children;
5123
while (child != NULL) {
5124
cur = xmlRelaxNGNewDefine(ctxt, child);
5128
cur->type = XML_RELAXNG_ATTRIBUTE;
5130
cur->type = XML_RELAXNG_ELEMENT;
5132
if (xmlRelaxNGParseNameClass(ctxt, child, cur) != NULL) {
5140
child = child->next;
5147
* xmlRelaxNGParseNameClass:
5148
* @ctxt: a Relax-NG parser context
5149
* @node: the nameClass node
5150
* @def: the current definition
5152
* parse the content of a RelaxNG nameClass node.
5154
* Returns the definition pointer or NULL in case of error.
5156
static xmlRelaxNGDefinePtr
5157
xmlRelaxNGParseNameClass(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node,
5158
xmlRelaxNGDefinePtr def)
5160
xmlRelaxNGDefinePtr ret, tmp;
5164
if ((IS_RELAXNG(node, "name")) || (IS_RELAXNG(node, "anyName")) ||
5165
(IS_RELAXNG(node, "nsName"))) {
5166
if ((def->type != XML_RELAXNG_ELEMENT) &&
5167
(def->type != XML_RELAXNG_ATTRIBUTE)) {
5168
ret = xmlRelaxNGNewDefine(ctxt, node);
5172
if (ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE)
5173
ret->type = XML_RELAXNG_ATTRIBUTE;
5175
ret->type = XML_RELAXNG_ELEMENT;
5178
if (IS_RELAXNG(node, "name")) {
5179
val = xmlNodeGetContent(node);
5180
xmlRelaxNGNormExtSpace(val);
5181
if (xmlValidateNCName(val, 0)) {
5182
if (node->parent != NULL)
5183
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5184
"Element %s name '%s' is not an NCName\n",
5185
node->parent->name, val);
5187
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NAME,
5188
"name '%s' is not an NCName\n",
5192
val = xmlGetProp(node, BAD_CAST "ns");
5194
if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5196
(xmlStrEqual(val, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5197
xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5198
"Attribute with namespace '%s' is not allowed\n",
5201
if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5203
(val[0] == 0) && (xmlStrEqual(ret->name, BAD_CAST "xmlns"))) {
5204
xmlRngPErr(ctxt, node, XML_RNGP_XMLNS_NAME,
5205
"Attribute with QName 'xmlns' is not allowed\n",
5208
} else if (IS_RELAXNG(node, "anyName")) {
5211
if (node->children != NULL) {
5213
xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5215
XML_RELAXNG_ATTRIBUTE));
5217
} else if (IS_RELAXNG(node, "nsName")) {
5219
ret->ns = xmlGetProp(node, BAD_CAST "ns");
5220
if (ret->ns == NULL) {
5221
xmlRngPErr(ctxt, node, XML_RNGP_NSNAME_NO_NS,
5222
"nsName has no ns attribute\n", NULL, NULL);
5224
if ((ctxt->flags & XML_RELAXNG_IN_ATTRIBUTE) &&
5225
(ret->ns != NULL) &&
5227
(ret->ns, BAD_CAST "http://www.w3.org/2000/xmlns"))) {
5228
xmlRngPErr(ctxt, node, XML_RNGP_XML_NS,
5229
"Attribute with namespace '%s' is not allowed\n",
5232
if (node->children != NULL) {
5234
xmlRelaxNGParseExceptNameClass(ctxt, node->children,
5236
XML_RELAXNG_ATTRIBUTE));
5238
} else if (IS_RELAXNG(node, "choice")) {
5240
xmlRelaxNGDefinePtr last = NULL;
5242
ret = xmlRelaxNGNewDefine(ctxt, node);
5246
ret->type = XML_RELAXNG_CHOICE;
5248
if (node->children == NULL) {
5249
xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_EMPTY,
5250
"Element choice is empty\n", NULL, NULL);
5253
child = node->children;
5254
while (child != NULL) {
5255
tmp = xmlRelaxNGParseNameClass(ctxt, child, ret);
5258
last = ret->nameClass = tmp;
5264
child = child->next;
5268
xmlRngPErr(ctxt, node, XML_RNGP_CHOICE_CONTENT,
5269
"expecting name, anyName, nsName or choice : got %s\n",
5274
if (def->nameClass == NULL) {
5275
def->nameClass = ret;
5277
tmp = def->nameClass;
5278
while (tmp->next != NULL) {
5288
* xmlRelaxNGParseElement:
5289
* @ctxt: a Relax-NG parser context
5290
* @node: the element node
5292
* parse the content of a RelaxNG element node.
5294
* Returns the definition pointer or NULL in case of error.
5296
static xmlRelaxNGDefinePtr
5297
xmlRelaxNGParseElement(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
5299
xmlRelaxNGDefinePtr ret, cur, last;
5301
const xmlChar *olddefine;
5303
ret = xmlRelaxNGNewDefine(ctxt, node);
5306
ret->type = XML_RELAXNG_ELEMENT;
5307
ret->parent = ctxt->def;
5308
child = node->children;
5309
if (child == NULL) {
5310
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_EMPTY,
5311
"xmlRelaxNGParseElement: element has no children\n",
5315
cur = xmlRelaxNGParseNameClass(ctxt, child, ret);
5317
child = child->next;
5319
if (child == NULL) {
5320
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_NO_CONTENT,
5321
"xmlRelaxNGParseElement: element has no content\n",
5325
olddefine = ctxt->define;
5326
ctxt->define = NULL;
5328
while (child != NULL) {
5329
cur = xmlRelaxNGParsePattern(ctxt, child);
5332
switch (cur->type) {
5333
case XML_RELAXNG_EMPTY:
5334
case XML_RELAXNG_NOT_ALLOWED:
5335
case XML_RELAXNG_TEXT:
5336
case XML_RELAXNG_ELEMENT:
5337
case XML_RELAXNG_DATATYPE:
5338
case XML_RELAXNG_VALUE:
5339
case XML_RELAXNG_LIST:
5340
case XML_RELAXNG_REF:
5341
case XML_RELAXNG_PARENTREF:
5342
case XML_RELAXNG_EXTERNALREF:
5343
case XML_RELAXNG_DEF:
5344
case XML_RELAXNG_ZEROORMORE:
5345
case XML_RELAXNG_ONEORMORE:
5346
case XML_RELAXNG_OPTIONAL:
5347
case XML_RELAXNG_CHOICE:
5348
case XML_RELAXNG_GROUP:
5349
case XML_RELAXNG_INTERLEAVE:
5351
ret->content = last = cur;
5353
if ((last->type == XML_RELAXNG_ELEMENT) &&
5354
(ret->content == last)) {
5355
ret->content = xmlRelaxNGNewDefine(ctxt, node);
5356
if (ret->content != NULL) {
5357
ret->content->type = XML_RELAXNG_GROUP;
5358
ret->content->content = last;
5360
ret->content = last;
5367
case XML_RELAXNG_ATTRIBUTE:
5368
cur->next = ret->attrs;
5371
case XML_RELAXNG_START:
5372
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5373
"RNG Internal error, start found in element\n",
5376
case XML_RELAXNG_PARAM:
5377
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5378
"RNG Internal error, param found in element\n",
5381
case XML_RELAXNG_EXCEPT:
5382
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5383
"RNG Internal error, except found in element\n",
5386
case XML_RELAXNG_NOOP:
5387
xmlRngPErr(ctxt, node, XML_RNGP_ELEMENT_CONTENT,
5388
"RNG Internal error, noop found in element\n",
5393
child = child->next;
5395
ctxt->define = olddefine;
5400
* xmlRelaxNGParsePatterns:
5401
* @ctxt: a Relax-NG parser context
5402
* @nodes: list of nodes
5403
* @group: use an implicit <group> for elements
5405
* parse the content of a RelaxNG start node.
5407
* Returns the definition pointer or NULL in case of error.
5409
static xmlRelaxNGDefinePtr
5410
xmlRelaxNGParsePatterns(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes,
5413
xmlRelaxNGDefinePtr def = NULL, last = NULL, cur, parent;
5416
while (nodes != NULL) {
5417
if (IS_RELAXNG(nodes, "element")) {
5418
cur = xmlRelaxNGParseElement(ctxt, nodes);
5422
if ((group == 1) && (def->type == XML_RELAXNG_ELEMENT) &&
5424
def = xmlRelaxNGNewDefine(ctxt, nodes);
5425
def->type = XML_RELAXNG_GROUP;
5426
def->content = last;
5431
cur->parent = parent;
5433
cur = xmlRelaxNGParsePattern(ctxt, nodes);
5443
nodes = nodes->next;
5449
* xmlRelaxNGParseStart:
5450
* @ctxt: a Relax-NG parser context
5451
* @nodes: start children nodes
5453
* parse the content of a RelaxNG start node.
5455
* Returns 0 in case of success, -1 in case of error
5458
xmlRelaxNGParseStart(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
5461
xmlRelaxNGDefinePtr def = NULL, last;
5463
if (nodes == NULL) {
5464
xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY, "start has no children\n",
5468
if (IS_RELAXNG(nodes, "empty")) {
5469
def = xmlRelaxNGNewDefine(ctxt, nodes);
5472
def->type = XML_RELAXNG_EMPTY;
5473
if (nodes->children != NULL) {
5474
xmlRngPErr(ctxt, nodes, XML_RNGP_EMPTY_CONTENT,
5475
"element empty is not empty\n", NULL, NULL);
5477
} else if (IS_RELAXNG(nodes, "notAllowed")) {
5478
def = xmlRelaxNGNewDefine(ctxt, nodes);
5481
def->type = XML_RELAXNG_NOT_ALLOWED;
5482
if (nodes->children != NULL) {
5483
xmlRngPErr(ctxt, nodes, XML_RNGP_NOTALLOWED_NOT_EMPTY,
5484
"element notAllowed is not empty\n", NULL, NULL);
5487
def = xmlRelaxNGParsePatterns(ctxt, nodes, 1);
5489
if (ctxt->grammar->start != NULL) {
5490
last = ctxt->grammar->start;
5491
while (last->next != NULL)
5495
ctxt->grammar->start = def;
5497
nodes = nodes->next;
5498
if (nodes != NULL) {
5499
xmlRngPErr(ctxt, nodes, XML_RNGP_START_CONTENT,
5500
"start more than one children\n", NULL, NULL);
5507
* xmlRelaxNGParseGrammarContent:
5508
* @ctxt: a Relax-NG parser context
5509
* @nodes: grammar children nodes
5511
* parse the content of a RelaxNG grammar node.
5513
* Returns 0 in case of success, -1 in case of error
5516
xmlRelaxNGParseGrammarContent(xmlRelaxNGParserCtxtPtr ctxt,
5521
if (nodes == NULL) {
5522
xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_EMPTY,
5523
"grammar has no children\n", NULL, NULL);
5526
while (nodes != NULL) {
5527
if (IS_RELAXNG(nodes, "start")) {
5528
if (nodes->children == NULL) {
5529
xmlRngPErr(ctxt, nodes, XML_RNGP_START_EMPTY,
5530
"start has no children\n", NULL, NULL);
5532
tmp = xmlRelaxNGParseStart(ctxt, nodes->children);
5536
} else if (IS_RELAXNG(nodes, "define")) {
5537
tmp = xmlRelaxNGParseDefine(ctxt, nodes);
5540
} else if (IS_RELAXNG(nodes, "include")) {
5541
tmp = xmlRelaxNGParseInclude(ctxt, nodes);
5545
xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
5546
"grammar has unexpected child %s\n", nodes->name,
5550
nodes = nodes->next;
5556
* xmlRelaxNGCheckReference:
5558
* @ctxt: a Relax-NG parser context
5559
* @name: the name associated to the defines
5561
* Applies the 4.17. combine attribute rule for all the define
5562
* element of a given grammar using the same name.
5565
xmlRelaxNGCheckReference(xmlRelaxNGDefinePtr ref,
5566
xmlRelaxNGParserCtxtPtr ctxt,
5567
const xmlChar * name)
5569
xmlRelaxNGGrammarPtr grammar;
5570
xmlRelaxNGDefinePtr def, cur;
5572
grammar = ctxt->grammar;
5573
if (grammar == NULL) {
5574
xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5575
"Internal error: no grammar in CheckReference %s\n",
5579
if (ref->content != NULL) {
5580
xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
5581
"Internal error: reference has content in CheckReference %s\n",
5585
if (grammar->defs != NULL) {
5586
def = xmlHashLookup(grammar->defs, name);
5589
while (cur != NULL) {
5591
cur = cur->nextHash;
5594
xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5595
"Reference %s has no matching definition\n", name,
5599
xmlRngPErr(ctxt, ref->node, XML_RNGP_REF_NO_DEF,
5600
"Reference %s has no matching definition\n", name,
5606
* xmlRelaxNGCheckCombine:
5607
* @define: the define(s) list
5608
* @ctxt: a Relax-NG parser context
5609
* @name: the name associated to the defines
5611
* Applies the 4.17. combine attribute rule for all the define
5612
* element of a given grammar using the same name.
5615
xmlRelaxNGCheckCombine(xmlRelaxNGDefinePtr define,
5616
xmlRelaxNGParserCtxtPtr ctxt, const xmlChar * name)
5619
int choiceOrInterleave = -1;
5621
xmlRelaxNGDefinePtr cur, last, tmp, tmp2;
5623
if (define->nextHash == NULL)
5626
while (cur != NULL) {
5627
combine = xmlGetProp(cur->node, BAD_CAST "combine");
5628
if (combine != NULL) {
5629
if (xmlStrEqual(combine, BAD_CAST "choice")) {
5630
if (choiceOrInterleave == -1)
5631
choiceOrInterleave = 1;
5632
else if (choiceOrInterleave == 0) {
5633
xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5634
"Defines for %s use both 'choice' and 'interleave'\n",
5637
} else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5638
if (choiceOrInterleave == -1)
5639
choiceOrInterleave = 0;
5640
else if (choiceOrInterleave == 1) {
5641
xmlRngPErr(ctxt, define->node, XML_RNGP_DEF_CHOICE_AND_INTERLEAVE,
5642
"Defines for %s use both 'choice' and 'interleave'\n",
5646
xmlRngPErr(ctxt, define->node, XML_RNGP_UNKNOWN_COMBINE,
5647
"Defines for %s use unknown combine value '%s''\n",
5655
xmlRngPErr(ctxt, define->node, XML_RNGP_NEED_COMBINE,
5656
"Some defines for %s needs the combine attribute\n",
5661
cur = cur->nextHash;
5664
xmlGenericError(xmlGenericErrorContext,
5665
"xmlRelaxNGCheckCombine(): merging %s defines: %d\n",
5666
name, choiceOrInterleave);
5668
if (choiceOrInterleave == -1)
5669
choiceOrInterleave = 0;
5670
cur = xmlRelaxNGNewDefine(ctxt, define->node);
5673
if (choiceOrInterleave == 0)
5674
cur->type = XML_RELAXNG_INTERLEAVE;
5676
cur->type = XML_RELAXNG_CHOICE;
5679
while (tmp != NULL) {
5680
if (tmp->content != NULL) {
5681
if (tmp->content->next != NULL) {
5683
* we need first to create a wrapper.
5685
tmp2 = xmlRelaxNGNewDefine(ctxt, tmp->content->node);
5688
tmp2->type = XML_RELAXNG_GROUP;
5689
tmp2->content = tmp->content;
5691
tmp2 = tmp->content;
5694
cur->content = tmp2;
5701
tmp = tmp->nextHash;
5703
define->content = cur;
5704
if (choiceOrInterleave == 0) {
5705
if (ctxt->interleaves == NULL)
5706
ctxt->interleaves = xmlHashCreate(10);
5707
if (ctxt->interleaves == NULL) {
5708
xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5709
"Failed to create interleaves hash table\n", NULL,
5714
snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5715
if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5717
xmlRngPErr(ctxt, define->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5718
"Failed to add %s to hash table\n",
5719
(const xmlChar *) tmpname, NULL);
5726
* xmlRelaxNGCombineStart:
5727
* @ctxt: a Relax-NG parser context
5728
* @grammar: the grammar
5730
* Applies the 4.17. combine rule for all the start
5731
* element of a given grammar.
5734
xmlRelaxNGCombineStart(xmlRelaxNGParserCtxtPtr ctxt,
5735
xmlRelaxNGGrammarPtr grammar)
5737
xmlRelaxNGDefinePtr starts;
5739
int choiceOrInterleave = -1;
5741
xmlRelaxNGDefinePtr cur;
5743
starts = grammar->start;
5744
if ((starts == NULL) || (starts->next == NULL))
5747
while (cur != NULL) {
5748
if ((cur->node == NULL) || (cur->node->parent == NULL) ||
5749
(!xmlStrEqual(cur->node->parent->name, BAD_CAST "start"))) {
5751
xmlRngPErr(ctxt, cur->node, XML_RNGP_START_MISSING,
5752
"Internal error: start element not found\n", NULL,
5755
combine = xmlGetProp(cur->node->parent, BAD_CAST "combine");
5758
if (combine != NULL) {
5759
if (xmlStrEqual(combine, BAD_CAST "choice")) {
5760
if (choiceOrInterleave == -1)
5761
choiceOrInterleave = 1;
5762
else if (choiceOrInterleave == 0) {
5763
xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5764
"<start> use both 'choice' and 'interleave'\n",
5767
} else if (xmlStrEqual(combine, BAD_CAST "interleave")) {
5768
if (choiceOrInterleave == -1)
5769
choiceOrInterleave = 0;
5770
else if (choiceOrInterleave == 1) {
5771
xmlRngPErr(ctxt, cur->node, XML_RNGP_START_CHOICE_AND_INTERLEAVE,
5772
"<start> use both 'choice' and 'interleave'\n",
5776
xmlRngPErr(ctxt, cur->node, XML_RNGP_UNKNOWN_COMBINE,
5777
"<start> uses unknown combine value '%s''\n",
5785
xmlRngPErr(ctxt, cur->node, XML_RNGP_NEED_COMBINE,
5786
"Some <start> element miss the combine attribute\n",
5794
xmlGenericError(xmlGenericErrorContext,
5795
"xmlRelaxNGCombineStart(): merging <start>: %d\n",
5796
choiceOrInterleave);
5798
if (choiceOrInterleave == -1)
5799
choiceOrInterleave = 0;
5800
cur = xmlRelaxNGNewDefine(ctxt, starts->node);
5803
if (choiceOrInterleave == 0)
5804
cur->type = XML_RELAXNG_INTERLEAVE;
5806
cur->type = XML_RELAXNG_CHOICE;
5807
cur->content = grammar->start;
5808
grammar->start = cur;
5809
if (choiceOrInterleave == 0) {
5810
if (ctxt->interleaves == NULL)
5811
ctxt->interleaves = xmlHashCreate(10);
5812
if (ctxt->interleaves == NULL) {
5813
xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5814
"Failed to create interleaves hash table\n", NULL,
5819
snprintf(tmpname, 32, "interleave%d", ctxt->nbInterleaves++);
5820
if (xmlHashAddEntry(ctxt->interleaves, BAD_CAST tmpname, cur) <
5822
xmlRngPErr(ctxt, cur->node, XML_RNGP_INTERLEAVE_CREATE_FAILED,
5823
"Failed to add %s to hash table\n",
5824
(const xmlChar *) tmpname, NULL);
5831
* xmlRelaxNGCheckCycles:
5832
* @ctxt: a Relax-NG parser context
5833
* @nodes: grammar children nodes
5834
* @depth: the counter
5838
* Returns 0 if check passed, and -1 in case of error
5841
xmlRelaxNGCheckCycles(xmlRelaxNGParserCtxtPtr ctxt,
5842
xmlRelaxNGDefinePtr cur, int depth)
5846
while ((ret == 0) && (cur != NULL)) {
5847
if ((cur->type == XML_RELAXNG_REF) ||
5848
(cur->type == XML_RELAXNG_PARENTREF)) {
5849
if (cur->depth == -1) {
5851
ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5853
} else if (depth == cur->depth) {
5854
xmlRngPErr(ctxt, cur->node, XML_RNGP_REF_CYCLE,
5855
"Detected a cycle in %s references\n",
5859
} else if (cur->type == XML_RELAXNG_ELEMENT) {
5860
ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth + 1);
5862
ret = xmlRelaxNGCheckCycles(ctxt, cur->content, depth);
5870
* xmlRelaxNGTryUnlink:
5871
* @ctxt: a Relax-NG parser context
5872
* @cur: the definition to unlink
5873
* @parent: the parent definition
5874
* @prev: the previous sibling definition
5876
* Try to unlink a definition. If not possble make it a NOOP
5878
* Returns the new prev definition
5880
static xmlRelaxNGDefinePtr
5881
xmlRelaxNGTryUnlink(xmlRelaxNGParserCtxtPtr ctxt ATTRIBUTE_UNUSED,
5882
xmlRelaxNGDefinePtr cur,
5883
xmlRelaxNGDefinePtr parent, xmlRelaxNGDefinePtr prev)
5886
prev->next = cur->next;
5888
if (parent != NULL) {
5889
if (parent->content == cur)
5890
parent->content = cur->next;
5891
else if (parent->attrs == cur)
5892
parent->attrs = cur->next;
5893
else if (parent->nameClass == cur)
5894
parent->nameClass = cur->next;
5896
cur->type = XML_RELAXNG_NOOP;
5904
* xmlRelaxNGSimplify:
5905
* @ctxt: a Relax-NG parser context
5906
* @nodes: grammar children nodes
5908
* Check for simplification of empty and notAllowed
5911
xmlRelaxNGSimplify(xmlRelaxNGParserCtxtPtr ctxt,
5912
xmlRelaxNGDefinePtr cur, xmlRelaxNGDefinePtr parent)
5914
xmlRelaxNGDefinePtr prev = NULL;
5916
while (cur != NULL) {
5917
if ((cur->type == XML_RELAXNG_REF) ||
5918
(cur->type == XML_RELAXNG_PARENTREF)) {
5919
if (cur->depth != -3) {
5921
xmlRelaxNGSimplify(ctxt, cur->content, cur);
5923
} else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
5924
cur->parent = parent;
5925
if ((parent != NULL) &&
5926
((parent->type == XML_RELAXNG_ATTRIBUTE) ||
5927
(parent->type == XML_RELAXNG_LIST) ||
5928
(parent->type == XML_RELAXNG_GROUP) ||
5929
(parent->type == XML_RELAXNG_INTERLEAVE) ||
5930
(parent->type == XML_RELAXNG_ONEORMORE) ||
5931
(parent->type == XML_RELAXNG_ZEROORMORE))) {
5932
parent->type = XML_RELAXNG_NOT_ALLOWED;
5935
if ((parent != NULL) && (parent->type == XML_RELAXNG_CHOICE)) {
5936
prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5939
} else if (cur->type == XML_RELAXNG_EMPTY) {
5940
cur->parent = parent;
5941
if ((parent != NULL) &&
5942
((parent->type == XML_RELAXNG_ONEORMORE) ||
5943
(parent->type == XML_RELAXNG_ZEROORMORE))) {
5944
parent->type = XML_RELAXNG_EMPTY;
5947
if ((parent != NULL) &&
5948
((parent->type == XML_RELAXNG_GROUP) ||
5949
(parent->type == XML_RELAXNG_INTERLEAVE))) {
5950
prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
5954
cur->parent = parent;
5955
if (cur->content != NULL)
5956
xmlRelaxNGSimplify(ctxt, cur->content, cur);
5957
if ((cur->type != XML_RELAXNG_VALUE) && (cur->attrs != NULL))
5958
xmlRelaxNGSimplify(ctxt, cur->attrs, cur);
5959
if (cur->nameClass != NULL)
5960
xmlRelaxNGSimplify(ctxt, cur->nameClass, cur);
5962
* On Elements, try to move attribute only generating rules on
5965
if (cur->type == XML_RELAXNG_ELEMENT) {
5967
xmlRelaxNGDefinePtr tmp, pre;
5969
while (cur->content != NULL) {
5971
xmlRelaxNGGenerateAttributes(ctxt, cur->content);
5972
if (attronly == 1) {
5974
* migrate cur->content to attrs
5977
cur->content = tmp->next;
5978
tmp->next = cur->attrs;
5982
* cur->content can generate elements or text
5988
while ((pre != NULL) && (pre->next != NULL)) {
5990
attronly = xmlRelaxNGGenerateAttributes(ctxt, tmp);
5991
if (attronly == 1) {
5993
* migrate tmp to attrs
5995
pre->next = tmp->next;
5996
tmp->next = cur->attrs;
6004
* This may result in a simplification
6006
if ((cur->type == XML_RELAXNG_GROUP) ||
6007
(cur->type == XML_RELAXNG_INTERLEAVE)) {
6008
if (cur->content == NULL)
6009
cur->type = XML_RELAXNG_EMPTY;
6010
else if (cur->content->next == NULL) {
6011
if ((parent == NULL) && (prev == NULL)) {
6012
cur->type = XML_RELAXNG_NOOP;
6013
} else if (prev == NULL) {
6014
parent->content = cur->content;
6015
cur->content->next = cur->next;
6018
cur->content->next = cur->next;
6019
prev->next = cur->content;
6025
* the current node may have been transformed back
6027
if ((cur->type == XML_RELAXNG_EXCEPT) &&
6028
(cur->content != NULL) &&
6029
(cur->content->type == XML_RELAXNG_NOT_ALLOWED)) {
6030
prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6031
} else if (cur->type == XML_RELAXNG_NOT_ALLOWED) {
6032
if ((parent != NULL) &&
6033
((parent->type == XML_RELAXNG_ATTRIBUTE) ||
6034
(parent->type == XML_RELAXNG_LIST) ||
6035
(parent->type == XML_RELAXNG_GROUP) ||
6036
(parent->type == XML_RELAXNG_INTERLEAVE) ||
6037
(parent->type == XML_RELAXNG_ONEORMORE) ||
6038
(parent->type == XML_RELAXNG_ZEROORMORE))) {
6039
parent->type = XML_RELAXNG_NOT_ALLOWED;
6042
if ((parent != NULL) &&
6043
(parent->type == XML_RELAXNG_CHOICE)) {
6044
prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6047
} else if (cur->type == XML_RELAXNG_EMPTY) {
6048
if ((parent != NULL) &&
6049
((parent->type == XML_RELAXNG_ONEORMORE) ||
6050
(parent->type == XML_RELAXNG_ZEROORMORE))) {
6051
parent->type = XML_RELAXNG_EMPTY;
6054
if ((parent != NULL) &&
6055
((parent->type == XML_RELAXNG_GROUP) ||
6056
(parent->type == XML_RELAXNG_INTERLEAVE) ||
6057
(parent->type == XML_RELAXNG_CHOICE))) {
6058
prev = xmlRelaxNGTryUnlink(ctxt, cur, parent, prev);
6070
* xmlRelaxNGGroupContentType:
6071
* @ct1: the first content type
6072
* @ct2: the second content type
6074
* Try to group 2 content types
6076
* Returns the content type
6078
static xmlRelaxNGContentType
6079
xmlRelaxNGGroupContentType(xmlRelaxNGContentType ct1,
6080
xmlRelaxNGContentType ct2)
6082
if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6083
(ct2 == XML_RELAXNG_CONTENT_ERROR))
6084
return (XML_RELAXNG_CONTENT_ERROR);
6085
if (ct1 == XML_RELAXNG_CONTENT_EMPTY)
6087
if (ct2 == XML_RELAXNG_CONTENT_EMPTY)
6089
if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) &&
6090
(ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6091
return (XML_RELAXNG_CONTENT_COMPLEX);
6092
return (XML_RELAXNG_CONTENT_ERROR);
6096
* xmlRelaxNGMaxContentType:
6097
* @ct1: the first content type
6098
* @ct2: the second content type
6100
* Compute the max content-type
6102
* Returns the content type
6104
static xmlRelaxNGContentType
6105
xmlRelaxNGMaxContentType(xmlRelaxNGContentType ct1,
6106
xmlRelaxNGContentType ct2)
6108
if ((ct1 == XML_RELAXNG_CONTENT_ERROR) ||
6109
(ct2 == XML_RELAXNG_CONTENT_ERROR))
6110
return (XML_RELAXNG_CONTENT_ERROR);
6111
if ((ct1 == XML_RELAXNG_CONTENT_SIMPLE) ||
6112
(ct2 == XML_RELAXNG_CONTENT_SIMPLE))
6113
return (XML_RELAXNG_CONTENT_SIMPLE);
6114
if ((ct1 == XML_RELAXNG_CONTENT_COMPLEX) ||
6115
(ct2 == XML_RELAXNG_CONTENT_COMPLEX))
6116
return (XML_RELAXNG_CONTENT_COMPLEX);
6117
return (XML_RELAXNG_CONTENT_EMPTY);
6121
* xmlRelaxNGCheckRules:
6122
* @ctxt: a Relax-NG parser context
6123
* @cur: the current definition
6124
* @flags: some accumulated flags
6125
* @ptype: the parent type
6127
* Check for rules in section 7.1 and 7.2
6129
* Returns the content type of @cur
6131
static xmlRelaxNGContentType
6132
xmlRelaxNGCheckRules(xmlRelaxNGParserCtxtPtr ctxt,
6133
xmlRelaxNGDefinePtr cur, int flags,
6134
xmlRelaxNGType ptype)
6137
xmlRelaxNGContentType ret, tmp, val = XML_RELAXNG_CONTENT_EMPTY;
6139
while (cur != NULL) {
6140
ret = XML_RELAXNG_CONTENT_EMPTY;
6141
if ((cur->type == XML_RELAXNG_REF) ||
6142
(cur->type == XML_RELAXNG_PARENTREF)) {
6144
* This should actually be caught by list//element(ref) at the
6145
* element boundaries, c.f. Bug #159968 local refs are dropped
6149
if (flags & XML_RELAXNG_IN_LIST) {
6150
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_REF,
6151
"Found forbidden pattern list//ref\n", NULL,
6155
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6156
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_REF,
6157
"Found forbidden pattern data/except//ref\n",
6160
if (cur->depth > -4) {
6162
ret = xmlRelaxNGCheckRules(ctxt, cur->content,
6164
cur->depth = ret - 15;
6165
} else if (cur->depth == -4) {
6166
ret = XML_RELAXNG_CONTENT_COMPLEX;
6168
ret = (xmlRelaxNGContentType) (cur->depth + 15);
6170
} else if (cur->type == XML_RELAXNG_ELEMENT) {
6172
* The 7.3 Attribute derivation rule for groups is plugged there
6174
xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6175
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6176
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ELEM,
6177
"Found forbidden pattern data/except//element(ref)\n",
6180
if (flags & XML_RELAXNG_IN_LIST) {
6181
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ELEM,
6182
"Found forbidden pattern list//element(ref)\n",
6185
if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6186
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6187
"Found forbidden pattern attribute//element(ref)\n",
6190
if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6191
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ELEM,
6192
"Found forbidden pattern attribute//element(ref)\n",
6196
* reset since in the simple form elements are only child
6201
xmlRelaxNGCheckRules(ctxt, cur->attrs, nflags, cur->type);
6202
if (ret != XML_RELAXNG_CONTENT_EMPTY) {
6203
xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_EMPTY,
6204
"Element %s attributes have a content type error\n",
6208
xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6210
if (ret == XML_RELAXNG_CONTENT_ERROR) {
6211
xmlRngPErr(ctxt, cur->node, XML_RNGP_ELEM_CONTENT_ERROR,
6212
"Element %s has a content type error\n",
6215
ret = XML_RELAXNG_CONTENT_COMPLEX;
6217
} else if (cur->type == XML_RELAXNG_ATTRIBUTE) {
6218
if (flags & XML_RELAXNG_IN_ATTRIBUTE) {
6219
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ATTR_ATTR,
6220
"Found forbidden pattern attribute//attribute\n",
6223
if (flags & XML_RELAXNG_IN_LIST) {
6224
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_ATTR,
6225
"Found forbidden pattern list//attribute\n",
6228
if (flags & XML_RELAXNG_IN_OOMGROUP) {
6229
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_GROUP_ATTR,
6230
"Found forbidden pattern oneOrMore//group//attribute\n",
6233
if (flags & XML_RELAXNG_IN_OOMINTERLEAVE) {
6234
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_ONEMORE_INTERLEAVE_ATTR,
6235
"Found forbidden pattern oneOrMore//interleave//attribute\n",
6238
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6239
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ATTR,
6240
"Found forbidden pattern data/except//attribute\n",
6243
if (flags & XML_RELAXNG_IN_START) {
6244
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ATTR,
6245
"Found forbidden pattern start//attribute\n",
6248
if ((!(flags & XML_RELAXNG_IN_ONEORMORE))
6249
&& (cur->name == NULL)) {
6250
if (cur->ns == NULL) {
6251
xmlRngPErr(ctxt, cur->node, XML_RNGP_ANYNAME_ATTR_ANCESTOR,
6252
"Found anyName attribute without oneOrMore ancestor\n",
6255
xmlRngPErr(ctxt, cur->node, XML_RNGP_NSNAME_ATTR_ANCESTOR,
6256
"Found nsName attribute without oneOrMore ancestor\n",
6260
nflags = flags | XML_RELAXNG_IN_ATTRIBUTE;
6261
xmlRelaxNGCheckRules(ctxt, cur->content, nflags, cur->type);
6262
ret = XML_RELAXNG_CONTENT_EMPTY;
6263
} else if ((cur->type == XML_RELAXNG_ONEORMORE) ||
6264
(cur->type == XML_RELAXNG_ZEROORMORE)) {
6265
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6266
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_ONEMORE,
6267
"Found forbidden pattern data/except//oneOrMore\n",
6270
if (flags & XML_RELAXNG_IN_START) {
6271
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_ONEMORE,
6272
"Found forbidden pattern start//oneOrMore\n",
6275
nflags = flags | XML_RELAXNG_IN_ONEORMORE;
6277
xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6279
ret = xmlRelaxNGGroupContentType(ret, ret);
6280
} else if (cur->type == XML_RELAXNG_LIST) {
6281
if (flags & XML_RELAXNG_IN_LIST) {
6282
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_LIST,
6283
"Found forbidden pattern list//list\n", NULL,
6286
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6287
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_LIST,
6288
"Found forbidden pattern data/except//list\n",
6291
if (flags & XML_RELAXNG_IN_START) {
6292
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_LIST,
6293
"Found forbidden pattern start//list\n", NULL,
6296
nflags = flags | XML_RELAXNG_IN_LIST;
6298
xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6300
} else if (cur->type == XML_RELAXNG_GROUP) {
6301
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6302
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_GROUP,
6303
"Found forbidden pattern data/except//group\n",
6306
if (flags & XML_RELAXNG_IN_START) {
6307
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_GROUP,
6308
"Found forbidden pattern start//group\n", NULL,
6311
if (flags & XML_RELAXNG_IN_ONEORMORE)
6312
nflags = flags | XML_RELAXNG_IN_OOMGROUP;
6316
xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6319
* The 7.3 Attribute derivation rule for groups is plugged there
6321
xmlRelaxNGCheckGroupAttrs(ctxt, cur);
6322
} else if (cur->type == XML_RELAXNG_INTERLEAVE) {
6323
if (flags & XML_RELAXNG_IN_LIST) {
6324
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_INTERLEAVE,
6325
"Found forbidden pattern list//interleave\n",
6328
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6329
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6330
"Found forbidden pattern data/except//interleave\n",
6333
if (flags & XML_RELAXNG_IN_START) {
6334
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_INTERLEAVE,
6335
"Found forbidden pattern start//interleave\n",
6338
if (flags & XML_RELAXNG_IN_ONEORMORE)
6339
nflags = flags | XML_RELAXNG_IN_OOMINTERLEAVE;
6343
xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6345
} else if (cur->type == XML_RELAXNG_EXCEPT) {
6346
if ((cur->parent != NULL) &&
6347
(cur->parent->type == XML_RELAXNG_DATATYPE))
6348
nflags = flags | XML_RELAXNG_IN_DATAEXCEPT;
6352
xmlRelaxNGCheckRules(ctxt, cur->content, nflags,
6354
} else if (cur->type == XML_RELAXNG_DATATYPE) {
6355
if (flags & XML_RELAXNG_IN_START) {
6356
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_DATA,
6357
"Found forbidden pattern start//data\n", NULL,
6360
xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6361
ret = XML_RELAXNG_CONTENT_SIMPLE;
6362
} else if (cur->type == XML_RELAXNG_VALUE) {
6363
if (flags & XML_RELAXNG_IN_START) {
6364
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_VALUE,
6365
"Found forbidden pattern start//value\n", NULL,
6368
xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6369
ret = XML_RELAXNG_CONTENT_SIMPLE;
6370
} else if (cur->type == XML_RELAXNG_TEXT) {
6371
if (flags & XML_RELAXNG_IN_LIST) {
6372
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_LIST_TEXT,
6373
"Found forbidden pattern list//text\n", NULL,
6376
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6377
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_TEXT,
6378
"Found forbidden pattern data/except//text\n",
6381
if (flags & XML_RELAXNG_IN_START) {
6382
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_TEXT,
6383
"Found forbidden pattern start//text\n", NULL,
6386
ret = XML_RELAXNG_CONTENT_COMPLEX;
6387
} else if (cur->type == XML_RELAXNG_EMPTY) {
6388
if (flags & XML_RELAXNG_IN_DATAEXCEPT) {
6389
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_DATA_EXCEPT_EMPTY,
6390
"Found forbidden pattern data/except//empty\n",
6393
if (flags & XML_RELAXNG_IN_START) {
6394
xmlRngPErr(ctxt, cur->node, XML_RNGP_PAT_START_EMPTY,
6395
"Found forbidden pattern start//empty\n", NULL,
6398
ret = XML_RELAXNG_CONTENT_EMPTY;
6399
} else if (cur->type == XML_RELAXNG_CHOICE) {
6400
xmlRelaxNGCheckChoiceDeterminism(ctxt, cur);
6402
xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6405
xmlRelaxNGCheckRules(ctxt, cur->content, flags, cur->type);
6408
if (ptype == XML_RELAXNG_GROUP) {
6409
val = xmlRelaxNGGroupContentType(val, ret);
6410
} else if (ptype == XML_RELAXNG_INTERLEAVE) {
6411
tmp = xmlRelaxNGGroupContentType(val, ret);
6412
if (tmp != XML_RELAXNG_CONTENT_ERROR)
6413
tmp = xmlRelaxNGMaxContentType(val, ret);
6414
} else if (ptype == XML_RELAXNG_CHOICE) {
6415
val = xmlRelaxNGMaxContentType(val, ret);
6416
} else if (ptype == XML_RELAXNG_LIST) {
6417
val = XML_RELAXNG_CONTENT_SIMPLE;
6418
} else if (ptype == XML_RELAXNG_EXCEPT) {
6419
if (ret == XML_RELAXNG_CONTENT_ERROR)
6420
val = XML_RELAXNG_CONTENT_ERROR;
6422
val = XML_RELAXNG_CONTENT_SIMPLE;
6424
val = xmlRelaxNGGroupContentType(val, ret);
6432
* xmlRelaxNGParseGrammar:
6433
* @ctxt: a Relax-NG parser context
6434
* @nodes: grammar children nodes
6436
* parse a Relax-NG <grammar> node
6438
* Returns the internal xmlRelaxNGGrammarPtr built or
6439
* NULL in case of error
6441
static xmlRelaxNGGrammarPtr
6442
xmlRelaxNGParseGrammar(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr nodes)
6444
xmlRelaxNGGrammarPtr ret, tmp, old;
6446
#ifdef DEBUG_GRAMMAR
6447
xmlGenericError(xmlGenericErrorContext, "Parsing a new grammar\n");
6450
ret = xmlRelaxNGNewGrammar(ctxt);
6455
* Link the new grammar in the tree
6457
ret->parent = ctxt->grammar;
6458
if (ctxt->grammar != NULL) {
6459
tmp = ctxt->grammar->children;
6461
ctxt->grammar->children = ret;
6463
while (tmp->next != NULL)
6469
old = ctxt->grammar;
6470
ctxt->grammar = ret;
6471
xmlRelaxNGParseGrammarContent(ctxt, nodes);
6472
ctxt->grammar = ret;
6473
if (ctxt->grammar == NULL) {
6474
xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_CONTENT,
6475
"Failed to parse <grammar> content\n", NULL, NULL);
6476
} else if (ctxt->grammar->start == NULL) {
6477
xmlRngPErr(ctxt, nodes, XML_RNGP_GRAMMAR_NO_START,
6478
"Element <grammar> has no <start>\n", NULL, NULL);
6482
* Apply 4.17 mergingd rules to defines and starts
6484
xmlRelaxNGCombineStart(ctxt, ret);
6485
if (ret->defs != NULL) {
6486
xmlHashScan(ret->defs, (xmlHashScanner) xmlRelaxNGCheckCombine,
6491
* link together defines and refs in this grammar
6493
if (ret->refs != NULL) {
6494
xmlHashScan(ret->refs, (xmlHashScanner) xmlRelaxNGCheckReference,
6498
ctxt->grammar = old;
6503
* xmlRelaxNGParseDocument:
6504
* @ctxt: a Relax-NG parser context
6505
* @node: the root node of the RelaxNG schema
6507
* parse a Relax-NG definition resource and build an internal
6508
* xmlRelaxNG struture which can be used to validate instances.
6510
* Returns the internal XML RelaxNG structure built or
6511
* NULL in case of error
6513
static xmlRelaxNGPtr
6514
xmlRelaxNGParseDocument(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6516
xmlRelaxNGPtr schema = NULL;
6517
const xmlChar *olddefine;
6518
xmlRelaxNGGrammarPtr old;
6520
if ((ctxt == NULL) || (node == NULL))
6523
schema = xmlRelaxNGNewRelaxNG(ctxt);
6527
olddefine = ctxt->define;
6528
ctxt->define = NULL;
6529
if (IS_RELAXNG(node, "grammar")) {
6530
schema->topgrammar = xmlRelaxNGParseGrammar(ctxt, node->children);
6532
xmlRelaxNGGrammarPtr tmp, ret;
6534
schema->topgrammar = ret = xmlRelaxNGNewGrammar(ctxt);
6535
if (schema->topgrammar == NULL) {
6539
* Link the new grammar in the tree
6541
ret->parent = ctxt->grammar;
6542
if (ctxt->grammar != NULL) {
6543
tmp = ctxt->grammar->children;
6545
ctxt->grammar->children = ret;
6547
while (tmp->next != NULL)
6552
old = ctxt->grammar;
6553
ctxt->grammar = ret;
6554
xmlRelaxNGParseStart(ctxt, node);
6556
ctxt->grammar = old;
6558
ctxt->define = olddefine;
6559
if (schema->topgrammar->start != NULL) {
6560
xmlRelaxNGCheckCycles(ctxt, schema->topgrammar->start, 0);
6561
if ((ctxt->flags & XML_RELAXNG_IN_EXTERNALREF) == 0) {
6562
xmlRelaxNGSimplify(ctxt, schema->topgrammar->start, NULL);
6563
while ((schema->topgrammar->start != NULL) &&
6564
(schema->topgrammar->start->type == XML_RELAXNG_NOOP) &&
6565
(schema->topgrammar->start->next != NULL))
6566
schema->topgrammar->start =
6567
schema->topgrammar->start->content;
6568
xmlRelaxNGCheckRules(ctxt, schema->topgrammar->start,
6569
XML_RELAXNG_IN_START, XML_RELAXNG_NOOP);
6574
xmlGenericError(xmlGenericErrorContext,
6575
"xmlRelaxNGParseDocument() failed\n");
6581
/************************************************************************
6583
* Reading RelaxNGs *
6585
************************************************************************/
6588
* xmlRelaxNGNewParserCtxt:
6589
* @URL: the location of the schema
6591
* Create an XML RelaxNGs parse context for that file/resource expected
6592
* to contain an XML RelaxNGs file.
6594
* Returns the parser context or NULL in case of error
6596
xmlRelaxNGParserCtxtPtr
6597
xmlRelaxNGNewParserCtxt(const char *URL)
6599
xmlRelaxNGParserCtxtPtr ret;
6605
(xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6607
xmlRngPErrMemory(NULL, "building parser\n");
6610
memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6611
ret->URL = xmlStrdup((const xmlChar *) URL);
6612
ret->error = xmlGenericError;
6613
ret->userData = xmlGenericErrorContext;
6618
* xmlRelaxNGNewMemParserCtxt:
6619
* @buffer: a pointer to a char array containing the schemas
6620
* @size: the size of the array
6622
* Create an XML RelaxNGs parse context for that memory buffer expected
6623
* to contain an XML RelaxNGs file.
6625
* Returns the parser context or NULL in case of error
6627
xmlRelaxNGParserCtxtPtr
6628
xmlRelaxNGNewMemParserCtxt(const char *buffer, int size)
6630
xmlRelaxNGParserCtxtPtr ret;
6632
if ((buffer == NULL) || (size <= 0))
6636
(xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6638
xmlRngPErrMemory(NULL, "building parser\n");
6641
memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6642
ret->buffer = buffer;
6644
ret->error = xmlGenericError;
6645
ret->userData = xmlGenericErrorContext;
6650
* xmlRelaxNGNewDocParserCtxt:
6651
* @doc: a preparsed document tree
6653
* Create an XML RelaxNGs parser context for that document.
6654
* Note: since the process of compiling a RelaxNG schemas modifies the
6655
* document, the @doc parameter is duplicated internally.
6657
* Returns the parser context or NULL in case of error
6659
xmlRelaxNGParserCtxtPtr
6660
xmlRelaxNGNewDocParserCtxt(xmlDocPtr doc)
6662
xmlRelaxNGParserCtxtPtr ret;
6667
copy = xmlCopyDoc(doc, 1);
6672
(xmlRelaxNGParserCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGParserCtxt));
6674
xmlRngPErrMemory(NULL, "building parser\n");
6677
memset(ret, 0, sizeof(xmlRelaxNGParserCtxt));
6678
ret->document = copy;
6680
ret->userData = xmlGenericErrorContext;
6685
* xmlRelaxNGFreeParserCtxt:
6686
* @ctxt: the schema parser context
6688
* Free the resources associated to the schema parser context
6691
xmlRelaxNGFreeParserCtxt(xmlRelaxNGParserCtxtPtr ctxt)
6695
if (ctxt->URL != NULL)
6697
if (ctxt->doc != NULL)
6698
xmlRelaxNGFreeDocument(ctxt->doc);
6699
if (ctxt->interleaves != NULL)
6700
xmlHashFree(ctxt->interleaves, NULL);
6701
if (ctxt->documents != NULL)
6702
xmlRelaxNGFreeDocumentList(ctxt->documents);
6703
if (ctxt->includes != NULL)
6704
xmlRelaxNGFreeIncludeList(ctxt->includes);
6705
if (ctxt->docTab != NULL)
6706
xmlFree(ctxt->docTab);
6707
if (ctxt->incTab != NULL)
6708
xmlFree(ctxt->incTab);
6709
if (ctxt->defTab != NULL) {
6712
for (i = 0; i < ctxt->defNr; i++)
6713
xmlRelaxNGFreeDefine(ctxt->defTab[i]);
6714
xmlFree(ctxt->defTab);
6716
if ((ctxt->document != NULL) && (ctxt->freedoc))
6717
xmlFreeDoc(ctxt->document);
6722
* xmlRelaxNGNormExtSpace:
6725
* Removes the leading and ending spaces of the value
6726
* The string is modified "in situ"
6729
xmlRelaxNGNormExtSpace(xmlChar * value)
6731
xmlChar *start = value;
6732
xmlChar *cur = value;
6737
while (IS_BLANK_CH(*cur))
6741
while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6746
while (IS_BLANK_CH(*cur))
6755
while ((*cur != 0) && (!IS_BLANK_CH(*cur)))
6761
/* don't try to normalize the inner spaces */
6762
while (IS_BLANK_CH(*cur))
6774
* xmlRelaxNGCleanupAttributes:
6775
* @ctxt: a Relax-NG parser context
6776
* @node: a Relax-NG node
6778
* Check all the attributes on the given node
6781
xmlRelaxNGCleanupAttributes(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr node)
6783
xmlAttrPtr cur, next;
6785
cur = node->properties;
6786
while (cur != NULL) {
6788
if ((cur->ns == NULL) ||
6789
(xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6790
if (xmlStrEqual(cur->name, BAD_CAST "name")) {
6791
if ((!xmlStrEqual(node->name, BAD_CAST "element")) &&
6792
(!xmlStrEqual(node->name, BAD_CAST "attribute")) &&
6793
(!xmlStrEqual(node->name, BAD_CAST "ref")) &&
6794
(!xmlStrEqual(node->name, BAD_CAST "parentRef")) &&
6795
(!xmlStrEqual(node->name, BAD_CAST "param")) &&
6796
(!xmlStrEqual(node->name, BAD_CAST "define"))) {
6797
xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6798
"Attribute %s is not allowed on %s\n",
6799
cur->name, node->name);
6801
} else if (xmlStrEqual(cur->name, BAD_CAST "type")) {
6802
if ((!xmlStrEqual(node->name, BAD_CAST "value")) &&
6803
(!xmlStrEqual(node->name, BAD_CAST "data"))) {
6804
xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6805
"Attribute %s is not allowed on %s\n",
6806
cur->name, node->name);
6808
} else if (xmlStrEqual(cur->name, BAD_CAST "href")) {
6809
if ((!xmlStrEqual(node->name, BAD_CAST "externalRef")) &&
6810
(!xmlStrEqual(node->name, BAD_CAST "include"))) {
6811
xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6812
"Attribute %s is not allowed on %s\n",
6813
cur->name, node->name);
6815
} else if (xmlStrEqual(cur->name, BAD_CAST "combine")) {
6816
if ((!xmlStrEqual(node->name, BAD_CAST "start")) &&
6817
(!xmlStrEqual(node->name, BAD_CAST "define"))) {
6818
xmlRngPErr(ctxt, node, XML_RNGP_FORBIDDEN_ATTRIBUTE,
6819
"Attribute %s is not allowed on %s\n",
6820
cur->name, node->name);
6822
} else if (xmlStrEqual(cur->name, BAD_CAST "datatypeLibrary")) {
6826
val = xmlNodeListGetString(node->doc, cur->children, 1);
6829
uri = xmlParseURI((const char *) val);
6831
xmlRngPErr(ctxt, node, XML_RNGP_INVALID_URI,
6832
"Attribute %s contains invalid URI %s\n",
6835
if (uri->scheme == NULL) {
6836
xmlRngPErr(ctxt, node, XML_RNGP_URI_NOT_ABSOLUTE,
6837
"Attribute %s URI %s is not absolute\n",
6840
if (uri->fragment != NULL) {
6841
xmlRngPErr(ctxt, node, XML_RNGP_URI_FRAGMENT,
6842
"Attribute %s URI %s has a fragment ID\n",
6850
} else if (!xmlStrEqual(cur->name, BAD_CAST "ns")) {
6851
xmlRngPErr(ctxt, node, XML_RNGP_UNKNOWN_ATTRIBUTE,
6852
"Unknown attribute %s on %s\n", cur->name,
6861
* xmlRelaxNGCleanupTree:
6862
* @ctxt: a Relax-NG parser context
6863
* @root: an xmlNodePtr subtree
6865
* Cleanup the subtree from unwanted nodes for parsing, resolve
6866
* Include and externalRef lookups.
6869
xmlRelaxNGCleanupTree(xmlRelaxNGParserCtxtPtr ctxt, xmlNodePtr root)
6871
xmlNodePtr cur, delete;
6875
while (cur != NULL) {
6876
if (delete != NULL) {
6877
xmlUnlinkNode(delete);
6878
xmlFreeNode(delete);
6881
if (cur->type == XML_ELEMENT_NODE) {
6883
* Simplification 4.1. Annotations
6885
if ((cur->ns == NULL) ||
6886
(!xmlStrEqual(cur->ns->href, xmlRelaxNGNs))) {
6887
if ((cur->parent != NULL) &&
6888
(cur->parent->type == XML_ELEMENT_NODE) &&
6889
((xmlStrEqual(cur->parent->name, BAD_CAST "name")) ||
6890
(xmlStrEqual(cur->parent->name, BAD_CAST "value")) ||
6891
(xmlStrEqual(cur->parent->name, BAD_CAST "param")))) {
6892
xmlRngPErr(ctxt, cur, XML_RNGP_FOREIGN_ELEMENT,
6893
"element %s doesn't allow foreign elements\n",
6894
cur->parent->name, NULL);
6899
xmlRelaxNGCleanupAttributes(ctxt, cur);
6900
if (xmlStrEqual(cur->name, BAD_CAST "externalRef")) {
6901
xmlChar *href, *ns, *base, *URL;
6902
xmlRelaxNGDocumentPtr docu;
6906
ns = xmlGetProp(cur, BAD_CAST "ns");
6909
while ((tmp != NULL) &&
6910
(tmp->type == XML_ELEMENT_NODE)) {
6911
ns = xmlGetProp(tmp, BAD_CAST "ns");
6917
href = xmlGetProp(cur, BAD_CAST "href");
6919
xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6920
"xmlRelaxNGParse: externalRef has no href attribute\n",
6927
uri = xmlParseURI((const char *) href);
6929
xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6930
"Incorrect URI for externalRef %s\n",
6939
if (uri->fragment != NULL) {
6940
xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6941
"Fragment forbidden in URI for externalRef %s\n",
6952
base = xmlNodeGetBase(cur->doc, cur);
6953
URL = xmlBuildURI(href, base);
6955
xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
6956
"Failed to compute URL for externalRef %s\n",
6971
docu = xmlRelaxNGLoadExternalRef(ctxt, URL, ns);
6973
xmlRngPErr(ctxt, cur, XML_RNGP_EXTERNAL_REF_FAILURE,
6974
"Failed to load externalRef %s\n", URL,
6986
} else if (xmlStrEqual(cur->name, BAD_CAST "include")) {
6987
xmlChar *href, *ns, *base, *URL;
6988
xmlRelaxNGIncludePtr incl;
6991
href = xmlGetProp(cur, BAD_CAST "href");
6993
xmlRngPErr(ctxt, cur, XML_RNGP_MISSING_HREF,
6994
"xmlRelaxNGParse: include has no href attribute\n",
6999
base = xmlNodeGetBase(cur->doc, cur);
7000
URL = xmlBuildURI(href, base);
7002
xmlRngPErr(ctxt, cur, XML_RNGP_HREF_ERROR,
7003
"Failed to compute URL for include %s\n",
7016
ns = xmlGetProp(cur, BAD_CAST "ns");
7019
while ((tmp != NULL) &&
7020
(tmp->type == XML_ELEMENT_NODE)) {
7021
ns = xmlGetProp(tmp, BAD_CAST "ns");
7027
incl = xmlRelaxNGLoadInclude(ctxt, URL, cur, ns);
7031
xmlRngPErr(ctxt, cur, XML_RNGP_INCLUDE_FAILURE,
7032
"Failed to load include %s\n", URL,
7040
} else if ((xmlStrEqual(cur->name, BAD_CAST "element")) ||
7041
(xmlStrEqual(cur->name, BAD_CAST "attribute")))
7044
xmlNodePtr text = NULL;
7047
* Simplification 4.8. name attribute of element
7048
* and attribute elements
7050
name = xmlGetProp(cur, BAD_CAST "name");
7052
if (cur->children == NULL) {
7054
xmlNewChild(cur, cur->ns, BAD_CAST "name",
7059
node = xmlNewDocNode(cur->doc, cur->ns,
7060
BAD_CAST "name", NULL);
7062
xmlAddPrevSibling(cur->children, node);
7063
text = xmlNewText(name);
7064
xmlAddChild(node, text);
7069
xmlRngPErr(ctxt, cur, XML_RNGP_CREATE_FAILURE,
7070
"Failed to create a name %s element\n",
7073
xmlUnsetProp(cur, BAD_CAST "name");
7075
ns = xmlGetProp(cur, BAD_CAST "ns");
7078
xmlSetProp(text, BAD_CAST "ns", ns);
7079
/* xmlUnsetProp(cur, BAD_CAST "ns"); */
7082
} else if (xmlStrEqual(cur->name,
7083
BAD_CAST "attribute")) {
7084
xmlSetProp(text, BAD_CAST "ns", BAD_CAST "");
7087
} else if ((xmlStrEqual(cur->name, BAD_CAST "name")) ||
7088
(xmlStrEqual(cur->name, BAD_CAST "nsName")) ||
7089
(xmlStrEqual(cur->name, BAD_CAST "value"))) {
7091
* Simplification 4.8. name attribute of element
7092
* and attribute elements
7094
if (xmlHasProp(cur, BAD_CAST "ns") == NULL) {
7099
while ((node != NULL) &&
7100
(node->type == XML_ELEMENT_NODE)) {
7101
ns = xmlGetProp(node, BAD_CAST "ns");
7105
node = node->parent;
7108
xmlSetProp(cur, BAD_CAST "ns", BAD_CAST "");
7110
xmlSetProp(cur, BAD_CAST "ns", ns);
7114
if (xmlStrEqual(cur->name, BAD_CAST "name")) {
7115
xmlChar *name, *local, *prefix;
7118
* Simplification: 4.10. QNames
7120
name = xmlNodeGetContent(cur);
7122
local = xmlSplitQName2(name, &prefix);
7123
if (local != NULL) {
7126
ns = xmlSearchNs(cur->doc, cur, prefix);
7128
xmlRngPErr(ctxt, cur,
7129
XML_RNGP_PREFIX_UNDEFINED,
7130
"xmlRelaxNGParse: no namespace for prefix %s\n",
7133
xmlSetProp(cur, BAD_CAST "ns",
7135
xmlNodeSetContent(cur, local);
7146
if (xmlStrEqual(cur->name, BAD_CAST "nsName")) {
7147
if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7148
xmlRngPErr(ctxt, cur,
7149
XML_RNGP_PAT_NSNAME_EXCEPT_NSNAME,
7150
"Found nsName/except//nsName forbidden construct\n",
7154
} else if ((xmlStrEqual(cur->name, BAD_CAST "except")) &&
7156
int oldflags = ctxt->flags;
7161
if ((cur->parent != NULL) &&
7163
(cur->parent->name, BAD_CAST "anyName"))) {
7164
ctxt->flags |= XML_RELAXNG_IN_ANYEXCEPT;
7165
xmlRelaxNGCleanupTree(ctxt, cur);
7166
ctxt->flags = oldflags;
7168
} else if ((cur->parent != NULL) &&
7170
(cur->parent->name, BAD_CAST "nsName"))) {
7171
ctxt->flags |= XML_RELAXNG_IN_NSEXCEPT;
7172
xmlRelaxNGCleanupTree(ctxt, cur);
7173
ctxt->flags = oldflags;
7176
} else if (xmlStrEqual(cur->name, BAD_CAST "anyName")) {
7180
if (ctxt->flags & XML_RELAXNG_IN_ANYEXCEPT) {
7181
xmlRngPErr(ctxt, cur,
7182
XML_RNGP_PAT_ANYNAME_EXCEPT_ANYNAME,
7183
"Found anyName/except//anyName forbidden construct\n",
7185
} else if (ctxt->flags & XML_RELAXNG_IN_NSEXCEPT) {
7186
xmlRngPErr(ctxt, cur,
7187
XML_RNGP_PAT_NSNAME_EXCEPT_ANYNAME,
7188
"Found nsName/except//anyName forbidden construct\n",
7193
* Thisd is not an else since "include" is transformed
7196
if (xmlStrEqual(cur->name, BAD_CAST "div")) {
7198
xmlNodePtr child, ins, tmp;
7201
* implements rule 4.11
7204
ns = xmlGetProp(cur, BAD_CAST "ns");
7206
child = cur->children;
7208
while (child != NULL) {
7210
if (!xmlHasProp(child, BAD_CAST "ns")) {
7211
xmlSetProp(child, BAD_CAST "ns", ns);
7215
xmlUnlinkNode(child);
7216
ins = xmlAddNextSibling(ins, child);
7222
* Since we are about to delete cur, if it's nsDef is non-NULL we
7223
* need to preserve it (it contains the ns definitions for the
7224
* children we just moved). We'll just stick it on to the end
7225
* of cur->parent's list, since it's never going to be re-serialized
7228
if (cur->nsDef != NULL) {
7229
xmlNsPtr parDef = (xmlNsPtr)&cur->parent->nsDef;
7230
while (parDef->next != NULL)
7231
parDef = parDef->next;
7232
parDef->next = cur->nsDef;
7241
* Simplification 4.2 whitespaces
7243
else if ((cur->type == XML_TEXT_NODE) ||
7244
(cur->type == XML_CDATA_SECTION_NODE)) {
7245
if (IS_BLANK_NODE(cur)) {
7246
if (cur->parent->type == XML_ELEMENT_NODE) {
7247
if ((!xmlStrEqual(cur->parent->name, BAD_CAST "value"))
7250
(cur->parent->name, BAD_CAST "param")))
7265
if (cur->children != NULL) {
7266
if ((cur->children->type != XML_ENTITY_DECL) &&
7267
(cur->children->type != XML_ENTITY_REF_NODE) &&
7268
(cur->children->type != XML_ENTITY_NODE)) {
7269
cur = cur->children;
7274
if (cur->next != NULL) {
7287
if (cur->next != NULL) {
7291
} while (cur != NULL);
7293
if (delete != NULL) {
7294
xmlUnlinkNode(delete);
7295
xmlFreeNode(delete);
7301
* xmlRelaxNGCleanupDoc:
7302
* @ctxt: a Relax-NG parser context
7303
* @doc: an xmldocPtr document pointer
7305
* Cleanup the document from unwanted nodes for parsing, resolve
7306
* Include and externalRef lookups.
7308
* Returns the cleaned up document or NULL in case of error
7311
xmlRelaxNGCleanupDoc(xmlRelaxNGParserCtxtPtr ctxt, xmlDocPtr doc)
7318
root = xmlDocGetRootElement(doc);
7320
xmlRngPErr(ctxt, (xmlNodePtr) doc, XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7324
xmlRelaxNGCleanupTree(ctxt, root);
7330
* @ctxt: a Relax-NG parser context
7332
* parse a schema definition resource and build an internal
7333
* XML Shema struture which can be used to validate instances.
7335
* Returns the internal XML RelaxNG structure built from the resource or
7336
* NULL in case of error
7339
xmlRelaxNGParse(xmlRelaxNGParserCtxtPtr ctxt)
7341
xmlRelaxNGPtr ret = NULL;
7345
xmlRelaxNGInitTypes();
7351
* First step is to parse the input document into an DOM/Infoset
7353
if (ctxt->URL != NULL) {
7354
doc = xmlReadFile((const char *) ctxt->URL,NULL,0);
7356
xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7357
"xmlRelaxNGParse: could not load %s\n", ctxt->URL,
7361
} else if (ctxt->buffer != NULL) {
7362
doc = xmlReadMemory(ctxt->buffer, ctxt->size,NULL,NULL,0);
7364
xmlRngPErr(ctxt, NULL, XML_RNGP_PARSE_ERROR,
7365
"xmlRelaxNGParse: could not parse schemas\n", NULL,
7369
doc->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7370
ctxt->URL = xmlStrdup(BAD_CAST "in_memory_buffer");
7371
} else if (ctxt->document != NULL) {
7372
doc = ctxt->document;
7374
xmlRngPErr(ctxt, NULL, XML_RNGP_EMPTY,
7375
"xmlRelaxNGParse: nothing to parse\n", NULL, NULL);
7378
ctxt->document = doc;
7381
* Some preprocessing of the document content
7383
doc = xmlRelaxNGCleanupDoc(ctxt, doc);
7385
xmlFreeDoc(ctxt->document);
7386
ctxt->document = NULL;
7391
* Then do the parsing for good
7393
root = xmlDocGetRootElement(doc);
7395
xmlRngPErr(ctxt, (xmlNodePtr) doc,
7396
XML_RNGP_EMPTY, "xmlRelaxNGParse: %s is empty\n",
7397
(ctxt->URL ? ctxt->URL : BAD_CAST "schemas"), NULL);
7399
xmlFreeDoc(ctxt->document);
7400
ctxt->document = NULL;
7403
ret = xmlRelaxNGParseDocument(ctxt, root);
7405
xmlFreeDoc(ctxt->document);
7406
ctxt->document = NULL;
7411
* Check the ref/defines links
7414
* try to preprocess interleaves
7416
if (ctxt->interleaves != NULL) {
7417
xmlHashScan(ctxt->interleaves,
7418
(xmlHashScanner) xmlRelaxNGComputeInterleaves, ctxt);
7422
* if there was a parsing error return NULL
7424
if (ctxt->nbErrors > 0) {
7425
xmlRelaxNGFree(ret);
7426
ctxt->document = NULL;
7432
* try to compile (parts of) the schemas
7434
if ((ret->topgrammar != NULL) && (ret->topgrammar->start != NULL)) {
7435
if (ret->topgrammar->start->type != XML_RELAXNG_START) {
7436
xmlRelaxNGDefinePtr def;
7438
def = xmlRelaxNGNewDefine(ctxt, NULL);
7440
def->type = XML_RELAXNG_START;
7441
def->content = ret->topgrammar->start;
7442
ret->topgrammar->start = def;
7445
xmlRelaxNGTryCompile(ctxt, ret->topgrammar->start);
7449
* Transfer the pointer for cleanup at the schema level.
7452
ctxt->document = NULL;
7453
ret->documents = ctxt->documents;
7454
ctxt->documents = NULL;
7456
ret->includes = ctxt->includes;
7457
ctxt->includes = NULL;
7458
ret->defNr = ctxt->defNr;
7459
ret->defTab = ctxt->defTab;
7460
ctxt->defTab = NULL;
7461
if (ctxt->idref == 1)
7468
* xmlRelaxNGSetParserErrors:
7469
* @ctxt: a Relax-NG validation context
7470
* @err: the error callback
7471
* @warn: the warning callback
7472
* @ctx: contextual data for the callbacks
7474
* Set the callback functions used to handle errors for a validation context
7477
xmlRelaxNGSetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7478
xmlRelaxNGValidityErrorFunc err,
7479
xmlRelaxNGValidityWarningFunc warn, void *ctx)
7484
ctxt->warning = warn;
7485
ctxt->serror = NULL;
7486
ctxt->userData = ctx;
7490
* xmlRelaxNGGetParserErrors:
7491
* @ctxt: a Relax-NG validation context
7492
* @err: the error callback result
7493
* @warn: the warning callback result
7494
* @ctx: contextual data for the callbacks result
7496
* Get the callback information used to handle errors for a validation context
7498
* Returns -1 in case of failure, 0 otherwise.
7501
xmlRelaxNGGetParserErrors(xmlRelaxNGParserCtxtPtr ctxt,
7502
xmlRelaxNGValidityErrorFunc * err,
7503
xmlRelaxNGValidityWarningFunc * warn, void **ctx)
7510
*warn = ctxt->warning;
7512
*ctx = ctxt->userData;
7517
* xmlRelaxNGSetParserStructuredErrors:
7518
* @ctxt: a Relax-NG parser context
7519
* @serror: the error callback
7520
* @ctx: contextual data for the callbacks
7522
* Set the callback functions used to handle errors for a parsing context
7525
xmlRelaxNGSetParserStructuredErrors(xmlRelaxNGParserCtxtPtr ctxt,
7526
xmlStructuredErrorFunc serror,
7531
ctxt->serror = serror;
7533
ctxt->warning = NULL;
7534
ctxt->userData = ctx;
7537
#ifdef LIBXML_OUTPUT_ENABLED
7539
/************************************************************************
7541
* Dump back a compiled form *
7543
************************************************************************/
7544
static void xmlRelaxNGDumpDefine(FILE * output,
7545
xmlRelaxNGDefinePtr define);
7548
* xmlRelaxNGDumpDefines:
7549
* @output: the file output
7550
* @defines: a list of define structures
7552
* Dump a RelaxNG structure back
7555
xmlRelaxNGDumpDefines(FILE * output, xmlRelaxNGDefinePtr defines)
7557
while (defines != NULL) {
7558
xmlRelaxNGDumpDefine(output, defines);
7559
defines = defines->next;
7564
* xmlRelaxNGDumpDefine:
7565
* @output: the file output
7566
* @define: a define structure
7568
* Dump a RelaxNG structure back
7571
xmlRelaxNGDumpDefine(FILE * output, xmlRelaxNGDefinePtr define)
7575
switch (define->type) {
7576
case XML_RELAXNG_EMPTY:
7577
fprintf(output, "<empty/>\n");
7579
case XML_RELAXNG_NOT_ALLOWED:
7580
fprintf(output, "<notAllowed/>\n");
7582
case XML_RELAXNG_TEXT:
7583
fprintf(output, "<text/>\n");
7585
case XML_RELAXNG_ELEMENT:
7586
fprintf(output, "<element>\n");
7587
if (define->name != NULL) {
7588
fprintf(output, "<name");
7589
if (define->ns != NULL)
7590
fprintf(output, " ns=\"%s\"", define->ns);
7591
fprintf(output, ">%s</name>\n", define->name);
7593
xmlRelaxNGDumpDefines(output, define->attrs);
7594
xmlRelaxNGDumpDefines(output, define->content);
7595
fprintf(output, "</element>\n");
7597
case XML_RELAXNG_LIST:
7598
fprintf(output, "<list>\n");
7599
xmlRelaxNGDumpDefines(output, define->content);
7600
fprintf(output, "</list>\n");
7602
case XML_RELAXNG_ONEORMORE:
7603
fprintf(output, "<oneOrMore>\n");
7604
xmlRelaxNGDumpDefines(output, define->content);
7605
fprintf(output, "</oneOrMore>\n");
7607
case XML_RELAXNG_ZEROORMORE:
7608
fprintf(output, "<zeroOrMore>\n");
7609
xmlRelaxNGDumpDefines(output, define->content);
7610
fprintf(output, "</zeroOrMore>\n");
7612
case XML_RELAXNG_CHOICE:
7613
fprintf(output, "<choice>\n");
7614
xmlRelaxNGDumpDefines(output, define->content);
7615
fprintf(output, "</choice>\n");
7617
case XML_RELAXNG_GROUP:
7618
fprintf(output, "<group>\n");
7619
xmlRelaxNGDumpDefines(output, define->content);
7620
fprintf(output, "</group>\n");
7622
case XML_RELAXNG_INTERLEAVE:
7623
fprintf(output, "<interleave>\n");
7624
xmlRelaxNGDumpDefines(output, define->content);
7625
fprintf(output, "</interleave>\n");
7627
case XML_RELAXNG_OPTIONAL:
7628
fprintf(output, "<optional>\n");
7629
xmlRelaxNGDumpDefines(output, define->content);
7630
fprintf(output, "</optional>\n");
7632
case XML_RELAXNG_ATTRIBUTE:
7633
fprintf(output, "<attribute>\n");
7634
xmlRelaxNGDumpDefines(output, define->content);
7635
fprintf(output, "</attribute>\n");
7637
case XML_RELAXNG_DEF:
7638
fprintf(output, "<define");
7639
if (define->name != NULL)
7640
fprintf(output, " name=\"%s\"", define->name);
7641
fprintf(output, ">\n");
7642
xmlRelaxNGDumpDefines(output, define->content);
7643
fprintf(output, "</define>\n");
7645
case XML_RELAXNG_REF:
7646
fprintf(output, "<ref");
7647
if (define->name != NULL)
7648
fprintf(output, " name=\"%s\"", define->name);
7649
fprintf(output, ">\n");
7650
xmlRelaxNGDumpDefines(output, define->content);
7651
fprintf(output, "</ref>\n");
7653
case XML_RELAXNG_PARENTREF:
7654
fprintf(output, "<parentRef");
7655
if (define->name != NULL)
7656
fprintf(output, " name=\"%s\"", define->name);
7657
fprintf(output, ">\n");
7658
xmlRelaxNGDumpDefines(output, define->content);
7659
fprintf(output, "</parentRef>\n");
7661
case XML_RELAXNG_EXTERNALREF:
7662
fprintf(output, "<externalRef>");
7663
xmlRelaxNGDumpDefines(output, define->content);
7664
fprintf(output, "</externalRef>\n");
7666
case XML_RELAXNG_DATATYPE:
7667
case XML_RELAXNG_VALUE:
7669
case XML_RELAXNG_START:
7670
case XML_RELAXNG_EXCEPT:
7671
case XML_RELAXNG_PARAM:
7673
case XML_RELAXNG_NOOP:
7674
xmlRelaxNGDumpDefines(output, define->content);
7680
* xmlRelaxNGDumpGrammar:
7681
* @output: the file output
7682
* @grammar: a grammar structure
7683
* @top: is this a top grammar
7685
* Dump a RelaxNG structure back
7688
xmlRelaxNGDumpGrammar(FILE * output, xmlRelaxNGGrammarPtr grammar, int top)
7690
if (grammar == NULL)
7693
fprintf(output, "<grammar");
7695
fprintf(output, " xmlns=\"http://relaxng.org/ns/structure/1.0\"");
7696
switch (grammar->combine) {
7697
case XML_RELAXNG_COMBINE_UNDEFINED:
7699
case XML_RELAXNG_COMBINE_CHOICE:
7700
fprintf(output, " combine=\"choice\"");
7702
case XML_RELAXNG_COMBINE_INTERLEAVE:
7703
fprintf(output, " combine=\"interleave\"");
7706
fprintf(output, " <!-- invalid combine value -->");
7708
fprintf(output, ">\n");
7709
if (grammar->start == NULL) {
7710
fprintf(output, " <!-- grammar had no start -->");
7712
fprintf(output, "<start>\n");
7713
xmlRelaxNGDumpDefine(output, grammar->start);
7714
fprintf(output, "</start>\n");
7716
/* TODO ? Dump the defines ? */
7717
fprintf(output, "</grammar>\n");
7722
* @output: the file output
7723
* @schema: a schema structure
7725
* Dump a RelaxNG structure back
7728
xmlRelaxNGDump(FILE * output, xmlRelaxNGPtr schema)
7732
if (schema == NULL) {
7733
fprintf(output, "RelaxNG empty or failed to compile\n");
7736
fprintf(output, "RelaxNG: ");
7737
if (schema->doc == NULL) {
7738
fprintf(output, "no document\n");
7739
} else if (schema->doc->URL != NULL) {
7740
fprintf(output, "%s\n", schema->doc->URL);
7742
fprintf(output, "\n");
7744
if (schema->topgrammar == NULL) {
7745
fprintf(output, "RelaxNG has no top grammar\n");
7748
xmlRelaxNGDumpGrammar(output, schema->topgrammar, 1);
7752
* xmlRelaxNGDumpTree:
7753
* @output: the file output
7754
* @schema: a schema structure
7756
* Dump the transformed RelaxNG tree.
7759
xmlRelaxNGDumpTree(FILE * output, xmlRelaxNGPtr schema)
7763
if (schema == NULL) {
7764
fprintf(output, "RelaxNG empty or failed to compile\n");
7767
if (schema->doc == NULL) {
7768
fprintf(output, "no document\n");
7770
xmlDocDump(output, schema->doc);
7773
#endif /* LIBXML_OUTPUT_ENABLED */
7775
/************************************************************************
7777
* Validation of compiled content *
7779
************************************************************************/
7780
static int xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
7781
xmlRelaxNGDefinePtr define);
7784
* xmlRelaxNGValidateCompiledCallback:
7785
* @exec: the regular expression instance
7786
* @token: the token which matched
7787
* @transdata: callback data, the define for the subelement if available
7788
@ @inputdata: callback data, the Relax NG validation context
7790
* Handle the callback and if needed validate the element children.
7793
xmlRelaxNGValidateCompiledCallback(xmlRegExecCtxtPtr exec ATTRIBUTE_UNUSED,
7794
const xmlChar * token,
7795
void *transdata, void *inputdata)
7797
xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
7798
xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
7801
#ifdef DEBUG_COMPILE
7802
xmlGenericError(xmlGenericErrorContext,
7803
"Compiled callback for: '%s'\n", token);
7806
fprintf(stderr, "callback on %s missing context\n", token);
7809
if (define == NULL) {
7810
if (token[0] == '#')
7812
fprintf(stderr, "callback on %s missing define\n", token);
7813
if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7814
ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7817
if ((ctxt == NULL) || (define == NULL)) {
7818
fprintf(stderr, "callback on %s missing info\n", token);
7819
if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
7820
ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7822
} else if (define->type != XML_RELAXNG_ELEMENT) {
7823
fprintf(stderr, "callback on %s define is not element\n", token);
7824
if (ctxt->errNo == XML_RELAXNG_OK)
7825
ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
7828
ret = xmlRelaxNGValidateDefinition(ctxt, define);
7834
* xmlRelaxNGValidateCompiledContent:
7835
* @ctxt: the RelaxNG validation context
7836
* @regexp: the regular expression as compiled
7837
* @content: list of children to test against the regexp
7839
* Validate the content model of an element or start using the regexp
7841
* Returns 0 in case of success, -1 in case of error.
7844
xmlRelaxNGValidateCompiledContent(xmlRelaxNGValidCtxtPtr ctxt,
7845
xmlRegexpPtr regexp, xmlNodePtr content)
7847
xmlRegExecCtxtPtr exec;
7852
if ((ctxt == NULL) || (regexp == NULL))
7854
oldperr = ctxt->perr;
7855
exec = xmlRegNewExecCtxt(regexp,
7856
xmlRelaxNGValidateCompiledCallback, ctxt);
7859
while (cur != NULL) {
7860
ctxt->state->seq = cur;
7861
switch (cur->type) {
7863
case XML_CDATA_SECTION_NODE:
7864
if (xmlIsBlankNode(cur))
7866
ret = xmlRegExecPushString(exec, BAD_CAST "#text", ctxt);
7868
VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG,
7872
case XML_ELEMENT_NODE:
7873
if (cur->ns != NULL) {
7874
ret = xmlRegExecPushString2(exec, cur->name,
7875
cur->ns->href, ctxt);
7877
ret = xmlRegExecPushString(exec, cur->name, ctxt);
7880
VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, cur->name);
7889
* Switch to next element
7893
ret = xmlRegExecPushString(exec, NULL, NULL);
7896
ctxt->state->seq = NULL;
7897
} else if (ret == 0) {
7899
* TODO: get some of the names needed to exit the current state of exec
7901
VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
7903
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
7904
xmlRelaxNGDumpValidError(ctxt);
7908
xmlRegFreeExecCtxt(exec);
7910
* There might be content model errors outside of the pure
7911
* regexp validation, e.g. for attribute values.
7913
if ((ret == 0) && (ctxt->perr != 0)) {
7916
ctxt->perr = oldperr;
7920
/************************************************************************
7922
* Progressive validation of when possible *
7924
************************************************************************/
7925
static int xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
7926
xmlRelaxNGDefinePtr defines);
7927
static int xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt,
7929
static void xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt);
7932
* xmlRelaxNGElemPush:
7933
* @ctxt: the validation context
7934
* @exec: the regexp runtime for the new content model
7936
* Push a new regexp for the current node content model on the stack
7938
* Returns 0 in case of success and -1 in case of error.
7941
xmlRelaxNGElemPush(xmlRelaxNGValidCtxtPtr ctxt, xmlRegExecCtxtPtr exec)
7943
if (ctxt->elemTab == NULL) {
7945
ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlMalloc(ctxt->elemMax *
7947
(xmlRegExecCtxtPtr));
7948
if (ctxt->elemTab == NULL) {
7949
xmlRngVErrMemory(ctxt, "validating\n");
7953
if (ctxt->elemNr >= ctxt->elemMax) {
7955
ctxt->elemTab = (xmlRegExecCtxtPtr *) xmlRealloc(ctxt->elemTab,
7958
(xmlRegExecCtxtPtr));
7959
if (ctxt->elemTab == NULL) {
7960
xmlRngVErrMemory(ctxt, "validating\n");
7964
ctxt->elemTab[ctxt->elemNr++] = exec;
7970
* xmlRelaxNGElemPop:
7971
* @ctxt: the validation context
7973
* Pop the regexp of the current node content model from the stack
7975
* Returns the exec or NULL if empty
7977
static xmlRegExecCtxtPtr
7978
xmlRelaxNGElemPop(xmlRelaxNGValidCtxtPtr ctxt)
7980
xmlRegExecCtxtPtr ret;
7982
if (ctxt->elemNr <= 0)
7985
ret = ctxt->elemTab[ctxt->elemNr];
7986
ctxt->elemTab[ctxt->elemNr] = NULL;
7987
if (ctxt->elemNr > 0)
7988
ctxt->elem = ctxt->elemTab[ctxt->elemNr - 1];
7995
* xmlRelaxNGValidateProgressiveCallback:
7996
* @exec: the regular expression instance
7997
* @token: the token which matched
7998
* @transdata: callback data, the define for the subelement if available
7999
@ @inputdata: callback data, the Relax NG validation context
8001
* Handle the callback and if needed validate the element children.
8002
* some of the in/out informations are passed via the context in @inputdata.
8005
xmlRelaxNGValidateProgressiveCallback(xmlRegExecCtxtPtr exec
8007
const xmlChar * token,
8008
void *transdata, void *inputdata)
8010
xmlRelaxNGValidCtxtPtr ctxt = (xmlRelaxNGValidCtxtPtr) inputdata;
8011
xmlRelaxNGDefinePtr define = (xmlRelaxNGDefinePtr) transdata;
8012
xmlRelaxNGValidStatePtr state, oldstate;
8014
int ret = 0, oldflags;
8016
#ifdef DEBUG_PROGRESSIVE
8017
xmlGenericError(xmlGenericErrorContext,
8018
"Progressive callback for: '%s'\n", token);
8021
fprintf(stderr, "callback on %s missing context\n", token);
8026
if (define == NULL) {
8027
if (token[0] == '#')
8029
fprintf(stderr, "callback on %s missing define\n", token);
8030
if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8031
ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8035
if ((ctxt == NULL) || (define == NULL)) {
8036
fprintf(stderr, "callback on %s missing info\n", token);
8037
if ((ctxt != NULL) && (ctxt->errNo == XML_RELAXNG_OK))
8038
ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8041
} else if (define->type != XML_RELAXNG_ELEMENT) {
8042
fprintf(stderr, "callback on %s define is not element\n", token);
8043
if (ctxt->errNo == XML_RELAXNG_OK)
8044
ctxt->errNo = XML_RELAXNG_ERR_INTERNAL;
8048
if (node->type != XML_ELEMENT_NODE) {
8049
VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
8050
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8051
xmlRelaxNGDumpValidError(ctxt);
8055
if (define->contModel == NULL) {
8057
* this node cannot be validated in a streamable fashion
8059
#ifdef DEBUG_PROGRESSIVE
8060
xmlGenericError(xmlGenericErrorContext,
8061
"Element '%s' validation is not streamable\n",
8065
ctxt->pdef = define;
8068
exec = xmlRegNewExecCtxt(define->contModel,
8069
xmlRelaxNGValidateProgressiveCallback, ctxt);
8074
xmlRelaxNGElemPush(ctxt, exec);
8077
* Validate the attributes part of the content.
8079
state = xmlRelaxNGNewValidState(ctxt, node);
8080
if (state == NULL) {
8084
oldstate = ctxt->state;
8085
ctxt->state = state;
8086
if (define->attrs != NULL) {
8087
ret = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
8090
VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
8093
if (ctxt->state != NULL) {
8094
ctxt->state->seq = NULL;
8095
ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
8099
xmlRelaxNGFreeValidState(ctxt, ctxt->state);
8100
} else if (ctxt->states != NULL) {
8103
oldflags = ctxt->flags;
8105
for (i = 0; i < ctxt->states->nbState; i++) {
8106
state = ctxt->states->tabState[i];
8107
ctxt->state = state;
8108
ctxt->state->seq = NULL;
8110
if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
8117
* validation error, log the message for the "best" one
8119
ctxt->flags |= FLAGS_IGNORABLE;
8120
xmlRelaxNGLogBestError(ctxt);
8122
for (i = 0; i < ctxt->states->nbState; i++) {
8123
xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[i]);
8125
xmlRelaxNGFreeStates(ctxt, ctxt->states);
8126
ctxt->states = NULL;
8127
if ((ret == 0) && (tmp == -1))
8129
ctxt->flags = oldflags;
8131
if (ctxt->pstate == -1) {
8132
if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
8133
xmlRelaxNGDumpValidError(ctxt);
8136
ctxt->state = oldstate;
8140
* xmlRelaxNGValidatePushElement:
8141
* @ctxt: the validation context
8142
* @doc: a document instance
8143
* @elem: an element instance
8145
* Push a new element start on the RelaxNG validation stack.
8147
* returns 1 if no validation problem was found or 0 if validating the
8148
* element requires a full node, and -1 in case of error.
8151
xmlRelaxNGValidatePushElement(xmlRelaxNGValidCtxtPtr ctxt,
8152
xmlDocPtr doc ATTRIBUTE_UNUSED,
8157
if ((ctxt == NULL) || (elem == NULL))
8160
#ifdef DEBUG_PROGRESSIVE
8161
xmlGenericError(xmlGenericErrorContext, "PushElem %s\n", elem->name);
8163
if (ctxt->elem == 0) {
8164
xmlRelaxNGPtr schema;
8165
xmlRelaxNGGrammarPtr grammar;
8166
xmlRegExecCtxtPtr exec;
8167
xmlRelaxNGDefinePtr define;
8169
schema = ctxt->schema;
8170
if (schema == NULL) {
8171
VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8174
grammar = schema->topgrammar;
8175
if ((grammar == NULL) || (grammar->start == NULL)) {
8176
VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
8179
define = grammar->start;
8180
if (define->contModel == NULL) {
8181
ctxt->pdef = define;
8184
exec = xmlRegNewExecCtxt(define->contModel,
8185
xmlRelaxNGValidateProgressiveCallback,
8190
xmlRelaxNGElemPush(ctxt, exec);
8194
if (elem->ns != NULL) {
8196
xmlRegExecPushString2(ctxt->elem, elem->name, elem->ns->href,
8199
ret = xmlRegExecPushString(ctxt->elem, elem->name, ctxt);
8202
VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, elem->name);
8204
if (ctxt->pstate == 0)
8206
else if (ctxt->pstate < 0)
8211
#ifdef DEBUG_PROGRESSIVE
8213
xmlGenericError(xmlGenericErrorContext, "PushElem %s failed\n",
8220
* xmlRelaxNGValidatePushCData:
8221
* @ctxt: the RelaxNG validation context
8222
* @data: some character data read
8223
* @len: the lenght of the data
8225
* check the CData parsed for validation in the current stack
8227
* returns 1 if no validation problem was found or -1 otherwise
8230
xmlRelaxNGValidatePushCData(xmlRelaxNGValidCtxtPtr ctxt,
8231
const xmlChar * data, int len ATTRIBUTE_UNUSED)
8235
if ((ctxt == NULL) || (ctxt->elem == NULL) || (data == NULL))
8238
#ifdef DEBUG_PROGRESSIVE
8239
xmlGenericError(xmlGenericErrorContext, "CDATA %s %d\n", data, len);
8242
while (*data != 0) {
8243
if (!IS_BLANK_CH(*data))
8250
ret = xmlRegExecPushString(ctxt->elem, BAD_CAST "#text", ctxt);
8252
VALID_ERR2(XML_RELAXNG_ERR_TEXTWRONG, BAD_CAST " TODO ");
8253
#ifdef DEBUG_PROGRESSIVE
8254
xmlGenericError(xmlGenericErrorContext, "CDATA failed\n");
8263
* xmlRelaxNGValidatePopElement:
8264
* @ctxt: the RelaxNG validation context
8265
* @doc: a document instance
8266
* @elem: an element instance
8268
* Pop the element end from the RelaxNG validation stack.
8270
* returns 1 if no validation problem was found or 0 otherwise
8273
xmlRelaxNGValidatePopElement(xmlRelaxNGValidCtxtPtr ctxt,
8274
xmlDocPtr doc ATTRIBUTE_UNUSED,
8278
xmlRegExecCtxtPtr exec;
8280
if ((ctxt == NULL) || (ctxt->elem == NULL) || (elem == NULL))
8282
#ifdef DEBUG_PROGRESSIVE
8283
xmlGenericError(xmlGenericErrorContext, "PopElem %s\n", elem->name);
8286
* verify that we reached a terminal state of the content model.
8288
exec = xmlRelaxNGElemPop(ctxt);
8289
ret = xmlRegExecPushString(exec, NULL, NULL);
8292
* TODO: get some of the names needed to exit the current state of exec
8294
VALID_ERR2(XML_RELAXNG_ERR_NOELEM, BAD_CAST "");
8296
} else if (ret < 0) {
8301
xmlRegFreeExecCtxt(exec);
8302
#ifdef DEBUG_PROGRESSIVE
8304
xmlGenericError(xmlGenericErrorContext, "PopElem %s failed\n",
8311
* xmlRelaxNGValidateFullElement:
8312
* @ctxt: the validation context
8313
* @doc: a document instance
8314
* @elem: an element instance
8316
* Validate a full subtree when xmlRelaxNGValidatePushElement() returned
8317
* 0 and the content of the node has been expanded.
8319
* returns 1 if no validation problem was found or -1 in case of error.
8322
xmlRelaxNGValidateFullElement(xmlRelaxNGValidCtxtPtr ctxt,
8323
xmlDocPtr doc ATTRIBUTE_UNUSED,
8327
xmlRelaxNGValidStatePtr state;
8329
if ((ctxt == NULL) || (ctxt->pdef == NULL) || (elem == NULL))
8331
#ifdef DEBUG_PROGRESSIVE
8332
xmlGenericError(xmlGenericErrorContext, "FullElem %s\n", elem->name);
8334
state = xmlRelaxNGNewValidState(ctxt, elem->parent);
8335
if (state == NULL) {
8339
ctxt->state = state;
8340
ctxt->errNo = XML_RELAXNG_OK;
8341
ret = xmlRelaxNGValidateDefinition(ctxt, ctxt->pdef);
8342
if ((ret != 0) || (ctxt->errNo != XML_RELAXNG_OK))
8346
xmlRelaxNGFreeValidState(ctxt, state);
8348
#ifdef DEBUG_PROGRESSIVE
8350
xmlGenericError(xmlGenericErrorContext, "FullElem %s failed\n",
8356
/************************************************************************
8358
* Generic interpreted validation implementation *
8360
************************************************************************/
8361
static int xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8362
xmlRelaxNGDefinePtr define);
8365
* xmlRelaxNGSkipIgnored:
8366
* @ctxt: a schema validation context
8367
* @node: the top node.
8369
* Skip ignorable nodes in that context
8371
* Returns the new sibling or NULL in case of error.
8374
xmlRelaxNGSkipIgnored(xmlRelaxNGValidCtxtPtr ctxt ATTRIBUTE_UNUSED,
8378
* TODO complete and handle entities
8380
while ((node != NULL) &&
8381
((node->type == XML_COMMENT_NODE) ||
8382
(node->type == XML_PI_NODE) ||
8383
(node->type == XML_XINCLUDE_START) ||
8384
(node->type == XML_XINCLUDE_END) ||
8385
(((node->type == XML_TEXT_NODE) ||
8386
(node->type == XML_CDATA_SECTION_NODE)) &&
8387
((ctxt->flags & FLAGS_MIXED_CONTENT) ||
8388
(IS_BLANK_NODE(node)))))) {
8395
* xmlRelaxNGNormalize:
8396
* @ctxt: a schema validation context
8397
* @str: the string to normalize
8399
* Implements the normalizeWhiteSpace( s ) function from
8400
* section 6.2.9 of the spec
8402
* Returns the new string or NULL in case of error.
8405
xmlRelaxNGNormalize(xmlRelaxNGValidCtxtPtr ctxt, const xmlChar * str)
8418
ret = (xmlChar *) xmlMallocAtomic((len + 1) * sizeof(xmlChar));
8420
xmlRngVErrMemory(ctxt, "validating\n");
8424
while (IS_BLANK_CH(*str))
8427
if (IS_BLANK_CH(*str)) {
8428
while (IS_BLANK_CH(*str))
8441
* xmlRelaxNGValidateDatatype:
8442
* @ctxt: a Relax-NG validation context
8443
* @value: the string value
8444
* @type: the datatype definition
8447
* Validate the given value against the dataype
8449
* Returns 0 if the validation succeeded or an error code.
8452
xmlRelaxNGValidateDatatype(xmlRelaxNGValidCtxtPtr ctxt,
8453
const xmlChar * value,
8454
xmlRelaxNGDefinePtr define, xmlNodePtr node)
8457
xmlRelaxNGTypeLibraryPtr lib;
8458
void *result = NULL;
8459
xmlRelaxNGDefinePtr cur;
8461
if ((define == NULL) || (define->data == NULL)) {
8464
lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8465
if (lib->check != NULL) {
8466
if ((define->attrs != NULL) &&
8467
(define->attrs->type == XML_RELAXNG_PARAM)) {
8469
lib->check(lib->data, define->name, value, &result, node);
8471
ret = lib->check(lib->data, define->name, value, NULL, node);
8476
VALID_ERR2(XML_RELAXNG_ERR_TYPE, define->name);
8477
if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8478
lib->freef(lib->data, result);
8480
} else if (ret == 1) {
8482
} else if (ret == 2) {
8483
VALID_ERR2P(XML_RELAXNG_ERR_DUPID, value);
8485
VALID_ERR3P(XML_RELAXNG_ERR_TYPEVAL, define->name, value);
8488
cur = define->attrs;
8489
while ((ret == 0) && (cur != NULL) && (cur->type == XML_RELAXNG_PARAM)) {
8490
if (lib->facet != NULL) {
8491
tmp = lib->facet(lib->data, define->name, cur->name,
8492
cur->value, value, result);
8498
if ((ret == 0) && (define->content != NULL)) {
8499
const xmlChar *oldvalue, *oldendvalue;
8501
oldvalue = ctxt->state->value;
8502
oldendvalue = ctxt->state->endvalue;
8503
ctxt->state->value = (xmlChar *) value;
8504
ctxt->state->endvalue = NULL;
8505
ret = xmlRelaxNGValidateValue(ctxt, define->content);
8506
ctxt->state->value = (xmlChar *) oldvalue;
8507
ctxt->state->endvalue = (xmlChar *) oldendvalue;
8509
if ((result != NULL) && (lib != NULL) && (lib->freef != NULL))
8510
lib->freef(lib->data, result);
8515
* xmlRelaxNGNextValue:
8516
* @ctxt: a Relax-NG validation context
8518
* Skip to the next value when validating within a list
8520
* Returns 0 if the operation succeeded or an error code.
8523
xmlRelaxNGNextValue(xmlRelaxNGValidCtxtPtr ctxt)
8527
cur = ctxt->state->value;
8528
if ((cur == NULL) || (ctxt->state->endvalue == NULL)) {
8529
ctxt->state->value = NULL;
8530
ctxt->state->endvalue = NULL;
8535
while ((cur != ctxt->state->endvalue) && (*cur == 0))
8537
if (cur == ctxt->state->endvalue)
8538
ctxt->state->value = NULL;
8540
ctxt->state->value = cur;
8545
* xmlRelaxNGValidateValueList:
8546
* @ctxt: a Relax-NG validation context
8547
* @defines: the list of definitions to verify
8549
* Validate the given set of definitions for the current value
8551
* Returns 0 if the validation succeeded or an error code.
8554
xmlRelaxNGValidateValueList(xmlRelaxNGValidCtxtPtr ctxt,
8555
xmlRelaxNGDefinePtr defines)
8559
while (defines != NULL) {
8560
ret = xmlRelaxNGValidateValue(ctxt, defines);
8563
defines = defines->next;
8569
* xmlRelaxNGValidateValue:
8570
* @ctxt: a Relax-NG validation context
8571
* @define: the definition to verify
8573
* Validate the given definition for the current value
8575
* Returns 0 if the validation succeeded or an error code.
8578
xmlRelaxNGValidateValue(xmlRelaxNGValidCtxtPtr ctxt,
8579
xmlRelaxNGDefinePtr define)
8581
int ret = 0, oldflags;
8584
value = ctxt->state->value;
8585
switch (define->type) {
8586
case XML_RELAXNG_EMPTY:{
8587
if ((value != NULL) && (value[0] != 0)) {
8590
while (IS_BLANK_CH(value[idx]))
8592
if (value[idx] != 0)
8597
case XML_RELAXNG_TEXT:
8599
case XML_RELAXNG_VALUE:{
8600
if (!xmlStrEqual(value, define->value)) {
8601
if (define->name != NULL) {
8602
xmlRelaxNGTypeLibraryPtr lib;
8604
lib = (xmlRelaxNGTypeLibraryPtr) define->data;
8605
if ((lib != NULL) && (lib->comp != NULL)) {
8606
ret = lib->comp(lib->data, define->name,
8607
define->value, define->node,
8608
(void *) define->attrs,
8609
value, ctxt->state->node);
8613
VALID_ERR2(XML_RELAXNG_ERR_TYPECMP,
8616
} else if (ret == 1) {
8622
xmlChar *nval, *nvalue;
8625
* TODO: trivial optimizations are possible by
8626
* computing at compile-time
8628
nval = xmlRelaxNGNormalize(ctxt, define->value);
8629
nvalue = xmlRelaxNGNormalize(ctxt, value);
8631
if ((nval == NULL) || (nvalue == NULL) ||
8632
(!xmlStrEqual(nval, nvalue)))
8641
xmlRelaxNGNextValue(ctxt);
8644
case XML_RELAXNG_DATATYPE:{
8645
ret = xmlRelaxNGValidateDatatype(ctxt, value, define,
8648
xmlRelaxNGNextValue(ctxt);
8652
case XML_RELAXNG_CHOICE:{
8653
xmlRelaxNGDefinePtr list = define->content;
8656
oldflags = ctxt->flags;
8657
ctxt->flags |= FLAGS_IGNORABLE;
8659
oldvalue = ctxt->state->value;
8660
while (list != NULL) {
8661
ret = xmlRelaxNGValidateValue(ctxt, list);
8665
ctxt->state->value = oldvalue;
8668
ctxt->flags = oldflags;
8670
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
8671
xmlRelaxNGDumpValidError(ctxt);
8673
if (ctxt->errNr > 0)
8674
xmlRelaxNGPopErrors(ctxt, 0);
8678
case XML_RELAXNG_LIST:{
8679
xmlRelaxNGDefinePtr list = define->content;
8680
xmlChar *oldvalue, *oldend, *val, *cur;
8686
oldvalue = ctxt->state->value;
8687
oldend = ctxt->state->endvalue;
8689
val = xmlStrdup(oldvalue);
8691
val = xmlStrdup(BAD_CAST "");
8694
VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
8699
if (IS_BLANK_CH(*cur)) {
8705
while (IS_BLANK_CH(*cur))
8711
xmlGenericError(xmlGenericErrorContext,
8712
"list value: '%s' found %d items\n",
8713
oldvalue, nb_values);
8716
ctxt->state->endvalue = cur;
8718
while ((*cur == 0) && (cur != ctxt->state->endvalue))
8721
ctxt->state->value = cur;
8723
while (list != NULL) {
8724
if (ctxt->state->value == ctxt->state->endvalue)
8725
ctxt->state->value = NULL;
8726
ret = xmlRelaxNGValidateValue(ctxt, list);
8729
xmlGenericError(xmlGenericErrorContext,
8730
"Failed to validate value: '%s' with %d rule\n",
8731
ctxt->state->value, nb_values);
8741
if ((ret == 0) && (ctxt->state->value != NULL) &&
8742
(ctxt->state->value != ctxt->state->endvalue)) {
8743
VALID_ERR2(XML_RELAXNG_ERR_LISTEXTRA,
8744
ctxt->state->value);
8748
ctxt->state->value = oldvalue;
8749
ctxt->state->endvalue = oldend;
8752
case XML_RELAXNG_ONEORMORE:
8753
ret = xmlRelaxNGValidateValueList(ctxt, define->content);
8757
/* no break on purpose */
8758
case XML_RELAXNG_ZEROORMORE:{
8759
xmlChar *cur, *temp;
8761
oldflags = ctxt->flags;
8762
ctxt->flags |= FLAGS_IGNORABLE;
8763
cur = ctxt->state->value;
8765
while ((cur != NULL) && (cur != ctxt->state->endvalue) &&
8769
xmlRelaxNGValidateValueList(ctxt, define->content);
8771
ctxt->state->value = temp;
8775
cur = ctxt->state->value;
8777
ctxt->flags = oldflags;
8778
if (ctxt->errNr > 0)
8779
xmlRelaxNGPopErrors(ctxt, 0);
8782
case XML_RELAXNG_EXCEPT:{
8783
xmlRelaxNGDefinePtr list;
8785
list = define->content;
8786
while (list != NULL) {
8787
ret = xmlRelaxNGValidateValue(ctxt, list);
8797
case XML_RELAXNG_DEF:
8798
case XML_RELAXNG_GROUP:{
8799
xmlRelaxNGDefinePtr list;
8801
list = define->content;
8802
while (list != NULL) {
8803
ret = xmlRelaxNGValidateValue(ctxt, list);
8813
case XML_RELAXNG_REF:
8814
case XML_RELAXNG_PARENTREF:
8815
ret = xmlRelaxNGValidateValue(ctxt, define->content);
8824
* xmlRelaxNGValidateValueContent:
8825
* @ctxt: a Relax-NG validation context
8826
* @defines: the list of definitions to verify
8828
* Validate the given definitions for the current value
8830
* Returns 0 if the validation succeeded or an error code.
8833
xmlRelaxNGValidateValueContent(xmlRelaxNGValidCtxtPtr ctxt,
8834
xmlRelaxNGDefinePtr defines)
8838
while (defines != NULL) {
8839
ret = xmlRelaxNGValidateValue(ctxt, defines);
8842
defines = defines->next;
8848
* xmlRelaxNGAttributeMatch:
8849
* @ctxt: a Relax-NG validation context
8850
* @define: the definition to check
8851
* @prop: the attribute
8853
* Check if the attribute matches the definition nameClass
8855
* Returns 1 if the attribute matches, 0 if no, or -1 in case of error
8858
xmlRelaxNGAttributeMatch(xmlRelaxNGValidCtxtPtr ctxt,
8859
xmlRelaxNGDefinePtr define, xmlAttrPtr prop)
8863
if (define->name != NULL) {
8864
if (!xmlStrEqual(define->name, prop->name))
8867
if (define->ns != NULL) {
8868
if (define->ns[0] == 0) {
8869
if (prop->ns != NULL)
8872
if ((prop->ns == NULL) ||
8873
(!xmlStrEqual(define->ns, prop->ns->href)))
8877
if (define->nameClass == NULL)
8879
define = define->nameClass;
8880
if (define->type == XML_RELAXNG_EXCEPT) {
8881
xmlRelaxNGDefinePtr list;
8883
list = define->content;
8884
while (list != NULL) {
8885
ret = xmlRelaxNGAttributeMatch(ctxt, list, prop);
8898
* xmlRelaxNGValidateAttribute:
8899
* @ctxt: a Relax-NG validation context
8900
* @define: the definition to verify
8902
* Validate the given attribute definition for that node
8904
* Returns 0 if the validation succeeded or an error code.
8907
xmlRelaxNGValidateAttribute(xmlRelaxNGValidCtxtPtr ctxt,
8908
xmlRelaxNGDefinePtr define)
8911
xmlChar *value, *oldvalue;
8912
xmlAttrPtr prop = NULL, tmp;
8915
if (ctxt->state->nbAttrLeft <= 0)
8917
if (define->name != NULL) {
8918
for (i = 0; i < ctxt->state->nbAttrs; i++) {
8919
tmp = ctxt->state->attrs[i];
8920
if ((tmp != NULL) && (xmlStrEqual(define->name, tmp->name))) {
8921
if ((((define->ns == NULL) || (define->ns[0] == 0)) &&
8922
(tmp->ns == NULL)) ||
8923
((tmp->ns != NULL) &&
8924
(xmlStrEqual(define->ns, tmp->ns->href)))) {
8931
value = xmlNodeListGetString(prop->doc, prop->children, 1);
8932
oldvalue = ctxt->state->value;
8933
oldseq = ctxt->state->seq;
8934
ctxt->state->seq = (xmlNodePtr) prop;
8935
ctxt->state->value = value;
8936
ctxt->state->endvalue = NULL;
8937
ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8938
if (ctxt->state->value != NULL)
8939
value = ctxt->state->value;
8942
ctxt->state->value = oldvalue;
8943
ctxt->state->seq = oldseq;
8946
* flag the attribute as processed
8948
ctxt->state->attrs[i] = NULL;
8949
ctxt->state->nbAttrLeft--;
8955
xmlGenericError(xmlGenericErrorContext,
8956
"xmlRelaxNGValidateAttribute(%s): %d\n",
8960
for (i = 0; i < ctxt->state->nbAttrs; i++) {
8961
tmp = ctxt->state->attrs[i];
8962
if ((tmp != NULL) &&
8963
(xmlRelaxNGAttributeMatch(ctxt, define, tmp) == 1)) {
8969
value = xmlNodeListGetString(prop->doc, prop->children, 1);
8970
oldvalue = ctxt->state->value;
8971
oldseq = ctxt->state->seq;
8972
ctxt->state->seq = (xmlNodePtr) prop;
8973
ctxt->state->value = value;
8974
ret = xmlRelaxNGValidateValueContent(ctxt, define->content);
8975
if (ctxt->state->value != NULL)
8976
value = ctxt->state->value;
8979
ctxt->state->value = oldvalue;
8980
ctxt->state->seq = oldseq;
8983
* flag the attribute as processed
8985
ctxt->state->attrs[i] = NULL;
8986
ctxt->state->nbAttrLeft--;
8992
if (define->ns != NULL) {
8993
xmlGenericError(xmlGenericErrorContext,
8994
"xmlRelaxNGValidateAttribute(nsName ns = %s): %d\n",
8997
xmlGenericError(xmlGenericErrorContext,
8998
"xmlRelaxNGValidateAttribute(anyName): %d\n",
9008
* xmlRelaxNGValidateAttributeList:
9009
* @ctxt: a Relax-NG validation context
9010
* @define: the list of definition to verify
9012
* Validate the given node against the list of attribute definitions
9014
* Returns 0 if the validation succeeded or an error code.
9017
xmlRelaxNGValidateAttributeList(xmlRelaxNGValidCtxtPtr ctxt,
9018
xmlRelaxNGDefinePtr defines)
9022
xmlRelaxNGDefinePtr cur;
9025
while (cur != NULL) {
9026
if (cur->type == XML_RELAXNG_ATTRIBUTE) {
9027
if (xmlRelaxNGValidateAttribute(ctxt, cur) != 0)
9036
while (cur != NULL) {
9037
if (cur->type != XML_RELAXNG_ATTRIBUTE) {
9038
if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9039
res = xmlRelaxNGValidateDefinition(ctxt, cur);
9043
VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9046
if (res == -1) /* continues on -2 */
9056
* xmlRelaxNGNodeMatchesList:
9058
* @list: a NULL terminated array of definitions
9060
* Check if a node can be matched by one of the definitions
9062
* Returns 1 if matches 0 otherwise
9065
xmlRelaxNGNodeMatchesList(xmlNodePtr node, xmlRelaxNGDefinePtr * list)
9067
xmlRelaxNGDefinePtr cur;
9070
if ((node == NULL) || (list == NULL))
9074
while (cur != NULL) {
9075
if ((node->type == XML_ELEMENT_NODE) &&
9076
(cur->type == XML_RELAXNG_ELEMENT)) {
9077
tmp = xmlRelaxNGElementMatch(NULL, cur, node);
9080
} else if (((node->type == XML_TEXT_NODE) ||
9081
(node->type == XML_CDATA_SECTION_NODE)) &&
9082
(cur->type == XML_RELAXNG_TEXT)) {
9091
* xmlRelaxNGValidateInterleave:
9092
* @ctxt: a Relax-NG validation context
9093
* @define: the definition to verify
9095
* Validate an interleave definition for a node.
9097
* Returns 0 if the validation succeeded or an error code.
9100
xmlRelaxNGValidateInterleave(xmlRelaxNGValidCtxtPtr ctxt,
9101
xmlRelaxNGDefinePtr define)
9103
int ret = 0, i, nbgroups;
9104
int errNr = ctxt->errNr;
9107
xmlRelaxNGValidStatePtr oldstate;
9108
xmlRelaxNGPartitionPtr partitions;
9109
xmlRelaxNGInterleaveGroupPtr group = NULL;
9110
xmlNodePtr cur, start, last = NULL, lastchg = NULL, lastelem;
9111
xmlNodePtr *list = NULL, *lasts = NULL;
9113
if (define->data != NULL) {
9114
partitions = (xmlRelaxNGPartitionPtr) define->data;
9115
nbgroups = partitions->nbgroups;
9117
VALID_ERR(XML_RELAXNG_ERR_INTERNODATA);
9121
* Optimizations for MIXED
9123
oldflags = ctxt->flags;
9124
if (define->dflags & IS_MIXED) {
9125
ctxt->flags |= FLAGS_MIXED_CONTENT;
9126
if (nbgroups == 2) {
9128
* this is a pure <mixed> case
9130
if (ctxt->state != NULL)
9131
ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9133
if (partitions->groups[0]->rule->type == XML_RELAXNG_TEXT)
9134
ret = xmlRelaxNGValidateDefinition(ctxt,
9135
partitions->groups[1]->
9138
ret = xmlRelaxNGValidateDefinition(ctxt,
9139
partitions->groups[0]->
9142
if (ctxt->state != NULL)
9143
ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt,
9147
ctxt->flags = oldflags;
9153
* Build arrays to store the first and last node of the chain
9154
* pertaining to each group
9156
list = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9158
xmlRngVErrMemory(ctxt, "validating\n");
9161
memset(list, 0, nbgroups * sizeof(xmlNodePtr));
9162
lasts = (xmlNodePtr *) xmlMalloc(nbgroups * sizeof(xmlNodePtr));
9163
if (lasts == NULL) {
9164
xmlRngVErrMemory(ctxt, "validating\n");
9167
memset(lasts, 0, nbgroups * sizeof(xmlNodePtr));
9170
* Walk the sequence of children finding the right group and
9171
* sorting them in sequences.
9173
cur = ctxt->state->seq;
9174
cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9176
while (cur != NULL) {
9177
ctxt->state->seq = cur;
9178
if ((partitions->triage != NULL) &&
9179
(partitions->flags & IS_DETERMINIST)) {
9182
if ((cur->type == XML_TEXT_NODE) ||
9183
(cur->type == XML_CDATA_SECTION_NODE)) {
9184
tmp = xmlHashLookup2(partitions->triage, BAD_CAST "#text",
9186
} else if (cur->type == XML_ELEMENT_NODE) {
9187
if (cur->ns != NULL) {
9188
tmp = xmlHashLookup2(partitions->triage, cur->name,
9191
tmp = xmlHashLookup2(partitions->triage,
9196
xmlHashLookup2(partitions->triage, cur->name,
9200
xmlHashLookup2(partitions->triage, BAD_CAST "#any",
9207
i = ((long) tmp) - 1;
9208
if (partitions->flags & IS_NEEDCHECK) {
9209
group = partitions->groups[i];
9210
if (!xmlRelaxNGNodeMatchesList(cur, group->defs))
9215
for (i = 0; i < nbgroups; i++) {
9216
group = partitions->groups[i];
9219
if (xmlRelaxNGNodeMatchesList(cur, group->defs))
9224
* We break as soon as an element not matched is found
9226
if (i >= nbgroups) {
9229
if (lasts[i] != NULL) {
9230
lasts[i]->next = cur;
9236
if (cur->next != NULL)
9237
lastchg = cur->next;
9240
cur = xmlRelaxNGSkipIgnored(ctxt, cur->next);
9243
VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9248
oldstate = ctxt->state;
9249
for (i = 0; i < nbgroups; i++) {
9250
ctxt->state = xmlRelaxNGCopyValidState(ctxt, oldstate);
9251
group = partitions->groups[i];
9252
if (lasts[i] != NULL) {
9253
last = lasts[i]->next;
9254
lasts[i]->next = NULL;
9256
ctxt->state->seq = list[i];
9257
ret = xmlRelaxNGValidateDefinition(ctxt, group->rule);
9260
if (ctxt->state != NULL) {
9261
cur = ctxt->state->seq;
9262
cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9263
xmlRelaxNGFreeValidState(ctxt, oldstate);
9264
oldstate = ctxt->state;
9267
VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9269
ctxt->state = oldstate;
9272
} else if (ctxt->states != NULL) {
9279
* PBM: what happen if there is attributes checks in the interleaves
9282
for (j = 0; j < ctxt->states->nbState; j++) {
9283
cur = ctxt->states->tabState[j]->seq;
9284
cur = xmlRelaxNGSkipIgnored(ctxt, cur);
9287
lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9291
if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9292
/* try to keep the latest one to mach old heuristic */
9293
lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9298
} else if (found == 0) {
9299
if (lowattr == -1) {
9300
lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9303
if (ctxt->states->tabState[j]->nbAttrLeft <= lowattr) {
9304
/* try to keep the latest one to mach old heuristic */
9305
lowattr = ctxt->states->tabState[j]->nbAttrLeft;
9311
* BIG PBM: here we pick only one restarting point :-(
9313
if (ctxt->states->nbState > 0) {
9314
xmlRelaxNGFreeValidState(ctxt, oldstate);
9316
oldstate = ctxt->states->tabState[best];
9317
ctxt->states->tabState[best] = NULL;
9320
ctxt->states->tabState[ctxt->states->nbState - 1];
9321
ctxt->states->tabState[ctxt->states->nbState - 1] = NULL;
9324
for (j = 0; j < ctxt->states->nbState ; j++) {
9325
xmlRelaxNGFreeValidState(ctxt, ctxt->states->tabState[j]);
9327
xmlRelaxNGFreeStates(ctxt, ctxt->states);
9328
ctxt->states = NULL;
9330
VALID_ERR2(XML_RELAXNG_ERR_INTEREXTRA, cur->name);
9332
ctxt->state = oldstate;
9339
if (lasts[i] != NULL) {
9340
lasts[i]->next = last;
9343
if (ctxt->state != NULL)
9344
xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9345
ctxt->state = oldstate;
9346
ctxt->state->seq = lastelem;
9348
VALID_ERR(XML_RELAXNG_ERR_INTERSEQ);
9354
ctxt->flags = oldflags;
9356
* builds the next links chain from the prev one
9359
while (cur != NULL) {
9360
if ((cur == start) || (cur->prev == NULL))
9362
cur->prev->next = cur;
9366
if (ctxt->errNr > errNr)
9367
xmlRelaxNGPopErrors(ctxt, errNr);
9376
* xmlRelaxNGValidateDefinitionList:
9377
* @ctxt: a Relax-NG validation context
9378
* @define: the list of definition to verify
9380
* Validate the given node content against the (list) of definitions
9382
* Returns 0 if the validation succeeded or an error code.
9385
xmlRelaxNGValidateDefinitionList(xmlRelaxNGValidCtxtPtr ctxt,
9386
xmlRelaxNGDefinePtr defines)
9391
if (defines == NULL) {
9392
VALID_ERR2(XML_RELAXNG_ERR_INTERNAL,
9393
BAD_CAST "NULL definition list");
9396
while (defines != NULL) {
9397
if ((ctxt->state != NULL) || (ctxt->states != NULL)) {
9398
res = xmlRelaxNGValidateDefinition(ctxt, defines);
9402
VALID_ERR(XML_RELAXNG_ERR_NOSTATE);
9405
if (res == -1) /* continues on -2 */
9407
defines = defines->next;
9414
* xmlRelaxNGElementMatch:
9415
* @ctxt: a Relax-NG validation context
9416
* @define: the definition to check
9417
* @elem: the element
9419
* Check if the element matches the definition nameClass
9421
* Returns 1 if the element matches, 0 if no, or -1 in case of error
9424
xmlRelaxNGElementMatch(xmlRelaxNGValidCtxtPtr ctxt,
9425
xmlRelaxNGDefinePtr define, xmlNodePtr elem)
9427
int ret = 0, oldflags = 0;
9429
if (define->name != NULL) {
9430
if (!xmlStrEqual(elem->name, define->name)) {
9431
VALID_ERR3(XML_RELAXNG_ERR_ELEMNAME, define->name, elem->name);
9435
if ((define->ns != NULL) && (define->ns[0] != 0)) {
9436
if (elem->ns == NULL) {
9437
VALID_ERR2(XML_RELAXNG_ERR_ELEMNONS, elem->name);
9439
} else if (!xmlStrEqual(elem->ns->href, define->ns)) {
9440
VALID_ERR3(XML_RELAXNG_ERR_ELEMWRONGNS,
9441
elem->name, define->ns);
9444
} else if ((elem->ns != NULL) && (define->ns != NULL) &&
9445
(define->name == NULL)) {
9446
VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, elem->name);
9448
} else if ((elem->ns != NULL) && (define->name != NULL)) {
9449
VALID_ERR2(XML_RELAXNG_ERR_ELEMEXTRANS, define->name);
9453
if (define->nameClass == NULL)
9456
define = define->nameClass;
9457
if (define->type == XML_RELAXNG_EXCEPT) {
9458
xmlRelaxNGDefinePtr list;
9461
oldflags = ctxt->flags;
9462
ctxt->flags |= FLAGS_IGNORABLE;
9465
list = define->content;
9466
while (list != NULL) {
9467
ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9470
ctxt->flags = oldflags;
9475
ctxt->flags = oldflags;
9482
ctxt->flags = oldflags;
9484
} else if (define->type == XML_RELAXNG_CHOICE) {
9485
xmlRelaxNGDefinePtr list;
9488
oldflags = ctxt->flags;
9489
ctxt->flags |= FLAGS_IGNORABLE;
9492
list = define->nameClass;
9493
while (list != NULL) {
9494
ret = xmlRelaxNGElementMatch(ctxt, list, elem);
9497
ctxt->flags = oldflags;
9502
ctxt->flags = oldflags;
9509
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9510
xmlRelaxNGDumpValidError(ctxt);
9512
if (ctxt->errNr > 0)
9513
xmlRelaxNGPopErrors(ctxt, 0);
9518
ctxt->flags = oldflags;
9527
* xmlRelaxNGBestState:
9528
* @ctxt: a Relax-NG validation context
9530
* Find the "best" state in the ctxt->states list of states to report
9531
* errors about. I.e. a state with no element left in the child list
9532
* or the one with the less attributes left.
9533
* This is called only if a falidation error was detected
9535
* Returns the index of the "best" state or -1 in case of error
9538
xmlRelaxNGBestState(xmlRelaxNGValidCtxtPtr ctxt)
9540
xmlRelaxNGValidStatePtr state;
9543
int value = 1000000;
9545
if ((ctxt == NULL) || (ctxt->states == NULL) ||
9546
(ctxt->states->nbState <= 0))
9549
for (i = 0; i < ctxt->states->nbState; i++) {
9550
state = ctxt->states->tabState[i];
9553
if (state->seq != NULL) {
9554
if ((best == -1) || (value > 100000)) {
9559
tmp = state->nbAttrLeft;
9560
if ((best == -1) || (value > tmp)) {
9570
* xmlRelaxNGLogBestError:
9571
* @ctxt: a Relax-NG validation context
9573
* Find the "best" state in the ctxt->states list of states to report
9574
* errors about and log it.
9577
xmlRelaxNGLogBestError(xmlRelaxNGValidCtxtPtr ctxt)
9581
if ((ctxt == NULL) || (ctxt->states == NULL) ||
9582
(ctxt->states->nbState <= 0))
9585
best = xmlRelaxNGBestState(ctxt);
9586
if ((best >= 0) && (best < ctxt->states->nbState)) {
9587
ctxt->state = ctxt->states->tabState[best];
9589
xmlRelaxNGValidateElementEnd(ctxt, 1);
9594
* xmlRelaxNGValidateElementEnd:
9595
* @ctxt: a Relax-NG validation context
9596
* @dolog: indicate that error logging should be done
9598
* Validate the end of the element, implements check that
9599
* there is nothing left not consumed in the element content
9600
* or in the attribute list.
9602
* Returns 0 if the validation succeeded or an error code.
9605
xmlRelaxNGValidateElementEnd(xmlRelaxNGValidCtxtPtr ctxt, int dolog)
9608
xmlRelaxNGValidStatePtr state;
9610
state = ctxt->state;
9611
if (state->seq != NULL) {
9612
state->seq = xmlRelaxNGSkipIgnored(ctxt, state->seq);
9613
if (state->seq != NULL) {
9615
VALID_ERR3(XML_RELAXNG_ERR_EXTRACONTENT,
9616
state->node->name, state->seq->name);
9621
for (i = 0; i < state->nbAttrs; i++) {
9622
if (state->attrs[i] != NULL) {
9624
VALID_ERR3(XML_RELAXNG_ERR_INVALIDATTR,
9625
state->attrs[i]->name, state->node->name);
9634
* xmlRelaxNGValidateState:
9635
* @ctxt: a Relax-NG validation context
9636
* @define: the definition to verify
9638
* Validate the current state against the definition
9640
* Returns 0 if the validation succeeded or an error code.
9643
xmlRelaxNGValidateState(xmlRelaxNGValidCtxtPtr ctxt,
9644
xmlRelaxNGDefinePtr define)
9647
int ret = 0, i, tmp, oldflags, errNr;
9648
xmlRelaxNGValidStatePtr oldstate = NULL, state;
9650
if (define == NULL) {
9651
VALID_ERR(XML_RELAXNG_ERR_NODEFINE);
9655
if (ctxt->state != NULL) {
9656
node = ctxt->state->seq;
9661
for (i = 0; i < ctxt->depth; i++)
9662
xmlGenericError(xmlGenericErrorContext, " ");
9663
xmlGenericError(xmlGenericErrorContext,
9664
"Start validating %s ", xmlRelaxNGDefName(define));
9665
if (define->name != NULL)
9666
xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
9667
if ((node != NULL) && (node->name != NULL))
9668
xmlGenericError(xmlGenericErrorContext, "on %s\n", node->name);
9670
xmlGenericError(xmlGenericErrorContext, "\n");
9673
switch (define->type) {
9674
case XML_RELAXNG_EMPTY:
9675
node = xmlRelaxNGSkipIgnored(ctxt, node);
9678
case XML_RELAXNG_NOT_ALLOWED:
9681
case XML_RELAXNG_TEXT:
9682
while ((node != NULL) &&
9683
((node->type == XML_TEXT_NODE) ||
9684
(node->type == XML_COMMENT_NODE) ||
9685
(node->type == XML_PI_NODE) ||
9686
(node->type == XML_CDATA_SECTION_NODE)))
9688
ctxt->state->seq = node;
9690
case XML_RELAXNG_ELEMENT:
9691
errNr = ctxt->errNr;
9692
node = xmlRelaxNGSkipIgnored(ctxt, node);
9694
VALID_ERR2(XML_RELAXNG_ERR_NOELEM, define->name);
9696
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9697
xmlRelaxNGDumpValidError(ctxt);
9700
if (node->type != XML_ELEMENT_NODE) {
9701
VALID_ERR(XML_RELAXNG_ERR_NOTELEM);
9703
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9704
xmlRelaxNGDumpValidError(ctxt);
9708
* This node was already validated successfully against
9711
if (node->psvi == define) {
9712
ctxt->state->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9713
if (ctxt->errNr > errNr)
9714
xmlRelaxNGPopErrors(ctxt, errNr);
9715
if (ctxt->errNr != 0) {
9716
while ((ctxt->err != NULL) &&
9717
(((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME)
9718
&& (xmlStrEqual(ctxt->err->arg2, node->name)))
9721
XML_RELAXNG_ERR_ELEMEXTRANS)
9722
&& (xmlStrEqual(ctxt->err->arg1, node->name)))
9723
|| (ctxt->err->err == XML_RELAXNG_ERR_NOELEM)
9724
|| (ctxt->err->err ==
9725
XML_RELAXNG_ERR_NOTELEM)))
9726
xmlRelaxNGValidErrorPop(ctxt);
9731
ret = xmlRelaxNGElementMatch(ctxt, define, node);
9734
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9735
xmlRelaxNGDumpValidError(ctxt);
9739
if (ctxt->errNr != 0) {
9740
if (ctxt->errNr > errNr)
9741
xmlRelaxNGPopErrors(ctxt, errNr);
9742
while ((ctxt->err != NULL) &&
9743
(((ctxt->err->err == XML_RELAXNG_ERR_ELEMNAME) &&
9744
(xmlStrEqual(ctxt->err->arg2, node->name))) ||
9745
((ctxt->err->err == XML_RELAXNG_ERR_ELEMEXTRANS) &&
9746
(xmlStrEqual(ctxt->err->arg1, node->name))) ||
9747
(ctxt->err->err == XML_RELAXNG_ERR_NOELEM) ||
9748
(ctxt->err->err == XML_RELAXNG_ERR_NOTELEM)))
9749
xmlRelaxNGValidErrorPop(ctxt);
9751
errNr = ctxt->errNr;
9753
oldflags = ctxt->flags;
9754
if (ctxt->flags & FLAGS_MIXED_CONTENT) {
9755
ctxt->flags -= FLAGS_MIXED_CONTENT;
9757
state = xmlRelaxNGNewValidState(ctxt, node);
9758
if (state == NULL) {
9760
if ((ctxt->flags & FLAGS_IGNORABLE) == 0)
9761
xmlRelaxNGDumpValidError(ctxt);
9765
oldstate = ctxt->state;
9766
ctxt->state = state;
9767
if (define->attrs != NULL) {
9768
tmp = xmlRelaxNGValidateAttributeList(ctxt, define->attrs);
9771
VALID_ERR2(XML_RELAXNG_ERR_ATTRVALID, node->name);
9774
if (define->contModel != NULL) {
9775
xmlRelaxNGValidStatePtr nstate, tmpstate = ctxt->state;
9776
xmlRelaxNGStatesPtr tmpstates = ctxt->states;
9779
nstate = xmlRelaxNGNewValidState(ctxt, node);
9780
ctxt->state = nstate;
9781
ctxt->states = NULL;
9783
tmp = xmlRelaxNGValidateCompiledContent(ctxt,
9786
nseq = ctxt->state->seq;
9787
ctxt->state = tmpstate;
9788
ctxt->states = tmpstates;
9789
xmlRelaxNGFreeValidState(ctxt, nstate);
9791
#ifdef DEBUG_COMPILE
9792
xmlGenericError(xmlGenericErrorContext,
9793
"Validating content of '%s' : %d\n",
9799
if (ctxt->states != NULL) {
9802
for (i = 0; i < ctxt->states->nbState; i++) {
9803
state = ctxt->states->tabState[i];
9804
ctxt->state = state;
9805
ctxt->state->seq = nseq;
9807
if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
9814
* validation error, log the message for the "best" one
9816
ctxt->flags |= FLAGS_IGNORABLE;
9817
xmlRelaxNGLogBestError(ctxt);
9819
for (i = 0; i < ctxt->states->nbState; i++) {
9820
xmlRelaxNGFreeValidState(ctxt,
9824
xmlRelaxNGFreeStates(ctxt, ctxt->states);
9825
ctxt->flags = oldflags;
9826
ctxt->states = NULL;
9827
if ((ret == 0) && (tmp == -1))
9830
state = ctxt->state;
9831
if (ctxt->state != NULL)
9832
ctxt->state->seq = nseq;
9834
ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
9835
xmlRelaxNGFreeValidState(ctxt, state);
9838
if (define->content != NULL) {
9839
tmp = xmlRelaxNGValidateDefinitionList(ctxt,
9844
if (ctxt->state == NULL) {
9845
ctxt->state = oldstate;
9846
VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9850
VALID_ERR2(XML_RELAXNG_ERR_CONTENTVALID,
9856
if (ctxt->states != NULL) {
9859
for (i = 0; i < ctxt->states->nbState; i++) {
9860
state = ctxt->states->tabState[i];
9861
ctxt->state = state;
9863
if (xmlRelaxNGValidateElementEnd(ctxt, 0) == 0) {
9870
* validation error, log the message for the "best" one
9872
ctxt->flags |= FLAGS_IGNORABLE;
9873
xmlRelaxNGLogBestError(ctxt);
9875
for (i = 0; i < ctxt->states->nbState; i++) {
9876
xmlRelaxNGFreeValidState(ctxt,
9880
xmlRelaxNGFreeStates(ctxt, ctxt->states);
9881
ctxt->flags = oldflags;
9882
ctxt->states = NULL;
9883
if ((ret == 0) && (tmp == -1))
9886
state = ctxt->state;
9888
ret = xmlRelaxNGValidateElementEnd(ctxt, 1);
9889
xmlRelaxNGFreeValidState(ctxt, state);
9893
node->psvi = define;
9895
ctxt->flags = oldflags;
9896
ctxt->state = oldstate;
9897
if (oldstate != NULL)
9898
oldstate->seq = xmlRelaxNGSkipIgnored(ctxt, node->next);
9900
if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
9901
xmlRelaxNGDumpValidError(ctxt);
9909
if (ctxt->errNr > errNr)
9910
xmlRelaxNGPopErrors(ctxt, errNr);
9914
xmlGenericError(xmlGenericErrorContext,
9915
"xmlRelaxNGValidateDefinition(): validated %s : %d",
9917
if (oldstate == NULL)
9918
xmlGenericError(xmlGenericErrorContext, ": no state\n");
9919
else if (oldstate->seq == NULL)
9920
xmlGenericError(xmlGenericErrorContext, ": done\n");
9921
else if (oldstate->seq->type == XML_ELEMENT_NODE)
9922
xmlGenericError(xmlGenericErrorContext, ": next elem %s\n",
9923
oldstate->seq->name);
9925
xmlGenericError(xmlGenericErrorContext, ": next %s %d\n",
9926
oldstate->seq->name, oldstate->seq->type);
9929
case XML_RELAXNG_OPTIONAL:{
9930
errNr = ctxt->errNr;
9931
oldflags = ctxt->flags;
9932
ctxt->flags |= FLAGS_IGNORABLE;
9933
oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
9935
xmlRelaxNGValidateDefinitionList(ctxt,
9938
if (ctxt->state != NULL)
9939
xmlRelaxNGFreeValidState(ctxt, ctxt->state);
9940
ctxt->state = oldstate;
9941
ctxt->flags = oldflags;
9943
if (ctxt->errNr > errNr)
9944
xmlRelaxNGPopErrors(ctxt, errNr);
9947
if (ctxt->states != NULL) {
9948
xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9950
ctxt->states = xmlRelaxNGNewStates(ctxt, 1);
9951
if (ctxt->states == NULL) {
9952
xmlRelaxNGFreeValidState(ctxt, oldstate);
9953
ctxt->flags = oldflags;
9955
if (ctxt->errNr > errNr)
9956
xmlRelaxNGPopErrors(ctxt, errNr);
9959
xmlRelaxNGAddStates(ctxt, ctxt->states, oldstate);
9960
xmlRelaxNGAddStates(ctxt, ctxt->states, ctxt->state);
9963
ctxt->flags = oldflags;
9965
if (ctxt->errNr > errNr)
9966
xmlRelaxNGPopErrors(ctxt, errNr);
9969
case XML_RELAXNG_ONEORMORE:
9970
errNr = ctxt->errNr;
9971
ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
9975
if (ctxt->errNr > errNr)
9976
xmlRelaxNGPopErrors(ctxt, errNr);
9977
/* no break on purpose */
9978
case XML_RELAXNG_ZEROORMORE:{
9980
xmlRelaxNGStatesPtr states = NULL, res = NULL;
9983
errNr = ctxt->errNr;
9984
res = xmlRelaxNGNewStates(ctxt, 1);
9990
* All the input states are also exit states
9992
if (ctxt->state != NULL) {
9993
xmlRelaxNGAddStates(ctxt, res,
9994
xmlRelaxNGCopyValidState(ctxt,
9998
for (j = 0; j < ctxt->states->nbState; j++) {
9999
xmlRelaxNGAddStates(ctxt, res,
10000
xmlRelaxNGCopyValidState(ctxt,
10007
oldflags = ctxt->flags;
10008
ctxt->flags |= FLAGS_IGNORABLE;
10011
base = res->nbState;
10013
if (ctxt->states != NULL) {
10014
states = ctxt->states;
10015
for (i = 0; i < states->nbState; i++) {
10016
ctxt->state = states->tabState[i];
10017
ctxt->states = NULL;
10018
ret = xmlRelaxNGValidateDefinitionList(ctxt,
10022
if (ctxt->state != NULL) {
10023
tmp = xmlRelaxNGAddStates(ctxt, res,
10025
ctxt->state = NULL;
10028
} else if (ctxt->states != NULL) {
10029
for (j = 0; j < ctxt->states->nbState;
10032
xmlRelaxNGAddStates(ctxt, res,
10040
xmlRelaxNGFreeStates(ctxt,
10042
ctxt->states = NULL;
10045
if (ctxt->state != NULL) {
10046
xmlRelaxNGFreeValidState(ctxt,
10048
ctxt->state = NULL;
10053
ret = xmlRelaxNGValidateDefinitionList(ctxt,
10057
xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10058
ctxt->state = NULL;
10060
base = res->nbState;
10061
if (ctxt->state != NULL) {
10062
tmp = xmlRelaxNGAddStates(ctxt, res,
10064
ctxt->state = NULL;
10067
} else if (ctxt->states != NULL) {
10068
for (j = 0; j < ctxt->states->nbState; j++) {
10069
tmp = xmlRelaxNGAddStates(ctxt, res,
10076
if (states == NULL) {
10077
states = ctxt->states;
10079
xmlRelaxNGFreeStates(ctxt,
10082
ctxt->states = NULL;
10088
* Collect all the new nodes added at that step
10089
* and make them the new node set
10091
if (res->nbState - base == 1) {
10092
ctxt->state = xmlRelaxNGCopyValidState(ctxt,
10097
if (states == NULL) {
10098
xmlRelaxNGNewStates(ctxt,
10099
res->nbState - base);
10100
states = ctxt->states;
10101
if (states == NULL) {
10106
states->nbState = 0;
10107
for (i = base; i < res->nbState; i++)
10108
xmlRelaxNGAddStates(ctxt, states,
10109
xmlRelaxNGCopyValidState
10111
res->tabState[i]));
10112
ctxt->states = states;
10115
} while (progress == 1);
10116
if (states != NULL) {
10117
xmlRelaxNGFreeStates(ctxt, states);
10119
ctxt->states = res;
10120
ctxt->flags = oldflags;
10123
* errors may have to be propagated back...
10125
if (ctxt->errNr > errNr)
10126
xmlRelaxNGPopErrors(ctxt, errNr);
10131
case XML_RELAXNG_CHOICE:{
10132
xmlRelaxNGDefinePtr list = NULL;
10133
xmlRelaxNGStatesPtr states = NULL;
10135
node = xmlRelaxNGSkipIgnored(ctxt, node);
10137
errNr = ctxt->errNr;
10138
if ((define->dflags & IS_TRIABLE) && (define->data != NULL) &&
10141
* node == NULL can't be optimized since IS_TRIABLE
10142
* doesn't account for choice which may lead to
10145
xmlHashTablePtr triage =
10146
(xmlHashTablePtr) define->data;
10149
* Something we can optimize cleanly there is only one
10150
* possble branch out !
10152
if ((node->type == XML_TEXT_NODE) ||
10153
(node->type == XML_CDATA_SECTION_NODE)) {
10155
xmlHashLookup2(triage, BAD_CAST "#text", NULL);
10156
} else if (node->type == XML_ELEMENT_NODE) {
10157
if (node->ns != NULL) {
10158
list = xmlHashLookup2(triage, node->name,
10162
xmlHashLookup2(triage, BAD_CAST "#any",
10166
xmlHashLookup2(triage, node->name, NULL);
10169
xmlHashLookup2(triage, BAD_CAST "#any",
10172
if (list == NULL) {
10174
VALID_ERR2(XML_RELAXNG_ERR_ELEMWRONG, node->name);
10177
ret = xmlRelaxNGValidateDefinition(ctxt, list);
10183
list = define->content;
10184
oldflags = ctxt->flags;
10185
ctxt->flags |= FLAGS_IGNORABLE;
10187
while (list != NULL) {
10188
oldstate = xmlRelaxNGCopyValidState(ctxt, ctxt->state);
10189
ret = xmlRelaxNGValidateDefinition(ctxt, list);
10191
if (states == NULL) {
10192
states = xmlRelaxNGNewStates(ctxt, 1);
10194
if (ctxt->state != NULL) {
10195
xmlRelaxNGAddStates(ctxt, states, ctxt->state);
10196
} else if (ctxt->states != NULL) {
10197
for (i = 0; i < ctxt->states->nbState; i++) {
10198
xmlRelaxNGAddStates(ctxt, states,
10202
xmlRelaxNGFreeStates(ctxt, ctxt->states);
10203
ctxt->states = NULL;
10206
xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10208
ctxt->state = oldstate;
10211
if (states != NULL) {
10212
xmlRelaxNGFreeValidState(ctxt, oldstate);
10213
ctxt->states = states;
10214
ctxt->state = NULL;
10217
ctxt->states = NULL;
10219
ctxt->flags = oldflags;
10221
if ((ctxt->flags & FLAGS_IGNORABLE) == 0) {
10222
xmlRelaxNGDumpValidError(ctxt);
10225
if (ctxt->errNr > errNr)
10226
xmlRelaxNGPopErrors(ctxt, errNr);
10230
case XML_RELAXNG_DEF:
10231
case XML_RELAXNG_GROUP:
10232
ret = xmlRelaxNGValidateDefinitionList(ctxt, define->content);
10234
case XML_RELAXNG_INTERLEAVE:
10235
ret = xmlRelaxNGValidateInterleave(ctxt, define);
10237
case XML_RELAXNG_ATTRIBUTE:
10238
ret = xmlRelaxNGValidateAttribute(ctxt, define);
10240
case XML_RELAXNG_START:
10241
case XML_RELAXNG_NOOP:
10242
case XML_RELAXNG_REF:
10243
case XML_RELAXNG_EXTERNALREF:
10244
case XML_RELAXNG_PARENTREF:
10245
ret = xmlRelaxNGValidateDefinition(ctxt, define->content);
10247
case XML_RELAXNG_DATATYPE:{
10249
xmlChar *content = NULL;
10252
while (child != NULL) {
10253
if (child->type == XML_ELEMENT_NODE) {
10254
VALID_ERR2(XML_RELAXNG_ERR_DATAELEM,
10255
node->parent->name);
10258
} else if ((child->type == XML_TEXT_NODE) ||
10259
(child->type == XML_CDATA_SECTION_NODE)) {
10260
content = xmlStrcat(content, child->content);
10262
/* TODO: handle entities ... */
10263
child = child->next;
10266
if (content != NULL)
10270
if (content == NULL) {
10271
content = xmlStrdup(BAD_CAST "");
10272
if (content == NULL) {
10273
xmlRngVErrMemory(ctxt, "validating\n");
10278
ret = xmlRelaxNGValidateDatatype(ctxt, content, define,
10281
VALID_ERR2(XML_RELAXNG_ERR_DATATYPE, define->name);
10282
} else if (ret == 0) {
10283
ctxt->state->seq = NULL;
10285
if (content != NULL)
10289
case XML_RELAXNG_VALUE:{
10290
xmlChar *content = NULL;
10295
while (child != NULL) {
10296
if (child->type == XML_ELEMENT_NODE) {
10297
VALID_ERR2(XML_RELAXNG_ERR_VALELEM,
10298
node->parent->name);
10301
} else if ((child->type == XML_TEXT_NODE) ||
10302
(child->type == XML_CDATA_SECTION_NODE)) {
10303
content = xmlStrcat(content, child->content);
10305
/* TODO: handle entities ... */
10306
child = child->next;
10309
if (content != NULL)
10313
if (content == NULL) {
10314
content = xmlStrdup(BAD_CAST "");
10315
if (content == NULL) {
10316
xmlRngVErrMemory(ctxt, "validating\n");
10321
oldvalue = ctxt->state->value;
10322
ctxt->state->value = content;
10323
ret = xmlRelaxNGValidateValue(ctxt, define);
10324
ctxt->state->value = oldvalue;
10326
VALID_ERR2(XML_RELAXNG_ERR_VALUE, define->name);
10327
} else if (ret == 0) {
10328
ctxt->state->seq = NULL;
10330
if (content != NULL)
10334
case XML_RELAXNG_LIST:{
10337
xmlChar *oldvalue, *oldendvalue;
10341
* Make sure it's only text nodes
10346
while (child != NULL) {
10347
if (child->type == XML_ELEMENT_NODE) {
10348
VALID_ERR2(XML_RELAXNG_ERR_LISTELEM,
10349
node->parent->name);
10352
} else if ((child->type == XML_TEXT_NODE) ||
10353
(child->type == XML_CDATA_SECTION_NODE)) {
10354
content = xmlStrcat(content, child->content);
10356
/* TODO: handle entities ... */
10357
child = child->next;
10360
if (content != NULL)
10364
if (content == NULL) {
10365
content = xmlStrdup(BAD_CAST "");
10366
if (content == NULL) {
10367
xmlRngVErrMemory(ctxt, "validating\n");
10372
len = xmlStrlen(content);
10373
oldvalue = ctxt->state->value;
10374
oldendvalue = ctxt->state->endvalue;
10375
ctxt->state->value = content;
10376
ctxt->state->endvalue = content + len;
10377
ret = xmlRelaxNGValidateValue(ctxt, define);
10378
ctxt->state->value = oldvalue;
10379
ctxt->state->endvalue = oldendvalue;
10381
VALID_ERR(XML_RELAXNG_ERR_LIST);
10382
} else if ((ret == 0) && (node != NULL)) {
10383
ctxt->state->seq = node->next;
10385
if (content != NULL)
10389
case XML_RELAXNG_EXCEPT:
10390
case XML_RELAXNG_PARAM:
10396
for (i = 0; i < ctxt->depth; i++)
10397
xmlGenericError(xmlGenericErrorContext, " ");
10398
xmlGenericError(xmlGenericErrorContext,
10399
"Validating %s ", xmlRelaxNGDefName(define));
10400
if (define->name != NULL)
10401
xmlGenericError(xmlGenericErrorContext, "%s ", define->name);
10403
xmlGenericError(xmlGenericErrorContext, "suceeded\n");
10405
xmlGenericError(xmlGenericErrorContext, "failed\n");
10411
* xmlRelaxNGValidateDefinition:
10412
* @ctxt: a Relax-NG validation context
10413
* @define: the definition to verify
10415
* Validate the current node lists against the definition
10417
* Returns 0 if the validation succeeded or an error code.
10420
xmlRelaxNGValidateDefinition(xmlRelaxNGValidCtxtPtr ctxt,
10421
xmlRelaxNGDefinePtr define)
10423
xmlRelaxNGStatesPtr states, res;
10424
int i, j, k, ret, oldflags;
10427
* We should NOT have both ctxt->state and ctxt->states
10429
if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10430
TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10431
ctxt->state = NULL;
10434
if ((ctxt->states == NULL) || (ctxt->states->nbState == 1)) {
10435
if (ctxt->states != NULL) {
10436
ctxt->state = ctxt->states->tabState[0];
10437
xmlRelaxNGFreeStates(ctxt, ctxt->states);
10438
ctxt->states = NULL;
10440
ret = xmlRelaxNGValidateState(ctxt, define);
10441
if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10442
TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10443
ctxt->state = NULL;
10445
if ((ctxt->states != NULL) && (ctxt->states->nbState == 1)) {
10446
ctxt->state = ctxt->states->tabState[0];
10447
xmlRelaxNGFreeStates(ctxt, ctxt->states);
10448
ctxt->states = NULL;
10453
states = ctxt->states;
10454
ctxt->states = NULL;
10457
oldflags = ctxt->flags;
10458
ctxt->flags |= FLAGS_IGNORABLE;
10459
for (i = 0; i < states->nbState; i++) {
10460
ctxt->state = states->tabState[i];
10461
ctxt->states = NULL;
10462
ret = xmlRelaxNGValidateState(ctxt, define);
10464
* We should NOT have both ctxt->state and ctxt->states
10466
if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10467
TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10468
ctxt->state = NULL;
10471
if (ctxt->states == NULL) {
10473
/* add the state to the container */
10474
xmlRelaxNGAddStates(ctxt, res, ctxt->state);
10475
ctxt->state = NULL;
10477
/* add the state directly in states */
10478
states->tabState[j++] = ctxt->state;
10479
ctxt->state = NULL;
10483
/* make it the new container and copy other results */
10484
res = ctxt->states;
10485
ctxt->states = NULL;
10486
for (k = 0; k < j; k++)
10487
xmlRelaxNGAddStates(ctxt, res,
10488
states->tabState[k]);
10490
/* add all the new results to res and reff the container */
10491
for (k = 0; k < ctxt->states->nbState; k++)
10492
xmlRelaxNGAddStates(ctxt, res,
10493
ctxt->states->tabState[k]);
10494
xmlRelaxNGFreeStates(ctxt, ctxt->states);
10495
ctxt->states = NULL;
10499
if (ctxt->state != NULL) {
10500
xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10501
ctxt->state = NULL;
10502
} else if (ctxt->states != NULL) {
10503
for (k = 0; k < ctxt->states->nbState; k++)
10504
xmlRelaxNGFreeValidState(ctxt,
10505
ctxt->states->tabState[k]);
10506
xmlRelaxNGFreeStates(ctxt, ctxt->states);
10507
ctxt->states = NULL;
10511
ctxt->flags = oldflags;
10513
xmlRelaxNGFreeStates(ctxt, states);
10514
ctxt->states = res;
10516
} else if (j > 1) {
10517
states->nbState = j;
10518
ctxt->states = states;
10520
} else if (j == 1) {
10521
ctxt->state = states->tabState[0];
10522
xmlRelaxNGFreeStates(ctxt, states);
10526
xmlRelaxNGFreeStates(ctxt, states);
10527
if (ctxt->states != NULL) {
10528
xmlRelaxNGFreeStates(ctxt, ctxt->states);
10529
ctxt->states = NULL;
10532
if ((ctxt->state != NULL) && (ctxt->states != NULL)) {
10533
TODO xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10534
ctxt->state = NULL;
10540
* xmlRelaxNGValidateDocument:
10541
* @ctxt: a Relax-NG validation context
10542
* @doc: the document
10544
* Validate the given document
10546
* Returns 0 if the validation succeeded or an error code.
10549
xmlRelaxNGValidateDocument(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10552
xmlRelaxNGPtr schema;
10553
xmlRelaxNGGrammarPtr grammar;
10554
xmlRelaxNGValidStatePtr state;
10557
if ((ctxt == NULL) || (ctxt->schema == NULL) || (doc == NULL))
10560
ctxt->errNo = XML_RELAXNG_OK;
10561
schema = ctxt->schema;
10562
grammar = schema->topgrammar;
10563
if (grammar == NULL) {
10564
VALID_ERR(XML_RELAXNG_ERR_NOGRAMMAR);
10567
state = xmlRelaxNGNewValidState(ctxt, NULL);
10568
ctxt->state = state;
10569
ret = xmlRelaxNGValidateDefinition(ctxt, grammar->start);
10570
if ((ctxt->state != NULL) && (state->seq != NULL)) {
10571
state = ctxt->state;
10573
node = xmlRelaxNGSkipIgnored(ctxt, node);
10574
if (node != NULL) {
10576
VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10580
} else if (ctxt->states != NULL) {
10584
for (i = 0; i < ctxt->states->nbState; i++) {
10585
state = ctxt->states->tabState[i];
10587
node = xmlRelaxNGSkipIgnored(ctxt, node);
10590
xmlRelaxNGFreeValidState(ctxt, state);
10594
VALID_ERR(XML_RELAXNG_ERR_EXTRADATA);
10599
if (ctxt->state != NULL) {
10600
xmlRelaxNGFreeValidState(ctxt, ctxt->state);
10601
ctxt->state = NULL;
10604
xmlRelaxNGDumpValidError(ctxt);
10606
else if (ctxt->errNr != 0) {
10607
ctxt->error(ctxt->userData,
10608
"%d Extra error messages left on stack !\n",
10610
xmlRelaxNGDumpValidError(ctxt);
10613
#ifdef LIBXML_VALID_ENABLED
10614
if (ctxt->idref == 1) {
10615
xmlValidCtxt vctxt;
10617
memset(&vctxt, 0, sizeof(xmlValidCtxt));
10619
vctxt.error = ctxt->error;
10620
vctxt.warning = ctxt->warning;
10621
vctxt.userData = ctxt->userData;
10623
if (xmlValidateDocumentFinal(&vctxt, doc) != 1)
10626
#endif /* LIBXML_VALID_ENABLED */
10627
if ((ret == 0) && (ctxt->errNo != XML_RELAXNG_OK))
10633
/************************************************************************
10635
* Validation interfaces *
10637
************************************************************************/
10640
* xmlRelaxNGNewValidCtxt:
10641
* @schema: a precompiled XML RelaxNGs
10643
* Create an XML RelaxNGs validation context based on the given schema
10645
* Returns the validation context or NULL in case of error
10647
xmlRelaxNGValidCtxtPtr
10648
xmlRelaxNGNewValidCtxt(xmlRelaxNGPtr schema)
10650
xmlRelaxNGValidCtxtPtr ret;
10652
ret = (xmlRelaxNGValidCtxtPtr) xmlMalloc(sizeof(xmlRelaxNGValidCtxt));
10654
xmlRngVErrMemory(NULL, "building context\n");
10657
memset(ret, 0, sizeof(xmlRelaxNGValidCtxt));
10658
ret->schema = schema;
10659
ret->error = xmlGenericError;
10660
ret->userData = xmlGenericErrorContext;
10664
ret->errTab = NULL;
10665
if (schema != NULL)
10666
ret->idref = schema->idref;
10667
ret->states = NULL;
10668
ret->freeState = NULL;
10669
ret->freeStates = NULL;
10670
ret->errNo = XML_RELAXNG_OK;
10675
* xmlRelaxNGFreeValidCtxt:
10676
* @ctxt: the schema validation context
10678
* Free the resources associated to the schema validation context
10681
xmlRelaxNGFreeValidCtxt(xmlRelaxNGValidCtxtPtr ctxt)
10687
if (ctxt->states != NULL)
10688
xmlRelaxNGFreeStates(NULL, ctxt->states);
10689
if (ctxt->freeState != NULL) {
10690
for (k = 0; k < ctxt->freeState->nbState; k++) {
10691
xmlRelaxNGFreeValidState(NULL, ctxt->freeState->tabState[k]);
10693
xmlRelaxNGFreeStates(NULL, ctxt->freeState);
10695
if (ctxt->freeStates != NULL) {
10696
for (k = 0; k < ctxt->freeStatesNr; k++) {
10697
xmlRelaxNGFreeStates(NULL, ctxt->freeStates[k]);
10699
xmlFree(ctxt->freeStates);
10701
if (ctxt->errTab != NULL)
10702
xmlFree(ctxt->errTab);
10703
if (ctxt->elemTab != NULL) {
10704
xmlRegExecCtxtPtr exec;
10706
exec = xmlRelaxNGElemPop(ctxt);
10707
while (exec != NULL) {
10708
xmlRegFreeExecCtxt(exec);
10709
exec = xmlRelaxNGElemPop(ctxt);
10711
xmlFree(ctxt->elemTab);
10717
* xmlRelaxNGSetValidErrors:
10718
* @ctxt: a Relax-NG validation context
10719
* @err: the error function
10720
* @warn: the warning function
10721
* @ctx: the functions context
10723
* Set the error and warning callback informations
10726
xmlRelaxNGSetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10727
xmlRelaxNGValidityErrorFunc err,
10728
xmlRelaxNGValidityWarningFunc warn, void *ctx)
10733
ctxt->warning = warn;
10734
ctxt->userData = ctx;
10735
ctxt->serror = NULL;
10739
* xmlRelaxNGSetValidStructuredErrors:
10740
* @ctxt: a Relax-NG validation context
10741
* @serror: the structured error function
10742
* @ctx: the functions context
10744
* Set the structured error callback
10747
xmlRelaxNGSetValidStructuredErrors(xmlRelaxNGValidCtxtPtr ctxt,
10748
xmlStructuredErrorFunc serror, void *ctx)
10752
ctxt->serror = serror;
10753
ctxt->error = NULL;
10754
ctxt->warning = NULL;
10755
ctxt->userData = ctx;
10759
* xmlRelaxNGGetValidErrors:
10760
* @ctxt: a Relax-NG validation context
10761
* @err: the error function result
10762
* @warn: the warning function result
10763
* @ctx: the functions context result
10765
* Get the error and warning callback informations
10767
* Returns -1 in case of error and 0 otherwise
10770
xmlRelaxNGGetValidErrors(xmlRelaxNGValidCtxtPtr ctxt,
10771
xmlRelaxNGValidityErrorFunc * err,
10772
xmlRelaxNGValidityWarningFunc * warn, void **ctx)
10777
*err = ctxt->error;
10779
*warn = ctxt->warning;
10781
*ctx = ctxt->userData;
10786
* xmlRelaxNGValidateDoc:
10787
* @ctxt: a Relax-NG validation context
10788
* @doc: a parsed document tree
10790
* Validate a document tree in memory.
10792
* Returns 0 if the document is valid, a positive error code
10793
* number otherwise and -1 in case of internal or API error.
10796
xmlRelaxNGValidateDoc(xmlRelaxNGValidCtxtPtr ctxt, xmlDocPtr doc)
10800
if ((ctxt == NULL) || (doc == NULL))
10805
ret = xmlRelaxNGValidateDocument(ctxt, doc);
10807
* TODO: build error codes
10814
#define bottom_relaxng
10815
#include "elfgcchack.h"
10816
#endif /* LIBXML_SCHEMAS_ENABLED */